Example #1
0
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)
Example #2
0
    def test_stochastic_well_mixed(self):

        S, E, I, R = sympy.symbols("S E I R")

        N = 75000
        tmax = 100
        model = SymbolicEpiModel([S, E, I, R], N)
        model.set_processes([
            (S, I, 2, E, I),
            (I, 1, R),
            (E, 1, I),
        ])
        model.set_initial_conditions({S: N - 100, I: 100})

        tt = np.linspace(0, tmax, 10)
        result_int = model.integrate(tt)

        t, result_sim = model.simulate(tmax,
                                       sampling_dt=1,
                                       return_compartments=[S, R])

        for c, res in result_sim.items():
            #print(c, np.abs(1-res[-1]/result_int[c][-1]))
            #print(c, np.abs(1-res[-1]/result_sim[c][-1]))
            assert (np.abs(1 - res[-1] / result_int[c][-1]) < 0.05)
Example #3
0
    def test_exceptions(self):

        B, mu, t = sympy.symbols("B mu t")
        epi = SymbolicEpiModel([B])
        epi.add_fission_processes([
            (B, mu, B, B),
        ])
        epi.set_initial_conditions({B: 1})

        self.assertRaises(ValueError, epi.integrate, [0, 1])

        self.assertRaises(ValueError, SymbolicEpiModel, [t])

        self.assertRaises(ValueError,
                          epi.get_eigenvalues_at_disease_free_state)
Example #4
0
    def test_time_dependent_rates(self):

        B, t = sympy.symbols("B t")
        epi = SymbolicEpiModel([B])
        epi.add_fission_processes([
            (B, t, B, B),
        ])
        epi.set_initial_conditions({B: 1})
        result = epi.integrate([0, 3], adopt_final_state=True)
        assert (np.isclose(epi.y0[0], np.exp(3**2 / 2)))
        epi.set_initial_conditions({B: 1})
        result = epi.integrate(np.linspace(0, 3, 10000),
                               integrator='euler',
                               adopt_final_state=True)
        eul = epi.y0[0]
        real = np.exp(3**2 / 2)
        assert (np.abs(1 - eul / real) < 1e-2)
Example #5
0
    def test_changing_population_size(self):

        A, B, C, t = sympy.symbols("A B C t")
        epi = SymbolicEpiModel([A, B, C],
                               10,
                               correct_for_dynamical_population_size=True)
        epi.set_initial_conditions({A: 5, B: 5})
        epi.set_processes([
            (A, B, 1, C),
        ], allow_nonzero_column_sums=True)

        dydt = epi.dydt()
        assert (dydt[0] == -1 * A * B / (A + B + C))

        _, res = epi.simulate(1e9)
        assert (res[C][-1] == 5)

        epi.set_processes([
            (None, 1 + sympy.log(1 + t), A),
            (A, 1 + sympy.log(1 + t), B),
            (B, 1 + sympy.log(1 + t), None),
        ],
                          allow_nonzero_column_sums=True)

        rates, comp_changes = epi.get_numerical_event_and_rate_functions()
        _, res = epi.simulate(200, sampling_dt=0.05)

        vals = np.concatenate([res[A][_ > 10], res[B][_ > 10]])
        rv = poisson(vals.mean())
        measured, bins = np.histogram(vals,
                                      bins=np.arange(10) - 0.5,
                                      density=True)
        theory = [
            rv.pmf(i) for i in range(0,
                                     len(bins) - 1) if measured[i] > 0
        ]
        experi = [
            measured[i] for i in range(0,
                                       len(bins) - 1) if measured[i] > 0
        ]
        # make sure the kullback-leibler divergence is below some threshold
        assert (entropy(theory, experi) < 2e-3)
        assert (np.median(res[A]) == 1)