Graph#

graph module#

class mmodel.graph.Graph(incoming_graph_data=None, **attr)[source]#

Bases: DiGraph, ReprMixin

Create model graphs.

mmodel.Graph inherits from networkx.DiGraph().

The class adds the “graph_module” and “node_type” attributes to the graph. The factory method returns a copy of the dictionary. It is equivalent to {"graph_module": "mmodel", "node_type": Node}.copy() when called.

The following additional graph operations are added:

  • add_grouped_edges and add_node_objects

  • Method add_node_object updates nodes with the node callable “func” and output.

add_edge(u_of_edge, v_of_edge, **attr)[source]#

Modify add_edge to update the edge attribute in the end.

add_edges_from(ebunch_to_add, **attr)[source]#

Modify add_edges_from to update the edge attributes.

add_grouped_edge(u, v)[source]#

Add linked edge.

For mmodel, a grouped edge (u, v) allows u or v to be a list of nodes. A grouped edge represents one or several nodes flowing into one node.

add_grouped_edges_from(group_edges: list)[source]#

Add edges from grouped values.

add_node_object(node_object)[source]#

Add or update the functions of an existing node.

add_node_objects_from(node_objects: list)[source]#

Update the functions of existing nodes.

This method is equivalent to adding node objects individually.

deepcopy()[source]#

Deepcopy graph.

The graph.copy method is a shallow copy. Deepcopy creates a copy of the attributes dictionary. graph.copy<https://networkx.org/documentation/stable/reference/classes /generated/networkx.Graph.copy.html>_

However, for subgraphs, deepcopy is incredibly inefficient because the subgraph contains ‘_graph’, which stores the original graph. An alternative method is to copy the code from the copy method, but use deepcopy for the items.

The parser is redefined in the new graph.

edit_node(node, **kwargs)[source]#

Edit node attributes.

Returns a new graph.

get_node(node)[source]#

Get node attributes from the graph.

get_node_object(node)[source]#

Get node object from the graph.

graph_attr_dict_factory()#

D.copy() -> a shallow copy of D

property grouped_edges#

Return grouped edges based on the graph.

replace_subgraph(subgraph, node_object)[source]#

Replace subgraph with a node.

subgraph(nodes=None, inputs=None, outputs=None)[source]#

Extract subgraph by nodes, inputs, and outputs.

If multiple parameters are specified, the result is a union of the selections. The subgraph is a deep copy of the original graph. The behavior is different from the parent class method, where the subgraph returns a view of the original graph.

update_graph()[source]#

Update edge attributes based on node objects and edges.

The edge “output” is not defined if the parent node does not have the “output” attribute or the child node does not have the parameter.

visualize(outfile=None)[source]#

Draw the graph.

Draws the default styled graph.

Parameters:

outfile (str) – filename to save the graph as. The file extension is needed.