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 configuration_model(): func = lambda n:nx.utils.powerlaw_sequence(n, 4.0) z=nx.create_degree_sequence(100, func) G=nx.configuration_model(z) G=nx.Graph(G) G.remove_edges_from(G.selfloop_edges()) G.name = 'power law' return G
""" Random graph from given degree sequence. Draw degree histogram with matplotlib. """ __author__ = """Aric Hagberg ([email protected])""" try: import matplotlib.pyplot as plt import matplotlib except: raise import networkx as nx z=nx.create_degree_sequence(100,nx.utils.powerlaw_sequence,exponent=2.1) nx.is_valid_degree_sequence(z) print "Configuration model" G=nx.configuration_model(z) # configuration model degree_sequence=sorted(nx.degree(G).values(),reverse=True) # degree sequence #print "Degree sequence", degree_sequence dmax=max(degree_sequence) plt.loglog(degree_sequence,'b-',marker='o') plt.title("Degree rank plot") plt.ylabel("degree") plt.xlabel("rank") # draw graph in inset
if __name__ == '__main__': import time try: import igraph except ImportError: # it would be nice to implement biconnected_components in NetworkX # it is in my TODO list raise Exception('Unable to import igraph (used only to test accuracy)') Gnp = networkx.gnp_random_graph(100, 0.03) Gba = networkx.barabasi_albert_graph(100, 2) Gpc = networkx.powerlaw_cluster_graph(100, 2, 0.1) constructor = [(20, 40, 0.8), (80, 140, 0.6)] Gshell = networkx.random_shell_graph(constructor) Gshell.name = 'Shell graph' deg_seq = networkx.create_degree_sequence(100, networkx.utils.powerlaw_sequence) Gconf = networkx.Graph(networkx.configuration_model(deg_seq)) Gconf.remove_edges_from(Gconf.selfloop_edges()) Gconf.name = 'Conf model' Gdeb_2m = networkx.read_adjlist('test_2m.adj') # file in ticket #589 Gdeb_2m.name = "Debian 2-mode" Gdeb_1m = networkx.read_adjlist('test_1m.adj') # file in ticket #589 Gdeb_1m.name = "Debian 1-mode" graph_list = [Gnp, Gba, Gpc, Gshell, Gconf, Gdeb_1m, Gdeb_2m] for G in graph_list: print("Testing with %s" % G.name) print(networkx.info(G)) print('Running analysis ...') print print("New version using the complement graph in step 3") start = time.time()
if __name__ == '__main__': import time try: import igraph except ImportError: # it would be nice to implement biconnected_components in NetworkX # it is in my TODO list raise Exception('Unable to import igraph (used only to test accuracy)') Gnp = networkx.gnp_random_graph(100, 0.03) Gba = networkx.barabasi_albert_graph(100, 2) Gpc = networkx.powerlaw_cluster_graph(100, 2, 0.1) constructor=[(20,40,0.8),(80,140,0.6)] Gshell = networkx.random_shell_graph(constructor) Gshell.name = 'Shell graph' deg_seq = networkx.create_degree_sequence(100, networkx.utils.powerlaw_sequence) Gconf = networkx.Graph(networkx.configuration_model(deg_seq)) Gconf.remove_edges_from(Gconf.selfloop_edges()) Gconf.name = 'Conf model' Gdeb_2m = networkx.read_adjlist('test_2m.adj') # file in ticket #589 Gdeb_2m.name = "Debian 2-mode" Gdeb_1m = networkx.read_adjlist('test_1m.adj') # file in ticket #589 Gdeb_1m.name = "Debian 1-mode" graph_list = [Gnp, Gba, Gpc, Gshell, Gconf, Gdeb_1m, Gdeb_2m] for G in graph_list: print("Testing with %s"%G.name) print(networkx.info(G)) print('Running analysis ...') print print("New version using the complement graph in step 3") start = time.time()
def main(): # Base parameters num_runs=500 num_agents=150 # Set up directory structure for data storage data_dir="ABM_data" # Directory for all ABM data outout sub_dirs={"binom":"binomial","uni":"uniform","pref":"pref_attach","par":"pareto","pl":"power_law"} # All sub-directries # Create directories try: os.mkdir(data_dir) except(OSError): pass for d in sub_dirs.values(): try: os.mkdir(data_dir+"/"+d) except(OSError): pass ###### SIMULATION RUNS ###### for r in xrange(num_runs): ### Group 1: Binomial random networks ### # Create random GNP network, and get degree sequence gnp=nx.generators.gnp_random_graph(num_agents,p=.5) binom_ds=gnp.degree() E_BINOM=SB.Environment(population=num_agents,degree_seq=binom_ds) E_BINOM.get_data(data_dir+"/"+sub_dirs["binom"]+"/"+str(r)+"_"+sub_dirs["binom"]+".csv") ### Group 2: Uniform degree distirbution random networks ### # Create unidform degree sequence uni_ds=nx.create_degree_sequence(num_agents,nx.utils.uniform_sequence) E_UNI=SB.Environment(population=num_agents,degree_seq=uni_ds) E_UNI.get_data(data_dir+"/"+sub_dirs["uni"]+"/"+str(r)+"_"+sub_dirs["uni"]+".csv") ### Group 3: Prefential attachment by agent wealth parameter (class default) ### E_PREF=SB.Environment(population=num_agents) E_PREF.get_data(data_dir+"/"+sub_dirs["pref"]+"/"+str(r)+"_"+sub_dirs["pref"]+".csv") ### Group 4: Pareto degree distribution random networks ### par_ds=nx.create_degree_sequence(num_agents,nx.utils.pareto_sequence) E_PAR=SB.Environment(population=num_agents,degree_seq=par_ds) E_PAR.get_data(data_dir+"/"+sub_dirs["par"]+"/"+str(r)+"_"+sub_dirs["par"]+".csv") ### Group 5: Power-law degree distribution random networks ### pl_ds=nx.create_degree_sequence(num_agents,nx.utils.powerlaw_sequence) E_PL=SB.Environment(population=num_agents,degree_seq=pl_ds) E_PL.get_data(data_dir+"/"+sub_dirs["pl"]+"/"+str(r)+"_"+sub_dirs["pl"]+".csv") # Update the stdout on progress. Dumb method, but straightforward if r==num_runs*.25: # 25% complete print "Simulation "+str((float(r)/num_runs)*100)+"% complete" else: if r==num_runs*.5: print "Simulation "+str((float(r)/num_runs)*100)+"% complete" else: if r==num_runs*.75: print "Simulation "+str((float(r)/num_runs)*100)+"% complete" print "SIMULATION COMPLETE" print "" # Create single CSV file from all saved files for each network type for d in sub_dirs.values(): for r in xrange(num_runs): dr=csv.DictReader(open(data_dir+"/"+d+"/"+str(r)+"_"+d+".csv","r")) if r==0: dw=csv.DictWriter(open(data_dir+"/"+d+"/FULL_"+d+".csv", "w"),fieldnames=dr.fieldnames) header=dict(zip(dr.fieldnames,dr.fieldnames)) dw.writerow(header) for row in dr: dw.writerow(row) ### Finally, archive data ### print "Archiving data" # Zip data files into single file makeArchive(dirEntries(data_dir,True),data_dir+".zip")
""" Random graph from given degree sequence. Draw degree histogram with matplotlib. """ __author__ = """Aric Hagberg ([email protected])""" try: import matplotlib.pyplot as plt import matplotlib except: raise import networkx as nx z=nx.create_degree_sequence(400,nx.utils.powerlaw_sequence,exponent=2.1) nx.is_valid_degree_sequence(z) print "Configuration model" G=nx.configuration_model(z) # configuration model degree_sequence=sorted(nx.degree(G),reverse=True) # degree sequence #print "Degree sequence", degree_sequence dmax=max(degree_sequence) plt.loglog(degree_sequence,'b-',marker='o') plt.title("Degree rank plot") plt.ylabel("degree") plt.xlabel("rank") # draw graph in inset