Beispiel #1
0
def test_Ensemble_data():
    """tests to check data from an ensemble"""
    # Test that the ensemble of two identical models
    # behalves like twice a single model.
    # Only for the independent (ie. diagonal) case
    # Note: this test would fail if a fixed contact_matrix
    # is passed which happens to be the identity matrix
    # The difference is that when specified as independent
    # each model is booted independently
    # If a contact matrix is specified, then the boot goal
    # is the combined total of all models. So there will be a different starting point.
    test_a = Model.open_file(path_model_2)
    test_a.name = 'test_a'
    test_b = Model.open_file(path_model_2)
    test_b.name = 'test_b'
    test_b.parameters['alpha_0'].set_value(0.7)

    reference = Model.open_file(path_model_2)
    reference.name = 'reference'

    off_diag = Parameter('off_diagonal',
                         0.1,
                         parameter_min=0.,
                         parameter_max=1.,
                         description='off diagonal element of contact matrix')
    off_diags = [off_diag]

    test_ensemble = Ensemble('test_ensemble', reference)
    test_ensemble.upload_models([test_a, test_b])
    test_ensemble.define_cross_transmission('infection cycle',
                                            'infected',
                                            'susceptible',
                                            'total',
                                            'contagious',
                                            'alpha',
                                            contact_type='simple',
                                            contact=off_diags)

    n_days = 100
    norm_day = 50
    test_ensemble.reset()
    test_ensemble.evolve_expectations(norm_day)
    for key in test_ensemble.populations:
        pop = test_ensemble.populations[key]
        nu = pop.history[norm_day]
        pop.history[norm_day] = int(round(nu))
        pop.scale_future(1., expectations=False)
    for model_name in test_ensemble.models:
        model = test_ensemble.models[model_name]
        for pop_name in model.populations:
            pop = model.populations[pop_name]
            nu = pop.history[norm_day]
            pop.history[norm_day] = int(round(nu))
            pop.scale_future(1., expectations=False)

    test_ensemble.generate_data(n_days, norm_day)
    for pop_name in test_ensemble.populations:
        pop = test_ensemble.populations[pop_name]
        if pop.show_sim:
            ens_hist = test_ensemble.populations[pop_name].history
Beispiel #2
0
def test_point_estimate_local_death():
    start_day = 80
    end_day = 100
    ref_2 = Model.open_file(
        '/Users/karlen/pypm-local/models/covid19/USA/ny_4_2_0109.pypm')
    sim_2 = Model.open_file(
        '/Users/karlen/pypm-local/models/covid19/USA/ny_4_2_0109.pypm')

    # do fit of recover_frac
    for par_name in ref_2.parameters:
        par = ref_2.parameters[par_name]
        par.set_fixed()
    for par_name in ['recover_frac']:
        par = ref_2.parameters[par_name]
        par.set_variable(None, None)

    sim_2.reset()
    sim_2.generate_data(end_day)
    optimizer = Optimizer(ref_2,
                          'total deaths',
                          sim_2.populations['deaths'].history,
                          [start_day, end_day],
                          cumul_reset=True)
    optimizer.reset_variables()

    popt, pcov = optimizer.fit()

    iii = 1
Beispiel #3
0
def test_point_estimates_repeated():
    start_day = 12
    end_day = 60
    ref_2 = Model.open_file(path_model_2_2)
    sim_2 = Model.open_file(path_model_2_2)

    # do fit of alpha_0, alpha_1, cont_0
    par_names = ['alpha_0', 'alpha_1', 'cont_0']
    sums = {}
    sum2s = {}
    for par_name in par_names:
        par = ref_2.parameters[par_name]
        par.set_variable(None, None)
        sums[par_name] = 0.
        sum2s[par_name] = 0.

    n_rep = 10
    fit_stat_list = []
    for i in range(n_rep):
        sim_2.reset()
        sim_2.generate_data(end_day)
        optimizer = Optimizer(ref_2, 'total reported',
                              sim_2.populations['reported'].history,
                              [start_day, end_day])
        optimizer.reset_variables()
        popt, pcov = optimizer.fit()
        fit_stat_list.append(optimizer.fit_statistics)
        for par_name in par_names:
            value = ref_2.parameters[par_name].get_value()
            sums[par_name] += value
            sum2s[par_name] += value**2

    ass_std = {}
    ass_std['alpha_0'] = 0.03
    ass_std['alpha_1'] = 0.01
    ass_std['cont_0'] = 10.

    means = {}
    std = {}
    for par_name in par_names:
        means[par_name] = sums[par_name] / n_rep
        std[par_name] = np.sqrt(sum2s[par_name] / n_rep - means[par_name]**2)
        assert std[par_name] < ass_std[par_name]
        truth = ref_2.parameters[par_name].initial_value
        assert np.abs((means[par_name] - truth) / std[par_name] /
                      np.sqrt(1. * n_rep)) < 3.

    ndof = fit_stat_list[0]['ndof']
    chi2_list = [fit_stat_list[i]['chi2'] for i in range(n_rep)]
    chi2_mean = np.mean(chi2_list)
    assert np.abs(chi2_mean - ndof) < 8.
    acor_list = [fit_stat_list[i]['acor'] for i in range(n_rep)]
    acor_mean = np.mean(acor_list)
    assert np.abs(acor_mean) < 0.2
