Previous topic

BaseGraph

Next topic

Network layout optimization

This Page

Reading and Writing Networks

Use methods in the Orange.network.readwrite module to read and write networks in Orange. If you must use the original NetworkX read/write methods (for some reason), do not forget to cast the network object (see Orange.network.readwrite._wrap method).

Orange.network.readwrite.read(path, encoding='UTF-8', auto_table=0)

Read graph in any of the supported file formats (.gpickle, .net, .gml). The parser is chosen based on the file extension.

Parameters:path (string) – File or filename to write.

Return the network of type Orange.network.Graph, Orange.network.DiGraph, Orange.network.Graph or Orange.network.DiGraph.

Orange.network.readwrite.write(G, path, encoding='UTF-8')

Write graph in any of the supported file formats (.gpickle, .net, .gml). The file format is chosen based on the file extension.

Parameters:
Orange.network.readwrite.read_gpickle(path, auto_table=False)

NetworkX read_gpickle method and wrap graph to Orange network.

Read graph object in Python pickle format.

Pickles are a serialized byte stream of a Python object [R1]. This format will preserve Python objects used as nodes or edges.

Parameters:

path : file or string

File or filename to write. Filenames ending in .gz or .bz2 will be uncompressed.

Returns:

G : graph

A NetworkX graph

References

[R1](1, 2) http://docs.python.org/library/pickle.html

Examples

>>> G=Orange.network.nx.path_graph(4)
>>> Orange.network.readwrite.write_gpickle(G,"test.gpickle")
>>> G=Orange.network.readwrite.read_gpickle("test.gpickle")
Orange.network.readwrite.write_gpickle(G, path)

NetworkX write_gpickle method.

Write graph in Python pickle format.

Pickles are a serialized byte stream of a Python object [R2]. This format will preserve Python objects used as nodes or edges.

Parameters:

G : graph

A NetworkX graph

path : file or string

File or filename to write. Filenames ending in .gz or .bz2 will be compressed.

References

[R2](1, 2) http://docs.python.org/library/pickle.html

Examples

>>> G=Orange.network.nx.path_graph(4)
>>> Orange.network.readwrite.write_gpickle(G,"test.gpickle")
Orange.network.readwrite.read_pajek(path, encoding='UTF-8', project=False, auto_table=False)

Reimplemented method for reading Pajek files; written in C++ for maximum performance.

Parameters:
  • path (string) – File or file name to write.
  • encoding (string) – Encoding of input text file, default ‘UTF-8’.
  • project (boolean.) – Determines whether the input file is a Pajek project file, possibly containing multiple networks and other data. If True, a list of networks is returned instead of just a network. Default is False.

Return the network (or a list of networks if project=:obj:True) of type Orange.network.Graph or Orange.network.DiGraph.

Examples

>>> G=Orange.network.nx.path_graph(4)
>>> Orange.network.readwrite.write_pajek(G, "test.net")
>>> G=Orange.network.readwrite.read_pajek("test.net")

To create a Graph instead of a MultiGraph use

>>> G1=Orange.network.Graph(G)

References

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

Orange.network.readwrite.write_pajek(G, path, encoding='UTF-8')

A copy & paste of NetworkX’s function with some bugs fixed (call the new generate_pajek).

Write graph in Pajek format to path.

Parameters:

G : graph

A Networkx graph

path : file or string

File or filename to write. Filenames ending in .gz or .bz2 will be compressed.

References

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

Examples

>>> G=Orange.network.nx.path_graph(4)
>>> Orange.network.readwrite.write_pajek(G, "test.net")
Orange.network.readwrite.parse_pajek(lines)

Parse string in Pajek file format. See read_pajek for usage examples.

Parameters:lines (string) – a string of network data in Pajek file format.
Orange.network.readwrite.generate_pajek(G)

A copy & paste of NetworkX’s function with some bugs fixed (generate one line per object: vertex, edge, arc. Do not add one per entry in data dictionary).

Generate lines in Pajek graph format.

Parameters:G (Orange.network.Graph) – A Orange graph.

References

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

Orange.network.readwrite.read_gml(path, encoding='latin-1', relabel=False, auto_table=False)

NetworkX read_gml method and wrap graph to Orange network.

Read graph in GML format from path.

Parameters:

path : filename or filehandle

The filename or filehandle to read from.

encoding : string, optional

Text encoding.

relabel : bool, optional

If True use the GML node label attribute for node names otherwise use the node id.

Returns:

G : MultiGraph or MultiDiGraph

Raises:

ImportError :

If the pyparsing module is not available.

See also

write_gml, parse_gml

Notes

Requires pyparsing: http://pyparsing.wikispaces.com/

References

GML specification: http://www.infosun.fim.uni-passau.de/Graphlet/GML/gml-tr.html

Examples

>>> G=Orange.network.nx.path_graph(4)
>>> Orange.network.readwrite.write_gml(G,'test.gml')
>>> H=Orange.network.readwrite.read_gml('test.gml')
Orange.network.readwrite.write_gml(G, path)

NetworkX write_gml method.

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

Parameters:

path : filename or filehandle

The filename or filehandle to write. Filenames ending in .gz or .gz2 will be compressed.

See also

read_gml, parse_gml

Notes

GML specifications indicate that the file should only use 7bit ASCII text encoding.iso8859-1 (latin-1).

This implementation does not support all Python data types as GML data. Nodes, node attributes, edge attributes, and graph attributes must be either dictionaries or single stings or numbers. If they are not an attempt is made to represent them as strings. For example, a list as edge data G[1][2][‘somedata’]=[1,2,3], will be represented in the GML file as:

edge [
  source 1
  target 2
  somedata "[1, 2, 3]"
]

Examples

>>> G=Orange.network.nx.path_graph(4)
>>> Orange.network.readwrite.write_gml(G,"test.gml")

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

>>> Orange.network.readwrite.write_gml(G,"test.gml.gz")