Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_lattice.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 """Unit tests for the :mod:`networkx.generators.lattice` module.""" | |
2 | |
3 import pytest | |
4 | |
5 import networkx as nx | |
6 from networkx.testing import assert_edges_equal | |
7 from itertools import product | |
8 | |
9 | |
10 class TestGrid2DGraph: | |
11 """Unit tests for :func:`networkx.generators.lattice.grid_2d_graph`""" | |
12 | |
13 def test_number_of_vertices(self): | |
14 m, n = 5, 6 | |
15 G = nx.grid_2d_graph(m, n) | |
16 assert len(G) == m * n | |
17 | |
18 def test_degree_distribution(self): | |
19 m, n = 5, 6 | |
20 G = nx.grid_2d_graph(m, n) | |
21 expected_histogram = [0, 0, 4, 2 * (m + n) - 8, (m - 2) * (n - 2)] | |
22 assert nx.degree_histogram(G) == expected_histogram | |
23 | |
24 def test_directed(self): | |
25 m, n = 5, 6 | |
26 G = nx.grid_2d_graph(m, n) | |
27 H = nx.grid_2d_graph(m, n, create_using=nx.DiGraph()) | |
28 assert H.succ == G.adj | |
29 assert H.pred == G.adj | |
30 | |
31 def test_multigraph(self): | |
32 m, n = 5, 6 | |
33 G = nx.grid_2d_graph(m, n) | |
34 H = nx.grid_2d_graph(m, n, create_using=nx.MultiGraph()) | |
35 assert list(H.edges()) == list(G.edges()) | |
36 | |
37 def test_periodic(self): | |
38 G = nx.grid_2d_graph(0, 0, periodic=True) | |
39 assert dict(G.degree()) == {} | |
40 | |
41 for m, n, H in [ | |
42 (2, 2, nx.cycle_graph(4)), | |
43 (1, 7, nx.cycle_graph(7)), | |
44 (7, 1, nx.cycle_graph(7)), | |
45 (2, 5, nx.circular_ladder_graph(5)), | |
46 (5, 2, nx.circular_ladder_graph(5)), | |
47 (2, 4, nx.cubical_graph()), | |
48 (4, 2, nx.cubical_graph()), | |
49 ]: | |
50 G = nx.grid_2d_graph(m, n, periodic=True) | |
51 assert nx.could_be_isomorphic(G, H) | |
52 | |
53 def test_periodic_iterable(self): | |
54 m, n = 3, 7 | |
55 for a, b in product([0, 1], [0, 1]): | |
56 G = nx.grid_2d_graph(m, n, periodic=(a, b)) | |
57 assert G.number_of_nodes() == m * n | |
58 assert G.number_of_edges() == (m + a - 1) * n + (n + b - 1) * m | |
59 | |
60 def test_periodic_directed(self): | |
61 G = nx.grid_2d_graph(4, 2, periodic=True) | |
62 H = nx.grid_2d_graph(4, 2, periodic=True, create_using=nx.DiGraph()) | |
63 assert H.succ == G.adj | |
64 assert H.pred == G.adj | |
65 | |
66 def test_periodic_multigraph(self): | |
67 G = nx.grid_2d_graph(4, 2, periodic=True) | |
68 H = nx.grid_2d_graph(4, 2, periodic=True, create_using=nx.MultiGraph()) | |
69 assert list(G.edges()) == list(H.edges()) | |
70 | |
71 def test_node_input(self): | |
72 G = nx.grid_2d_graph(4, 2, periodic=True) | |
73 H = nx.grid_2d_graph(range(4), range(2), periodic=True) | |
74 assert nx.is_isomorphic(H, G) | |
75 H = nx.grid_2d_graph("abcd", "ef", periodic=True) | |
76 assert nx.is_isomorphic(H, G) | |
77 G = nx.grid_2d_graph(5, 6) | |
78 H = nx.grid_2d_graph(range(5), range(6)) | |
79 assert_edges_equal(H, G) | |
80 | |
81 | |
82 class TestGridGraph: | |
83 """Unit tests for :func:`networkx.generators.lattice.grid_graph`""" | |
84 | |
85 def test_grid_graph(self): | |
86 """grid_graph([n,m]) is a connected simple graph with the | |
87 following properties: | |
88 number_of_nodes = n*m | |
89 degree_histogram = [0,0,4,2*(n+m)-8,(n-2)*(m-2)] | |
90 """ | |
91 for n, m in [(3, 5), (5, 3), (4, 5), (5, 4)]: | |
92 dim = [n, m] | |
93 g = nx.grid_graph(dim) | |
94 assert len(g) == n * m | |
95 assert nx.degree_histogram(g) == [ | |
96 0, | |
97 0, | |
98 4, | |
99 2 * (n + m) - 8, | |
100 (n - 2) * (m - 2), | |
101 ] | |
102 | |
103 for n, m in [(1, 5), (5, 1)]: | |
104 dim = [n, m] | |
105 g = nx.grid_graph(dim) | |
106 assert len(g) == n * m | |
107 assert nx.is_isomorphic(g, nx.path_graph(5)) | |
108 | |
109 # mg = nx.grid_graph([n,m], create_using=MultiGraph()) | |
110 # assert_equal(mg.edges(), g.edges()) | |
111 | |
112 def test_node_input(self): | |
113 G = nx.grid_graph([range(7, 9), range(3, 6)]) | |
114 assert len(G) == 2 * 3 | |
115 assert nx.is_isomorphic(G, nx.grid_graph([2, 3])) | |
116 | |
117 def test_periodic_iterable(self): | |
118 m, n, k = 3, 7, 5 | |
119 for a, b, c in product([0, 1], [0, 1], [0, 1]): | |
120 G = nx.grid_graph([m, n, k], periodic=(a, b, c)) | |
121 num_e = (m + a - 1) * n * k + (n + b - 1) * m * k + (k + c - 1) * m * n | |
122 assert G.number_of_nodes() == m * n * k | |
123 assert G.number_of_edges() == num_e | |
124 | |
125 | |
126 class TestHypercubeGraph: | |
127 """Unit tests for :func:`networkx.generators.lattice.hypercube_graph`""" | |
128 | |
129 def test_special_cases(self): | |
130 for n, H in [ | |
131 (0, nx.null_graph()), | |
132 (1, nx.path_graph(2)), | |
133 (2, nx.cycle_graph(4)), | |
134 (3, nx.cubical_graph()), | |
135 ]: | |
136 G = nx.hypercube_graph(n) | |
137 assert nx.could_be_isomorphic(G, H) | |
138 | |
139 def test_degree_distribution(self): | |
140 for n in range(1, 10): | |
141 G = nx.hypercube_graph(n) | |
142 expected_histogram = [0] * n + [2 ** n] | |
143 assert nx.degree_histogram(G) == expected_histogram | |
144 | |
145 | |
146 class TestTriangularLatticeGraph: | |
147 "Tests for :func:`networkx.generators.lattice.triangular_lattice_graph`" | |
148 | |
149 def test_lattice_points(self): | |
150 """Tests that the graph is really a triangular lattice.""" | |
151 for m, n in [(2, 3), (2, 2), (2, 1), (3, 3), (3, 2), (3, 4)]: | |
152 G = nx.triangular_lattice_graph(m, n) | |
153 N = (n + 1) // 2 | |
154 assert len(G) == (m + 1) * (1 + N) - (n % 2) * ((m + 1) // 2) | |
155 for (i, j) in G.nodes(): | |
156 nbrs = G[(i, j)] | |
157 if i < N: | |
158 assert (i + 1, j) in nbrs | |
159 if j < m: | |
160 assert (i, j + 1) in nbrs | |
161 if j < m and (i > 0 or j % 2) and (i < N or (j + 1) % 2): | |
162 assert (i + 1, j + 1) in nbrs or (i - 1, j + 1) in nbrs | |
163 | |
164 def test_directed(self): | |
165 """Tests for creating a directed triangular lattice.""" | |
166 G = nx.triangular_lattice_graph(3, 4, create_using=nx.Graph()) | |
167 H = nx.triangular_lattice_graph(3, 4, create_using=nx.DiGraph()) | |
168 assert H.is_directed() | |
169 for u, v in H.edges(): | |
170 assert v[1] >= u[1] | |
171 if v[1] == u[1]: | |
172 assert v[0] > u[0] | |
173 | |
174 def test_multigraph(self): | |
175 """Tests for creating a triangular lattice multigraph.""" | |
176 G = nx.triangular_lattice_graph(3, 4, create_using=nx.Graph()) | |
177 H = nx.triangular_lattice_graph(3, 4, create_using=nx.MultiGraph()) | |
178 assert list(H.edges()) == list(G.edges()) | |
179 | |
180 def test_periodic(self): | |
181 G = nx.triangular_lattice_graph(4, 6, periodic=True) | |
182 assert len(G) == 12 | |
183 assert G.size() == 36 | |
184 # all degrees are 6 | |
185 assert len([n for n, d in G.degree() if d != 6]) == 0 | |
186 G = nx.triangular_lattice_graph(5, 7, periodic=True) | |
187 TLG = nx.triangular_lattice_graph | |
188 pytest.raises(nx.NetworkXError, TLG, 2, 4, periodic=True) | |
189 pytest.raises(nx.NetworkXError, TLG, 4, 4, periodic=True) | |
190 pytest.raises(nx.NetworkXError, TLG, 2, 6, periodic=True) | |
191 | |
192 | |
193 class TestHexagonalLatticeGraph: | |
194 "Tests for :func:`networkx.generators.lattice.hexagonal_lattice_graph`" | |
195 | |
196 def test_lattice_points(self): | |
197 """Tests that the graph is really a hexagonal lattice.""" | |
198 for m, n in [(4, 5), (4, 4), (4, 3), (3, 2), (3, 3), (3, 5)]: | |
199 G = nx.hexagonal_lattice_graph(m, n) | |
200 assert len(G) == 2 * (m + 1) * (n + 1) - 2 | |
201 C_6 = nx.cycle_graph(6) | |
202 hexagons = [ | |
203 [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)], | |
204 [(0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4)], | |
205 [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3)], | |
206 [(2, 0), (2, 1), (2, 2), (3, 0), (3, 1), (3, 2)], | |
207 [(2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4)], | |
208 ] | |
209 for hexagon in hexagons: | |
210 assert nx.is_isomorphic(G.subgraph(hexagon), C_6) | |
211 | |
212 def test_directed(self): | |
213 """Tests for creating a directed hexagonal lattice.""" | |
214 G = nx.hexagonal_lattice_graph(3, 5, create_using=nx.Graph()) | |
215 H = nx.hexagonal_lattice_graph(3, 5, create_using=nx.DiGraph()) | |
216 assert H.is_directed() | |
217 pos = nx.get_node_attributes(H, "pos") | |
218 for u, v in H.edges(): | |
219 assert pos[v][1] >= pos[u][1] | |
220 if pos[v][1] == pos[u][1]: | |
221 assert pos[v][0] > pos[u][0] | |
222 | |
223 def test_multigraph(self): | |
224 """Tests for creating a hexagonal lattice multigraph.""" | |
225 G = nx.hexagonal_lattice_graph(3, 5, create_using=nx.Graph()) | |
226 H = nx.hexagonal_lattice_graph(3, 5, create_using=nx.MultiGraph()) | |
227 assert list(H.edges()) == list(G.edges()) | |
228 | |
229 def test_periodic(self): | |
230 G = nx.hexagonal_lattice_graph(4, 6, periodic=True) | |
231 assert len(G) == 48 | |
232 assert G.size() == 72 | |
233 # all degrees are 3 | |
234 assert len([n for n, d in G.degree() if d != 3]) == 0 | |
235 G = nx.hexagonal_lattice_graph(5, 8, periodic=True) | |
236 HLG = nx.hexagonal_lattice_graph | |
237 pytest.raises(nx.NetworkXError, HLG, 2, 7, periodic=True) | |
238 pytest.raises(nx.NetworkXError, HLG, 1, 4, periodic=True) | |
239 pytest.raises(nx.NetworkXError, HLG, 2, 1, periodic=True) |