colour.utilities.PortGraph#
- class colour.utilities.PortGraph(*args: Any, **kwargs: Any)[source]#
Bases:
PortNode
Define a node-graph for
colour.utilities.PortNode
class instances.Attributes
Methods
Examples
>>> class NodeAdd(PortNode): ... def __init__(self, *args, **kwargs): ... super().__init__(*args, **kwargs) ... ... self.description = "Perform the addition of the two input port values." ... ... self.add_input_port("a") ... self.add_input_port("b") ... self.add_output_port("output") ... ... def process(self): ... a = self.get_input("a") ... b = self.get_input("b") ... ... if a is None or b is None: ... return ... ... self._output_ports["output"].value = a + b ... ... self.dirty = False >>> node_1 = NodeAdd() >>> node_1.set_input("a", 1) >>> node_1.set_input("b", 1) >>> node_2 = NodeAdd() >>> node_1.connect("output", node_2, "a") >>> node_2.set_input("b", 1) >>> graph = PortGraph() >>> graph.add_node(node_1) >>> graph.add_node(node_2) >>> graph.nodes {'NodeAdd#...': <...NodeAdd object at 0x...>, 'NodeAdd#...': <...NodeAdd object at 0x...>} >>> graph.process() >>> node_2.get_output("output") 3
Return a new instance of the
colour.utilities.Node
class.- Parameters:
- property nodes: Dict[str, PortNode]#
Getter property for the node-graph nodes.
- Returns:
Node-graph nodes.
- Return type:
- __str__() str [source]#
Return a formatted string representation of the node-graph.
- Returns:
Formatted string representation.
- Return type:
class`str`
- add_node(node: PortNode) None [source]#
Add given node to the node-graph.
- Parameters:
node (PortNode) – Node to add to the node-graph.
- Raises:
AsssertionError – If the node is not a
colour.utilities.PortNode
class instance.- Return type:
None
Examples
>>> node_1 = PortNode() >>> node_2 = PortNode() >>> graph = PortGraph() >>> graph.nodes {} >>> graph.add_node(node_1) >>> graph.nodes {'PortNode#...': <...PortNode object at 0x...>} >>> graph.add_node(node_2) >>> graph.nodes {'PortNode#...': <...PortNode object at 0x...>, 'PortNode#...': <...PortNode object at 0x...>}
- remove_node(node: PortNode) None [source]#
Remove given node from the node-graph.
The node input and output ports will be disconnected from all their connections.
- Parameters:
node (PortNode) – Node to remove from the node-graph.
- Raises:
AsssertionError – If the node is not a member of the node-graph.
- Return type:
None
Examples
>>> node_1 = PortNode() >>> node_2 = PortNode() >>> graph = PortGraph() >>> graph.add_node(node_1) >>> graph.add_node(node_2) >>> graph.nodes {'PortNode#...': <...PortNode object at 0x...>, 'PortNode#...': <...PortNode object at 0x...>} >>> graph.remove_node(node_2) >>> graph.nodes {'PortNode#...': <...PortNode object at 0x...>} >>> graph.remove_node(node_1) >>> graph.nodes {}
- walk_ports() Generator [source]#
Return a generator used to walk into the node-graph.
The node is walked according to a topological sorted order. A topological sort is a non-unique permutation of the nodes of a directed graph such that an edge from \(u\) to \(v\) implies that \(u\) appears before \(v\) in the topological sort order. This ordering is valid only if the graph has no directed cycles.
To walk the node-graph, an NetworkX graph is constructed by connecting the ports together and in turn connecting them to the nodes.
- Yields:
Generator – Node-graph walker.
- Return type:
Examples
>>> node_1 = PortNode() >>> port = node_1.add_output_port("output") >>> node_2 = PortNode() >>> port = node_2.add_input_port("a") >>> graph = PortGraph() >>> graph.add_node(node_1) >>> graph.add_node(node_2) >>> node_1.connect("output", node_2, "a") >>> list(graph.walk_ports()) [<...PortNode object at 0x...>, <...PortNode object at 0x...>]
- process(**kwargs: Dict) None [source]#
Process the node-graph by walking it and calling the
colour.utilities.PortNode.process()
method.- Parameters:
kwargs (Dict) – Keyword arguments.
- Return type:
None
Examples
>>> class NodeAdd(PortNode): ... def __init__(self, *args, **kwargs): ... super().__init__(*args, **kwargs) ... ... self.description = ( ... "Perform the addition of the two input port values." ... ) ... ... self.add_input_port("a") ... self.add_input_port("b") ... self.add_output_port("output") ... ... def process(self): ... a = self.get_input("a") ... b = self.get_input("b") ... ... if a is None or b is None: ... return ... ... self._output_ports["output"].value = a + b ... ... self.dirty = False >>> node_1 = NodeAdd() >>> node_1.set_input("a", 1) >>> node_1.set_input("b", 1) >>> node_2 = NodeAdd() >>> node_1.connect("output", node_2, "a") >>> node_2.set_input("b", 1) >>> graph = PortGraph() >>> graph.add_node(node_1) >>> graph.add_node(node_2) >>> graph.nodes {'NodeAdd#...': <...NodeAdd object at 0x...>, 'NodeAdd#...': <...NodeAdd object at 0x...>} >>> graph.process() >>> node_2.get_output("output") 3 >>> node_2.dirty False
- to_graphviz() Dot [source]#
Return a visualisation node-graph for Graphviz.
- Returns:
Pydot graph.
- Return type:
pydot.Dot
Examples
>>> node_1 = PortNode() >>> port = node_1.add_output_port("output") >>> node_2 = PortNode() >>> port = node_2.add_input_port("a") >>> graph = PortGraph() >>> graph.add_node(node_1) >>> graph.add_node(node_2) >>> node_1.connect("output", node_2, "a") >>> graph.to_graphviz() <pydot.core.Dot object at 0x...>