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.
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.
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
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.)
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.
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.
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.
“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).
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”.
“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
“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.
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.
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.
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.
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.
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.
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.
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.
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.”
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’.
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.
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.
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:
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.
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:
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”.
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.
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.
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.
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.
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.
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.
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.
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
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