Repository navigation
Graph Utilities API
The main utility functions of CSP are documented here. These functions enable users to execute and visualize graphs.
csp.run(
g,
*args,
starttime=None,
endtime=MAX_END_TIME,
realtime=False,
output_numpy=False,
**kwargs,
):Runs the graph g from starttime to endtime.
Args:
-
g: a graph function or CSP node. -
args: any positional arguments passed tog. -
starttime: the engine time that the graph will start at. -
endtime: the engine time that the graph will end at. Can be adatetimeor atimedelta. If atimedelta, the end time is treated as an offset from the start time. -
realtime: whether the graph runs in realtime versus historical mode. Defaults to historical. -
output_numpy: if True, will return each output ofgas a two element tuple of Numpy arrays, one for the times and one for the values. -
kwargs: any keyword arguments passed tog.
Returns the outputs of g in a dictionary where the key is the output name and the value is a list of tuples. Each tuple has two elements, the time of the event and its value.
def run_on_thread(
g,
*args,
starttime=None,
endtime=MAX_END_TIME,
queue_wait_time=None,
realtime=False,
auto_shutdown=False,
daemon=False,
**kwargs,
):Runs the graph g on a separate thread.
Args:
See csp.run for overlapping arguments.
-
auto_shutdown: if True, the graph will automatically stop and join when the runner goes out scope. Defaults to False. -
daemon: if True, will launch the thread as a daemon. Defaults to False.
Returns a runner object, which has the following methods:
def join(self, suppress=False):
"""wait for engine thread to finish and return results. If suppress=True, will suppress exceptions."""
def is_alive(self):
"""Checks whether the thread is still running"""
def stop_engine(self):
"""request engine to stop ( async )"""csp.is_configured_realtime()Returns the realtime flag passed to csp.run, csp.run_on_thread, or csp.build_graph (default False). Takes no arguments and raises RuntimeError outside a graph run/build context.
During graph construction, this flag can select historical or live input adapters. It does not report the engine's current phase: a realtime-configured run can replay history first. Inside a node, csp.in_realtime() reports that phase. See Execution Modes.
This example uses constants in place of adapters:
from datetime import datetime, timedelta
import csp
def graph():
if csp.is_configured_realtime():
return csp.const("live")
return csp.const("historical")
start = datetime(2024, 1, 1)
for realtime, expected in ((False, "historical"), (True, "live")):
result = csp.run(graph, starttime=start, endtime=timedelta(0), realtime=realtime)
assert result[0] == [(start, expected)]Both runs end at the historical start time. The second selects "live" because its configured mode is realtime, even though no realtime processing occurs.
def show_graph(
graph_func,
*args,
graph_filename=None,
**kwargs,
):Displays the graph using graphviz.
Args:
-
graph_func: a graph function or CSP node. -
args: any positional arguments passed tograph_func. -
graph_filename: a file path to save the image. Default format ispngbutjpgand others are also supported. -
kwargs: any keyword arguments passed tograph_func.
Returns None.
This wiki is autogenerated. To made updates, open a PR against the original source file in docs/wiki.
Get Started (Tutorials)
Concepts
- CSP Node
- CSP Graph
- Historical Buffers
- Execution Modes
- Adapters
- Feedback and Delayed Edge
- Common Mistakes / FAQ
How-to guides
- Use Statistical Nodes
- Create Dynamic Baskets
- Write Adapters:
- Profile CSP Code
References
- API Reference
- Glossary of Terms
- Examples
Developer Guide