Graphs¶
Abstract graphs in ReGraph.
This module contains abstract classes for graph objects in ReGraph. Such graph objects represent simple graphs with dictionary-like attributes on nodes and edges.
- 
class regraph.graphs.Graph[source]¶
- Abstract class for graph objects in ReGraph. - Methods - add_edge(self, s, t[, attrs])- Add an edge to a graph. - add_edge_attrs(self, s, t, attrs)- Add attributes of an edge in a graph. - add_edges_from(self, edge_list)- Add edges from an edge list. - add_node(self, node_id[, attrs])- Abstract method for adding a node. - add_node_attrs(self, node, attrs)- Add new attributes to a node. - add_nodes_from(self, node_list)- Add nodes from a node list. - clone_node(self, node_id[, name])- Clone node. - copy_node(self, node_id[, copy_id])- Copy node. - edges(self[, data])- Return the list of edges. - exists_edge(self, s, t)- Check if an edge exists. - export(self, filename)- Export graph to JSON file. - filter_edges_by_attributes(self, attr_key, …)- Filter graph edges by attributes. - find_matching(self, pattern[, nodes])- Find matching of a pattern in a graph. - from_json(json_data)- Create a NetworkX graph from a json-like dictionary. - generate_new_node_id(self, basename)- Generate new unique node identifier. - get_edge(self, s, t)- Get edge attributes. - get_edge_attrs(self, s, t)- Get edge attributes. - get_node(self, n)- Get node attributes. - get_node_attrs(self, n)- Get node attributes. - in_edges(self, node_id)- Return the set of in-coming edges. - load(filename)- Load a graph from a JSON file. - merge_nodes(self, nodes[, node_id, method, …])- Merge a list of nodes. - nodes(self[, data])- Return the list of nodes. - number_of_edges(self, u, v)- Return number of directed edges from u to v. - out_edges(self, node_id)- Return the set of out-going edges. - predecessors(self, node_id)- Return the set of predecessors. - print_graph(self)- Pretty-print the graph. - relabel_node(self, node_id, new_id)- Relabel a node in the graph. - relabel_nodes(self, mapping)- Relabel graph nodes inplace given a mapping. - remove_edge(self, source_id, target_id)- Remove edge from the graph. - remove_edge_attrs(self, s, t, attrs)- Remove attrs of an edge specified by attrs. - remove_node(self, node_id)- Remove node. - remove_node_attrs(self, node_id, attrs)- Remove attrs of a node specified by attrs_dict. - rewrite(self, rule[, instance])- Perform SqPO rewiting of the graph with a rule. - set_edge(self, s, t, attrs[, normalize, update])- Set edge attrs. - set_edge_attrs(self, s, t, attrs[, …])- Set edge attrs. - set_node_attrs(self, node_id, attrs[, …])- Set node attrs. - successors(self, node_id)- Return the set of successors. - to_d3_json(self[, attrs, …])- Create a JSON representation of a graph. - to_json(self)- Create a JSON representation of a graph. - update_edge_attrs(self, s, t, attrs[, normalize])- Update attributes of a node. - update_node_attrs(self, node_id, attrs[, …])- Update attributes of a node. - 
abstract add_edge(self, s, t, attrs=None, **attr)[source]¶
- Add an edge to a graph. - Parameters
- shashable
- Source node id. 
- thashable
- Target node id. 
- attrsdict
- Edge attributes. 
 
 
 - 
add_edge_attrs(self, s, t, attrs)[source]¶
- Add attributes of an edge in a graph. - Parameters
- shashable
- Source node id. 
- thashable
- Target node id. 
- attrsdict
- Dictionary with attributes to remove. 
 
- Raises
- GraphError
- If an edge between s and t does not exist. 
 
 
 - 
add_edges_from(self, edge_list)[source]¶
- Add edges from an edge list. - Parameters
- edge_listiterable
- Iterable containing a collection of edges, optionally, with their attributes 
 
 
 - 
abstract add_node(self, node_id, attrs=None)[source]¶
- Abstract method for adding a node. - Parameters
- node_idhashable
- Prefix that is prepended to the new unique name. 
- attrsdict, optional
- Node attributes. 
 
 
 - 
add_node_attrs(self, node, attrs)[source]¶
- Add new attributes to a node. - Parameters
- nodehashable
- Id of a node to add attributes to. 
- attrsdict
- Attributes to add. 
 
- Raises
- GraphError
- If a node node_id does not exist. 
 
 
 - 
add_nodes_from(self, node_list)[source]¶
- Add nodes from a node list. - Parameters
- node_listiterable
- Iterable containing a collection of nodes, optionally, with their attributes 
 
 
 - 
clone_node(self, node_id, name=None)[source]¶
- Clone node. - Create a new node, a copy of a node with node_id, and reconnect it with all the adjacent nodes of node_id. - Parameters
- node_idhashable,
- Id of a node to clone. 
- namehashable, optional
- Id for the clone, if is not specified, new id will be generated. 
 
- Returns
- new_nodehashable
- Id of the new node corresponding to the clone 
 
- Raises
- GraphError
- If node wiht node_id does not exists or a node with name (clone’s name) already exists. 
 
 
 - 
copy_node(self, node_id, copy_id=None)[source]¶
- Copy node. - Create a copy of a node in a graph. A new id for the copy is generated by regraph.primitives.unique_node_id. - Parameters
- node_idhashable
- Node to copy. 
 
- Returns
- new_name
- Id of the copy node. 
 
 
 - 
exists_edge(self, s, t)[source]¶
- Check if an edge exists. - Parameters
- shashable
- Source node id. 
- thashable
- Target node id. 
 
 
 - 
