Skip to content

Instantly share code, notes, and snippets.

@MJacobs1985
Created September 29, 2021 09:49
Show Gist options
  • Save MJacobs1985/d8dbb36fe2ccc7106e0f71e1f486cdb4 to your computer and use it in GitHub Desktop.
Save MJacobs1985/d8dbb36fe2ccc7106e0f71e1f486cdb4 to your computer and use it in GitHub Desktop.
Network Graphs for Epidemiolgy
import networkx as nx
import matplotlib.pyplot as plt
import dynetx as dn
import ndlib.models.ModelConfig as mc
import ndlib.models.dynamic as dm
from past.builtins import xrange
import ndlib.models.epidemics as ep
from ndlib.utils import multi_runs
from ndlib.viz.mpl.DiffusionTrend import DiffusionTrend
from ndlib.viz.mpl.TrendComparison import DiffusionTrendComparison
import warnings
warnings.filterwarnings("ignore")
%matplotlib inline
# Network topology
g = nx.random_geometric_graph(1000, 0.1, seed=896803) # Random Clustered graph 1000 nodes - distance =0.1, no self loops
# Model selection
model1 = ep.SIModel(g)
model2 = ep.SISModel(g)
model3 = ep.SEIRModel(g)
model4 = ep.SIRModel(g)
model5 = ep.SEISModel(g)
# Model Configuration
config = mc.Configuration()
config.add_model_parameter('beta', 0.1) # Infection probability S -> I
config.add_model_parameter('lambda', 0.5) # Recovery probability I -> S
config.add_model_parameter('gamma', 0.95) # Removal probability I -> R
config.add_model_parameter('alpha', 0.1) # Latent Period E -> I
config.add_model_parameter("fraction_infected", 0.004) # 4 infected nodes at start in the network.
model1.set_initial_status(config)
model2.set_initial_status(config)
model3.set_initial_status(config)
model4.set_initial_status(config)
model5.set_initial_status(config)
# Simulation multiple execution
e_n=4 # Execution number = to number of infection sets
i_n=365 # Iteration Number - number of days
infection_sets = [(1, 2, 3, 4, 5), (3, 23, 22, 54, 2), (98, 2, 12, 26, 3), (4, 6, 9) ]
trends1 = multi_runs(model1, execution_number=e_n, iteration_number=i_n, infection_sets=infection_sets, nprocesses=6)
trends2 = multi_runs(model2, execution_number=e_n, iteration_number=i_n, infection_sets=infection_sets, nprocesses=6)
trends3 = multi_runs(model3, execution_number=e_n, iteration_number=i_n, infection_sets=infection_sets, nprocesses=6)
trends4 = multi_runs(model4, execution_number=e_n, iteration_number=i_n, infection_sets=infection_sets, nprocesses=6)
trends5 = multi_runs(model5, execution_number=e_n, iteration_number=i_n, infection_sets=infection_sets, nprocesses=6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment