Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_degree_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 """ | |
2 Unit tests for degree centrality. | |
3 """ | |
4 | |
5 | |
6 import networkx as nx | |
7 from networkx.testing import almost_equal | |
8 | |
9 | |
10 class TestDegreeCentrality: | |
11 def setup_method(self): | |
12 | |
13 self.K = nx.krackhardt_kite_graph() | |
14 self.P3 = nx.path_graph(3) | |
15 self.K5 = nx.complete_graph(5) | |
16 | |
17 F = nx.Graph() # Florentine families | |
18 F.add_edge("Acciaiuoli", "Medici") | |
19 F.add_edge("Castellani", "Peruzzi") | |
20 F.add_edge("Castellani", "Strozzi") | |
21 F.add_edge("Castellani", "Barbadori") | |
22 F.add_edge("Medici", "Barbadori") | |
23 F.add_edge("Medici", "Ridolfi") | |
24 F.add_edge("Medici", "Tornabuoni") | |
25 F.add_edge("Medici", "Albizzi") | |
26 F.add_edge("Medici", "Salviati") | |
27 F.add_edge("Salviati", "Pazzi") | |
28 F.add_edge("Peruzzi", "Strozzi") | |
29 F.add_edge("Peruzzi", "Bischeri") | |
30 F.add_edge("Strozzi", "Ridolfi") | |
31 F.add_edge("Strozzi", "Bischeri") | |
32 F.add_edge("Ridolfi", "Tornabuoni") | |
33 F.add_edge("Tornabuoni", "Guadagni") | |
34 F.add_edge("Albizzi", "Ginori") | |
35 F.add_edge("Albizzi", "Guadagni") | |
36 F.add_edge("Bischeri", "Guadagni") | |
37 F.add_edge("Guadagni", "Lamberteschi") | |
38 self.F = F | |
39 | |
40 G = nx.DiGraph() | |
41 G.add_edge(0, 5) | |
42 G.add_edge(1, 5) | |
43 G.add_edge(2, 5) | |
44 G.add_edge(3, 5) | |
45 G.add_edge(4, 5) | |
46 G.add_edge(5, 6) | |
47 G.add_edge(5, 7) | |
48 G.add_edge(5, 8) | |
49 self.G = G | |
50 | |
51 def test_degree_centrality_1(self): | |
52 d = nx.degree_centrality(self.K5) | |
53 exact = dict(zip(range(5), [1] * 5)) | |
54 for n, dc in d.items(): | |
55 assert almost_equal(exact[n], dc) | |
56 | |
57 def test_degree_centrality_2(self): | |
58 d = nx.degree_centrality(self.P3) | |
59 exact = {0: 0.5, 1: 1, 2: 0.5} | |
60 for n, dc in d.items(): | |
61 assert almost_equal(exact[n], dc) | |
62 | |
63 def test_degree_centrality_3(self): | |
64 d = nx.degree_centrality(self.K) | |
65 exact = { | |
66 0: 0.444, | |
67 1: 0.444, | |
68 2: 0.333, | |
69 3: 0.667, | |
70 4: 0.333, | |
71 5: 0.556, | |
72 6: 0.556, | |
73 7: 0.333, | |
74 8: 0.222, | |
75 9: 0.111, | |
76 } | |
77 for n, dc in d.items(): | |
78 assert almost_equal(exact[n], float(f"{dc:.3f}")) | |
79 | |
80 def test_degree_centrality_4(self): | |
81 d = nx.degree_centrality(self.F) | |
82 names = sorted(self.F.nodes()) | |
83 dcs = [ | |
84 0.071, | |
85 0.214, | |
86 0.143, | |
87 0.214, | |
88 0.214, | |
89 0.071, | |
90 0.286, | |
91 0.071, | |
92 0.429, | |
93 0.071, | |
94 0.214, | |
95 0.214, | |
96 0.143, | |
97 0.286, | |
98 0.214, | |
99 ] | |
100 exact = dict(zip(names, dcs)) | |
101 for n, dc in d.items(): | |
102 assert almost_equal(exact[n], float(f"{dc:.3f}")) | |
103 | |
104 def test_indegree_centrality(self): | |
105 d = nx.in_degree_centrality(self.G) | |
106 exact = { | |
107 0: 0.0, | |
108 1: 0.0, | |
109 2: 0.0, | |
110 3: 0.0, | |
111 4: 0.0, | |
112 5: 0.625, | |
113 6: 0.125, | |
114 7: 0.125, | |
115 8: 0.125, | |
116 } | |
117 for n, dc in d.items(): | |
118 assert almost_equal(exact[n], dc) | |
119 | |
120 def test_outdegree_centrality(self): | |
121 d = nx.out_degree_centrality(self.G) | |
122 exact = { | |
123 0: 0.125, | |
124 1: 0.125, | |
125 2: 0.125, | |
126 3: 0.125, | |
127 4: 0.125, | |
128 5: 0.375, | |
129 6: 0.0, | |
130 7: 0.0, | |
131 8: 0.0, | |
132 } | |
133 for n, dc in d.items(): | |
134 assert almost_equal(exact[n], dc) | |
135 | |
136 def test_small_graph_centrality(self): | |
137 G = nx.empty_graph(create_using=nx.DiGraph) | |
138 assert {} == nx.degree_centrality(G) | |
139 assert {} == nx.out_degree_centrality(G) | |
140 assert {} == nx.in_degree_centrality(G) | |
141 | |
142 G = nx.empty_graph(1, create_using=nx.DiGraph) | |
143 assert {0: 1} == nx.degree_centrality(G) | |
144 assert {0: 1} == nx.out_degree_centrality(G) | |
145 assert {0: 1} == nx.in_degree_centrality(G) |