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


2 
import random 
3 
from nose.tools import * 
4 
import networkx as nx 
5 
from networkx.testing.utils import * 
6  
7  
8 
class TestFunction(object): 
9 
def setUp(self): 
10 
self.G = nx.Graph({0: [1, 2, 3], 1: [1, 2, 0], 4: []}, name='Test') 
11 
self.Gdegree = {0: 3, 1: 2, 2: 2, 3: 1, 4: 0} 
12 
self.Gnodes = list(range(5)) 
13 
self.Gedges = [(0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2)] 
14 
self.DG = nx.DiGraph({0: [1, 2, 3], 1: [1, 2, 0], 4: []}) 
15 
self.DGin_degree = {0: 1, 1: 2, 2: 2, 3: 1, 4: 0} 
16 
self.DGout_degree = {0: 3, 1: 3, 2: 0, 3: 0, 4: 0} 
17 
self.DGnodes = list(range(5)) 
18 
self.DGedges = [(0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2)] 
19  
20 
def test_nodes(self): 
21 
assert_nodes_equal(self.G.nodes(), list(nx.nodes(self.G))) 
22 
assert_nodes_equal(self.DG.nodes(), list(nx.nodes(self.DG))) 
23  
24 
def test_edges(self): 
25 
assert_edges_equal(self.G.edges(), list(nx.edges(self.G))) 
26 
assert_equal(sorted(self.DG.edges()), sorted(nx.edges(self.DG))) 
27 
assert_edges_equal(self.G.edges(nbunch=[0, 1, 3]), 
28 
list(nx.edges(self.G, nbunch=[0, 1, 3]))) 
29 
assert_equal(sorted(self.DG.edges(nbunch=[0, 1, 3])), 
30 
sorted(nx.edges(self.DG, nbunch=[0, 1, 3]))) 
31  
32 
def test_degree(self): 
33 
assert_edges_equal(self.G.degree(), list(nx.degree(self.G))) 
34 
assert_equal(sorted(self.DG.degree()), sorted(nx.degree(self.DG))) 
35 
assert_edges_equal(self.G.degree(nbunch=[0, 1]), 
36 
list(nx.degree(self.G, nbunch=[0, 1]))) 
37 
assert_equal(sorted(self.DG.degree(nbunch=[0, 1])), 
38 
sorted(nx.degree(self.DG, nbunch=[0, 1]))) 
39 
assert_edges_equal(self.G.degree(weight='weight'), 
40 
list(nx.degree(self.G, weight='weight'))) 
41 
assert_equal(sorted(self.DG.degree(weight='weight')), 
42 
sorted(nx.degree(self.DG, weight='weight'))) 
43  
44 
def test_neighbors(self): 
45 
assert_equal(list(self.G.neighbors(1)), list(nx.neighbors(self.G, 1))) 
46 
assert_equal(list(self.DG.neighbors(1)), list(nx.neighbors(self.DG, 1))) 
47  
48 
def test_number_of_nodes(self): 
49 
assert_equal(self.G.number_of_nodes(), nx.number_of_nodes(self.G)) 
50 
assert_equal(self.DG.number_of_nodes(), nx.number_of_nodes(self.DG)) 
51  
52 
def test_number_of_edges(self): 
53 
assert_equal(self.G.number_of_edges(), nx.number_of_edges(self.G)) 
54 
assert_equal(self.DG.number_of_edges(), nx.number_of_edges(self.DG)) 
55  
56 
def test_is_directed(self): 
57 
assert_equal(self.G.is_directed(), nx.is_directed(self.G)) 
58 
assert_equal(self.DG.is_directed(), nx.is_directed(self.DG)) 
59  
60 
def test_add_star(self): 
61 
G = self.G.copy()

62 
nlist = [12, 13, 14, 15] 
63 
nx.add_star(G, nlist) 
64 
assert_edges_equal(G.edges(nlist), [(12, 13), (12, 14), (12, 15)]) 
65  
66 
G = self.G.copy()

67 
nx.add_star(G, nlist, weight=2.0)

