1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
from nose import SkipTest
from nose.tools import assert_raises, assert_true, assert_equal
import networkx as nx
from networkx.generators.classic import barbell_graph,cycle_graph,path_graph
class TestConvertNumpy(object):
numpy=1 # nosetests attribute, use nosetests -a 'not numpy' to skip test
@classmethod
def setupClass(cls):
global np
global np_assert_equal
try:
import numpy as np
np_assert_equal=np.testing.assert_equal
except ImportError:
raise SkipTest('NumPy not available.')
def __init__(self):
self.G1 = barbell_graph(10, 3)
self.G2 = cycle_graph(10, create_using=nx.DiGraph())
self.G3 = self.create_weighted(nx.Graph())
self.G4 = self.create_weighted(nx.DiGraph())
def create_weighted(self, G):
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)
G.add_weighted_edges_from(ex)
return G
def assert_equal(self, G1, G2):
assert_true( sorted(G1.nodes())==sorted(G2.nodes()) )
assert_true( sorted(G1.edges())==sorted(G2.edges()) )
def identity_conversion(self, G, A, create_using):
GG = nx.from_numpy_matrix(A, create_using=create_using)
self.assert_equal(G, GG)
GW = nx.to_networkx_graph(A, create_using=create_using)
self.assert_equal(G, GW)
GI = create_using.__class__(A)
self.assert_equal(G, GI)
def test_shape(self):
"Conversion from non-square array."
A=np.array([[1,2,3],[4,5,6]])
assert_raises(nx.NetworkXError, nx.from_numpy_matrix, A)
def test_identity_graph_matrix(self):
"Conversion from graph to matrix to graph."
A = nx.to_numpy_matrix(self.G1)
self.identity_conversion(self.G1, A, nx.Graph())
def test_identity_graph_array(self):
"Conversion from graph to array to graph."
A = nx.to_numpy_matrix(self.G1)
A = np.asarray(A)
self.identity_conversion(self.G1, A, nx.Graph())
def test_identity_digraph_matrix(self):
"""Conversion from digraph to matrix to digraph."""
A = nx.to_numpy_matrix(self.G2)
self.identity_conversion(self.G2, A, nx.DiGraph())
def test_identity_digraph_array(self):
"""Conversion from digraph to array to digraph."""
A = nx.to_numpy_matrix(self.G2)
A = np.asarray(A)
self.identity_conversion(self.G2, A, nx.DiGraph())
def test_identity_weighted_graph_matrix(self):
"""Conversion from weighted graph to matrix to weighted graph."""
A = nx.to_numpy_matrix(self.G3)
self.identity_conversion(self.G3, A, nx.Graph())
def test_identity_weighted_graph_array(self):
"""Conversion from weighted graph to array to weighted graph."""
A = nx.to_numpy_matrix(self.G3)
A = np.asarray(A)
self.identity_conversion(self.G3, A, nx.Graph())
def test_identity_weighted_digraph_matrix(self):
"""Conversion from weighted digraph to matrix to weighted digraph."""
A = nx.to_numpy_matrix(self.G4)
self.identity_conversion(self.G4, A, nx.DiGraph())
def test_identity_weighted_digraph_array(self):
"""Conversion from weighted digraph to array to weighted digraph."""
A = nx.to_numpy_matrix(self.G4)
A = np.asarray(A)
self.identity_conversion(self.G4, A, nx.DiGraph())
def test_nodelist(self):
"""Conversion from graph to matrix to graph with nodelist."""
P4 = path_graph(4)
P3 = path_graph(3)
nodelist = P3.nodes()
A = nx.to_numpy_matrix(P4, nodelist=nodelist)
GA = nx.Graph(A)
self.assert_equal(GA, P3)
# Make nodelist ambiguous by containing duplicates.
nodelist += [nodelist[0]]
assert_raises(nx.NetworkXError, nx.to_numpy_matrix, P3, nodelist=nodelist)
def test_weight_keyword(self):
WP4 = nx.Graph()
WP4.add_edges_from( (n,n+1,dict(weight=0.5,other=0.3)) for n in range(3) )
P4 = path_graph(4)
A = nx.to_numpy_matrix(P4)
np_assert_equal(A, nx.to_numpy_matrix(WP4,weight=None))
np_assert_equal(0.5*A, nx.to_numpy_matrix(WP4))
np_assert_equal(0.3*A, nx.to_numpy_matrix(WP4,weight='other'))
def test_from_numpy_matrix_type(self):
A=np.matrix([[1]])
G=nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']),int)
A=np.matrix([[1]]).astype(np.float)
G=nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']),float)
A=np.matrix([[1]]).astype(np.str)
G=nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']),str)
A=np.matrix([[1]]).astype(np.bool)
G=nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']),bool)
A=np.matrix([[1]]).astype(np.complex)
G=nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']),complex)
A=np.matrix([[1]]).astype(np.object)
assert_raises(TypeError,nx.from_numpy_matrix,A)
def test_from_numpy_matrix_dtype(self):
dt=[('weight',float),('cost',int)]
A=np.matrix([[(1.0,2)]],dtype=dt)
G=nx.from_numpy_matrix(A)
assert_equal(type(G[0][0]['weight']),float)
assert_equal(type(G[0][0]['cost']),int)
assert_equal(G[0][0]['cost'],2)
assert_equal(G[0][0]['weight'],1.0)
def test_to_numpy_recarray(self):
G=nx.Graph()
G.add_edge(1,2,weight=7.0,cost=5)
A=nx.to_numpy_recarray(G,dtype=[('weight',float),('cost',int)])
assert_equal(sorted(A.dtype.names),['cost','weight'])
assert_equal(A.weight[0,1],7.0)
assert_equal(A.weight[0,0],0.0)
assert_equal(A.cost[0,1],5)
assert_equal(A.cost[0,0],0)
def test_numpy_multigraph(self):
G=nx.MultiGraph()
G.add_edge(1,2,weight=7)
G.add_edge(1,2,weight=70)
A=nx.to_numpy_matrix(G)
assert_equal(A[1,0],77)
A=nx.to_numpy_matrix(G,multigraph_weight=min)
assert_equal(A[1,0],7)
A=nx.to_numpy_matrix(G,multigraph_weight=max)
assert_equal(A[1,0],70)
|