Python networkx.configuration_model() Examples
The following are 22
code examples of networkx.configuration_model().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: test_degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_havel_hakimi_construction(): G = nx.havel_hakimi_graph([]) assert_equal(len(G), 0) z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z) z = ["A", 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z) z = [5, 4, 3, 3, 3, 2, 2, 2] G = nx.havel_hakimi_graph(z) G = nx.configuration_model(z) z = [6, 5, 4, 4, 2, 1, 1, 1] assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z) z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2] G = nx.havel_hakimi_graph(z) assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z, create_using=nx.DiGraph())
Example #2
Source File: fig1p2.py From Mathematics-of-Epidemics-on-Networks with MIT License | 6 votes |
def generate_network(Pk, N, ntries = 100): r'''Generates an N-node random network whose degree distribution is given by Pk''' counter = 0 while counter< ntries: counter += 1 ks = [] for ctr in range(N): ks.append(Pk()) if sum(ks)%2 == 0: break if sum(ks)%2 ==1: raise EoN.EoNError("cannot generate even degree sum") G = nx.configuration_model(ks) return G #An erdos-renyi network has a Poisson degree distribution.
Example #3
Source File: test_from_joel.py From Mathematics-of-Epidemics-on-Networks with MIT License | 6 votes |
def test_SIS_simulations(self): print("test_SIS_simulations") plt.clf() tau = 0.1 gamma = 0.3 G = nx.configuration_model([1, 5, 10] * 100000) G = nx.Graph(G) G.remove_edges_from(G.selfloop_edges()) N = G.order() initial_size = 5000 for counter in range(10): print('fast_SIS') t, S, I = EoN.fast_SIS(G, tau, gamma, initial_infecteds=range(initial_size), tmax=20) plt.plot(t, S, '-.', color='b', alpha=0.3) plt.plot(t, I, '-.', color='b', alpha=0.3) print('Gillespie_SIS') t, S, I = EoN.Gillespie_SIS(G, tau, gamma, initial_infecteds=range(initial_size), tmax=20) plt.plot(t, S, '--', color='r', alpha=0.3) plt.plot(t, I, '--', color='r', alpha=0.3) plt.title('curves should overlie to show event driven and gillespie agree') plt.savefig('SIS_sims')
Example #4
Source File: analytic.py From Mathematics-of-Epidemics-on-Networks with MIT License | 6 votes |
def EBCM_pref_mix_discrete_from_graph(G, p, rho = None, tmin = 0, tmax = 100, return_full_data=False): ''' Takes a given graph, finds degree correlations, and calls EBCM_pref_mix_discrete :SAMPLE USE: :: import networkx as nx import EoN import matplotlib.pyplot as plt G = nx.bipartite.configuration_model([5]*300000, [2]*750000) t, S, I, R = EoN.basic_discrete_SIR(G, 0.6, rho = 0.002) tx, Sx, Ix, Rx = EoN.EBCM_pref_mix_discrete_from_graph(G, 0.6, rho=0.002, tmax=t[-1]) plt.plot(t, I, label = 'simulation') plt.plot(tx, Ix, '--', label = 'analytic prediction') plt.legend(loc='upper right') plt.show() ''' N = G.order() Pk = get_Pk(G) Pnk = get_Pnk(G) return EBCM_pref_mix_discrete(N, Pk, Pnk, p, rho=rho, tmin=tmin, tmax=tmax, return_full_data=return_full_data)
Example #5
Source File: make-monitor-progress.py From epydemic with GNU General Public License v3.0 | 6 votes |
def generateFrom(N, p, maxdeg=100): # construct degrees according to the distribution given # by the model function ns = [] t = 0 for i in range(N): while True: k = rng.integers(1, maxdeg) if rng.random() < p(k): ns.append(k) t += k break # the final sequence of degrees has to sum to an even number, as # each edge has two endpoints # if the sequence is odd, remove an element and draw another from # the distribution, repeating until the overall sequence is even while t % 2 != 0: # pick a node at random i = rng.integers(0, len(ns) - 1) # remove it from the sequence and from the total t -= ns[i] del ns[i] # draw a new degree from the distribution while True: k = rng.integers(1, maxdeg) if rng.random() < p(k): # add new node to the sequence ns.append(k) t += k break # populate the network using the configuration # model with the given degree distribution g = networkx.configuration_model(ns, create_using=networkx.Graph()) return g
Example #6
Source File: test_degree_seq.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_havel_hakimi_construction(): G = nx.havel_hakimi_graph([]) assert_equal(len(G), 0) z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z) z = ["A", 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z) z = [5, 4, 3, 3, 3, 2, 2, 2] G = nx.havel_hakimi_graph(z) G = nx.configuration_model(z) z = [6, 5, 4, 4, 2, 1, 1, 1] assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z) z = [10, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2] G = nx.havel_hakimi_graph(z) assert_raises(nx.NetworkXError, nx.havel_hakimi_graph, z, create_using=nx.DiGraph())
Example #7
Source File: test_degree_seq.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_degree_sequence(self): """Tests that the degree sequence of the generated graph matches the input degree sequence. """ deg_seq = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] G = nx.configuration_model(deg_seq, seed=12345678) assert_equal(sorted((d for n, d in G.degree()), reverse=True), [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]) assert_equal(sorted((d for n, d in G.degree(range(len(deg_seq)))), reverse=True), [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1])
Example #8
Source File: test_degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_odd_degree_sum(self): """Tests that a degree sequence whose sum is odd yields an exception. """ nx.configuration_model([1, 2])
Example #9
Source File: test_degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_random_seed(self): """Tests that each call with the same random seed generates the same graph. """ deg_seq = [3] * 12 G1 = nx.configuration_model(deg_seq, seed=1000) G2 = nx.configuration_model(deg_seq, seed=1000) assert_true(nx.is_isomorphic(G1, G2)) G1 = nx.configuration_model(deg_seq, seed=10) G2 = nx.configuration_model(deg_seq, seed=10) assert_true(nx.is_isomorphic(G1, G2))
Example #10
Source File: test_degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_degree_sequence(self): """Tests that the degree sequence of the generated graph matches the input degree sequence. """ deg_seq = [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] G = nx.configuration_model(deg_seq, seed=12345678) assert_equal(sorted((d for n, d in G.degree()), reverse=True), [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1]) assert_equal(sorted((d for n, d in G.degree(range(len(deg_seq)))), reverse=True), [5, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1])
Example #11
Source File: test_degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_degree_zero(self): """Tests that a degree sequence of all zeros yields the empty graph. """ G = nx.configuration_model([0, 0, 0]) assert_equal(len(G), 3) assert_equal(G.number_of_edges(), 0)
Example #12
Source File: test_degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_empty_degree_sequence(self): """Tests that an empty degree sequence yields the null graph.""" G = nx.configuration_model([]) assert_equal(len(G), 0)
Example #13
Source File: test_degree_seq.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_odd_degree_sum(self): """Tests that a degree sequence whose sum is odd yields an exception. """ nx.configuration_model([1, 2])
Example #14
Source File: test_degree_seq.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_random_seed(self): """Tests that each call with the same random seed generates the same graph. """ deg_seq = [3] * 12 G1 = nx.configuration_model(deg_seq, seed=1000) G2 = nx.configuration_model(deg_seq, seed=1000) assert_true(nx.is_isomorphic(G1, G2)) G1 = nx.configuration_model(deg_seq, seed=10) G2 = nx.configuration_model(deg_seq, seed=10) assert_true(nx.is_isomorphic(G1, G2))
Example #15
Source File: fig6p24.py From Mathematics-of-Epidemics-on-Networks with MIT License | 5 votes |
def get_G(N, Pk): while True: ks = [] for ctr in range(N): r = random.random() for k in Pk: if r<Pk[k]: break else: r-= Pk[k] ks.append(k) if sum(ks)%2==0: break G = nx.configuration_model(ks) return G
Example #16
Source File: test_degree_seq.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_degree_zero(self): """Tests that a degree sequence of all zeros yields the empty graph. """ G = nx.configuration_model([0, 0, 0]) assert_equal(len(G), 3) assert_equal(G.number_of_edges(), 0)
Example #17
Source File: test_degree_seq.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_empty_degree_sequence(self): """Tests that an empty degree sequence yields the null graph.""" G = nx.configuration_model([]) assert_equal(len(G), 0)
Example #18
Source File: fig9p4.py From Mathematics-of-Epidemics-on-Networks with MIT License | 5 votes |
def regular_graph_generation(N, kave): return nx.configuration_model([kave]*N)
Example #19
Source File: degree_seq.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def random_degree_sequence_graph(sequence, seed=None, tries=10): r"""Return a simple random graph with the given degree sequence. If the maximum degree $d_m$ in the sequence is $O(m^{1/4})$ then the algorithm produces almost uniform random graphs in $O(m d_m)$ time where $m$ is the number of edges. Parameters ---------- sequence : list of integers Sequence of degrees seed : hashable object, optional Seed for random number generator tries : int, optional Maximum number of tries to create a graph Returns ------- G : Graph A graph with the specified degree sequence. Nodes are labeled starting at 0 with an index corresponding to the position in the sequence. Raises ------ NetworkXUnfeasible If the degree sequence is not graphical. NetworkXError If a graph is not produced in specified number of tries See Also -------- is_graphical, configuration_model Notes ----- The generator algorithm [1]_ is not guaranteed to produce a graph. References ---------- .. [1] Moshen Bayati, Jeong Han Kim, and Amin Saberi, A sequential algorithm for generating random graphs. Algorithmica, Volume 58, Number 4, 860-910, DOI: 10.1007/s00453-009-9340-1 Examples -------- >>> sequence = [1, 2, 2, 3] >>> G = nx.random_degree_sequence_graph(sequence) >>> sorted(d for n, d in G.degree()) [1, 2, 2, 3] """ DSRG = DegreeSequenceRandomGraph(sequence, seed=seed) for try_n in range(tries): try: return DSRG.generate() except nx.NetworkXUnfeasible: pass raise nx.NetworkXError('failed to generate graph in %d tries' % tries)
Example #20
Source File: degree_seq.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def random_degree_sequence_graph(sequence, seed=None, tries=10): r"""Returns a simple random graph with the given degree sequence. If the maximum degree $d_m$ in the sequence is $O(m^{1/4})$ then the algorithm produces almost uniform random graphs in $O(m d_m)$ time where $m$ is the number of edges. Parameters ---------- sequence : list of integers Sequence of degrees seed : integer, random_state, or None (default) Indicator of random number generation state. See :ref:`Randomness<randomness>`. tries : int, optional Maximum number of tries to create a graph Returns ------- G : Graph A graph with the specified degree sequence. Nodes are labeled starting at 0 with an index corresponding to the position in the sequence. Raises ------ NetworkXUnfeasible If the degree sequence is not graphical. NetworkXError If a graph is not produced in specified number of tries See Also -------- is_graphical, configuration_model Notes ----- The generator algorithm [1]_ is not guaranteed to produce a graph. References ---------- .. [1] Moshen Bayati, Jeong Han Kim, and Amin Saberi, A sequential algorithm for generating random graphs. Algorithmica, Volume 58, Number 4, 860-910, DOI: 10.1007/s00453-009-9340-1 Examples -------- >>> sequence = [1, 2, 2, 3] >>> G = nx.random_degree_sequence_graph(sequence) >>> sorted(d for n, d in G.degree()) [1, 2, 2, 3] """ DSRG = DegreeSequenceRandomGraph(sequence, seed) for try_n in range(tries): try: return DSRG.generate() except nx.NetworkXUnfeasible: pass raise nx.NetworkXError('failed to generate graph in %d tries' % tries)
Example #21
Source File: degree_seq.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def random_degree_sequence_graph(sequence, seed=None, tries=10): r"""Return a simple random graph with the given degree sequence. If the maximum degree `d_m` in the sequence is `O(m^{1/4})` then the algorithm produces almost uniform random graphs in `O(m d_m)` time where `m` is the number of edges. Parameters ---------- sequence : list of integers Sequence of degrees seed : hashable object, optional Seed for random number generator tries : int, optional Maximum number of tries to create a graph Returns ------- G : Graph A graph with the specified degree sequence. Nodes are labeled starting at 0 with an index corresponding to the position in the sequence. Raises ------ NetworkXUnfeasible If the degree sequence is not graphical. NetworkXError If a graph is not produced in specified number of tries See Also -------- is_valid_degree_sequence, configuration_model Notes ----- The generator algorithm [1]_ is not guaranteed to produce a graph. References ---------- .. [1] Moshen Bayati, Jeong Han Kim, and Amin Saberi, A sequential algorithm for generating random graphs. Algorithmica, Volume 58, Number 4, 860-910, DOI: 10.1007/s00453-009-9340-1 Examples -------- >>> sequence = [1, 2, 2, 3] >>> G = nx.random_degree_sequence_graph(sequence) >>> sorted(G.degree().values()) [1, 2, 2, 3] """ DSRG = DegreeSequenceRandomGraph(sequence, seed=seed) for try_n in range(tries): try: return DSRG.generate() except nx.NetworkXUnfeasible: pass raise nx.NetworkXError('failed to generate graph in %d tries'%tries)
Example #22
Source File: test_from_joel.py From Mathematics-of-Epidemics-on-Networks with MIT License | 4 votes |
def test_Gillespie_complex_contagion(self): def transition_rate(G, node, status, parameters): # this function needs to return the rate at which ``node`` changes status # r = parameters[0] if status[node] == 'S' and len([nbr for nbr in G.neighbors(node) if status[nbr] == 'I']) > 1: return 1 else: # status[node] might be 0 or length might be 0 or 1. return 0 def transition_choice(G, node, status, parameters): # this function needs to return the new status of node. We assume going # in that we have already calculated it is changing status. # # this function could be more elaborate if there were different # possible transitions that could happen. However, for this model, # the 'I' nodes aren't changing status, and the 'S' ones are changing to 'I' # So if we're in this function, the node must be 'S' and becoming 'I' # return 'I' def get_influence_set(G, node, status, parameters): # this function needs to return any node whose rates might change # because ``node`` has just changed status. That is, which nodes # might ``node`` influence? # # For our models the only nodes a node might affect are the susceptible neighbors. return {nbr for nbr in G.neighbors(node) if status[nbr] == 'S'} parameters = (2,) # this is the threshold. Note the comma. It is needed # for python to realize this is a 1-tuple, not just a number. # ``parameters`` is sent as a tuple so we need the comma. N = 60000 deg_dist = [2, 4, 6] * int(N / 3) G = nx.configuration_model(deg_dist) for rho in np.linspace(3. / 80, 7. / 80, 8): # 8 values from 3/80 to 7/80. print(rho) IC = defaultdict(lambda: 'S') for node in G.nodes(): if np.random.random() < rho: # there are faster ways to do this random selection IC[node] = 'I' t, S, I = EoN.Gillespie_complex_contagion(G, transition_rate, transition_choice, get_influence_set, IC, return_statuses=('S', 'I'), parameters=parameters) plt.plot(t, I) plt.savefig('test_Gillespie_complex_contagion')