68 
assert_edges_equal(G.edges(nlist, data=True),

69 
[(12, 13, {'weight': 2.}), 
70 
(12, 14, {'weight': 2.}), 
71 
(12, 15, {'weight': 2.})]) 
72  
73 
G = self.G.copy()

74 
nlist = [12]

75 
nx.add_star(G, nlist) 
76 
assert_nodes_equal(G, list(self.G) + nlist) 
77  
78 
G = self.G.copy()

79 
nlist = [] 
80 
nx.add_star(G, nlist) 
81 
assert_nodes_equal(G.nodes, self.Gnodes)

82 
assert_edges_equal(G.edges, self.G.edges)

83  
84 
def test_add_path(self): 
85 
G = self.G.copy()

86 
nlist = [12, 13, 14, 15] 
87 
nx.add_path(G, nlist) 
88 
assert_edges_equal(G.edges(nlist), [(12, 13), (13, 14), (14, 15)]) 
89 
G = self.G.copy()

90 
nx.add_path(G, nlist, weight=2.0)

91 
assert_edges_equal(G.edges(nlist, data=True),

92 
[(12, 13, {'weight': 2.}), 
93 
(13, 14, {'weight': 2.}), 
94 
(14, 15, {'weight': 2.})]) 
95  
96 
G = self.G.copy()

97 
nlist = [None]

98 
nx.add_path(G, nlist) 
99 
assert_edges_equal(G.edges(nlist), []) 
100 
assert_nodes_equal(G, list(self.G) + [None]) 
101  
102 
G = self.G.copy()

103 
nlist = iter([None]) 
104 
nx.add_path(G, nlist) 
105 
assert_edges_equal(G.edges([None]), [])

106 
assert_nodes_equal(G, list(self.G) + [None]) 
107  
108 
G = self.G.copy()

109 
nlist = [12]

110 
nx.add_path(G, nlist) 
111 
assert_edges_equal(G.edges(nlist), []) 
112 
assert_nodes_equal(G, list(self.G) + [12]) 
113  
114 
G = self.G.copy()

115 
nlist = iter([12]) 
116 
nx.add_path(G, nlist) 
117 
assert_edges_equal(G.edges([12]), [])

118 
assert_nodes_equal(G, list(self.G) + [12]) 
119  
120 
G = self.G.copy()

121 
nlist = [] 
122 
nx.add_path(G, nlist) 
123 
assert_edges_equal(G.edges, self.G.edges)

124 
assert_nodes_equal(G, list(self.G)) 
125  
126 
G = self.G.copy()

127 
nlist = iter([])

128 
nx.add_path(G, nlist) 
129 
assert_edges_equal(G.edges, self.G.edges)

130 
assert_nodes_equal(G, list(self.G)) 
131  
132 
def test_add_cycle(self): 
133 
G = self.G.copy()

134 
nlist = [12, 13, 14, 15] 
135 
oklists = [[(12, 13), (12, 15), (13, 14), (14, 15)], 
136 
[(12, 13), (13, 14), (14, 15), (15, 12)]] 
137 
nx.add_cycle(G, nlist) 
138 
assert_true(sorted(G.edges(nlist)) in oklists) 
139 
G = self.G.copy()

140 
oklists = [[(12, 13, {'weight': 1.}), 
141 
(12, 15, {'weight': 1.}), 
142 
(13, 14, {'weight': 1.}), 
143 
(14, 15, {'weight': 1.})], 
144 
[(12, 13, {'weight': 1.}), 
145 
(13, 14, {'weight': 1.}), 
146 
(14, 15, {'weight': 1.}), 
147 
(15, 12, {'weight': 1.})]] 
148 
nx.add_cycle(G, nlist, weight=1.0)

149 
assert_true(sorted(G.edges(nlist, data=True)) in oklists) 
150  
151 
G = self.G.copy()

152 
nlist = [12]

153 
nx.add_cycle(G, nlist) 
154 
assert_nodes_equal(G, list(self.G) + nlist) 
155  
156 
G = self.G.copy()

157 
nlist = [] 
158 
nx.add_cycle(G, nlist) 
159 
assert_nodes_equal(G.nodes, self.Gnodes)

160 
assert_edges_equal(G.edges, self.G.edges)

161  
162 
def test_subgraph(self): 
163 
assert_equal(self.G.subgraph([0, 1, 2, 4]).adj, 
164 
nx.subgraph(self.G, [0, 1, 2, 4]).adj) 
165 
assert_equal(self.DG.subgraph([0, 1, 2, 4]).adj, 
166 
nx.subgraph(self.DG, [0, 1, 2, 4]).adj) 
167 
assert_equal(self.G.subgraph([0, 1, 2, 4]).adj, 
168 
nx.induced_subgraph(self.G, [0, 1, 2, 4]).adj) 
169 
assert_equal(self.DG.subgraph([0, 1, 2, 4]).adj, 
170 
nx.induced_subgraph(self.DG, [0, 1, 2, 4]).adj) 
171 
# subgraphsubgraph chain is allowed in function interface

172 
H = nx.induced_subgraph(self.G.subgraph([0, 1, 2, 4]), [0, 1, 4]) 
173 
assert_is_not(H._graph, self.G)

174 
assert_equal(H.adj, self.G.subgraph([0, 1, 4]).adj) 
175  
176 
def test_edge_subgraph(self): 
177 
assert_equal(self.G.edge_subgraph([(1, 2), (0, 3)]).adj, 
178 
nx.edge_subgraph(self.G, [(1, 2), (0, 3)]).adj) 
179 
assert_equal(self.DG.edge_subgraph([(1, 2), (0, 3)]).adj, 
180 
nx.edge_subgraph(self.DG, [(1, 2), (0, 3)]).adj) 
181  
182 
def test_restricted_view(self): 
183 
H = nx.restricted_view(self.G, [0, 2, 5], [(1, 2), (3, 4)]) 
184 
assert_equal(set(H.nodes), {1, 3, 4}) 
185 
assert_equal(set(H.edges), {(1, 1)}) 
186  
187 
def test_create_empty_copy(self): 
188 
G = nx.create_empty_copy(self.G, with_data=False) 
189 
assert_nodes_equal(G, list(self.G)) 
190 
assert_equal(G.graph, {}) 
191 
assert_equal(G._node, {}.fromkeys(self.G.nodes(), {}))

192 
assert_equal(G._adj, {}.fromkeys(self.G.nodes(), {}))

193 
G = nx.create_empty_copy(self.G)

194 
assert_nodes_equal(G, list(self.G)) 
195 
assert_equal(G.graph, self.G.graph)

196 
assert_equal(G._node, self.G._node)

197 
assert_equal(G._adj, {}.fromkeys(self.G.nodes(), {}))

198  
199 
def test_degree_histogram(self): 
200 
assert_equal(nx.degree_histogram(self.G), [1, 1, 1, 1, 1]) 
201  
202 
def test_density(self): 
203 
assert_equal(nx.density(self.G), 0.5) 
204 
assert_equal(nx.density(self.DG), 0.3) 
205 
G = nx.Graph() 
206 
G.add_node(1)

207 
assert_equal(nx.density(G), 0.0)

208  
209 
def test_density_selfloop(self): 
210 
G = nx.Graph() 
211 
G.add_edge(1, 1) 
212 
assert_equal(nx.density(G), 0.0)

213 
G.add_edge(1, 2) 
214 
assert_equal(nx.density(G), 2.0)

215  
216 
def test_freeze(self): 
217 
G = nx.freeze(self.G)

218 
assert_equal(G.frozen, True)

219 
assert_raises(nx.NetworkXError, G.add_node, 1)

220 
assert_raises(nx.NetworkXError, G.add_nodes_from, [1])

221 
assert_raises(nx.NetworkXError, G.remove_node, 1)

222 
assert_raises(nx.NetworkXError, G.remove_nodes_from, [1])

223 
assert_raises(nx.NetworkXError, G.add_edge, 1, 2) 
224 
assert_raises(nx.NetworkXError, G.add_edges_from, [(1, 2)]) 
225 
assert_raises(nx.NetworkXError, G.remove_edge, 1, 2) 
226 
assert_raises(nx.NetworkXError, G.remove_edges_from, [(1, 2)]) 
227 
assert_raises(nx.NetworkXError, G.clear) 
228  
229 
def test_is_frozen(self): 
230 
assert_equal(nx.is_frozen(self.G), False) 
231 
G = nx.freeze(self.G)

232 
assert_equal(G.frozen, nx.is_frozen(self.G))

233 
assert_equal(G.frozen, True)

234  
235 
def test_info(self): 
236 
G = nx.path_graph(5)

237 
G.name = "path_graph(5)"

238 
info = nx.info(G) 
239 
expected_graph_info = '\n'.join(['Name: path_graph(5)', 
240 
'Type: Graph',

241 
'Number of nodes: 5',

242 
'Number of edges: 4',

243 
'Average degree: 1.6000'])

244 
assert_equal(info, expected_graph_info) 
245  
246 
info = nx.info(G, n=1)

247 
expected_node_info = '\n'.join(

248 
['Node 1 has the following properties:',

249 
'Degree: 2',

250 
'Neighbors: 0 2'])

251 
assert_equal(info, expected_node_info) 
252  
253 
def test_info_digraph(self): 
254 
G = nx.DiGraph(name='path_graph(5)')

255 
nx.add_path(G, [0, 1, 2, 3, 4]) 
256 
info = nx.info(G) 
257 
expected_graph_info = '\n'.join(['Name: path_graph(5)', 
258 
'Type: DiGraph',

259 
'Number of nodes: 5',

260 
'Number of edges: 4',

261 
'Average in degree: 0.8000',

262 
'Average out degree: 0.8000'])

263 
assert_equal(info, expected_graph_info) 
264  
265 
info = nx.info(G, n=1)

266 
expected_node_info = '\n'.join(

267 
['Node 1 has the following properties:',

268 
'Degree: 2',

269 
'Neighbors: 2'])

270 
assert_equal(info, expected_node_info) 
271  
272 
assert_raises(nx.NetworkXError, nx.info, G, n=1)

273  
274 
def test_neighbors_complete_graph(self): 
275 
graph = nx.complete_graph(100)

276 
pop = random.sample(list(graph), 1) 
277 
nbors = list(nx.neighbors(graph, pop[0])) 
278 
# should be all the other vertices in the graph

279 
assert_equal(len(nbors), len(graph)  1) 
280  
281 
graph = nx.path_graph(100)

282 
node = random.sample(list(graph), 1)[0] 
283 
nbors = list(nx.neighbors(graph, node))

284 
# should be all the other vertices in the graph

285 
if node != 0 and node != 99: 
286 
assert_equal(len(nbors), 2) 
287 
else:

288 
assert_equal(len(nbors), 1) 
289  
290 
# create a star graph with 99 outer nodes

291 
graph = nx.star_graph(99)

292 
nbors = list(nx.neighbors(graph, 0)) 
293 
assert_equal(len(nbors), 99) 
294  
295 
def test_non_neighbors(self): 
296 
graph = nx.complete_graph(100)

297 
pop = random.sample(list(graph), 1) 
298 
nbors = list(nx.non_neighbors(graph, pop[0])) 
299 
# should be all the other vertices in the graph

300 
assert_equal(len(nbors), 0) 
301  
302 
graph = nx.path_graph(100)

303 
node = random.sample(list(graph), 1)[0] 
304 
nbors = list(nx.non_neighbors(graph, node))

305 
# should be all the other vertices in the graph

306 
if node != 0 and node != 99: 
307 
assert_equal(len(nbors), 97) 
308 
else:

309 
assert_equal(len(nbors), 98) 
310  
311 
# create a star graph with 99 outer nodes

312 
graph = nx.star_graph(99)

313 
nbors = list(nx.non_neighbors(graph, 0)) 
314 
assert_equal(len(nbors), 0) 
315  
316 
# disconnected graph

317 
graph = nx.Graph() 
318 
graph.add_nodes_from(range(10)) 
319 
nbors = list(nx.non_neighbors(graph, 0)) 
320 
assert_equal(len(nbors), 9) 
321  
322 
def test_non_edges(self): 
323 
# All possible edges exist

324 
graph = nx.complete_graph(5)

325 
nedges = list(nx.non_edges(graph))

326 
assert_equal(len(nedges), 0) 
327  
328 
graph = nx.path_graph(4)

329 
expected = [(0, 2), (0, 3), (1, 3)] 
330 
nedges = list(nx.non_edges(graph))

331 
for (u, v) in expected: 
332 
assert_true((u, v) in nedges or (v, u) in nedges) 
333  
334 
graph = nx.star_graph(4)

335 
expected = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] 
336 
nedges = list(nx.non_edges(graph))

337 
for (u, v) in expected: 
338 
assert_true((u, v) in nedges or (v, u) in nedges) 
339  
340 
# Directed graphs

341 
graph = nx.DiGraph() 
342 
graph.add_edges_from([(0, 2), (2, 0), (2, 1)]) 
343 
expected = [(0, 1), (1, 0), (1, 2)] 
344 
nedges = list(nx.non_edges(graph))

345 
for e in expected: 
346 
assert_true(e in nedges)

347  
348 
def test_is_weighted(self): 
349 
G = nx.Graph() 
350 
assert_false(nx.is_weighted(G)) 
351  
352 
G = nx.path_graph(4)

353 
assert_false(nx.is_weighted(G)) 
354 
assert_false(nx.is_weighted(G, (2, 3))) 
355  
356 
G.add_node(4)

357 
G.add_edge(3, 4, weight=4) 
358 
assert_false(nx.is_weighted(G)) 
359 
assert_true(nx.is_weighted(G, (3, 4))) 
360  
361 
G = nx.DiGraph() 
362 
G.add_weighted_edges_from([('0', '3', 3), ('0', '1', 5), 
363 
('1', '0', 5), ('0', '2', 2), 
364 
('1', '2', 4), ('2', '3', 1)]) 
365 
assert_true(nx.is_weighted(G)) 
366 
assert_true(nx.is_weighted(G, ('1', '0'))) 
367  
368 
G = G.to_undirected() 
369 
assert_true(nx.is_weighted(G)) 
370 
assert_true(nx.is_weighted(G, ('1', '0'))) 
371  
372 
assert_raises(nx.NetworkXError, nx.is_weighted, G, (1, 2)) 
373  
374 
def test_is_negatively_weighted(self): 
375 
G = nx.Graph() 
376 
assert_false(nx.is_negatively_weighted(G)) 
377  
378 
G.add_node(1)

379 
G.add_nodes_from([2, 3, 4, 5]) 
380 
assert_false(nx.is_negatively_weighted(G)) 
381  
382 
G.add_edge(1, 2, weight=4) 
383 
assert_false(nx.is_negatively_weighted(G, (1, 2))) 
384  
385 
G.add_edges_from([(1, 3), (2, 4), (2, 6)]) 
386 
G[1][3]['color'] = 'blue' 
387 
assert_false(nx.is_negatively_weighted(G)) 
388 
assert_false(nx.is_negatively_weighted(G, (1, 3))) 
389  
390 
G[2][4]['weight'] = 2 
391 
assert_true(nx.is_negatively_weighted(G, (2, 4))) 
392 
assert_true(nx.is_negatively_weighted(G)) 
393  
394 
G = nx.DiGraph() 
395 
G.add_weighted_edges_from([('0', '3', 3), ('0', '1', 5), 
396 
('1', '0', 2), ('0', '2', 2), 
397 
('1', '2', 3), ('2', '3', 1)]) 
398 
assert_true(nx.is_negatively_weighted(G)) 
399 
assert_false(nx.is_negatively_weighted(G, ('0', '3'))) 
400 
assert_true(nx.is_negatively_weighted(G, ('1', '0'))) 
401  
402 
assert_raises(nx.NetworkXError, nx.is_negatively_weighted, G, (1, 4)) 
403  
404  
405 
class TestCommonNeighbors(): 
406 
def setUp(self): 
407 
self.func = nx.common_neighbors

408  
409 
def test_func(G, u, v, expected): 
410 
result = sorted(self.func(G, u, v)) 
411 
assert_equal(result, expected) 
412 
self.test = test_func

413  
414 
def test_K5(self): 
415 
G = nx.complete_graph(5)

416 
self.test(G, 0, 1, [2, 3, 4]) 
417  
418 
def test_P3(self): 
419 
G = nx.path_graph(3)

420 
self.test(G, 0, 2, [1]) 
421  
422 
def test_S4(self): 
423 
G = nx.star_graph(4)

424 
self.test(G, 1, 2, [0]) 
425  
426 
@raises(nx.NetworkXNotImplemented)

427 
def test_digraph(self): 
428 
G = nx.DiGraph() 
429 
G.add_edges_from([(0, 1), (1, 2)]) 
430 
self.func(G, 0, 2) 
431  
432 
def test_nonexistent_nodes(self): 
433 
G = nx.complete_graph(5)

434 
assert_raises(nx.NetworkXError, nx.common_neighbors, G, 5, 4) 
435 
assert_raises(nx.NetworkXError, nx.common_neighbors, G, 4, 5) 
436 
assert_raises(nx.NetworkXError, nx.common_neighbors, G, 5, 6) 
437  
438 
def test_custom1(self): 
439 
"""Case of no common neighbors."""

440 
G = nx.Graph() 
441 
G.add_nodes_from([0, 1]) 
442 
self.test(G, 0, 1, []) 
443  
444 
def test_custom2(self): 
445 
"""Case of equal nodes."""

446 
G = nx.complete_graph(4)

447 
self.test(G, 0, 0, [1, 2, 3]) 
448  
449  
450 
def test_set_node_attributes(): 
451 
graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] 
452 
for G in graphs: 
453 
# Test single value

454 
G = nx.path_graph(3, create_using=G)

455 
vals = 100

456 
attr = 'hello'

457 
nx.set_node_attributes(G, vals, attr) 
458 
assert_equal(G.nodes[0][attr], vals)

459 
assert_equal(G.nodes[1][attr], vals)

460 
assert_equal(G.nodes[2][attr], vals)

461  
462 
# Test dictionary

463 
G = nx.path_graph(3, create_using=G)

464 
vals = dict(zip(sorted(G.nodes()), range(len(G)))) 
465 
attr = 'hi'

466 
nx.set_node_attributes(G, vals, attr) 
467 
assert_equal(G.nodes[0][attr], 0) 
468 
assert_equal(G.nodes[1][attr], 1) 
469 
assert_equal(G.nodes[2][attr], 2) 
470  
471 
# Test dictionary of dictionaries

472 
G = nx.path_graph(3, create_using=G)

473 
d = {'hi': 0, 'hello': 200} 
474 
vals = dict.fromkeys(G.nodes(), d)

475 
vals.pop(0)

476 
nx.set_node_attributes(G, vals) 
477 
assert_equal(G.nodes[0], {})

478 
assert_equal(G.nodes[1]["hi"], 0) 
479 
assert_equal(G.nodes[2]["hello"], 200) 
480  
481  
482 
def test_set_edge_attributes(): 
483 
graphs = [nx.Graph(), nx.DiGraph()] 
484 
for G in graphs: 
485 
# Test single value

486 
G = nx.path_graph(3, create_using=G)

487 
attr = 'hello'

488 
vals = 3

489 
nx.set_edge_attributes(G, vals, attr) 
490 
assert_equal(G[0][1][attr], vals) 
491 
assert_equal(G[1][2][attr], vals) 
492  
493 
# Test multiple values

494 
G = nx.path_graph(3, create_using=G)

495 
attr = 'hi'

496 
edges = [(0, 1), (1, 2)] 
497 
vals = dict(zip(edges, range(len(edges)))) 
498 
nx.set_edge_attributes(G, vals, attr) 
499 
assert_equal(G[0][1][attr], 0) 
500 
assert_equal(G[1][2][attr], 1) 
501  
502 
# Test dictionary of dictionaries

503 
G = nx.path_graph(3, create_using=G)

504 
d = {'hi': 0, 'hello': 200} 
505 
edges = [(0, 1)] 
506 
vals = dict.fromkeys(edges, d)

507 
nx.set_edge_attributes(G, vals) 
508 
assert_equal(G[0][1]['hi'], 0) 
509 
assert_equal(G[0][1]['hello'], 200) 
510 
assert_equal(G[1][2], {}) 
511  
512  
513 
def test_set_edge_attributes_multi(): 
514 
graphs = [nx.MultiGraph(), nx.MultiDiGraph()] 
515 
for G in graphs: 
516 
# Test single value

517 
G = nx.path_graph(3, create_using=G)

518 
attr = 'hello'

519 
vals = 3

520 
nx.set_edge_attributes(G, vals, attr) 
521 
assert_equal(G[0][1][0][attr], vals) 
522 
assert_equal(G[1][2][0][attr], vals) 
523  
524 
# Test multiple values

525 
G = nx.path_graph(3, create_using=G)

526 
attr = 'hi'

527 
edges = [(0, 1, 0), (1, 2, 0)] 
528 
vals = dict(zip(edges, range(len(edges)))) 
529 
nx.set_edge_attributes(G, vals, attr) 
530 
assert_equal(G[0][1][0][attr], 0) 
531 
assert_equal(G[1][2][0][attr], 1) 
532  
533 
# Test dictionary of dictionaries

534 
G = nx.path_graph(3, create_using=G)

535 
d = {'hi': 0, 'hello': 200} 
536 
edges = [(0, 1, 0)] 
537 
vals = dict.fromkeys(edges, d)

538 
nx.set_edge_attributes(G, vals) 
539 
assert_equal(G[0][1][0]['hi'], 0) 
540 
assert_equal(G[0][1][0]['hello'], 200) 
541 
assert_equal(G[1][2][0], {}) 
542  
543  
544 
def test_get_node_attributes(): 
545 
graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] 
546 
for G in graphs: 
547 
G = nx.path_graph(3, create_using=G)

548 
attr = 'hello'

549 
vals = 100

550 
nx.set_node_attributes(G, vals, attr) 
551 
attrs = nx.get_node_attributes(G, attr) 
552 
assert_equal(attrs[0], vals)

553 
assert_equal(attrs[1], vals)

554 
assert_equal(attrs[2], vals)

555  
556  
557 
def test_get_edge_attributes(): 
558 
graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] 
559 
for G in graphs: 
560 
G = nx.path_graph(3, create_using=G)

561 
attr = 'hello'

562 
vals = 100

563 
nx.set_edge_attributes(G, vals, attr) 
564 
attrs = nx.get_edge_attributes(G, attr) 
565  
566 
assert_equal(len(attrs), 2) 
567 
if G.is_multigraph():

568 
keys = [(0, 1, 0), (1, 2, 0)] 
569 
for u, v, k in keys: 
570 
try:

571 
assert_equal(attrs[(u, v, k)], 100)

572 
except KeyError: 
573 
assert_equal(attrs[(v, u, k)], 100)

574 
else:

575 
keys = [(0, 1), (1, 2)] 
576 
for u, v in keys: 
577 
try:

578 
assert_equal(attrs[(u, v)], 100)

579 
except KeyError: 
580 
assert_equal(attrs[(v, u)], 100)

581  
582  
583 
def test_is_empty(): 
584 
graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] 
585 
for G in graphs: 
586 
assert_true(nx.is_empty(G)) 
587 
G.add_nodes_from(range(5)) 
588 
assert_true(nx.is_empty(G)) 
589 
G.add_edges_from([(1, 2), (3, 4)]) 
590 
assert_false(nx.is_empty(G)) 
591  
592  
593 
def test_selfloops(): 
594 
graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] 
595 
for graph in graphs: 
596 
G = nx.complete_graph(3, create_using=graph)

597 
G.add_edge(0, 0) 
598 
assert_nodes_equal(nx.nodes_with_selfloops(G), [0])

599 
assert_edges_equal(nx.selfloop_edges(G), [(0, 0)]) 
600 
assert_edges_equal(nx.selfloop_edges(G, data=True), [(0, 0, {})]) 
601 
assert_equal(nx.number_of_selfloops(G), 1)

602 
# test selfloop attr

603 
G.add_edge(1, 1, weight=2) 
604 
assert_edges_equal(nx.selfloop_edges(G, data=True),

605 
[(0, 0, {}), (1, 1, {'weight': 2})]) 
606 
assert_edges_equal(nx.selfloop_edges(G, data='weight'),

607 
[(0, 0, None), (1, 1, 2)]) 