easygraph.readwrite package

Subpackages

Submodules

easygraph.readwrite.edgelist module

easygraph.readwrite.edgelist.generate_edgelist(G, delimiter=' ', data=True)[source]

Generate a single line of the graph G in edge list format.

Parameters:
  • G (EasyGraph graph) –

  • delimiter (string, optional) – Separator for node labels

  • data (bool or list of keys) – If False generate no edge data. If True use a dictionary representation of edge data. If a list of keys use a list of data values corresponding to the keys.

Returns:

lines – Lines of data in adjlist format.

Return type:

string

Examples

>>> G = eg.lollipop_graph(4, 3)
>>> G[1][2]["weight"] = 3
>>> G[3][4]["capacity"] = 12
>>> for line in eg.generate_edgelist(G, data=False):
...     print(line)
0 1
0 2
0 3
1 2
1 3
2 3
3 4
4 5
5 6
>>> for line in eg.generate_edgelist(G):
...     print(line)
0 1 {}
0 2 {}
0 3 {}
1 2 {'weight': 3}
1 3 {}
2 3 {}
3 4 {'capacity': 12}
4 5 {}
5 6 {}
>>> for line in eg.generate_edgelist(G, data=["weight"]):
...     print(line)
0 1
0 2
0 3
1 2 3
1 3
2 3
3 4
4 5
5 6

See also

write_adjlist, read_adjlist

easygraph.readwrite.edgelist.parse_edgelist(lines, comments='#', delimiter=None, create_using=None, nodetype=None, data=True)[source]

Parse lines of an edge list representation of a graph.

Parameters:
  • lines (list or iterator of strings) – Input data in edgelist format

  • comments (string, optional) – Marker for comment lines. Default is ‘#’. To specify that no character should be treated as a comment, use comments=None.

  • delimiter (string, optional) – Separator for node labels. Default is None, meaning any whitespace.

  • create_using (EasyGraph graph constructor, optional (default=eg.Graph)) – Graph type to create. If graph instance, then cleared before populated.

  • nodetype (Python type, optional) – Convert nodes to this type. Default is None, meaning no conversion is performed.

  • data (bool or list of (label,type) tuples) – If False generate no edge data or if True use a dictionary representation of edge data or a list tuples specifying dictionary key names and types for edge data.

Returns:

G – The graph corresponding to lines

Return type:

EasyGraph Graph

Examples

Edgelist with no data:

>>> lines = ["1 2", "2 3", "3 4"]
>>> G = eg.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges)
[(1, 2), (2, 3), (3, 4)]

Edgelist with data in Python dictionary representation:

>>> lines = ["1 2 {'weight': 3}", "2 3 {'weight': 27}", "3 4 {'weight': 3.0}"]
>>> G = eg.parse_edgelist(lines, nodetype=int)
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges)
[(1, 2, {'weight': 3}), (2, 3, {'weight': 27}), (3, 4, {'weight': 3.0})]

Edgelist with data in a list:

>>> lines = ["1 2 3", "2 3 27", "3 4 3.0"]
>>> G = eg.parse_edgelist(lines, nodetype=int, data=(("weight", float),))
>>> list(G)
[1, 2, 3, 4]
>>> list(G.edges)
[(1, 2, {'weight': 3.0}), (2, 3, {'weight': 27.0}), (3, 4, {'weight': 3.0})]
easygraph.readwrite.edgelist.read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8')[source]

Read a graph from a list of edges.

Parameters:
  • path (file or string) – File or filename to read. If a file is provided, it must be opened in ‘rb’ mode. Filenames ending in .gz or .bz2 will be uncompressed.

  • comments (string, optional) – The character used to indicate the start of a comment. To specify that no character should be treated as a comment, use comments=None.

  • delimiter (string, optional) – The string used to separate values. The default is whitespace.

  • create_using (EasyGraph graph constructor, optional (default=eg.Graph)) – Graph type to create. If graph instance, then cleared before populated.

  • nodetype (int, float, str, Python type, optional) – Convert node data from strings to specified type

  • data (bool or list of (label,type) tuples) – Tuples specifying dictionary key names and types for edge data

  • edgetype (int, float, str, Python type, optional OBSOLETE) – Convert edge data from strings to specified type and use as ‘weight’

  • encoding (string, optional) – Specify which encoding to use when reading file.

Returns:

G – A easygraph Graph or other type specified with create_using

Return type:

graph

Examples

