def update_all_parameter(diff): #print 'each difference - %s' % diff luc_node = int(30*diff) hcc_node = int(5) time = 10 #parameter luc_gro = int(6*diff) hcc_gro = int(2) lucG = nx.barabasi_albert_graph(luc_node, luc_gro) hccG = nx.barabasi_albert_graph(hcc_node, hcc_gro) frequency = np.array([0.9, 0.1]) G_combine =nx.Graph() G_combine = graph.merge_graph(G_combine, hccG, lucG, frequency) frequency_1 = np.array([0.5, 0.5]) G_combine_1 =nx.Graph() G_combine_1 = graph.merge_graph(G_combine_1, hccG, lucG, frequency_1) #Time series cell volume LucN = [] hccN = [] #Number of initial cell LucN0 = 100 hccN0 = 100 LucN_init = 100 hccN_init = 100 for t in range(time): LucN.append(calc.convert_volume(LucN0)) lucG = graph.update_graph(lucG, luc_gro) LucN0 = LucN_init*calc.calc_entropy(lucG, t+1) for t in range(time): hccN.append(calc.convert_volume(hccN0)) hccG = graph.update_graph(hccG, hcc_gro) hccN0 = hccN_init*calc.calc_entropy(hccG, t+1) #Mix Number of cell MixN0 = 100 MixN_init = 100 initial_populations = MixN0*frequency G_comb_gro = ((frequency*np.array([luc_gro, hcc_gro])).sum())/2 MixN = [] x = [] for t in range(time): x.append(t) MixN.append(calc.convert_volume(MixN0)) G_combine = graph.update_graph(G_combine, G_comb_gro) MixN0 = MixN_init*calc.calc_entropy(G_combine, t+1) sim_ratio = np.array(LucN)/np.array(MixN) return sim_ratio """
def setUp(self): self.G=nx.barabasi_albert_graph(1000, 10, 8) pth_graph=nx.path_graph(1000) edge=random.choice(pth_graph.edges()) pth_graph.remove_edge(edge[0], edge[1]) self.dG=pth_graph self.dG2=nx.barabasi_albert_graph(900, 10, 0) for i in xrange(900,1000): self.dG2.add_node(i)
def test_powerlaw_mle(): print 'Testing Power law MLE estimator' G = nx.barabasi_albert_graph(100, 5) print 'nn: %d, alpha: %f'%(G.number_of_nodes(),powerlaw_mle(G)) G = nx.barabasi_albert_graph(1000, 5) print 'nn: %d, alpha: %f'%(G.number_of_nodes(),powerlaw_mle(G)) G = nx.barabasi_albert_graph(10000, 5) print 'nn: %d, alpha: %f'%(G.number_of_nodes(),powerlaw_mle(G)) G = nx.barabasi_albert_graph(100000, 5) print 'nn: %d, alpha: %f'%(G.number_of_nodes(),powerlaw_mle(G)) print 'Expected: 2.9 (or thereabout)'
def main(): msg = "usage: ./p2p2012.py type r|g|g2 ttl par tries churn_rate" if len(sys.argv) < 7: print msg sys.exit(1) global out_file, churn_rate out_file = sys.stdout gtype = sys.argv[1] walk = sys.argv[2] ttl = int(sys.argv[3]) par = int(sys.argv[4]) tries = int(sys.argv[5]) churn_rate = float(sys.argv[6]) if gtype == "a": g = nx.barabasi_albert_graph(97134, 3) elif gtype == "b": g = nx.barabasi_albert_graph(905668, 12) elif gtype == "c": g = sm.randomWalk_mod(97134, 0.90, 0.23) elif gtype == "d": g = sm.randomWalk_mod(905668, 0.93, 0.98) elif gtype == "e": g = sm.nearestNeighbor_mod(97134, 0.53, 1) elif gtype == "f": g = sm.nearestNeighbor_mod(905668, 0.90, 5) elif gtype == "g": g = nx.random_regular_graph(6, 97134) elif gtype == "h": g = nx.random_regular_graph(20, 905668) elif gtype == "i": g = nx.read_edgelist(sys.argv[7]) if walk == "r": lookup(g, ttl, tries, par, get_random_node) elif walk == "g": lookup(g, ttl, tries, par, get_greedy_node) elif walk == "g2": lookup(g, ttl, tries, par, get_greedy2_node) elif walk == "sum": sum_edges(g, int(sys.argv[3])) nodes = g.number_of_nodes() edges = g.size() avg_cc = nx.average_clustering(g) print >> sys.stderr, nodes, edges, avg_cc
def test_networkx_matrix(self): print('\n---------- Matrix Test Start -----------\n') g = nx.barabasi_albert_graph(30, 2) nodes = g.nodes() edges = g.edges() print(edges) mx1 = nx.adjacency_matrix(g) fp = tempfile.NamedTemporaryFile() file_name = fp.name sp.savetxt(file_name, mx1.toarray(), fmt='%d') # Load it back to matrix mx2 = sp.loadtxt(file_name) fp.close() g2 = nx.from_numpy_matrix(mx2) cyjs_g = util.from_networkx(g2) #print(json.dumps(cyjs_g, indent=4)) self.assertIsNotNone(cyjs_g) self.assertIsNotNone(cyjs_g['data']) self.assertEqual(len(nodes), len(cyjs_g['elements']['nodes'])) self.assertEqual(len(edges), len(cyjs_g['elements']['edges'])) # Make sure all edges are reproduced print(set(edges)) diff = compare_edge_sets(set(edges), cyjs_g['elements']['edges']) self.assertEqual(0, len(diff))
def compare_graphs(graph): n = nx.number_of_nodes(graph) m = nx.number_of_edges(graph) k = np.mean(list(nx.degree(graph).values())) erdos = nx.erdos_renyi_graph(n, p=m/float(n*(n-1)/2)) barabasi = nx.barabasi_albert_graph(n, m=int(k)-7) small_world = nx.watts_strogatz_graph(n, int(k), p=0.04) print(' ') print('Compare the number of edges') print(' ') print('My network: ' + str(nx.number_of_edges(graph))) print('Erdos: ' + str(nx.number_of_edges(erdos))) print('Barabasi: ' + str(nx.number_of_edges(barabasi))) print('SW: ' + str(nx.number_of_edges(small_world))) print(' ') print('Compare average clustering coefficients') print(' ') print('My network: ' + str(nx.average_clustering(graph))) print('Erdos: ' + str(nx.average_clustering(erdos))) print('Barabasi: ' + str(nx.average_clustering(barabasi))) print('SW: ' + str(nx.average_clustering(small_world))) print(' ') print('Compare average path length') print(' ') print('My network: ' + str(nx.average_shortest_path_length(graph))) print('Erdos: ' + str(nx.average_shortest_path_length(erdos))) print('Barabasi: ' + str(nx.average_shortest_path_length(barabasi))) print('SW: ' + str(nx.average_shortest_path_length(small_world))) print(' ') print('Compare graph diameter') print(' ') print('My network: ' + str(nx.diameter(graph))) print('Erdos: ' + str(nx.diameter(erdos))) print('Barabasi: ' + str(nx.diameter(barabasi))) print('SW: ' + str(nx.diameter(small_world)))
def ba_network(p=25, n=150): """ Barabasi-Albert algorithm is used to generate a scale free network. The precision matrix is constructed as the above nearest neighbor based algorithm. We also set the degree as 5.""" m = 5 G = networkx.barabasi_albert_graph(p, m) # obtain networkx library graph G A = np.array(networkx.adjacency_matrix(G)) # numpy matrix is returned # We weight the edges using a random number in [-1.0, -0.5] \cup [0.5, 1.0] for i in range(p): for j in range(p): if A[i, j] > 0: A[i, j] = A[i, j] * np.random.uniform(0.5, 1.0) A[j, i] = A[i, j] # ensure symmetry A = (A + A.T) / 2.0 # Randomize sign A = A * pow(-1.0, np.random.random_integers(0, 1, [p, p])) # The diagonal entires are set to the sum of the absolute values of the row # then, we obtain precision matrix # I placed the factor 2 to ensure invertibility P = A + 0.25 * np.diag(np.sum(np.absolute(A), 1)) # normalize entries to make the diagonal elements equal to one P = P / np.diag(P) cov = np.linalg.inv(P) # covariance matrix # Sample from the covariance matrix samples = np.random.multivariate_normal(np.zeros(p), cov, n) return samples, cov
def test_random_model(): n_node = graph.number_of_nodes() n_edge = graph.number_of_edges() p = float(2 * n_edge) / (n_node*n_node - 2*n_node) #new_graph = networkx.erdos_renyi_graph(n_node, p) new_graph = networkx.barabasi_albert_graph(n_node, n_edge/n_node) mapping = dict(zip(new_graph.nodes(), graph.nodes())) new_graph = networkx.relabel_nodes(new_graph, mapping) return new_graph available_edges = graph.edges() for edge in new_graph.edges(): if len(available_edges) > 0: edge_org = available_edges.pop() new_graph.add_edge(edge[0], edge[1], graph.get_edge(edge_org[0], edge_org[1])) else: print "Removing:", edge new_graph.remove_edge(edge[0], edge[1]) for edge_org in available_edges: print "Adding:", edge_org new_graph.add_edge(edge_org[0], edge_org[1], graph.get_edge(edge_org[0], edge_org[1])) return new_graph
def get_graph(objects, properties): graph_type = properties['graph_type'] n = len(objects)-1 if 'num_nodes_to_attach' in properties.keys(): k = properties['num_nodes_to_attach'] else: k = 3 r = properties['connection_probability'] tries = 0 while(True): if graph_type == 'random': x = nx.fast_gnp_random_graph(n,r) elif graph_type == 'erdos_renyi_graph': x = nx.erdos_renyi_graph(n,r) elif graph_type == 'watts_strogatz_graph': x = nx.watts_strogatz_graph(n, k, r) elif graph_type == 'newman_watts_strogatz_graph': x = nx.newman_watts_strogatz_graph(n, k, r) elif graph_type == 'barabasi_albert_graph': x = nx.barabasi_albert_graph(n, k, r) elif graph_type == 'powerlaw_cluster_graph': x = nx.powerlaw_cluster_graph(n, k, r) elif graph_type == 'cycle_graph': x = nx.cycle_graph(n) else: ##Star by default x = nx.star_graph(len(objects)-1) tries += 1 cc_conn = nx.connected_components(x) if len(cc_conn) == 1 or tries > 5: ##best effort to create a connected graph! break return x, cc_conn
def main(): ### Undirected graph ### # Initialize model using the Petersen graph model=gmm.gmm(nx.petersen_graph()) old_graph=model.get_base() model.set_termination(node_ceiling) model.set_rule(rand_add) # Run simualation with tau=4 and Poisson density for motifs gmm.algorithms.simulate(model,4) # View results new_graph=model.get_base() print(nx.info(new_graph)) # Draw graphs old_pos=nx.spring_layout(old_graph) new_pos=nx.spring_layout(new_graph,iterations=2000) fig1=plt.figure(figsize=(15,7)) fig1.add_subplot(121) #fig1.text(0.1,0.9,"Base Graph") nx.draw(old_graph,pos=old_pos,node_size=25,with_labels=False) fig1.add_subplot(122) #fig1.text(0.1,0.45,"Simulation Results") nx.draw(new_graph,pos=new_pos,node_size=20,with_labels=False) fig1.savefig("undirected_model.png") ### Directed graph ### # Initialize model using random directed Barabasi-Albert model directed_base=nx.barabasi_albert_graph(25,2).to_directed() directed_model=gmm.gmm(directed_base) directed_model.set_termination(node_ceiling) directed_model.set_rule(rand_add) # Run simualation with tau=4 and Poisson density for motifs gmm.algorithms.simulate(directed_model,4) # View results new_directed=directed_model.get_base() print(nx.info(new_directed)) # Draw directed graphs old_dir_pos=new_pos=nx.spring_layout(directed_base) new_dir_pos=new_pos=nx.spring_layout(new_directed,iterations=2000) fig2=plt.figure(figsize=(7,10)) fig2.add_subplot(211) fig2.text(0.1,0.9,"Base Directed Graph") nx.draw(directed_base,pos=old_dir_pos,node_size=25,with_labels=False) fig2.add_subplot(212) fig2.text(0.1,0.45, "Simualtion Results") nx.draw(new_directed,pos=new_dir_pos,node_size=20,with_labels=False) fig2.savefig("directed_model.png") # Export files nx.write_graphml(model.get_base(), "base_model.graphml") nx.write_graphml(directed_model.get_base(), "directed_model.graphml") nx.write_graphml(nx.petersen_graph(), "petersen_graph.graphml")
def generate(self): barabasi_albert = nx.barabasi_albert_graph(self.nodes, self.m, self.seed) self.nx_topology = nx.MultiDiGraph() self.nx_topology.clear() index = 0 nodes = [] for node in barabasi_albert.nodes(): #SSnodes.append(node+1) nodes.append(str(node+1)) self.nx_topology.add_nodes_from(nodes) for (n1, n2) in barabasi_albert.edges(): n1 = n1 + 1 n2 = n2 + 1 self.sip.update(str(index)) id_ = str(self.sip.hash()) #SSself.nx_topology.add_edge(n1, n2, capacity=self.DEFAULT_SPEED, allocated=0.0, src_port="", dst_port="", src_port_no="", dst_port_no="", src_mac="", dst_mac="", flows=[], id=id_) self.nx_topology.add_edge(str(n1), str(n2), capacity=self.DEFAULT_SPEED, allocated=0.0, src_port="", dst_port="", src_port_no="", dst_port_no="", src_mac="", dst_mac="", flows=[], id=id_) index = index + 1 self.sip.update(str(index)) id_ = str(self.sip.hash()) #SSself.nx_topology.add_edge(n2, n1, capacity=self.DEFAULT_SPEED, allocated=0.0, src_port="", dst_port="", src_port_no="", dst_port_no="", src_mac="", dst_mac="", flows=[], id=id_) self.nx_topology.add_edge(str(n2), str(n1), capacity=self.DEFAULT_SPEED, allocated=0.0, src_port="", dst_port="", src_port_no="", dst_port_no="", src_mac="", dst_mac="", flows=[], id=id_) index = index + 1
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 __init__(self, N = 10000, m_0 = 3): """ :Purpose: This is the base class used to generate the social network for the other agents, i.e. . The class inherits from the PopulationClass. :Input: N : int Number of agents. Default: 10000 m_0: int Number of nodes each node is connected to in preferential attachment step """ if type(N) is not int: raise ValueError(('Population size must be integer,\ n = %s, not %s')%(string(N), type(N))) else: pass if m_0 not in range(10): raise ValueError('m_0 must be integer smaller than 10') else: self.m_0 = m_0 PopulationClass.__init__(self, n = N) # Create population SpecialAgents = set(self.IDU_agents).union(set(self.MSM_agents)) SpecialAgents = SpecialAgents.union(set(self.NIDU_agents)) NormalAgents = set(range(self.PopulationSize)).difference(SpecialAgents) NormalAgents = list(NormalAgents) self.NetworkSize = len(NormalAgents) # scale free Albert Barabsai Graph self.G = nx.barabasi_albert_graph(self.NetworkSize,m_0)
def correlation_betweenness_degree_on_BA(): n = 1000 m = 2 G = nx.barabasi_albert_graph(n, m) print nx.info(G) ND, ND_lambda = ECT.get_number_of_driver_nodes(G) print "ND = ", ND print "ND lambda:", ND_lambda ND, driverNodes = ECT.get_driver_nodes(G) print "ND =", ND degrees = [] betweenness = [] tot_degree = nx.degree_centrality(G) tot_betweenness = nx.betweenness_centrality(G,weight=None) for node in driverNodes: degrees.append(tot_degree[node]) betweenness.append(tot_betweenness[node]) with open("results/driver_degree_BA.txt", "w") as f: for x in degrees: print >> f, x with open("results/driver_betweenness_BA.txt", "w") as f: for x in betweenness: print >> f, x with open("results/tot_degree_BA.txt", "w") as f: for key, value in tot_degree.iteritems(): print >> f, value with open("results/tot_betweenness_BA.txt", "w") as f: for key, value in tot_betweenness.iteritems(): print >> f, value
def createScaleFreeNetwork(numOfNodes, degree): ''' numOfNodes: The number of nodes that the scale free network should have degree: The degree of the Scale Free Network This function creates a Scale Free Network containing 'numOfNodes' nodes, each of degree 'degree' It generates the required graph and saves it in a file. It runs the Reinforcement Algorithm to create a weightMatrix and an ordering of the vertices based on their importance by Flagging. ''' global reinforce_time G = nx.barabasi_albert_graph(numOfNodes, degree) #Create a Scale Free Network of the given number of nodes and degree StrMap = {} for node in G.nodes(): StrMap[node] = str(node) G = nx.convert.relabel_nodes(G,StrMap) print "Undergoing Machine Learning..." start = time.time() H = reinforce(G) #Enforce Machine Learning to generate a gml file of the learnt graph. finish = time.time() reinforce_time = finish - start print "Machine Learning Completed..." filename = "SFN_" + str(numOfNodes) + "_" + str(degree) + '.gpickle' nx.write_gpickle(H,filename)#generate a gpickle file of the learnt graph. print "Learnt graph Successfully written into " + filename
def generateRandomNetworks(randomSeed=622527): seed(randomSeed) # Network size will be 10^1, 10 ^2, 10^3, 10^4 for exponent in range(1, 4): # 1 .. 4 n = 10 ** exponent for p in [0.1, 0.3, 0.5, 0.7, 0.9]: m = round(n * p) # Generate erdos Renyi networks graph = nx.erdos_renyi_graph(n, p, randomNum()) graphName = "erdos_renyi_n{}_p{}.graph6".format(n, p) nx.write_graph6(graph, directory + graphName) # Generate Barabasi Albert networks graph = nx.barabasi_albert_graph(n, m, randomNum()) graphName = "barabasi_albert_n{}_m{}.graph6".format(n, m) nx.write_graph6(graph, directory + graphName) for k in [0.1, 0.3, 0.5, 0.7, 0.9]: k = round(n * k) # Generate Watts Strogatz networks graph = nx.watts_strogatz_graph(n, k, p, randomNum()) graphName = "watts_strogatz_n{}_k{}_p{}.graph6".format(n, k, p) nx.write_graph6(graph, directory + graphName)
def barabasi_albert(N, M, seed, verbose=True): '''Create random graph using Barabási-Albert preferential attachment model. A graph of N nodes is grown by attaching new nodes each with M edges that are preferentially attached to existing nodes with high degree. Args: N (int):Number of nodes M (int):Number of edges to attach from a new node to existing nodes seed (int) Seed for random number generator Returns: The NxN adjacency matrix of the network as a numpy array. ''' A_nx = nx.barabasi_albert_graph(N, M, seed=seed) A = nx.adjacency_matrix(A_nx).toarray() if verbose: print('Barbasi-Albert Network Created: N = {N}, ' 'Mean Degree = {deg}'.format(N=N, deg=mean_degree(A))) return A
def __init__(self, size, attachmentCount): self.size = size self.m = attachmentCount self.network = nx.barabasi_albert_graph(self.size, self.m) # Network stats object self.networkStats = Networkstats(self.network, self.size)
def test_valid_degree_sequence2(): n = 100 for i in range(10): G = nx.barabasi_albert_graph(n,1) deg = list(G.degree().values()) assert_true( nx.is_valid_degree_sequence(deg, method='eg') ) assert_true( nx.is_valid_degree_sequence(deg, method='hh') )
def test083_barabasi_albert_graph(self): """ Random graph using Barabasi-Albert preferential attachment model. """ g = nx.barabasi_albert_graph(100, 5) mate1 = mv.max_cardinality_matching(g) mate2 = nx.max_weight_matching(g, True) self.assertEqual(len(mate1), len(mate2))
def test_valid_degree_sequence2(): n = 100 for i in range(10): G = nx.barabasi_albert_graph(n, 1) deg = (d for n, d in G.degree()) assert_true(nx.is_valid_degree_sequence(deg, method="eg")) assert_true(nx.is_valid_degree_sequence(deg, method="hh"))
def __init__(self, name, para1, para2): if name == "PAM": # nodes, edges, and nodes > edges self.G = nx.barabasi_albert_graph(para1, para2) # nodes, prob elif name == "ER": self.G = nx.erdos_renyi_graph(para1, para2)
def firstFitTopo(self, jobs, reservations): if len(jobs) != len(reservations): raise IndexError("Length of jobs and reservations input lists do no match") import networkx as nx skeleton = nx.barabasi_albert_graph(780) G = nx.empty_graph()
def spread_size_distribution_vs_time(): probabilities = [float(i) / 10. for i in range(2, 7)] graph = sm.SISModel.get_opera_graph() seed = sm.SISModel.get_random_seed(graph, 100) for t in [1, 5, 10]: time_series = [] for i in xrange(len(probabilities)): model = sm.SISModel(graph, seed, probabilities[i], t, True, 100) model.spread() time_series.append(model.get_time_series()) print(time_series) sm.SISModel.plot_spread_size_distribution( xrange(len(time_series[0])), time_series, ['blue', 'red', 'green', 'orange', 'black'], sm.SISModel.get_data_dir() + sm.SISModel.RESULT_DIR + 'o_spread_size_distribution_time_series_t{}.png'.format(str(t)), 't, steps' ) graph = nx.barabasi_albert_graph(4604, 11) seed = sm.SISModel.get_random_seed(graph, 100) for t in [1, 5, 10]: time_series = [] for i in xrange(len(probabilities)): model = sm.SISModel(graph, seed, probabilities[i], t, True, 100) model.spread() time_series.append(model.get_time_series()) print(time_series) sm.SISModel.plot_spread_size_distribution( xrange(len(time_series[0])), time_series, ['blue', 'red', 'green', 'orange', 'black'], sm.SISModel.get_data_dir() + sm.SISModel.RESULT_DIR + 'ba_spread_size_distribution_time_series_t{}.png'.format(str(t)), 't, steps' ) graph = nx.erdos_renyi_graph(4604, 0.0047) seed = sm.SISModel.get_random_seed(graph, 100) for t in [1, 5, 10]: time_series = [] for i in xrange(len(probabilities)): model = sm.SISModel(graph, seed, probabilities[i], t, True, 100) model.spread() time_series.append(model.get_time_series()) print(time_series) sm.SISModel.plot_spread_size_distribution( xrange(len(time_series[0])), time_series, ['blue', 'red', 'green', 'orange', 'black'], sm.SISModel.get_data_dir() + sm.SISModel.RESULT_DIR + 'er_spread_size_distribution_time_series_t{}.png'.format(str(t)), 't, steps' )
def generate_graph(n, expected_degree, model="ba"): """ Generates a graph with a given model and expected_mean degree :param n: int Number of nodes of the graph :param expected_degree: int Expected mean degree :param model: string Model (ba, er, or ws) :return: networkx graph """ global m global ws_p g = None if model == "ba": # BA expected avg. degree? m = ba_mean_degrees() if m is None: m = ba_mean_degrees(n, expected_degree) g = nx.barabasi_albert_graph(n, m, seed=None) if model == "er": # ER expected avg. degree: d = p*(n-1) p = float(expected_degree) / float(n - 1) g = nx.erdos_renyi_graph(n, p, seed=None, directed=False) if model == "ws": # WS expected degree == k g = nx.watts_strogatz_graph(n, expected_degree, ws_p) return g
def createGraphsAndCommunities(): g = nx.scale_free_graph(500, alpha=0.40, beta=0.40, gamma=0.20) g1 = nx.powerlaw_cluster_graph(500, 10, 0.2) g2 = nx.barabasi_albert_graph(500, 10) g3 = nx.newman_watts_strogatz_graph(500, 10, 0.2) nx.write_graphml (g, direc+"sfg.graphml") nx.write_graphml(g1, direc+"pcg.graphml") nx.write_graphml(g2, direc+"bag.graphml") nx.write_graphml(g3, direc+"nwsg.graphml") graphs = {} graphs["sfg"] = graph_tool.load_graph(direc+"sfg.graphml") graphs["pcg"] = graph_tool.load_graph(direc+"pcg.graphml") graphs["bag"] = graph_tool.load_graph(direc+"bag.graphml") graphs["nwsg"] = graph_tool.load_graph(direc+"nwsg.graphml") graphs["price"] = graph_tool.generation.price_network(1000) for i,h in graphs.iteritems(): s = graph_tool.community.minimize_blockmodel_dl(h) b = s.b graph_tool.draw.graph_draw(h, vertex_fill_color=b, vertex_shape=b, output=direc+"block"+str(i)+".pdf") com = graph_tool.community.community_structure(h, 10000, 20) graph_tool.draw.graph_draw(h, vertex_fill_color=com, vertex_shape=com, output=direc+"community"+str(i)+".pdf") state = graph_tool.community.minimize_nested_blockmodel_dl(h) graph_tool.draw.draw_hierarchy(state, output=direc+"nestedblock"+str(i)+".pdf") pagerank = graph_tool.centrality.pagerank(h) graph_tool.draw.graph_draw(h, vertex_fill_color=pagerank, vertex_size = graph_tool.draw.prop_to_size(pagerank, mi=5, ma=15), vorder=pagerank, output=direc+"pagerank"+str(i)+".pdf") h.set_reversed(is_reversed=True) pagerank = graph_tool.centrality.pagerank(h) graph_tool.draw.graph_draw(h, vertex_fill_color=pagerank, vertex_size = graph_tool.draw.prop_to_size(pagerank, mi=5, ma=15), vorder=pagerank, output=direc+"reversed_pagerank"+str(i)+".pdf")
def spread_size_distribution_vs_probability(): probabilities = [float(i) / 100. for i in range(100)] t = 1 graph = sm.SISModel.get_opera_graph() seed = sm.SISModel.get_random_seed(graph, 100) print(seed) o_sizes = [] for i in xrange(len(probabilities)): print(probabilities[i]) o_sizes.append(sm.SISModel(graph, seed, probabilities[i], t).spread()) print(o_sizes) graph = nx.barabasi_albert_graph(4604, 11) seed = sm.SISModel.get_random_seed(graph, 100) ba_sizes = [] for i in xrange(len(probabilities)): ba_sizes.append(sm.SISModel(graph, seed, probabilities[i], t).spread()) print(ba_sizes) graph = nx.erdos_renyi_graph(4604, 0.0047) seed = sm.SISModel.get_random_seed(graph, 100) er_sizes = [] for i in xrange(len(probabilities)): er_sizes.append(sm.SISModel(graph, seed, probabilities[i], t).spread()) print(er_sizes) sm.SISModel.plot_spread_size_distribution( probabilities, [o_sizes, ba_sizes, er_sizes], ['blue', 'black', 'red'], sm.SISModel.get_data_dir() + sm.SISModel.RESULT_DIR + 'spread_size_distribution.png', 'p' )
def generate_random_topology_BA(N_nodes, n): """Generate a Barabasi-Albert random graph with N_nodes nodes, and new link parameter n Return the triplet (Graph, adjacancy matrix, Laplacian matrix) """ G = nx.barabasi_albert_graph(N_nodes, n) adja = np.array(nx.to_numpy_matrix(G)) L = control2.get_Laplacian(adja) return (G, adja, L)
def generate_network(self): net_type_dict = dict( complete=nx.complete_graph(self.N), barabasi_albert=nx.barabasi_albert_graph(self.N,self.k), watts_strogatz=nx.watts_strogatz_graph(self.N,self.k, self.rewiring_prob), ) return net_type_dict[self.net_type]
def preferentialAttachment(G): n = G.number_of_nodes() m = random.randrange(15,20) PG = nx.barabasi_albert_graph(n,m) plot(PG) l = math.log(n)/math.log(math.log(n)) print 'Global Clustering: {0}\t'.format(str(nx.transitivity(PG))), print 'Average path length : {0}\n'.format(str(l))
def diffusion(): """Analyze similarities and differences between simplified infection model and linear diffusion on Barabasi-Albert networks. Modify input and output as needed. ---Discussion--- The key points we will discuss here are: - The mean of S, I, and V across all nodes, for various tau and D - How long it takes for nodes to become 'infected' - The effect of inital conditions on behaviour We will compare how these behave on Barabasi-Albert graphs, for both a simplified version of the infection model from part 2.2 (call this model 1), and a linear diffusion model (call this model 2). KEY POINT 1: mean of S, I, and V across all nodes, for various tau and D (and simple starting conditions). The parameter tau controls the strength of the flux matrix in model 1, whilst the parameter D controls the strength of the linear diffusion in model 2. Therefore it seems sensible to compare how these two parameters affect the models' behaviour. (We have chosen not to investigate the effect of varying theta0 in model 1, as there is no clear equivalent in model 2 with which to compare it.) Small values of tau or D: very little flux or diffusion Figure 0 shows the mean value of S, I, and V in model 1 for some small values of tau. The initial conditions are one infected node at (0.1, 0.05, 0.05) as in part 2.1, the rest zero. #plotmeans(RHS1, y01, [0.05, 0.1, 1], 100, 0) (We ignore the source node itself when calculating the means, as this tends to skew the outcome, and in any case we are more interested in the spread of infected cells to the rest of the network than how the source node behaves.) We see that the numbers of spreaders and infected cells grow together, whilst the number of vulnerable cells stays at or close to zero. Looking at the simplified infection model's equations, this makes sense; the summation terms will have no effect of interest, as they are independent of one another and so just cause identical growth of each cell type based on degree. The nonlinear theta0*Si*Vi term is what triggers the behaviour - it is negative in dVi/dt, meaning <V> will tend to shrink, whilst it is positive in dIi/dt which will cause <I> to grow. In particular, the bigger S is, the more <I> will grow, meaning its growth can keep up with that of <S>. The exact ratio of growth speeds is decided by theta=theta0, which does not depend on time (and is fixed at 80 here). Figure 1 shows the same statistics, but for model 2. #plotmeans(RHS2, y01, [0.01, 0.05, 0.1], 100, 1) This time <S>, <I>, and <V> all follow the same behaviour, except <S> is consistently twice as large as <I> and <V>. This makes sense considering the linear diffusion model; growth of S, I, and V do not depend on their current states, but only on the Laplacian matrix L (and the constant D). Since <S> starts twice as high as the other two, this remains the case under linear growth. More precisely, we are repeatedly applying a constant matrix to the system, so the system is tending to an equilibrium state at which the means do not change. This corresponds to a distribution of S, I, and V which is invariant under applying linear growth, i.e. corresponds to eigenvectors of the system. The lines in the plot flatten out over time as the values converge to that of the equilibrium state. (Note: below, model 2 is implemented with odeint rather than explicitly finding eigenvectors, as it leads to nicer plot-making code for both models) Medium and high values of tau or D: stronger flux or diffusion Figures 2 and 3 show the same behaviour but with larger values of tau and D. #plotmeans(RHS1, y01, [5, 30, 50], 100, 2) #plotmeans(RHS2, y01, [1, 5, 10], 100, 3) In model 1, a larger tau inevitably exaggerates the fast growth of <S>, and hence of I. However, as tau becomes very large, we get a sudden burst of vulnerable cells before they die out. This is most likely because at first, the huge flux dominates any of the system's non-linear terms, meaning high-degree nodes far from the infected source can spawn lots of cells of all kinds. As soon as spreaders start to reach these nodes, however, the vulnerable cells are smothered by the theta*S*V term and quickly die out. In model 2, a larger value of D simply speeds up the linear diffusion process, which on the plots corresponds to quickly converging to the equilibrium state. This helps confirm the analysis above. #KEY POINT 2: how long it takes for nodes to become infected (for simple starting conditions and a few tau or D) Figures 4 and 5 (for models 1 and 2) show the fraction of all nodes which are over K% infected, for various K. (This is over a larger timeframe than above.) #plotfracs(RHS1, y01, 0.05, 4) #plotfracs(RHS2, y01, 0.005, 5) (tau and D control similar behaviour, but have different scaling factors; they have been chosen here to display typical non-degenerate behaviours.) In model 1, we see an initial burst in which many cells immediately become more that 0.01% infected. After that, cells' infected percentages seem to increase roughly linearly, with eventually all cells reaching 0.1% infection. Model 2 shows almost identical behaviour, though it takes a bit longer for the cells to become infected to each percentage. The fact that both models show fairly steady increase of infected nodes is not surprising; nodes tend to become infected when their neighbours are, which will propagate through the graph. A closer look shows that each line tends to sit still for a fraction of a second before jumping up. This suggests that nodes tend to reach the next percentage of infection in batches, i.e. that a connected cluster of nodes will all become more infected at once (in both models). In particular, since Barabasi-Albert models are scale-free, we would expect some nodes with high degrees; once one of these becomes infeced, its many direct neighbours will likely be infected soon as well, explaining the jumps on the plots. #KEY POINT 3: effect of initial condition on behaviour We will consider: 1) the case when five nodes are initially infected 2) the case when only the highest-degree node is initially infected. Case 1: Figures 6 and 7 show the means of S, I, and V with five initially infected nodes, in models 1 and 2 respectively. #plotmeans(RHS1, y02, [0.1, 1, 5], 100, 6) #plotmeans(RHS2, y02, [0.01, 0.1, 1], 100, 7) These plots are most meaningful when compared to figures 0 and 1. However, the values of the initially-infected nodes were not excluded this time when calculating the means, so the graphs must be interpreted carefully. In essence, we see the same behaviour (in terms of growth and ratios of the three means), but occurring much faster. This is to be expected, as a greater initial infection in five random nodes will inevitably be able to spread more quickly than from one node. In particular, in model 2 (figure 7), the snap to the equilibrium state is very fast even for quite small parameter values. The effect is more easily seen by observing the fraction of infected nodes; figures 8 and 9 (in comparison to 4 and 5) show a much faster infection rate than before. #plotfracs(RHS1, y02, 0.05, 8) #plotfracs(RHS2, y02, 0.005, 9) Case 2: highest-degree node only is infected As mentioned, the scale-free property of Barabasi-Albert models means they are likely to have outlier nodes of very high degree. We would expect the infection to happen faster than when a random node is initially infected. Figures 10 to 13 are as above, with this initialy condition. #plotmeans(RHS1, y03, [0.1, 1, 5], 100, 10) #plotmeans(RHS2, y03, [0.01, 0.1, 1], 100, 11) #plotfracs(RHS1, y03, 0.05, 12) #plotfracs(RHS2, y03, 0.005, 13) Plots 10 and 11 do show what we might expect; the same behaviour as in plots 0 to 3 (comparing the same tau) but faster and more exaggerated. Interestingly, plots 12 and 13 show that the time it takes for nodes to become infected is similar to that for a low-degree node initially infected, but slower than for five initially infected nodes. This might indicate that it is not the degree of infected nodes that affects how quickly the illness spreads, but how many nodes are infected to start with. (This effect could also happen if we were unlucky and randomly chose a high- degree node earlier, but by taking several runs this was ensured not to be the case.) """ #Barabasi-Albert graph and timestep array N, M = 100, 5 BA = nx.barabasi_albert_graph(N, M) tf, Nt = 40, 400 tarray = np.linspace(0, tf, Nt + 1) #Setup for simplified infection model theta0 = 80 A = nx.adjacency_matrix(BA).todense() #adjacency matrix B = np.multiply(A, sum(A)).transpose() #ij entry is qi*Aij F = np.nan_to_num(np.divide( B, sum(B))) #not multiplied by tau yet as we will vary it bigmat = sparse.block_diag((F, F, F)).toarray() #Outputs RHS of simplified infection model, with given tau def RHS1(y, t, tau): S, V = y[:N], y[2 * N:] #don't need I to find the simplified model dy = np.zeros(3 * N) dy[N:2 * N] = theta0 * S * V dy[2 * N:] = -theta0 * S * V return dy - tau * y + tau * bigmat.dot(y) #Outputs RHS of linear diffusion model L = np.diag([BA.degree(i) for i in range(N)]) - A #Laplacian matrix bigL = sparse.block_diag((L, L, L)) def RHS2(y, t, D): return -D * bigL * y #Solve specified system with given y0, and tau or D. Return things I want to plot def solve(func, y0, tD, Ifrac): #func is one of RHS1, RHS2 #tD is either tau (RHS1) or D (RHS2) sol = odeint(func, y0, tarray, args=(tD, )) #Find mean of each cell type at all times, not including the source node Smean = [sol[i][1:N].mean() for i in range(Nt + 1)] Imean = [sol[i][N + 1:2 * N].mean() for i in range(Nt + 1)] Vmean = [sol[i][2 * N + 1:].mean() for i in range(Nt + 1)] #Find fraction of nodes whose fraction of infected cells is over Ifrac fracreached = [ sum(i > Ifrac for i in sublist) / float(N) for sublist in sol[:, N:2 * N] ] return (Smean, Imean, Vmean, fracreached) #Plot means of S, I, V for specified model, y0, cutoff time, and tau or D. Save result def plotmeans(func, y0, tDs, cutoff, fignum): fig, axs = plt.subplots(1, 3, figsize=(15, 6)) sols = [[] for i in range(9)] sols[0], sols[1], sols[2], _ = solve(func, y0, tDs[0], 0.1) sols[3], sols[4], sols[5], _ = solve(func, y0, tDs[1], 0.1) sols[6], sols[7], sols[8], _ = solve(func, y0, tDs[2], 0.1) sols = [sublist[:cutoff] for sublist in sols] x = tarray[:cutoff] l1, l2, l3 = axs[0].plot(x, sols[0], x, sols[1], '.', x, sols[2]) l4, l5, l6 = axs[1].plot(x, sols[3], x, sols[4], '.', x, sols[5]) l7, l8, l9 = axs[2].plot(x, sols[6], x, sols[7], '.', x, sols[8]) axs[0].set_ylabel('Mean over all nodes in the network') for i in range(3): axs[i].set_xlabel('Time') if func == RHS1: axs[i].set_title("tau = " + str(tDs[i])) if func == RHS2: axs[i].set_title("D = " + str(tDs[i])) fig.legend((l1, l2, l3), ('S', 'I', 'V'), 'center right') if func == RHS1: fig.suptitle( 'Simplified infection model: mean of S, I, and V over all nodes \n Christophe Jefferies \n Plot by diffusion: plotmeans' ) if func == RHS2: fig.suptitle( 'Linear diffusion model: mean of S, I, and V over all nodes \n Christophe Jefferies \n Plot by diffusion: plotmeans' ) plt.savefig("fig" + str(fignum) + ".png", bbox_inches='tight') #Plot fraction of infected nodes for specified model, y0, and tau or D. Save result def plotfracs(func, y0, tD, fignum): #Simplified infection model fracs = [[] for i in range(10)] #will store output for value in range(10): (_, _, _, fracs[value]) = solve(func, y0, tD, (value + 1) / float(10000)) #Make and save plot plt.figure(figsize=(8, 8)) for i in range(10): plt.plot(tarray, fracs[i], label=str((i + 1) / float(100))) if func == RHS1: plt.title( 'Simplified infection model: fraction of nodes over K% infected (tau = ' + str(tD) + ')\n Christophe Jefferies \n Plot by diffusion: plotfracs') if func == RHS2: plt.title( 'Linear diffusion model: fraction of nodes over K% infected (D = ' + str(tD) + ')\n Christophe Jefferies \n Plot by diffusion: plotfracs') plt.xlabel('Time') plt.ylabel('Fraction of nodes over K% infected') plt.legend(title='K') plt.savefig("fig" + str(fignum) + ".png", bbox_inches='tight') #Define initial conditions #Basic initial condition as in 2.1 piece1 = [1] + (N - 1) * [0.0] y01 = piece1 + 2 * ([i / 2 for i in piece1]) #k nodes infected k = 5 piece2 = [0.0 for i in range(N)] for i in range(k): piece2[int(i * N / float(k))] = 1 y02 = piece2 + 2 * ([i / 2 for i in piece2]) #Infect a node with highest degree maxdeg = max([deg for (node, deg) in list(BA.degree())]) topnode = [node for node in range(N) if BA.degree(node) == maxdeg][0] piece3 = [0.0 for i in range(N)] piece3[topnode] = 1 y03 = piece3 + 2 * ([i / 2 for i in piece3]) plotmeans(RHS1, y01, [0.05, 0.1, 1], 100, 0) plotmeans(RHS2, y01, [0.01, 0.05, 0.1], 100, 1) plotmeans(RHS1, y01, [5, 30, 50], 100, 2) plotmeans(RHS2, y01, [1, 5, 10], 100, 3) plotfracs(RHS1, y01, 0.05, 4) plotfracs(RHS2, y01, 0.005, 5) plotmeans(RHS1, y02, [0.1, 1, 5], 100, 6) plotmeans(RHS2, y02, [0.01, 0.1, 1], 100, 7) plotfracs(RHS1, y02, 0.05, 8) plotfracs(RHS2, y02, 0.005, 9) plotmeans(RHS1, y03, [0.1, 1, 5], 100, 10) plotmeans(RHS2, y03, [0.01, 0.1, 1], 100, 11) plotfracs(RHS1, y03, 0.05, 12) plotfracs(RHS2, y03, 0.005, 13)
def __init__(self, structure='grid', distribution='uniform', dimensions=(2, 2), custom_net=None, rules=np.matrix([[0., 0.], [0., 0.]])): # This will define a class that generates a grid of n by m elemets of agents # Input Testing if (isinstance(structure, str)): self.rules = {} self.define_rules(rules) # Building a grid with dimensions specified by the args if structure == 'grid': if len(dimensions) < 3 and isinstance( dimensions[0], int) and isinstance(dimensions[1], int): self.network = nx.grid_2d_graph(dimensions[0], dimensions[1], periodic=True) else: ValueError('Too many arguments for option for grid') elif structure == 'BA': if len(dimensions) < 3 and isinstance( dimensions[0], int) and isinstance(dimensions[1], int): self.network = nx.barabasi_albert_graph( dimensions[0], dimensions[1]) else: ValueError('Too many arguments for option for grid') elif structure == 'ER': if len(dimensions) < 3 and isinstance( dimensions[0], int) and isinstance( dimensions[1], float): self.network = nx.erdos_renyi_graph( dimensions[0], dimensions[1]) else: ValueError('Too many arguments for option for grid') elif structure == 'custom': if custom_net != None: self.network = custom_net else: ValueError('Value for custom network was not provided') else: raise ValueError('Unknown option for structure') if (isinstance(distribution, str)): # Building a uniform distribution of strategies if distribution == 'uniform': cooperation_prob = {} strategies = {} for node in self.network.nodes(): cooperation_prob[node] = np.random.rand() if cooperation_prob[node] > 0.5: strategies[node] = 0 else: strategies[node] = 1 # Setting the p attributes nx.set_node_attributes(self.network, strategies, name='strategy') # for node in self.network.nodes(): # print(nx.get_node_attributes(self.network,'strategy')) else: raise ValueError('Unknown option for distribution') else: raise TypeError('Option argument requires a string type')
from matplotlib import pyplot as plt import networkx as nx import numpy as np import torch from deep_graphs.utils.random_walks_metrics import score_matrix_from_random_walks, graph_from_scores from deep_graphs.data.datasets import read_all_graphs from deep_graphs.processes import node2vec_walk number_of_nodes = 20 if __name__ == "__main__": #BARABASI GRAPH barabasi_graph = nx.barabasi_albert_graph(number_of_nodes, 3) sparse_adjacency = nx.adjacency_matrix(barabasi_graph).tocoo() #GENERATE GRAPHS node2vec_walk(sparse_adjacency, 5) walks = [] #READ FROM MODELS for i in range(100): walks.append(node2vec_walk(sparse_adjacency, 5)) walks = torch.Tensor(walks) score_matrix = score_matrix_from_random_walks(walks, number_of_nodes, symmetric=True) graph_adjacency_from_scores = graph_from_scores( score_matrix, barabasi_graph.number_of_edges())
def generate_graphs(N, n_subgraphs, n_subgraph_nodes, p_keep_edge=1, density_multiplier=1, n_duplicate_names=5, force_connectivity=False): assert n_subgraph_nodes <= N, "Subgraphs cannot be larger than the " + \ "underlying graph" # Generate the underlying graph G = nx.barabasi_albert_graph(N, 2) n_uniq_names = int(np.ceil(N / float(n_duplicate_names))) name_pool = [] for i in range(n_uniq_names): name_pool += [i] * n_duplicate_names for node in G.nodes(): G.node[node]['name'] = name_pool.pop( random.randint(0, len(name_pool) - 1)) #G.node[node]['entity'] = -1 base_density = nx.density(G) #print "\nUnderlying graph density {}.\n".format(base_density) # Compute the edge adding probability so that density is correctly # multiplied assert density_multiplier >= 1, "Density multiplier must be at least 1." p_add_edge = base_density * (density_multiplier - 1) sGs = [] # Subgraphs for i in range(n_subgraphs): sG = nx.Graph() unexplored = [random.choice(G.nodes())] while len(sG) < n_subgraph_nodes and len(unexplored) > 0: next_node = random.choice(unexplored) sG.add_node(next_node, name=G.node[next_node]['name'], entity=next_node, subgraph=i, id=next_node) neighs = G.neighbors(next_node) if not force_connectivity: for neigh in neighs: if not sG.has_node(neigh): if neigh not in unexplored: unexplored.append(neigh) elif random.random() < p_keep_edge: # Add neighbor sG.add_edge(next_node, neigh) else: random.shuffle(neighs) for ni, neigh in enumerate(neighs): if not sG.has_node(neigh): if neigh not in unexplored: unexplored.append(neigh) elif random.random() < p_keep_edge or sG.degree( next_node) == 0: # Add neighbor sG.add_edge(next_node, neigh) unexplored.remove(next_node) density0 = nx.density(sG) # Add edges to the subgraph if p_add_edge > 0: for u in sG.nodes(): for v in sG.nodes(): if v <= u: # Don't try adding the same edge twice continue if not sG.has_edge(u, v) and random.random() < p_add_edge: sG.add_edge(u, v) density1 = nx.density(sG) #print "Subgraph {} density after removal {} and after adding {}."\ # .format(i, density0, density1) sGs.append(sG) return sGs, G
Test = 0 n=1000 # número de nós grau=2 # grau médio de ligação iter=20# número de iterações th=0.5 # threshold do cálculo de contaminação #-------------------------------------- # Escolha da rede #-------------------------------------- print('Qual topologia deseja utilizar?\n') print(" 'B' - Barabasi \n 'N' - regular\n 'T' - From txt \n 'E' - Erdös-Rényi \n") R = input('Topologia: ') if R == "B" or R == "b": #BARABASI G = nx.barabasi_albert_graph(n,grau) elif R == "N": #REGULAR G = nx.cycle_graph(n) elif R == "T" or R == "t": #Por arquivo de Texto G = nx.Graph() #Adicionando os nós ao Grafo a partir do arquivo do professor. os.chdir('C:\\Users\\Ellen') #os.chdir('//storage//emulated//0') #path = input('Digite o caminho completo (duas "//") até o arquivo ex: C://Eduardo//MODCOMP//grafos.dat') #path = input('Digite o caminho completo (duas "//") até o arquivo ex: C://Users//Ellen//network.dat') #os.chdir(path) arestas_txt = open('network.dat','r')
def main(): import os p = 0.7 delta = 1 parser = argparse.ArgumentParser() parser.add_argument('-t', '--type', choices=all_graph_types, help='graph type') parser.add_argument('-s', '--size', type=int, default=0, help="size of graph") parser.add_argument('-e', '--size_exponent', type=int, default=1, help="exponent of the size") parser.add_argument('-b', '--exponent_base', type=int, default=10, help="base of the size exponent") parser.add_argument('-n', '--n_rounds', type=int, default=100, help="number of simulated cascades") args = parser.parse_args() gtype = args.type if args.size: size = args.size output_dir = 'data/{}/{}'.format(gtype, size) else: size = args.exponent_base**args.size_exponent output_dir = 'data/{}/{}-{}'.format(gtype, args.exponent_base, args.size_exponent) if gtype == KRONECKER_HIER: g = gen_kronecker(P=P_hier, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == KRONECKER_PERI: g = gen_kronecker(P=P_peri, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == KRONECKER_RAND: g = gen_kronecker(P=P_rand, k=args.size_exponent, n_edges=2**args.size_exponent * 3) elif gtype == PL_TREE: p = 0.88 g = random_powerlaw_tree(size, tries=999999) elif gtype == B_TREE: g = nx.balanced_tree(args.exponent_base, args.size_exponent - 1) elif gtype == ER: g = extract_larges_CC(nx.fast_gnp_random_graph(size, 0.1)) elif gtype == BARABASI: g = extract_larges_CC(nx.barabasi_albert_graph(size, 5)) elif gtype == GRID: g = grid_2d(int(np.sqrt(size))) elif gtype == CLIQUE: g = nx.complete_graph(size) elif gtype == LINE: g = nx.path_graph(size) else: raise ValueError('unsupported graph type {}'.format(gtype)) g.remove_edges_from(g.selfloop_edges()) print('|V|={}, |E|={}'.format(g.number_of_nodes(), g.number_of_edges())) if gtype == GRID: mapping = {(i, j): int(np.sqrt(size)) * i + j for i, j in g.nodes_iter()} g = nx.relabel_nodes(g, mapping) else: g = nx.convert_node_labels_to_integers(g) if not os.path.exists(output_dir): os.makedirs(output_dir) print('graph type: {}'.format(gtype)) # g = add_p_and_delta(g, p, delta) output_path = '{}/graph.graphml'.format(output_dir, gtype) print('saving to {}'.format(output_path)) nx.write_graphml(g, output_path) nx.write_gpickle(g, '{}/graph.gpkl'.format(output_dir, gtype)) if False: pkl.dump( time_probas, open('{}/{}.pkl'.format(output_dir, INF_TIME_PROBA_FILE), 'wb')) pkl.dump(node2id, open('{}/{}.pkl'.format(output_dir, NODE2ID_FILE), 'wb')) pkl.dump(id2node, open('{}/{}.pkl'.format(output_dir, ID2NODE_FILE), 'wb'))
def test_short_example(tmpdir, request): fname = "short_example.svg" ref_fpath = os.path.join(TEST_DIR, fname) test_fpath = str(tmpdir.join(fname)) # seed must be the same random.seed(1) # a network g = networkx.barabasi_albert_graph(50, 2, seed=2) # our hiveplot object h = Hiveplot(test_fpath) # start end axis0 = Axis((200, 200), (200, 100), stroke="grey") axis1 = Axis((200, 200), (300, 300), stroke="blue") axis2 = Axis((200, 200), (10, 310), stroke="black") h.axes = [axis0, axis1, axis2] # randomly distribute nodes in axes for n in g.nodes(): node = Node(n) random.choice(h.axes).add_node(node, random.random()) for e in g.edges(): if (e[0] in axis0.nodes) and ( e[1] in axis1.nodes): # edges from axis0 to axis1 h.connect(axis0, e[0], 45, axis1, e[1], -45, stroke_width='0.34', stroke_opacity='0.4', stroke='purple') elif (e[0] in axis0.nodes) and ( e[1] in axis2.nodes): # edges from axis0 to axis2 h.connect(axis0, e[0], -45, axis2, e[1], 45, stroke_width='0.34', stroke_opacity='0.4', stroke='red') elif (e[0] in axis1.nodes) and ( e[1] in axis2.nodes): # edges from axis1 to axis2 h.connect(axis1, e[0], 15, axis2, e[1], -15, stroke_width='0.34', stroke_opacity='0.4', stroke='magenta') h.save(pretty=True) try: if sys.version_info >= (3, 6): assert_same_contents(ref_fpath, test_fpath) # elif (3, 0) < sys.version_info < (3, 6): else: # cannot check for exact identity in CPython < 3.6 due to # non-deterministic dict ordering assert_same_lines(ref_fpath, test_fpath) except AssertionError: if request.config.getoption("--dump"): ver = '.'.join(str(i) for i in sys.version_info) dt = datetime.now().isoformat().replace(':', '-') shutil.copyfile(test_fpath, os.path.join(TEST_DIR, '_'.join([ver, dt, fname]))) if sys.version_info < (3, 0): pytest.xfail( "Python 2 paths are visibly identical, but numerically different" ) raise
def barabasi_albert(): g = nx.barabasi_albert_graph(10000, 5) return g
def barabasi_albert(nodes, edges): """creates a network using the Barabasi-Albert model """ g.network = nx.barabasi_albert_graph(nodes, edges) g.empty_nodes = g.network.nodes() g.nodes_with_agents = [] g.neighbors_with_agents = {}
# write_json_graph(nx.balanced_tree(3, 5), 'balanced_tree(3,5).json') write_json_graph(nx.balanced_tree(3, 6), 'balanced_tree(3,6).json') # write_json_graph(nx.balanced_tree(3, 7), 'balanced_tree(3,7).json') write_json_graph(nx.balanced_tree(3, 8), 'balanced_tree(3,8).json') # write_json_graph(nx.balanced_tree(3, 10), 'balanced_tree(3,10).json') write_json_graph(nx.balanced_tree(4, 4), 'balanced_tree(4,4).json') # write_json_graph(nx.balanced_tree(4, 5), 'balanced_tree(4,5).json') write_json_graph(nx.balanced_tree(4, 6), 'balanced_tree(4,6).json') write_json_graph(nx.balanced_tree(4, 8), 'balanced_tree(4,7).json') # write_json_graph(nx.balanced_tree(5, 2), 'balanced_tree(5,2).json') # write_json_graph(nx.balanced_tree(5, 4), 'balanced_tree(5,4).json') # write_json_graph(nx.barabasi_albert_graph(10, 5), 'barabasi_albert_graph(10,5).json') # write_json_graph(nx.barabasi_albert_graph(20, 3), 'barabasi_albert_graph(20,3).json') # write_json_graph(nx.barabasi_albert_graph(20, 10), 'barabasi_albert_graph(20,10).json') write_json_graph(nx.barabasi_albert_graph(50, 40), 'barabasi_albert_graph(50,40).json') # write_json_graph(nx.random_lobster(5, 0.6, 0.4, seed=0), 'random_lobster(5,0_6,0_4).json') # write_json_graph(nx.random_lobster(5, 0.4, 0.6, seed=0), 'random_lobster(5,0_4,0_6).json') write_json_graph(nx.random_lobster(10, 0.6, 0.4, seed=0), 'random_lobster(10,0_6,0_4).json') # write_json_graph(nx.random_lobster(10, 0.4, 0.6, seed=0), 'random_lobster(10,0_4,0_6).json') write_json_graph(nx.random_lobster(20, 0.6, 0.4, seed=0), 'random_lobster(20,0_6,0_4).json') # write_json_graph(nx.random_lobster(20, 0.4, 0.6, seed=0), 'random_lobster(20,0_4,0_6).json') write_json_graph(nx.random_lobster(50, 0.6, 0.4, seed=0), 'random_lobster(50,0_6,0_4).json') # write_json_graph(nx.random_lobster(50, 0.4, 0.6, seed=0), 'random_lobster(50,0_4,0_6).json') write_json_graph(nx.random_lobster(100, 0.6, 0.4, seed=0), 'random_lobster(100,0_6,0_4).json')
def BA(N,m,wseed): if wseed == 1: G = nx.barabasi_albert_graph(N, m, seed=123) else: G = nx.barabasi_albert_graph(N, m) return G
}, { 'delay': random.choice(e2e_latency), 'para': (32.595, 4.5222) }, { 'delay': random.choice(e2e_latency), 'para': (86.982, 5.7892) }, { 'delay': random.choice(e2e_latency), 'para': (39.205, 2.9545) }] numOfTan = 6 # this is a special parameter # rdgraph = gp.fast_gnp_random_graph(NumberOfNode, conn_prob) rdgraph = gp.barabasi_albert_graph(NumberOfNode, 2, seed=2) # rdgraph = gp.barabasi_albert_graph(NumberOfNode, 20) # rdgraph = gp.balanced_tree(2, 4) # rdgraph = gp.gnp_random_graph(NumberOfNode, conn_prob) node_list = range(0, NumberOfNode) access_node_list = random.choice(node_list, NumberOfAccessNode, replace=False) # pickle.dump(access_node_list, open("an_list.pickle", "wb")) # # print "access list:", access_node_list edge_node_list = [node for node in node_list if node not in access_node_list] # pickle.dump(edge_node_list, open("en_list.pickle", "wb"))
import networkx as network import networkx as nx import matplotlib.pyplot as plot import matplotlib.pyplot as plt import math import numpy as np alpha=0.4 beta=0.3 nodenumber=50 initial_average_degree=1 graph1 = network.barabasi_albert_graph(nodenumber, initial_average_degree) graph2 = network.barabasi_albert_graph(nodenumber, initial_average_degree) ps1 = network.spring_layout(graph1) ps2 = network.spring_layout(graph2) start_edges=graph1.number_of_edges()+graph2.number_of_edges()#初始边数量 network.draw(graph1, ps1, with_labels = False, node_size = nodenumber) #plt.savefig("A网络.png") plot.show() network.draw(graph2, ps2, with_labels = False, node_size = nodenumber) #plt.savefig("B网络.png") plot.show() G = network.union(graph1, graph2, rename=('Gone', 'Gtwo')) psG=network.spring_layout(G) network.draw(G, psG, with_labels = False, node_size = 30) #plt.savefig("组合网络.png") plot.show() ###三种匹配方式
# Add some outliers to the system num_outliers = 0 scio_auc = np.zeros(no_runs) glasso_auc = np.zeros(no_runs) threshold_auc = np.zeros(no_runs) ns_auc = np.zeros(no_runs) space_auc = np.zeros(no_runs) clime_auc = np.zeros(no_runs) #d_trace_auc = np.zeros(no_runs) for i in range(no_runs): if network_structure == "uniform": K = make_sparse_spd_matrix(p, norm_diag=True) elif network_structure == "power law": L = nx.to_numpy_array(nx.barabasi_albert_graph(p, 5)) np.fill_diagonal(L, 1) alpha = 0.8 K = (1 - alpha) * np.eye(p) + alpha * L elif network_structure == "caveman": no_cliques = int(p / 5) L = nx.to_numpy_array(nx.caveman_graph(no_cliques, 5)) np.fill_diagonal(L, 1) alpha = 0.8 K = (1 - alpha) * np.eye(p) + alpha * L C = np.linalg.inv(K) X = np.random.multivariate_normal(np.zeros(p), C, n) if noise: X += np.random.multivariate_normal(np.zeros(p), np.eye(p), n)
def graf(): q = 0; while q == 0: beta = float(raw_input("Podaj wartosc bety (predkosc rozprzestrzenania sie epidemii, beta > gamma): ")) gamma = float(raw_input("Podaj wartosc gamma (predkosc zdrowienia populacji, beta > gamma): ")) if beta > gamma: q = 1 else: print 'Nieprawidlowe dane! (beta > gamma; liczby zmiennoprzecinkowe)' q = 0 N = int(raw_input("Podaj liczebnosc populacji: ")) czas = int(raw_input("Podaj czas trwania obserwacji epidemii: ")) options_2 = { 'with_labels': False, 'node_color': 'grey', 'node_size': 100, 'edge_color': 'grey', 'linewidths': 0, 'width': 0.3, } options_i = { 'with_labels': False, 'node_color': 'red', 'node_size': 100, 'edge_color': 'grey', 'linewidths': 0, 'width': 0.3, } options_r = { 'with_labels': False, 'node_color': 'blue', 'node_size': 100, 'edge_color': 'grey', 'linewidths': 0, 'width': 0.3, } nazwy=[] i=[] r=[] graf = nx.barabasi_albert_graph(N,4) # graf w ksztalcie kuli, 4 oznacza kreski odchodzace od kulek ich zasieg pos = nx.spring_layout(graf) # pierwszy chory losowo pierwszy = random.choice(graf.nodes()) graf.node[pierwszy] = 'I' i.append(pierwszy) for x in range(czas): liczba = 0 while liczba < N: liczba = liczba + 1 los = random.choice(graf.nodes()) # wybiera losowo kulke if graf.node[los] == 'I': # chory - losowanie czy wyzdrowieje, czy zarazi (0.0,1.0) if len(i) == 1: sasiad = random.choice(graf.neighbors(los)) # los na sasiada if graf.node[sasiad] != 'R' and graf.node[sasiad] != 'I': # gdy sasiad jest zdrowy porownanie do szybkosci rozprzestrzeniania i zarazenie lub nie if random.random() < beta: graf.node[sasiad] = 'I' else: a = random.random() if a < gamma: graf.node[los] = 'R' if a > gamma: sasiad = random.choice(graf.neighbors(los)) # los na sasiada if graf.node[sasiad] != 'R' and graf.node[sasiad] != 'I': # gdy sasiad jest zdrowy porownanie do szybkosci rozprzestrzeniania i zarazenie lub nie if random.random() < beta: graf.node[sasiad] = 'I' for no in graf.nodes(): if graf.node[no] == 'I': i.append(no) if graf.node[no] == 'R': r.append(no) else: continue plot.axis('off') nx.draw_networkx_edges(graf,pos,edge_color='k') nx.draw_networkx_nodes(graf, pos, **options_2) nx.draw_networkx_nodes(graf, pos, nodelist=i,label=len(i), **options_i) nx.draw_networkx_nodes(graf, pos, nodelist=r, **options_r) p = str(len(i)) plot.title(x) plot.annotate(p, xy=(2, 1), xytext=(3, 1.5)) filenames = str(x) + ".png" plot.savefig(filenames) plot.savefig(filenames) nazwy.append(filenames) images=[] for nazwa in nazwy: images.append(imageio.imread(nazwa)) imageio.mimsave('test.gif', images, duration=2, fps=100)
def generateBASyntheticGraph( n_nodes, n_edges): #generates a synthetic graph using the barabasi-albert model return nx.barabasi_albert_graph(n_nodes, n_edges)
import sys import networkx as nx import seir if __name__ == "__main__": NET_SIZE = int(sys.argv[1]) # network size M = int(sys.argv[2]) # min degree E2I_rate = float(sys.argv[3]) # rate from E to I states trans_rate = float(sys.argv[4]) # transmission rate recov_rate = float(sys.argv[5]) # recovery rate output_log_file = sys.argv[6] output_net_file = sys.argv[7] G = nx.barabasi_albert_graph(NET_SIZE, M) params = { "E2I_rate": E2I_rate, "trans_rate": trans_rate, # transmission rate (for edges) "recov_rate": recov_rate, # recovery rate (for nodes) "init_seed_frac": 0.001, # initial seed fraction } sim_data = seir.run_SEIR(G, **params) seir.to_log(G, sim_data, output_log_file) nx.write_edgelist(G, output_net_file)
def sim_data(genes1, genes2, background, patients1, patients2, dens): n = genes1 + genes2 + background m = patients1 + patients2 genes = np.arange(n) groups_genes = list(np.ones(genes1)) + list(np.ones(genes2) * 2) + list( np.ones(background) * 3) groups_p = [1 if node < patients1 else 2 for node in range(m)] to_sparce = 0.3 #to sparcify bipartite to_mix = 0.99 # to mix edges berween groups b = np.zeros((n, m)) ge = np.random.normal(0, 1, n * m).reshape(n, m) for patient in range(m): for gene in range(n): p_gr = groups_p[patient] g_gr = groups_genes[gene] if p_gr == 1 and g_gr == 1: #all up ge[gene, patient] = np.random.normal(1, 0.35, 1) elif p_gr == 2 and g_gr == 2: ge[gene, patient] = np.random.normal(1, 0.35, 1) #also up elif p_gr == 1 and g_gr == 2: ge[gene, patient] = np.random.normal(-1, 0.35, 1) #down elif p_gr == 2 and g_gr == 1: ge[gene, patient] = np.random.normal(-1, 0.35, 1) #down for patient in range(m): for gene in range(genes1 + genes2): prob = np.random.uniform(0, 1) if prob > 0.9: ge[gene, patient] = np.random.normal(0, 1, 1) for gene in range(genes1 + genes2, n): prob = np.random.uniform(0, 1) if prob < 0.05: for patient in range(m): if groups_p[patient] == 1: #all up ge[gene, patient] = np.random.normal(0.3, 0.35, 1) else: ge[gene, patient] = np.random.normal(-0.3, 0.35, 1) if prob > 0.05 and prob < 0.1: for patient in range(m): if groups_p[patient] == 1: #all up ge[gene, patient] = np.random.normal(-0.3, 0.35, 1) else: ge[gene, patient] = np.random.normal(0.3, 0.35, 1) g1 = nx.barabasi_albert_graph(genes1, 1) g2 = nx.barabasi_albert_graph(genes2, 1) g3 = nx.barabasi_albert_graph(background, 1) G = nx.disjoint_union(g1, g2) G = nx.disjoint_union(G, g3) for _ in range(int(dens * n)): node1 = np.random.randint(0, genes1) node2 = np.random.randint(genes1, genes1 + genes2) node3_1 = np.random.randint(genes1 + genes2, n) node3_2 = np.random.randint(genes1 + genes2, n) G.add_edges_from([(node1, node3_1), (node2, node3_2)]) d = nx.density(G) count = 0 while d > 0.002 and count < 10: node3_1 = np.random.randint(genes1 + genes2, n) node3_2 = np.random.randint(genes1 + genes2, n) count = count + 1 if G.has_edge(node3_1, node3_2): G.remove_edge(node3_1, node3_2) d = nx.density(G) #A_g = nx.adj_matrix(G).todense() *1 b_sp = csr_matrix(b) #sparse matrix for making bipartite graph B = bipartite.from_biadjacency_matrix(b_sp) GE = pd.DataFrame(ge, index=np.arange(n), columns=np.arange(n, n + m)) H = HI_big(GE, 1, 1, 1) return (B, GE, G, H, d, n, m)
def create(args): ### load datasets graphs=[] # synthetic graphs if args.graph_type=='ladder': graphs = [] for i in range(100, 201): graphs.append(nx.ladder_graph(i)) args.max_prev_node = 10 elif args.graph_type=='ladder_small': graphs = [] for i in range(2, 11): graphs.append(nx.ladder_graph(i)) args.max_prev_node = 10 elif args.graph_type=='tree': graphs = [] for i in range(2,5): for j in range(3,5): graphs.append(nx.balanced_tree(i,j)) args.max_prev_node = 256 elif args.graph_type=='caveman': # graphs = [] # for i in range(5,10): # for j in range(5,25): # for k in range(5): # graphs.append(nx.relaxed_caveman_graph(i, j, p=0.1)) graphs = [] for i in range(2, 3): for j in range(30, 81): for k in range(10): graphs.append(caveman_special(i,j, p_edge=0.3)) args.max_prev_node = 100 elif args.graph_type=='caveman_small': # graphs = [] # for i in range(2,5): # for j in range(2,6): # for k in range(10): # graphs.append(nx.relaxed_caveman_graph(i, j, p=0.1)) graphs = [] for i in range(2, 3): for j in range(6, 11): for k in range(20): graphs.append(caveman_special(i, j, p_edge=0.8)) # default 0.8 args.max_prev_node = 20 elif args.graph_type=='caveman_small_single': # graphs = [] # for i in range(2,5): # for j in range(2,6): # for k in range(10): # graphs.append(nx.relaxed_caveman_graph(i, j, p=0.1)) graphs = [] for i in range(2, 3): for j in range(8, 9): for k in range(100): graphs.append(caveman_special(i, j, p_edge=0.5)) args.max_prev_node = 20 elif args.graph_type.startswith('community'): num_communities = int(args.graph_type[-1]) print('Creating dataset with ', num_communities, ' communities') c_sizes = np.random.choice([12, 13, 14, 15, 16, 17], num_communities) #c_sizes = [15] * num_communities for k in range(3000): graphs.append(n_community(c_sizes, p_inter=0.01)) args.max_prev_node = 80 elif args.graph_type=='grid': graphs = [] for i in range(10,20): for j in range(10,20): graphs.append(nx.grid_2d_graph(i,j)) args.max_prev_node = 40 elif args.graph_type=='grid_small': graphs = [] for i in range(2,5): for j in range(2,6): graphs.append(nx.grid_2d_graph(i,j)) args.max_prev_node = 15 elif args.graph_type=='barabasi': graphs = [] for i in range(100,200): for j in range(4,5): for k in range(5): graphs.append(nx.barabasi_albert_graph(i,j)) args.max_prev_node = 130 elif args.graph_type=='barabasi_small': graphs = [] for i in range(4,21): for j in range(3,4): for k in range(10): graphs.append(nx.barabasi_albert_graph(i,j)) args.max_prev_node = 20 elif args.graph_type=='grid_big': graphs = [] for i in range(36, 46): for j in range(36, 46): graphs.append(nx.grid_2d_graph(i, j)) args.max_prev_node = 90 elif 'barabasi_noise' in args.graph_type: graphs = [] for i in range(100,101): for j in range(4,5): for k in range(500): graphs.append(nx.barabasi_albert_graph(i,j)) graphs = perturb_new(graphs,p=args.noise/10.0) args.max_prev_node = 99 # real graphs elif args.graph_type == 'enzymes': graphs= Graph_load_batch(min_num_nodes=10, name='ENZYMES') args.max_prev_node = 25 elif args.graph_type == 'enzymes_small': graphs_raw = Graph_load_batch(min_num_nodes=10, name='ENZYMES') graphs = [] for G in graphs_raw: if G.number_of_nodes()<=20: graphs.append(G) args.max_prev_node = 15 elif args.graph_type == 'protein': graphs = Graph_load_batch(min_num_nodes=20, name='PROTEINS_full') args.max_prev_node = 80 elif args.graph_type == 'DD': graphs = Graph_load_batch(min_num_nodes=100, max_num_nodes=500, name='DD',node_attributes=False,graph_labels=True) args.max_prev_node = 230 elif args.graph_type == 'citeseer': _, _, G = Graph_load(dataset='citeseer') G = max(nx.connected_component_subgraphs(G), key=len) G = nx.convert_node_labels_to_integers(G) graphs = [] for i in range(G.number_of_nodes()): G_ego = nx.ego_graph(G, i, radius=3) if G_ego.number_of_nodes() >= 50 and (G_ego.number_of_nodes() <= 400): graphs.append(G_ego) args.max_prev_node = 250 elif args.graph_type == 'citeseer_small': _, _, G = Graph_load(dataset='citeseer') G = max(nx.connected_component_subgraphs(G), key=len) G = nx.convert_node_labels_to_integers(G) graphs = [] for i in range(G.number_of_nodes()): G_ego = nx.ego_graph(G, i, radius=1) if (G_ego.number_of_nodes() >= 4) and (G_ego.number_of_nodes() <= 20): graphs.append(G_ego) shuffle(graphs) graphs = graphs[0:200] args.max_prev_node = 15 elif args.graph_type == 'generated': graphs = [] file_list = glob.glob('./generated_graphs/*.pkl') file_list = [s for s in file_list if args.generated_name in s and args.generated_size in s] print('File_list:\n{}'.format(file_list)) for file in file_list: A = pkl.load(open(file, 'rb'), encoding='latin-1') for graph in A['data']: G = nx.from_numpy_matrix(graph) graphs.append(G) args.max_prev_node = 63 return graphs
def gen_BA_graph(n, k): """ generate one node at a time, connecting preferentially to higher degree nodes """ return nx.barabasi_albert_graph(n, k)
B.add_edge(u, v, weight=w[weight]) return B else: B = nx.Graph() #Undirected case: for u, v, w in G.edges(data=True): try: alpha = w['alpha'] except KeyError: #there is no alpha, so we assign 1. It will never pass the cut alpha = 1 if alpha < alpha_t: B.add_edge(u, v, weight=w[weight]) return B if __name__ == '__main__': G = nx.barabasi_albert_graph(1000, 5) for u, v in G.edges(): G[u][v]['weight'] = np.random.randint(1, 100) alpha = 0.05 G = disparity_filter(G) G2 = nx.Graph([(u, v, d) for u, v, d in G.edges(data=True) if d['alpha'] < alpha]) print 'alpha = %s' % alpha print 'original: nodes = %s, edges = %s' % (G.number_of_nodes(), G.number_of_edges()) print 'backbone: nodes = %s, edges = %s' % (G2.number_of_nodes(), G2.number_of_edges()) print G2.edges(data=True)
def test(n=1, num_nodes=500000, debug=True, eid=0, tid=0, gid=0): global cluster_nodes, node_clusters, g if not g: g = nx.barabasi_albert_graph(num_nodes, 5) #g = nx.erdos_renyi_graph(1000000, .1) #g = nx.random_lobster(100000, .4, .4) #g = nx.random_powerlaw_tree(10000) #g = nx.powerlaw_cluster_graph(100000, 10, .2) #print str(g.adj) #nx.draw(g) #plt.show() #num_landmarks = 20 print "Running Louvain algorithm" cluster_nodes, node_clusters = louvain_clustering(g) #print "Cluster Nodes:", str(len(node_clusters)) #ALP will have this many landmarks as well num_landmarks = len(cluster_nodes.keys()) print "Cluster Nodes:", str(num_landmarks) first = True import calendar #time since epoch print "Running queries..." cur_time = calendar.timegm(time.gmtime()) sys.stdout.flush() for i in range(n): num_alp_calculations = 0 num_alp_estimates = 0 s = choice(g.nodes()) t = choice(g.nodes()) if debug: print "S:", str(s) print "T:", str(t) if debug: print "Running A* with no heuristic (Dijkstra's)" start = time.time() path, astar_size = pp.astar_path(g,s,t, search_space_size=True) if debug: print "A* (no heuristic): ", str(path), "Size:", astar_size end = time.time() empty_a_star_time = end - start #print str(end - start), "seconds." num_alp_calculations = 0 num_alp_estimates = 0 start = time.time() path, alp_size = pp.astar_path(g,s,t,ALP, search_space_size=True) if debug: print "A* (ALP)): ", str(path), "Size:", alp_size, "Number of ALP calculations:", str(num_alp_calculations), "Numbber of ALP Estimates:", str(num_alp_estimates) end = time.time() alp_time = end - start #print str(end - start), "seconds." num_alt_calculations = 0 num_alt_estimates = 0 start = time.time() path, alt_size = pp.astar_path(g,s,t,ALT, search_space_size=True) if debug: print "A* (ALT)): ", str(path), "Size:", alt_size, "Number of ALT calculations:", str(num_alt_calculations), "Numbber of ALT Estimates:", str(num_alt_estimates) end = time.time() alt_time = end - start #print str(end - start), "seconds." if debug: print(str(empty_a_star_time) + ',' + str(astar_size) + ',' + str(alt_time) + ',' + str(alt_size) + ',' + str(num_alt_calculations) + ',' + str(num_alt_estimates) + ',' + str(alp_time) + ',' + str(alp_size) + ',' + str(num_alp_calculations) + ',' + str(num_alp_estimates) + ',' + str(len(path)) + ',' + str(num_landmarks) + '\n') if first: first = False else: a_star_sql = "(NULL," + str(tid) + "," + str(heuristics.DIJKSTRA) + ", NULL," + str(s) + "," + str(t) + "," + str(len(path)) + ", NULL," + str(empty_a_star_time) + "," + str(astar_size) + ",0,0)" alt_sql = "(NULL," + str(tid) + "," + str(heuristics.ALT_OPT) + "," + str(embedding_methods.RANDOM) + "," + str(s) + "," + str(t) + "," + str(len(path)) + "," + str(len(landmarks)) + "," + str(alt_time) + "," + str(alt_size) + "," + str(num_alt_calculations) + "," + str(num_alt_estimates) + ")" alp_sql = "(NULL," + str(tid) + "," + str(heuristics.ALP_OPT) + "," + str(embedding_methods.RANDOM) + "," + str(s) + "," + str(t) + "," + str(len(path)) + "," + str(num_landmarks) + "," + str(alp_time) + "," + str(alp_size) + "," + str(num_alp_calculations) + "," + str(num_alp_estimates) + ")" query_sql = "INSERT INTO query VALUES " + a_star_sql + ", " + alt_sql + ", " + alp_sql if debug: print "QUERY SQL:", query_sql dba.query_insert(query_sql) #insert trial tend_time = time.time() #trial has ended. wipe the graph #start with ALT for v in g.nodes(): for l in landmarks: if 'ALT_'+str(l) in g.node[v]: del g.node[v]['ALT_'+str(l)] #now ALP for l in alp_landmarks: for v in [n for n in g.nodes() if 'ALP_'+str(l) in g.node[n]]: del g.node[v]['ALP_' + str(l)]
for i in range(number_of_seeds)] winner_change_graph_avg = [0 for j in range(xLengthGraph)] numOfIteration = 0 for seedi in range(number_of_seeds): seede = 363 + seedi * 140 random.seed(seede) print("######################### next seed , the seed is ", seede, "#################") for a1 in range(xLengthGraph): Clean(winnerVotes) num_of_Friends = 300 + (50 * a1) #300-800 friends barabasiGraph = barabasi_albert_graph(num_of_Friends, num_of_Arcs, seed=seede) # מתחיל לייצר קשתות רק כאשר יש לו אמ קודקודים ברשת # (n-m)m = edge edges = nx.edges(barabasiGraph) # print(edges) friends = [set() for j in range(len(barabasiGraph))] # [(0, 2), (0, 3), (0, 1), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4)] Opinions = creatOpinions(barabasiGraph, typeOfVotes, Opinions2, winnerVotes) # print(np.matrix(Opinions)) winnerStart = getWinner(winnerVotes, typeOfVotes)
pylab.rcParams['xtick.major.pad'] = '8' pylab.rcParams['ytick.major.pad'] = '8' Trans = 5000 nNodes = int(os.getenv('Nodes')) initial_nodes = int(os.getenv('Initial_Nodes')) Which_network = os.getenv('Network') Mean1 = [] ColorMap = [] if (Which_network == 'BARABASI'): Mean = [] in_Degree = [] for Seed in range(1, 51): path = 'Output_Scale_free_1_init_node/Output_Network_Seed_%04i/Output_0001/Output_alpha_20.00/Output_beta_30.00/' % Seed G = nx.barabasi_albert_graph(nNodes, initial_nodes, seed=Seed) H = G.to_directed() A = np.loadtxt('%s/Data_0155.dat' % path, unpack=True) C = np.loadtxt('%s/List_Reg_not_sorted.dat' % path, unpack=True) C.view('f8,f8,f8').sort(order=['f0'], axis=-1) for i in range(1, nNodes + 1): Mean.append(A[i][Trans:].mean()) for i in range(nNodes): in_Degree.append(H.in_degree(i)) ColorMap.append(C[2][i]) fig = matplotlib.pyplot.gcf() axes = fig.add_subplot(111) fig.set_size_inches(14.5, 10.5)
tree_prior = TreePrior(n) prior = Prior(n, Param, alpha=.5, tree_prior=tree_prior) g = Param(n) n_edges = n * (n - 1) // 2 while g.EdgeCount() <= int(.1 * n_edges) or g.EdgeCount() > int(.4 * n_edges): g = prior.Sample() graphs.append(g.copy()) g = Param(n) while g.EdgeCount() <= int(.6 * n_edges) or g.EdgeCount() > int(.9 * n_edges): g = prior.Sample() graphs.append(g.copy()) # Random Graph ### change this to only graph in the cycle space g = Param(n, nx.to_dict_of_lists(nx.erdos_renyi_graph(n, .2, seed=123))) graphs.append(g.copy()) g = Param(n, nx.to_dict_of_lists(nx.barabasi_albert_graph(n, 2, seed=123))) graphs.append(g.copy()) names = ["empty", "circle", "random0", "random1", "random2", "random3"] assert(len(names) == len(graphs)) for i in range(len(names)): generate_data_and_save(n, n_obs, graphs[i], f"data/{names[i]}_{n}_{n_obs}.dat", threshold=None, seed=123) import pickle for i in range(len(names)): with open(f"data/graph_{names[i]}_{n}_{n_obs}.pkl", 'wb') as handle: pickle.dump(graphs[i], handle)
def simulate_series(simulation_data): disease_params = simulation_data #disease_params['order'] = 1 simulation_data['G'] = simulation_data['network_n'] # genes simulation_data['S'] = simulation_data['P'] simulation_data['patients_number'] = 2 * simulation_data['S'] # make network if (simulation_data['network_type'] == 'BA'): g = nx.barabasi_albert_graph(simulation_data['network_n'], simulation_data['network_m']) elif (simulation_data['network_type'] == 'ER'): g = nx.erdos_renyi_graph(simulation_data['network_n'], simulation_data['network_p']) elif (simulation_data['model'] == '2D'): g = nx.grid_2d_graph(simulation_data['network_x'], simulation_data['network_y'], periodic=True) elif (simulation_data['model'] == 'CYC'): g = nx.cycle_graph(simulation_data['network_n']) elif (simulation_data['model'] == 'REG'): g = nx.random_regular_graph(simulation_data['network_d'], simulation_data['network_n']) #neighbors_dict = all_neighbors_order(g, simulation_params['order']) colored_graph = color_nodes_order(g, disease_params['D'], disease_params['p'], disease_params['order']) #colored_graph = color_nodes_order(g, disease_params['D'], disease_params['p'], disease_params['order']) g_strip = g.copy() #solitary = [ n for n,d in g_strip.degree_iter() if d == 0 ] solitary = [n for n, d in g_strip.degree() if d == 0] g_strip.remove_nodes_from(solitary) layout = nx.spring_layout(g_strip) result = {} #result['layout'] = layout #result['g'] = g #result['g_strip'] = g_strip for disease_params['rho_0'] in disease_params['rho_0_list']: result[str(disease_params['rho_0'])] = {} result_disease = simulate_artificial_disease(disease_params, simulation_data, colored_graph, colored_graph) collective_genes = get_collective_gene_expression( simulation_data, result_disease['expressed_genes_under'], result_disease['expressed_genes_over'], result_disease['phenotype_table'], mode='normal') filtered = collective_genes['flt'] for flt in simulation_data['f_list']: #result[str(disease_params['rho_0'])][str(flt)] = {} tmp_result = {} tmp_result['expressed_genes_sick'] = result_disease[ 'expressed_genes_sick'] tmp_result['expressed_genes_healthy'] = result_disease[ 'expressed_genes_healthy'] tmp_result['extracted_genes'] = list( set(filtered['dis_over_flt_' + str(flt)])) tmp_result['disease_genes'] = list( set(filtered['dis_under_flt_' + str(flt)])) tmp_result['true_poositive_genes'] = list( set(filtered['dis_under_flt_' + str(flt)]) & set(filtered['dis_over_flt_' + str(flt)])) tmp_result['disease_params'] = disease_params tmp_result['layout'] = layout tmp_result['g'] = g tmp_result['g_strip'] = g_strip tmp_result['rho_0'] = disease_params['rho_0'] tmp_result['flt'] = flt result[str(disease_params['rho_0'])][str(flt)] = tmp_result return result
# Prints out statistics of real network info(G) # Plots degree distribution of real network plot(G) # Prints out statistics of Erdös-Rényi random graph ER = nx.gnm_random_graph(G.number_of_nodes(), G.number_of_edges()) ER.name = 'Erdös-Rényi' info(ER) # Prints out statistics of Barabási–Albert scale-free graph BA = nx.barabasi_albert_graph( G.number_of_nodes(), round(G.number_of_edges() / G.number_of_nodes())) BA.name = 'Barabási–Albert' info(BA) # Prints out statistics of Watts–Strogatz small-world graph WS = nx.watts_strogatz_graph( G.number_of_nodes(), round(2.0 * G.number_of_edges() / G.number_of_nodes()), 0.05) WS.name = 'Watts–Strogatz' info(WS) print("{0:>15s} | {1:.1f} sec\n".format('Total', time() - tic))
for graph_type in range(12, 21): if first: sizes = [175000, 300000] first = False else: sizes = [500, 2500, 12500, 60000, 175000, 300000] first = False try: #choose different graph size (x2) for num_n in sizes: #choose different graph g_name = "NULL" print "Forming graph of size ", str(num_n) print str(graph_type) if graph_type == 0: g = nx.barabasi_albert_graph(num_n, 2) g_name = 'NETWORKX.BARABASI_ALBERT_2' elif graph_type == 1: g = nx.barabasi_albert_graph(num_n, 3) g_name = 'NETWORKX.BARABASI_ALBERT_3' elif graph_type == 2: g = nx.barabasi_albert_graph(num_n, 5) g_name = 'NETWORKX.BARABASI_ALBERT_5' elif graph_type == 3: g = nx.barabasi_albert_graph(num_n, 7) g_name = 'NETWORKX.BARABASI_ALBERT_7' elif graph_type == 4: g = nx.barabasi_albert_graph(num_n, 9) g_name = 'NETWORKX.BARABASI_ALBERT_9' elif graph_type == 5: g = nx.barabasi_albert_graph(num_n, 11)
#!/usr/bin/env python import networkx as nx #nx.write_edgelist(nx.DiGraph(nx.complete_graph(100)), 'complete-small.txt', data=False) #nx.write_edgelist(nx.DiGraph(nx.barabasi_albert_graph(100, 3)), 'social-small.txt', data=False) #nx.write_edgelist(nx.DiGraph(nx.barabasi_albert_graph(200000, 3)), 'social-large-200k.txt', data=False) #nx.write_edgelist(nx.DiGraph(nx.barabasi_albert_graph(400000, 3)), 'social-large-400k.txt', data=False) nx.write_edgelist(nx.DiGraph(nx.barabasi_albert_graph(800000, 3)), 'social-large-800k.txt', data=False) #nx.write_edgelist(nx.DiGraph(nx.barabasi_albert_graph(1600000, 3)), 'social-large-1600k.txt', data=False) #nx.write_edgelist(nx.DiGraph(nx.barabasi_albert_graph(3200000, 3)), 'social-large-3200k.txt', data=False)
from sklearn.preprocessing import Normalizer, MinMaxScaler from scipy.sparse import csgraph import scipy import os from sklearn import datasets os.chdir('Documents/research/graph_bandit/code/') from bandit_model import * from utils import * os.chdir('Documents/research/graph_bandit/results/') node_num = 10 dimension = 5 step_size = 0.001 iteration = 100 pos = np.random.normal(size=(node_num, 2)) graph = nx.barabasi_albert_graph(node_num, 5) adj = np.array( nx.to_numpy_matrix(graph)) * np.random.uniform(size=(node_num, node_num)) lap = csgraph.laplacian(adj, normed=False) user_f = np.random.random(size=(node_num, dimension)) adj = rbf_kernel(user_f) np.fill_diagonal(adj, 0) lap = csgraph.laplacian(adj, normed=False) def generate_signal(lap, s_t_1): p_t = np.random.normal(size=1, scale=1) s_t = np.dot(np.exp(-lap), s_t_1) + p_t return s_t