ioftools / networkxMiCe / networkxmaster / networkx / classes / tests / test_multidigraph.py @ 5cef0f13
History  View  Annotate  Download (14.5 KB)
1 
#!/usr/bin/env python


2 
from nose.tools import * 
3 
from networkx.testing import assert_edges_equal 
4 
import networkx as nx 
5 
from test_multigraph import BaseMultiGraphTester, TestMultiGraph 
6 
from test_multigraph import TestEdgeSubgraph as TestMultiGraphEdgeSubgraph 
7  
8  
9 
class BaseMultiDiGraphTester(BaseMultiGraphTester): 
10 
def test_edges(self): 
11 
G = self.K3

12 
edges = [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)] 
13 
assert_equal(sorted(G.edges()), edges)

14 
assert_equal(sorted(G.edges(0)), [(0, 1), (0, 2)]) 
15 
assert_raises((KeyError, nx.NetworkXError), G.edges, 1) 
16  
17 
def test_edges_data(self): 
18 
G = self.K3

19 
edges = [(0, 1, {}), (0, 2, {}), (1, 0, {}), 
20 
(1, 2, {}), (2, 0, {}), (2, 1, {})] 
21 
assert_equal(sorted(G.edges(data=True)), edges) 
22 
assert_equal(sorted(G.edges(0, data=True)), [(0, 1, {}), (0, 2, {})]) 
23 
assert_raises((KeyError, nx.NetworkXError), G.neighbors, 1) 
24  
25 
def test_edges_multi(self): 
26 
G = self.K3

27 
assert_equal(sorted(G.edges()),

28 
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
29 
assert_equal(sorted(G.edges(0)), [(0, 1), (0, 2)]) 
30 
G.add_edge(0, 1) 
31 
assert_equal(sorted(G.edges()),

32 
[(0, 1), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
33  
34 
def test_out_edges(self): 
35 
G = self.K3

36 
assert_equal(sorted(G.out_edges()),

37 
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
38 
assert_equal(sorted(G.out_edges(0)), [(0, 1), (0, 2)]) 
39 
assert_raises((KeyError, nx.NetworkXError), G.out_edges, 1) 
40 
assert_equal(sorted(G.out_edges(0, keys=True)), [(0, 1, 0), (0, 2, 0)]) 
41  
42 
def test_out_edges_multi(self): 
43 
G = self.K3

44 
assert_equal(sorted(G.out_edges()),

45 
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
46 
assert_equal(sorted(G.out_edges(0)), [(0, 1), (0, 2)]) 
47 
G.add_edge(0, 1, 2) 
48 
assert_equal(sorted(G.out_edges()),

49 
[(0, 1), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
50  
51 
def test_out_edges_data(self): 
52 
G = self.K3

53 
assert_equal(sorted(G.edges(0, data=True)), [(0, 1, {}), (0, 2, {})]) 
54 
G.remove_edge(0, 1) 
55 
G.add_edge(0, 1, data=1) 
56 
assert_equal(sorted(G.edges(0, data=True)), 
57 
[(0, 1, {'data': 1}), (0, 2, {})]) 
58 
assert_equal(sorted(G.edges(0, data='data')), 
59 
[(0, 1, 1), (0, 2, None)]) 
60 
assert_equal(sorted(G.edges(0, data='data', default=1)), 
61 
[(0, 1, 1), (0, 2, 1)]) 
62  
63 
def test_in_edges(self): 
64 
G = self.K3

65 
assert_equal(sorted(G.in_edges()),

66 
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
67 
assert_equal(sorted(G.in_edges(0)), [(1, 0), (2, 0)]) 
68 
assert_raises((KeyError, nx.NetworkXError), G.in_edges, 1) 
69 
G.add_edge(0, 1, 2) 
70 
assert_equal(sorted(G.in_edges()),

71 
[(0, 1), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
72 
assert_equal(sorted(G.in_edges(0, keys=True)), [(1, 0, 0), (2, 0, 0)]) 
73  
74 
def test_in_edges_no_keys(self): 
75 
G = self.K3

76 
assert_equal(sorted(G.in_edges()),

77 
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
78 
assert_equal(sorted(G.in_edges(0)), [(1, 0), (2, 0)]) 
79 
G.add_edge(0, 1, 2) 
80 
assert_equal(sorted(G.in_edges()),

81 
[(0, 1), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]) 
82  
83 
assert_equal(sorted(G.in_edges(data=True, keys=False)), 
84 
[(0, 1, {}), (0, 1, {}), (0, 2, {}), (1, 0, {}), 
85 
(1, 2, {}), (2, 0, {}), (2, 1, {})]) 
86  
87 
def test_in_edges_data(self): 
88 
G = self.K3

89 
assert_equal(sorted(G.in_edges(0, data=True)), 
90 
[(1, 0, {}), (2, 0, {})]) 
91 
G.remove_edge(1, 0) 
92 
G.add_edge(1, 0, data=1) 
93 
assert_equal(sorted(G.in_edges(0, data=True)), 
94 
[(1, 0, {'data': 1}), (2, 0, {})]) 
95 
assert_equal(sorted(G.in_edges(0, data='data')), 
96 
[(1, 0, 1), (2, 0, None)]) 
97 
assert_equal(sorted(G.in_edges(0, data='data', default=1)), 
98 
[(1, 0, 1), (2, 0, 1)]) 
99  
100 
def is_shallow(self, H, G): 
101 
# graph

102 
assert_equal(G.graph['foo'], H.graph['foo']) 
103 
G.graph['foo'].append(1) 
104 
assert_equal(G.graph['foo'], H.graph['foo']) 
105 
# node

106 
assert_equal(G.nodes[0]['foo'], H.nodes[0]['foo']) 
107 
G.nodes[0]['foo'].append(1) 
108 
assert_equal(G.nodes[0]['foo'], H.nodes[0]['foo']) 
109 
# edge

110 
assert_equal(G[1][2][0]['foo'], H[1][2][0]['foo']) 
111 
G[1][2][0]['foo'].append(1) 
112 
assert_equal(G[1][2][0]['foo'], H[1][2][0]['foo']) 
113  
114 
def is_deep(self, H, G): 
115 
# graph

116 
assert_equal(G.graph['foo'], H.graph['foo']) 
117 
G.graph['foo'].append(1) 
118 
assert_not_equal(G.graph['foo'], H.graph['foo']) 
119 
# node

120 
assert_equal(G.nodes[0]['foo'], H.nodes[0]['foo']) 
121 
G.nodes[0]['foo'].append(1) 
122 
assert_not_equal(G.nodes[0]['foo'], H.nodes[0]['foo']) 
123 
# edge

124 
assert_equal(G[1][2][0]['foo'], H[1][2][0]['foo']) 
125 
G[1][2][0]['foo'].append(1) 
126 
assert_not_equal(G[1][2][0]['foo'], H[1][2][0]['foo']) 
127  
128 
def test_to_undirected(self): 
129 
# MultiDiGraph > MultiGraph changes number of edges so it is

130 
# not a copy operation... use is_shallow, not is_shallow_copy

131 
G = self.K3

132 
self.add_attributes(G)

133 
H = nx.MultiGraph(G) 
134 
# self.is_shallow(H,G)

135 
# the result is traversal order dependent so we

136 
# can't use the is_shallow() test here.

137 
try:

138 
assert_edges_equal(H.edges(), [(0, 1), (1, 2), (2, 0)]) 
139 
except AssertionError: 
140 
assert_edges_equal(H.edges(), [(0, 1), (1, 2), (1, 2), (2, 0)]) 
141 
H = G.to_undirected() 
142 
self.is_deep(H, G)

143  
144 
def test_has_successor(self): 
145 
G = self.K3

146 
assert_equal(G.has_successor(0, 1), True) 
147 
assert_equal(G.has_successor(0, 1), False) 
148  
149 
def test_successors(self): 
150 
G = self.K3

151 
assert_equal(sorted(G.successors(0)), [1, 2]) 
152 
assert_raises((KeyError, nx.NetworkXError), G.successors, 1) 
153  
154 
def test_has_predecessor(self): 
155 
G = self.K3

156 
assert_equal(G.has_predecessor(0, 1), True) 
157 
assert_equal(G.has_predecessor(0, 1), False) 
158  
159 
def test_predecessors(self): 
160 
G = self.K3

161 
assert_equal(sorted(G.predecessors(0)), [1, 2]) 
162 
assert_raises((KeyError, nx.NetworkXError), G.predecessors, 1) 
163  
164 
def test_degree(self): 
165 
G = self.K3

166 
assert_equal(sorted(G.degree()), [(0, 4), (1, 4), (2, 4)]) 
167 
assert_equal(dict(G.degree()), {0: 4, 1: 4, 2: 4}) 
168 
assert_equal(G.degree(0), 4) 
169 
assert_equal(list(G.degree(iter([0]))), [(0, 4)]) 
170 
G.add_edge(0, 1, weight=0.3, other=1.2) 
171 
assert_equal(sorted(G.degree(weight='weight')), 
172 
[(0, 4.3), (1, 4.3), (2, 4)]) 
173 
assert_equal(sorted(G.degree(weight='other')), 
174 
[(0, 5.2), (1, 5.2), (2, 4)]) 
175  
176 
def test_in_degree(self): 
177 
G = self.K3

178 
assert_equal(sorted(G.in_degree()), [(0, 2), (1, 2), (2, 2)]) 
179 
assert_equal(dict(G.in_degree()), {0: 2, 1: 2, 2: 2}) 
180 
assert_equal(G.in_degree(0), 2) 
181 
assert_equal(list(G.in_degree(iter([0]))), [(0, 2)]) 
182 
assert_equal(G.in_degree(0, weight='weight'), 2) 
183  
184 
def test_out_degree(self): 
185 
G = self.K3

186 
assert_equal(sorted(G.out_degree()), [(0, 2), (1, 2), (2, 2)]) 
187 
assert_equal(dict(G.out_degree()), {0: 2, 1: 2, 2: 2}) 
188 
assert_equal(G.out_degree(0), 2) 
189 
assert_equal(list(G.out_degree(iter([0]))), [(0, 2)]) 
190 
assert_equal(G.out_degree(0, weight='weight'), 2) 
191  
192 
def test_size(self): 
193 
G = self.K3

194 
assert_equal(G.size(), 6)

195 
assert_equal(G.number_of_edges(), 6)

196 
G.add_edge(0, 1, weight=0.3, other=1.2) 
197 
assert_equal(round(G.size(weight='weight'), 2), 6.3) 
198 
assert_equal(round(G.size(weight='other'), 2), 7.2) 
199  
200 
def test_to_undirected_reciprocal(self): 
201 
G = self.Graph()

202 
G.add_edge(1, 2) 
203 
assert_true(G.to_undirected().has_edge(1, 2)) 
204 
assert_false(G.to_undirected(reciprocal=True).has_edge(1, 2)) 
205 
G.add_edge(2, 1) 
206 
assert_true(G.to_undirected(reciprocal=True).has_edge(1, 2)) 
207  
208 
def test_reverse_copy(self): 
209 
G = nx.MultiDiGraph([(0, 1), (0, 1)]) 
210 
R = G.reverse() 
211 
assert_equal(sorted(R.edges()), [(1, 0), (1, 0)]) 
212 
R.remove_edge(1, 0) 
213 
assert_equal(sorted(R.edges()), [(1, 0)]) 
214 
assert_equal(sorted(G.edges()), [(0, 1), (0, 1)]) 
215  
216 
def test_reverse_nocopy(self): 
217 
G = nx.MultiDiGraph([(0, 1), (0, 1)]) 
218 
R = G.reverse(copy=False)

219 
assert_equal(sorted(R.edges()), [(1, 0), (1, 0)]) 
220 
assert_raises(nx.NetworkXError, R.remove_edge, 1, 0) 
221  
222  
223 
class TestMultiDiGraph(BaseMultiDiGraphTester, TestMultiGraph): 
224 
def setUp(self): 
225 
self.Graph = nx.MultiDiGraph

226 
# build K3

227 
self.k3edges = [(0, 1), (0, 2), (1, 2)] 
228 
self.k3nodes = [0, 1, 2] 
229 
self.K3 = self.Graph() 
230 
self.K3._adj = {0: {}, 1: {}, 2: {}} 
231 
self.K3._succ = self.K3._adj 
232 
self.K3._pred = {0: {}, 1: {}, 2: {}} 
233 
for u in self.k3nodes: 
234 
for v in self.k3nodes: 
235 
if u == v:

236 
continue

237 
d = {0: {}}

238 
self.K3._succ[u][v] = d

239 
self.K3._pred[v][u] = d

240 
self.K3._node = {}

241 
self.K3._node[0] = {} 
242 
self.K3._node[1] = {} 
243 
self.K3._node[2] = {} 
244  
245 
def test_add_edge(self): 
246 
G = self.Graph()

247 
G.add_edge(0, 1) 
248 
assert_equal(G._adj, {0: {1: {0: {}}}, 1: {}}) 
249 
assert_equal(G._succ, {0: {1: {0: {}}}, 1: {}}) 
250 
assert_equal(G._pred, {0: {}, 1: {0: {0: {}}}}) 
251 
G = self.Graph()

252 
G.add_edge(*(0, 1)) 
253 
assert_equal(G._adj, {0: {1: {0: {}}}, 1: {}}) 
254 
assert_equal(G._succ, {0: {1: {0: {}}}, 1: {}}) 
255 
assert_equal(G._pred, {0: {}, 1: {0: {0: {}}}}) 
256  
257 
def test_add_edges_from(self): 
258 
G = self.Graph()

259 
G.add_edges_from([(0, 1), (0, 1, {'weight': 3})]) 
260 
assert_equal(G._adj, {0: {1: {0: {}, 1: {'weight': 3}}}, 1: {}}) 
261 
assert_equal(G._succ, {0: {1: {0: {}, 1: {'weight': 3}}}, 1: {}}) 
262 
assert_equal(G._pred, {0: {}, 1: {0: {0: {}, 1: {'weight': 3}}}}) 
263  
264 
G.add_edges_from([(0, 1), (0, 1, {'weight': 3})], weight=2) 
265 
assert_equal(G._succ, {0: {1: {0: {}, 
266 
1: {'weight': 3}, 
267 
2: {'weight': 2}, 
268 
3: {'weight': 3}}}, 
269 
1: {}})

270 
assert_equal(G._pred, {0: {}, 1: {0: {0: {}, 1: {'weight': 3}, 
271 
2: {'weight': 2}, 
272 
3: {'weight': 3}}}}) 
273  
274 
G = self.Graph()

275 
edges = [(0, 1, {'weight': 3}), (0, 1, (('weight', 2),)), 
276 
(0, 1, 5), (0, 1, 's')] 
277 
G.add_edges_from(edges) 
278 
keydict = {0: {'weight': 3}, 1: {'weight': 2}, 5: {}, 's': {}} 
279 
assert_equal(G._succ, {0: {1: keydict}, 1: {}}) 
280 
assert_equal(G._pred, {1: {0: keydict}, 0: {}}) 
281  
282 
# too few in tuple

283 
assert_raises(nx.NetworkXError, G.add_edges_from, [(0,)])

284 
# too many in tuple

285 
assert_raises(nx.NetworkXError, G.add_edges_from, [(0, 1, 2, 3, 4)]) 
286 
# not a tuple

287 
assert_raises(TypeError, G.add_edges_from, [0]) 
288  
289 
def test_remove_edge(self): 
290 
G = self.K3

291 
G.remove_edge(0, 1) 
292 
assert_equal(G._succ, {0: {2: {0: {}}}, 
293 
1: {0: {0: {}}, 2: {0: {}}}, 
294 
2: {0: {0: {}}, 1: {0: {}}}}) 
295 
assert_equal(G._pred, {0: {1: {0: {}}, 2: {0: {}}}, 
296 
1: {2: {0: {}}}, 
297 
2: {0: {0: {}}, 1: {0: {}}}}) 
298 
assert_raises((KeyError, nx.NetworkXError), G.remove_edge, 1, 0) 
299 
assert_raises((KeyError, nx.NetworkXError), G.remove_edge, 0, 2, 
300 
key=1)

301  
302 
def test_remove_multiedge(self): 
303 
G = self.K3

304 
G.add_edge(0, 1, key='parallel edge') 
305 
G.remove_edge(0, 1, key='parallel edge') 
306 
assert_equal(G._adj, {0: {1: {0: {}}, 2: {0: {}}}, 
307 
1: {0: {0: {}}, 2: {0: {}}}, 
308 
2: {0: {0: {}}, 1: {0: {}}}}) 
309  
310 
assert_equal(G._succ, {0: {1: {0: {}}, 2: {0: {}}}, 
311 
1: {0: {0: {}}, 2: {0: {}}}, 
312 
2: {0: {0: {}}, 1: {0: {}}}}) 
313  
314 
assert_equal(G._pred, {0: {1: {0: {}}, 2: {0: {}}}, 
315 
1: {0: {0: {}}, 2: {0: {}}}, 
316 
2: {0: {0: {}}, 1: {0: {}}}}) 
317 
G.remove_edge(0, 1) 
318 
assert_equal(G._succ, {0: {2: {0: {}}}, 
319 
1: {0: {0: {}}, 2: {0: {}}}, 
320 
2: {0: {0: {}}, 1: {0: {}}}}) 
321 
assert_equal(G._pred, {0: {1: {0: {}}, 2: {0: {}}}, 
322 
1: {2: {0: {}}}, 
323 
2: {0: {0: {}}, 1: {0: {}}}}) 
324 
assert_raises((KeyError, nx.NetworkXError), G.remove_edge, 1, 0) 
325  
326 
def test_remove_edges_from(self): 
327 
G = self.K3

328 
G.remove_edges_from([(0, 1)]) 
329 
assert_equal(G._succ, {0: {2: {0: {}}}, 
330 
1: {0: {0: {}}, 2: {0: {}}}, 
331 
2: {0: {0: {}}, 1: {0: {}}}}) 
332 
assert_equal(G._pred, {0: {1: {0: {}}, 2: {0: {}}}, 
333 
1: {2: {0: {}}}, 
334 
2: {0: {0: {}}, 1: {0: {}}}}) 
335 
G.remove_edges_from([(0, 0)]) # silent fail 
336  
337  
338 
class TestEdgeSubgraph(TestMultiGraphEdgeSubgraph): 
339 
"""Unit tests for the :meth:`MultiDiGraph.edge_subgraph` method."""

340  
341 
def setup(self): 
342 
# Create a quadruplylinked path graph on five nodes.

343 
G = nx.MultiDiGraph() 
344 
nx.add_path(G, range(5)) 
345 
nx.add_path(G, range(5)) 
346 
nx.add_path(G, reversed(range(5))) 
347 
nx.add_path(G, reversed(range(5))) 
348 
# Add some node, edge, and graph attributes.

349 
for i in range(5): 
350 
G.nodes[i]['name'] = 'node{}'.format(i) 
351 
G.adj[0][1][0]['name'] = 'edge010' 
352 
G.adj[0][1][1]['name'] = 'edge011' 
353 
G.adj[3][4][0]['name'] = 'edge340' 
354 
G.adj[3][4][1]['name'] = 'edge341' 
355 
G.graph['name'] = 'graph' 
356 
# Get the subgraph induced by one of the first edges and one of

357 
# the last edges.

358 
self.G = G

359 
self.H = G.edge_subgraph([(0, 1, 0), (3, 4, 1)]) 