Convert
=======

>>> from networkx import *
>>> from networkx.operators import *
>>> from networkx.generators.classic import barbell_graph,cycle_graph,path_graph
>>> import numpy

Bad shape
---------

>>> A=numpy.array([[1,2,3],[4,5,6]])
>>> from_numpy_matrix(A)
Traceback (most recent call last):
...
NetworkXError: Adjacency matrix is not square. nx,ny=(2, 3)

Simple Graphs
--------------

>>> G=barbell_graph(10,3)

Numpy matrix
~~~~~~~~~~~~

>>> A=to_numpy_matrix(G)
>>> GG=from_numpy_matrix(A)
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(A)
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=Graph(A)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True


Numpy array
~~~~~~~~~~~~
>>> A=numpy.asarray(to_numpy_matrix(G))
>>> GG=from_numpy_matrix(A)
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(A)
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=Graph(A)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True

DiGraphs
--------

>>> G=cycle_graph(10,create_using=DiGraph())

Numpy matrix
~~~~~~~~~~~~

>>> A=to_numpy_matrix(G)
>>> GG=from_numpy_matrix(A,create_using=DiGraph())
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(A,create_using=DiGraph())
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=DiGraph(A)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True


Graph
------

>>> G=cycle_graph(4)
>>> e=G.edges()
>>> source=[u for u,v in e]
>>> dest=[v for u,v in e]
>>> weight=[s+10 for s in source]
>>> ex=zip(source,dest,weight)
>>> XG=Graph()
>>> XG.add_weighted_edges_from(ex)


Numpy matrix
~~~~~~~~~~~~

>>> A=to_numpy_matrix(XG)
>>> GG=from_numpy_matrix(A,create_using=Graph())
>>> sorted(XG.nodes())==sorted(GG.nodes())
True
>>> sorted(XG.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(A,create_using=Graph())
>>> sorted(XG.nodes())==sorted(GW.nodes())
True
>>> sorted(XG.edges())==sorted(GW.edges())
True
>>> GI=Graph(A)
>>> sorted(XG.nodes())==sorted(GI.nodes())
True
>>> sorted(XG.edges())==sorted(GI.edges())
True


DiGraph
------

>>> G=cycle_graph(4)
>>> e=G.edges()
>>> source=[u for u,v in e]
>>> dest=[v for u,v in e]
>>> weight=[s+10 for s in source]
>>> ex=zip(source,dest,weight)
>>> XG=DiGraph()
>>> XG.add_weighted_edges_from(ex)


Numpy matrix
~~~~~~~~~~~~

>>> A=to_numpy_matrix(XG)
>>> GG=from_numpy_matrix(A,create_using=DiGraph())
>>> sorted(XG.nodes())==sorted(GG.nodes())
True
>>> sorted(XG.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(A,create_using=DiGraph())
>>> sorted(XG.nodes())==sorted(GW.nodes())
True
>>> sorted(XG.edges())==sorted(GW.edges())
True
>>> GI=DiGraph(A)
>>> sorted(XG.nodes())==sorted(GI.nodes())
True
>>> sorted(XG.edges())==sorted(GI.edges())
True


With nodelist keyword
---------------------

>>> P4=path_graph(4)
>>> P3=path_graph(3)
>>> A=to_numpy_matrix(P4,nodelist=[0,1,2])
>>> GA=Graph(A)
>>> sorted(GA.nodes())==sorted(P3.nodes())
True
>>> sorted(GA.edges())==sorted(P3.edges())
True
