def test_prevalence_comparison(self): # Network topology g = nx.erdos_renyi_graph(1000, 0.1) # Model selection model = sir.SIRModel(g) # Model Configuration cfg = mc.Configuration() cfg.add_model_parameter('beta', 0.001) cfg.add_model_parameter('gamma', 0.02) cfg.add_model_parameter("percentage_infected", 0.01) model.set_initial_status(cfg) iterations = model.iteration_bunch(200) trends = model.build_trends(iterations) model1 = si.SIModel(g) cfg = mc.Configuration() cfg.add_model_parameter('beta', 0.001) cfg.add_model_parameter("percentage_infected", 0.01) model1.set_initial_status(cfg) iterations = model1.iteration_bunch(200) trends1 = model1.build_trends(iterations) viz = DiffusionPrevalenceComparison([model, model1], [trends, trends1]) viz.plot("prev_comparison.pdf") os.remove("prev_comparison.pdf")
def test_si_model(self): g = nx.erdos_renyi_graph(1000, 0.1) model = si.SIModel(g) config = mc.Configuration() config.add_model_parameter('beta', 0.5) config.add_model_parameter("percentage_infected", 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10)
def si_model(g, er_g, ba_g): print '\nSI MODEL SIMULATION' print 'MODEL CONFIGURATION' cfg = mc.Configuration() beta = float(raw_input('INFECTION RATE: ')) cfg.add_model_parameter('beta', beta ) cfg.add_model_parameter('percentage_infected', float(raw_input('PERCENTAGE INFECTED: '))) for_comparison = dict() for network in [g, er_g, ba_g]: model_si = si.SIModel(network) model_si.set_initial_status(cfg) iterations = model_si.iteration_bunch(200) trends_si = model_si.build_trends(iterations) if network is g: print 'Original graph' model_si.name = 'G' viz = DiffusionTrend(model_si, trends_si) viz.plot('../../report/images/spreading/si/diffusion.pdf') viz = DiffusionPrevalence(model_si, trends_si) viz.plot('../../report/images/spreading/si/prevalence.pdf') for_comparison['original_si'] = [model_si, trends_si] elif network is er_g: print 'ER graph' model_si.name = 'ER' viz = DiffusionTrend(model_si, trends_si) viz.plot('../../report/images/spreading/si/diffusion_er.pdf') viz = DiffusionPrevalence(model_si, trends_si) viz.plot('../../report/images/spreading/si/prevalence_er.pdf') for_comparison['er_si'] = [model_si, trends_si] else: print 'BA graph' model_si.name = 'BA' viz = DiffusionTrend(model_si, trends_si) viz.plot('../../report/images/spreading/si/diffusion_ba.pdf') viz = DiffusionPrevalence(model_si, trends_si) viz.plot('../../report/images/spreading/si/prevalence_ba.pdf') for_comparison['ba_si'] = [model_si, trends_si] viz = DiffusionTrendComparison([ for_comparison['original_si'][0], for_comparison['er_si'][0], for_comparison['ba_si'][0] ], [ for_comparison['original_si'][1], for_comparison['er_si'][1], for_comparison['ba_si'][1] ]) viz.plot("../../report/images/spreading/si/trend_comparison_beta{}.pdf".format(beta))
def get_epidemic_si(graph, beta, perc_inf, infected_nodes): model_si = si.SIModel(graph) cfg_si = mc.Configuration() cfg_si.add_model_parameter("beta", beta) cfg_si.add_model_parameter("percentage_infected", perc_inf) cfg_si.add_model_parameter("Infected", infected_nodes) model_si.set_initial_status(cfg_si) iteration_si = model_si.iteration_bunch(50) trends_si = model_si.build_trends(iteration_si) viz_si = DiffusionTrend(model_si, trends_si) viz_si.plot()
def test_multiple_si_model(self): g = nx.erdos_renyi_graph(1000, 0.1) model = si.SIModel(g) config = mc.Configuration() config.add_model_parameter('beta', 0.01) config.add_model_parameter("percentage_infected", 0.1) model.set_initial_status(config) executions = ut.multi_runs(model, execution_number=10, iteration_number=50) self.assertEqual(len(executions), 10) iterations = model.iteration_bunch(10, node_status=False) self.assertEqual(len(iterations), 10)
def simulate_si_on_barabasi_albert(): print('######################################') print('Simulating SI Model on Barabasi-Albert Graph...') print('######################################') g = nx.barabasi_albert_graph(1000, 10) model = si.SIModel(g) cfg = mc.Configuration() cfg.add_model_parameter('beta', 0.01) cfg.add_model_parameter("percentage_infected", 0.05) model.set_initial_status(cfg) iterations = model.iteration_bunch(200) trends = model.build_trends(iterations) draw_epidemic_plot(model, trends)
def simulate_si_on_erdos_renyi(): print('######################################') print('Simulating SI Model on Erdős-Rényi Graph...') print('######################################') g = nx.erdos_renyi_graph(1000, 0.15) model = si.SIModel(g) cfg = mc.Configuration() cfg.add_model_parameter('beta', 0.01) cfg.add_model_parameter("percentage_infected", 0.05) model.set_initial_status(cfg) iterations = model.iteration_bunch(200) trends = model.build_trends(iterations) draw_epidemic_plot(model, trends)
def siModel(filename): # Network topology g = nx.read_edgelist(filename, nodetype=int, data=(('weight',float),), create_using=nx.Graph()) # Model selection model = si.SIModel(g) # Model Configuration cfg = mc.Configuration() cfg.add_model_parameter('beta', 0.34) cfg.add_model_parameter("percentage_infected", 0) model.set_initial_status(cfg) col=[] sus=[] inf=[] # Simulation execution iterations = model.iteration_bunch(4) for i in iterations: iter_ind=i['iteration'] #c=i['node_count'] node_status_list=i['status'] #print("Iteration:",iter_ind) #print(node_status_list) listOfKeys = [key for (key, value) in node_status_list.items() if value == 1] #print(listOfKeys) if(listOfKeys != []): inf.extend(listOfKeys) #print("infected nodes = ", inf) print("total nodes", len(g)) print("infected nodes", len(inf)) if(len(inf) > 5000): sys.exit() h = g.subgraph(inf) #print(len(h)) edgesOfInfectedGraph = list(h.edges()) f = open('DiffusionModel/InfectedGraph.txt', 'w') f.write(str(inf[0]) + '\n') for t in edgesOfInfectedGraph: line = ' '.join(str(x) for x in t) #print(line) f.write(line + '\n') f.close() #nx.draw_spring(h, cmap = plt.get_cmap('jet'), node_size=100, with_labels= True) #plt.show() return inf[0], h
def siModel(graph, iteration): # Model Selection si_model = si.SIModel(graph) # Model Configuration config = mc.Configuration() config.add_model_parameter('beta', 0.005) config.add_model_parameter("percentage_infected", 0.001) si_model.set_initial_status(config) # Simulation iterations = si_model.iteration_bunch(iteration) trends = si_model.build_trends(iterations) viz = DiffusionTrend(si_model, trends) p = viz.plot(width=800, height=800) show(p)
# Network topology #H = nx.karate_club_graph() fh=open("../../DataSet/blogs.txt", 'rb') H = nx.read_weighted_edgelist(fh,nodetype=int, encoding='utf-8') fh.close() #H = nx.convert_node_labels_to_integers(G, first_label=0, ordering="default", label_attribute = None) b=nx.density(H)#*3 #b=1 ex=5 it=30 #seed nodes #-------------------------------------------------------------- # Model selection model = si.SIModel(H) # Model Configuration cfg = mc.Configuration() cfg.add_model_parameter('beta', b) infected_nodes1 = [46] cfg.add_model_initial_configuration("Infected", infected_nodes1) model.set_initial_status(cfg) # Simulation execution iterations = model.iteration_bunch(it) trends1 = model.build_trends(iterations)
node_size=10, alpha=0.6, width=0.2, node_color=color) plt.show() #%% import networkx as nx import ndlib.models.ModelConfig as mc import ndlib.models.epidemics.SIModel as si # Network topology g = nx.erdos_renyi_graph(1000, 0.1) # Model selection model = si.SIModel(g) # Model Configuration cfg = mc.Configuration() cfg.add_model_parameter('beta', 0.01) cfg.add_model_parameter("percentage_infected", 0.05) model.set_initial_status(cfg) # Simulation execution iterations = model.iteration_bunch(200) trends = model.build_trends(iterations) from bokeh.io import output_notebook, show from ndlib.viz.bokeh.DiffusionTrend import DiffusionTrend viz1 = DiffusionTrend(model, trends) p = viz1.plot(width=400, height=400) # show(p)
# print the adjacency list to terminal try: nx.write_adjlist(G, sys.stdout) except TypeError: # Python 3.x nx.write_adjlist(G, sys.stdout.buffer) nx.draw(G,node_size=50, with_labels=True) plt.show() # Model Selection #model = sir.SIRModel(h) model = si.SIModel(h) #print(model) # Each model has its own parameters: in order to completely # instantiate the simulation we need to specify them using a # Configuration object: # Model Configuration config = mc.Configuration() #infection probability config.add_model_parameter('beta',0.05) #config.add_model_parameter('beta', 0.04) #recovery probability #config.add_model_parameter('lambda', 0.01) #removal probability