def default_numeric_models(): from epipack import EpiModel import matplotlib.pyplot as pl from epipack.plottools import plot import numpy as np S, I, R = list("SIR") N = 1000 SIRS = EpiModel([S,I,R],N)\ .set_processes([ #### transmission process #### # S + I (eta=2.5/d)-> I + I (S, I, 2.5, I, I), #### transition processes #### # I (rho=1/d)-> R # R (omega=1/14d)-> S (I, 1, R), (R, 1/14, S), ])\ .set_initial_conditions({S:N-10, I:10}) t = np.linspace(0, 40, 1000) result_int = SIRS.integrate(t) t_sim, result_sim = SIRS.simulate(t[-1]) ax = plot(t_sim, result_sim) ax = plot(t, result_int, ax=ax) ax.get_figure().savefig('numeric_model.png', dpi=300)
def symbolic_simulation_temporally_forced(): from epipack import SymbolicEpiModel import sympy as sy from epipack.plottools import plot import numpy as np S, I, R, eta, rho, omega, t, T = \ sy.symbols("S I R eta rho omega t T") N = 1000 SIRS = SymbolicEpiModel([S,I,R],N)\ .set_processes([ (S, I, 3+sy.cos(2*sy.pi*t/T), I, I), (I, rho, R), (R, omega, S), ]) SIRS.set_parameter_values({ rho: 1, omega: 1 / 14, T: 100, }) SIRS.set_initial_conditions({S: N - 100, I: 100}) _t = np.linspace(0, 150, 1000) result = SIRS.integrate(_t) t_sim, result_sim = SIRS.simulate(max(_t)) ax = plot(_t, result) plot(t_sim, result_sim, ax=ax) ax.get_figure().savefig('symbolic_model_time_varying_rate.png', dpi=300)
def varying_rate_numeric_models(): import numpy as np from epipack import SISModel from epipack.plottools import plot N = 100 recovery_rate = 1.0 def infection_rate(t, y, *args, **kwargs): return 3 + np.sin(2 * np.pi * t / 100) SIS = SISModel( infection_rate=infection_rate, recovery_rate=recovery_rate, initial_population_size=N )\ .set_initial_conditions({ 'S': 90, 'I': 10, }) t = np.arange(200) result_int = SIS.integrate(t) t_sim, result_sim = SIS.simulate(199) ax = plot(t_sim, result_sim) ax = plot(t, result_int, ax=ax) ax.get_figure().savefig('numeric_model_time_varying_rate.png', dpi=300)
def network_simulation(): from epipack import StochasticEpiModel from epipack.plottools import plot import matplotlib.pyplot as pl import networkx as nx k0 = 50 R0 = 2.5 rho = 1 eta = R0 * rho / k0 omega = 1 / 14 N = int(1e4) edges = [ (e[0], e[1], 1.0) for e in \ nx.fast_gnp_random_graph(N,k0/(N-1)).edges() ] SIRS = StochasticEpiModel( compartments=list('SIR'), N=N, edge_weight_tuples=edges )\ .set_link_transmission_processes([ ('I', 'S', eta, 'I', 'I'), ])\ .set_node_transition_processes([ ('I', rho, 'R'), ('R', omega, 'S'), ])\ .set_random_initial_conditions({ 'S': N-100, 'I': 100 }) t_s, result_s = SIRS.simulate(40) ax = plot(t_s, result_s) ax.get_figure().savefig('network_simulation.png', dpi=300)
(2.25, 2.78), (3.0, 2.14), (3.75, 1.43), (4.5, 1.02), (5.25, 1.14), (6.0, 1.72), ]) times, rates = data[:,0], data[:,1] f = get_temporal_interpolation(times, rates, interpolation_degree=1) S, I, R, t, rho = sympy.symbols("S I R t rho") model = SymbolicEpiModel([S,I,R]) model.set_processes([ (S, I, f, I, I), (I, rho, R), ])\ .set_initial_conditions({S:0.99,I:0.01})\ .set_parameter_values({rho:1}) t = np.linspace(0,6,1000) result = model.integrate(t) ax = plot(t,result) ax.legend() ax.get_figure().savefig('interp_SIR_symbolic.png',dpi=300) pl.show()
f = interp1d(times, rates, kind='linear', bounds_error=False) def infection_rate(t,y): return f(t) model = EpiModel(list("SIR")) model.set_processes([ ('S', 'I', infection_rate, 'I', 'I'), ('I', 1.0, 'R'), ])\ .set_initial_conditions({'S':0.99,'I':0.01})\ t = np.linspace(0,6,1000) result = model.integrate(t) ax = plot(t,result) ax.legend(frameon=False) model = EpiModel(list("SIR")) model.set_processes([ ('S', 'I', 2.0, 'I', 'I'), ('I', 1.0, 'R'), ])\ .set_initial_conditions({'S':0.99,'I':0.01})\ t = np.linspace(0,6,1000) result = model.integrate(t,return_compartments='I') ax = plot(t,result,ax=ax,curve_label_format='constant rate {}') ax.set_ylim([0,1]) ax.legend(frameon=False)