### comparison env/lib/python3.9/site-packages/networkx/tests/test_all_random_functions.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
comparison
equal inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import pytest
2
3 np = pytest.importorskip("numpy")
4 import random
5
6 import networkx as nx
7 from networkx.algorithms import approximation as approx
8 from networkx.algorithms import threshold
9
10 progress = 0
11
12 # store the random numbers after setting a global seed
13 np.random.seed(42)
14 np_rv = np.random.rand()
15 random.seed(42)
16 py_rv = random.random()
17
18
19 def t(f, *args, **kwds):
20 """call one function and check if global RNG changed"""
21 global progress
22 progress += 1
23 print(progress, ",", end="")
24
25 f(*args, **kwds)
26
27 after_np_rv = np.random.rand()
28 # if np_rv != after_np_rv:
29 # print(np_rv, after_np_rv, "don't match np!")
30 assert np_rv == after_np_rv
31 np.random.seed(42)
32
33 after_py_rv = random.random()
34 # if py_rv != after_py_rv:
35 # print(py_rv, after_py_rv, "don't match py!")
36 assert py_rv == after_py_rv
37 random.seed(42)
38
39
40 def run_all_random_functions(seed):
41 n = 20
42 m = 10
43 k = l = 2
44 s = v = 10
45 p = q = p1 = p2 = p_in = p_out = 0.4
46 alpha = radius = theta = 0.75
47 sizes = (20, 20, 10)
48 colors = [1, 2, 3]
49 G = nx.barbell_graph(12, 20)
50 deg_sequence = [3, 2, 1, 3, 2, 1, 3, 2, 1, 2, 1, 2, 1]
51 in_degree_sequence = w = sequence = aseq = bseq = deg_sequence
52
53 # print("starting...")
54 t(nx.maximal_independent_set, G, seed=seed)
55 t(nx.rich_club_coefficient, G, seed=seed, normalized=False)
56 t(nx.random_reference, G, seed=seed)
57 t(nx.lattice_reference, G, seed=seed)
58 t(nx.sigma, G, 1, 2, seed=seed)
59 t(nx.omega, G, 1, 2, seed=seed)
60 # print("out of smallworld.py")
61 t(nx.double_edge_swap, G, seed=seed)
62 # print("starting connected_double_edge_swap")
63 t(nx.connected_double_edge_swap, nx.complete_graph(9), seed=seed)
64 # print("ending connected_double_edge_swap")
65 t(nx.random_layout, G, seed=seed)
66 t(nx.fruchterman_reingold_layout, G, seed=seed)
67 t(nx.algebraic_connectivity, G, seed=seed)
68 t(nx.fiedler_vector, G, seed=seed)
69 t(nx.spectral_ordering, G, seed=seed)
70 # print('starting average_clustering')
71 t(approx.average_clustering, G, seed=seed)
72 t(nx.betweenness_centrality, G, seed=seed)
73 t(nx.edge_betweenness_centrality, G, seed=seed)
74 t(nx.edge_betweenness, G, seed=seed)
75 t(nx.approximate_current_flow_betweenness_centrality, G, seed=seed)
76 # print("kernighan")
77 t(nx.algorithms.community.kernighan_lin_bisection, G, seed=seed)
78 # nx.algorithms.community.asyn_lpa_communities(G, seed=seed)
79 t(nx.algorithms.tree.greedy_branching, G, seed=seed)
80 t(nx.algorithms.tree.Edmonds, G, seed=seed)
81 # print('done with graph argument functions')
82
83 t(nx.spectral_graph_forge, G, alpha, seed=seed)
84 t(nx.algorithms.community.asyn_fluidc, G, k, max_iter=1, seed=seed)
85 t(
86 nx.algorithms.connectivity.edge_augmentation.greedy_k_edge_augmentation,
87 G,
88 k,
89 seed=seed,
90 )
91 t(nx.algorithms.coloring.strategy_random_sequential, G, colors, seed=seed)
92
93 cs = ["d", "i", "i", "d", "d", "i"]
94 t(threshold.swap_d, cs, seed=seed)
95 t(nx.configuration_model, deg_sequence, seed=seed)
96 t(
97 nx.directed_configuration_model,
98 in_degree_sequence,
99 in_degree_sequence,
100 seed=seed,
101 )
102 t(nx.expected_degree_graph, w, seed=seed)
103 t(nx.random_degree_sequence_graph, sequence, seed=seed)
104 joint_degrees = {
105 1: {4: 1},
106 2: {2: 2, 3: 2, 4: 2},
107 3: {2: 2, 4: 1},
108 4: {1: 1, 2: 2, 3: 1},
109 }
110 t(nx.joint_degree_graph, joint_degrees, seed=seed)
111 joint_degree_sequence = [
112 (1, 0),
113 (1, 0),
114 (1, 0),
115 (2, 0),
116 (1, 0),
117 (2, 1),
118 (0, 1),
119 (0, 1),
120 ]
121 t(nx.random_clustered_graph, joint_degree_sequence, seed=seed)
122 constructor = [(3, 3, 0.5), (10, 10, 0.7)]
123 t(nx.random_shell_graph, constructor, seed=seed)
124 mapping = {1: 0.4, 2: 0.3, 3: 0.3}
125 t(nx.utils.random_weighted_sample, mapping, k, seed=seed)
126 t(nx.utils.weighted_choice, mapping, seed=seed)
127 t(nx.algorithms.bipartite.configuration_model, aseq, bseq, seed=seed)
128 t(nx.algorithms.bipartite.preferential_attachment_graph, aseq, p, seed=seed)
129
130 def kernel_integral(u, w, z):
131 return z - w
132
133 t(nx.random_kernel_graph, n, kernel_integral, seed=seed)
134
135 sizes = [75, 75, 300]
136 probs = [[0.25, 0.05, 0.02], [0.05, 0.35, 0.07], [0.02, 0.07, 0.40]]
137 t(nx.stochastic_block_model, sizes, probs, seed=seed)
138 t(nx.random_partition_graph, sizes, p_in, p_out, seed=seed)
139
140 # print("starting generator functions")
141 t(threshold.random_threshold_sequence, n, p, seed=seed)
142 t(nx.tournament.random_tournament, n, seed=seed)
143 t(nx.relaxed_caveman_graph, l, k, p, seed=seed)
144 t(nx.planted_partition_graph, l, k, p_in, p_out, seed=seed)
145 t(nx.gaussian_random_partition_graph, n, s, v, p_in, p_out, seed=seed)
146 t(nx.gn_graph, n, seed=seed)
147 t(nx.gnr_graph, n, p, seed=seed)
148 t(nx.gnc_graph, n, seed=seed)
149 t(nx.scale_free_graph, n, seed=seed)
150 t(nx.directed.random_uniform_k_out_graph, n, k, seed=seed)
151 t(nx.random_k_out_graph, n, k, alpha, seed=seed)
152 N = 1000
153 t(nx.partial_duplication_graph, N, n, p, q, seed=seed)
154 t(nx.duplication_divergence_graph, n, p, seed=seed)
157 t(nx.geographical_threshold_graph, n, theta, seed=seed)
158 t(nx.waxman_graph, n, seed=seed)
159 t(nx.navigable_small_world_graph, n, seed=seed)
160 t(nx.thresholded_random_geometric_graph, n, radius, theta, seed=seed)
161 t(nx.uniform_random_intersection_graph, n, m, p, seed=seed)
162 t(nx.k_random_intersection_graph, n, m, k, seed=seed)
163
164 t(nx.general_random_intersection_graph, n, 2, [0.1, 0.5], seed=seed)
165 t(nx.fast_gnp_random_graph, n, p, seed=seed)
166 t(nx.gnp_random_graph, n, p, seed=seed)
167 t(nx.dense_gnm_random_graph, n, m, seed=seed)
168 t(nx.gnm_random_graph, n, m, seed=seed)
169 t(nx.newman_watts_strogatz_graph, n, k, p, seed=seed)
170 t(nx.watts_strogatz_graph, n, k, p, seed=seed)
171 t(nx.connected_watts_strogatz_graph, n, k, p, seed=seed)
172 t(nx.random_regular_graph, 3, n, seed=seed)
173 t(nx.barabasi_albert_graph, n, m, seed=seed)
174 t(nx.extended_barabasi_albert_graph, n, m, p, q, seed=seed)
175 t(nx.powerlaw_cluster_graph, n, m, p, seed=seed)
176 t(nx.random_lobster, n, p1, p2, seed=seed)
177 t(nx.random_powerlaw_tree, n, seed=seed, tries=5000)
178 t(nx.random_powerlaw_tree_sequence, 10, seed=seed, tries=5000)
179 t(nx.random_tree, n, seed=seed)
180 t(nx.utils.powerlaw_sequence, n, seed=seed)
181 t(nx.utils.zipf_rv, 2.3, seed=seed)
182 cdist = [0.2, 0.4, 0.5, 0.7, 0.9, 1.0]
183 t(nx.utils.discrete_sequence, n, cdistribution=cdist, seed=seed)
184 t(nx.algorithms.bipartite.random_graph, n, m, p, seed=seed)
185 t(nx.algorithms.bipartite.gnmk_random_graph, n, m, k, seed=seed)
186 LFR = nx.generators.LFR_benchmark_graph
187 t(
188 LFR,
189 25,
190 3,
191 1.5,
192 0.1,
193 average_degree=3,
194 min_community=10,
195 seed=seed,
196 max_community=20,
197 )
198 t(nx.random_internet_as_graph, n, seed=seed)
199 # print("done")
200
201
202 # choose to test an integer seed, or whether a single RNG can be everywhere
203 # np_rng = np.random.RandomState(14)
204 # seed = np_rng
205 # seed = 14
206
207
208 @pytest.mark.slow
209 # print("NetworkX Version:", nx.__version__)
210 def test_rng_interface():
211 global progress
212
213 # try different kinds of seeds
214 for seed in [14, np.random.RandomState(14)]:
215 np.random.seed(42)
216 random.seed(42)
217 run_all_random_functions(seed)
218 progress = 0
219
220 # check that both global RNGs are unaffected
221 after_np_rv = np.random.rand()
222 # if np_rv != after_np_rv:
223 # print(np_rv, after_np_rv, "don't match np!")
224 assert np_rv == after_np_rv
225 after_py_rv = random.random()
226 # if py_rv != after_py_rv:
227 # print(py_rv, after_py_rv, "don't match py!")
228 assert py_rv == after_py_rv
229
230
231 # print("\nDone testing seed:", seed)
232
233 # test_rng_interface()