comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_percolation_centrality.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import networkx as nx
2 from networkx.testing import almost_equal
3
4
5 def example1a_G():
6 G = nx.Graph()
7 G.add_node(1, percolation=0.1)
8 G.add_node(2, percolation=0.2)
9 G.add_node(3, percolation=0.2)
10 G.add_node(4, percolation=0.2)
11 G.add_node(5, percolation=0.3)
12 G.add_node(6, percolation=0.2)
13 G.add_node(7, percolation=0.5)
14 G.add_node(8, percolation=0.5)
15 G.add_edges_from([(1, 4), (2, 4), (3, 4), (4, 5), (5, 6), (6, 7), (6, 8)])
16 return G
17
18
19 def example1b_G():
20 G = nx.Graph()
21 G.add_node(1, percolation=0.3)
22 G.add_node(2, percolation=0.5)
23 G.add_node(3, percolation=0.5)
24 G.add_node(4, percolation=0.2)
25 G.add_node(5, percolation=0.3)
26 G.add_node(6, percolation=0.2)
27 G.add_node(7, percolation=0.1)
28 G.add_node(8, percolation=0.1)
29 G.add_edges_from([(1, 4), (2, 4), (3, 4), (4, 5), (5, 6), (6, 7), (6, 8)])
30 return G
31
32
33 class TestPercolationCentrality:
34 def test_percolation_example1a(self):
35 """percolation centrality: example 1a"""
36 G = example1a_G()
37 p = nx.percolation_centrality(G)
38 p_answer = {4: 0.625, 6: 0.667}
39 for n in p_answer:
40 assert almost_equal(p[n], p_answer[n], places=3)
41
42 def test_percolation_example1b(self):
43 """percolation centrality: example 1a"""
44 G = example1b_G()
45 p = nx.percolation_centrality(G)
46 p_answer = {4: 0.825, 6: 0.4}
47 for n in p_answer:
48 assert almost_equal(p[n], p_answer[n], places=3)
49
50 def test_converge_to_betweenness(self):
51 """percolation centrality: should converge to betweenness
52 centrality when all nodes are percolated the same"""
53 # taken from betweenness test test_florentine_families_graph
54 G = nx.florentine_families_graph()
55 b_answer = {
56 "Acciaiuoli": 0.000,
57 "Albizzi": 0.212,
58 "Barbadori": 0.093,
59 "Bischeri": 0.104,
60 "Castellani": 0.055,
61 "Ginori": 0.000,
62 "Guadagni": 0.255,
63 "Lamberteschi": 0.000,
64 "Medici": 0.522,
65 "Pazzi": 0.000,
66 "Peruzzi": 0.022,
67 "Ridolfi": 0.114,
68 "Salviati": 0.143,
69 "Strozzi": 0.103,
70 "Tornabuoni": 0.092,
71 }
72
73 p_states = {k: 1.0 for k, v in b_answer.items()}
74 p_answer = nx.percolation_centrality(G, states=p_states)
75 for n in sorted(G):
76 assert almost_equal(p_answer[n], b_answer[n], places=3)
77
78 p_states = {k: 0.3 for k, v in b_answer.items()}
79 p_answer = nx.percolation_centrality(G, states=p_states)
80 for n in sorted(G):
81 assert almost_equal(p_answer[n], b_answer[n], places=3)