Beispiel #4
0
def test_Ensemble_properties_different():
    """tests to ensure the properties of Ensemble with different sub models"""
    # Test that the ensemble of two identical models
    # behalves like twice a single model.
    # Only for the independent (ie. diagonal) case
    # Note: this test would fail if a fixed contact_matrix
    # is passed which happens to be the identity matrix
    # The difference is that when specified as independent
    # each model is booted independently
    # If a contact matrix is specified, then the boot goal
    # is the combined total of all models. So there will be a different starting point.
    test_a = Model.open_file(path_model_2)
    test_a.name = 'test_a'
    test_b = Model.open_file(path_model_2)
    test_b.name = 'test_b'
    test_b.parameters['alpha_0'].set_value(0.7)
    test_c = Model.open_file(path_model_2)
    test_c.name = 'test_c'
    test_d = Model.open_file(path_model_2)
    test_d.name = 'test_d'
    test_d.parameters['alpha_0'].set_value(0.7)
    reference = Model.open_file(path_model_2)
    reference.name = 'reference'

    test_ensemble = Ensemble('test_ensemble', reference)
    test_ensemble.upload_models([test_a, test_b])
    test_ensemble.define_cross_transmission('infection cycle',
                                            'infected',
                                            'susceptible',
                                            'total',
                                            'contagious',
                                            'alpha',
                                            contact_type='diagonal')

    n_days = 100
    test_c.reset()
    test_c.evolve_expectations(n_days)
    test_d.reset()
    test_d.evolve_expectations(n_days)
    test_ensemble.reset()
    test_ensemble.evolve_expectations(n_days)
    for pop_name in test_ensemble.populations:
        pop = test_ensemble.populations[pop_name]
        if pop.show_sim:
            ens_hist = test_ensemble.populations[pop_name].history
            tc_hist = test_c.populations[pop_name].history
            td_hist = test_d.populations[pop_name].history
            for i in range(len(ens_hist)):
                ratio = ens_hist[i] / (tc_hist[i] + td_hist[i])
                assert np.abs(ratio - 1.) < 0.01
Beispiel #5
0
def test_Ensemble_properties_identical():
    """tests to ensure the properties of Ensemble"""
    # Test that the ensemble of two identical models
    # behalves like twice a single model.
    # independent of the contact matrix
    test_a = Model.open_file(path_model_2)
    test_a.name = 'test_a'
    test_b = Model.open_file(path_model_2)
    test_b.name = 'test_b'
    reference = Model.open_file(path_model_2)
    reference.name = 'reference'
    single = Model.open_file(path_model_2)
    single.name = 'single'

    test_ensemble = Ensemble('test_ensemble', reference)
    test_ensemble.upload_models([test_a, test_b])

    contacts = [
        [[1., 0.0], [0.0, 1.]],
        [[1., 1.0], [1.0, 1.]],
        [[1., 0.5], [0.7, 1.]],
    ]

    for contact in contacts:
        test_ensemble.define_cross_transmission('infection cycle',
                                                'infected',
                                                'susceptible',
                                                'total',
                                                'contagious',
                                                'alpha',
                                                contact_type='fixed',
                                                contact=contact)

        n_days = 100
        test_ensemble.reset()
        test_ensemble.evolve_expectations(n_days)

        single.reset()
        single.evolve_expectations(n_days)

        for pop_name in test_ensemble.populations:
            pop = test_ensemble.populations[pop_name]
            if pop.show_sim:
                ens_hist = test_ensemble.populations[pop_name].history
                single_hist = single.populations[pop_name].history
                for i in range(len(ens_hist)):
                    ratio = ens_hist[i] / single_hist[i]
                    assert np.abs(ratio - 2.) < 0.001
Beispiel #6
0
def test_interval_maker():

    hub_date = datetime.date(2020, 4, 1)
    my_IntervalMaker = IntervalMaker("USA", hub_date)
    categories = ['case', 'death', 'hospitalization']
    n_period_dict = {'case': 5, 'death': 5, 'hospitalization': 30}
    n_rep = 10
    scale_std_alpha = 2.
    model = Model.open_file(path_model_2_6)
    if 'interval_maker' not in model.user_dict:
        model.user_dict['interval_maker'] = {}
        model.user_dict['interval_maker']['smearing parameters'] = [
            'non_icu_hosp_frac', 'recover_frac'
        ]
    model.parameters['non_icu_hosp_frac'].std_estimator = 0.002
    model.parameters['recover_frac'].set_value(0.99)
    model.parameters['recover_frac'].std_estimator = 0.01

    my_IntervalMaker.get_quantiles(categories,
                                   n_period_dict,
                                   model,
                                   n_rep=n_rep,
                                   scale_std_alpha=scale_std_alpha,
                                   back_up=21,
                                   fall_back=True,
                                   rescale=True)
    for category in categories:
        my_IntervalMaker.append_user_dict(category, model)
    i = 1