>>> eg.write_edgelist(eg.path_graph(4), "test.edgelist")
>>> G = eg.read_edgelist("test.edgelist")
>>> fh = open("test.edgelist", "rb")
>>> G = eg.read_edgelist(fh)
>>> fh.close()
>>> G = eg.read_edgelist("test.edgelist", nodetype=int)
>>> G = eg.read_edgelist("test.edgelist", create_using=eg.DiGraph)

Edgelist with data in a list:

>>> textline = "1 2 3"
>>> fh = open("test.edgelist", "w")
>>> d = fh.write(textline)
>>> fh.close()
>>> G = eg.read_edgelist("test.edgelist", nodetype=int, data=(("weight", float),))
>>> list(G)
[1, 2]
>>> list(G.edges)
[(1, 2, {'weight': 3.0})]

See parse_edgelist() for more examples of formatting.

Notes

Since nodes must be hashable, the function nodetype must return hashable types (e.g. int, float, str, frozenset - or tuples of those, etc.)

easygraph.readwrite.edgelist.read_weighted_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, encoding='utf-8')[source]

Read a graph as list of edges with numeric weights.

Parameters:
  • path (file or string) – File or filename to read. If a file is provided, it must be opened in ‘rb’ mode. Filenames ending in .gz or .bz2 will be uncompressed.

  • comments (string, optional) – The character used to indicate the start of a comment.

  • delimiter (string, optional) – The string used to separate values. The default is whitespace.

  • create_using (EasyGraph graph constructor, optional (default=eg.Graph)) – Graph type to create. If graph instance, then cleared before populated.

  • nodetype (int, float, str, Python type, optional) – Convert node data from strings to specified type

  • encoding (string, optional) – Specify which encoding to use when reading file.

Returns:

G – A easygraph Graph or other type specified with create_using

Return type:

graph

Notes

Since nodes must be hashable, the function nodetype must return hashable types (e.g. int, float, str, frozenset - or tuples of those, etc.)

Example edgelist file format.

With numeric edge data:

# read with
# >>> G=eg.read_weighted_edgelist(fh)
# source target data
a b 1
a c 3.14159
d e 42
easygraph.readwrite.edgelist.write_edgelist(G, path, comments='#', delimiter=' ', data=True, encoding='utf-8')[source]

Write graph as a list of edges.

Parameters:
  • G (graph) – A EasyGraph graph

  • path (file or string) – File or filename to write. If a file is provided, it must be opened in ‘wb’ mode. Filenames ending in .gz or .bz2 will be compressed.

  • comments (string, optional) – The character used to indicate the start of a comment

  • delimiter (string, optional) – The string used to separate values. The default is whitespace.

  • data (bool or list, optional) – If False write no edge data. If True write a string representation of the edge data dictionary.. If a list (or other iterable) is provided, write the keys specified in the list.

  • encoding (string, optional) – Specify which encoding to use when writing file.

Examples

>>> G = eg.path_graph(4)
>>> eg.write_edgelist(G, "test.edgelist")
>>> G = eg.path_graph(4)
>>> fh = open("test.edgelist", "wb")
>>> eg.write_edgelist(G, fh)
>>> eg.write_edgelist(G, "test.edgelist.gz")
>>> eg.write_edgelist(G, "test.edgelist.gz", data=False)
>>> G = eg.Graph()
>>> G.add_edge(1, 2, weight=7, color="red")
>>> eg.write_edgelist(G, "test.edgelist", data=False)
>>> eg.write_edgelist(G, "test.edgelist", data=["color"])
>>> eg.write_edgelist(G, "test.edgelist", data=["color", "weight"])
easygraph.readwrite.edgelist.write_weighted_edgelist(G, path, comments='#', delimiter=' ', encoding='utf-8')[source]

Write graph G as a list of edges with numeric weights.

Parameters:
  • G (graph) – A EasyGraph graph

  • path (file or string) – File or filename to write. If a file is provided, it must be opened in ‘wb’ mode. Filenames ending in .gz or .bz2 will be compressed.

  • comments (string, optional) – The character used to indicate the start of a comment

  • delimiter (string, optional) – The string used to separate values. The default is whitespace.

  • encoding (string, optional) – Specify which encoding to use when writing file.

Examples

>>> G = eg.Graph()
>>> G.add_edge(1, 2, weight=7)
>>> eg.write_weighted_edgelist(G, "test.weighted.edgelist")

easygraph.readwrite.gexf module

easygraph.readwrite.gexf.generate_gexf(G, encoding='utf-8', prettyprint=True, version='1.2draft')[source]

Generate lines of GEXF format representation of G.

