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))
def get_corr_rand_set(G,disease_seeds,num_reps=5,alpha=.5,num_its=20,conserve_heat=True): ''' Calculate the dot-product of heat propagated on N disease gene sets (disease_seeds: dict with keys disease names and values lists of disease genes), on an edge-shuffled, degree-preserving random matrix, with number of repetitions = num_reps, alpha=alpha, num_its = num_its. Return the mean of the dot-product averaged over num_reps, and the standard deviation over num_reps, over all pairs of gene sets in disease_seeds. This way we only have to create one random matrix for each pair, which will speed up processing time a bit. ''' num_Ds = len(disease_seeds) dnames = disease_seeds.keys() dname_pairs = list(itertools.combinations(dnames, 2)) dot_rand=dict() dot_rand_mean = dict() dot_rand_std = dict() for d in dname_pairs: # initialize dictionaries dot_rand[d] = [] dot_rand_mean[d] = [] dot_rand_std[d] = [] for r in range(num_reps): G_temp = nx.configuration_model(G.degree().values()) G_rand = nx.Graph() # switch from multigraph to digraph G_rand.add_edges_from(G_temp.edges()) # remove self-loops #G_rand.remove_edges_from(G_rand.selfloop_edges()) G_rand = nx.relabel_nodes(G_rand,dict(zip(range(len(G_rand.nodes())),G.degree().keys()))) Wprime_rand = normalized_adj_matrix(G_rand,conserve_heat=conserve_heat) for i in range(len(dname_pairs)): seeds_D1 = disease_seeds[dname_pairs[i][0]] seeds_D2 = disease_seeds[dname_pairs[i][1]] Fnew_D1 = network_propagation(G_rand,Wprime_rand,seeds_D1,alpha=alpha,num_its=num_its) Fnew_D1_norm = Fnew_D1/np.linalg.norm(Fnew_D1) rand_seeds = seeds_D2 #set(random.sample(G.nodes(),size_rand_set)) Fnew_D2 = network_propagation(G_rand,Wprime_rand,seeds_D2,alpha=alpha,num_its=num_its) Fnew_D2_norm = Fnew_D2/np.linalg.norm(Fnew_D2) idx_g0 = list(Fnew_D1[(Fnew_D1>0)&(Fnew_D2>0)].index) idx_ND1ND2 = list(np.setdiff1d(list(Fnew_D1.index),np.union1d(seeds_D1,seeds_D2))) dot_D1_D1 = np.dot(Fnew_D1_norm,Fnew_D2_norm) dot_rand[dname_pairs[i]].append(dot_D1_D1) for d in dname_pairs: dot_rand_mean[d] = np.mean(dot_rand[d]) dot_rand_std[d] = np.std(dot_rand[d]) return dot_rand_mean,dot_rand_std
def powerlaw_degree_sequence(n, a): """ Create a graph without self-loops or parallel edges; having power law degree distribution with an exponent 'around' a. Parameters ---------- n : int Number of nodes in graph. a : float Ideal exponent. Returns ------- G : networkx.Graph The constructed graph. """ dsq = nx.create_degree_sequence(n, nx.utils.powerlaw_sequence, exponent = a) G = nx.Graph(nx.configuration_model(dsq)) G.remove_edges_from(G.selfloop_edges()) # Check for a disconnected graph just in case... if not nx.is_connected(G): emsg = "The generated power-law graph is not connected!" logger.error(emsg) raise NepidemiXBaseException(emsg) return G
def scale_free_network(n=100, gamma=2.5, avrdeg=8): """ Generates a scale free network by configuration model, which is shown in :cite:`Wu2011` :param int n: The number of nodes :param float gamma: Exponent of degree distribution :param float avrdeg: Average degree :return: Scale-free network :rtype: networkx.Graph Generates degree sequence from calling :func:`~.powerlaw_sequence` and :func:`networkx.utils.random_sequence.create_degree_sequence` then, build network by configuration model (see :func:`networkx.generators.degree_seq.configuration_model`) finally, remove self-loop and parallel edge to simplify. .. Note:: Actual average degree is smaller than `avrdeg` """ seq = create_degree_sequence( n, powerlaw_sequence, gamma=gamma, avrdeg=avrdeg) G = configuration_model(seq) # multigraph -> graph G.remove_edges_from(G.selfloop_edges()) return nx.Graph(G)
def normaliseNodeWise(G, func, n=500, retNorm=True, inVal=None): """ This function normalises the function G by generating a series of n random graphs and averaging the results. If retNorm is specified, the normalised value is returned, else a list of n values for a random graph are returned. """ nodesDict = {v:[] for v in G.nodes()} for i in range(n): rand = configuration_model(G.degree().values()) rand = nx.Graph(rand) # convert to simple graph from multigraph nodeList = [v for v in rand.nodes() if rand.degree(v)==0] rand.remove_nodes_from(nodeList) try: res = func(rand) # collect values using the function except KeyError: # exception raised if the spatial information is missing print "Adding spatial info" nx.set_node_attributes(rand, 'xyz', {rn:G.node[v]['xyz'] for rn,v in enumerate(G.nodes())}) # copy across spatial information res = func(rand) # collect values using the function for x,node in enumerate(nodesDict): nodesDict[node].append(res[x]) if retNorm: # return the normalised values if not inVal: inVal = func(G) for node in nodesDict: nodesDict[node] = inVal[node]/np.mean(nodesDict[node]) return(nodesDict) else: # return a list of the values from the random graph return(nodesDict)
def normalise(G, func, n=500, retNorm=True, inVal=None, printLCC=False): """ This function normalises the function G by generating a series of n random graphs and averaging the results. If retNorm is specified, the normalised value is returned, else a list of n values for a random graph are returned. """ vals = [] for i in range(n): rand = configuration_model(G.degree().values()) if nx.components.number_connected_components(rand)>1: graphList = ccs(rand) rand = graphList.next() if printLCC: print "Selecting largest connected components of "+str(len(rand.nodes()))+" nodes" try: vals.append(func(rand)) # collect values using the function except KeyError: # exception raised if the spatial information is missing nx.set_node_attributes(rand, 'xyz', {rn:G.node[v]['xyz'] for rn,v in enumerate(G.nodes())}) vals.append(func(rand)) # collect values using the function if retNorm: # return the normalised values if not inVal: inVal = func(G) return(inVal/np.mean(vals)) else: # return a list of the values from the random graph return(vals)
def initScaleFreeGraph(self,N,k,e): s = [] while len(s) < N: nextval = int(nx.utils.powerlaw_sequence(k, e)[0]) if nextval != 0: s.append(nextval) #TODO: s must be scaled s = s/np.mean(s) * k s = np.around(s) s = s.astype(int) # Sum of degrees must be even. One way to solve this is to add a single node with degree 1 if sum(s) % 2: s[len(s)-1] = s[len(s)-1] + 1 print "length s:", len(s) print "Sum:", sum(s) G = nx.configuration_model(s) G = nx.Graph(G) # Remove self-loops G.remove_edges_from(G.selfloop_edges()) return G
def test_configuration(): seeds = [2718183590, 2470619828, 1694705158, 3001036531, 2401251497] for seed in seeds: deg_seq = nx.random_powerlaw_tree_sequence(20, seed=seed, tries=5000) G = nx.Graph(nx.configuration_model(deg_seq, seed=seed)) G.remove_edges_from(nx.selfloop_edges(G)) _check_augmentations(G)
def createGraph(self): """ Calculate the basic data for the graph """ """ Generate everything for the scale free network """ # Create a graph with degrees following a power law distribution s = [] count = 0 while len(s) < self.N: nextval = int(nx.utils.powerlaw_sequence(int(self.k), self.e)[0]) if nextval != 0: count += nextval s.append(nextval) # s scaled and rounded such that the average degree equals k s = s / np.mean(s) * self.k s = np.around(s).astype(int) # Sum of degrees must be even. I added one edge to the first node to fix this if sum(s) % 2: s[0] += 1 G = nx.configuration_model(s) G = nx.Graph(G) # Remove self-loops G.remove_edges_from(G.selfloop_edges()) self.G = G self.generateInfected()
def generate_graph(type = 'PL', n = 100, seed = 1.0, parameter = 2.1): if type == 'ER': G = nx.erdos_renyi_graph(n, p=parameter, seed=seed, directed=True) G = nx.DiGraph(G) G.remove_edges_from(G.selfloop_edges()) elif type == 'PL': z = nx.utils.create_degree_sequence(n, nx.utils.powerlaw_sequence, exponent = parameter) while not nx.is_valid_degree_sequence(z): z = nx.utils.create_degree_sequence(n, nx.utils.powerlaw_sequence, exponent = parameter) G = nx.configuration_model(z) G = nx.DiGraph(G) G.remove_edges_from(G.selfloop_edges()) elif type == 'BA': G = nx.barabasi_albert_graph(n, 3, seed=None) G = nx.DiGraph(G) elif type == 'grid': G = nx.grid_2d_graph(int(np.ceil(np.sqrt(n))), int(np.ceil(np.sqrt(n)))) G = nx.DiGraph(G) elif type in ['facebook', 'enron', 'twitter', 'students', 'tumblr', 'facebookBig']: #print 'start reading' #_, G, _, _ = readRealGraph(os.path.join("..","..","Data", type+".txt")) _, G, _, _ = readRealGraph(os.path.join("..","Data", type+".txt")) print 'size of graph', G.number_of_nodes() #Gcc = sorted(nx.connected_component_subgraphs(G.to_undirected()), key = len, reverse=True) #print Gcc[0].number_of_nodes() #print 'num of connected components', len(sorted(nx.connected_component_subgraphs(G.to_undirected()), key = len, reverse=True)) #exit() if G.number_of_nodes() > n: G = getSubgraph(G, n) #G = getSubgraphSimulation(G, n, infP = 0.3) #nx.draw(G) #plt.show() return G
def randomly_clustering(g, tries = 10): """ Comparing the average clustering coefficient of g with other graphs h which share identical degree sequence. This function returns the comparison ratio. Parameters: ----------- g: NetworkX Graph, NetworkX DiGraph tries: int, optional, (default = 10) number of tries (compared graphs) See also: --------- mean_clustering Returns: -------- float, the ratio of avg clustering coefficient, avg_cc(g) / mean(avg_cc(h)) """ from scipy import average g = to_undirected(g) d = g.degree().values() c = mean_clustering(g, normalized = False) p = list() for t in xrange(tries): ng = nx.configuration_model(d, create_using = nx.Graph()) p.append(mean_clustering(ng)) del ng return(c / average(p))
def generateNetwork(): ## use a networkx function to create a degree sequence that follows a power law degreeSequence=nx.utils.create_degree_sequence(numberOfNodes,powerlaw_sequence, 100) ## use aforementioned degree sequence to configure a pseudograph that contains self-loops & hyper-edges pseudoGraph=nx.configuration_model(degreeSequence) ## remove hyper (parallel) edges Graph = nx.Graph(pseudoGraph) ## remove self edges Graph.remove_edges_from(Graph.selfloop_edges()) ## loop through all nodes and set capacity equal to degree for bankID in range(0, len(Graph.node)): Graph.node[bankID]['bankID'] = bankID Graph.node[bankID]['capacity'] = Graph.degree(bankID) Graph.node[bankID]['solventNeighbors'] = Graph.degree(bankID) ## right now capacity = degree Graph.node[bankID]['cumulativeShock'] = 0 ## solvent = normal ## exposed = cumulative shock is less than capacity ## fail = recently failed, about to spread ## dead = can no longer spread or receive shocks Graph.node[bankID]['status'] = 'solvent' ## here we set the timestep that the bank becomes insolvent to a big number Graph.node[bankID]['insolventTimestep'] = 50 ## here we set the size of the shock to be propagated (zero at sim start) Graph.node[bankID]['shockToPropagate'] = 0 return Graph
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)
def test_configuration_directed(): # seeds = [671221681, 2403749451, 124433910, 672335939, 1193127215] seeds = [67] for seed in seeds: deg_seq = nx.random_powerlaw_tree_sequence(20, seed=seed, tries=5000) G = nx.DiGraph(nx.configuration_model(deg_seq, seed=seed)) G.remove_edges_from(nx.selfloop_edges(G)) _check_edge_connectivity(G)
def measure_connectivity(G,focal_nodes,method='jaccard',num_reps=10): avg_focal_degree = np.mean(G.degree(focal_nodes).values()) var_focal_degree = np.std(G.degree(focal_nodes).values()) if method=='jaccard': #focal_sim = jaccard_sim_array(G,focal_nodes) # select random node set rand_sim=[] focal_sim=[] for r in range(num_reps): # bootstrap a sample from focal_nodes focal_subset = np.random.choice(list(focal_nodes),size=len(focal_nodes),replace=True) focal_sim.append(jaccard_sim_array(G,focal_subset)) print('calculating random set ' + str(r) + ' out of ' + str(num_reps)) G_temp = nx.configuration_model(G.degree().values()) G_rand = nx.Graph() # switch from multigraph to digraph G_rand.add_edges_from(G_temp.edges()) # remove self-loops #G_rand.remove_edges_from(G_rand.selfloop_edges()) G_rand = nx.relabel_nodes(G_rand,dict(zip(range(len(G_rand.nodes())),G.degree().keys()))) rand_sim.append(jaccard_sim_array(G_rand,focal_subset)) # measure the jaccard similarity of degree-preserving edge shuffled network elif method=='edge_overlap': focal_sim = num_shared_neighbors(G,focal_nodes) # select random node set rand_sim=[] for r in range(num_reps): print('calculating random set ' + str(r) + ' out of ' + str(num_reps)) G_rand = nx.configuration_model(G.degree().values()) G_rand = nx.relabel_nodes(G_rand,dict(zip(range(len(G_rand.nodes())),G.degree().keys()))) rand_sim.append(num_shared_neighbors(G_rand,focal_nodes)) # rand_sim is array of length num_reps return focal_sim,rand_sim
def generate_random_graph_powerlaw(n): m = 1 p = 0.7 #H = powerlaw_cluster_graph(n, m, p) gamma = 2.5 powerlaw_gamma = lambda N: nx.utils.powerlaw_sequence(N, exponent=gamma) z = nx.utils.create_degree_sequence(n, powerlaw_gamma, max_tries=1000) G = nx.configuration_model(z) while not nx.is_connected(G): powerlaw_gamma = lambda N: nx.utils.powerlaw_sequence(N, exponent=gamma) z = nx.utils.create_degree_sequence(n, powerlaw_gamma, max_tries=100) G = nx.configuration_model(z) G = [n for n in nx.connected_component_subgraphs(G)][0] G.remove_edges_from(G.selfloop_edges()) return G
def calc_localization(Gint,genes_focal,write_file_name='localization_results',num_reps=5,num_genes=20, conserve_heat=True, replace=True,subsample=True, savefile=True): seed_FOCAL = list(np.intersect1d(list(genes_focal),Gint.nodes())) if subsample: num_genes_S=num_genes else: num_genes_S=len(seed_FOCAL) Wprime = normalized_adj_matrix(Gint,conserve_heat=conserve_heat) kurt_FOCAL =[] kurt_Srand=[] var_FOCAL, var_Srand=[],[] sumTop_FOCAL= [] sumTop_Srand= [] for r in range(num_reps): print(r) subset_FOCAL = np.random.choice(seed_FOCAL,size=num_genes_S,replace=replace) Fnew_FOCAL = network_propagation(Gint,Wprime,subset_FOCAL,alpha=.5,num_its=20) Fnew_FOCAL.sort() kurt_FOCAL.append(scipy.stats.kurtosis(Fnew_FOCAL)) var_FOCAL.append(np.var(Fnew_FOCAL)) sumTop_FOCAL.append(np.sum(Fnew_FOCAL.head(1000))) G_temp = nx.configuration_model(Gint.degree().values()) G_rand = nx.Graph() # switch from multigraph to digraph G_rand.add_edges_from(G_temp.edges()) # remove self-loops #G_rand.remove_edges_from(G_rand.selfloop_edges()) G_rand = nx.relabel_nodes(G_rand,dict(zip(range(len(G_rand.nodes())),Gint.degree().keys()))) Wprime_rand = normalized_adj_matrix(G_rand,conserve_heat=conserve_heat) Fnew_Srand = network_propagation(G_rand,Wprime_rand,subset_FOCAL,alpha=.5,num_its=20) Fnew_Srand.sort() kurt_Srand.append(scipy.stats.kurtosis(Fnew_Srand)) var_Srand.append(np.var(Fnew_Srand)) sumTop_Srand.append(np.sum(Fnew_Srand.head(1000))) print(var_FOCAL[-1]) print(var_Srand[-1]) results_dict = {'kurtosis':kurt_FOCAL,'kurt_rand':kurt_Srand, 'var':var_FOCAL,'var_rand':var_Srand, 'sumTop':sumTop_FOCAL, 'sumTop_rand':sumTop_Srand, 'num_reps':num_reps, 'conserve_heat':conserve_heat, 'replace':replace,'subsample':subsample,'num_genes':num_genes} if savefile: json.dump(results_dict,open(write_file_name,'w')) return results_dict
def generate_graph(gamma, kmin, kmax, dk): # map probability to power law: p = np.random.random(graph_size) C = (-gamma+1) / ((kmax+dk)**(-gamma+1) - (kmin+dk)**(-gamma+1)) k = ((-gamma+1)/C * p + (kmin+dk)**(-gamma + 1)) ** (1/(-gamma+1)) - dk sequence = map(int, sorted(np.round(k))) # map sorted intervalls to int if np.mod(np.sum(sequence), 2) != 0: # if odd, get rid of last element sequence[-1]-=1 G = nx.configuration_model(sequence) G = nx.Graph(G) # remove multiple connections return G
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])
def connect_configuration(self,N,degseq): """ Uses the configuration model to wire up a pulse oscillator network with a given input degree sequence. The length of degseq must equal N. The resulting network is pruned of both self loops and parallel (multi) edges. """ assert len(degseq) == N, "ERROR. Each node needs an input degree." self.connect_empty(N) G = nx.configuration_model(degseq) G = nx.Graph(G) G.remove_edges_from(G.selfloop_edges()) self.add_edges_from(G.edges())
def create_star(self,node): sequence = create_degree_sequence(node, powerlaw_sequence, exponent=2.5) graph = nx.configuration_model(sequence) loops = graph.selfloop_edges() graph = nx.Graph(graph) graph.remove_edges_from(loops) components = sorted(nx.connected_components(graph), key=len, reverse=True) lcc = graph.subgraph(components[0]) #pos = nx.spring_layout(lcc) #nx.draw_networkx(lcc, pos) graph = list(nx.generate_edgelist(lcc)) edges = lcc.edges() #print(edges) flat = list(sum(edges, ())) return edges, max(flat,key=flat.count)
def DegreeSequenceConfigurationModel(deg_sequence, seed=None): """ Returns a random pseudograph with the given degree sequence. Raises a NetworkX error if the proposed degree sequence cannot be that of a graph with multiple edges and loops. One requirement is that the sum of the degrees must be even, since every edge must be incident with two vertices. INPUT: - ``deg_sequence`` - a list of integers with each entry corresponding to the expected degree of a different vertex. - ``seed`` - a ``random.Random`` seed or a Python ``int`` for the random number generator (default: ``None``). EXAMPLES:: sage: G = graphs.DegreeSequenceConfigurationModel([1,1]) sage: G.adjacency_matrix() [0 1] [1 0] Note: as of this writing, plotting of loops and multiple edges is not supported, and the output is allowed to contain both types of edges. :: sage: G = graphs.DegreeSequenceConfigurationModel([3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) sage: len(G.edges()) 30 sage: G.show() # long time REFERENCE: [New2003]_ """ if seed is None: seed = int(current_randstate().long_seed() % sys.maxsize) import networkx return Graph(networkx.configuration_model([int(i) for i in deg_sequence], seed=seed), loops=True, multiedges=True, sparse=True)
def topology_chars(self): """ Calculates clustering coefficient, average shortest path length, small-worldness and compares them with random graph. """ Gcc = sorted(nx.connected_components(self.G), key=len, reverse=True) G0 = self.G.subgraph(Gcc[0]) H = nx.gnm_random_graph(self.G.number_of_nodes(), self.G.number_of_edges()) #for clustering F = nx.configuration_model([d for v, d in self.G.degree() ]) #for shortest path length Fcc = sorted(nx.connected_components(F), key=len, reverse=True) F0 = F.subgraph(Fcc[0]) cc = nx.average_clustering(self.G) cc_r = nx.average_clustering(H) # cc_r = nx.average_clustering(F) asph = nx.average_shortest_path_length(G0) asph_r = nx.average_shortest_path_length(F0) sigma = (cc / cc_r) / (asph / asph_r) omega = asph_r / asph - cc / cc_r title = 'Topological Charateristics.txt' topol_file = open(self.path + title, 'w') topol_file.write('Size of the Giant Component is: ' + str(G0.number_of_nodes()) + ' with ' + str(G0.number_of_edges()) + ' edges' + '\n') topol_file.write('Average Shortert Path Length' + '\n') topol_file.write(str(asph) + '\n') topol_file.write('Clustering Coeficient' + '\n') topol_file.write(str(cc) + '\n') topol_file.write('Small-Worldness Sigma' + '\n') topol_file.write(str(sigma) + '\n') topol_file.write('Small-Worldness Omega' + '\n') topol_file.write(str(omega) + '\n') topol_file.write('\n') topol_file.write('The Corresponding Random Graph Has:' + '\n') topol_file.write('Shortert Path Length: ' + str(asph_r) + '\n') topol_file.write('Clustering Coeficient: ' + str(cc_r) + '\n' + '\n') topol_file.write( 'A graph is commonly classified as small-world if Small-Worldness Sigma is bigger than 1. \n\n' ) topol_file.write( 'The small-world coefficient Omega ranges between -1 and 1.\nValues close to 0 means the G features small-world characteristics.\nValues close to -1 means G has a lattice shape whereas values close to 1 means G is a random graph.' ) topol_file.close() return asph, cc, asph_r, cc_r, sigma, omega
def DegreeSequenceConfigurationModel(deg_sequence, seed=None): """ Returns a random pseudograph with the given degree sequence. Raises a NetworkX error if the proposed degree sequence cannot be that of a graph with multiple edges and loops. One requirement is that the sum of the degrees must be even, since every edge must be incident with two vertices. INPUT: - ``deg_sequence`` - a list of integers with each entry corresponding to the expected degree of a different vertex. - ``seed`` - for the random number generator. EXAMPLES:: sage: G = graphs.DegreeSequenceConfigurationModel([1,1]) sage: G.adjacency_matrix() [0 1] [1 0] Note: as of this writing, plotting of loops and multiple edges is not supported, and the output is allowed to contain both types of edges. :: sage: G = graphs.DegreeSequenceConfigurationModel([3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) sage: sorted(G.edges(labels=False)) [(0, 2), (0, 10), (0, 15), (1, 6), (1, 16), (1, 17), (2, 5), (2, 19), (3, 7), (3, 14), (3, 14), (4, 9), (4, 13), (4, 19), (5, 6), (5, 15), (6, 11), (7, 11), (7, 17), (8, 11), (8, 18), (8, 19), (9, 12), (9, 13), (10, 15), (10, 18), (12, 13), (12, 16), (14, 17), (16, 18)] sage: G.show() # long time REFERENCE: .. [Newman2003] Newman, M.E.J. The Structure and function of complex networks, SIAM Review vol. 45, no. 2 (2003), pp. 167-256. """ if seed is None: seed = current_randstate().long_seed() import networkx return Graph(networkx.configuration_model([int(i) for i in deg_sequence], seed=seed), loops=True, multiedges=True, sparse=True)
def make_graph_with_same_degree_dist(G): G_sequence = list(d for n, d in G.degree()) G_sequence.sort() sorted_G_sequence = list((d, n) for n, d in G.degree()) sorted_G_sequence.sort(key=lambda tup: tup[0]) done = False while not done: G_prime = nx.configuration_model(G_sequence) G_prime = nx.Graph(G_prime) G_prime.remove_edges_from(G_prime.selfloop_edges()) tries = 10 while tries > 0 and (len(G.edges()) != len(G_prime.edges())): sorted_G_prime_sequence = list((d, n) for n, d in G_prime.degree()) sorted_G_prime_sequence.sort(key=lambda tup: tup[0]) #print("Sorted G_sequence:") #print(sorted_G_sequence) #print("Sorted G_prime_sequence:") #print(sorted_G_prime_sequence) missing = [] for i in range(0, len(G.nodes())): while sorted_G_sequence[i][0] > sorted_G_prime_sequence[i][0]: missing.append(sorted_G_prime_sequence[i][1]) sorted_G_prime_sequence[i] = ( sorted_G_prime_sequence[i][0] + 1, sorted_G_prime_sequence[i][1]) missing = np.random.permutation(missing) if len(missing) % 2 != 0: print("Sanity issue! Alert!") #print("Edges before:") #print(G_prime.edges()) #print("Missing:") #print(missing) for i in range(0, int(len(missing) / 2)): G_prime.add_edge(missing[2 * i], missing[2 * i + 1]) G_prime = nx.Graph(G_prime) G_prime.remove_edges_from(G_prime.selfloop_edges()) #print("Edges after:") #print(G_prime.edges()) if not is_connected(G_prime): # print("Bad: G_prime disconnected") pass tries -= 1 if not is_connected(G_prime): pass elif len(G.edges()) == len(G_prime.edges()): #print("Graph creation successful") done = True return G_prime
def DegreeSequenceConfigurationModel(deg_sequence, seed=None): """ Returns a random pseudograph with the given degree sequence. Raises a NetworkX error if the proposed degree sequence cannot be that of a graph with multiple edges and loops. One requirement is that the sum of the degrees must be even, since every edge must be incident with two vertices. INPUT: - ``deg_sequence`` - a list of integers with each entry corresponding to the expected degree of a different vertex. - ``seed`` - for the random number generator. EXAMPLES:: sage: G = graphs.DegreeSequenceConfigurationModel([1,1]) sage: G.adjacency_matrix() [0 1] [1 0] Note: as of this writing, plotting of loops and multiple edges is not supported, and the output is allowed to contain both types of edges. :: sage: G = graphs.DegreeSequenceConfigurationModel([3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) sage: G.edges(labels=False) [(0, 2), (0, 10), (0, 15), (1, 6), (1, 16), (1, 17), (2, 5), (2, 19), (3, 7), (3, 14), (3, 14), (4, 9), (4, 13), (4, 19), (5, 6), (5, 15), (6, 11), (7, 11), (7, 17), (8, 11), (8, 18), (8, 19), (9, 12), (9, 13), (10, 15), (10, 18), (12, 13), (12, 16), (14, 17), (16, 18)] sage: G.show() # long time REFERENCE: .. [Newman2003] Newman, M.E.J. The Structure and function of complex networks, SIAM Review vol. 45, no. 2 (2003), pp. 167-256. """ if seed is None: seed = current_randstate().long_seed() import networkx return Graph(networkx.configuration_model([int(i) for i in deg_sequence], seed=seed), loops=True, multiedges=True, sparse=True)
def power_law_graph(x_min, power_law_exponent, size_of_net): degree_sequence = [1] #~ G = nx.fast_gnp_random_graph(1, 1) #~ while min(G.degree().values()) != x_min: while sum(degree_sequence) % 2 == 1: degree_sequence = [] for i in range(size_of_net): degree_sequence.append( int(pl_gen.random_power_no(x_min, power_law_exponent))) G = nx.configuration_model(degree_sequence) G = nx.Graph(G) G.remove_edges_from(G.selfloop_edges()) return G
def perturbed_graph(seed_graph): ''' Returns random graph with approximately the same degree distribution as given seed graph ''' # Generate new graph with given degree sequence deg_sequence = shuffle(list(seed_graph.degree)) new_G = nx.configuration_model(list(zip(*deg_sequence))[1]) # Rename graph with nodes in seed_graph with corresponding degrees deg2names = get_degree_to_names(deg_sequence) mapping = get_degree_preserving_relabelling(deg2names, new_G) new_G = nx.relabel_nodes(new_G, mapping) # Remove self loops and parallel edges and return return util.simple_two_core(nx.Graph(new_G), verbose=False)
def experiment(n_tries): samples = 5 G_list = list() #array to hold the average_clustering coeff for all the n_tries data = np.zeros(n_tries) for i in range(n_tries): for j in range(samples): G = nx.configuration_model(deg_seq) G = nx.Graph(G) G_list.append(G) G = random.choice(G_list) average_clustering = nx.average_clustering(G) data[i] = average_clustering return data
def generate_graph_mean(degree_start=10, degree_end=15, size=5000): z=[numpy.random.geometric(0.07) for i in range(size)] print("Mean degree", numpy.mean(z)) # if sum of degree sequence is odd, make it even if sum(z) % 2 != 0: z[random.randint(0, size)] += 1 G=nx.configuration_model(z) # remove self-loops and parallel edges G=nx.Graph(G) G.remove_edges_from(G.selfloop_edges()) return G
def __init__(self, f, s, r, graph_type="Regular", rows=-1, cols=-1, num_hubs=-1): self.G = None self.F = set([]) self.owners_list = [] self.collected = [] self.path = [] self.batch_nums = [] self.fig, self.pos, self.ax = None, None, None self.ani_title = "" self.num_times = 0 self.ani = None self.f = f self.s = s self.r = r self.graph_type = graph_type self.num_hubs = num_hubs if graph_type == "Regular": self.G = self.make_reg_graph(f, s, r) elif graph_type == "Euclidean": self.s = rows * cols self.G = nx.grid_2d_graph(rows, cols) elif graph_type == "Network": assert (num_hubs > 0) # sequence = nx.generators.degree_seq.create_degree_sequence(s, nx.utils.powerlaw_sequence) # self.G = nx.configuration_model(sequence) # Check all combinations of feasible degrees to see if we can make a valid graph hub_degrees = range((2 * s) // num_hubs, (3 * s) // num_hubs) non_hub_degrees = range(1, 4) for degree_seq in itertools.product( *[hub_degrees for _ in range(num_hubs)], *[non_hub_degrees for _ in range(s - num_hubs)]): if sum(degree_seq) % 2 != 0: continue G_curr = nx.configuration_model(degree_seq) if nx.is_connected(G_curr): self.G = G_curr break assert (self.G != None) self.F = set(random.sample(self.G.nodes, f))
def save_power_law(n, exponent, save=False): sequence = nx.random_powerlaw_tree_sequence(n=n, gamma=exponent, tries=50000) graph = nx.configuration_model(sequence) print(sequence) adj = nx.adjacency_matrix(graph) adj[range(n), range(n)] = 1 #adding self-loops to the adj matrix # print('avg path length: ', nx.average_shortest_path_length(graph)) print('density: ', nx.density(graph)) if save: # average_shortest_path_length = nx.average_shortest_path_length(graph) print('saving...') adjacency_matrix_file = 'power_law_n_{}_exp_{}.pickle'.format( n, exponent) with open(adjacency_matrix_file, 'wb') as handle: pickle.dump(adj, handle, -1)
def degree_sequence_graphs(): print("Degree sequence") print("Configuration model") z = nx.utils.create_degree_sequence(30, powerlaw_sequence) G = nx.configuration_model(z) draw_graph(G) # to remove paralel edges G = nx.Graph(G) draw_graph(G) # to remove self loops G.remove_edges_from(G.selfloop_edges()) draw_graph(G) print("Directed configuration model") D = nx.DiGraph([(0, 1), (1, 2), (2, 3), (1, 3)]) # directed path graph din = list(D.in_degree().values()) dout = list(D.out_degree().values()) din.append(1) dout[0] = 2 D = nx.directed_configuration_model(din, dout) D = nx.DiGraph(D) # to remove paralell edges D.remove_edges_from(D.selfloop_edges()) # to remove self loops draw_graph(D) print("Expected degree graphs") z = [i for i in range(10)] G = nx.expected_degree_graph(z) draw_graph(G) print("Havel Hakimi graphs") z = [2, 4, 5, 6, 7, 3] G = nx.havel_hakimi_graph(z) draw_graph(G) print("Directed Havel Hakimi graphs") inds = [2, 4, 5, 6, 7, 3] outds = [2, 4, 5, 6, 7, 3] G = nx.directed_havel_hakimi_graph(in_deg_sequence=inds, out_deg_sequence=outds) draw_graph(G) print("Degree Sequence Tree") dg = [1, 2, 3, 4, 2, 3] G = nx.degree_sequence_tree(dg) draw_graph(G) print("Random Degree Sequence") sequence = [1, 2, 2, 3] G = nx.random_degree_sequence_graph(sequence) sorted(G.degree().values()) draw_graph(G)
def DegreeSequenceConfigurationModel(deg_sequence, seed=None): """ Returns a random pseudograph with the given degree sequence. Raises a NetworkX error if the proposed degree sequence cannot be that of a graph with multiple edges and loops. One requirement is that the sum of the degrees must be even, since every edge must be incident with two vertices. INPUT: - ``deg_sequence`` - a list of integers with each entry corresponding to the expected degree of a different vertex. - ``seed`` - a ``random.Random`` seed or a Python ``int`` for the random number generator (default: ``None``). EXAMPLES:: sage: G = graphs.DegreeSequenceConfigurationModel([1,1]) sage: G.adjacency_matrix() [0 1] [1 0] Note: as of this writing, plotting of loops and multiple edges is not supported, and the output is allowed to contain both types of edges. :: sage: G = graphs.DegreeSequenceConfigurationModel([3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]) sage: len(G.edges()) 30 sage: G.show() # long time REFERENCE: .. [Newman2003] Newman, M.E.J. The Structure and function of complex networks, SIAM Review vol. 45, no. 2 (2003), pp. 167-256. """ if seed is None: seed = int(current_randstate().long_seed() % sys.maxsize) import networkx return Graph(networkx.configuration_model([int(i) for i in deg_sequence], seed=seed), loops=True, multiedges=True, sparse=True)
def buildGraphFromDegreeInfo(self): if self.read: self.readGraph() if self.G != None: degreeSequence = sorted([d for n, d in self.G.degree()], reverse=True) degreeCount = dict(collections.Counter(degreeSequence)) for d in range(1, max(degreeSequence) + 1): degreeCount[ d] = degreeCount[d] / self.n if d in degreeCount else 0 self.degreeDistribution = degreeCount return if self.degreeDistribution != None: degreeSequence = [] temp = 0 for i in range(self.n - 1): d = utils.sampleDegreeFromDistribution(self.degreeDistribution) degreeSequence.append(d) temp += d d = utils.sampleDegreeFromDistribution(self.degreeDistribution) while (temp + d) % 2 != 0: d = utils.sampleDegreeFromDistribution(self.degreeDistribution) degreeSequence.append(d) temp += d self.degreeSequence = degreeSequence if self.degreeSequence != None: self.G = nx.configuration_model(self.degreeSequence) self.degreeSequence = [] for i in range(self.n): self.degreeSequence.append(len(self.G[i])) self.G.nodes[i]["id"] = i self.G.nodes[i]["products"] = set([0, 1]) self.G.nodes[i]["payoff"] = 0 degreeCount = dict(collections.Counter(self.degreeSequence)) for d in range(1, self.n): degreeCount[ d] = degreeCount[d] / self.n if d in degreeCount else 0 self.degreeDistribution = degreeCount print(self.degreeSequence) print(len(self.degreeSequence))
def pvalue(G, comms, LLRtest, L=3000, plothist=False): ''' Compute the distribution of the test statistic in a range. Warning: Enumerating all Null networks is very slow. Do not call this function in practical community detection. Use threshold on the LR test statistic directly. Args: G: input networkx graph instance comms: the suggested partiton, list of lists LLRtest: the log-likelihood ratio test statistics L: number of synthetic null networks (default 3000) Returns: (float): pvalue of LLRtest ''' node_seq, deg_seq = zip(*list(G.degree())) index = {n: i for i, n in enumerate(node_seq)} # nodes should be index-0 comms = [list(map(index.get, c)) for c in comms] null_distri = [] for niter in range(L): # debug if niter % 100 == 1: print("iter", niter, "H0 LLR", LRnull) # generate configuration network F = nx.Graph(nx.configuration_model(deg_seq)) # obtain test statistic on null network LRnull = _2ll(F, comms) null_distri.append(LRnull) pval = sum([LLRnull > LLRtest for LLRnull in null_distri]) / float(len(null_distri)) # plot if plothist: print("plotting") hist(null_distri, LLRtest, "%d_%d" % (len(comms), G.number_of_nodes()), pval) return pval
def GraphType(num_nodes, str, p=0.05, m=3): """ :param num_nodes: the number of nodes of the graph (if that option is available) :param str: the type of graph that is used. We have 'erdos' an erdos renyi graph 'powerlaw' a graph with powerlaw degree distribution 'enron' a social network graph loaded from http://snap.stanford.edu/data/email-Enron.html. (36692 nodes) 'karateclub' some karate club graph 'women' women social network :return: the graph """ if str == 'erdos': graph = nx.erdos_renyi_graph(num_nodes, p) elif str == 'powerlaw': graph = nx.powerlaw_cluster_graph(num_nodes, m, p) elif str == 'enron': graph = nx.Graph() edges = np.loadtxt('Enron.txt',skiprows=4) graph.add_edges_from(edges) elif str == 'karateclub': graph = nx.karate_club_graph() elif str == 'women': graph = nx.davis_southern_women_graph() elif str == 'pair': graph = nx.DiGraph() graph.add_edge(0,1) graph.add_edge(1,0) elif str == 'star': graph = nx.star_graph(num_nodes) elif str == 'cycle': graph = nx.cycle_graph(num_nodes) elif str == 'config': max_degree = int(num_nodes/5) #Create some degrees degrees = np.asarray(np.round(np.exp(np.log(max_degree) * np.random.uniform(size=num_nodes))), np.int) #Ensure the total number of degrees is even if sum(degrees) % 2 != 0: degrees[np.random.randint(num_nodes)] += 2 * np.random.randint(2) - 1 #Create a graph and apply the configuration model graph = nx.Graph() graph = nx.configuration_model(degrees, graph) graph = graph.to_directed() return graph
def DegreeSequenceConfigurationModel(deg_sequence, seed=None): """ Return a random pseudograph with the given degree sequence. This method raises a NetworkX error if the proposed degree sequence cannot be that of a graph with multiple edges and loops. One requirement is that the sum of the degrees must be even, since every edge must be incident with two vertices. INPUT: - ``deg_sequence`` -- list of integers with each entry corresponding to the expected degree of a different vertex - ``seed`` -- (optional) a ``random.Random`` seed or a Python ``int`` for the random number generator EXAMPLES:: sage: G = graphs.DegreeSequenceConfigurationModel([1,1]) sage: G.adjacency_matrix() [0 1] [1 0] The output is allowed to contain both loops and multiple edges:: sage: deg_sequence = [3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3] sage: G = graphs.DegreeSequenceConfigurationModel(deg_sequence) sage: G.order(), G.size() (20, 30) sage: G.has_loops() or G.has_multiple_edges() # random True sage: G.show() # long time REFERENCE: [New2003]_ """ if seed is None: seed = int(current_randstate().long_seed() % sys.maxsize) import networkx deg_sequence = [int(i) for i in deg_sequence] return Graph(networkx.configuration_model(deg_sequence, seed=seed), loops=True, multiedges=True, sparse=True)
def _gen(self, gname: str, gen_id: int) -> nx.Graph: assert 'degree_seq' in self.params, 'imporper parameters for Chung-Lu' try: g = nx.configuration_model( self.params['degree_seq']) # fit the model to the degree seq except nx.NetworkXError: # config model failed raise Exception('Generation failed!') else: # gets called only if the exception is not thrown g = nx.Graph(g) # make it into a simple graph g.remove_edges_from(nx.selfloop_edges(g)) # remove self-loops g.name = gname g.gen_id = gen_id return g
def under_weighted_model(G,reps): lmbda=1 old_clustering=nx.clustering(G).values() degree=G.degree().values() clust_mat=np.zeros((reps,G.number_of_nodes())) for i in xrange(reps): C=nx.configuration_model(degree) C=nx.Graph(C) #remove parallel edges C.remove_edges_from(C.selfloop_edges()) #remove self-loops for e in C.edges(): C.edge[e[0]][e[1]]['weight']=np.random.poisson()+1 #start with 1 clustering=nx.clustering(C, weight='weight') clust_mat[i]=clustering.values() means=np.mean(clust_mat, axis=0) #mean over iterations for each node per25=np.percentile(clust_mat, 25, axis=0) per75=np.percentile(clust_mat, 75, axis=0) return [old_clustering-means, old_clustering-per25, old_clustering-per75]
def scale_free_network(number_of_nodes, powerlaw_exponent): sequence = nx.utils.random_sequence.powerlaw_sequence( number_of_nodes, powerlaw_exponent) sequence = np.round(sequence).astype(int) # Ensure that the total number of stubs is even if (sequence.sum() % 2) != 0: sequence[np.random.randint(number_of_nodes)] += 1 # Initiate the network network = nx.configuration_model(sequence) adjacency = adjacency_from_edges(list(network.edges()), number_of_nodes) adjacency = clean_adjacency(adjacency) adjacency = sparse.csr_matrix(adjacency) return adjacency
def normalise(G, func, n=500, retNorm=True, inVal=None, weight='weight'): """ This function normalises the function G by generating a series of n random graphs and averaging the results. If retNorm is specified, the normalised value is returned, else a list of n values for a random graph are returned. """ vals = [] for i in range(n): rand = configuration_model(G.degree().values()) rand = nx.Graph(rand) # convert to simple graph from multigraph # check whether the graph is weighted # if so, the algorithm randomly reassigns weights from the input graph to the random graph node by node # this means that for each node the total weighted degree will be similar # nb this depends on the random graph having the same or fewer vertices for each node if isinstance(G.degree(weight=weight)[0], float): randDegs = rand.degree() for node in rand.nodes(): edgeVals = [v[2][weight] for v in G.edges(G.nodes()[node],data=True)] shuffle(edgeVals) for en,edge in enumerate(rand.edges(node)): rand.edge[edge[0]][edge[1]]['weight'] = edgeVals[en] # remove unconnected nodes nodeList = [rand.degree()[v] for v in rand.nodes() if rand.degree(v)==0] rand.remove_nodes_from(nodeList) # add spatial information if necessary if spatial: print "Adding spatial info" nx.set_node_attributes(rand, 'xyz', {rn:G.node[v]['xyz'] for rn,v in enumerate(G.nodes())}) # copy across spatial information try: vals.append(func(rand),weight='weight') # collect values using the function except: vals.append(func(rand)) # collect values using the function if retNorm: # return the normalised values if not inVal: inVal = func(G) return(inVal/np.mean(vals)) else: # return a list of the values from the random graph return(vals)
def from_degree_distribution(N, degree_dist, return_pos=False): number_of_nodes = N * np.array(degree_dist) degree_sequence = [] for i in range(int(math.floor(len(number_of_nodes)))): number_with_that_degree = number_of_nodes[i] for k in range(int(math.floor(number_with_that_degree))): degree_sequence.append(i) graphical = nx.is_graphical(degree_sequence) if not graphical: degree_sequence.append(1) G = nx.configuration_model(degree_sequence) try: G.remove_edges_from(nx.selfloop_edges(G)) except RuntimeError: print('No self loops to remove') if return_pos: pos = nx.spring_layout(G) return G, pos return G, None
def power_law_graph_cuts(size_of_net, x_min, power_law_exponent): degree_sequence = [] norm_const = float(size_of_net) / sum( [var**(-power_law_exponent) for var in range(x_min, size_of_net)] ) #The maximum degree fo an undirected simple graph is N-1 where N is size_of_net. for i in range(x_min, size_of_net): for j in range(int(round(norm_const * i**(-power_law_exponent)))): degree_sequence.append(i) if sum(degree_sequence) % 2 != 0: if x_min % 2 != 0: degree_sequence.append(x_min) else: degree_sequence.append(x_min + 1) G = nx.configuration_model(degree_sequence) G = nx.Graph(G) G.remove_edges_from(G.selfloop_edges()) return G
def randomized_graph(G,num_randomization=None): ''' Input: G: an undirected weighted network IR_weight_cutoff: threshold on the minimum weight to reach Output: E: an undirected weighted graph with the same degree and weight sequence of G. ''' if num_randomization==None: num_randomization=G.number_of_edges(); max_index=0; print('Begin creation corresponding configuration model...'); weight_dictionary=nx.get_edge_attributes(G,'weight'); weight_sequence=list(weight_dictionary.values()); degree_sequence=list(nx.degree(G).values()); #preliminary scan of edge weights to define filtration steps print('Preliminary scan of edge weights to define filtration steps...'); edge_weights=[]; edge_weights=list(set(nx.get_edge_attributes(G,'weight').values())); edge_weights=sorted(edge_weights, reverse=True); print('Preliminary scan and sorting completed.') rn.seed(rn.randint(0,1000000)+time.time()); E=nx.configuration_model(degree_sequence); E=nx.Graph(E); E.remove_edges_from(E.selfloop_edges()) weight_sequence_temp=weight_sequence; rn.shuffle(weight_sequence_temp); print('Setting new weights.'); for e in E.edges_iter(): E.edge[e[0]][e[1]]['weight']=weight_sequence_temp[0]; weight_sequence_temp=weight_sequence_temp[1:]; print('Weights setup completed.'); return E
def min_degree_sbm(num_nodes: int, num_blocks: int, p_in: float, p_out: float, min_degree: int, seed: int = 0) -> nx.Graph: """ This function creates a minimum degree SBM approximating the input parameters. This requries that num_nodes / num_blocks is an integer division result. Note that when min_degree was 2, this failed on the assert for actual_degrees. I don't know why, so we may need to remove that assert. """ # First make the random graph with minimum-degree: degree_list = [min_degree for i in range(num_nodes)] rnd_G = nx.configuration_model(degree_list, seed=seed) # The returned graph is a multigraph, which may have parallel edges. To remove any # parallel edges from the returned graph... rnd_G = nx.Graph(rnd_G) # check actual_degrees = [d for v, d in rnd_G.degree()] for i in range(len(actual_degrees)): if actual_degrees[i] != degree_list[i]: print('At %d, %d != %d' % (i, actual_degrees[i], degree_list[i])) assert actual_degrees == degree_list, 'Random graph generator failed' # Make the SBM num_nodes_per_block = int(num_nodes / num_blocks) assert num_nodes_per_block * num_blocks == num_nodes, 'Input number of nodes not '\ 'divisible by number of blocks' sizes = [num_nodes_per_block for b in range(num_blocks)] probs = p_out * np.ones((num_blocks, num_blocks)) for i in range(num_blocks): probs[i][i] = p_in SBM_G = nx.stochastic_block_model(sizes, probs, seed=seed) # Now merge them and return return merge_graphs(SBM_G, rnd_G)
def configuration_model(G, verbose=True): """ Creates a random graph with the same degree sequence as G. Arguments: G (networkx.Graph): Graph for which the degree sequence is from Returns: A random graph with the same degree sequence as G (configuration model) """ deg_sequence = [] for nid in G.nodes(): deg_sequence.append(G.degree(nid)) config = nx.configuration_model(deg_sequence) if verbose: print("Number of nodes: {}".format(nx.number_of_nodes(config))) print("Number of edges: {}".format(nx.number_of_edges(config))) return config
def test_havel_hakimi_construction(): G = nx.havel_hakimi_graph([]) assert len(G) == 0 z = [1000, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z) z = ["A", 3, 3, 3, 3, 2, 2, 2, 1, 1, 1] pytest.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] pytest.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) pytest.raises(nx.NetworkXError, nx.havel_hakimi_graph, z, create_using=nx.DiGraph())
def sf_net(n, gamma, k_min): deg = powerlaw_degree_sequence(n, gamma, k_min) # ================================================================================ # "sum of all degrees" must be even. Why is that? if sum(deg) % 2 == 1: # if sum of "deg" is odd deg[0] += 1 # make it "even" # ================================================================================ # configure the model with "power-law distributed deg" G = nx.configuration_model(deg) # ================================================================================ H = nx.Graph(G) # ================================================================================ H.remove_edges_from(H.selfloop_edges()) # ================================================================================ return H
def get_C_rand_CM(degSeq, samples=10, package='nx', verbose=False): C_values = np.zeros(samples) Cws_values = np.zeros(samples) for i in range(samples): if verbose: print('rand CM, it', i) if package == 'nx': g = nx.configuration_model(degSeq, seed=i) g = nx.Graph(g) C = nx.transitivity(g) Cws = nx.average_clustering(g) elif package == 'ig': g = ig.Graph().Degree_Sequence(degSeq, method='vl') C = g.transitivity_undirected(mode='zero') Cws = g.transitivity_avglocal_undirected(mode='zero') else: print('ERROR: Package "{}" not supported'.format(package)) C_values[i] = C Cws_values[i] = Cws return C_values, Cws_values
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())
def random_net(G=None): ''' Random the network but keep the original node degree roughly @parameter G - graph @return GG - random graph that only contains genes ''' sequence = [d for (_, d) in G.degree] GG = nx.configuration_model(sequence) GG = nx.Graph(GG) GG.remove_edges_from(GG.selfloop_edges()) # re-label mapping = {i: j for i, j in enumerate(G.nodes)} GG = nx.relabel_nodes(GG, mapping, copy=False) for node in GG.nodes: GG.nodes[node]['type'] = 'gene' return GG
def __graphlet_configuration_model(self, G_in): """ Converts network input network into graph sequence and generates new configuration graph. Number of edges is not conserved! """ if G_in.is_directed(): inseq = G_in.in_degree().values() outseq = G_in.out_degree().values() H = nx.directed_configuration_model(inseq, outseq) H = nx.DiGraph(H) H.remove_edges_from(H.selfloop_edges()) else: seq = G_in.degree().values() H = nx.configuration_model(seq) H = nx.Graph(H) H.remove_edges_from(H.selfloop_edges()) return H.edges()
def build_random_network(model_path, name="random-network.txt"): """ Generates a random network with degree sequence matching the network at model_path. Args: model_path (string) """ model_networkx, _, protein_to_node = load_network(model_path) node_to_protein = { node: protein for protein, node in protein_to_node.items() } deg_sequence = np.array(model_networkx.degree())[:, 1] random_network = nx.configuration_model(deg_sequence, create_using=nx.Graph) with open(os.path.join("data/networks/", name), 'w') as file: for edge in random_network.edges(): node_1, node_2 = edge[0], edge[1] protein_1, protein_2 = node_to_protein[node_1], node_to_protein[ node_2] file.write(str(protein_1) + " " + str(protein_2) + '\n')
def generate_graph(type='PL', n=100, seed=1.0, parameter=2.1): if type == 'ER': G = nx.erdos_renyi_graph(n, p=parameter, seed=seed, directed=True) G = nx.DiGraph(G) G.remove_edges_from(G.selfloop_edges()) elif type == 'PL': z = nx.utils.create_degree_sequence(n, nx.utils.powerlaw_sequence, exponent=parameter) while not nx.is_valid_degree_sequence(z): z = nx.utils.create_degree_sequence(n, nx.utils.powerlaw_sequence, exponent=parameter) G = nx.configuration_model(z) G = nx.DiGraph(G) G.remove_edges_from(G.selfloop_edges()) elif type == 'BA': G = nx.barabasi_albert_graph(n, 3, seed=None) G = nx.DiGraph(G) elif type == 'grid': G = nx.grid_2d_graph(int(np.ceil(np.sqrt(n))), int(np.ceil(np.sqrt(n)))) G = nx.DiGraph(G) elif type in [ 'facebook', 'enron', 'twitter', 'students', 'tumblr', 'facebookBig' ]: #print 'start reading' #_, G, _, _ = readRealGraph(os.path.join("..","..","Data", type+".txt")) _, G, _, _ = readRealGraph(os.path.join("..", "Data", type + ".txt")) print 'size of graph', G.number_of_nodes() #Gcc = sorted(nx.connected_component_subgraphs(G.to_undirected()), key = len, reverse=True) #print Gcc[0].number_of_nodes() #print 'num of connected components', len(sorted(nx.connected_component_subgraphs(G.to_undirected()), key = len, reverse=True)) #exit() if G.number_of_nodes() > n: G = getSubgraph(G, n) #G = getSubgraphSimulation(G, n, infP = 0.3) #nx.draw(G) #plt.show() return G
def job(degree_sequence): try: G = nx.configuration_model(degree_sequence) G = nx.Graph(G) # to remove patallel edges G.remove_edges_from(G.selfloop_edges()) distinct_prop = distinct(G) only_best_list = [i for x in zip(*distinct_prop)[1] if len(x)==1 for i in x] if len(set(only_best_list)) >= 2: len_best = [len(x) for x in zip(*distinct_prop)[1]] num_uniq = len(set([i for x in zip(*distinct_prop)[1] for i in x])) if sum(len_best) < 7 and len_best.count(0) == 0 and num_uniq >= 3: with open("result.txt", "a") as f: print "" print num_uniq, zip(*distinct_prop) print G.nodes(), G.edges() print "" print>>f, num_uniq, zip(*distinct_prop) print>>f, G.nodes(), G.edges() except nx.exception.NetworkXError, e: #print e, degree_sequence pass
def test_SIR_final_sizes(self): print("test_SIR_final_sizes") plt.clf() G = nx.configuration_model([3, 6, 3, 6, 20] * 10000) N = G.order() tau = 0.2 gamma = 1 t, S, I, R = EoN.fast_SIR(G, tau, gamma, initial_infecteds=range(5000)) plt.plot(t, S, color=colors[0], label='simulation', alpha=0.3) plt.plot(t, I, color=colors[0], alpha=0.3) plt.plot(t, R, color=colors[0], alpha=0.3) infected_nodes = EoN.get_infected_nodes(G, tau, gamma, initial_infecteds=range(5000)) A = len(infected_nodes) print(A) plt.plot([0, 10], [A, A], label=r'percolation based', alpha=0.3) A = EoN.Attack_rate_cts_time_from_graph(G, tau, gamma, rho=0.1) plt.plot([0, 10], [A * N, A * N], '-.', label='analytic', alpha=0.3) plt.legend(loc='upper right') plt.savefig('test_SIR_final_sizes')
def weight_preserving_configuration_model(G,filename=' '): import random as rn import time weight_dictionary=nx.get_edge_attributes(G,'weight'); weight_sequence=weight_dictionary.values(); degree_sequence=list(nx.degree(G).values()); rn.seed(rn.randint(0,1000000)+time.time()); E=nx.configuration_model(degree_sequence); E=nx.Graph(E); E.remove_edges_from(E.selfloop_edges()); weight_sequence_temp=weight_sequence; for t in range(100): rn.shuffle(weight_sequence_temp); for e in E.edges_iter(): E.edge[e[0]][e[1]]['weight']=weight_sequence_temp[0]; weight_sequence_temp=weight_sequence_temp[1:]; if filename!=' ': nx.write_weighted_edgelist(E, filename , delimiter=' ', encoding='utf-8') print('Randomized edgelist dumped to '+ filename); return E;