easygraph.readwrite.graphml module#

Read and write graphs in GraphML format.

Warning

This parser uses the standard xml library present in Python, which is insecure - see library/xml for additional information. Only parse GraphML files you trust.

This implementation does not support mixed graphs (directed and unidirected edges together), hyperedges, nested graphs, or ports.

“GraphML is a comprehensive and easy-to-use file format for graphs. It consists of a language core to describe the structural properties of a graph and a flexible extension mechanism to add application-specific data. Its main features include support of

  • directed, undirected, and mixed graphs,

  • hypergraphs,

  • hierarchical graphs,

  • graphical representations,

  • references to external data,

  • application-specific attribute data, and

  • light-weight parsers.

Unlike many other file formats for graphs, GraphML does not use a custom syntax. Instead, it is based on XML and hence ideally suited as a common denominator for all kinds of services generating, archiving, or processing graphs.”

http://graphml.graphdrawing.org/

Format#

GraphML is an XML format. See http://graphml.graphdrawing.org/specification.html for the specification and http://graphml.graphdrawing.org/primer/graphml-primer.html for examples.

class easygraph.readwrite.graphml.GraphMLReader(node_type=<class 'str'>, edge_key_type=<class 'int'>, force_multigraph=False)[source]#

Bases: GraphML

Read a GraphML document. Produces EasyGraph graph objects.

Methods

__call__([path, string])

Call self as a function.

add_edge(G, edge_element, graphml_keys)

Add an edge to the graph.

add_node(G, node_xml, graphml_keys, defaults)

Add a node to the graph.

decode_data_elements(graphml_keys, obj_xml)

Use the key information to decode the data XML if present.

find_graphml_keys(graph_element)

Extracts all the keys and key defaults from the xml.

get_xml_type(key)

Wrapper around the xml_type dict that raises a more informative exception message when a user attempts to use data of a type not supported by GraphML.

construct_types

make_graph

add_edge(G, edge_element, graphml_keys)[source]#

Add an edge to the graph.

add_node(G, node_xml, graphml_keys, defaults)[source]#

Add a node to the graph.

decode_data_elements(graphml_keys, obj_xml)[source]#

Use the key information to decode the data XML if present.

find_graphml_keys(graph_element)[source]#

Extracts all the keys and key defaults from the xml.

make_graph(graph_xml, graphml_keys, defaults, G=None)[source]#
class easygraph.readwrite.graphml.GraphMLWriter(graph=None, encoding='utf-8', prettyprint=True, infer_numeric_types=False, named_key_ids=False, edge_id_from_attribute=None)[source]#

Bases: GraphML

Methods

add_attributes(scope, xml_obj, data, default)

Appends attribute data to edges or nodes, and stores type information to be added later.

add_data(name, element_type, value[, scope, ...])

Make a data element for an edge or a node.

add_graph_element(G)

Serialize graph G in GraphML to the stream.

add_graphs(graph_list)

Add many graphs to this GraphML document.

attr_type(name, scope, value)

Infer the attribute type of data named name.

get_xml_type(key)

Wrapper around the xml_type dict that raises a more informative exception message when a user attempts to use data of a type not supported by GraphML.

add_edges

add_nodes

construct_types

dump

get_key

indent

add_attributes(scope, xml_obj, data, default)[source]#

Appends attribute data to edges or nodes, and stores type information to be added later. See add_graph_element.

add_data(name, element_type, value, scope='all', default=None)[source]#

Make a data element for an edge or a node. Keep a log of the type in the keys table.

add_edges(G, graph_element)[source]#
add_graph_element(G)[source]#

Serialize graph G in GraphML to the stream.

add_graphs(graph_list)[source]#

Add many graphs to this GraphML document.

add_nodes(G, graph_element)[source]#
attr_type(name, scope, value)[source]#

Infer the attribute type of data named name. Currently this only supports inference of numeric types.

If self.infer_numeric_types is false, type is used. Otherwise, pick the most general of types found across all values with name and scope. This means edges with data named ‘weight’ are treated separately from nodes with data named ‘weight’.

dump(stream)[source]#
get_key(name, attr_type, scope, default)[source]#
indent(elem, level=0)[source]#
easygraph.readwrite.graphml.generate_graphml(G, encoding='utf-8', prettyprint=True, named_key_ids=False, edge_id_from_attribute=None)[source]#

