Graph#

graph module#

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

Bases: DiGraph

Create model graphs.

mmodel.Graph inherits from networkx.DiGraph(), which has all DiGraph methods.

The class adds the “type” attribute to the graph attribute. The factory method returns a copy of the dictionary. It is equivalent to {"type": "mmodel_graph"}.copy() when called.

The additional graph operations are added: - add_grouped_edges and set_node_objects. - Method add_grouped_edges, cannot have both edges list. - Method set_node_object updates nodes with the node callable “func” and output. - The method adds callable signature ‘sig’ to the node attribute.

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 group 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.

deepcopy()[source]#

Deepcopy graph.

The graph.copy method is a shallow copy. Deepcopy creates a copy for 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 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

replace_subgraph(subgraph, node_object)[source]#

Replace subgraph with a node.

set_node_object(node_object)[source]#

Add or update the functions of an existing node.

set_node_objects_from(node_objects: list)[source]#

Update the functions of existing nodes.

The method is the same as adding a node object.

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

Extract subgraph by nodes, inputs, and output.

If multiple parameters are specified, the result is a union of the selection. 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.

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.