Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / tests / test_all_random_functions.py @ 5cef0f13

History | View | Annotate | Download (7.84 KB)

1
from __future__ import print_function
2
import random
3
from nose import SkipTest
4
from nose.tools import assert_equal
5

    
6
try:
7
    import numpy as np
8
except ImportError:
9
    raise SkipTest('Numpy not available')
10

    
11
import networkx as nx
12
from networkx.algorithms import approximation as approx
13
from networkx.algorithms import threshold
14

    
15
progress = 0
16

    
17
# store the random numbers after setting a global seed
18
np.random.seed(42)
19
np_rv = np.random.rand()
20
random.seed(42)
21
py_rv = random.random()
22

    
23

    
24
def t(f, *args, **kwds):
25
    """call one function and check if global RNG changed""" 
26
    global progress
27
    progress += 1
28
    print(progress, ",", end="")
29

    
30
    f(*args, **kwds)
31

    
32
    after_np_rv = np.random.rand()
33
    # if np_rv != after_np_rv:
34
    #    print(np_rv, after_np_rv, "don't match np!")
35
    assert_equal(np_rv, after_np_rv)
36
    np.random.seed(42)
37

    
38
    after_py_rv = random.random()
39
    # if py_rv != after_py_rv:
40
    #    print(py_rv, after_py_rv, "don't match py!")
41
    assert_equal(py_rv, after_py_rv)
42
    random.seed(42)
43

    
44

    
45
def run_all_random_functions(seed):
46
    n = 20
47
    m = 10
48
    k = l = 2
49
    s = v = 10
50
    p = q = p1 = p2 = p_in = p_out = 0.4
51
    alpha = radius = theta = 0.75
52
    sizes = (20, 20, 10)
53
    colors = [1, 2, 3]
54
    G = nx.barbell_graph(12, 20)
55
    deg_sequence = in_degree_sequence = w = sequence = aseq = bseq = \
56
        [3, 2, 1, 3, 2, 1, 3, 2, 1, 2, 1, 2, 1]
57

    
58
    # print("starting...")
59
    t(nx.maximal_independent_set, G, seed=seed)
60
    t(nx.rich_club_coefficient, G, seed=seed, normalized=False)
61
    t(nx.random_reference, G, seed=seed)
62
    t(nx.lattice_reference, G, seed=seed)
63
    t(nx.sigma, G, 1, 2, seed=seed)
64
    t(nx.omega, G, 1, 2, seed=seed)
65
    # print("out of smallworld.py")
66
    t(nx.double_edge_swap, G, seed=seed)
67
    # print("starting connected_double_edge_swap")
68
    t(nx.connected_double_edge_swap, nx.complete_graph(9), seed=seed)
69
    # print("ending connected_double_edge_swap")
70
    t(nx.random_layout, G, seed=seed)
71
    t(nx.fruchterman_reingold_layout, G, seed=seed)
72
    t(nx.algebraic_connectivity, G, seed=seed)
73
    t(nx.fiedler_vector, G, seed=seed)
74
    t(nx.spectral_ordering, G, seed=seed)
75
    # print('starting average_clustering')
76
    t(approx.average_clustering, G, seed=seed)
77
    t(nx.betweenness_centrality, G, seed=seed)
78
    t(nx.edge_betweenness_centrality, G, seed=seed)
79
    t(nx.edge_betweenness, G, seed=seed)
80
    t(nx.approximate_current_flow_betweenness_centrality, G, seed=seed)
81
    # print("kernighan")
82
    t(nx.algorithms.community.kernighan_lin_bisection, G, seed=seed)
83
    # nx.algorithms.community.asyn_lpa_communities(G, seed=seed)
84
    t(nx.algorithms.tree.greedy_branching, G, seed=seed)
85
    t(nx.algorithms.tree.Edmonds, G, seed=seed)
86
    # print('done with graph argument functions')
87

    
88
    t(nx.spectral_graph_forge, G, alpha, seed=seed)
89
    t(nx.algorithms.community.asyn_fluidc, G, k, max_iter=1, seed=seed)
90
    t(nx.algorithms.connectivity.edge_augmentation.greedy_k_edge_augmentation,
91
      G, k, seed=seed)
92
    t(nx.algorithms.coloring.strategy_random_sequential, G, colors, seed=seed)
93

    
94
    cs = ['d', 'i', 'i', 'd', 'd', 'i']
95
    t(threshold.swap_d, cs, seed=seed)
96
    t(nx.configuration_model, deg_sequence, seed=seed)
97
    t(nx.directed_configuration_model,
98
        in_degree_sequence, in_degree_sequence, seed=seed)
99
    t(nx.expected_degree_graph, w, seed=seed)
100
    t(nx.random_degree_sequence_graph, sequence, seed=seed)
101
    joint_degrees = {1: {4: 1},
102
                     2: {2: 2, 3: 2, 4: 2},
103
                     3: {2: 2, 4: 1},
104
                     4: {1: 1, 2: 2, 3: 1}}
105
    t(nx.joint_degree_graph, joint_degrees, seed=seed)
106
    joint_degree_sequence = [(1, 0), (1, 0), (1, 0), (2, 0), (1, 0), (2, 1),
107
                             (0, 1), (0, 1)]
108
    t(nx.random_clustered_graph, joint_degree_sequence, seed=seed)
109
    constructor = [(3, 3, .5), (10, 10, .7)]
110
    t(nx.random_shell_graph, constructor, seed=seed)
111
    mapping = {1: 0.4, 2: 0.3, 3: 0.3}
112
    t(nx.utils.random_weighted_sample, mapping, k, seed=seed)
113
    t(nx.utils.weighted_choice, mapping, seed=seed)
114
    t(nx.algorithms.bipartite.configuration_model, aseq, bseq, seed=seed)
115
    t(nx.algorithms.bipartite.preferential_attachment_graph,
116
      aseq, p, seed=seed)
117

    
118
    def kernel_integral(u, w, z):
119
        return (z - w)
120

    
121
    t(nx.random_kernel_graph, n, kernel_integral, seed=seed)
122

    
123
    sizes = [75, 75, 300]
124
    probs = [[0.25, 0.05, 0.02],
125
             [0.05, 0.35, 0.07],
126
             [0.02, 0.07, 0.40]]
127
    t(nx.stochastic_block_model, sizes, probs, seed=seed)
128
    t(nx.random_partition_graph, sizes, p_in, p_out, seed=seed)
129

    
130
    # print("starting generator functions")
131
    t(threshold.random_threshold_sequence, n, p, seed=seed)
132
    t(nx.tournament.random_tournament, n, seed=seed)
133
    t(nx.relaxed_caveman_graph, l, k, p, seed=seed)
134
    t(nx.planted_partition_graph, l, k, p_in, p_out, seed=seed)
135
    t(nx.gaussian_random_partition_graph, n, s, v, p_in, p_out, seed=seed)
136
    t(nx.gn_graph, n, seed=seed)
137
    t(nx.gnr_graph, n, p, seed=seed)
138
    t(nx.gnc_graph, n, seed=seed)
139
    t(nx.scale_free_graph, n, seed=seed)
140
    t(nx.directed.random_uniform_k_out_graph, n, k, seed=seed)
141
    t(nx.random_k_out_graph, n, k, alpha, seed=seed)
142
    N = 1000
143
    t(nx.partial_duplication_graph, N, n, p, q, seed=seed)
144
    t(nx.duplication_divergence_graph, n, p, seed=seed)
145
    t(nx.random_geometric_graph, n, radius, seed=seed)
146
    t(nx.soft_random_geometric_graph, n, radius, seed=seed)
147
    t(nx.geographical_threshold_graph, n, theta, seed=seed)
148
    t(nx.waxman_graph, n, seed=seed)
149
    t(nx.navigable_small_world_graph, n, seed=seed)
150
    t(nx.thresholded_random_geometric_graph, n, radius, theta, seed=seed)
151
    t(nx.uniform_random_intersection_graph, n, m, p, seed=seed)
152
    t(nx.k_random_intersection_graph, n, m, k, seed=seed)
153

    
154
    t(nx.general_random_intersection_graph, n, 2, [0.1, 0.5], seed=seed)
155
    t(nx.fast_gnp_random_graph, n, p, seed=seed)
156
    t(nx.gnp_random_graph, n, p, seed=seed)
157
    t(nx.dense_gnm_random_graph, n, m, seed=seed)
158
    t(nx.gnm_random_graph, n, m, seed=seed)
159
    t(nx.newman_watts_strogatz_graph, n, k, p, seed=seed)
160
    t(nx.watts_strogatz_graph, n, k, p, seed=seed)
161
    t(nx.connected_watts_strogatz_graph, n, k, p, seed=seed)
162
    t(nx.random_regular_graph, 3, n, seed=seed)
163
    t(nx.barabasi_albert_graph, n, m, seed=seed)
164
    t(nx.extended_barabasi_albert_graph, n, m, p, q, seed=seed)
165
    t(nx.powerlaw_cluster_graph, n, m, p, seed=seed)
166
    t(nx.random_lobster, n, p1, p2, seed=seed)
167
    t(nx.random_powerlaw_tree, n, seed=seed, tries=5000)
168
    t(nx.random_powerlaw_tree_sequence, 10, seed=seed, tries=5000)
169
    t(nx.random_tree, n, seed=seed)
170
    t(nx.utils.powerlaw_sequence, n, seed=seed)
171
    t(nx.utils.zipf_rv, 2.3, seed=seed)
172
    cdist = [.2, .4, .5, .7, .9, 1.0]
173
    t(nx.utils.discrete_sequence, n, cdistribution=cdist, seed=seed)
174
    t(nx.algorithms.bipartite.random_graph, n, m, p, seed=seed)
175
    t(nx.algorithms.bipartite.gnmk_random_graph, n, m, k, seed=seed)
176
    LFR = nx.generators.LFR_benchmark_graph
177
    t(LFR, 25, 3, 1.5, 0.1, average_degree=3, min_community=10,
178
        seed=seed, max_community=20)
179
    # print("done")
180

    
181

    
182
# choose to test an integer seed, or whether a single RNG can be everywhere
183
# np_rng = np.random.RandomState(14)
184
# seed = np_rng
185
# seed = 14
186

    
187

    
188
# print("NetworkX Version:", nx.__version__)
189
def test_rng_interface():
190
    global progress
191

    
192
    # try different kinds of seeds
193
    for seed in [14, np.random.RandomState(14)]:
194
        np.random.seed(42)
195
        random.seed(42)
196
        run_all_random_functions(seed)
197
        progress = 0
198

    
199
        # check that both global RNGs are unaffected
200
        after_np_rv = np.random.rand()
201
#        if np_rv != after_np_rv:
202
#            print(np_rv, after_np_rv, "don't match np!")
203
        assert_equal(np_rv, after_np_rv)
204
        after_py_rv = random.random()
205
#        if py_rv != after_py_rv:
206
#            print(py_rv, after_py_rv, "don't match py!")
207
        assert_equal(py_rv, after_py_rv)
208

    
209
#        print("\nDone testing seed:", seed)
210

    
211
# test_rng_interface()