Beispiel #7
0
def test_model_2_9():
    ref_2_9 = Model.open_file(path_model_2_9)
    ref_2_9.parameters['alpha_0'].set_value(0.35)

    ref_2_9.transitions['outbreak_v'].enabled = True
    ref_2_9.parameters['outbreak_v_time'].set_value(30)
    ref_2_9.parameters['outbreak_v_number'].set_value(2.)
    ref_2_9.parameters['alpha_0_v'].set_value(0.35)

    ref_2_9.transitions['outbreak_w'].enabled = True
    ref_2_9.parameters['outbreak_w_time'].set_value(60)
    ref_2_9.parameters['outbreak_w_number'].set_value(4.)
    ref_2_9.parameters['alpha_0_w'].set_value(0.45)

    ref_2_9.transitions['vaccination_1'].enabled = True
    ref_2_9.parameters['vacc_time_1'].set_value(80)
    ref_2_9.parameters['vacc_number_1'].set_value(1000.)

    #    ref_2_9.reset()
    #    ref_2_9.evolve_expectations(200)

    ref_2_9.reset()
    ref_2_9.generate_data(200)

    i = 1
Beispiel #8
0
def test_point_estimate_daily():
    def delta(cumul):
        diff = []
        for i in range(1, len(cumul)):
            diff.append(cumul[i] - cumul[i - 1])
        # first daily value is repeated since val(t0-1) is unknown
        diff.insert(0, diff[0])
        return diff

    start_day = 12
    end_day = 60
    ref_2 = Model.open_file(path_model_2_2)
    sim_2 = Model.open_file(path_model_2_2)

    # do fit of alpha_0, alpha_1, cont_0, trans_rate_1_time
    for par_name in ['alpha_0', 'alpha_1', 'cont_0']:
        par = ref_2.parameters[par_name]
        par.set_variable(None, None)

    par = ref_2.parameters['trans_rate_1_time']
    par.set_variable(None, None)
    par.set_min(13)
    par.set_max(19)

    sim_2.reset()
    sim_2.generate_data(end_day)
    daily_data = delta(sim_2.populations['reported'].history)
    daily_data[47] = np.inf
    optimizer = Optimizer(ref_2,
                          'daily reported',
                          daily_data, [start_day, end_day],
                          skip_data='42,45:48')
    optimizer.reset_variables()

    scan_dict = optimizer.i_fit()
    assert ref_2.parameters['trans_rate_1_time'].get_value() in [15, 16, 17]

    par = ref_2.parameters['trans_rate_1_time']
    par.set_fixed()

    popt, pcov = optimizer.fit()
    assert np.abs(ref_2.parameters['alpha_0'].get_value() -
                  ref_2.parameters['alpha_0'].initial_value) < 0.06
    assert np.abs(ref_2.parameters['alpha_1'].get_value() -
                  ref_2.parameters['alpha_1'].initial_value) < 0.02
    assert np.abs(ref_2.parameters['cont_0'].get_value() -
                  ref_2.parameters['cont_0'].initial_value) < 20.
Beispiel #9
0
def test_Model_properties():
    """tests to ensure the properties of Model"""
    ref_model_1 = Model.open_file(path_model_4_1)
    ref_model_2 = Model.open_file(path_model_4_1)

    for test_model in [ref_model_1, ref_model_2]:
        # check simple scaling of initial contagious population
        # Will not be exact due to bootstrap
        EPS = 0.01
        cont_0 = test_model.parameters['cont_0'].get_value()
        test_model.reset()
        test_model.evolve_expectations(200)
        n_reported1 = test_model.populations['reported'].history[-1]
        assert test_model.populations['contagious_o'].history[-1] < 10.

        test_model.parameters['cont_0'].set_value(2 * cont_0)
        test_model.reset()
        test_model.evolve_expectations(200)
        n_reported2 = test_model.populations['reported'].history[-1]
        assert np.abs(n_reported2 - n_reported1 * 2) < EPS * n_reported1 * 2

    # check that the mean of many data runs is near expectation

    ref_model = Model.open_file(path_model_2)
    ref_model.reset()
    n_days = 60
    ref_model.evolve_expectations(n_days)
    sim_models = []
    n_rep = 100
    for i in range(n_rep):
        sim_model = copy.deepcopy(ref_model)
        sim_model.reset()
        sim_model.generate_data(n_days)
        sim_models.append(sim_model)
    for pop_name in ref_model.populations:
        pop = ref_model.populations[pop_name]
        if pop.show_sim:
            results = []
            for sim_model in sim_models:
                results.append(sim_model.populations[pop_name].history[-1])
            mean = np.mean(np.array(results))
            std = np.std(np.array(results))
            error = std / np.sqrt(1. * n_rep)
            expect = ref_model.populations[pop_name].history[-1]
            assert np.abs(expect - mean) < 8. * error
Beispiel #10
0
def test_max_mult():
    ref_2_5 = Model.open_file(path_model_2_5)
    ref_2_5.transitions['vaccination'].enabled = True
    ref_2_5.parameters['vaccination_number'].set_value(800000.)
    ref_2_5.parameters['vaccination_time'].set_value(5)
    ref_2_5.reset()
    ref_2_5.boot(expectations=True)
    ref_2_5.evolve_expectations(200)
    i = 1
Beispiel #11
0
def test_trajectory():
    ref_2 = Model.open_file(path_model_2_2)
    trajectory = Trajectory(ref_2, 'contagious', 'trans_rate_1', [0.03, 0.75])
    alpha_c = trajectory.get_alpha(0.)
    delta_1 = trajectory.get_delta(0.1)
    delta_2 = trajectory.get_delta(0.2)
    assert np.abs(alpha_c - 0.152) < 0.001
    assert np.abs(trajectory.get_delta(alpha_c)) < 0.00001
    assert np.abs(delta_1 + 0.0513) < 0.0001
    assert np.abs(delta_2 - 0.0389) < 0.0001
Beispiel #12
0
def test_model_2_5():
    ref_2_5 = Model.open_file(path_model_2_5)
    ref_2_3 = Model.open_file(path_model_2_3)
    #ref_2_5.transitions['vaccination'].enabled = True
    #ref_2_5.parameters['vaccination_number'].set_value(10000.)
    ref_2_5.reset()
    ref_2_5.evolve_expectations(200)
    ref_2_3.reset()
    ref_2_3.evolve_expectations(200)

    for pop_name in ref_2_3.populations:
        pop = ref_2_3.populations[pop_name]
        if pop.show_sim:
            print(pop_name)
            hist_2_3 = ref_2_3.populations[pop_name].history
            hist_2_5 = ref_2_5.populations[pop_name].history
            for i in range(len(hist_2_3)):
                if hist_2_3[i] > 0:
                    ratio = hist_2_5[i] / hist_2_3[i]
                    assert np.abs(ratio - 1.) < 0.001
Beispiel #13
0
def test_point_estimate_skip_zeros():
    start_day = 12
    end_day = 60
    ref_2 = Model.open_file(path_model_2_2)
    sim_2 = Model.open_file(path_model_2_2)

    # do fit of alpha_1, trans_rate_1_time
    for par_name in ['alpha_1']:
        par = ref_2.parameters[par_name]
        par.set_variable(None, None)

    par = ref_2.parameters['trans_rate_1_time']
    par.set_variable(None, None)
    par.set_min(13)
    par.set_max(19)

    sim_2.reset()
    rn_dict = sim_2.populations['reported'].get_report_noise()
    rn_dict['report_days'].set_value(7)
    sim_2.generate_data(end_day)
    sim_2.populations['reported'].history[47] = np.inf
    optimizer = Optimizer(ref_2,
                          'total reported',
                          sim_2.populations['reported'].history,
                          [start_day, end_day],
                          cumul_reset=True,
                          skip_data='42,45:48',
                          skip_zeros=True)
    #optimizer = Optimizer(ref_2, 'total reported', sim_2.populations['reported'].history, [start_day, end_day],
    #                      cumul_reset=True, skip_zeros=False)
    optimizer.reset_variables()

    scan_dict = optimizer.i_fit()
    assert ref_2.parameters['trans_rate_1_time'].get_value() in [15, 16, 17]

    par = ref_2.parameters['trans_rate_1_time']
    par.set_fixed()

    popt, pcov = optimizer.fit()
    assert np.abs(ref_2.parameters['alpha_1'].get_value() -
                  ref_2.parameters['alpha_1'].initial_value) < 0.02
Beispiel #14
0
def test_model_2_8():
    ref_2_8 = Model.open_file(path_model_2_8)
    ref_2_8.transitions['outbreak_v'].enabled = True
    ref_2_8.parameters['outbreak_v_time'].set_value(30)
    ref_2_8.parameters['outbreak_v_number'].set_value(2.)
    ref_2_8.parameters['alpha_0'].set_value(0.35)
    #    ref_2_8.reset()
    #    ref_2_8.evolve_expectations(200)
    ref_2_8.reset()
    ref_2_8.generate_data(200)

    i = 1
Beispiel #15
0
def test_model_2_7():
    ref_2_7 = Model.open_file(path_model_2_7)
    ref_2_7.transitions['vaccination_1'].enabled = True
    ref_2_7.transitions['vaccination_2'].enabled = True
    ref_2_7.parameters['vacc_time_2'].set_value(80)
    ref_2_7.parameters['vacc_number_2'].set_value(-5.)
    #    ref_2_7.reset()
    #    ref_2_7.evolve_expectations(200)
    ref_2_7.reset()
    ref_2_7.generate_data(200)

    i = 1
Beispiel #16
0
def test_model_ks_4_2():
    ks_4_2 = Model.open_file('ks_4_2_0116_problem.pypm')
    ks_4_2.reset()
    ks_4_2.evolve_expectations(720)
    recent_oe_ve = [[ks_4_2.populations['os_susceptible'].history[i], ks_4_2.populations['ve_susceptible'].history[i]] \
                    for i in range(695,700)]
    ks_4_2.reset()
    ks_4_2.parameters['ve_frac'].set_value(0.801)
    ks_4_2.evolve_expectations(720)
    recent_oe_ve2 = [[ks_4_2.populations['os_susceptible'].history[i], ks_4_2.populations['ve_susceptible'].history[i]] \
                    for i in range(695,700)]
    i = 1
Beispiel #17
0
def test_class_Delay():
    """tests to ensure the behaviour class Delay"""
    test_model = Model('test_model')
    EPS = 0.1
    mean = 10.
    std_dev = 4.
    half_width = float(std_dev * np.sqrt(12.) / 2.)
    k_vals = [1, 2, 3]
    for delay_type in ['norm', 'uniform', 'erlang', 'gamma']:
        k_s = [1]
        if delay_type == 'erlang':
            k_s = k_vals
        for k in k_s:
            delay_pars = {
                'mean': Parameter('mean', mean, parameter_min=-100., parameter_max=100.),
                'sigma': Parameter('sigma', std_dev, parameter_min=-100., parameter_max=100.),
                'half_width': Parameter('hw', half_width, parameter_min=-100., parameter_max=100.),
                'k': Parameter('k', k, parameter_type='int', parameter_min=1, parameter_max=100)
            }
            for time_step in [1., 1. / 4.]:
                test_model.set_time_step(time_step)
                # The delay is created after the model: since it is not associated with a connector, the model
                # does not know to call its update method. (The model does not have stand alone delays.)
                test_delay = Delay('test_delay', delay_type, delay_parameters=delay_pars, model=test_model)
                distribution = test_delay.future_expectations
                sum_p = 0.
                sum_tp = 0.
                sum_ttp = 0.
                for i in range(len(distribution)):
                    sum_p += distribution[i]
                    sum_tp += i * distribution[i] * time_step
                    sum_ttp += i * i * distribution[i] * time_step ** 2
                assert np.abs(sum_p - 1.) < EPS
                est_mean = sum_tp
                assert np.abs(est_mean - mean) < EPS
                est_sigma = np.sqrt(sum_ttp - sum_tp * sum_tp)
                if delay_type != 'erlang':
                    assert np.abs(est_sigma - std_dev) < EPS
                else:
                    assert np.abs(est_sigma - mean / np.sqrt(1. * k)) < EPS
Beispiel #18
0
    def upload_model_files(self, model_filenames: list):
        if not isinstance(model_filenames, list):
            raise TypeError(
                'Error in upload_model_files: argument must be a list of .pypm filenames'
            )
        for model_file in model_filenames:
            if not isinstance(model_file, str):
                raise TypeError(
                    'Error in upload_model_files: argument must be a list of .pypm filenames'
                )

        for model_filename in model_filenames:
            self.__add_model(Model.open_file(model_filename))
Beispiel #19
0
def test_Model_copy_values_from():
    ref_model_2 = Model.open_file(path_model_2)
    alberta_model_2 = Model.open_file('ab_2_0514.pypm')

    ref_model_2.copy_values_from(alberta_model_2)

    ref_model_2.reset()
    ref_model_2.evolve_expectations(80)
    alberta_model_2.reset()
    alberta_model_2.evolve_expectations(80)

    for pop_name in ref_model_2.populations:
        pop = ref_model_2.populations[pop_name]
        # Bug fixed in ref_model_2, not in alberta_model_2 accounts for small differences in icu
        if 'vent' not in pop.name and 'in_' not in pop.name:
            ref_hist = pop.history
            alberta_hist = alberta_model_2.populations[pop_name].history
            for i in range(len(ref_hist)):
                if np.abs(ref_hist[i] - alberta_hist[i]) > 0.0001:
                    print(pop.name, i, ref_hist[i], alberta_hist[i],
                          np.abs(ref_hist[i] - alberta_hist[i]))
                assert np.abs(ref_hist[i] - alberta_hist[i]) < 0.001
