easygraph.readwrite.gml module#

Read graphs in GML format.

“GML, the Graph Modelling Language, is our proposal for a portable file format for graphs. GML’s key features are portability, simple syntax, extensibility and flexibility. A GML file consists of a hierarchical key-value lists. Graphs can be annotated with arbitrary data structures. The idea for a common file format was born at the GD’95; this proposal is the outcome of many discussions. GML is the standard file format in the Graphlet graph editor system. It has been overtaken and adapted by several other systems for drawing graphs.”

GML files are stored using a 7-bit ASCII encoding with any extended ASCII characters (iso8859-1) appearing as HTML character entities. You will need to give some thought into how the exported data should interact with different languages and even different Python versions. Re-importing from gml is also a concern.

Without specifying a stringizer/destringizer, the code is capable of writing int/float/str/dict/list data as required by the GML specification. For writing other data types, and for reading data other than str you need to explicitly supply a stringizer/destringizer.

For additional documentation on the GML file format, please see the GML website.

Several example graphs in GML format may be found on Mark Newman’s Network data page.

easygraph.readwrite.gml.generate_gml(G, stringizer=None)[source]#

Generate a single entry of the graph G in GML format.

Parameters:
  • G (EasyGraph graph) – The graph to be converted to GML.

  • stringizer (callable, optional) – A stringizer which converts non-int/non-float/non-dict values into strings. If it cannot convert a value into a string, it should raise a ValueError to indicate that. Default value: None.

Returns:

lines – Lines of GML data. Newlines are not appended.

Return type:

generator of strings

Raises:

EasyGraphError – If stringizer cannot convert a value into a string, or the value to convert is not a string while stringizer is None.

See also

literal_stringizer

Notes

Graph attributes named ‘directed’, ‘multigraph’, ‘node’ or ‘edge’, node attributes named ‘id’ or ‘label’, edge attributes named ‘source’ or ‘target’ (or ‘key’ if G is a multigraph) are ignored because these attribute names are used to encode the graph structure.

GML files are stored using a 7-bit ASCII encoding with any extended ASCII characters (iso8859-1) appearing as HTML character entities. Without specifying a stringizer/destringizer, the code is capable of writing int/float/str/dict/list data as required by the GML specification. For writing other data types, and for reading data other than str you need to explicitly supply a stringizer/destringizer.

For additional documentation on the GML file format, please see the GML url.

See the module docstring easygraph.readwrite.gml for more details.

Examples

>>> G = eg.Graph()
>>> G.add_node("1")
>>> print("\n".join(eg.generate_gml(G)))
graph [
  node [
    id 0
    label "1"
  ]
]
easygraph.readwrite.gml.parse_gml(lines, label='label', destringizer=None)[source]#

Parse GML graph from a string or iterable.

Parameters:
  • lines (string or iterable of strings) – Data in GML format.

  • label (string, optional) – If not None, the parsed nodes will be renamed according to node attributes indicated by label. Default value: ‘label’.

  • destringizer (callable, optional) – A destringizer that recovers values stored as strings in GML. If it cannot convert a string to a value, a ValueError is raised. Default value : None.

Returns:

G – The parsed graph.

Return type:

EasyGraph graph

Raises:

EasyGraphError – If the input cannot be parsed.

See also

write_gml, read_gml

Notes

This stores nested GML attributes as dictionaries in the EasyGraph graph, node, and edge attribute structures.

GML files are stored using a 7-bit ASCII encoding with any extended ASCII characters (iso8859-1) appearing as HTML character entities. Without specifying a stringizer/destringizer, the code is capable of writing int/float/str/dict/list data as required by the GML specification. For writing other data types, and for reading data other than str you need to explicitly supply a stringizer/destringizer.

For additional documentation on the GML file format, please see the GML url.

See the module docstring easygraph.readwrite.gml for more details.

easygraph.readwrite.gml.read_gml(path, label='label', destringizer=None)[source]#

Read graph in GML format from path.

Parameters:
  • path (filename or filehandle) – The filename or filehandle to read from.

  • label (string, optional) – If not None, the parsed nodes will be renamed according to node attributes indicated by label. Default value: ‘label’.

  • destringizer (callable, optional) – A destringizer that recovers values stored as strings in GML. If it cannot convert a string to a value, a ValueError is raised. Default value : None.

Returns:

G – The parsed graph.

Return type:

EasyGraph graph

Raises:

EasyGraphError – If the input cannot be parsed.

See also

write_gml, parse_gml, literal_destringizer

Notes

GML files are stored using a 7-bit ASCII encoding with any extended ASCII characters (iso8859-1) appearing as HTML character entities. Without specifying a stringizer/destringizer, the code is capable of writing int/float/str/dict/list data as required by the GML specification. For writing other data types, and for reading data other than str you need to explicitly supply a stringizer/destringizer.

For additional documentation on the GML file format, please see the GML url.

See the module docstring easygraph.readwrite.gml for more details.

Examples

>>> G = eg.path_graph(4)
>>> eg.write_gml(G, "test.gml")

GML values are interpreted as strings by default:

>>> H = eg.read_gml("test.gml")
>>> H.nodes
NodeView(('0', '1', '2', '3'))

When a destringizer is provided, GML values are converted to the provided type. For example, integer nodes can be recovered as shown below:

>>> J = eg.read_gml("test.gml", destringizer=int)
>>> J.nodes
NodeView((0, 1, 2, 3))
easygraph.readwrite.gml.write_gml(G, path, stringizer=None)[source]#

Write a graph G in GML format to the file or file handle path.

Parameters:
  • G (EasyGraph graph) – The graph to be converted to GML.

  • path (filename or filehandle) – The filename or filehandle to write. Files whose names end with .gz or .bz2 will be compressed.

  • stringizer (callable, optional) – A stringizer which converts non-int/non-float/non-dict values into strings. If it cannot convert a value into a string, it should raise a ValueError to indicate that. Default value: None.

Raises:

EasyGraphError – If stringizer cannot convert a value into a string, or the value to convert is not a string while stringizer is None.

See also

read_gml, generate_gml, literal_stringizer

Notes

Graph attributes named ‘directed’, ‘multigraph’, ‘node’ or ‘edge’, node attributes named ‘id’ or ‘label’, edge attributes named ‘source’ or ‘target’ (or ‘key’ if G is a multigraph) are ignored because these attribute names are used to encode the graph structure.

GML files are stored using a 7-bit ASCII encoding with any extended ASCII characters (iso8859-1) appearing as HTML character entities. Without specifying a stringizer/destringizer, the code is capable of writing int/float/str/dict/list data as required by the GML specification. For writing other data types, and for reading data other than str you need to explicitly supply a stringizer/destringizer.

Note that while we allow non-standard GML to be read from a file, we make sure to write GML format. In particular, underscores are not allowed in attribute names. For additional documentation on the GML file format, please see the GML url.

See the module docstring easygraph.readwrite.gml for more details.

Examples

>>> G = eg.path_graph(4)
>>> eg.write_gml(G, "test.gml")

Filenames ending in .gz or .bz2 will be compressed.

>>> eg.write_gml(G, "test.gml.gz")