Graph

class golem.core.dag.graph.Graph[source]

Bases: abc.ABC

Defines abstract graph interface that’s required by graph optimisation process.

abstract add_node(node: golem.core.dag.graph_node.GraphNode)[source]

Adds new node to the graph together with its parent nodes.

Parameters

node – graph nodes

abstract update_node(old_node: golem.core.dag.graph_node.GraphNode, new_node: golem.core.dag.graph_node.GraphNode)[source]

Replaces old_node node with new_node

Parameters
  • old_node – node to be replaced

  • new_node – node to be placed instead

abstract update_subtree(old_subtree: golem.core.dag.graph_node.GraphNode, new_subtree: golem.core.dag.graph_node.GraphNode)[source]

Changes old_subtree subtree to new_subtree

Parameters
  • old_subtree – node and its subtree to be removed

  • new_subtree – node and its subtree to be placed instead

abstract delete_node(node: golem.core.dag.graph_node.GraphNode, reconnect: golem.core.dag.graph.ReconnectType = ReconnectType.single)[source]

Removes node from the graph. If node has only one child, then connects all of the node parents to it.

Parameters
  • node – node of the graph to be deleted

  • reconnect – defines how to treat left edges between parents and children

abstract delete_subtree(subtree: golem.core.dag.graph_node.GraphNode)[source]

Deletes given node with all its parents. Deletes all edges from removed nodes to remaining graph nodes

Parameters

subtree – node to be deleted with all of its parents and their connections amongst the remaining graph nodes

abstract node_children(node: golem.core.dag.graph_node.GraphNode) Sequence[Optional[golem.core.dag.graph_node.GraphNode]][source]

Returns all children of the node

Parameters

node – for getting children from

Returns: children of the node

abstract connect_nodes(node_parent: golem.core.dag.graph_node.GraphNode, node_child: golem.core.dag.graph_node.GraphNode)[source]

Adds edge between parent and child

Parameters
  • node_parent – acts like parent in graph connection relations

  • node_child – acts like child in graph connection relations

abstract disconnect_nodes(node_parent: golem.core.dag.graph_node.GraphNode, node_child: golem.core.dag.graph_node.GraphNode, clean_up_leftovers: bool = False)[source]

Removes an edge between two nodes

Parameters
  • node_parent – where the removing edge comes out

  • node_child – where the removing edge enters

  • clean_up_leftovers – whether to remove the remaining invalid vertices with edges or not

abstract get_edges() Sequence[Tuple[golem.core.dag.graph_node.GraphNode, golem.core.dag.graph_node.GraphNode]][source]

Gets all available edges in this graph

Returns

pairs of parent_node -> child_node

get_nodes_by_name(name: str) List[golem.core.dag.graph_node.GraphNode][source]

Returns list of nodes with the required name

Parameters

name – name to filter by

Returns

relevant nodes (empty if there are no such nodes)

Return type

list

get_node_by_uid(uid: str) Optional[golem.core.dag.graph_node.GraphNode][source]

Returns node with the required uid

Parameters

uid – uid of node to filter by

Returns

relevant node (None if there is no such node)

Return type

Optional[Node]

abstract __eq__(other_graph: golem.core.dag.graph.Graph) bool[source]

Compares this graph with the other_graph

Parameters

other_graph – another graph

Returns

is it equal to other_graph in terms of the graphs

property root_node: Union[golem.core.dag.graph_node.GraphNode, Sequence[golem.core.dag.graph_node.GraphNode]]

Gets the final layer node(s) of the graph

Returns

the final layer node(s)

abstract property nodes: List[golem.core.dag.graph_node.GraphNode]

Return list of all graph nodes

Returns

graph nodes

abstract property depth: int

Gets this graph depth from its sink-node to its source-node

Returns

length of a path from the root node to the farthest primary node

property length: int

Return size of the graph (number of nodes)

Returns

graph size

show(save_path: Optional[Union[os.PathLike, str]] = None, engine: Optional[str] = None, node_color: Optional[Union[str, Sequence[float], Dict[str, Union[str, Sequence[float]]], Callable[[Iterable[str]], Dict[str, Union[str, Sequence[float]]]]]] = None, dpi: Optional[int] = None, node_size_scale: Optional[float] = None, font_size_scale: Optional[float] = None, edge_curvature_scale: Optional[float] = None, title: Optional[str] = None, node_names_placement: Optional[Literal['auto', 'nodes', 'legend', 'none']] = None, nodes_labels: Optional[Dict[int, str]] = None, edges_labels: Optional[Dict[int, str]] = None, nodes_layout_function: Optional[Callable[[networkx.classes.digraph.DiGraph], Dict[Any, Tuple[float, float]]]] = None)[source]

Visualizes graph or saves its picture to the specified path

Parameters
  • save_path – optional, save location of the graph visualization image.

  • engine – engine to visualize the graph. Possible values: ‘matplotlib’, ‘pyvis’, ‘graphviz’.

  • node_color – color of nodes to use.

  • node_size_scale – use to make node size bigger or lesser. Supported only for the engine ‘matplotlib’.

  • font_size_scale – use to make font size bigger or lesser. Supported only for the engine ‘matplotlib’.

  • edge_curvature_scale – use to make edges more or less curved. Supported only for the engine ‘matplotlib’.

  • dpi – DPI of the output image. Not supported for the engine ‘pyvis’.

  • title – title for plot

  • node_names_placement

    variant of node names displaying. Defaults to auto.

    Possible options:

    • auto -> empirical rule by node size

    • nodes -> place node names on top of the nodes

    • legend -> place node names at the legend

    • none -> do not show node names

  • nodes_labels – labels to display near nodes

  • edges_labels – labels to display near edges

  • nodes_layout_function – any of Networkx layout functions .

property graph_description: Dict

Return summary characteristics of the graph

Returns

containing information about the graph

Return type

dict

property descriptive_id: str

Returns human-readable identifier of the graph.

Returns

text description of the content in the node and its parameters

Return type

str

class golem.core.dag.graph_node.GraphNode[source]

Bases: abc.ABC

Definition of the node in directed graph structure.

Provides interface for getting and modifying the parent nodes and recursive description based on all preceding nodes.

abstract property nodes_from: List[golem.core.dag.graph_node.GraphNode]

Gets all parent nodes of this graph node

Returns

all the parent nodes

Return type

List[‘GraphNode’]

abstract property name: str

Str name of this graph node

abstract __str__() str[source]

Returns short node type description

Returns

text graph node representation

Return type

str

__repr__() str[source]

Returns full node description

Returns

text graph node representation

Return type

str

description() str[source]

Returns full node description for use in recursive id.

Returns

text graph node representation

Return type

str

property descriptive_id: str

Returns structural identifier of the subgraph starting at this node

Returns

text description of the content in the node and its parameters

Return type

str