Reading and writing networks

When using networks in Orange data mining suite, I advise you not to use NetworkX reading and writing methods. Instead, use new methods provided in the Orange.network.readwrite module. If, for some reason, you have to use the original read / write methods, do not forget to cast the network (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

G=Orange.network.nx.path_graph(4) Orange.network.readwrite.write_gpickle(G,”test.gpickle”) G=Orange.network.readwrite.read_gpickle(“test.gpickle”)

See cPickle.

Orange.network.readwrite.write_gpickle(G, path)

NetworkX write_gpickle method.

Write graph object in Python pickle format.

This will preserve Python objects used as nodes or edges.

G=Orange.network.nx.path_graph(4) Orange.network.readwrite.write_gpickle(G,”test.gpickle”)

See cPickle.

Orange.network.readwrite.read_pajek(path, encoding=UTF-8, project=False, auto_table=False)

A completely reimplemented method for reading Pajek files. Written in C++ for maximum performance.

Parameters:
  • path (string) – File or filename 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 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.

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.

Returns :

G : MultiGraph or MultiDiGraph

Raises :

ImportError :

If the pyparsing module is not available.

See also

write_gml, parse_gml

Notes

This doesn’t implement the complete GML specification for nested attributes for graphs, edges, and nodes.

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

The output file will use the default text encoding on your system. It is possible to write files in other encodings by opening the file with the codecs module. See doc/examples/unicode.py for hints.

>>> G=Orange.network.nx.path_graph(4)
>>> import codecs
>>> fh=codecs.open('test.gml','w',encoding='iso8859-1')# use iso8859-1
>>> Orange.network.readwrite.write_gml(G,fh)

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

For nested attributes for graphs, nodes, and edges you should use dicts for the value of the attribute.

Examples

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

path can be a filehandle or a string with the name of the file.

>>> fh=open("test.gml",'w')
>>> Orange.network.readwrite.write_gml(G,fh)

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

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