Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_katz_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 math | |
2 | |
3 import networkx as nx | |
4 from networkx.testing import almost_equal | |
5 import pytest | |
6 | |
7 | |
8 class TestKatzCentrality: | |
9 def test_K5(self): | |
10 """Katz centrality: K5""" | |
11 G = nx.complete_graph(5) | |
12 alpha = 0.1 | |
13 b = nx.katz_centrality(G, alpha) | |
14 v = math.sqrt(1 / 5.0) | |
15 b_answer = dict.fromkeys(G, v) | |
16 for n in sorted(G): | |
17 assert almost_equal(b[n], b_answer[n]) | |
18 nstart = {n: 1 for n in G} | |
19 b = nx.katz_centrality(G, alpha, nstart=nstart) | |
20 for n in sorted(G): | |
21 assert almost_equal(b[n], b_answer[n]) | |
22 | |
23 def test_P3(self): | |
24 """Katz centrality: P3""" | |
25 alpha = 0.1 | |
26 G = nx.path_graph(3) | |
27 b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} | |
28 b = nx.katz_centrality(G, alpha) | |
29 for n in sorted(G): | |
30 assert almost_equal(b[n], b_answer[n], places=4) | |
31 | |
32 def test_maxiter(self): | |
33 with pytest.raises(nx.PowerIterationFailedConvergence): | |
34 alpha = 0.1 | |
35 G = nx.path_graph(3) | |
36 max_iter = 0 | |
37 try: | |
38 b = nx.katz_centrality(G, alpha, max_iter=max_iter) | |
39 except nx.NetworkXError as e: | |
40 assert str(max_iter) in e.args[0], "max_iter value not in error msg" | |
41 raise # So that the decorater sees the exception. | |
42 | |
43 def test_beta_as_scalar(self): | |
44 alpha = 0.1 | |
45 beta = 0.1 | |
46 b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} | |
47 G = nx.path_graph(3) | |
48 b = nx.katz_centrality(G, alpha, beta) | |
49 for n in sorted(G): | |
50 assert almost_equal(b[n], b_answer[n], places=4) | |
51 | |
52 def test_beta_as_dict(self): | |
53 alpha = 0.1 | |
54 beta = {0: 1.0, 1: 1.0, 2: 1.0} | |
55 b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} | |
56 G = nx.path_graph(3) | |
57 b = nx.katz_centrality(G, alpha, beta) | |
58 for n in sorted(G): | |
59 assert almost_equal(b[n], b_answer[n], places=4) | |
60 | |
61 def test_multiple_alpha(self): | |
62 alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] | |
63 for alpha in alpha_list: | |
64 b_answer = { | |
65 0.1: { | |
66 0: 0.5598852584152165, | |
67 1: 0.6107839182711449, | |
68 2: 0.5598852584152162, | |
69 }, | |
70 0.2: { | |
71 0: 0.5454545454545454, | |
72 1: 0.6363636363636365, | |
73 2: 0.5454545454545454, | |
74 }, | |
75 0.3: { | |
76 0: 0.5333964609104419, | |
77 1: 0.6564879518897746, | |
78 2: 0.5333964609104419, | |
79 }, | |
80 0.4: { | |
81 0: 0.5232045649263551, | |
82 1: 0.6726915834767423, | |
83 2: 0.5232045649263551, | |
84 }, | |
85 0.5: { | |
86 0: 0.5144957746691622, | |
87 1: 0.6859943117075809, | |
88 2: 0.5144957746691622, | |
89 }, | |
90 0.6: { | |
91 0: 0.5069794004195823, | |
92 1: 0.6970966755769258, | |
93 2: 0.5069794004195823, | |
94 }, | |
95 } | |
96 G = nx.path_graph(3) | |
97 b = nx.katz_centrality(G, alpha) | |
98 for n in sorted(G): | |
99 assert almost_equal(b[n], b_answer[alpha][n], places=4) | |
100 | |
101 def test_multigraph(self): | |
102 with pytest.raises(nx.NetworkXException): | |
103 e = nx.katz_centrality(nx.MultiGraph(), 0.1) | |
104 | |
105 def test_empty(self): | |
106 e = nx.katz_centrality(nx.Graph(), 0.1) | |
107 assert e == {} | |
108 | |
109 def test_bad_beta(self): | |
110 with pytest.raises(nx.NetworkXException): | |
111 G = nx.Graph([(0, 1)]) | |
112 beta = {0: 77} | |
113 e = nx.katz_centrality(G, 0.1, beta=beta) | |
114 | |
115 def test_bad_beta_numbe(self): | |
116 with pytest.raises(nx.NetworkXException): | |
117 G = nx.Graph([(0, 1)]) | |
118 e = nx.katz_centrality(G, 0.1, beta="foo") | |
119 | |
120 | |
121 class TestKatzCentralityNumpy: | |
122 @classmethod | |
123 def setup_class(cls): | |
124 global np | |
125 np = pytest.importorskip("numpy") | |
126 scipy = pytest.importorskip("scipy") | |
127 | |
128 def test_K5(self): | |
129 """Katz centrality: K5""" | |
130 G = nx.complete_graph(5) | |
131 alpha = 0.1 | |
132 b = nx.katz_centrality(G, alpha) | |
133 v = math.sqrt(1 / 5.0) | |
134 b_answer = dict.fromkeys(G, v) | |
135 for n in sorted(G): | |
136 assert almost_equal(b[n], b_answer[n]) | |
137 nstart = {n: 1 for n in G} | |
138 b = nx.eigenvector_centrality_numpy(G) | |
139 for n in sorted(G): | |
140 assert almost_equal(b[n], b_answer[n], places=3) | |
141 | |
142 def test_P3(self): | |
143 """Katz centrality: P3""" | |
144 alpha = 0.1 | |
145 G = nx.path_graph(3) | |
146 b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} | |
147 b = nx.katz_centrality_numpy(G, alpha) | |
148 for n in sorted(G): | |
149 assert almost_equal(b[n], b_answer[n], places=4) | |
150 | |
151 def test_beta_as_scalar(self): | |
152 alpha = 0.1 | |
153 beta = 0.1 | |
154 b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} | |
155 G = nx.path_graph(3) | |
156 b = nx.katz_centrality_numpy(G, alpha, beta) | |
157 for n in sorted(G): | |
158 assert almost_equal(b[n], b_answer[n], places=4) | |
159 | |
160 def test_beta_as_dict(self): | |
161 alpha = 0.1 | |
162 beta = {0: 1.0, 1: 1.0, 2: 1.0} | |
163 b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} | |
164 G = nx.path_graph(3) | |
165 b = nx.katz_centrality_numpy(G, alpha, beta) | |
166 for n in sorted(G): | |
167 assert almost_equal(b[n], b_answer[n], places=4) | |
168 | |
169 def test_multiple_alpha(self): | |
170 alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] | |
171 for alpha in alpha_list: | |
172 b_answer = { | |
173 0.1: { | |
174 0: 0.5598852584152165, | |
175 1: 0.6107839182711449, | |
176 2: 0.5598852584152162, | |
177 }, | |
178 0.2: { | |
179 0: 0.5454545454545454, | |
180 1: 0.6363636363636365, | |
181 2: 0.5454545454545454, | |
182 }, | |
183 0.3: { | |
184 0: 0.5333964609104419, | |
185 1: 0.6564879518897746, | |
186 2: 0.5333964609104419, | |
187 }, | |
188 0.4: { | |
189 0: 0.5232045649263551, | |
190 1: 0.6726915834767423, | |
191 2: 0.5232045649263551, | |
192 }, | |
193 0.5: { | |
194 0: 0.5144957746691622, | |
195 1: 0.6859943117075809, | |
196 2: 0.5144957746691622, | |
197 }, | |
198 0.6: { | |
199 0: 0.5069794004195823, | |
200 1: 0.6970966755769258, | |
201 2: 0.5069794004195823, | |
202 }, | |
203 } | |
204 G = nx.path_graph(3) | |
205 b = nx.katz_centrality_numpy(G, alpha) | |
206 for n in sorted(G): | |
207 assert almost_equal(b[n], b_answer[alpha][n], places=4) | |
208 | |
209 def test_multigraph(self): | |
210 with pytest.raises(nx.NetworkXException): | |
211 e = nx.katz_centrality(nx.MultiGraph(), 0.1) | |
212 | |
213 def test_empty(self): | |
214 e = nx.katz_centrality(nx.Graph(), 0.1) | |
215 assert e == {} | |
216 | |
217 def test_bad_beta(self): | |
218 with pytest.raises(nx.NetworkXException): | |
219 G = nx.Graph([(0, 1)]) | |
220 beta = {0: 77} | |
221 e = nx.katz_centrality_numpy(G, 0.1, beta=beta) | |
222 | |
223 def test_bad_beta_numbe(self): | |
224 with pytest.raises(nx.NetworkXException): | |
225 G = nx.Graph([(0, 1)]) | |
226 e = nx.katz_centrality_numpy(G, 0.1, beta="foo") | |
227 | |
228 def test_K5_unweighted(self): | |
229 """Katz centrality: K5""" | |
230 G = nx.complete_graph(5) | |
231 alpha = 0.1 | |
232 b = nx.katz_centrality(G, alpha, weight=None) | |
233 v = math.sqrt(1 / 5.0) | |
234 b_answer = dict.fromkeys(G, v) | |
235 for n in sorted(G): | |
236 assert almost_equal(b[n], b_answer[n]) | |
237 nstart = {n: 1 for n in G} | |
238 b = nx.eigenvector_centrality_numpy(G, weight=None) | |
239 for n in sorted(G): | |
240 assert almost_equal(b[n], b_answer[n], places=3) | |
241 | |
242 def test_P3_unweighted(self): | |
243 """Katz centrality: P3""" | |
244 alpha = 0.1 | |
245 G = nx.path_graph(3) | |
246 b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} | |
247 b = nx.katz_centrality_numpy(G, alpha, weight=None) | |
248 for n in sorted(G): | |
249 assert almost_equal(b[n], b_answer[n], places=4) | |
250 | |
251 | |
252 class TestKatzCentralityDirected: | |
253 @classmethod | |
254 def setup_class(cls): | |
255 G = nx.DiGraph() | |
256 edges = [ | |
257 (1, 2), | |
258 (1, 3), | |
259 (2, 4), | |
260 (3, 2), | |
261 (3, 5), | |
262 (4, 2), | |
263 (4, 5), | |
264 (4, 6), | |
265 (5, 6), | |
266 (5, 7), | |
267 (5, 8), | |
268 (6, 8), | |
269 (7, 1), | |
270 (7, 5), | |
271 (7, 8), | |
272 (8, 6), | |
273 (8, 7), | |
274 ] | |
275 G.add_edges_from(edges, weight=2.0) | |
276 cls.G = G.reverse() | |
277 cls.G.alpha = 0.1 | |
278 cls.G.evc = [ | |
279 0.3289589783189635, | |
280 0.2832077296243516, | |
281 0.3425906003685471, | |
282 0.3970420865198392, | |
283 0.41074871061646284, | |
284 0.272257430756461, | |
285 0.4201989685435462, | |
286 0.34229059218038554, | |
287 ] | |
288 | |
289 H = nx.DiGraph(edges) | |
290 cls.H = G.reverse() | |
291 cls.H.alpha = 0.1 | |
292 cls.H.evc = [ | |
293 0.3289589783189635, | |
294 0.2832077296243516, | |
295 0.3425906003685471, | |
296 0.3970420865198392, | |
297 0.41074871061646284, | |
298 0.272257430756461, | |
299 0.4201989685435462, | |
300 0.34229059218038554, | |
301 ] | |
302 | |
303 def test_katz_centrality_weighted(self): | |
304 G = self.G | |
305 alpha = self.G.alpha | |
306 p = nx.katz_centrality(G, alpha, weight="weight") | |
307 for (a, b) in zip(list(p.values()), self.G.evc): | |
308 assert almost_equal(a, b) | |
309 | |
310 def test_katz_centrality_unweighted(self): | |
311 H = self.H | |
312 alpha = self.H.alpha | |
313 p = nx.katz_centrality(H, alpha, weight="weight") | |
314 for (a, b) in zip(list(p.values()), self.H.evc): | |
315 assert almost_equal(a, b) | |
316 | |
317 | |
318 class TestKatzCentralityDirectedNumpy(TestKatzCentralityDirected): | |
319 @classmethod | |
320 def setup_class(cls): | |
321 global np | |
322 np = pytest.importorskip("numpy") | |
323 scipy = pytest.importorskip("scipy") | |
324 | |
325 def test_katz_centrality_weighted(self): | |
326 G = self.G | |
327 alpha = self.G.alpha | |
328 p = nx.katz_centrality_numpy(G, alpha, weight="weight") | |
329 for (a, b) in zip(list(p.values()), self.G.evc): | |
330 assert almost_equal(a, b) | |
331 | |
332 def test_katz_centrality_unweighted(self): | |
333 H = self.H | |
334 alpha = self.H.alpha | |
335 p = nx.katz_centrality_numpy(H, alpha, weight="weight") | |
336 for (a, b) in zip(list(p.values()), self.H.evc): | |
337 assert almost_equal(a, b) | |
338 | |
339 | |
340 class TestKatzEigenvectorVKatz: | |
341 @classmethod | |
342 def setup_class(cls): | |
343 global np | |
344 global eigvals | |
345 np = pytest.importorskip("numpy") | |
346 scipy = pytest.importorskip("scipy") | |
347 from numpy.linalg import eigvals | |
348 | |
349 def test_eigenvector_v_katz_random(self): | |
350 G = nx.gnp_random_graph(10, 0.5, seed=1234) | |
351 l = float(max(eigvals(nx.adjacency_matrix(G).todense()))) | |
352 e = nx.eigenvector_centrality_numpy(G) | |
353 k = nx.katz_centrality_numpy(G, 1.0 / l) | |
354 for n in G: | |
355 assert almost_equal(e[n], k[n]) |