“GEXF (Graph Exchange XML Format) is a language for describing complex networks structures, their associated data and dynamics” [1]_.

Parameters:
  • G (graph) –

  • graph (A EasyGraph) –

  • encoding (string (optional, default: 'utf-8')) –

  • data. (Encoding for text) –

  • prettyprint (bool (optional, default: True)) –

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

  • version (string (default: 1.2draft)) –

  • http (Version of GEFX File Format (see) –

  • values (Supported) –

Examples

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

Notes

This implementation does not support mixed graphs (directed and undirected edges together).

The node id attribute is set to be the string of the node label. If you want to specify an id use set it as node data, e.g. node[‘a’][‘id’]=1 to set the id of node ‘a’ to 1.

References

easygraph.readwrite.gexf.read_gexf(path, node_type=None, relabel=False, version='1.2draft')[source]

Read graph in GEXF format from path.

“GEXF (Graph Exchange XML Format) is a language for describing complex networks structures, their associated data and dynamics” [1]_.

Parameters:
  • path (file or string) – File or file name to read. File names ending in .gz or .bz2 will be decompressed.

  • node_type (Python type (default: None)) – Convert node ids to this type if not None.

  • relabel (bool (default: False)) – If True relabel the nodes to use the GEXF node “label” attribute instead of the node “id” attribute as the EasyGraph node label.

  • version (string (default: 1.2draft)) –

  • http (Version of GEFX File Format (see) – Supported values: “1.1draft”, “1.2draft”

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

Notes

This implementation does not support mixed graphs (directed and undirected edges together).

References

easygraph.readwrite.gexf.relabel_gexf_graph(G)[source]

Relabel graph using “label” node keyword for node label.

Parameters:

G (graph) – A EasyGraph graph read from GEXF data

Returns:

H – A EasyGraph graph with relabeled nodes

Return type:

graph

Raises:

EasyGraphError – If node labels are missing or not unique while relabel=True.

Notes

This function relabels the nodes in a EasyGraph graph with the “label” attribute. It also handles relabeling the specific GEXF node attributes “parents”, and “pid”.

easygraph.readwrite.gexf.write_gexf(G, path, encoding='utf-8', prettyprint=True, version='1.2draft')[source]

Write G in GEXF format to path.

“GEXF (Graph Exchange XML Format) is a language for describing complex networks structures, their associated data and dynamics” [1]_.

Node attributes are checked according to the version of the GEXF schemas used for parameters which are not user defined, e.g. visualization ‘viz’ [2]_. See example for usage.

Parameters:
  • G (graph) – An EasyGraph graph

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

  • encoding (string (optional, default: 'utf-8')) – Encoding for text data.

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

  • version (string (optional, default: '1.2draft')) – The version of GEXF to be used for nodes attributes checking

Examples

>>> G = eg.path_graph(4)
>>> eg.write_gexf(G, "test.gexf")

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")

easygraph.readwrite.graphml module

GraphML

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.

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

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.

easygraph.readwrite.graphviz module

easygraph.readwrite.graphviz.from_agraph(A, create_using=None)[source]

Returns a EasyGraph Graph or DiGraph from a PyGraphviz graph.

Parameters:
  • A (PyGraphviz AGraph) – A graph created with PyGraphviz

  • create_using (EasyGraph graph constructor, optional (default=None)) – Graph type to create. If graph instance, then cleared before populated. If None, then the appropriate Graph type is inferred from A.

Examples

>>> K5 = eg.complete_graph(5)
>>> A = eg.to_agraph(K5)
>>> G = eg.from_agraph(A)

Notes

The Graph G will have a dictionary G.graph_attr containing the default graphviz attributes for graphs, nodes and edges.

Default node attributes will be in the dictionary G.node_attr which is keyed by node.

Edge attributes will be returned as edge data in G. With edge_attr=False the edge data will be the Graphviz edge weight attribute or the value 1 if no edge weight attribute is found.

easygraph.readwrite.graphviz.read_dot(path)[source]

Returns a EasyGraph graph from a dot file on path.

Parameters:

path (file or string) – File name or file handle to read.

easygraph.readwrite.graphviz.to_agraph(N)[source]

Returns a pygraphviz graph from a EasyGraph graph N.

Parameters:

N (EasyGraph graph) – A graph created with EasyGraph

Examples

>>> K5 = eg.complete_graph(5)
>>> A = eg.to_agraph(K5)

Notes

If N has an dict N.graph_attr an attempt will be made first to copy properties attached to the graph (see from_agraph) and then updated with the calling arguments if any.

easygraph.readwrite.graphviz.write_dot(G, path)[source]

Write EasyGraph graph G to Graphviz dot format on path.

Parameters:
  • G (graph) – A easygraph graph

  • path (filename) – Filename or file handle to write

easygraph.readwrite.pajek module

Pajek

Read graphs in Pajek format.

This implementation handles directed and undirected graphs including those with self loops and parallel edges.

Format

See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm for format information.

easygraph.readwrite.pajek.generate_pajek(G)[source]

Generate lines in Pajek graph format.

Parameters:

G (graph) – A EasyGraph graph

References

See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm for format information.

easygraph.readwrite.pajek.parse_pajek(lines)[source]

Parse Pajek format graph from string or iterable.

Parameters:

lines (string or iterable) – Data in Pajek format.

Returns:

G

Return type:

EasyGraph graph

See also

read_pajek

easygraph.readwrite.pajek.read_pajek(path)[source]

Read graph in Pajek format from path.

Parameters:

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

Returns:

G

Return type:

EasyGraph MultiGraph or MultiDiGraph.

Examples

>>> G = eg.path_graph(4)
>>> eg.write_pajek(G, "test.net")
>>> G = eg.read_pajek("test.net")

To create a Graph instead of a MultiGraph use

>>> G1 = eg.Graph(G)

References

See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm for format information.

easygraph.readwrite.pajek.write_pajek(G, path, encoding='UTF-8')[source]

Write graph in Pajek 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.

Examples

>>> G = eg.path_graph(4)
>>> eg.write_pajek(G, "test.net")

Warning

Optional node attributes and edge attributes must be non-empty strings. Otherwise it will not be written into the file. You will need to convert those attributes to strings if you want to keep them.

References

See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm for format information.

easygraph.readwrite.pickle module

easygraph.readwrite.pickle.read_pickle(file_name)[source]
easygraph.readwrite.pickle.write_pickle(file_name, obj)[source]

easygraph.readwrite.ucinet module

UCINET DL

Read and write graphs in UCINET DL format. This implementation currently supports only the ‘fullmatrix’ data format. Format —— The UCINET DL format is the most common file format used by UCINET package. Basic example: DL N = 5 Data: 0 1 1 1 1 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 1 0 1 0 0 .. rubric:: References

See UCINET User Guide or http://www.analytictech.com/ucinet/help/hs5000.htm for full format information. Short version on http://www.analytictech.com/networks/dataentry.htm

easygraph.readwrite.ucinet.generate_ucinet(G)[source]

Generate lines in UCINET graph format. :param G: A EasyGraph graph :type G: graph

Examples

Notes

The default format ‘fullmatrix’ is used (for UCINET DL format).

References

See UCINET User Guide or http://www.analytictech.com/ucinet/help/hs5000.htm for full format information. Short version on http://www.analytictech.com/networks/dataentry.htm

easygraph.readwrite.ucinet.parse_ucinet(lines)[source]

Parse UCINET format graph from string or iterable. Currently only the ‘fullmatrix’, ‘nodelist1’ and ‘nodelist1b’ formats are supported. :param lines: Data in UCINET format. :type lines: string or iterable

Returns:

G

Return type:

EasyGraph graph

See also

read_ucinet

References

See UCINET User Guide or http://www.analytictech.com/ucinet/help/hs5000.htm for full format information. Short version on http://www.analytictech.com/networks/dataentry.htm

easygraph.readwrite.ucinet.read_ucinet(path, encoding='UTF-8')[source]

Read graph in UCINET format from path. :param path: File or filename to read.

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

Returns:

G

Return type:

EasyGraph MultiGraph or MultiDiGraph.

Examples

>>> G=eg.path_graph(4)
>>> eg.write_ucinet(G, "test.dl")
>>> G=eg.read_ucinet("test.dl")
To create a Graph instead of a MultiGraph use
>>> G1=eg.Graph(G)

See also

parse_ucinet

References

See UCINET User Guide or http://www.analytictech.com/ucinet/help/hs5000.htm for full format information. Short version on http://www.analytictech.com/networks/dataentry.htm

easygraph.readwrite.ucinet.write_ucinet(G, path, encoding='UTF-8')[source]

Write graph in UCINET format to path. :param G: A EasyGraph graph :type G: graph :param path: File or filename to write.

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

Examples

>>> G=eg.path_graph(4)
>>> eg.write_ucinet(G, "test.net")

References

See UCINET User Guide or http://www.analytictech.com/ucinet/help/hs5000.htm for full format information. Short version on http://www.analytictech.com/networks/dataentry.htm

Module contents