def test_multi(self): vm = MultiPlot() g = nx.erdos_renyi_graph(1000, 0.1) model = sir.SIRModel(g) config = mc.Configuration() config.add_model_parameter('beta', 0.001) config.add_model_parameter('gamma', 0.01) config.add_model_parameter("percentage_infected", 0.05) model.set_initial_status(config) iterations = model.iteration_bunch(200) viz = VisualizeDiffusion(model, iterations) p = viz.plot() vm.add_plot(p) g = nx.erdos_renyi_graph(1000, 0.1) model = sir.SIRModel(g) config = mc.Configuration() config.add_model_parameter('beta', 0.001) config.add_model_parameter('gamma', 0.01) config.add_model_parameter("percentage_infected", 0.05) model.set_initial_status(config) iterations = model.iteration_bunch(200) viz = VisualizeDiffusion(model, iterations) p1 = viz.plot() vm.add_plot(p1) m = vm.plot() self.assertIsInstance(m, Column)
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 sir_model(g, er_g, ba_g, g_stat): print '\nSIR MODEL SIMULATION WITH GAMMA ' + g_stat.upper() + ' THAN BETA' print 'MODEL CONFIGURATION' beta = float(raw_input('INFECTION PROBABILITY: ')) gamma = float(raw_input('REMOVAL PROBABILITY: ')) if g_stat == 'smaller': while gamma >= beta: print 'ERROR! GAMMA HAS TO BE SMALLER THAN BETA!' gamma = float(raw_input('REMOVAL PROBABILITY: ')) else: while gamma <= beta: print 'ERROR! GAMMA HAS TO BE GREATER THAN BETA!' gamma = float(raw_input('REMOVAL PROBABILITY: ')) cfg = mc.Configuration() cfg.add_model_parameter('beta', beta) cfg.add_model_parameter('gamma', gamma) cfg.add_model_parameter('percentage_infected', float(raw_input('PERCENTAGE INFECTED: '))) for_comparison = dict() for network in [g, er_g, ba_g]: model_sir = sir.SIRModel(network) model_sir.set_initial_status(cfg) iterations = model_sir.iteration_bunch(200) trends_sir = model_sir.build_trends(iterations) if network is g: viz = DiffusionTrend(model_sir, trends_sir) viz.plot("../../report/images/spreading/sir/diffusion_" + g_stat + ".pdf") for_comparison['original_sir'] = [model_sir, trends_sir] elif network is er_g: viz = DiffusionTrend(model_sir, trends_sir) viz.plot("../../report/images/spreading/sir/diffusion_er_" + g_stat + ".pdf") for_comparison['er_sir'] = [model_sir, trends_sir] else: viz = DiffusionTrend(model_sir, trends_sir) viz.plot("../../report/images/spreading/sir/diffusion_ba_" + g_stat + ".pdf") for_comparison['ba_sir'] = [model_sir, trends_sir] viz = DiffusionTrendComparison([ for_comparison['original_sir'][0], for_comparison['er_sir'][0], for_comparison['ba_sir'][0] ], [ for_comparison['original_sir'][1], for_comparison['er_sir'][1], for_comparison['ba_sir'][1] ]) viz.plot("../../report/images/spreading/sir/trend_comparison_" + g_stat + ".pdf")
def test_sir_model(self): g = nx.erdos_renyi_graph(1000, 0.1) model = sir.SIRModel(g) config = mc.Configuration() config.add_model_parameter('beta', 0.5) config.add_model_parameter('gamma', 0.2) config.add_model_parameter("percentage_infected", 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10)
def test_visualize_prevalence(self): g = nx.erdos_renyi_graph(1000, 0.1) model = sir.SIRModel(g) config = mc.Configuration() config.add_model_parameter('beta', 0.001) config.add_model_parameter('gamma', 0.01) config.add_model_parameter("percentage_infected", 0.05) model.set_initial_status(config) iterations = model.iteration_bunch(200) trends = model.build_trends(iterations) viz = DiffusionPrevalence(model, trends) p = viz.plot() self.assertIsInstance(p, Figure)
def get_epidemic_sir(graph, beta, gamma, perc_inf, infected_nodes): model_sir = sir.SIRModel(graph) cfg_sir = mc.Configuration() cfg_sir.add_model_parameter("beta", beta) # infection cfg_sir.add_model_parameter("gamma", gamma) # recovery cfg_sir.add_model_parameter("percentage_infected", perc_inf) cfg_sir.add_model_parameter("Infected", infected_nodes) model_sir.set_initial_status(cfg_sir) iterations_sir = model_sir.iteration_bunch(100, node_status=True) trends_sir = model_sir.build_trends(iterations_sir) viz_sir = DiffusionTrend(model_sir, trends_sir) viz_sir.plot()
def simulate_sir_on_barabasi_albert(): print('######################################') print('Simulating SIR Model on Barabasi-Albert Graph...') print('######################################') g = nx.barabasi_albert_graph(1000, 10) model = sir.SIRModel(g) cfg = mc.Configuration() cfg.add_model_parameter('beta', 0.01) cfg.add_model_parameter('gamma', 0.005) 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_sir_on_erdos_renyi(): print('######################################') print('Simulating SIR Model on Erdős-Rényi Graph...') print('######################################') g = nx.erdos_renyi_graph(1000, 0.15) model = sir.SIRModel(g) cfg = mc.Configuration() cfg.add_model_parameter('beta', 0.01) cfg.add_model_parameter('gamma', 0.005) 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 test_visualize(self): g = nx.erdos_renyi_graph(1000, 0.1) model = sir.SIRModel(g) config = mc.Configuration() config.add_model_parameter('beta', 0.001) config.add_model_parameter('gamma', 0.01) config.add_model_parameter("percentage_infected", 0.05) model.set_initial_status(config) iterations = model.iteration_bunch(200) trends = model.build_trends(iterations) # Visualization viz = DiffusionTrend(model, trends) viz.plot("diffusion.pdf") os.remove("diffusion.pdf")
def sirModel(graph, iteration): # Model Selection sir_model = sir.SIRModel(graph) # Model Configuration config = mc.Configuration() config.add_model_parameter('beta', 0.005) config.add_model_parameter('gamma', 0.01) config.add_model_parameter("percentage_infected", 0.01) sir_model.set_initial_status(config) # Simulation iterations = sir_model.iteration_bunch(iteration) trends = sir_model.build_trends(iterations) viz = DiffusionTrend(sir_model, trends) p = viz.plot(width=800, height=800) show(p)
def create_sir_model(graph, beta=0.01, gamma=0.01, infected=0.01): """ Susceptible 0 Infected 1 Removed 2 beta - probability of transition from S to I gamma - probability of transition from I to R infected - part of population infected at the begining """ model = sir.SIRModel(graph) # Model Configuration cfg = mc.Configuration() cfg.add_model_parameter('beta', beta) cfg.add_model_parameter('gamma', gamma) cfg.add_model_parameter('percentage_infected', infected) model.set_initial_status(cfg) return model
def test_multi(self): # Network topology g = nx.erdos_renyi_graph(1000, 0.1) # Model selection model1 = sir.SIRModel(g) # Model Configuration config = mc.Configuration() config.add_model_parameter('beta', 0.001) config.add_model_parameter('gamma', 0.01) config.add_model_parameter("percentage_infected", 0.05) model1.set_initial_status(config) # Simulation multiple execution trends = multi_runs(model1, execution_number=10, iteration_number=100, infection_sets=None, nprocesses=4) self.assertIsNotNone(trends)
def test_multi_initial_set(self): # Network topology g = nx.erdos_renyi_graph(1000, 0.1) # Model selection model1 = sir.SIRModel(g) # Model Configuration config = mc.Configuration() config.add_model_parameter('beta', 0.001) config.add_model_parameter('gamma', 0.01) model1.set_initial_status(config) # Simulation multiple execution infection_sets = [(1, 2, 3, 4, 5), (3, 23, 22, 54, 2), (98, 2, 12, 26, 3), (4, 6, 9)] trends = multi_runs(model1, execution_number=4, iteration_number=100, infection_sets=infection_sets, nprocesses=4) self.assertIsNotNone(trends)
import networkx as nx import ndlib.models.epidemics.SIRModel as sir import ndlib.models.ModelConfig as mc from bokeh.io import output_notebook, show from ndlib.viz.bokeh.DiffusionTrend import DiffusionTrend from ndlib.viz.bokeh.DiffusionPrevalence import DiffusionPrevalence from ndlib.viz.bokeh.MultiPlot import MultiPlot import ndlib.models.epidemics.SISModel as sis import ndlib.models.epidemics.SIModel as si import ndlib.models.epidemics.ThresholdModel as th # Network Definition g = nx.erdos_renyi_graph(1000, 0.1) # Model Selection model = sir.SIRModel(g) # Model Configuration config = mc.Configuration() config.add_model_parameter('beta', 0.001) config.add_model_parameter('gamma', 0.01) config.add_model_parameter("percentage_infected", 0.05) model.set_initial_status(config) # Simulation iterations = model.iteration_bunch(200) trends = model.build_trends(iterations) viz = DiffusionTrend(model, trends) p = viz.plot(width=400, height=400) show(p)