コード例 #1
0
def test_total_infected():
    params = Params(
        total_population=5,
        initial_num_E=1,
        initial_num_I=0,
        initial_num_M=0,
        mu_ei=14,  # takes a long time for E to goto I
        alpha=1.0,  # E is very infectivous
        beta=0.0
    )

    params_vac = ParamsVac(
        vac_time=1,
        vac_count_per_day=2,
        gamma=1.0,
        s_proba=0.0,
        v2_proba=0.0,
        v1_proba=1.0,  # all go to V1
        time_to_take_effect=1,
        ev1_to_r_time=100,
        **params.kwargs
    )

    total_days = 5
    sim = SimulatorWithVaccination(params_vac, total_days=total_days, verbose=VERBOSE)
    sim.run()

    assert np.isclose(sim.total_infected, 5)  # EV1 should be counted as well
コード例 #2
0
    gamma=0.001)

# Issue 3 ends here

p0_time = T('29/11/2019')  # set the start time of epidemic

# **Issue 1:**
# the `bed_info` below was for the Wuhan case
# bed_info = pkl.load(open('data/bed_info.pkl', 'rb'))
# to set a fixed value for bed info, use the following
bed_info = [(0, 3000)
            ]  # say there are 3000 beds at the begining of the epidemic

total_days = 90

sim = SimulatorWithVaccination(params, p0_time, total_days, bed_info)

# **Issue 2:**
# the returned variables below store time-dependent statistics
# - total_array: total number of each state on each day
# - delta_array: the delta (change of value of day t w.r.t day t-1) value of each state on each day
# - delta_array: the delta plus (newly incoming population on day t w.r.t day t-1) value of each state on each day
# - trans_array: number of transitions of each transition type (S->E, E->I, etc) on each day
# - stats: some statistics
total_array, delta_array, delta_plus_array, trans_array, stats = sim.run()

# to access the value of some state (say V1) on some variable (say total_array), do the following
total_V1_over_time = total_array[:, sim.state_space.V1]
total_V2_over_time = total_array[:, sim.state_space.V2]

sim.set_state_ids_to_plot(exclude_ids=[sim.state_space.S])
コード例 #3
0
def test_infection_from_E_and_EV1_slower_vaccination():
    params = Params(
        total_population=5,
        initial_num_E=1,
        initial_num_I=0,
        initial_num_M=0,
        mu_ei=14,  # takes a long time for E to goto I
        alpha=1.0,  # E is very infectivous
        beta=0.0
    )

    params_vac = ParamsVac(
        vac_time=1,
        vac_count_per_day=2,
        gamma=1.0,
        s_proba=0.0,
        v2_proba=0.0,
        v1_proba=1.0,  # all go to V1
        time_to_take_effect=1,
        **params.kwargs
    )

    total_days = 5
    sim = SimulatorWithVaccination(params_vac, total_days=total_days, verbose=VERBOSE)
    sim.run()

    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.S],
        [4, 0, 0, 0, 0, 0]
    )
    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.S],
        [4, -4, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.S],
        [4, 0, 0, 0, 0, 0]
    )

    # note that 2 S become E (not tested here)
    
    # check V array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V],
        [0, 2, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V],
        [0, 2, -2, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V],
        [0, 2, 0, 0, 0, 0]
    )

    # check V1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V1],
        [0, 0, 2, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V1],
        [0, 0, 2, -2, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V1],
        [0, 0, 2, 0, 0, 0]
    )

    # check EV1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.EV1],
        [0, 0, 0, 2, 2, 2]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.EV1],
        [0, 0, 0, 2, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.EV1],
        [0, 0, 0, 2, 0, 0]
    )
コード例 #4
0
def test_EV1_to_O_slower_vaccination():
    """EV1 must move to O in the end
    vaccination is slower (takes 2 batches)
    """
    params = Params(
        total_population=5,
        initial_num_E=1,
        initial_num_I=0,
        initial_num_M=0,
        mu_ei=14,  # takes a long time for E to goto I
        alpha=1.0,  # E is very infectivous
        beta=0.0
    )

    params_vac = ParamsVac(
        vac_time=1,
        vac_count_per_day=2,
        gamma=1.0,
        s_proba=0.0,
        v2_proba=0.0,
        v1_proba=1.0,  # all go to V1
        time_to_take_effect=1,
        ev1_to_r_time=1,
        **params.kwargs
    )

    total_days = 5
    sim = SimulatorWithVaccination(params_vac, total_days=total_days, verbose=VERBOSE)

    sim.run()

    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.S],
        [4, 0, 0, 0, 0, 0]
    )
    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.S],
        [4, -4, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.S],
        [4, 0, 0, 0, 0, 0]
    )

    # check V array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V],
        [0, 2, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V],
        [0, 2, -2, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V],
        [0, 2, 0, 0, 0, 0]
    )

    # check V1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V1],
        [0, 0, 2, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V1],
        [0, 0, 2, -2, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V1],
        [0, 0, 2, 0, 0, 0]
    )

    # check EV1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.EV1],
        [0, 0, 0, 2, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.EV1],
        [0, 0, 0, 2, -2, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.EV1],
        [0, 0, 0, 2, 0, 0]
    )

    # check O array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.O],
        [0, 0, 0, 0, 2, 2]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.O],
        [0, 0, 0, 0, 2, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.O],
        [0, 0, 0, 0, 2, 0]
    )        
コード例 #5
0
def test_infection_from_E_and_EV1():
    params = Params(
        total_population=6,
        initial_num_E=1,
        initial_num_I=0,
        initial_num_M=0,
        mu_ei=14,  # takes a long time for E to goto I
        alpha=10.0,  # E is very infectivous
        beta=0.0
    )

    params_vac = ParamsVac(
        vac_time=1,
        vac_count_per_day=5,
        gamma=1.0,
        s_proba=0.0,
        v2_proba=0.0,
        v1_proba=1.0,  # all go to V1
        time_to_take_effect=1,
        **params.kwargs
    )

    total_days = 3
    sim = SimulatorWithVaccination(params_vac, total_days=total_days, verbose=VERBOSE)
    assert sim.inf_proba_EV1 == 0.0
    sim.run()
    sim.update_inf_probas(total_days + 1)
    assert sim.inf_proba_EV1 > 0.0
    assert sim.inf_proba == 1.0

    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.S],
        [5, 0, 0, 0]
    )
    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.S],
        [5, -5, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.S],
        [5, 0, 0, 0]
    )

    # check V array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V],
        [0, 5, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V],
        [0, 5, -5, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V],
        [0, 5, 0, 0]
    )

    # check V1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V1],
        [0, 0, 5, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V1],
        [0, 0, 5, -5]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V1],
        [0, 0, 5, 0]
    )

    # check EV1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.EV1],
        [0, 0, 0, 5]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.EV1],
        [0, 0, 0, 5]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.EV1],
        [0, 0, 0, 5]
    )
コード例 #6
0
def test_imprefect_vaccination_2(infectiousless_params):
    """test case: no infection + imperfect vaccination (v2_proba < 1)
    time for vaccination to take effect is short

    note that s_proba > 0 and v1_proba = 0
    """
    params_vac = ParamsVac(
        vac_time=2,
        vac_count_per_day=2,
        gamma=0.0,
        s_proba=0.5,
        v2_proba=0.5,
        v1_proba=0.0,
        time_to_take_effect=1,
        **infectiousless_params.kwargs
    )

    total_days = 8

    sim = SimulatorWithVaccination(params_vac, total_days=total_days, verbose=VERBOSE)
    sim.run()
    # check S array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.S],
        [5, 5, 3, 2, 1, 1, 1/2, 1/2, 1/4]
    )
    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.S],
        [5, 0, -2, -1, -1, 0, -1/2, 0, -1/4]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.S],
        [5, 0, 0, 1, 1, 1, 1/2, 1/2, 1/4]
    )

    # check V array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V],
        [0, 0, 2, 2, 2, 1, 1, 1/2, 1/2]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V],
        [0, 0, 2, 0, 0, -1, 0, -1/2, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V],
        [0, 0, 2, 2, 2, 1, 1, 1/2, 1/2]
    )

    # check V1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0, 0, 0, 0]
    )
    
    # check V2 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V2],
        [0, 0, 0, 1, 2, 3, 3+1/2, 4, 4+1/4]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V2],
        [0, 0, 0, 1, 1, 1, 1/2, 1/2, 1/4]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V2],
        [0, 0, 0, 1, 1, 1, 1/2, 1/2, 1/4]
    )
コード例 #7
0
def test_worst_possible_vaccination_2(infectiousless_params):
    """test case: no infection + worst possible vaccination (s_proba = 1.0)
    time for vaccination to take effect is 2 days
    """
    params_vac = ParamsVac(
        vac_time=2,
        vac_count_per_day=2,
        gamma=0.0,
        s_proba=1.0,
        v2_proba=0.0,
        v1_proba=0.0,
        time_to_take_effect=2,
        **infectiousless_params.kwargs
    )

    total_days = 6

    sim = SimulatorWithVaccination(params_vac, total_days=total_days, verbose=VERBOSE)
    sim.run()
    # check S array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.S],
        [5, 5, 3, 1, 2, 2, 1]
    )
    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.S],
        [5, 0, -2, -2, 1, 0, -1]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.S],
        [5, 0, 0, 0, 2, 2, 1]
    )

    # check V array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V],
        [0, 0, 2, 4, 3, 3, 4]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V],
        [0, 0, 2, 2, -1, 0, 1]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V],
        [0, 0, 2, 2, 1, 2, 2]
    )

    # check V1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0, 0]
    )
    
    # check V2 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V2],
        [0, 0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V2],
        [0, 0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V2],
        [0, 0, 0, 0, 0, 0, 0]
    )
コード例 #8
0
def test_prefect_vaccination_1(infectiousless_params):
    """test case: no infection + perfect vaccination (v2_proba = 1)
    and it takes long for the vaccination to take effect (people stay in V)
    """
    params_vac = ParamsVac(
        vac_time=2,
        vac_count_per_day=2,
        gamma=0.0,
        s_proba=.0,
        v2_proba=1.0,
        v1_proba=0.0,
        time_to_take_effect=100,
        **infectiousless_params.kwargs
    )

    total_days = 5

    sim = SimulatorWithVaccination(params_vac, total_days=total_days, verbose=VERBOSE)
    sim.run()
    # check S array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.S],
        [5, 5, 3, 1, 0, 0]
    )
    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.S],
        [5, 0, -2, -2, -1, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.S],
        [5, 0, 0, 0, 0, 0]
    )

    # check V array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V],
        [0, 0, 2, 4, 5, 5]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V],
        [0, 0, 2, 2, 1, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V],
        [0, 0, 2, 2, 1, 0]
    )

    # check V1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0]
    )
    
    # check V2 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V2],
        [0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V2],
        [0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V2],
        [0, 0, 0, 0, 0, 0]
    )
コード例 #9
0
def test_prefect_vaccination_2(infectiousless_params):
    """test case: no infection + perfect vaccination (v2_proba = 1)
    time for vaccination to take effec is short
    """
    params_vac = ParamsVac(
        vac_time=2,
        vac_count_per_day=2,
        gamma=0.0,
        s_proba=.0,
        v2_proba=1.0,
        v1_proba=0.0,
        time_to_take_effect=1,
        **infectiousless_params.kwargs
    )

    total_days = 5

    sim = SimulatorWithVaccination(params_vac, total_days=total_days, verbose=VERBOSE)
    sim.run()
    # check S array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.S],
        [5, 5, 3, 1, 0, 0]
    )
    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.S],
        [5, 0, -2, -2, -1, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.S],
        [5, 0, 0, 0, 0, 0]
    )

    # check V array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V],
        [0, 0, 2, 2, 1, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V],
        [0, 0, 2, 0, -1, -1]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V],
        [0, 0, 2, 2, 1, 0]
    )

    # check V1 array
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V1],
        [0, 0, 0, 0, 0, 0]
    )
    
    # check V2 array
    # basically, V2 is the same as V but shifted by `time_to_take_effect` days
    np.testing.assert_almost_equal(
        sim.total_array[:, sim.state_space.V2],
        [0, 0, 0, 2, 4, 5]
    )

    np.testing.assert_almost_equal(
        sim.delta_array[:, sim.state_space.V2],
        [0, 0, 0, 2, 2, 1]
    )

    np.testing.assert_almost_equal(
        sim.delta_plus_array[:, sim.state_space.V2],
        [0, 0, 0, 2, 2, 1]
    )