Package networkx :: Module convert
[hide private]
[frames] | no frames]

Module convert

source code

Convert NetworkX graphs to and from other formats.

from_whatever attemps to guess the input format

Create a 10 node random digraph

>>> from networkx import *
>>> import numpy
>>> a=numpy.reshape(numpy.random.random_integers(0,1,size=100),(10,10))
>>> D=from_whatever(a,create_using=DiGraph()) # or D=DiGraph(a)

For graphviz formats see networkx.drawing.nx_pygraphviz or networkx.drawing.nx_pydot.

$Id: convert.py 701 2007-11-08 05:08:53Z aric $




Author: Aric Hagberg (hagberg@lanl.gov)

Functions [hide private]
 
_prep_create_using(create_using)
Returns a graph object ready to be populated.
source code
 
from_whatever(thing, create_using=None)
Attempt to make a NetworkX graph from an known type.
source code
 
to_dict_of_lists(G, nodelist=None)
Return graph G as a Python dict of lists.
source code
 
from_dict_of_lists(d, create_using=None)
Return a NetworkX graph G from a Python dict of lists.
source code
 
to_dict_of_dicts(G, nodelist=None, edge_data=None)
Return graph G as a Python dictionary of dictionaries.
source code
 
from_dict_of_dicts(d, create_using=None)
Return a NetworkX graph G from a Python dictionary of dictionaries.
source code
 
to_numpy_matrix(G, nodelist=None)
Return adjacency matrix of graph as a numpy matrix.
source code
 
from_numpy_matrix(A, create_using=None)
Return networkx graph G from numpy matrix adjacency list.
source code
 
to_scipy_sparse_matrix(G, nodelist=None)
Return adjacency matrix of graph as a scipy sparse matrix.
source code
 
from_scipy_sparse_matrix(A, create_using=None)
Return networkx graph G from scipy scipy sparse matrix adjacency list.
source code
 
_test_suite() source code
 
_test_suite_numpy() source code
 
_test_suite_scipy() source code
Function Details [hide private]

_prep_create_using(create_using)

source code 

Returns a graph object ready to be populated.

If create_using is None return the default (just networkx.Graph()) If create_using.clear() works, assume it returns a graph object. Otherwise raise an exception because create_using is not a networkx graph.

from_whatever(thing, create_using=None)

source code 

Attempt to make a NetworkX graph from an known type.

Current known types are:

any NetworkX graph dict-of-dicts dist-of-lists numpy matrix numpy ndarray scipy sparse matrix pygraphviz agraph

to_dict_of_lists(G, nodelist=None)

source code 

Return graph G as a Python dict of lists.

If nodelist is defined return a dict of lists with only those nodes.

Completely ignores edge data for XGraph and XDiGraph.

to_dict_of_dicts(G, nodelist=None, edge_data=None)

source code 

Return graph G as a Python dictionary of dictionaries.

If nodelist is defined return a dict of dicts with only those nodes.

If edge_data is given, the value of the dictionary will be set to edge_data for all edges. This is useful to make an adjacency matrix type representation with 1 as the edge data.

from_dict_of_dicts(d, create_using=None)

source code 

Return a NetworkX graph G from a Python dictionary of dictionaries.

The value of the inner dict becomes the edge_data for the NetworkX graph EVEN if create_using is a NetworkX Graph which doesn't ever use this data.

If create_using is an XGraph/XDiGraph with multiedges==True, the edge_data should be a list, though this routine does not check for that.

to_numpy_matrix(G, nodelist=None)

source code 

Return adjacency matrix of graph as a numpy matrix.

If nodelist is defined return adjacency matrix with nodes in nodelist in the order specified. If not the ordering is whatever order the method G.nodes() produces.

For Graph/DiGraph types which have no edge data The value of the entry A[u,v] is one if there is an edge u-v and zero otherwise.

For XGraph/XDiGraph the edge data is assumed to be a weight and be able to be converted to a valid numpy type (e.g. an int or a float). The value of the entry A[u,v] is the weight given by get_edge(u,v) one if there is an edge u-v and zero otherwise.

Graphs with multi-edges are not handled.

to_scipy_sparse_matrix(G, nodelist=None)

source code 

Return adjacency matrix of graph as a scipy sparse matrix.

Uses lil_matrix format. To convert to other formats see scipy.sparse documentation.

If nodelist is defined return adjacency matrix with nodes in nodelist in the order specified. If not the ordering is whatever order the method G.nodes() produces.

For Graph/DiGraph types which have no edge data The value of the entry A[u,v] is one if there is an edge u-v and zero otherwise.

For XGraph/XDiGraph the edge data is assumed to be a weight and be able to be converted to a valid numpy type (e.g. an int or a float). The value of the entry A[u,v] is the weight given by get_edge(u,v) one if there is an edge u-v and zero otherwise.

Graphs with multi-edges are not handled.

>>> A=scipy_sparse_matrix(G)
>>> A.tocsr() # convert to compressed row storage