Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_community.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 import pytest | |
3 | |
4 | |
5 def test_random_partition_graph(): | |
6 G = nx.random_partition_graph([3, 3, 3], 1, 0, seed=42) | |
7 C = G.graph["partition"] | |
8 assert C == [{0, 1, 2}, {3, 4, 5}, {6, 7, 8}] | |
9 assert len(G) == 9 | |
10 assert len(list(G.edges())) == 9 | |
11 | |
12 G = nx.random_partition_graph([3, 3, 3], 0, 1) | |
13 C = G.graph["partition"] | |
14 assert C == [{0, 1, 2}, {3, 4, 5}, {6, 7, 8}] | |
15 assert len(G) == 9 | |
16 assert len(list(G.edges())) == 27 | |
17 | |
18 G = nx.random_partition_graph([3, 3, 3], 1, 0, directed=True) | |
19 C = G.graph["partition"] | |
20 assert C == [{0, 1, 2}, {3, 4, 5}, {6, 7, 8}] | |
21 assert len(G) == 9 | |
22 assert len(list(G.edges())) == 18 | |
23 | |
24 G = nx.random_partition_graph([3, 3, 3], 0, 1, directed=True) | |
25 C = G.graph["partition"] | |
26 assert C == [{0, 1, 2}, {3, 4, 5}, {6, 7, 8}] | |
27 assert len(G) == 9 | |
28 assert len(list(G.edges())) == 54 | |
29 | |
30 G = nx.random_partition_graph([1, 2, 3, 4, 5], 0.5, 0.1) | |
31 C = G.graph["partition"] | |
32 assert C == [{0}, {1, 2}, {3, 4, 5}, {6, 7, 8, 9}, {10, 11, 12, 13, 14}] | |
33 assert len(G) == 15 | |
34 | |
35 rpg = nx.random_partition_graph | |
36 pytest.raises(nx.NetworkXError, rpg, [1, 2, 3], 1.1, 0.1) | |
37 pytest.raises(nx.NetworkXError, rpg, [1, 2, 3], -0.1, 0.1) | |
38 pytest.raises(nx.NetworkXError, rpg, [1, 2, 3], 0.1, 1.1) | |
39 pytest.raises(nx.NetworkXError, rpg, [1, 2, 3], 0.1, -0.1) | |
40 | |
41 | |
42 def test_planted_partition_graph(): | |
43 G = nx.planted_partition_graph(4, 3, 1, 0, seed=42) | |
44 C = G.graph["partition"] | |
45 assert len(C) == 4 | |
46 assert len(G) == 12 | |
47 assert len(list(G.edges())) == 12 | |
48 | |
49 G = nx.planted_partition_graph(4, 3, 0, 1) | |
50 C = G.graph["partition"] | |
51 assert len(C) == 4 | |
52 assert len(G) == 12 | |
53 assert len(list(G.edges())) == 54 | |
54 | |
55 G = nx.planted_partition_graph(10, 4, 0.5, 0.1, seed=42) | |
56 C = G.graph["partition"] | |
57 assert len(C) == 10 | |
58 assert len(G) == 40 | |
59 | |
60 G = nx.planted_partition_graph(4, 3, 1, 0, directed=True) | |
61 C = G.graph["partition"] | |
62 assert len(C) == 4 | |
63 assert len(G) == 12 | |
64 assert len(list(G.edges())) == 24 | |
65 | |
66 G = nx.planted_partition_graph(4, 3, 0, 1, directed=True) | |
67 C = G.graph["partition"] | |
68 assert len(C) == 4 | |
69 assert len(G) == 12 | |
70 assert len(list(G.edges())) == 108 | |
71 | |
72 G = nx.planted_partition_graph(10, 4, 0.5, 0.1, seed=42, directed=True) | |
73 C = G.graph["partition"] | |
74 assert len(C) == 10 | |
75 assert len(G) == 40 | |
76 | |
77 ppg = nx.planted_partition_graph | |
78 pytest.raises(nx.NetworkXError, ppg, 3, 3, 1.1, 0.1) | |
79 pytest.raises(nx.NetworkXError, ppg, 3, 3, -0.1, 0.1) | |
80 pytest.raises(nx.NetworkXError, ppg, 3, 3, 0.1, 1.1) | |
81 pytest.raises(nx.NetworkXError, ppg, 3, 3, 0.1, -0.1) | |
82 | |
83 | |
84 def test_relaxed_caveman_graph(): | |
85 G = nx.relaxed_caveman_graph(4, 3, 0) | |
86 assert len(G) == 12 | |
87 G = nx.relaxed_caveman_graph(4, 3, 1) | |
88 assert len(G) == 12 | |
89 G = nx.relaxed_caveman_graph(4, 3, 0.5) | |
90 assert len(G) == 12 | |
91 G = nx.relaxed_caveman_graph(4, 3, 0.5, seed=42) | |
92 assert len(G) == 12 | |
93 | |
94 | |
95 def test_connected_caveman_graph(): | |
96 G = nx.connected_caveman_graph(4, 3) | |
97 assert len(G) == 12 | |
98 | |
99 G = nx.connected_caveman_graph(1, 5) | |
100 K5 = nx.complete_graph(5) | |
101 K5.remove_edge(3, 4) | |
102 assert nx.is_isomorphic(G, K5) | |
103 | |
104 # need at least 2 nodes in each clique | |
105 pytest.raises(nx.NetworkXError, nx.connected_caveman_graph, 4, 1) | |
106 | |
107 | |
108 def test_caveman_graph(): | |
109 G = nx.caveman_graph(4, 3) | |
110 assert len(G) == 12 | |
111 | |
112 G = nx.caveman_graph(1, 5) | |
113 K5 = nx.complete_graph(5) | |
114 assert nx.is_isomorphic(G, K5) | |
115 | |
116 | |
117 def test_gaussian_random_partition_graph(): | |
118 G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01) | |
119 assert len(G) == 100 | |
120 G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01, directed=True) | |
121 assert len(G) == 100 | |
122 G = nx.gaussian_random_partition_graph( | |
123 100, 10, 10, 0.3, 0.01, directed=False, seed=42 | |
124 ) | |
125 assert len(G) == 100 | |
126 assert not isinstance(G, nx.DiGraph) | |
127 G = nx.gaussian_random_partition_graph( | |
128 100, 10, 10, 0.3, 0.01, directed=True, seed=42 | |
129 ) | |
130 assert len(G) == 100 | |
131 assert isinstance(G, nx.DiGraph) | |
132 pytest.raises( | |
133 nx.NetworkXError, nx.gaussian_random_partition_graph, 100, 101, 10, 1, 0 | |
134 ) | |
135 | |
136 | |
137 def test_ring_of_cliques(): | |
138 for i in range(2, 20, 3): | |
139 for j in range(2, 20, 3): | |
140 G = nx.ring_of_cliques(i, j) | |
141 assert G.number_of_nodes() == i * j | |
142 if i != 2 or j != 1: | |
143 expected_num_edges = i * (((j * (j - 1)) // 2) + 1) | |
144 else: | |
145 # the edge that already exists cannot be duplicated | |
146 expected_num_edges = i * (((j * (j - 1)) // 2) + 1) - 1 | |
147 assert G.number_of_edges() == expected_num_edges | |
148 pytest.raises(nx.NetworkXError, nx.ring_of_cliques, 1, 5) | |
149 pytest.raises(nx.NetworkXError, nx.ring_of_cliques, 3, 0) | |
150 | |
151 | |
152 def test_windmill_graph(): | |
153 for n in range(2, 20, 3): | |
154 for k in range(2, 20, 3): | |
155 G = nx.windmill_graph(n, k) | |
156 assert G.number_of_nodes() == (k - 1) * n + 1 | |
157 assert G.number_of_edges() == n * k * (k - 1) / 2 | |
158 assert G.degree(0) == G.number_of_nodes() - 1 | |
159 for i in range(1, G.number_of_nodes()): | |
160 assert G.degree(i) == k - 1 | |
161 pytest.raises(nx.NetworkXError, nx.ring_of_cliques, 1, 3) | |
162 pytest.raises(nx.NetworkXError, nx.ring_of_cliques, 15, 0) | |
163 | |
164 | |
165 def test_stochastic_block_model(): | |
166 sizes = [75, 75, 300] | |
167 probs = [[0.25, 0.05, 0.02], [0.05, 0.35, 0.07], [0.02, 0.07, 0.40]] | |
168 G = nx.stochastic_block_model(sizes, probs, seed=0) | |
169 C = G.graph["partition"] | |
170 assert len(C) == 3 | |
171 assert len(G) == 450 | |
172 assert G.size() == 22160 | |
173 | |
174 GG = nx.stochastic_block_model(sizes, probs, range(450), seed=0) | |
175 assert G.nodes == GG.nodes | |
176 | |
177 # Test Exceptions | |
178 sbm = nx.stochastic_block_model | |
179 badnodelist = list(range(400)) # not enough nodes to match sizes | |
180 badprobs1 = [[0.25, 0.05, 1.02], [0.05, 0.35, 0.07], [0.02, 0.07, 0.40]] | |
181 badprobs2 = [[0.25, 0.05, 0.02], [0.05, -0.35, 0.07], [0.02, 0.07, 0.40]] | |
182 probs_rect1 = [[0.25, 0.05, 0.02], [0.05, -0.35, 0.07]] | |
183 probs_rect2 = [[0.25, 0.05], [0.05, -0.35], [0.02, 0.07]] | |
184 asymprobs = [[0.25, 0.05, 0.01], [0.05, -0.35, 0.07], [0.02, 0.07, 0.40]] | |
185 pytest.raises(nx.NetworkXException, sbm, sizes, badprobs1) | |
186 pytest.raises(nx.NetworkXException, sbm, sizes, badprobs2) | |
187 pytest.raises(nx.NetworkXException, sbm, sizes, probs_rect1, directed=True) | |
188 pytest.raises(nx.NetworkXException, sbm, sizes, probs_rect2, directed=True) | |
189 pytest.raises(nx.NetworkXException, sbm, sizes, asymprobs, directed=False) | |
190 pytest.raises(nx.NetworkXException, sbm, sizes, probs, badnodelist) | |
191 nodelist = [0] + list(range(449)) # repeated node name in nodelist | |
192 pytest.raises(nx.NetworkXException, sbm, sizes, probs, nodelist) | |
193 | |
194 # Extra keyword arguments test | |
195 GG = nx.stochastic_block_model(sizes, probs, seed=0, selfloops=True) | |
196 assert G.nodes == GG.nodes | |
197 GG = nx.stochastic_block_model(sizes, probs, selfloops=True, directed=True) | |
198 assert G.nodes == GG.nodes | |
199 GG = nx.stochastic_block_model(sizes, probs, seed=0, sparse=False) | |
200 assert G.nodes == GG.nodes | |
201 | |
202 | |
203 def test_generator(): | |
204 n = 250 | |
205 tau1 = 3 | |
206 tau2 = 1.5 | |
207 mu = 0.1 | |
208 G = nx.LFR_benchmark_graph( | |
209 n, tau1, tau2, mu, average_degree=5, min_community=20, seed=10 | |
210 ) | |
211 assert len(G) == 250 | |
212 C = {frozenset(G.nodes[v]["community"]) for v in G} | |
213 assert nx.community.is_partition(G.nodes(), C) | |
214 | |
215 | |
216 def test_invalid_tau1(): | |
217 with pytest.raises(nx.NetworkXError): | |
218 n = 100 | |
219 tau1 = 2 | |
220 tau2 = 1 | |
221 mu = 0.1 | |
222 nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2) | |
223 | |
224 | |
225 def test_invalid_tau2(): | |
226 with pytest.raises(nx.NetworkXError): | |
227 n = 100 | |
228 tau1 = 1 | |
229 tau2 = 2 | |
230 mu = 0.1 | |
231 nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2) | |
232 | |
233 | |
234 def test_mu_too_large(): | |
235 with pytest.raises(nx.NetworkXError): | |
236 n = 100 | |
237 tau1 = 2 | |
238 tau2 = 2 | |
239 mu = 1.1 | |
240 nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2) | |
241 | |
242 | |
243 def test_mu_too_small(): | |
244 with pytest.raises(nx.NetworkXError): | |
245 n = 100 | |
246 tau1 = 2 | |
247 tau2 = 2 | |
248 mu = -1 | |
249 nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2) | |
250 | |
251 | |
252 def test_both_degrees_none(): | |
253 with pytest.raises(nx.NetworkXError): | |
254 n = 100 | |
255 tau1 = 2 | |
256 tau2 = 2 | |
257 mu = -1 | |
258 nx.LFR_benchmark_graph(n, tau1, tau2, mu) | |
259 | |
260 | |
261 def test_neither_degrees_none(): | |
262 with pytest.raises(nx.NetworkXError): | |
263 n = 100 | |
264 tau1 = 2 | |
265 tau2 = 2 | |
266 mu = -1 | |
267 nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2, average_degree=5) |