Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_current_flow_betweenness_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 pytest | |
2 | |
3 import networkx as nx | |
4 from networkx.testing import almost_equal | |
5 from networkx import edge_current_flow_betweenness_centrality as edge_current_flow | |
6 from networkx import approximate_current_flow_betweenness_centrality as approximate_cfbc | |
7 | |
8 | |
9 np = pytest.importorskip("numpy") | |
10 npt = pytest.importorskip("numpy.testing") | |
11 scipy = pytest.importorskip("scipy") | |
12 | |
13 | |
14 class TestFlowBetweennessCentrality: | |
15 def test_K4_normalized(self): | |
16 """Betweenness centrality: K4""" | |
17 G = nx.complete_graph(4) | |
18 b = nx.current_flow_betweenness_centrality(G, normalized=True) | |
19 b_answer = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25} | |
20 for n in sorted(G): | |
21 assert almost_equal(b[n], b_answer[n]) | |
22 G.add_edge(0, 1, weight=0.5, other=0.3) | |
23 b = nx.current_flow_betweenness_centrality(G, normalized=True, weight=None) | |
24 for n in sorted(G): | |
25 assert almost_equal(b[n], b_answer[n]) | |
26 wb_answer = {0: 0.2222222, 1: 0.2222222, 2: 0.30555555, 3: 0.30555555} | |
27 b = nx.current_flow_betweenness_centrality(G, normalized=True, weight="weight") | |
28 for n in sorted(G): | |
29 assert almost_equal(b[n], wb_answer[n]) | |
30 wb_answer = {0: 0.2051282, 1: 0.2051282, 2: 0.33974358, 3: 0.33974358} | |
31 b = nx.current_flow_betweenness_centrality(G, normalized=True, weight="other") | |
32 for n in sorted(G): | |
33 assert almost_equal(b[n], wb_answer[n]) | |
34 | |
35 def test_K4(self): | |
36 """Betweenness centrality: K4""" | |
37 G = nx.complete_graph(4) | |
38 for solver in ["full", "lu", "cg"]: | |
39 b = nx.current_flow_betweenness_centrality( | |
40 G, normalized=False, solver=solver | |
41 ) | |
42 b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75} | |
43 for n in sorted(G): | |
44 assert almost_equal(b[n], b_answer[n]) | |
45 | |
46 def test_P4_normalized(self): | |
47 """Betweenness centrality: P4 normalized""" | |
48 G = nx.path_graph(4) | |
49 b = nx.current_flow_betweenness_centrality(G, normalized=True) | |
50 b_answer = {0: 0, 1: 2.0 / 3, 2: 2.0 / 3, 3: 0} | |
51 for n in sorted(G): | |
52 assert almost_equal(b[n], b_answer[n]) | |
53 | |
54 def test_P4(self): | |
55 """Betweenness centrality: P4""" | |
56 G = nx.path_graph(4) | |
57 b = nx.current_flow_betweenness_centrality(G, normalized=False) | |
58 b_answer = {0: 0, 1: 2, 2: 2, 3: 0} | |
59 for n in sorted(G): | |
60 assert almost_equal(b[n], b_answer[n]) | |
61 | |
62 def test_star(self): | |
63 """Betweenness centrality: star """ | |
64 G = nx.Graph() | |
65 nx.add_star(G, ["a", "b", "c", "d"]) | |
66 b = nx.current_flow_betweenness_centrality(G, normalized=True) | |
67 b_answer = {"a": 1.0, "b": 0.0, "c": 0.0, "d": 0.0} | |
68 for n in sorted(G): | |
69 assert almost_equal(b[n], b_answer[n]) | |
70 | |
71 def test_solvers2(self): | |
72 """Betweenness centrality: alternate solvers""" | |
73 G = nx.complete_graph(4) | |
74 for solver in ["full", "lu", "cg"]: | |
75 b = nx.current_flow_betweenness_centrality( | |
76 G, normalized=False, solver=solver | |
77 ) | |
78 b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75} | |
79 for n in sorted(G): | |
80 assert almost_equal(b[n], b_answer[n]) | |
81 | |
82 | |
83 class TestApproximateFlowBetweennessCentrality: | |
84 def test_K4_normalized(self): | |
85 "Approximate current-flow betweenness centrality: K4 normalized" | |
86 G = nx.complete_graph(4) | |
87 b = nx.current_flow_betweenness_centrality(G, normalized=True) | |
88 epsilon = 0.1 | |
89 ba = approximate_cfbc(G, normalized=True, epsilon=0.5 * epsilon) | |
90 for n in sorted(G): | |
91 npt.assert_allclose(b[n], ba[n], atol=epsilon) | |
92 | |
93 def test_K4(self): | |
94 "Approximate current-flow betweenness centrality: K4" | |
95 G = nx.complete_graph(4) | |
96 b = nx.current_flow_betweenness_centrality(G, normalized=False) | |
97 epsilon = 0.1 | |
98 ba = approximate_cfbc(G, normalized=False, epsilon=0.5 * epsilon) | |
99 for n in sorted(G): | |
100 npt.assert_allclose(b[n], ba[n], atol=epsilon * len(G) ** 2) | |
101 | |
102 def test_star(self): | |
103 "Approximate current-flow betweenness centrality: star" | |
104 G = nx.Graph() | |
105 nx.add_star(G, ["a", "b", "c", "d"]) | |
106 b = nx.current_flow_betweenness_centrality(G, normalized=True) | |
107 epsilon = 0.1 | |
108 ba = approximate_cfbc(G, normalized=True, epsilon=0.5 * epsilon) | |
109 for n in sorted(G): | |
110 npt.assert_allclose(b[n], ba[n], atol=epsilon) | |
111 | |
112 def test_grid(self): | |
113 "Approximate current-flow betweenness centrality: 2d grid" | |
114 G = nx.grid_2d_graph(4, 4) | |
115 b = nx.current_flow_betweenness_centrality(G, normalized=True) | |
116 epsilon = 0.1 | |
117 ba = approximate_cfbc(G, normalized=True, epsilon=0.5 * epsilon) | |
118 for n in sorted(G): | |
119 npt.assert_allclose(b[n], ba[n], atol=epsilon) | |
120 | |
121 def test_seed(self): | |
122 G = nx.complete_graph(4) | |
123 b = approximate_cfbc(G, normalized=False, epsilon=0.05, seed=1) | |
124 b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75} | |
125 for n in sorted(G): | |
126 npt.assert_allclose(b[n], b_answer[n], atol=0.1) | |
127 | |
128 def test_solvers(self): | |
129 "Approximate current-flow betweenness centrality: solvers" | |
130 G = nx.complete_graph(4) | |
131 epsilon = 0.1 | |
132 for solver in ["full", "lu", "cg"]: | |
133 b = approximate_cfbc( | |
134 G, normalized=False, solver=solver, epsilon=0.5 * epsilon | |
135 ) | |
136 b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75} | |
137 for n in sorted(G): | |
138 npt.assert_allclose(b[n], b_answer[n], atol=epsilon) | |
139 | |
140 | |
141 class TestWeightedFlowBetweennessCentrality: | |
142 pass | |
143 | |
144 | |
145 class TestEdgeFlowBetweennessCentrality: | |
146 def test_K4(self): | |
147 """Edge flow betweenness centrality: K4""" | |
148 G = nx.complete_graph(4) | |
149 b = edge_current_flow(G, normalized=True) | |
150 b_answer = dict.fromkeys(G.edges(), 0.25) | |
151 for (s, t), v1 in b_answer.items(): | |
152 v2 = b.get((s, t), b.get((t, s))) | |
153 assert almost_equal(v1, v2) | |
154 | |
155 def test_K4_normalized(self): | |
156 """Edge flow betweenness centrality: K4""" | |
157 G = nx.complete_graph(4) | |
158 b = edge_current_flow(G, normalized=False) | |
159 b_answer = dict.fromkeys(G.edges(), 0.75) | |
160 for (s, t), v1 in b_answer.items(): | |
161 v2 = b.get((s, t), b.get((t, s))) | |
162 assert almost_equal(v1, v2) | |
163 | |
164 def test_C4(self): | |
165 """Edge flow betweenness centrality: C4""" | |
166 G = nx.cycle_graph(4) | |
167 b = edge_current_flow(G, normalized=False) | |
168 b_answer = {(0, 1): 1.25, (0, 3): 1.25, (1, 2): 1.25, (2, 3): 1.25} | |
169 for (s, t), v1 in b_answer.items(): | |
170 v2 = b.get((s, t), b.get((t, s))) | |
171 assert almost_equal(v1, v2) | |
172 | |
173 def test_P4(self): | |
174 """Edge betweenness centrality: P4""" | |
175 G = nx.path_graph(4) | |
176 b = edge_current_flow(G, normalized=False) | |
177 b_answer = {(0, 1): 1.5, (1, 2): 2.0, (2, 3): 1.5} | |
178 for (s, t), v1 in b_answer.items(): | |
179 v2 = b.get((s, t), b.get((t, s))) | |
180 assert almost_equal(v1, v2) |