export(self, filename)[source]¶
- Export graph to JSON file. - Parameters
- filenamestr
- Name of the file to save the json serialization of the graph 
 
 
 - 
filter_edges_by_attributes(self, attr_key, attr_cond)[source]¶
- Filter graph edges by attributes. - Removes all the edges of the graph (inplace) that do not satisfy attr_cond. - Parameters
- attrs_keyhashable
- Attribute key 
- attrs_condcallable
- Condition for an attribute to satisfy: callable that returns True if condition is satisfied, False otherwise. 
 
 
 - 
classmethod from_json(json_data)[source]¶
- Create a NetworkX graph from a json-like dictionary. - Parameters
- json_datadict
- JSON-like dictionary with graph representation 
 
 
 - 
abstract get_edge(self, s, t)[source]¶
- Get edge attributes. - Parameters
- shashable
- Source node id. 
- thashable
- Target node id. 
 
 
 - 
get_edge_attrs(self, s, t)[source]¶
- Get edge attributes. - Parameters
- graphnetworkx.(Di)Graph
- shashable, source node id.
- thashable, target node id.
 
 
 - 
abstract get_node(self, n)[source]¶
- Get node attributes. - Parameters
- shashable
- Source node id. 
- thashable,
- Target node id. 
 
 
 - 
classmethod load(filename)[source]¶
- Load a graph from a JSON file. - Create a networkx.(Di)Graph object from a JSON representation stored in a file. - Parameters
- filenamestr
- Name of the file to load the json serialization of the graph 
 
- Returns
- Graph object
 
- Raises
- ReGraphError
- If was not able to load the file 
 
 
 - 
merge_nodes(self, nodes, node_id=None, method='union', edge_method='union')[source]¶
- Merge a list of nodes. - Parameters
- nodesiterable
- Collection of node id’s to merge. 
- node_idhashable, optional
- Id of a new node corresponding to the result of merge. 
- methodoptional
- Method of node attributes merge: if “union” the resulting node will contain the union of all attributes of the merged nodes, if “intersection”, the resulting node will contain their intersection. Default value is “union”. 
- edge_methodoptional
- Method of edge attributes merge: if “union” the edges that were merged will contain the union of all attributes, if “intersection” – their ntersection. Default value is “union”. 
 
 
 - 
relabel_node(self, node_id, new_id)[source]¶
- Relabel a node in the graph. - Parameters
- node_idhashable
- Id of the node to relabel. 
- new_idhashable
- New label of a node. 
 
 
 - 
relabel_nodes(self, mapping)[source]¶
- Relabel graph nodes inplace given a mapping. - Similar to networkx.relabel.relabel_nodes: https://networkx.github.io/documentation/development/_modules/networkx/relabel.html - Parameters
- mapping: dict
- A dictionary with keys being old node ids and their values being new id’s of the respective nodes. 
 
- Raises
- ReGraphError
- If new id’s do not define a set of distinct node id’s. 
 
 
 - 
abstract remove_edge(self, source_id, target_id)[source]¶
- Remove edge from the graph. - Parameters
- shashable
- Source node id. 
- thashable
- Target node id. 
 
 
 - 
remove_edge_attrs(self, s, t, attrs)[source]¶
- Remove attrs of an edge specified by attrs. - Parameters
- shashable
- Source node id. 
- thashable
- Target node id. 
- attrsdict
- Dictionary with attributes to remove. 
 
- Raises
- GraphError
- If an edge between s and t does not exist. 
 
 
 - 
remove_node_attrs(self, node_id, attrs)[source]¶
- Remove attrs of a node specified by attrs_dict. - Parameters
- node_idhashable
- Node whose attributes to remove. 
- attrsdict
- Dictionary with attributes to remove. 
 
- Raises
- GraphError
- If a node with the specified id does not exist. 
 
 
 - 
rewrite(self, rule, instance=None)[source]¶
- Perform SqPO rewiting of the graph with a rule. - Parameters
- ruleregraph.Rule
- SqPO rewriting rule 
- instancedict, optional
- Instance of the input rule. If not specified, the identity map of the rule’s left-hand side is used 
 
 
 - 
set_edge(self, s, t, attrs, normalize=True, update=True)[source]¶
- Set edge attrs. - Parameters
- shashable
- Source node id. 
- thashable
- Target node id. 
- attrsdictionary
- Dictionary with attributes to set. 
 
- Raises
- GraphError
- If an edge between s and t does not exist. 
 
 
 - 
set_edge_attrs(self, s, t, attrs, normalize=True, update=True)[source]¶
- Set edge attrs. - Parameters
- attrsdict
- Dictionary with new attributes to set 
- normalizebool, optional
- Flag, when set to True attributes are normalized to be set-valued. True by default 
- updatebool, optional
- Flag, when set to True attributes whose keys are not present in attrs are removed, True by default 
 
- Raises
- GraphError
- If an edge between s and t does not exist. 
 
 
 - 
set_node_attrs(self, node_id, attrs, normalize=True, update=True)[source]¶
- Set node attrs. - Parameters
- node_idhashable
- Id of the node to update 
- attrsdict
- Dictionary with new attributes to set 
- normalizebool, optional
- Flag, when set to True attributes are normalized to be set-valued. True by default 
- updatebool, optional
- Flag, when set to True attributes whose keys are not present in attrs are removed, True by default 
 
- Raises
- GraphError
- If a node node_id does not exist. 
 
 
 - 
to_d3_json(self, attrs=True, node_attrs_to_attach=None, edge_attrs_to_attach=None, nodes=None)[source]¶
- Create a JSON representation of a graph. 
 
- 
abstract