ioftools / networkxMiCe / networkxmaster / networkx / generators / tests / test_community.py @ 5cef0f13
History  View  Annotate  Download (8.77 KB)
1 
import networkx as nx 

2 
from nose.tools import * 
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_equal(C, [set([0, 1, 2]), set([3, 4, 5]), set([6, 7, 8])]) 
9 
assert_equal(len(G), 9) 
10 
assert_equal(len(list(G.edges())), 9) 
11  
12 
G = nx.random_partition_graph([3, 3, 3], 0, 1) 
13 
C = G.graph['partition']

14 
assert_equal(C, [set([0, 1, 2]), set([3, 4, 5]), set([6, 7, 8])]) 
15 
assert_equal(len(G), 9) 
16 
assert_equal(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_equal(C, [set([0, 1, 2]), set([3, 4, 5]), set([6, 7, 8])]) 
21 
assert_equal(len(G), 9) 
22 
assert_equal(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_equal(C, [set([0, 1, 2]), set([3, 4, 5]), set([6, 7, 8])]) 
27 
assert_equal(len(G), 9) 
28 
assert_equal(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_equal(C, [set([0]), set([1, 2]), set([3, 4, 5]), 
33 
set([6, 7, 8, 9]), set([10, 11, 12, 13, 14])]) 
34 
assert_equal(len(G), 15) 
35  
36 
rpg = nx.random_partition_graph 
37 
assert_raises(nx.NetworkXError, rpg, [1, 2, 3], 1.1, 0.1) 
38 
assert_raises(nx.NetworkXError, rpg, [1, 2, 3], 0.1, 0.1) 
39 
assert_raises(nx.NetworkXError, rpg, [1, 2, 3], 0.1, 1.1) 
40 
assert_raises(nx.NetworkXError, rpg, [1, 2, 3], 0.1, 0.1) 
41  
42  
43 
def test_planted_partition_graph(): 
44 
G = nx.planted_partition_graph(4, 3, 1, 0, seed=42) 
45 
C = G.graph['partition']

46 
assert_equal(len(C), 4) 
47 
assert_equal(len(G), 12) 
48 
assert_equal(len(list(G.edges())), 12) 
49  
50 
G = nx.planted_partition_graph(4, 3, 0, 1) 
51 
C = G.graph['partition']

52 
assert_equal(len(C), 4) 
53 
assert_equal(len(G), 12) 
54 
assert_equal(len(list(G.edges())), 54) 
55  
56 
G = nx.planted_partition_graph(10, 4, .5, .1, seed=42) 
57 
C = G.graph['partition']

58 
assert_equal(len(C), 10) 
59 
assert_equal(len(G), 40) 
60  
61 
G = nx.planted_partition_graph(4, 3, 1, 0, directed=True) 
62 
C = G.graph['partition']

63 
assert_equal(len(C), 4) 
64 
assert_equal(len(G), 12) 
65 
assert_equal(len(list(G.edges())), 24) 
66  
67 
G = nx.planted_partition_graph(4, 3, 0, 1, directed=True) 
68 
C = G.graph['partition']

69 
assert_equal(len(C), 4) 
70 
assert_equal(len(G), 12) 
71 
assert_equal(len(list(G.edges())), 108) 
72  
73 
G = nx.planted_partition_graph(10, 4, .5, .1, seed=42, directed=True) 
74 
C = G.graph['partition']

75 
assert_equal(len(C), 10) 
76 
assert_equal(len(G), 40) 
77  
78 
ppg = nx.planted_partition_graph 
79 
assert_raises(nx.NetworkXError, ppg, 3, 3, 1.1, 0.1) 
80 
assert_raises(nx.NetworkXError, ppg, 3, 3, 0.1, 0.1) 
81 
assert_raises(nx.NetworkXError, ppg, 3, 3, 0.1, 1.1) 
82 
assert_raises(nx.NetworkXError, ppg, 3, 3, 0.1, 0.1) 
83  
84  
85 
def test_relaxed_caveman_graph(): 
86 
G = nx.relaxed_caveman_graph(4, 3, 0) 
87 
assert_equal(len(G), 12) 
88 
G = nx.relaxed_caveman_graph(4, 3, 1) 
89 
assert_equal(len(G), 12) 
90 
G = nx.relaxed_caveman_graph(4, 3, 0.5) 
91 
assert_equal(len(G), 12) 
92 
G = nx.relaxed_caveman_graph(4, 3, 0.5, seed=42) 
93 
assert_equal(len(G), 12) 
94  
95  
96 
def test_connected_caveman_graph(): 
97 
G = nx.connected_caveman_graph(4, 3) 
98 
assert_equal(len(G), 12) 
99  
100 
G = nx.connected_caveman_graph(1, 5) 
101 
K5 = nx.complete_graph(5)

102 
K5.remove_edge(3, 4) 
103 
assert_true(nx.is_isomorphic(G, K5)) 
104  
105  
106 
def test_caveman_graph(): 
107 
G = nx.caveman_graph(4, 3) 
108 
assert_equal(len(G), 12) 
109  
110 
G = nx.caveman_graph(1, 5) 
111 
K5 = nx.complete_graph(5)

112 
assert_true(nx.is_isomorphic(G, K5)) 
113  
114  
115 
def test_gaussian_random_partition_graph(): 
116 
G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01) 
117 
assert_equal(len(G), 100) 
118 
G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01, 
119 
directed=True)

120 
assert_equal(len(G), 100) 
121 
G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01, 
122 
directed=False, seed=42) 
123 
assert_equal(len(G), 100) 
124 
G = nx.gaussian_random_partition_graph(100, 10, 10, 0.3, 0.01, 
125 
directed=True, seed=42) 
126 
assert_equal(len(G), 100) 
127 
assert_raises(nx.NetworkXError, 
128 
nx.gaussian_random_partition_graph, 100, 101, 10, 1, 0) 
129  
130  
131 
def test_ring_of_cliques(): 
132 
for i in range(2, 20, 3): 
133 
for j in range(2, 20, 3): 
134 
G = nx.ring_of_cliques(i, j) 
135 
assert_equal(G.number_of_nodes(), i * j) 
136 
if i != 2 or j != 1: 
137 
expected_num_edges = i * (((j * (j  1)) // 2) + 1) 
138 
else:

139 
# the edge that already exists cannot be duplicated

140 
expected_num_edges = i * (((j * (j  1)) // 2) + 1)  1 
141 
assert_equal(G.number_of_edges(), expected_num_edges) 
142 
assert_raises(nx.NetworkXError, nx.ring_of_cliques, 1, 5) 
143 
assert_raises(nx.NetworkXError, nx.ring_of_cliques, 3, 0) 
144  
145  
146 
def test_windmill_graph(): 
147 
for n in range(2, 20, 3): 
148 
for k in range(2, 20, 3): 
149 
G = nx.windmill_graph(n, k) 
150 
assert_equal(G.number_of_nodes(), (k  1) * n + 1) 
151 
assert_equal(G.number_of_edges(), n * k * (k  1) / 2) 
152 
assert_equal(G.degree(0), G.number_of_nodes()  1) 
153 
for i in range(1, G.number_of_nodes()): 
154 
assert_equal(G.degree(i), k  1)

155 
assert_raises(nx.NetworkXError, nx.ring_of_cliques, 1, 3) 
156 
assert_raises(nx.NetworkXError, nx.ring_of_cliques, 15, 0) 
157  
158  
159 
def test_stochastic_block_model(): 
160 
sizes = [75, 75, 300] 
161 
probs = [[0.25, 0.05, 0.02], 
162 
[0.05, 0.35, 0.07], 
163 
[0.02, 0.07, 0.40]] 
164 
G = nx.stochastic_block_model(sizes, probs, seed=0)

165 
C = G.graph['partition']

166 
assert_equal(len(C), 3) 
167 
assert_equal(len(G), 450) 
168 
assert_equal(G.size(), 22160)

169  
170 
GG = nx.stochastic_block_model(sizes, probs, range(450), seed=0) 
171 
assert_equal(G.nodes, GG.nodes) 
172  
173 
# Test Exceptions

174 
sbm = nx.stochastic_block_model 
175 
badnodelist = list(range(400)) # not enough nodes to match sizes 
176 
badprobs1 = [[0.25, 0.05, 1.02], 
177 
[0.05, 0.35, 0.07], 
178 
[0.02, 0.07, 0.40]] 
179 
badprobs2 = [[0.25, 0.05, 0.02], 
180 
[0.05, 0.35, 0.07], 
181 
[0.02, 0.07, 0.40]] 
182 
probs_rect1 = [[0.25, 0.05, 0.02], 
183 
[0.05, 0.35, 0.07]] 
184 
probs_rect2 = [[0.25, 0.05], 
185 
[0.05, 0.35], 
186 
[0.02, 0.07]] 
187 
asymprobs = [[0.25, 0.05, 0.01], 
188 
[0.05, 0.35, 0.07], 
189 
[0.02, 0.07, 0.40]] 
190 
assert_raises(nx.NetworkXException, sbm, sizes, badprobs1) 
191 
assert_raises(nx.NetworkXException, sbm, sizes, badprobs2) 
192 
assert_raises(nx.NetworkXException, sbm, sizes, probs_rect1, directed=True)

193 
assert_raises(nx.NetworkXException, sbm, sizes, probs_rect2, directed=True)

194 
assert_raises(nx.NetworkXException, sbm, sizes, asymprobs, directed=False)

195 
assert_raises(nx.NetworkXException, sbm, sizes, probs, badnodelist) 
196 
nodelist = [0] + list(range(449)) # repeated node name in nodelist 
197 
assert_raises(nx.NetworkXException, sbm, sizes, probs, nodelist) 
198  
199 
# Extra keyword arguments test

200 
GG = nx.stochastic_block_model(sizes, probs, seed=0, selfloops=True) 
201 
assert_equal(G.nodes, GG.nodes) 
202 
GG = nx.stochastic_block_model(sizes, probs, selfloops=True, directed=True) 
203 
assert_equal(G.nodes, GG.nodes) 
204 
GG = nx.stochastic_block_model(sizes, probs, seed=0, sparse=False) 
205 
assert_equal(G.nodes, GG.nodes) 
206  
207  
208 
def test_generator(): 
209 
n = 250

210 
tau1 = 3

211 
tau2 = 1.5

212 
mu = 0.1

213 
G = nx.LFR_benchmark_graph(n, tau1, tau2, mu, average_degree=5,

214 
min_community=20, seed=10) 
215 
assert_equal(len(G), 250) 
216 
C = {frozenset(G.nodes[v]['community']) for v in G} 
217 
assert_true(nx.community.is_partition(G.nodes(), C)) 
218  
219  
220 
@raises(nx.NetworkXError)

221 
def test_invalid_tau1(): 
222 
n = 100

223 
tau1 = 2

224 
tau2 = 1

225 
mu = 0.1

226 
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)

227  
228  
229 
@raises(nx.NetworkXError)

230 
def test_invalid_tau2(): 
231 
n = 100

232 
tau1 = 1

233 
tau2 = 2

234 
mu = 0.1

235 
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)

236  
237  
238 
@raises(nx.NetworkXError)

239 
def test_mu_too_large(): 
240 
n = 100

241 
tau1 = 2

242 
tau2 = 2

243 
mu = 1.1

244 
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)

245  
246  
247 
@raises(nx.NetworkXError)

248 
def test_mu_too_small(): 
249 
n = 100

250 
tau1 = 2

251 
tau2 = 2

252 
mu = 1

253 
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2)

254  
255  
256 
@raises(nx.NetworkXError)

257 
def test_both_degrees_none(): 
258 
n = 100

259 
tau1 = 2

260 
tau2 = 2

261 
mu = 1

262 
nx.LFR_benchmark_graph(n, tau1, tau2, mu) 
263  
264  
265 
@raises(nx.NetworkXError)

266 
def test_neither_degrees_none(): 
267 
n = 100

268 
tau1 = 2

269 
tau2 = 2

270 
mu = 1

271 
nx.LFR_benchmark_graph(n, tau1, tau2, mu, min_degree=2, average_degree=5) 