Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_geometric.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 from itertools import combinations | |
2 from math import sqrt | |
3 import random | |
4 | |
5 | |
6 import networkx as nx | |
7 from networkx.generators.geometric import euclidean | |
8 | |
9 | |
10 def l1dist(x, y): | |
11 return sum(abs(a - b) for a, b in zip(x, y)) | |
12 | |
13 | |
14 class TestRandomGeometricGraph: | |
15 """Unit tests for the :func:`~networkx.random_geometric_graph` | |
16 function. | |
17 | |
18 """ | |
19 | |
20 def test_number_of_nodes(self): | |
21 G = nx.random_geometric_graph(50, 0.25, seed=42) | |
22 assert len(G) == 50 | |
23 G = nx.random_geometric_graph(range(50), 0.25, seed=42) | |
24 assert len(G) == 50 | |
25 | |
26 def test_distances(self): | |
27 """Tests that pairs of vertices adjacent if and only if they are | |
28 within the prescribed radius. | |
29 | |
30 """ | |
31 # Use the Euclidean metric, the default according to the | |
32 # documentation. | |
33 dist = euclidean | |
34 G = nx.random_geometric_graph(50, 0.25) | |
35 for u, v in combinations(G, 2): | |
36 # Adjacent vertices must be within the given distance. | |
37 if v in G[u]: | |
38 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
39 # Nonadjacent vertices must be at greater distance. | |
40 else: | |
41 assert not dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
42 | |
43 def test_p(self): | |
44 """Tests for providing an alternate distance metric to the | |
45 generator. | |
46 | |
47 """ | |
48 # Use the L1 metric. | |
49 dist = l1dist | |
50 G = nx.random_geometric_graph(50, 0.25, p=1) | |
51 for u, v in combinations(G, 2): | |
52 # Adjacent vertices must be within the given distance. | |
53 if v in G[u]: | |
54 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
55 # Nonadjacent vertices must be at greater distance. | |
56 else: | |
57 assert not dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
58 | |
59 def test_node_names(self): | |
60 """Tests using values other than sequential numbers as node IDs. | |
61 | |
62 """ | |
63 import string | |
64 | |
65 nodes = list(string.ascii_lowercase) | |
66 G = nx.random_geometric_graph(nodes, 0.25) | |
67 assert len(G) == len(nodes) | |
68 | |
69 dist = euclidean | |
70 for u, v in combinations(G, 2): | |
71 # Adjacent vertices must be within the given distance. | |
72 if v in G[u]: | |
73 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
74 # Nonadjacent vertices must be at greater distance. | |
75 else: | |
76 assert not dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
77 | |
78 | |
79 class TestSoftRandomGeometricGraph: | |
80 """Unit tests for the :func:`~networkx.soft_random_geometric_graph` | |
81 function. | |
82 | |
83 """ | |
84 | |
85 def test_number_of_nodes(self): | |
86 G = nx.soft_random_geometric_graph(50, 0.25, seed=42) | |
87 assert len(G) == 50 | |
88 G = nx.soft_random_geometric_graph(range(50), 0.25, seed=42) | |
89 assert len(G) == 50 | |
90 | |
91 def test_distances(self): | |
92 """Tests that pairs of vertices adjacent if and only if they are | |
93 within the prescribed radius. | |
94 | |
95 """ | |
96 # Use the Euclidean metric, the default according to the | |
97 # documentation. | |
98 def dist(x, y): | |
99 return sqrt(sum((a - b) ** 2 for a, b in zip(x, y))) | |
100 | |
101 G = nx.soft_random_geometric_graph(50, 0.25) | |
102 for u, v in combinations(G, 2): | |
103 # Adjacent vertices must be within the given distance. | |
104 if v in G[u]: | |
105 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
106 | |
107 def test_p(self): | |
108 """Tests for providing an alternate distance metric to the | |
109 generator. | |
110 | |
111 """ | |
112 # Use the L1 metric. | |
113 def dist(x, y): | |
114 return sum(abs(a - b) for a, b in zip(x, y)) | |
115 | |
116 G = nx.soft_random_geometric_graph(50, 0.25, p=1) | |
117 for u, v in combinations(G, 2): | |
118 # Adjacent vertices must be within the given distance. | |
119 if v in G[u]: | |
120 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
121 | |
122 def test_node_names(self): | |
123 """Tests using values other than sequential numbers as node IDs. | |
124 | |
125 """ | |
126 import string | |
127 | |
128 nodes = list(string.ascii_lowercase) | |
129 G = nx.soft_random_geometric_graph(nodes, 0.25) | |
130 assert len(G) == len(nodes) | |
131 | |
132 def dist(x, y): | |
133 return sqrt(sum((a - b) ** 2 for a, b in zip(x, y))) | |
134 | |
135 for u, v in combinations(G, 2): | |
136 # Adjacent vertices must be within the given distance. | |
137 if v in G[u]: | |
138 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
139 | |
140 def test_p_dist_default(self): | |
141 """Tests default p_dict = 0.5 returns graph with edge count <= RGG with | |
142 same n, radius, dim and positions | |
143 | |
144 """ | |
145 nodes = 50 | |
146 dim = 2 | |
147 pos = {v: [random.random() for i in range(dim)] for v in range(nodes)} | |
148 RGG = nx.random_geometric_graph(50, 0.25, pos=pos) | |
149 SRGG = nx.soft_random_geometric_graph(50, 0.25, pos=pos) | |
150 assert len(SRGG.edges()) <= len(RGG.edges()) | |
151 | |
152 def test_p_dist_zero(self): | |
153 """Tests if p_dict = 0 returns disconencted graph with 0 edges | |
154 | |
155 """ | |
156 | |
157 def p_dist(dist): | |
158 return 0 | |
159 | |
160 G = nx.soft_random_geometric_graph(50, 0.25, p_dist=p_dist) | |
161 assert len(G.edges) == 0 | |
162 | |
163 | |
164 def join(G, u, v, theta, alpha, metric): | |
165 """Returns ``True`` if and only if the nodes whose attributes are | |
166 ``du`` and ``dv`` should be joined, according to the threshold | |
167 condition for geographical threshold graphs. | |
168 | |
169 ``G`` is an undirected NetworkX graph, and ``u`` and ``v`` are nodes | |
170 in that graph. The nodes must have node attributes ``'pos'`` and | |
171 ``'weight'``. | |
172 | |
173 ``metric`` is a distance metric. | |
174 | |
175 """ | |
176 du, dv = G.nodes[u], G.nodes[v] | |
177 u_pos, v_pos = du["pos"], dv["pos"] | |
178 u_weight, v_weight = du["weight"], dv["weight"] | |
179 return (u_weight + v_weight) * metric(u_pos, v_pos) ** alpha >= theta | |
180 | |
181 | |
182 class TestGeographicalThresholdGraph: | |
183 """Unit tests for the :func:`~networkx.geographical_threshold_graph` | |
184 function. | |
185 | |
186 """ | |
187 | |
188 def test_number_of_nodes(self): | |
189 G = nx.geographical_threshold_graph(50, 100, seed=42) | |
190 assert len(G) == 50 | |
191 G = nx.geographical_threshold_graph(range(50), 100, seed=42) | |
192 assert len(G) == 50 | |
193 | |
194 def test_distances(self): | |
195 """Tests that pairs of vertices adjacent if and only if their | |
196 distances meet the given threshold. | |
197 | |
198 """ | |
199 # Use the Euclidean metric and alpha = -2 | |
200 # the default according to the documentation. | |
201 dist = euclidean | |
202 G = nx.geographical_threshold_graph(50, 10) | |
203 for u, v in combinations(G, 2): | |
204 # Adjacent vertices must exceed the threshold. | |
205 if v in G[u]: | |
206 assert join(G, u, v, 10, -2, dist) | |
207 # Nonadjacent vertices must not exceed the threshold. | |
208 else: | |
209 assert not join(G, u, v, 10, -2, dist) | |
210 | |
211 def test_metric(self): | |
212 """Tests for providing an alternate distance metric to the | |
213 generator. | |
214 | |
215 """ | |
216 # Use the L1 metric. | |
217 dist = l1dist | |
218 G = nx.geographical_threshold_graph(50, 10, metric=dist) | |
219 for u, v in combinations(G, 2): | |
220 # Adjacent vertices must exceed the threshold. | |
221 if v in G[u]: | |
222 assert join(G, u, v, 10, -2, dist) | |
223 # Nonadjacent vertices must not exceed the threshold. | |
224 else: | |
225 assert not join(G, u, v, 10, -2, dist) | |
226 | |
227 def test_p_dist_zero(self): | |
228 """Tests if p_dict = 0 returns disconencted graph with 0 edges | |
229 | |
230 """ | |
231 | |
232 def p_dist(dist): | |
233 return 0 | |
234 | |
235 G = nx.geographical_threshold_graph(50, 1, p_dist=p_dist) | |
236 assert len(G.edges) == 0 | |
237 | |
238 | |
239 class TestWaxmanGraph: | |
240 """Unit tests for the :func:`~networkx.waxman_graph` function.""" | |
241 | |
242 def test_number_of_nodes_1(self): | |
243 G = nx.waxman_graph(50, 0.5, 0.1, seed=42) | |
244 assert len(G) == 50 | |
245 G = nx.waxman_graph(range(50), 0.5, 0.1, seed=42) | |
246 assert len(G) == 50 | |
247 | |
248 def test_number_of_nodes_2(self): | |
249 G = nx.waxman_graph(50, 0.5, 0.1, L=1) | |
250 assert len(G) == 50 | |
251 G = nx.waxman_graph(range(50), 0.5, 0.1, L=1) | |
252 assert len(G) == 50 | |
253 | |
254 def test_metric(self): | |
255 """Tests for providing an alternate distance metric to the | |
256 generator. | |
257 | |
258 """ | |
259 # Use the L1 metric. | |
260 dist = l1dist | |
261 G = nx.waxman_graph(50, 0.5, 0.1, metric=dist) | |
262 assert len(G) == 50 | |
263 | |
264 | |
265 class TestNavigableSmallWorldGraph: | |
266 def test_navigable_small_world(self): | |
267 G = nx.navigable_small_world_graph(5, p=1, q=0, seed=42) | |
268 gg = nx.grid_2d_graph(5, 5).to_directed() | |
269 assert nx.is_isomorphic(G, gg) | |
270 | |
271 G = nx.navigable_small_world_graph(5, p=1, q=0, dim=3) | |
272 gg = nx.grid_graph([5, 5, 5]).to_directed() | |
273 assert nx.is_isomorphic(G, gg) | |
274 | |
275 G = nx.navigable_small_world_graph(5, p=1, q=0, dim=1) | |
276 gg = nx.grid_graph([5]).to_directed() | |
277 assert nx.is_isomorphic(G, gg) | |
278 | |
279 | |
280 class TestThresholdedRandomGeometricGraph: | |
281 """Unit tests for the :func:`~networkx.thresholded_random_geometric_graph` | |
282 function. | |
283 | |
284 """ | |
285 | |
286 def test_number_of_nodes(self): | |
287 G = nx.thresholded_random_geometric_graph(50, 0.2, 0.1, seed=42) | |
288 assert len(G) == 50 | |
289 G = nx.thresholded_random_geometric_graph(range(50), 0.2, 0.1) | |
290 assert len(G) == 50 | |
291 | |
292 def test_distances(self): | |
293 """Tests that pairs of vertices adjacent if and only if they are | |
294 within the prescribed radius. | |
295 | |
296 """ | |
297 # Use the Euclidean metric, the default according to the | |
298 # documentation. | |
299 def dist(x, y): | |
300 return sqrt(sum((a - b) ** 2 for a, b in zip(x, y))) | |
301 | |
302 G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1) | |
303 for u, v in combinations(G, 2): | |
304 # Adjacent vertices must be within the given distance. | |
305 if v in G[u]: | |
306 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
307 | |
308 def test_p(self): | |
309 """Tests for providing an alternate distance metric to the | |
310 generator. | |
311 | |
312 """ | |
313 # Use the L1 metric. | |
314 def dist(x, y): | |
315 return sum(abs(a - b) for a, b in zip(x, y)) | |
316 | |
317 G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1, p=1) | |
318 for u, v in combinations(G, 2): | |
319 # Adjacent vertices must be within the given distance. | |
320 if v in G[u]: | |
321 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
322 | |
323 def test_node_names(self): | |
324 """Tests using values other than sequential numbers as node IDs. | |
325 | |
326 """ | |
327 import string | |
328 | |
329 nodes = list(string.ascii_lowercase) | |
330 G = nx.thresholded_random_geometric_graph(nodes, 0.25, 0.1) | |
331 assert len(G) == len(nodes) | |
332 | |
333 def dist(x, y): | |
334 return sqrt(sum((a - b) ** 2 for a, b in zip(x, y))) | |
335 | |
336 for u, v in combinations(G, 2): | |
337 # Adjacent vertices must be within the given distance. | |
338 if v in G[u]: | |
339 assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25 | |
340 | |
341 def test_theta(self): | |
342 """Tests that pairs of vertices adjacent if and only if their sum | |
343 weights exceeds the threshold parameter theta. | |
344 """ | |
345 G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1) | |
346 | |
347 for u, v in combinations(G, 2): | |
348 # Adjacent vertices must be within the given distance. | |
349 if v in G[u]: | |
350 assert (G.nodes[u]["weight"] + G.nodes[v]["weight"]) >= 0.1 |