Skip to content

contango.trading.analysis.data.graph_runner

contango.trading.analysis.data.graph_runner

GraphRunner

GraphRunner(specs: list[GraphSpec] = DEFAULT_GRAPH_SPECS, template: Template | None = DARK_GRAY_TEMPLATE)

Manages a set of graph specs, instantiating them once the experiment DataFrame is available.

Initializes GraphRunner.

Parameters:

Name Type Description Default
specs list[GraphSpec]

A list of specs for each graph type.

DEFAULT_GRAPH_SPECS
template Template | None

The plotly template for all graphs to follow.

DARK_GRAY_TEMPLATE
Source code in src\contango\trading\analysis\data\graph_runner.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def __init__(
    self, 
    specs: list[GraphSpec] = DEFAULT_GRAPH_SPECS,
    template: go.layout.Template | None = DARK_GRAY_TEMPLATE
) -> None:
    """
    Initializes `GraphRunner`.

    Args:
        specs: A list of specs for each graph type.
        template: The plotly template for all graphs to follow.
    """
    self._specs: list[GraphSpec] = list(specs)
    self._graphs: list[GraphType[Any]] = []
    self._template = template

graphs property

graphs: list[GraphType[Any]]

The currently active graph instances (empty until build_all has run at least once, since some configs depend on the data).

add_graph

add_graph(name: str, config: Union[GraphConfig, GraphConfigFactory]) -> None

Registers a graph spec to be instantiated on the next build_all.

Source code in src\contango\trading\analysis\data\graph_runner.py
124
125
126
127
128
129
130
def add_graph(self, name: str, config: Union[GraphConfig, GraphConfigFactory]) -> None:
    """
    Registers a graph spec to be instantiated on the next `build_all`.
    """
    if name not in GRAPH_REGISTRY:
        raise KeyError(f"No graph registered under '{name}'. Known: {list(GRAPH_REGISTRY)}")
    self._specs.append(GraphSpec(name, config))

build_all

build_all(experiments: list[GraphingExperiment]) -> dict[str, go.Figure]

Resolves every spec's config against the data, instantiates the graphs, and builds all of their figures.

Source code in src\contango\trading\analysis\data\graph_runner.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def build_all(self, experiments: list[GraphingExperiment]) -> dict[str, go.Figure]:
    """
    Resolves every spec's config against the data, instantiates the
    graphs, and builds all of their figures.
    """
    df = results_to_dataframe(experiments)

    self._graphs = []
    for spec in self._specs:
        config = _resolve(spec.config, df)
        graph_cls = GRAPH_REGISTRY[spec.name]
        self._graphs.append(graph_cls(config))

    figures = {graph.graph_name: graph.build(df) for graph in self._graphs}

    if self._template is not None:
        for fig in figures.values():
            fig.update_layout(template=self._template)  # type: ignore[unknownMemberType]

    return figures

remove_by_name

remove_by_name(name: str) -> None

Removes all graphs with the same name.

Source code in src\contango\trading\analysis\data\graph_runner.py
139
140
141
142
143
144
def remove_by_name(self, name: str) -> None:
    """
    Removes all graphs with the same name.
    """
    self._specs = [s for s in self._specs if s.name != name]
    self._graphs = [g for g in self._graphs if g.graph_name != name]

remove_graph

remove_graph(graph: GraphType[Any]) -> None

Removes all graphs by instance.

Source code in src\contango\trading\analysis\data\graph_runner.py
132
133
134
135
136
137
def remove_graph(self, graph: GraphType[Any]) -> None:
    """
    Removes all graphs by instance.
    """
    self._graphs.remove(graph)
    self._specs = [s for s in self._specs if s.name != graph.graph_name]

GraphSpec dataclass

GraphSpec(name: str, config: Union[GraphConfig, GraphConfigFactory])

A named graph type paired with its config — either a concrete config, or a factory that builds one once the experiment DataFrame is available.