comparison env/lib/python3.9/site-packages/networkx/algorithms/centrality/tests/test_eigenvector_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 import pytest
3
4 np = pytest.importorskip("numpy")
5 scipy = pytest.importorskip("scipy")
6
7
8 import networkx as nx
9 from networkx.testing import almost_equal
10
11
12 class TestEigenvectorCentrality:
13 def test_K5(self):
14 """Eigenvector centrality: K5"""
15 G = nx.complete_graph(5)
16 b = nx.eigenvector_centrality(G)
17 v = math.sqrt(1 / 5.0)
18 b_answer = dict.fromkeys(G, v)
19 for n in sorted(G):
20 assert almost_equal(b[n], b_answer[n])
21 nstart = {n: 1 for n in G}
22 b = nx.eigenvector_centrality(G, nstart=nstart)
23 for n in sorted(G):
24 assert almost_equal(b[n], b_answer[n])
25
26 b = nx.eigenvector_centrality_numpy(G)
27 for n in sorted(G):
28 assert almost_equal(b[n], b_answer[n], places=3)
29
30 def test_P3(self):
31 """Eigenvector centrality: P3"""
32 G = nx.path_graph(3)
33 b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
34 b = nx.eigenvector_centrality_numpy(G)
35 for n in sorted(G):
36 assert almost_equal(b[n], b_answer[n], places=4)
37 b = nx.eigenvector_centrality(G)
38 for n in sorted(G):
39 assert almost_equal(b[n], b_answer[n], places=4)
40
41 def test_P3_unweighted(self):
42 """Eigenvector centrality: P3"""
43 G = nx.path_graph(3)
44 b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
45 b = nx.eigenvector_centrality_numpy(G, weight=None)
46 for n in sorted(G):
47 assert almost_equal(b[n], b_answer[n], places=4)
48
49 def test_maxiter(self):
50 with pytest.raises(nx.PowerIterationFailedConvergence):
51 G = nx.path_graph(3)
52 b = nx.eigenvector_centrality(G, max_iter=0)
53
54
55 class TestEigenvectorCentralityDirected:
56 @classmethod
57 def setup_class(cls):
58 G = nx.DiGraph()
59
60 edges = [
61 (1, 2),
62 (1, 3),
63 (2, 4),
64 (3, 2),
65 (3, 5),
66 (4, 2),
67 (4, 5),
68 (4, 6),
69 (5, 6),
70 (5, 7),
71 (5, 8),
72 (6, 8),
73 (7, 1),
74 (7, 5),
75 (7, 8),
76 (8, 6),
77 (8, 7),
78 ]
79
80 G.add_edges_from(edges, weight=2.0)
81 cls.G = G.reverse()
82 cls.G.evc = [
83 0.25368793,
84 0.19576478,
85 0.32817092,
86 0.40430835,
87 0.48199885,
88 0.15724483,
89 0.51346196,
90 0.32475403,
91 ]
92
93 H = nx.DiGraph()
94
95 edges = [
96 (1, 2),
97 (1, 3),
98 (2, 4),
99 (3, 2),
100 (3, 5),
101 (4, 2),
102 (4, 5),
103 (4, 6),
104 (5, 6),
105 (5, 7),
106 (5, 8),
107 (6, 8),
108 (7, 1),
109 (7, 5),
110 (7, 8),
111 (8, 6),
112 (8, 7),
113 ]
114
115 G.add_edges_from(edges)
116 cls.H = G.reverse()
117 cls.H.evc = [
118 0.25368793,
119 0.19576478,
120 0.32817092,
121 0.40430835,
122 0.48199885,
123 0.15724483,
124 0.51346196,
125 0.32475403,
126 ]
127
128 def test_eigenvector_centrality_weighted(self):
129 G = self.G
130 p = nx.eigenvector_centrality(G)
131 for (a, b) in zip(list(p.values()), self.G.evc):
132 assert almost_equal(a, b, places=4)
133
134 def test_eigenvector_centrality_weighted_numpy(self):
135 G = self.G
136 p = nx.eigenvector_centrality_numpy(G)
137 for (a, b) in zip(list(p.values()), self.G.evc):
138 assert almost_equal(a, b)
139
140 def test_eigenvector_centrality_unweighted(self):
141 G = self.H
142 p = nx.eigenvector_centrality(G)
143 for (a, b) in zip(list(p.values()), self.G.evc):
144 assert almost_equal(a, b, places=4)
145
146 def test_eigenvector_centrality_unweighted_numpy(self):
147 G = self.H
148 p = nx.eigenvector_centrality_numpy(G)
149 for (a, b) in zip(list(p.values()), self.G.evc):
150 assert almost_equal(a, b)
151
152
153 class TestEigenvectorCentralityExceptions:
154 def test_multigraph(self):
155 with pytest.raises(nx.NetworkXException):
156 e = nx.eigenvector_centrality(nx.MultiGraph())
157
158 def test_multigraph_numpy(self):
159 with pytest.raises(nx.NetworkXException):
160 e = nx.eigenvector_centrality_numpy(nx.MultiGraph())
161
162 def test_empty(self):
163 with pytest.raises(nx.NetworkXException):
164 e = nx.eigenvector_centrality(nx.Graph())
165
166 def test_empty_numpy(self):
167 with pytest.raises(nx.NetworkXException):
168 e = nx.eigenvector_centrality_numpy(nx.Graph())