def RBR(Na, Nb, za, zb, p): ''' Return a NetworkX graph composed of two random za-, zb-regular graphs of sizes Na, Nb, with Bernoulli(p) distributed coupling. ''' network_build_time = time.time() a_internal_graph = nx.random_regular_graph(za, Na) b_internal_graph = nx.random_regular_graph(zb, Nb) a_inter_stubs = [bern(p) for i in xrange(Na)] b_inter_stubs = [bern(p*float(Na)/float(Nb)) for i in xrange(Nb)] while sum(a_inter_stubs) != sum(b_inter_stubs) or abs(sum(a_inter_stubs)-(p*Na)) > 0: a_inter_stubs = [bern(p) for i in xrange(Na)] b_inter_stubs = [bern(p*float(Na)/float(Nb)) for i in xrange(Nb)] G = nx.bipartite_configuration_model(a_inter_stubs, b_inter_stubs) for u, v in a_internal_graph.edges(): G.add_edge(u, v) for u, v in b_internal_graph.edges(): G.add_edge(Na+u, Na+v) network_build_time = time.time() - network_build_time print('Generating the network took {0:.3f} seconds, {1:.3f} minutes, {2:.3f} hours.'.format(network_build_time, network_build_time/60.0, network_build_time/3600.0)) return G
def RBR(Na, Nb, za, zb, p): ''' Return a NetworkX graph composed of two random za-, zb-regular graphs of sizes Na, Nb, with Bernoulli(p) distributed coupling. ''' network_build_time = time.time() a_internal_graph = nx.random_regular_graph(za, Na) b_internal_graph = nx.random_regular_graph(zb, Nb) a_inter_stubs = [bern(p) for i in xrange(Na)] b_inter_stubs = [bern(p * float(Na) / float(Nb)) for i in xrange(Nb)] while sum(a_inter_stubs) != sum(b_inter_stubs) or abs( sum(a_inter_stubs) - (p * Na)) > 0: a_inter_stubs = [bern(p) for i in xrange(Na)] b_inter_stubs = [bern(p * float(Na) / float(Nb)) for i in xrange(Nb)] G = nx.bipartite_configuration_model(a_inter_stubs, b_inter_stubs) for u, v in a_internal_graph.edges(): G.add_edge(u, v) for u, v in b_internal_graph.edges(): G.add_edge(Na + u, Na + v) network_build_time = time.time() - network_build_time print( 'Generating the network took {0:.3f} seconds, {1:.3f} minutes, {2:.3f} hours.' .format(network_build_time, network_build_time / 60.0, network_build_time / 3600.0)) return G
def generateNullData(geneMarginals, sampleMarginals): graph = networkx.bipartite_configuration_model( geneMarginals, sampleMarginals, create_using=networkx.Graph()) events = numpy.asarray( networkx.algorithms.bipartite.biadjacency_matrix( graph, [n for n in graph if graph.node[n]["bipartite"] == 0])) return events
def _random_biregular_graph(num_left_vertices, num_right_vertices, left_degree, right_degree, half_only=False, seed=None): """Returns the adjacency matrix of a random biregular graph with the specified number of left and right vertices, and the specified left and right degree. The returned adjacency matrix is a NumPy array (not a list of lists, not a NumPy matrix). If **L** and **R** are the number of left and right vertices respectively and **d** and **e** are the left and right degree respectively, then the returned adjacency matrix has the form:: _ _ | 0 B | |_B^T 0_| where B is an L by R matrix in which each row sums to **d** and each column sums to **e**. In this form, the first L nonnegative integers represent the left vertices and the following R integers represent the right vertices. If `half_only` is ``True``, this function returns only the submatrix B. If `seed` is specified, it must be an integer provided as the seed to the pseudorandom number generator used to generate the graph. .. note:: Although Networkx includes a function for generating random bipartite graphs (:func:`networkx.generators.bipartite.bipartite_configuration_model`), their adjacency matrices do not necessarily have this block form. """ # Rename some variables for brevity. L, R = num_left_vertices, num_right_vertices n = L + R # Generate a random graph with the appropriate degree sequence. left_sequence = [left_degree for x in range(L)] right_sequence = [right_degree for x in range(R)] # Need to use `create_using=Graph()` or else networkx will create a # multigraph. graph = bipartite_configuration_model(left_sequence, right_sequence, create_using=Graph(), seed=seed) # Find the nodes that are in the left and right sets. The `bipartite` # attribute specifies which set each vertex is in. left_or_right = get_node_attributes(graph, 'bipartite') left_nodes = (v for v, side in left_or_right.items() if side == 0) right_nodes = (v for v, side in left_or_right.items() if side == 1) all_nodes = itertools.chain(left_nodes, right_nodes) # Determine the permutation that moves all the left nodes to the top rows # of the matrix and all the right nodes to the bottom rows of the # matrix. The permutation maps row number to vertex that should be in that # row. permutation = {i: v for i, v in enumerate(all_nodes)} P = permutation_to_matrix(permutation) # Apply the permutation matrix to the adjacency matrix. M = to_numpy_matrix(graph) result = P * M # If `half_only` is True, only return the submatrix block consisting of the # first L rows and the last R columns. return result[:L, -R:] if half_only else result
def ERBER(Na, Nb, za, zb, p): ''' Return a NetworkX graph composed of two random ER random graphs of sizes Na, Nb, mean degrees za, zb, with Bernoulli(p) distributed coupling. ''' network_build_time = time.time() a_internal_graph = nx.erdos_renyi_graph(Na, float(za) / float(Na)) b_internal_graph = nx.erdos_renyi_graph(Nb, float(zb) / float(Nb)) # Just use the largest connected component a_internal_graph = nx.connected_component_subgraphs(a_internal_graph)[0] b_internal_graph = nx.connected_component_subgraphs(b_internal_graph)[0] # Relabe the nodes to count from 0 (since Sandpile uses this structure) mapping_a = dict( zip(a_internal_graph.nodes(), np.arange(0, len(a_internal_graph.nodes())))) mapping_b = dict( zip(b_internal_graph.nodes(), np.arange(0, len(b_internal_graph.nodes())))) a_internal_graph = nx.relabel_nodes(a_internal_graph, mapping_a) b_internal_graph = nx.relabel_nodes(b_internal_graph, mapping_b) a_inter_stubs = [bern(p) for i in a_internal_graph.nodes()] b_inter_stubs = [bern(p) for i in b_internal_graph.nodes()] while sum(a_inter_stubs) != sum(b_inter_stubs) or abs( sum(a_inter_stubs) - (p * a_internal_graph.number_of_nodes())) > 1: a_inter_stubs = [bern(p) for i in a_internal_graph.nodes()] b_inter_stubs = [bern(p) for i in b_internal_graph.nodes()] G = nx.bipartite_configuration_model(a_inter_stubs, b_inter_stubs) for u, v in a_internal_graph.edges(): G.add_edge(u, v) for u, v in b_internal_graph.edges(): G.add_edge(a_internal_graph.number_of_nodes() + u, a_internal_graph.number_of_nodes() + v) network_build_time = time.time() - network_build_time print( 'Generating the network took {0:.3f} seconds, {1:.3f} minutes, {2:.3f} hours.' .format(network_build_time, network_build_time / 60.0, network_build_time / 3600.0)) print('The network has {0} nodes, {1} edges'.format( G.number_of_nodes(), G.number_of_edges())) return G
def multiplex_configuration_bipartite(mg,seed=None): """Bipartite configuration model. First convert the multiplex network to a bipartite graph using layer-edge view. Then run a configuraion model on the bipartite graph and reconstruct a multiplex network. This configuration model will preserve the aggregated network and the number of layers each edge sits on. Parameters ---------- mg : Multinet Multiplex network to be configured. seed : object Seed for the configuration model. Return ------ A new Multinet instance. """ bg = bipartize(mg,'e') top,bottom = bipartite_sets(bg) top = list(top) bottom = list(bottom) degree_getter = lambda x: bg.degree(x) dtop = map(degree_getter,top) dbottom = map(degree_getter,bottom) rbg = nx.bipartite_configuration_model(dtop,dbottom,create_using=nx.Graph(),seed=seed) rtop,rbottom = bipartite_sets(rbg) keys = list(rtop)+list(rbottom) values = list(top)+list(bottom) mapping = dict(zip(keys,values)) nrbg = nx.relabel_nodes(rbg,mapping) nmg = reconstruct_from_bipartite(nrbg) return nmg
def ERBER(Na, Nb, za, zb, p): ''' Return a NetworkX graph composed of two random ER random graphs of sizes Na, Nb, mean degrees za, zb, with Bernoulli(p) distributed coupling. ''' network_build_time = time.time() a_internal_graph = nx.erdos_renyi_graph(Na, float(za)/float(Na)) b_internal_graph = nx.erdos_renyi_graph(Nb, float(zb)/float(Nb)) # Just use the largest connected component a_internal_graph = nx.connected_component_subgraphs(a_internal_graph)[0] b_internal_graph = nx.connected_component_subgraphs(b_internal_graph)[0] # Relabe the nodes to count from 0 (since Sandpile uses this structure) mapping_a = dict(zip(a_internal_graph.nodes(),np.arange(0,len(a_internal_graph.nodes())))) mapping_b = dict(zip(b_internal_graph.nodes(),np.arange(0,len(b_internal_graph.nodes())))) a_internal_graph = nx.relabel_nodes(a_internal_graph,mapping_a) b_internal_graph = nx.relabel_nodes(b_internal_graph,mapping_b) a_inter_stubs = [bern(p) for i in a_internal_graph.nodes()] b_inter_stubs = [bern(p) for i in b_internal_graph.nodes()] while sum(a_inter_stubs) != sum(b_inter_stubs) or abs(sum(a_inter_stubs)-(p*a_internal_graph.number_of_nodes())) > 1: a_inter_stubs = [bern(p) for i in a_internal_graph.nodes()] b_inter_stubs = [bern(p) for i in b_internal_graph.nodes()] G = nx.bipartite_configuration_model(a_inter_stubs, b_inter_stubs) for u, v in a_internal_graph.edges(): G.add_edge(u, v) for u, v in b_internal_graph.edges(): G.add_edge(a_internal_graph.number_of_nodes()+u, a_internal_graph.number_of_nodes()+v) network_build_time = time.time() - network_build_time print('Generating the network took {0:.3f} seconds, {1:.3f} minutes, {2:.3f} hours.'.format(network_build_time, network_build_time/60.0, network_build_time/3600.0)) print('The network has {0} nodes, {1} edges'.format(G.number_of_nodes(), G.number_of_edges())) return G
def tripartite_regular_configuration_graph(n, pA, pB, kAB, kAC, kBC, verbose=False): ''' Generate a tripartite regular random graph using a stub-joining process. Parameters ========== n, int Number of nodes. pA, float, 0 <= pA <= 1 Size of set A. pB, float, 0 <= pB <= 1 Size of set B. kAB, float, kAB >= 0 Degree from set A to B. kAC, float, kAC >= 0 Degree from set A to C. kBC, float, kBC >= 0 Degree from set B to C. The graph is generated by creating bipartite configuration model graphs for each pair of node sets (A,B), (A,C), (B,C). example ======= import tripartite as tri import spectrum as spec import matplotlib.pyplot as plt g = tri.tripartite_regular_configuration_graph(2400, .2, .3, 3,5,5) L = spec.compute_spectrum(g) plt.hist(L, bins=301, normed=True) ''' nA = int(np.round(n * pA)) nB = int(np.round(n * pB)) nC = n - nA - nB pC = 1 - pA - pB kBA = int(np.round((pA / pB) * kAB)) kCB = int(np.round((pB / pC) * kBC)) kCA = int(np.round((pA / pC) * kAC)) if verbose: print 'generating tripartite graph' print 'n = ', n print 'pA = ', pA print 'pB = ', pB print 'pC = ', pC print 'nA = ', nA print 'nB = ', nB print 'nC = ', nC print 'kAB = ', kAB print 'kAC = ', kAC print 'kBA = ', kBA print 'kBC = ', kBC print 'kCB = ', kCB print 'kCA = ', kCA def _extract_blocks(A, n1, n2): X = A[0:n1, n1:n1 + n2] Xt = A[n1:n1 + n2, 0:n1] return X, Xt g1 = nx.bipartite_configuration_model([kAB] * nA, [kBA] * nB) # while g1.is_multigraph(): # g1 = nx.bipartite_configuration_model([kAB]*nA, [kBA]*nB) X, Xt = _extract_blocks(nx.to_numpy_matrix(g1), nA, nB) g2 = nx.bipartite_configuration_model([kAC] * nA, [kCA] * nC) # while g2.is_multigraph(): # g2 = nx.bipartite_configuration_model([kAC]*nA, [kCA]*nC) Y, Yt = _extract_blocks(nx.to_numpy_matrix(g2), nA, nC) g3 = nx.bipartite_configuration_model([kBC] * nB, [kCB] * nC) # while g3.is_multigraph(): # g3 = nx.bipartite_configuration_model([kBC]*nB, [kCB]*nC) Z, Zt = _extract_blocks(nx.to_numpy_matrix(g3), nB, nC) A = np.bmat([[np.zeros((nA, nA)), X, Y], [Xt, np.zeros((nB, nB)), Z], [Yt, Zt, np.zeros((nC, nC))]]) g = nx.from_numpy_matrix(A) b = dict(zip(range(0, nA), [0] * nA)) b.update(dict(zip(range(nA, nA + nB), [1] * nB))) b.update(dict(zip(range(nA + nB, nA + nB + nC), [2] * nC))) nx.set_node_attributes(g, 'tripartite', b) return g