Beispiel #20
0
def test_class_Population():
    """tests to ensure the behaviour class Population"""
    init_value = 100
    test_pop = Population('test population', init_value, description='For testing populations',
                          hidden=True, color='black', show_sim=False, report_noise=False,
                          report_noise_par=None)
    assert test_pop.history[0] == init_value

    model = Model.open_file(path_model_2_8)
    test_pop.set_model(model)

    incoming = 10
    test_pop.update_future_fast(incoming)
    assert test_pop.future[0] == incoming

    test_pop.do_time_step()
    assert test_pop.history[1] == init_value + incoming
    assert len(test_pop.future) == 0 or test_pop.future == 0

    scale_factor = 0.5
    test_pop.scale_history(scale_factor)
    assert test_pop.history[0] == init_value * scale_factor
    assert test_pop.history[1] == (init_value + incoming) * scale_factor

    # check that noise in reporting makes sense
    # Expectations are not effected - data should be
    # but the total reported should be the same after all reporting complete
    noise_factor = Parameter('noise_factor', 0.3)
    backlog_factor = Parameter('backlog_factor', 0.8)
    # restart - back to initial value
    for expectations in [True, False]:
        test_pop.reset()
        future = [0, 10, 100, 100, 100, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        future_sum = np.sum(np.array(future))
        test_pop.set_report_noise(True, noise_factor, backlog_factor, None)
        test_pop.future = future
        for i in range(len(future) + 5):
            test_pop.do_time_step(expectations=expectations)
        assert test_pop.history[-1] == init_value + future_sum

    report_days = Parameter('report_days', 127, parameter_min=-7, parameter_max=127, parameter_type='int')
    for report_day_value in [127, 63, -1, -2, -5, -7]:
        report_days.set_value(report_day_value)
        test_pop.reset()
        future = [0, 10, 100, 100, 100, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        future_sum = np.sum(np.array(future))
        test_pop.set_report_noise(True, noise_factor, backlog_factor, report_days)
        test_pop.future = future
        for i in range(len(future) + 5):
            test_pop.do_time_step(expectations=expectations)
        assert test_pop.history[-1] == init_value + future_sum
Beispiel #21
0
def test_sim_gof_local():
    start_day = 12
    end_day = 60
    ref_2 = Model.open_file(path_model_2_2)
    sim_2 = Model.open_file(path_model_2_2)

    # do fit of alpha_0, alpha_1, cont_0
    par_names = ['alpha_1']
    for par_name in par_names:
        par = ref_2.parameters[par_name]
        par.set_variable(None, None)

    sim_2.reset()
    sim_2.generate_data(end_day)
    sim_2.populations['reported'].history[47] = np.inf
    optimizer = Optimizer(ref_2,
                          'total reported',
                          sim_2.populations['reported'].history,
                          [start_day, end_day],
                          cumul_reset=True,
                          skip_data='42,45:48')
    optimizer.reset_variables()
    popt, pcov = optimizer.fit()
    fit_statistics = optimizer.fit_statistics

    optimizer.calc_chi2s = False
    optimizer.calc_chi2f = True
    n_rep = 10
    optimizer.calc_sim_gof(n_rep)

    fit_stat_list = optimizer.fit_stat_list
    ndof = fit_stat_list[0]['ndof']
    chi2_list = [fit_stat_list[i]['chi2'] for i in range(n_rep)]
    chi2_mean = np.mean(chi2_list)
    assert np.abs(chi2_mean - ndof) < 8.E6
    acor_list = [fit_stat_list[i]['acor'] for i in range(n_rep)]
    acor_mean = np.mean(acor_list)
    assert np.abs(acor_mean) < 0.2
Beispiel #22
0
def test_class_Chain():
    """tests to ensure the behaviour class Chain"""
    test_model = Model('test_model')
    from_pop = Population('from_pop', 0.)
    to_pop = Population('to_pop', 0.)
    pop_a = Population('pop_a', 0.)
    pop_b = Population('pop_b', 0.)
    start = 100000
    from_pop.future = [start]

    mean = 10.
    sigma = 2.
    delay_pars = {'mean': Parameter('mean', mean, parameter_min=0.1, parameter_max=100.),
                  'sigma': Parameter('sigma', sigma, parameter_min=0.1, parameter_max=1000.)}
    delay = Delay('delay', 'norm', delay_pars, test_model)
    frac = 0.8
    fraction = Parameter('frac', frac)
    chain = []
    chain.append(Propagator('prop_0', from_pop, pop_a, fraction, delay))
    chain.append(Propagator('prop_1', pop_a, pop_b, fraction, delay))

    test_chain = Chain('test_chain', from_pop, to_pop, chain, fraction, delay, test_model)
    test_model.add_connector(test_chain)

    for time_step in [1., 1. / 4.]:
        test_model.set_time_step(time_step)
        for func in [test_chain.update_expectation, test_chain.update_data]:
            EPS = 0.02
            if func == test_chain.update_data:
                EPS = 0.2
            to_pop.reset()
            pop_a.reset()
            pop_b.reset()
            func()
            for pop in [to_pop, pop_b]:
                distribution = pop.future
                total = start * (1. - frac ** 2) * frac
                ave = mean
                std_dev = sigma
                if pop == pop_b:
                    total = start * frac ** 2
                    ave = 2. * mean
                    std_dev = np.sqrt(2.) * sigma
                sum_p = 0.
                sum_tp = 0.
                sum_ttp = 0.
                for i in range(len(distribution)):
                    sum_p += distribution[i]
                    sum_tp += i * distribution[i] * time_step
                    sum_ttp += i * i * distribution[i] * time_step ** 2
                assert np.abs(sum_p - total) < EPS * total
                est_mean = sum_tp / total
                assert np.abs(est_mean - ave) < EPS * ave
                est_sigma = np.sqrt(sum_ttp / total - est_mean ** 2)
                assert np.abs(est_sigma - std_dev) < EPS * std_dev
Beispiel #23
0
def test_model_ny_4_2():
    ny_4_2 = Model.open_file(
        '/Users/karlen/pypm-local/models/covid19/USA/ny_4_2_0109.pypm')
    ny_4_2.reset()
    ny_4_2.evolve_expectations(40)
    num = ny_4_2.populations['reported'].history[-1]
    cont_0 = ny_4_2.parameters['cont_0'].get_value()
    ny_4_2.parameters['cont_0'].set_value(2. * cont_0)
    ny_4_2.reset()
    ny_4_2.evolve_expectations(40)
    num2 = ny_4_2.populations['reported'].history[-1]
    if (ny_4_2.populations['contagious_o'].initial_value !=
            ny_4_2.parameters['cont_0']):
        ny_4_2.populations['contagious_o'].initial_value = ny_4_2.parameters[
            'cont_0']
        print('changed')
    ny_4_2.reset()
    ny_4_2.evolve_expectations(40)
    num3 = ny_4_2.populations['reported'].history[-1]
    i = 1
Beispiel #24
0
def test_class_Injector():
    """tests to ensure the behaviour class Injector"""
    test_model = Model('test_model')
    number = 50.
    inject = Parameter('inject', number, parameter_min=0., parameter_max=1000.)
    time = 5
    trans_time = Parameter('time', time, parameter_type='int', parameter_min=0, parameter_max=1000)
    to_pop = Population('to_pop', 0)
    test_injector = Injector('injector', 'rel_days', trans_time, to_pop, inject, model=test_model)
    test_model.add_transition(test_injector)
    for time_step in [1., 1. / 4.]:
        test_model.set_time_step(time_step)
        to_pop.reset()
        test_injector.take_action()
        assert to_pop.future[0] == number
        assert np.abs(test_injector.trigger_step - time / time_step) < 0.1
Beispiel #25
0
def test_class_Multiplier():
    """tests to ensure the behaviour class Multiplier"""
    test_model = Model('test_model')
    EPS = 1.
    n1 = 50.
    n2 = 20.
    n3 = 2.
    scale = 0.1
    f_pops = [Population('f1_pop', n1), Population('f2_pop', n2), Population('f3_pop', n3)]
    to_pop = Population('to_pop', 0.)
    scale_par = Parameter('alpha', scale)
    delay = Delay('fast', 'fast')
    test_multiplier = Multiplier('test_multiplier', f_pops, to_pop, scale_par, delay, model=test_model)
    test_model.add_connector(test_multiplier)
    for time_step in [1., 1. / 4.]:
        test_model.set_time_step(time_step)
        # expectation:
        expected = n1 * n2 / n3 * scale * time_step
        to_pop.reset()
        test_multiplier.set_distribution('poisson', None)
        test_multiplier.update_expectation()
        assert to_pop.future[0] == expected

        # Poisson
        n_rep = 1000
        n_list = []
        for i in range(n_rep):
            to_pop.reset()
            test_multiplier.update_data()
            n_list.append(to_pop.future[0])
        assert np.abs(np.mean(n_list) - expected) < EPS
        assert np.abs(np.std(n_list) - np.sqrt(expected)) < EPS

        # Negative binomial
        p_nb = 0.2
        nbinom_par = Parameter('nb', p_nb)
        test_multiplier.set_distribution('nbinom', nbinom_par)

        n_rep = 1000
        n_list = []
        for i in range(n_rep):
            to_pop.reset()
            test_multiplier.update_data()
            n_list.append(to_pop.future[0])
        assert np.abs(np.mean(n_list) - expected) < EPS
        assert np.abs(np.std(n_list) - np.sqrt(expected / p_nb)) < EPS
Beispiel #26
0
def test_class_Splitter():
    """tests to ensure the behaviour class Splitter"""
    test_model = Model('test_model')
    start = 100000
    from_pop = Population('from_pop', 0)
    from_pop.future = [start]
    to_pops = [Population('to_pop1', 0.), Population('to_pop2', 0.)]
    fracs = [0.4, 0.6]
    fraction = Parameter('frac', fracs[0])
    mean = 10.
    std_dev = 4.
    delay_pars = {
        'mean': Parameter('mean', mean, parameter_min=-100., parameter_max=100.),
        'sigma': Parameter('sigma', std_dev, parameter_min=-100., parameter_max=100.)
    }
    test_delay = Delay('test_delay', 'norm', delay_parameters=delay_pars, model=test_model)

    test_split = Splitter('test_prop', from_pop, to_pops, [fraction], test_delay)
    test_model.add_connector(test_split)
    for time_step in [1., 1. / 4.]:
        test_model.set_time_step(time_step)
        for func in [test_split.update_expectation, test_split.update_data]:
            EPS = 0.01
            if func == test_split.update_data:
                EPS = 0.1
            to_pops[0].reset()
            to_pops[1].reset()
            func()
            total = 0
            for i in range(2):
                distribution = to_pops[i].future
                frac = fracs[i]
                sum_p = 0.
                sum_tp = 0.
                sum_ttp = 0.
                for i in range(len(distribution)):
                    sum_p += distribution[i]
                    sum_tp += i * distribution[i] * time_step
                    sum_ttp += i * i * distribution[i] * time_step ** 2
                total += sum_p
                assert np.abs(sum_p - start * frac) < EPS * start * frac
                est_mean = sum_tp / (start * frac)
                assert np.abs(est_mean - mean) < EPS * mean
                est_sigma = np.sqrt(sum_ttp / (start * frac) - est_mean ** 2)
                assert np.abs(est_sigma - std_dev) < EPS * std_dev
            assert np.abs(total - start) < 0.1
Beispiel #27
0
def test_linear_modifier():
    ref_2_4 = Model.open_file(path_model_2_4)
    trajectory = Trajectory(ref_2_4, 'contagious', 'trans_rate_1', [0.02, 2.0])
    alpha_c = trajectory.get_alpha(0.)
    ref_2_4.parameters['alpha_1'].set_value(alpha_c)
    ref_2_4.parameters['to_icu_delay_mean'].set_value(0.5)
    ref_2_4.parameters['to_icu_delay_sigma'].set_value(0.5)
    ref_2_4.transitions['mod_icu_frac'].enabled = True
    ref_2_4.parameters['icu_frac_time'].set_value(60)
    ref_2_4.parameters['icu_frac_0'].set_value(0.1)
    # the meaning of this parameter changed to the end_value
    # ref_2_4.parameters['icu_frac_slope'].set_value(0.01)
    ref_2_4.parameters['icu_frac_slope'].set_value(0.2)
    ref_2_4.parameters['icu_frac_nstep'].set_value(10)
    ref_2_4.reset()
    ref_2_4.evolve_expectations(100)
    pop_hist = ref_2_4.populations['icu admissions'].history
    assert np.abs(pop_hist[55] - pop_hist[54] - pop_hist[50] +
                  pop_hist[49]) < 0.01
    assert np.abs(pop_hist[75] - pop_hist[74] - pop_hist[70] +
                  pop_hist[69]) < 0.1
    assert np.abs((pop_hist[75] - pop_hist[74]) /
                  (pop_hist[55] - pop_hist[54]) - 2.) < 0.1
Beispiel #28
0
def test_class_Propagator():
    """tests to ensure the behaviour class Propagator"""
    test_model = Model('test_model')
    start = 100000
    from_pop = Population('from_pop', 0)
    from_pop.future = [start]
    to_pop = Population('to_pop', 0.)
    frac = 0.4
    fraction = Parameter('frac', frac)
    mean = 10.
    std_dev = 4.
    delay_pars = {
        'mean': Parameter('mean', mean, parameter_min=-100., parameter_max=100.),
        'sigma': Parameter('sigma', std_dev, parameter_min=-100., parameter_max=100.)
    }
    test_delay = Delay('test_delay', 'norm', delay_parameters=delay_pars, model=test_model)

    test_prop = Propagator('test_prop', from_pop, to_pop, fraction, test_delay)
    test_model.add_connector(test_prop)
    for time_step in [1., 1. / 4.]:
        test_model.set_time_step(time_step)
        for func in [test_prop.update_expectation, test_prop.update_data]:
            EPS = 0.01
            if func == test_prop.update_data:
                EPS = 0.1
            to_pop.reset()
            func()
            distribution = to_pop.future
            sum_p = 0.
            sum_tp = 0.
            sum_ttp = 0.
            for i in range(len(distribution)):
                sum_p += distribution[i]
                sum_tp += i * distribution[i] * time_step
                sum_ttp += i * i * distribution[i] * time_step ** 2
            assert np.abs(sum_p - start * frac) < EPS * start * frac
            est_mean = sum_tp / (start * frac)
            assert np.abs(est_mean - mean) < EPS * mean
            est_sigma = np.sqrt(sum_ttp / (start * frac) - est_mean ** 2)
            assert np.abs(est_sigma - std_dev) < EPS * std_dev
Beispiel #29
0
def test_class_Modifier():
    """tests to ensure the behaviour class Modifier"""
    test_model = Model('test_model')
    mod_time = Parameter('time', 5, parameter_type='int', parameter_min=0, parameter_max=1000)
    par_val = 0.3
    par_0_val = 0.5
    par_1_val = 0.7
    parameter = Parameter('par', par_val)
    parameter_0 = Parameter('par_0', par_0_val)
    parameter_1 = Parameter('par_1', par_1_val)
    test_modifier = Modifier('test_modifier', 'rel_days', mod_time, parameter, parameter_0, parameter_1,
                             model=test_model)
    test_model.add_transition(test_modifier)
    for time_step in [1., 1. / 4.]:
        test_model.set_time_step(time_step)
        parameter.reset()
        assert parameter.get_value() == par_val
        test_modifier.take_action()
        assert parameter.get_value() == par_1_val
        test_modifier.reset()
        assert parameter.get_value() == par_0_val
        assert np.abs(test_modifier.trigger_step - 5 / time_step) < 0.1
Beispiel #30
0
def test_mixing_evolve_data():
    ref_2_9 = Model.open_file(path_model_2_9)
    ref_2_9.reset()
    ref_2_9.generate_data(100, from_step=0, data_start=50)
    reported = ref_2_9.populations['reported'].history
    i = 1