Generate GraphML lines for G

Parameters:
  • G (graph) – A easygraph graph

  • encoding (string (optional)) – Encoding for text data.

  • prettyprint (bool (optional)) – If True use line breaks and indenting in output XML.

  • named_key_ids (bool (optional)) – If True use attr.name as value for key elements’ id attribute.

  • edge_id_from_attribute (dict key (optional)) – If provided, the graphml edge id is set by looking up the corresponding edge data attribute keyed by this parameter. If None or the key does not exist in edge data, the edge id is set by the edge key if G is a MultiGraph, else the edge id is left unset.

Examples

>>> G = eg.path_graph(4)
>>> linefeed = chr(10)  # linefeed =
>>> s = linefeed.join(eg.generate_graphml(G))
>>> for line in eg.generate_graphml(G):  
...     print(line)

Notes

This implementation does not support mixed graphs (directed and unidirected edges together) hyperedges, nested graphs, or ports.

easygraph.readwrite.graphml.parse_graphml(graphml_string, node_type=<class 'str'>, edge_key_type=<class 'int'>, force_multigraph=False)[source]#

Read graph in GraphML format from string.

Parameters:
  • graphml_string (string) – String containing graphml information (e.g., contents of a graphml file).

  • node_type (Python type (default: str)) – Convert node ids to this type

  • edge_key_type (Python type (default: int)) – Convert graphml edge ids to this type. Multigraphs use id as edge key. Non-multigraphs add to edge attribute dict with name “id”.

  • force_multigraph (bool (default: False)) – If True, return a multigraph with edge keys. If False (the default) return a multigraph when multiedges are in the graph.

Returns:

graph – If no parallel edges are found a Graph or DiGraph is returned. Otherwise a MultiGraph or MultiDiGraph is returned.

Return type:

EasyGraph graph

Examples

>>> G = eg.path_graph(4)
>>> linefeed = chr(10)  # linefeed =
>>> s = linefeed.join(eg.generate_graphml(G))
>>> H = eg.parse_graphml(s)

Notes

Default node and edge attributes are not propagated to each node and edge. They can be obtained from G.graph and applied to node and edge attributes if desired using something like this:

>>> default_color = G.graph["node_default"]["color"]  
>>> for node, data in G.nodes(data=True):  
...     if "color" not in data:
...         data["color"] = default_color
>>> default_color = G.graph["edge_default"]["color"]  
>>> for u, v, data in G.edges(data=True):  
...     if "color" not in data:
...         data["color"] = default_color

This implementation does not support mixed graphs (directed and unidirected edges together), hypergraphs, nested graphs, or ports.

For multigraphs the GraphML edge “id” will be used as the edge key. If not specified then they “key” attribute will be used. If there is no “key” attribute a default EasyGraph multigraph edge key will be provided.

easygraph.readwrite.graphml.read_graphml(path, node_type=<class 'str'>, edge_key_type=<class 'int'>, force_multigraph=False)[source]#

Read graph in GraphML format from path.

Parameters:
  • path (file or string) – File or filename to write. Filenames ending in .gz or .bz2 will be compressed.

  • node_type (Python type (default: str)) – Convert node ids to this type

  • edge_key_type (Python type (default: int)) – Convert graphml edge ids to this type. Multigraphs use id as edge key. Non-multigraphs add to edge attribute dict with name “id”.

  • force_multigraph (bool (default: False)) – If True, return a multigraph with edge keys. If False (the default) return a multigraph when multiedges are in the graph.

Returns:

graph – If parallel edges are present or force_multigraph=True then a MultiGraph or MultiDiGraph is returned. Otherwise a Graph/DiGraph. The returned graph is directed if the file indicates it should be.

Return type:

EasyGraph graph

Notes

Default node and edge attributes are not propagated to each node and edge. They can be obtained from G.graph and applied to node and edge attributes if desired using something like this:

>>> default_color = G.graph["node_default"]["color"]  
>>> for node, data in G.nodes(data=True):  
...     if "color" not in data:
...         data["color"] = default_color
>>> default_color = G.graph["edge_default"]["color"]  
>>> for u, v, data in G.edges(data=True):  
...     if "color" not in data:
...         data["color"] = default_color

This implementation does not support mixed graphs (directed and unidirected edges together), hypergraphs, nested graphs, or ports.

