Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/tests/test_cluster.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 | |
3 | |
4 class TestTriangles: | |
5 def test_empty(self): | |
6 G = nx.Graph() | |
7 assert list(nx.triangles(G).values()) == [] | |
8 | |
9 def test_path(self): | |
10 G = nx.path_graph(10) | |
11 assert list(nx.triangles(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
12 assert nx.triangles(G) == { | |
13 0: 0, | |
14 1: 0, | |
15 2: 0, | |
16 3: 0, | |
17 4: 0, | |
18 5: 0, | |
19 6: 0, | |
20 7: 0, | |
21 8: 0, | |
22 9: 0, | |
23 } | |
24 | |
25 def test_cubical(self): | |
26 G = nx.cubical_graph() | |
27 assert list(nx.triangles(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0] | |
28 assert nx.triangles(G, 1) == 0 | |
29 assert list(nx.triangles(G, [1, 2]).values()) == [0, 0] | |
30 assert nx.triangles(G, 1) == 0 | |
31 assert nx.triangles(G, [1, 2]) == {1: 0, 2: 0} | |
32 | |
33 def test_k5(self): | |
34 G = nx.complete_graph(5) | |
35 assert list(nx.triangles(G).values()) == [6, 6, 6, 6, 6] | |
36 assert sum(nx.triangles(G).values()) / 3.0 == 10 | |
37 assert nx.triangles(G, 1) == 6 | |
38 G.remove_edge(1, 2) | |
39 assert list(nx.triangles(G).values()) == [5, 3, 3, 5, 5] | |
40 assert nx.triangles(G, 1) == 3 | |
41 | |
42 | |
43 class TestDirectedClustering: | |
44 def test_clustering(self): | |
45 G = nx.DiGraph() | |
46 assert list(nx.clustering(G).values()) == [] | |
47 assert nx.clustering(G) == {} | |
48 | |
49 def test_path(self): | |
50 G = nx.path_graph(10, create_using=nx.DiGraph()) | |
51 assert list(nx.clustering(G).values()) == [ | |
52 0.0, | |
53 0.0, | |
54 0.0, | |
55 0.0, | |
56 0.0, | |
57 0.0, | |
58 0.0, | |
59 0.0, | |
60 0.0, | |
61 0.0, | |
62 ] | |
63 assert nx.clustering(G) == { | |
64 0: 0.0, | |
65 1: 0.0, | |
66 2: 0.0, | |
67 3: 0.0, | |
68 4: 0.0, | |
69 5: 0.0, | |
70 6: 0.0, | |
71 7: 0.0, | |
72 8: 0.0, | |
73 9: 0.0, | |
74 } | |
75 | |
76 def test_k5(self): | |
77 G = nx.complete_graph(5, create_using=nx.DiGraph()) | |
78 assert list(nx.clustering(G).values()) == [1, 1, 1, 1, 1] | |
79 assert nx.average_clustering(G) == 1 | |
80 G.remove_edge(1, 2) | |
81 assert list(nx.clustering(G).values()) == [ | |
82 11.0 / 12.0, | |
83 1.0, | |
84 1.0, | |
85 11.0 / 12.0, | |
86 11.0 / 12.0, | |
87 ] | |
88 assert nx.clustering(G, [1, 4]) == {1: 1.0, 4: 11.0 / 12.0} | |
89 G.remove_edge(2, 1) | |
90 assert list(nx.clustering(G).values()) == [ | |
91 5.0 / 6.0, | |
92 1.0, | |
93 1.0, | |
94 5.0 / 6.0, | |
95 5.0 / 6.0, | |
96 ] | |
97 assert nx.clustering(G, [1, 4]) == {1: 1.0, 4: 0.83333333333333337} | |
98 | |
99 def test_triangle_and_edge(self): | |
100 G = nx.cycle_graph(3, create_using=nx.DiGraph()) | |
101 G.add_edge(0, 4) | |
102 assert nx.clustering(G)[0] == 1.0 / 6.0 | |
103 | |
104 | |
105 class TestDirectedWeightedClustering: | |
106 def test_clustering(self): | |
107 G = nx.DiGraph() | |
108 assert list(nx.clustering(G, weight="weight").values()) == [] | |
109 assert nx.clustering(G) == {} | |
110 | |
111 def test_path(self): | |
112 G = nx.path_graph(10, create_using=nx.DiGraph()) | |
113 assert list(nx.clustering(G, weight="weight").values()) == [ | |
114 0.0, | |
115 0.0, | |
116 0.0, | |
117 0.0, | |
118 0.0, | |
119 0.0, | |
120 0.0, | |
121 0.0, | |
122 0.0, | |
123 0.0, | |
124 ] | |
125 assert nx.clustering(G, weight="weight") == { | |
126 0: 0.0, | |
127 1: 0.0, | |
128 2: 0.0, | |
129 3: 0.0, | |
130 4: 0.0, | |
131 5: 0.0, | |
132 6: 0.0, | |
133 7: 0.0, | |
134 8: 0.0, | |
135 9: 0.0, | |
136 } | |
137 | |
138 def test_k5(self): | |
139 G = nx.complete_graph(5, create_using=nx.DiGraph()) | |
140 assert list(nx.clustering(G, weight="weight").values()) == [1, 1, 1, 1, 1] | |
141 assert nx.average_clustering(G, weight="weight") == 1 | |
142 G.remove_edge(1, 2) | |
143 assert list(nx.clustering(G, weight="weight").values()) == [ | |
144 11.0 / 12.0, | |
145 1.0, | |
146 1.0, | |
147 11.0 / 12.0, | |
148 11.0 / 12.0, | |
149 ] | |
150 assert nx.clustering(G, [1, 4], weight="weight") == {1: 1.0, 4: 11.0 / 12.0} | |
151 G.remove_edge(2, 1) | |
152 assert list(nx.clustering(G, weight="weight").values()) == [ | |
153 5.0 / 6.0, | |
154 1.0, | |
155 1.0, | |
156 5.0 / 6.0, | |
157 5.0 / 6.0, | |
158 ] | |
159 assert nx.clustering(G, [1, 4], weight="weight") == { | |
160 1: 1.0, | |
161 4: 0.83333333333333337, | |
162 } | |
163 | |
164 def test_triangle_and_edge(self): | |
165 G = nx.cycle_graph(3, create_using=nx.DiGraph()) | |
166 G.add_edge(0, 4, weight=2) | |
167 assert nx.clustering(G)[0] == 1.0 / 6.0 | |
168 assert nx.clustering(G, weight="weight")[0] == 1.0 / 12.0 | |
169 | |
170 | |
171 class TestWeightedClustering: | |
172 def test_clustering(self): | |
173 G = nx.Graph() | |
174 assert list(nx.clustering(G, weight="weight").values()) == [] | |
175 assert nx.clustering(G) == {} | |
176 | |
177 def test_path(self): | |
178 G = nx.path_graph(10) | |
179 assert list(nx.clustering(G, weight="weight").values()) == [ | |
180 0.0, | |
181 0.0, | |
182 0.0, | |
183 0.0, | |
184 0.0, | |
185 0.0, | |
186 0.0, | |
187 0.0, | |
188 0.0, | |
189 0.0, | |
190 ] | |
191 assert nx.clustering(G, weight="weight") == { | |
192 0: 0.0, | |
193 1: 0.0, | |
194 2: 0.0, | |
195 3: 0.0, | |
196 4: 0.0, | |
197 5: 0.0, | |
198 6: 0.0, | |
199 7: 0.0, | |
200 8: 0.0, | |
201 9: 0.0, | |
202 } | |
203 | |
204 def test_cubical(self): | |
205 G = nx.cubical_graph() | |
206 assert list(nx.clustering(G, weight="weight").values()) == [ | |
207 0, | |
208 0, | |
209 0, | |
210 0, | |
211 0, | |
212 0, | |
213 0, | |
214 0, | |
215 ] | |
216 assert nx.clustering(G, 1) == 0 | |
217 assert list(nx.clustering(G, [1, 2], weight="weight").values()) == [0, 0] | |
218 assert nx.clustering(G, 1, weight="weight") == 0 | |
219 assert nx.clustering(G, [1, 2], weight="weight") == {1: 0, 2: 0} | |
220 | |
221 def test_k5(self): | |
222 G = nx.complete_graph(5) | |
223 assert list(nx.clustering(G, weight="weight").values()) == [1, 1, 1, 1, 1] | |
224 assert nx.average_clustering(G, weight="weight") == 1 | |
225 G.remove_edge(1, 2) | |
226 assert list(nx.clustering(G, weight="weight").values()) == [ | |
227 5.0 / 6.0, | |
228 1.0, | |
229 1.0, | |
230 5.0 / 6.0, | |
231 5.0 / 6.0, | |
232 ] | |
233 assert nx.clustering(G, [1, 4], weight="weight") == { | |
234 1: 1.0, | |
235 4: 0.83333333333333337, | |
236 } | |
237 | |
238 def test_triangle_and_edge(self): | |
239 G = nx.cycle_graph(3) | |
240 G.add_edge(0, 4, weight=2) | |
241 assert nx.clustering(G)[0] == 1.0 / 3.0 | |
242 assert nx.clustering(G, weight="weight")[0] == 1.0 / 6.0 | |
243 | |
244 | |
245 class TestClustering: | |
246 def test_clustering(self): | |
247 G = nx.Graph() | |
248 assert list(nx.clustering(G).values()) == [] | |
249 assert nx.clustering(G) == {} | |
250 | |
251 def test_path(self): | |
252 G = nx.path_graph(10) | |
253 assert list(nx.clustering(G).values()) == [ | |
254 0.0, | |
255 0.0, | |
256 0.0, | |
257 0.0, | |
258 0.0, | |
259 0.0, | |
260 0.0, | |
261 0.0, | |
262 0.0, | |
263 0.0, | |
264 ] | |
265 assert nx.clustering(G) == { | |
266 0: 0.0, | |
267 1: 0.0, | |
268 2: 0.0, | |
269 3: 0.0, | |
270 4: 0.0, | |
271 5: 0.0, | |
272 6: 0.0, | |
273 7: 0.0, | |
274 8: 0.0, | |
275 9: 0.0, | |
276 } | |
277 | |
278 def test_cubical(self): | |
279 G = nx.cubical_graph() | |
280 assert list(nx.clustering(G).values()) == [0, 0, 0, 0, 0, 0, 0, 0] | |
281 assert nx.clustering(G, 1) == 0 | |
282 assert list(nx.clustering(G, [1, 2]).values()) == [0, 0] | |
283 assert nx.clustering(G, 1) == 0 | |
284 assert nx.clustering(G, [1, 2]) == {1: 0, 2: 0} | |
285 | |
286 def test_k5(self): | |
287 G = nx.complete_graph(5) | |
288 assert list(nx.clustering(G).values()) == [1, 1, 1, 1, 1] | |
289 assert nx.average_clustering(G) == 1 | |
290 G.remove_edge(1, 2) | |
291 assert list(nx.clustering(G).values()) == [ | |
292 5.0 / 6.0, | |
293 1.0, | |
294 1.0, | |
295 5.0 / 6.0, | |
296 5.0 / 6.0, | |
297 ] | |
298 assert nx.clustering(G, [1, 4]) == {1: 1.0, 4: 0.83333333333333337} | |
299 | |
300 | |
301 class TestTransitivity: | |
302 def test_transitivity(self): | |
303 G = nx.Graph() | |
304 assert nx.transitivity(G) == 0.0 | |
305 | |
306 def test_path(self): | |
307 G = nx.path_graph(10) | |
308 assert nx.transitivity(G) == 0.0 | |
309 | |
310 def test_cubical(self): | |
311 G = nx.cubical_graph() | |
312 assert nx.transitivity(G) == 0.0 | |
313 | |
314 def test_k5(self): | |
315 G = nx.complete_graph(5) | |
316 assert nx.transitivity(G) == 1.0 | |
317 G.remove_edge(1, 2) | |
318 assert nx.transitivity(G) == 0.875 | |
319 | |
320 | |
321 class TestSquareClustering: | |
322 def test_clustering(self): | |
323 G = nx.Graph() | |
324 assert list(nx.square_clustering(G).values()) == [] | |
325 assert nx.square_clustering(G) == {} | |
326 | |
327 def test_path(self): | |
328 G = nx.path_graph(10) | |
329 assert list(nx.square_clustering(G).values()) == [ | |
330 0.0, | |
331 0.0, | |
332 0.0, | |
333 0.0, | |
334 0.0, | |
335 0.0, | |
336 0.0, | |
337 0.0, | |
338 0.0, | |
339 0.0, | |
340 ] | |
341 assert nx.square_clustering(G) == { | |
342 0: 0.0, | |
343 1: 0.0, | |
344 2: 0.0, | |
345 3: 0.0, | |
346 4: 0.0, | |
347 5: 0.0, | |
348 6: 0.0, | |
349 7: 0.0, | |
350 8: 0.0, | |
351 9: 0.0, | |
352 } | |
353 | |
354 def test_cubical(self): | |
355 G = nx.cubical_graph() | |
356 assert list(nx.square_clustering(G).values()) == [ | |
357 0.5, | |
358 0.5, | |
359 0.5, | |
360 0.5, | |
361 0.5, | |
362 0.5, | |
363 0.5, | |
364 0.5, | |
365 ] | |
366 assert list(nx.square_clustering(G, [1, 2]).values()) == [0.5, 0.5] | |
367 assert nx.square_clustering(G, [1])[1] == 0.5 | |
368 assert nx.square_clustering(G, [1, 2]) == {1: 0.5, 2: 0.5} | |
369 | |
370 def test_k5(self): | |
371 G = nx.complete_graph(5) | |
372 assert list(nx.square_clustering(G).values()) == [1, 1, 1, 1, 1] | |
373 | |
374 def test_bipartite_k5(self): | |
375 G = nx.complete_bipartite_graph(5, 5) | |
376 assert list(nx.square_clustering(G).values()) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
377 | |
378 def test_lind_square_clustering(self): | |
379 """Test C4 for figure 1 Lind et al (2005)""" | |
380 G = nx.Graph( | |
381 [ | |
382 (1, 2), | |
383 (1, 3), | |
384 (1, 6), | |
385 (1, 7), | |
386 (2, 4), | |
387 (2, 5), | |
388 (3, 4), | |
389 (3, 5), | |
390 (6, 7), | |
391 (7, 8), | |
392 (6, 8), | |
393 (7, 9), | |
394 (7, 10), | |
395 (6, 11), | |
396 (6, 12), | |
397 (2, 13), | |
398 (2, 14), | |
399 (3, 15), | |
400 (3, 16), | |
401 ] | |
402 ) | |
403 G1 = G.subgraph([1, 2, 3, 4, 5, 13, 14, 15, 16]) | |
404 G2 = G.subgraph([1, 6, 7, 8, 9, 10, 11, 12]) | |
405 assert nx.square_clustering(G, [1])[1] == 3 / 75.0 | |
406 assert nx.square_clustering(G1, [1])[1] == 2 / 6.0 | |
407 assert nx.square_clustering(G2, [1])[1] == 1 / 5.0 | |
408 | |
409 | |
410 def test_average_clustering(): | |
411 G = nx.cycle_graph(3) | |
412 G.add_edge(2, 3) | |
413 assert nx.average_clustering(G) == (1 + 1 + 1 / 3.0) / 4.0 | |
414 assert nx.average_clustering(G, count_zeros=True) == (1 + 1 + 1 / 3.0) / 4.0 | |
415 assert nx.average_clustering(G, count_zeros=False) == (1 + 1 + 1 / 3.0) / 3.0 | |
416 | |
417 | |
418 class TestGeneralizedDegree: | |
419 def test_generalized_degree(self): | |
420 G = nx.Graph() | |
421 assert nx.generalized_degree(G) == {} | |
422 | |
423 def test_path(self): | |
424 G = nx.path_graph(5) | |
425 assert nx.generalized_degree(G, 0) == {0: 1} | |
426 assert nx.generalized_degree(G, 1) == {0: 2} | |
427 | |
428 def test_cubical(self): | |
429 G = nx.cubical_graph() | |
430 assert nx.generalized_degree(G, 0) == {0: 3} | |
431 | |
432 def test_k5(self): | |
433 G = nx.complete_graph(5) | |
434 assert nx.generalized_degree(G, 0) == {3: 4} | |
435 G.remove_edge(0, 1) | |
436 assert nx.generalized_degree(G, 0) == {2: 3} |