ioftools / networkxMiCe / networkxmaster / networkx / algorithms / centrality / tests / test_percolation_centrality.py @ 5cef0f13
History  View  Annotate  Download (2.77 KB)
1 
#!/usr/bin/env python


2 
from __future__ import division 
3 
from nose.tools import * 
4 
import networkx as nx 
5  
6  
7 
def example1a_G(): 
8 
G = nx.Graph() 
9 
G.add_node(1, percolation=0.1) 
10 
G.add_node(2, percolation=0.2) 
11 
G.add_node(3, percolation=0.2) 
12 
G.add_node(4, percolation=0.2) 
13 
G.add_node(5, percolation=0.3) 
14 
G.add_node(6, percolation=0.2) 
15 
G.add_node(7, percolation=0.5) 
16 
G.add_node(8, percolation=0.5) 
17 
G.add_edges_from([(1, 4), (2, 4), (3, 4), (4, 5), (5, 6), (6, 7), (6, 8)]) 
18 
return G

19  
20  
21 
def example1b_G(): 
22 
G = nx.Graph() 
23 
G.add_node(1, percolation=0.3) 
24 
G.add_node(2, percolation=0.5) 
25 
G.add_node(3, percolation=0.5) 
26 
G.add_node(4, percolation=0.2) 
27 
G.add_node(5, percolation=0.3) 
28 
G.add_node(6, percolation=0.2) 
29 
G.add_node(7, percolation=0.1) 
30 
G.add_node(8, percolation=0.1) 
31 
G.add_edges_from([(1, 4), (2, 4), (3, 4), (4, 5), (5, 6), (6, 7), (6, 8)]) 
32 
return G

33  
34  
35 
class TestPercolationCentrality(object): 
36 
def test_percolation_example1a(self): 
37 
"""percolation centrality: example 1a"""

38 
G = example1a_G() 
39 
p = nx.percolation_centrality(G) 
40 
p_answer = {4: 0.625, 6: 0.667} 
41 
for n in p_answer: 
42 
assert_almost_equal(p[n], p_answer[n], places=3)

43  
44 
def test_percolation_example1b(self): 
45 
"""percolation centrality: example 1a"""

46 
G = example1b_G() 
47 
p = nx.percolation_centrality(G) 
48 
p_answer = {4: 0.825, 6: 0.4} 
49 
for n in p_answer: 
50 
assert_almost_equal(p[n], p_answer[n], places=3)

51  
52 
def test_converge_to_betweenness(self): 
53 
"""percolation centrality: should converge to betweenness

54 
centrality when all nodes are percolated the same"""

55 
# taken from betweenness test test_florentine_families_graph

56 
G = nx.florentine_families_graph() 
57 
b_answer =\ 
58 
{'Acciaiuoli': 0.000, 
59 
'Albizzi': 0.212, 
60 
'Barbadori': 0.093, 
61 
'Bischeri': 0.104, 
62 
'Castellani': 0.055, 
63 
'Ginori': 0.000, 
64 
'Guadagni': 0.255, 
65 
'Lamberteschi': 0.000, 
66 
'Medici': 0.522, 
67 
'Pazzi': 0.000, 
68 
'Peruzzi': 0.022, 
69 
'Ridolfi': 0.114, 
70 
'Salviati': 0.143, 
71 
'Strozzi': 0.103, 
72 
'Tornabuoni': 0.092} 
73  
74 
p_states = {k: 1.0 for k, v in b_answer.items()} 
75 
p_answer = nx.percolation_centrality(G, states=p_states) 
76 
for n in sorted(G): 
77 
assert_almost_equal(p_answer[n], b_answer[n], places=3)

78  
79 
p_states = {k: 0.3 for k, v in b_answer.items()} 
80 
p_answer = nx.percolation_centrality(G, states=p_states) 
81 
for n in sorted(G): 
82 
assert_almost_equal(p_answer[n], b_answer[n], places=3)