For multigraphs the GraphML edge “id” will be used as the edge key. If not specified then they “key” attribute will be used. If there is no “key” attribute a default EasyGraph multigraph edge key will be provided.

Files with the yEd “yfiles” extension can be read. The type of the node’s shape is preserved in the shape_type node attribute.

yEd compressed files (“file.graphmlz” extension) can be read by renaming the file to “file.graphml.gz”.

easygraph.readwrite.graphml.write_graphml(G, path, encoding='utf-8', prettyprint=True, infer_numeric_types=False, named_key_ids=False, edge_id_from_attribute=None)#

Write G in GraphML XML format to path

This function uses the LXML framework and should be faster than the version using the xml library.

Parameters:
  • G (graph) – A easygraph graph

  • path (file or string) – File or filename to write. Filenames ending in .gz or .bz2 will be compressed.

  • encoding (string (optional)) – Encoding for text data.

  • prettyprint (bool (optional)) – If True use line breaks and indenting in output XML.

  • infer_numeric_types (boolean) – Determine if numeric types should be generalized. For example, if edges have both int and float ‘weight’ attributes, we infer in GraphML that both are floats.

  • named_key_ids (bool (optional)) – If True use attr.name as value for key elements’ id attribute.

  • edge_id_from_attribute (dict key (optional)) – If provided, the graphml edge id is set by looking up the corresponding edge data attribute keyed by this parameter. If None or the key does not exist in edge data, the edge id is set by the edge key if G is a MultiGraph, else the edge id is left unset.

Examples

>>> G = eg.path_graph(4)
>>> eg.write_graphml_lxml(G, "fourpath.graphml")

Notes

This implementation does not support mixed graphs (directed and unidirected edges together) hyperedges, nested graphs, or ports.

easygraph.readwrite.graphml.write_graphml_lxml(G, path, encoding='utf-8', prettyprint=True, infer_numeric_types=False, named_key_ids=False, edge_id_from_attribute=None)[source]#

Write G in GraphML XML format to path

This function uses the LXML framework and should be faster than the version using the xml library.

Parameters:
  • G (graph) – A easygraph graph

  • path (file or string) – File or filename to write. Filenames ending in .gz or .bz2 will be compressed.

  • encoding (string (optional)) – Encoding for text data.

  • prettyprint (bool (optional)) – If True use line breaks and indenting in output XML.

  • infer_numeric_types (boolean) – Determine if numeric types should be generalized. For example, if edges have both int and float ‘weight’ attributes, we infer in GraphML that both are floats.

  • named_key_ids (bool (optional)) – If True use attr.name as value for key elements’ id attribute.

  • edge_id_from_attribute (dict key (optional)) – If provided, the graphml edge id is set by looking up the corresponding edge data attribute keyed by this parameter. If None or the key does not exist in edge data, the edge id is set by the edge key if G is a MultiGraph, else the edge id is left unset.

Examples

>>> G = eg.path_graph(4)
>>> eg.write_graphml_lxml(G, "fourpath.graphml")

Notes

This implementation does not support mixed graphs (directed and unidirected edges together) hyperedges, nested graphs, or ports.

easygraph.readwrite.graphml.write_graphml_xml(G, path, encoding='utf-8', prettyprint=True, infer_numeric_types=False, named_key_ids=False, edge_id_from_attribute=None)[source]#

Write G in GraphML XML format to path

Parameters:
  • G (graph) – A easygraph graph

  • path (file or string) – File or filename to write. Filenames ending in .gz or .bz2 will be compressed.

  • encoding (string (optional)) – Encoding for text data.

  • prettyprint (bool (optional)) – If True use line breaks and indenting in output XML.

  • infer_numeric_types (boolean) – Determine if numeric types should be generalized. For example, if edges have both int and float ‘weight’ attributes, we infer in GraphML that both are floats.

  • named_key_ids (bool (optional)) – If True use attr.name as value for key elements’ id attribute.

  • edge_id_from_attribute (dict key (optional)) – If provided, the graphml edge id is set by looking up the corresponding edge data attribute keyed by this parameter. If None or the key does not exist in edge data, the edge id is set by the edge key if G is a MultiGraph, else the edge id is left unset.

Examples

>>> G = eg.path_graph(4)
>>> eg.write_graphml(G, "test.graphml")

Notes

This implementation does not support mixed graphs (directed and unidirected edges together) hyperedges, nested graphs, or ports.