libtcspc.Graph¶
- class libtcspc.Graph[source]¶
Bases:
objectProcessing graph.
A processing graph is a directed acyclic graph of
Nodeobjects. Each node is also assigned a name for later reference and retrieval.Edges of the graph connect a specific output port of one node to a specific input port of another (in that direction). Any given input or output port may have at most a single edge connected to it.
At any point in time, the graph may contain nodes with unconnected input or output ports. These ports are the input and output ports of the graph as a whole.
Note: It is not possible to add a connection between a graph input and a graph output that does not have an intermediate node. But this can be worked around by using a SelectAll (i.e., no-op) node.
- add_chain(nodes, *, upstream=None, downstream=None)[source]¶
Add a chain of single-input single-output nodes connected in series.
The last node may be a sink — that is, it may have zero outputs in addition to its single input. The first and middle nodes must have exactly one input and one output.
- Parameters:
nodes (Sequence[tuple[str, Node] or Node]) – The nodes to add, in order. Each element is either a
(name, node)tuple or a bareNode; in the latter case a name is auto-generated as byadd_node.upstream (tuple[str, str] or str or None) – If given, connect
upstreamto the first added node’s input port. The shorthand rules ofadd_nodeapply.downstream (tuple[str, str] or str or None) – If given, connect the last added node’s output port to
downstream. The shorthand rules ofadd_nodeapply. Must beNoneif the last node is a sink (zero outputs).
- Raises:
ValueError – If the first or any middle node does not have exactly one input and one output; if the last node does not have exactly one input and (zero or one) outputs; if
downstreamis given but the last node has zero outputs; or if any requested connection fails.- Return type:
None
Notes
If
nodesis empty and bothupstreamanddownstreamare given,upstreamis connected directly todownstream.If any requested connection or node fails, all nodes and connections made by the call are removed and the exception propagates, leaving the graph unchanged.
- add_node(name, node, *, upstream=None, downstream=None)[source]¶
Add a node to the graph, optionally connecting it to an existing node.
- Parameters:
name (str or None) – Name under which the node will be retrievable. If
None, an auto-generated name of the form"{ClassName}-{N}"is assigned, whereNis the smallest non-negative integer making the name unique.node (Node) – The node to add.
upstream (tuple[str, str] or str or Mapping or None) – If given, connect
upstreamto the new node’s"input"port. A(node_name, port_name)tuple selects an explicit output port; a bare node name defaults to its"output"port. May also be a mapping from this node’s input port names to connection sources, connecting several inputs at once; each value follows the same shorthand (bare name → that node’s"output"port, or an explicit(name, port)tuple).downstream (tuple[str, str] or str or Mapping or None) – If given, connect the new node’s
"output"port todownstream. A(node_name, port_name)tuple selects an explicit input port; a bare node name defaults to its"input"port. May also be a mapping from this node’s output port names to connection targets, connecting several outputs at once; each value follows the same shorthand (bare name → that node’s"input"port, or an explicit(name, port)tuple).
- Returns:
The name under which the node was added (the auto-generated name if
namewasNone, otherwisename).- Return type:
- Raises:
ValueError – If
namealready exists in the graph, if a requested connection’s port types do not match, or if a requested connection would introduce a cycle.
Notes
If any requested connection fails, the node and all connections made by the call are removed and the exception propagates, leaving the graph unchanged.
- connect(producer, consumer)[source]¶
Connect an output port to an input port.
- Parameters:
producer (tuple[str, str] or str) – A
(node_name, port_name)tuple selecting the producing node and its output port. A bare node name is shorthand for(name, "output").consumer (tuple[str, str] or str) – A
(node_name, port_name)tuple selecting the consuming node and its input port. A bare node name is shorthand for(name, "input").
- Raises:
ValueError – If either port is already connected, if the producer’s output event set is not compatible with the consumer’s input, or if the connection would introduce a cycle.
- Return type:
None
Notes
Validation is performed immediately. If the connection is rejected, the graph is restored to its previous state before the exception propagates.
- to_graphviz(*, flatten=False)[source]¶
Return a Graphviz DOT representation of the graph for visualization.
The output is intended for debugging and visualization only. The exact format is not stable and should not be consumed programmatically.
Nodes correspond 1:1 to the Python
Nodeobjects in the graph; any expansion that occurs during C++ code generation is not reflected. Partially-built graphs (unconnected ports, etc.) are supported — no validation is performed.Edges are colored by thread region: all edges driven on the same thread share one color, and a
BufferorRealTimeBufferboundary introduces a new color for the events it re-emits on its pump thread (so the producer and consumer halves are visually distinct). A node whose inputs illegally arrive on different threads — for example aMergefed through a buffer on only one branch — is outlined in red. Like the rest of the output, the specific colors and this scheme may change without notice in future versions.- Parameters:
flatten (bool, optional) – If
False(the default), eachSubgraphis rendered as a Graphvizcluster_…subgraph surrounding its inner nodes. IfTrue,Subgraphboundaries are not drawn and inner nodes appear directly in their containing graph.- Returns:
A complete
digraph { ... }block in Graphviz DOT format.- Return type: