Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / tests / test_link_prediction.py @ 5cef0f13

History | View | Annotate | Download (18.6 KB)

1
import math
2

    
3
from functools import partial
4
from nose.tools import *
5

    
6
import networkx as nx
7

    
8

    
9
def _test_func(G, ebunch, expected, predict_func, **kwargs):
10
    result = predict_func(G, ebunch, **kwargs)
11
    exp_dict = dict((tuple(sorted([u, v])), score) for u, v, score in expected)
12
    res_dict = dict((tuple(sorted([u, v])), score) for u, v, score in result)
13

    
14
    assert_equal(len(exp_dict), len(res_dict))
15
    for p in exp_dict:
16
        assert_almost_equal(exp_dict[p], res_dict[p])
17

    
18

    
19
class TestResourceAllocationIndex():
20
    def setUp(self):
21
        self.func = nx.resource_allocation_index
22
        self.test = partial(_test_func, predict_func=self.func)
23

    
24
    def test_K5(self):
25
        G = nx.complete_graph(5)
26
        self.test(G, [(0, 1)], [(0, 1, 0.75)])
27

    
28
    def test_P3(self):
29
        G = nx.path_graph(3)
30
        self.test(G, [(0, 2)], [(0, 2, 0.5)])
31

    
32
    def test_S4(self):
33
        G = nx.star_graph(4)
34
        self.test(G, [(1, 2)], [(1, 2, 0.25)])
35

    
36
    @raises(nx.NetworkXNotImplemented)
37
    def test_digraph(self):
38
        G = nx.DiGraph()
39
        G.add_edges_from([(0, 1), (1, 2)])
40
        self.func(G, [(0, 2)])
41

    
42
    @raises(nx.NetworkXNotImplemented)
43
    def test_multigraph(self):
44
        G = nx.MultiGraph()
45
        G.add_edges_from([(0, 1), (1, 2)])
46
        self.func(G, [(0, 2)])
47

    
48
    @raises(nx.NetworkXNotImplemented)
49
    def test_multidigraph(self):
50
        G = nx.MultiDiGraph()
51
        G.add_edges_from([(0, 1), (1, 2)])
52
        self.func(G, [(0, 2)])
53

    
54
    def test_no_common_neighbor(self):
55
        G = nx.Graph()
56
        G.add_nodes_from([0, 1])
57
        self.test(G, [(0, 1)], [(0, 1, 0)])
58

    
59
    def test_equal_nodes(self):
60
        G = nx.complete_graph(4)
61
        self.test(G, [(0, 0)], [(0, 0, 1)])
62

    
63
    def test_all_nonexistent_edges(self):
64
        G = nx.Graph()
65
        G.add_edges_from([(0, 1), (0, 2), (2, 3)])
66
        self.test(G, None, [(0, 3, 0.5), (1, 2, 0.5), (1, 3, 0)])
67

    
68

    
69
class TestJaccardCoefficient():
70
    def setUp(self):
71
        self.func = nx.jaccard_coefficient
72
        self.test = partial(_test_func, predict_func=self.func)
73

    
74
    def test_K5(self):
75
        G = nx.complete_graph(5)
76
        self.test(G, [(0, 1)], [(0, 1, 0.6)])
77

    
78
    def test_P4(self):
79
        G = nx.path_graph(4)
80
        self.test(G, [(0, 2)], [(0, 2, 0.5)])
81

    
82
    @raises(nx.NetworkXNotImplemented)
83
    def test_digraph(self):
84
        G = nx.DiGraph()
85
        G.add_edges_from([(0, 1), (1, 2)])
86
        self.func(G, [(0, 2)])
87

    
88
    @raises(nx.NetworkXNotImplemented)
89
    def test_multigraph(self):
90
        G = nx.MultiGraph()
91
        G.add_edges_from([(0, 1), (1, 2)])
92
        self.func(G, [(0, 2)])
93

    
94
    @raises(nx.NetworkXNotImplemented)
95
    def test_multidigraph(self):
96
        G = nx.MultiDiGraph()
97
        G.add_edges_from([(0, 1), (1, 2)])
98
        self.func(G, [(0, 2)])
99

    
100
    def test_no_common_neighbor(self):
101
        G = nx.Graph()
102
        G.add_edges_from([(0, 1), (2, 3)])
103
        self.test(G, [(0, 2)], [(0, 2, 0)])
104

    
105
    def test_isolated_nodes(self):
106
        G = nx.Graph()
107
        G.add_nodes_from([0, 1])
108
        self.test(G, [(0, 1)], [(0, 1, 0)])
109

    
110
    def test_all_nonexistent_edges(self):
111
        G = nx.Graph()
112
        G.add_edges_from([(0, 1), (0, 2), (2, 3)])
113
        self.test(G, None, [(0, 3, 0.5), (1, 2, 0.5), (1, 3, 0)])
114

    
115

    
116
class TestAdamicAdarIndex():
117
    def setUp(self):
118
        self.func = nx.adamic_adar_index
119
        self.test = partial(_test_func, predict_func=self.func)
120

    
121
    def test_K5(self):
122
        G = nx.complete_graph(5)
123
        self.test(G, [(0, 1)], [(0, 1, 3 / math.log(4))])
124

    
125
    def test_P3(self):
126
        G = nx.path_graph(3)
127
        self.test(G, [(0, 2)], [(0, 2, 1 / math.log(2))])
128

    
129
    def test_S4(self):
130
        G = nx.star_graph(4)
131
        self.test(G, [(1, 2)], [(1, 2, 1 / math.log(4))])
132

    
133
    @raises(nx.NetworkXNotImplemented)
134
    def test_digraph(self):
135
        G = nx.DiGraph()
136
        G.add_edges_from([(0, 1), (1, 2)])
137
        self.func(G, [(0, 2)])
138

    
139
    @raises(nx.NetworkXNotImplemented)
140
    def test_multigraph(self):
141
        G = nx.MultiGraph()
142
        G.add_edges_from([(0, 1), (1, 2)])
143
        self.func(G, [(0, 2)])
144

    
145
    @raises(nx.NetworkXNotImplemented)
146
    def test_multidigraph(self):
147
        G = nx.MultiDiGraph()
148
        G.add_edges_from([(0, 1), (1, 2)])
149
        self.func(G, [(0, 2)])
150

    
151
    def test_no_common_neighbor(self):
152
        G = nx.Graph()
153
        G.add_nodes_from([0, 1])
154
        self.test(G, [(0, 1)], [(0, 1, 0)])
155

    
156
    def test_equal_nodes(self):
157
        G = nx.complete_graph(4)
158
        self.test(G, [(0, 0)], [(0, 0, 3 / math.log(3))])
159

    
160
    def test_all_nonexistent_edges(self):
161
        G = nx.Graph()
162
        G.add_edges_from([(0, 1), (0, 2), (2, 3)])
163
        self.test(G, None, [(0, 3, 1 / math.log(2)), (1, 2, 1 / math.log(2)),
164
                            (1, 3, 0)])
165

    
166

    
167
class TestPreferentialAttachment():
168
    def setUp(self):
169
        self.func = nx.preferential_attachment
170
        self.test = partial(_test_func, predict_func=self.func)
171

    
172
    def test_K5(self):
173
        G = nx.complete_graph(5)
174
        self.test(G, [(0, 1)], [(0, 1, 16)])
175

    
176
    def test_P3(self):
177
        G = nx.path_graph(3)
178
        self.test(G, [(0, 1)], [(0, 1, 2)])
179

    
180
    def test_S4(self):
181
        G = nx.star_graph(4)
182
        self.test(G, [(0, 2)], [(0, 2, 4)])
183

    
184
    @raises(nx.NetworkXNotImplemented)
185
    def test_digraph(self):
186
        G = nx.DiGraph()
187
        G.add_edges_from([(0, 1), (1, 2)])
188
        self.func(G, [(0, 2)])
189

    
190
    @raises(nx.NetworkXNotImplemented)
191
    def test_multigraph(self):
192
        G = nx.MultiGraph()
193
        G.add_edges_from([(0, 1), (1, 2)])
194
        self.func(G, [(0, 2)])
195

    
196
    @raises(nx.NetworkXNotImplemented)
197
    def test_multidigraph(self):
198
        G = nx.MultiDiGraph()
199
        G.add_edges_from([(0, 1), (1, 2)])
200
        self.func(G, [(0, 2)])
201

    
202
    def test_zero_degrees(self):
203
        G = nx.Graph()
204
        G.add_nodes_from([0, 1])
205
        self.test(G, [(0, 1)], [(0, 1, 0)])
206

    
207
    def test_all_nonexistent_edges(self):
208
        G = nx.Graph()
209
        G.add_edges_from([(0, 1), (0, 2), (2, 3)])
210
        self.test(G, None, [(0, 3, 2), (1, 2, 2), (1, 3, 1)])
211

    
212

    
213
class TestCNSoundarajanHopcroft():
214
    def setUp(self):
215
        self.func = nx.cn_soundarajan_hopcroft
216
        self.test = partial(_test_func, predict_func=self.func,
217
                            community='community')
218

    
219
    def test_K5(self):
220
        G = nx.complete_graph(5)
221
        G.nodes[0]['community'] = 0
222
        G.nodes[1]['community'] = 0
223
        G.nodes[2]['community'] = 0
224
        G.nodes[3]['community'] = 0
225
        G.nodes[4]['community'] = 1
226
        self.test(G, [(0, 1)], [(0, 1, 5)])
227

    
228
    def test_P3(self):
229
        G = nx.path_graph(3)
230
        G.nodes[0]['community'] = 0
231
        G.nodes[1]['community'] = 1
232
        G.nodes[2]['community'] = 0
233
        self.test(G, [(0, 2)], [(0, 2, 1)])
234

    
235
    def test_S4(self):
236
        G = nx.star_graph(4)
237
        G.nodes[0]['community'] = 1
238
        G.nodes[1]['community'] = 1
239
        G.nodes[2]['community'] = 1
240
        G.nodes[3]['community'] = 0
241
        G.nodes[4]['community'] = 0
242
        self.test(G, [(1, 2)], [(1, 2, 2)])
243

    
244
    @raises(nx.NetworkXNotImplemented)
245
    def test_digraph(self):
246
        G = nx.DiGraph()
247
        G.add_edges_from([(0, 1), (1, 2)])
248
        G.nodes[0]['community'] = 0
249
        G.nodes[1]['community'] = 0
250
        G.nodes[2]['community'] = 0
251
        self.func(G, [(0, 2)])
252

    
253
    @raises(nx.NetworkXNotImplemented)
254
    def test_multigraph(self):
255
        G = nx.MultiGraph()
256
        G.add_edges_from([(0, 1), (1, 2)])
257
        G.nodes[0]['community'] = 0
258
        G.nodes[1]['community'] = 0
259
        G.nodes[2]['community'] = 0
260
        self.func(G, [(0, 2)])
261

    
262
    @raises(nx.NetworkXNotImplemented)
263
    def test_multidigraph(self):
264
        G = nx.MultiDiGraph()
265
        G.add_edges_from([(0, 1), (1, 2)])
266
        G.nodes[0]['community'] = 0
267
        G.nodes[1]['community'] = 0
268
        G.nodes[2]['community'] = 0
269
        self.func(G, [(0, 2)])
270

    
271
    def test_no_common_neighbor(self):
272
        G = nx.Graph()
273
        G.add_nodes_from([0, 1])
274
        G.nodes[0]['community'] = 0
275
        G.nodes[1]['community'] = 0
276
        self.test(G, [(0, 1)], [(0, 1, 0)])
277

    
278
    def test_equal_nodes(self):
279
        G = nx.complete_graph(3)
280
        G.nodes[0]['community'] = 0
281
        G.nodes[1]['community'] = 0
282
        G.nodes[2]['community'] = 0
283
        self.test(G, [(0, 0)], [(0, 0, 4)])
284

    
285
    def test_different_community(self):
286
        G = nx.Graph()
287
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
288
        G.nodes[0]['community'] = 0
289
        G.nodes[1]['community'] = 0
290
        G.nodes[2]['community'] = 0
291
        G.nodes[3]['community'] = 1
292
        self.test(G, [(0, 3)], [(0, 3, 2)])
293

    
294
    @raises(nx.NetworkXAlgorithmError)
295
    def test_no_community_information(self):
296
        G = nx.complete_graph(5)
297
        list(self.func(G, [(0, 1)]))
298

    
299
    @raises(nx.NetworkXAlgorithmError)
300
    def test_insufficient_community_information(self):
301
        G = nx.Graph()
302
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
303
        G.nodes[0]['community'] = 0
304
        G.nodes[1]['community'] = 0
305
        G.nodes[3]['community'] = 0
306
        list(self.func(G, [(0, 3)]))
307

    
308
    def test_sufficient_community_information(self):
309
        G = nx.Graph()
310
        G.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 4), (3, 4), (4, 5)])
311
        G.nodes[1]['community'] = 0
312
        G.nodes[2]['community'] = 0
313
        G.nodes[3]['community'] = 0
314
        G.nodes[4]['community'] = 0
315
        self.test(G, [(1, 4)], [(1, 4, 4)])
316

    
317
    def test_custom_community_attribute_name(self):
318
        G = nx.Graph()
319
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
320
        G.nodes[0]['cmty'] = 0
321
        G.nodes[1]['cmty'] = 0
322
        G.nodes[2]['cmty'] = 0
323
        G.nodes[3]['cmty'] = 1
324
        self.test(G, [(0, 3)], [(0, 3, 2)], community='cmty')
325

    
326
    def test_all_nonexistent_edges(self):
327
        G = nx.Graph()
328
        G.add_edges_from([(0, 1), (0, 2), (2, 3)])
329
        G.nodes[0]['community'] = 0
330
        G.nodes[1]['community'] = 1
331
        G.nodes[2]['community'] = 0
332
        G.nodes[3]['community'] = 0
333
        self.test(G, None, [(0, 3, 2), (1, 2, 1), (1, 3, 0)])
334

    
335

    
336
class TestRAIndexSoundarajanHopcroft():
337
    def setUp(self):
338
        self.func = nx.ra_index_soundarajan_hopcroft
339
        self.test = partial(_test_func, predict_func=self.func,
340
                            community='community')
341

    
342
    def test_K5(self):
343
        G = nx.complete_graph(5)
344
        G.nodes[0]['community'] = 0
345
        G.nodes[1]['community'] = 0
346
        G.nodes[2]['community'] = 0
347
        G.nodes[3]['community'] = 0
348
        G.nodes[4]['community'] = 1
349
        self.test(G, [(0, 1)], [(0, 1, 0.5)])
350

    
351
    def test_P3(self):
352
        G = nx.path_graph(3)
353
        G.nodes[0]['community'] = 0
354
        G.nodes[1]['community'] = 1
355
        G.nodes[2]['community'] = 0
356
        self.test(G, [(0, 2)], [(0, 2, 0)])
357

    
358
    def test_S4(self):
359
        G = nx.star_graph(4)
360
        G.nodes[0]['community'] = 1
361
        G.nodes[1]['community'] = 1
362
        G.nodes[2]['community'] = 1
363
        G.nodes[3]['community'] = 0
364
        G.nodes[4]['community'] = 0
365
        self.test(G, [(1, 2)], [(1, 2, 0.25)])
366

    
367
    @raises(nx.NetworkXNotImplemented)
368
    def test_digraph(self):
369
        G = nx.DiGraph()
370
        G.add_edges_from([(0, 1), (1, 2)])
371
        G.nodes[0]['community'] = 0
372
        G.nodes[1]['community'] = 0
373
        G.nodes[2]['community'] = 0
374
        self.func(G, [(0, 2)])
375

    
376
    @raises(nx.NetworkXNotImplemented)
377
    def test_multigraph(self):
378
        G = nx.MultiGraph()
379
        G.add_edges_from([(0, 1), (1, 2)])
380
        G.nodes[0]['community'] = 0
381
        G.nodes[1]['community'] = 0
382
        G.nodes[2]['community'] = 0
383
        self.func(G, [(0, 2)])
384

    
385
    @raises(nx.NetworkXNotImplemented)
386
    def test_multidigraph(self):
387
        G = nx.MultiDiGraph()
388
        G.add_edges_from([(0, 1), (1, 2)])
389
        G.nodes[0]['community'] = 0
390
        G.nodes[1]['community'] = 0
391
        G.nodes[2]['community'] = 0
392
        self.func(G, [(0, 2)])
393

    
394
    def test_no_common_neighbor(self):
395
        G = nx.Graph()
396
        G.add_nodes_from([0, 1])
397
        G.nodes[0]['community'] = 0
398
        G.nodes[1]['community'] = 0
399
        self.test(G, [(0, 1)], [(0, 1, 0)])
400

    
401
    def test_equal_nodes(self):
402
        G = nx.complete_graph(3)
403
        G.nodes[0]['community'] = 0
404
        G.nodes[1]['community'] = 0
405
        G.nodes[2]['community'] = 0
406
        self.test(G, [(0, 0)], [(0, 0, 1)])
407

    
408
    def test_different_community(self):
409
        G = nx.Graph()
410
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
411
        G.nodes[0]['community'] = 0
412
        G.nodes[1]['community'] = 0
413
        G.nodes[2]['community'] = 0
414
        G.nodes[3]['community'] = 1
415
        self.test(G, [(0, 3)], [(0, 3, 0)])
416

    
417
    @raises(nx.NetworkXAlgorithmError)
418
    def test_no_community_information(self):
419
        G = nx.complete_graph(5)
420
        list(self.func(G, [(0, 1)]))
421

    
422
    @raises(nx.NetworkXAlgorithmError)
423
    def test_insufficient_community_information(self):
424
        G = nx.Graph()
425
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
426
        G.nodes[0]['community'] = 0
427
        G.nodes[1]['community'] = 0
428
        G.nodes[3]['community'] = 0
429
        list(self.func(G, [(0, 3)]))
430

    
431
    def test_sufficient_community_information(self):
432
        G = nx.Graph()
433
        G.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 4), (3, 4), (4, 5)])
434
        G.nodes[1]['community'] = 0
435
        G.nodes[2]['community'] = 0
436
        G.nodes[3]['community'] = 0
437
        G.nodes[4]['community'] = 0
438
        self.test(G, [(1, 4)], [(1, 4, 1)])
439

    
440
    def test_custom_community_attribute_name(self):
441
        G = nx.Graph()
442
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
443
        G.nodes[0]['cmty'] = 0
444
        G.nodes[1]['cmty'] = 0
445
        G.nodes[2]['cmty'] = 0
446
        G.nodes[3]['cmty'] = 1
447
        self.test(G, [(0, 3)], [(0, 3, 0)], community='cmty')
448

    
449
    def test_all_nonexistent_edges(self):
450
        G = nx.Graph()
451
        G.add_edges_from([(0, 1), (0, 2), (2, 3)])
452
        G.nodes[0]['community'] = 0
453
        G.nodes[1]['community'] = 1
454
        G.nodes[2]['community'] = 0
455
        G.nodes[3]['community'] = 0
456
        self.test(G, None, [(0, 3, 0.5), (1, 2, 0), (1, 3, 0)])
457

    
458

    
459
class TestWithinInterCluster():
460
    def setUp(self):
461
        self.delta = 0.001
462
        self.func = nx.within_inter_cluster
463
        self.test = partial(_test_func, predict_func=self.func,
464
                            delta=self.delta, community='community')
465

    
466
    def test_K5(self):
467
        G = nx.complete_graph(5)
468
        G.nodes[0]['community'] = 0
469
        G.nodes[1]['community'] = 0
470
        G.nodes[2]['community'] = 0
471
        G.nodes[3]['community'] = 0
472
        G.nodes[4]['community'] = 1
473
        self.test(G, [(0, 1)], [(0, 1, 2 / (1 + self.delta))])
474

    
475
    def test_P3(self):
476
        G = nx.path_graph(3)
477
        G.nodes[0]['community'] = 0
478
        G.nodes[1]['community'] = 1
479
        G.nodes[2]['community'] = 0
480
        self.test(G, [(0, 2)], [(0, 2, 0)])
481

    
482
    def test_S4(self):
483
        G = nx.star_graph(4)
484
        G.nodes[0]['community'] = 1
485
        G.nodes[1]['community'] = 1
486
        G.nodes[2]['community'] = 1
487
        G.nodes[3]['community'] = 0
488
        G.nodes[4]['community'] = 0
489
        self.test(G, [(1, 2)], [(1, 2, 1 / self.delta)])
490

    
491
    @raises(nx.NetworkXNotImplemented)
492
    def test_digraph(self):
493
        G = nx.DiGraph()
494
        G.add_edges_from([(0, 1), (1, 2)])
495
        G.nodes[0]['community'] = 0
496
        G.nodes[1]['community'] = 0
497
        G.nodes[2]['community'] = 0
498
        self.func(G, [(0, 2)])
499

    
500
    @raises(nx.NetworkXNotImplemented)
501
    def test_multigraph(self):
502
        G = nx.MultiGraph()
503
        G.add_edges_from([(0, 1), (1, 2)])
504
        G.nodes[0]['community'] = 0
505
        G.nodes[1]['community'] = 0
506
        G.nodes[2]['community'] = 0
507
        self.func(G, [(0, 2)])
508

    
509
    @raises(nx.NetworkXNotImplemented)
510
    def test_multidigraph(self):
511
        G = nx.MultiDiGraph()
512
        G.add_edges_from([(0, 1), (1, 2)])
513
        G.nodes[0]['community'] = 0
514
        G.nodes[1]['community'] = 0
515
        G.nodes[2]['community'] = 0
516
        self.func(G, [(0, 2)])
517

    
518
    def test_no_common_neighbor(self):
519
        G = nx.Graph()
520
        G.add_nodes_from([0, 1])
521
        G.nodes[0]['community'] = 0
522
        G.nodes[1]['community'] = 0
523
        self.test(G, [(0, 1)], [(0, 1, 0)])
524

    
525
    def test_equal_nodes(self):
526
        G = nx.complete_graph(3)
527
        G.nodes[0]['community'] = 0
528
        G.nodes[1]['community'] = 0
529
        G.nodes[2]['community'] = 0
530
        self.test(G, [(0, 0)], [(0, 0, 2 / self.delta)])
531

    
532
    def test_different_community(self):
533
        G = nx.Graph()
534
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
535
        G.nodes[0]['community'] = 0
536
        G.nodes[1]['community'] = 0
537
        G.nodes[2]['community'] = 0
538
        G.nodes[3]['community'] = 1
539
        self.test(G, [(0, 3)], [(0, 3, 0)])
540

    
541
    def test_no_inter_cluster_common_neighbor(self):
542
        G = nx.complete_graph(4)
543
        G.nodes[0]['community'] = 0
544
        G.nodes[1]['community'] = 0
545
        G.nodes[2]['community'] = 0
546
        G.nodes[3]['community'] = 0
547
        self.test(G, [(0, 3)], [(0, 3, 2 / self.delta)])
548

    
549
    @raises(nx.NetworkXAlgorithmError)
550
    def test_no_community_information(self):
551
        G = nx.complete_graph(5)
552
        list(self.func(G, [(0, 1)]))
553

    
554
    @raises(nx.NetworkXAlgorithmError)
555
    def test_insufficient_community_information(self):
556
        G = nx.Graph()
557
        G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3)])
558
        G.nodes[0]['community'] = 0
559
        G.nodes[1]['community'] = 0
560
        G.nodes[3]['community'] = 0
561
        list(self.func(G, [(0, 3)]))
562

    
563
    def test_sufficient_community_information(self):
564
        G = nx.Graph()
565
        G.add_edges_from([(0, 1), (1, 2), (1, 3), (2, 4), (3, 4), (4, 5)])
566
        G.nodes[1]['community'] = 0
567
        G.nodes[2]['community'] = 0
568
        G.nodes[3]['community'] = 0
569
        G.nodes[4]['community'] = 0
570
        self.test(G, [(1, 4)], [(1, 4, 2 / self.delta)])
571

    
572
    @raises(nx.NetworkXAlgorithmError)
573
    def test_zero_delta(self):
574
        G = nx.complete_graph(3)
575
        G.nodes[0]['community'] = 0
576
        G.nodes[1]['community'] = 0
577
        G.nodes[2]['community'] = 0
578
        list(self.func(G, [(0, 1)], 0))
579

    
580
    @raises(nx.NetworkXAlgorithmError)
581
    def test_negative_delta(self):
582
        G = nx.complete_graph(3)
583
        G.nodes[0]['community'] = 0
584
        G.nodes[1]['community'] = 0
585
        G.nodes[2]['community'] = 0
586
        list(self.func(G, [(0, 1)], -0.5))
587

    
588
    def test_custom_community_attribute_name(self):
589
        G = nx.complete_graph(4)
590
        G.nodes[0]['cmty'] = 0
591
        G.nodes[1]['cmty'] = 0
592
        G.nodes[2]['cmty'] = 0
593
        G.nodes[3]['cmty'] = 0
594
        self.test(G, [(0, 3)], [(0, 3, 2 / self.delta)], community='cmty')
595

    
596
    def test_all_nonexistent_edges(self):
597
        G = nx.Graph()
598
        G.add_edges_from([(0, 1), (0, 2), (2, 3)])
599
        G.nodes[0]['community'] = 0
600
        G.nodes[1]['community'] = 1
601
        G.nodes[2]['community'] = 0
602
        G.nodes[3]['community'] = 0
603
        self.test(G, None, [(0, 3, 1 / self.delta), (1, 2, 0), (1, 3, 0)])