Ejemplo n.º 1
0
                    # Call the function to build the sensitivity analysis
                    modscen = sc.dcp(scen)
                    for builder in builders:
                        modscen = builder(sim, modscen, sc.dcp(test))


                    sm = cvsch.schools_manager(modscen)
                    sim['interventions'] += [sm]

                    sims.append(sim)
                    proc += 1

                    if len(sims) == batch_size:# or proc == tot or (tidx == len(testing)-1 and sidx == len(scenarios)-1 and eidx == len(par_list)-1):
                        print(f'Running sims {proc-len(sims)}-{proc-1} of {tot}')
                        msim = cv.MultiSim(sims)
                        msims.append(msim)
                        msim.run(reseed=False, par_args={'ncpus': 32}, noise=0.0, keep_people=False)
                        sims = []

        #fn = os.path.join(folder, 'msims', f'{stem}_{senskey}.msim')
        #print(f'*** Saving to {fn} after completing {senskey}')
        #sims_this_scenario = [s for msim in msims for s in msim.sims if s.key3 == senskey]
        #msim = cv.MultiSim(sims_this_scenario)
        #cv.save(fn, msim)

    msim = cv.MultiSim.merge(msims)
    msim.base_sim = [] # Save disk space
    fn = os.path.join(folder, 'msims', f'{stem}.msim')
    print(f'Saving msims to {fn}')
    cv.save(fn, msim)
    proc = 0
    step = 16
    for skey, scen in scenarios.items():
        for idx, dp in enumerate(dynamic_pars):
            sim = cs.create_sim(dp, pop_size=pop_size)

            sim.label = skey
            sim.scen = scen
            sim.dynamic_par = dp

            ns = new_schools(scen) # Not sure if need new mem for each
            sim['interventions'] += [ns]
            sims.append(sim)
            proc += 1

            if len(sims) == step or proc == tot:
                print(f'Running sims {proc-len(sims)}:{proc} of {tot}')
                msim = cv.MultiSim(sims)
                msims.append(msim)
                msim.run(reseed=False, par_args={'ncpus': 16}, noise=0.0, keep_people=debug)
                sims = []

    msim = cv.MultiSim.merge(msims)

    cv.save(os.path.jsoin('msims', f'msim_{int(pop_size)}.obj'), msim)

    if debug:
        for sim in msim.sims:
            sim.plot(to_plot='overview')
            t = sim.make_transtree()
Ejemplo n.º 3
0
def test_misc():
    sc.heading('Testing miscellaneous functions')

    sim_path = 'test_misc.sim'
    json_path = 'test_misc.json'
    gitinfo_path = 'test_misc.gitinfo'
    fig_path = 'test_misc.png'
    fig_comments = 'Test comment'

    # Data loading
    cv.load_data(csv_file)
    cv.load_data(xlsx_file)

    with pytest.raises(NotImplementedError):
        cv.load_data('example_data.unsupported_extension')

    with pytest.raises(ValueError):
        cv.load_data(xlsx_file, columns=['missing_column'])

    # Dates
    d1 = cv.date('2020-04-04')
    d2 = cv.date(sc.readdate('2020-04-04'))
    ds = cv.date('2020-04-04', d2)
    assert d1 == d2
    assert d2 == ds[0]

    with pytest.raises(ValueError):
        cv.date([(2020, 4, 4)])  # Raises a TypeError which raises a ValueError

    with pytest.raises(ValueError):
        cv.date('Not a date')

    cv.daydiff('2020-04-04')

    # Run sim for more investigations
    sim = cv.Sim(pop_size=500, verbose=0)
    sim.run()
    sim.plot(do_show=False)

    # Saving and loading
    cv.savefig(fig_path, comments=fig_comments)
    cv.save(filename=sim_path, obj=sim)
    cv.load(filename=sim_path)

    # Version checks
    cv.check_version('0.0.0')  # Nonsense version
    print('↑ Should complain about version')
    with pytest.raises(ValueError):
        cv.check_version('0.0.0', die=True)

    # Git checks
    cv.git_info(json_path)
    cv.git_info(json_path, check=True)

    # Poisson tests
    c1 = 5
    c2 = 8
    for alternative in ['two-sided', 'larger', 'smaller']:
        cv.poisson_test(c1, c2, alternative=alternative)
    for method in ['score', 'wald', 'sqrt', 'exact-cond']:
        cv.poisson_test(c1, c2, method=method)

    with pytest.raises(ValueError):
        cv.poisson_test(c1, c2, method='not a method')

    # Test locations
    for location in [None, 'viet-nam']:
        cv.data.show_locations(location)

    # Test versions
    with pytest.raises(ValueError):
        cv.check_save_version('1.3.2', die=True)
    cv.check_save_version(cv.__version__,
                          filename=gitinfo_path,
                          comments='Test')

    # Test PNG
    try:
        metadata = cv.get_png_metadata(fig_path, output=True)
        assert metadata['Covasim version'] == cv.__version__
        assert metadata['Covasim comments'] == fig_comments
    except ImportError as E:
        print(
            f'Cannot test PNG function since pillow not installed ({str(E)}), skipping'
        )

    # Tidy up
    remove_files(sim_path, json_path, fig_path, gitinfo_path)

    return
Ejemplo n.º 4
0
def test_misc():
    sc.heading('Testing miscellaneous functions')

    sim_path = 'test_misc.sim'
    json_path = 'test_misc.json'

    # Data loading
    cv.load_data(csv_file)
    cv.load_data(xlsx_file)

    with pytest.raises(NotImplementedError):
        cv.load_data('example_data.unsupported_extension')

    with pytest.raises(ValueError):
        cv.load_data(xlsx_file, columns=['missing_column'])

    # Dates
    d1 = cv.date('2020-04-04')
    d2 = cv.date(sc.readdate('2020-04-04'))
    ds = cv.date('2020-04-04', d2)
    assert d1 == d2
    assert d2 == ds[0]

    with pytest.raises(ValueError):
        cv.date([(2020, 4, 4)])  # Raises a TypeError which raises a ValueError

    with pytest.raises(ValueError):
        cv.date('Not a date')

    cv.daydiff('2020-04-04')

    # Saving and loading
    sim = cv.Sim()
    cv.save(filename=sim_path, obj=sim)
    cv.load(filename=sim_path)

    # Version checks
    cv.check_version('0.0.0')  # Nonsense version
    print('↑ Should complain about version')
    with pytest.raises(ValueError):
        cv.check_version('0.0.0', die=True)

    # Git checks
    cv.git_info(json_path)
    cv.git_info(json_path, check=True)

    # Poisson tests
    c1 = 5
    c2 = 8
    for alternative in ['two-sided', 'larger', 'smaller']:
        cv.poisson_test(c1, c2, alternative=alternative)
    for method in ['score', 'wald', 'sqrt', 'exact-cond']:
        cv.poisson_test(c1, c2, method=method)

    with pytest.raises(ValueError):
        cv.poisson_test(c1, c2, method='not a method')

    # Tidy up
    remove_files(sim_path, json_path)

    return
Ejemplo n.º 5
0
sub2.run()
assert sub2['beta_layer']['a'] == 0.5
with pytest.raises(sc.KeyNotFoundError):  # q is not ok
    sub2.update_pars(beta_layer={'q': 0.5})
    sub2.run()

sc.heading('SynthPops test')
ssp = cv.Sim(pars, pop_type='synthpops')
ssp.run()

sc.heading('SynthPops + LTCF test')
sf = cv.Sim(pop_size=2000, pop_type='synthpops')
sf.popdict = cv.make_synthpop(sf,
                              with_facilities=True,
                              layer_mapping={'LTCF': 'f'})
cv.save('synth.pop', sf.popdict)
with pytest.raises(ValueError):  # Layer key mismatch, f is not initialized
    sf.run()
sf.reset_layer_pars()
sf.run()
assert 'f' in sf.layer_keys()
assert sf['beta_layer']['f'] == 1.0

sc.heading('Population save test')
sps = cv.Sim(pars, pop_type='hybrid', popfile='test.pop', save_pop=True)
sps.run()

sc.heading('Population load test')
spl = cv.Sim(pars, pop_type='hybrid', popfile='test.pop', load_pop=True)
spl.run()
Ejemplo n.º 6
0
            sim['rand_seed'] = seed
            sim.set_seed()
            sim.label = f"Sim {seed}"
            sims.append(sim)
        msim = cv.MultiSim(sims)
        msim.run()
        #        msim.run(par_args={'n_cpus':24})

        # Figure out the seeds that give a good fit
        mismatches = np.array(
            [sim.compute_fit().mismatch for sim in msim.sims])
        threshold = np.quantile(mismatches, 0.01)  # Take the best 1%
        goodseeds = [
            i for i in range(len(mismatches)) if mismatches[i] < threshold
        ]
        cv.save(f'{resfolder}/goodseeds.obj', goodseeds)

    # Run calibration with best-fitting seeds and parameters
    elif whattorun == 'finalisefit':
        goodseeds = cv.load(f'{resfolder}/goodseeds.obj')
        s0 = make_sim(seed=1, end_day='2020-08-25', verbose=-1)
        sims = []
        for seed in goodseeds:
            sim = s0.copy()
            sim['rand_seed'] = seed
            sim.set_seed()
            sim.label = f"Sim {seed}"
            sims.append(sim)
        msim = cv.MultiSim(sims)
        msim.run()
Ejemplo n.º 7
0
    quar_factor=dict(h=0.8, s=0.0, w=0.0, c=0.3),
)

# Run sims
s = sc.objdict()
t = sc.objdict()
for k in ['none', 'test', 'trace']:
    interventions = []
    if k in ['test', 'trace']:
        interventions += [
            cv.test_prob(start_day=iday,
                         symp_prob=0.15,
                         symp_quar_prob=1.0,
                         asymp_quar_prob=1.0,
                         quar_policy='start')
        ]
    if k in ['trace']:
        interventions += [
            cv.contact_tracing(start_day=iday,
                               trace_probs=dict(h=0.7, s=0.1, w=0.1, c=0.0))
        ]

    s[k] = cv.Sim(
        p, interventions=interventions, version='2.0.0'
    )  # Version 2.0 parameters are what were used in the manuscript
    s[k].run()
    t[k] = s[k].make_transtree()

# Save
cv.save(tt_filename, [p, s, t])
Ejemplo n.º 8
0
                                          ili_prev=0.002,
                                          schedule=schedule[i],
                                          num_pos=None,
                                          label='reopen_schools')
                    ]
                    if rel_trans:
                        sim['prognoses']['trans_ORs'][0] = 0.5
                    if beta_layer:
                        sim['beta_layer']['s'] = 3

                    if NPI[i] is not None:
                        interventions += [
                            cv.change_beta(days='2020-09-01',
                                           changes=NPI[i],
                                           layers='s',
                                           label='NPI_schools')
                        ]
                    sim['interventions'] = interventions
                    for interv in sim['interventions']:
                        interv.do_plot = False
                    all_sims.append(sim)

    msim = cv.MultiSim(all_sims)
    msim.run(reseed=False, par_args={'ncpus': 7}, noise=0.0, keep_people=True)
    for sim in msim.sims:
        sdict[sim.label] = sim
        sdict[sim.label].shrink()
    cv.save('sdict.obj', sdict)

    sc.toc(T)
    print('Done.')
Ejemplo n.º 9
0
                    builder(sim, sc.dcp(scen), sc.dcp(test))

                    sims.append(sim)
                    proc += 1

                    if len(sims) == batch_size or proc == tot or (
                            tidx == len(testing) - 1
                            and sidx == len(scenarios) - 1
                            and eidx == len(par_list) - 1):
                        print(
                            f'Running sims {proc-len(sims)}-{proc-1} of {tot}')
                        msim = cv.MultiSim(sims)
                        msims.append(msim)
                        msim.run(reseed=False,
                                 par_args={'ncpus': 32},
                                 noise=0.0,
                                 keep_people=False)
                        sims = []

        fn = os.path.join(folder, 'msims', f'{stem}_{senskey}.msim')
        print(f'*** Saving to {fn} after completing {senskey}')
        sims_this_scenario = [
            s for msim in msims for s in msim.sims if s.key3 == senskey
        ]
        msim = cv.MultiSim(sims_this_scenario)
        cv.save(fn, msim)

    msim = cv.MultiSim.merge(msims)
    msim.base_sim = []  # Save disk space
    cv.save(os.path.join(folder, 'msims', f'{stem}.msim'), msim)
Ejemplo n.º 10
0
    ''' Find matching indices in a large dataframe '''
    inds = np.arange(len(df))
    for arg in args:
        filterinds = sc.findinds(arg)
        inds = np.intersect1d(inds, filterinds)
    return inds


if __name__ == '__main__':

    if regenerate:
        print('Loading...')
        msim = cv.load(msimfile)

        print('Processing...')
        data = []
        for sim in msim.sims:
            info = splitlabel(sim.label)
            pars = get_params(sim.pars)
            results = get_results(sim.results)
            data.append(sc.mergedicts(info, pars, results))

        df = pd.DataFrame(data)
        cv.save(dffile, df)
    else:
        print('Reloading...')
        df = cv.load(dffile)

    print('Done.')
    sc.toc(T)
Ejemplo n.º 11
0
            hs_concat = pd.concat(hs)
            by_row_index = hs_concat.groupby(hs_concat.index)
            hs_means = by_row_index.mean()
            hs_means.columns = [scen]
            hs_with_a_case.append(hs_means)

        es_with_a_case = pd.concat(es_with_a_case, ignore_index=True, axis=1)
        es_with_a_case.columns = schools_reopening_scenarios_label
        ms_with_a_case = pd.concat(ms_with_a_case, ignore_index=True, axis=1)
        ms_with_a_case.columns = schools_reopening_scenarios_label
        hs_with_a_case = pd.concat(hs_with_a_case, ignore_index=True, axis=1)
        hs_with_a_case.columns = schools_reopening_scenarios_label
        with pd.ExcelWriter(f'results/schools_with_a_case_{case}_{date}.xlsx') as writer:
            es_with_a_case.to_excel(writer, sheet_name='ES')
            ms_with_a_case.to_excel(writer, sheet_name='MS')
            hs_with_a_case.to_excel(writer, sheet_name='HS')

        school_results = school_dict(msims, day_schools_reopen)
        if rel_trans:
            filename = f'results/school_reopening_analysis_output_{case}_under10_0.5trans_{date}.csv'
        else:
            filename = f'results/school_reopening_analysis_output_{case}_{date}.csv'
        school_results.to_csv(filename, header=True)


    cv.save('all_scens.msims', msims)

    print('Done!')
    sc.toc(T)

Ejemplo n.º 12
0
fns = [
    f'{base}_baseline.msim',
    f'{base}_lower_sens_spec.msim',
    f'{base}_no_NPI_reduction.msim',
    f'{base}_lower_random_screening.msim',
    f'{base}_no_screening.msim',
    f'{base}_lower_coverage.msim',
    f'{base}_alt_symp.msim',
    f'{base}_children_equally_sus.msim',
    f'{base}_increased_mobility.msim',
    f'{base}_broken_bubbles.msim',
]
fns = [os.path.join(folder, 'msims', fn) for fn in fns]

sims = []
for fn in fns:
    print(f'Loading {fn}')
    ext = os.path.splitext(fn)[1]
    if ext == '.sims':
        sims += cv.load(fn)
    elif ext == '.msim':
        msim = cv.MultiSim.load(fn)
        sims += msim.sims
    else:
        print('ERROR')

fn = os.path.join(folder, 'msims', f'{base}.msim')
print(f'Saving to {fn}')
cv.save(fn, sims)
        json = sc.loadjson(jsonfile)

        all_sims = []
        for index in indices:
            entry = json[index]
            pars = entry['pars']
            pars['rand_seed'] = int(entry['index'])
            all_sims.append(cs.create_sim(pars=pars))

        msim = cv.MultiSim(sims=all_sims)
        msim.run(reseed=False,
                 par_args={'maxload': 0.8},
                 noise=0.0,
                 keep_people=False)
        msim.reduce()
        if do_save:
            cv.save(filename='msims/calibrated.msim', obj=msim)

    if do_plot:

        for interv in msim.base_sim['interventions']:
            interv.do_plot = False

        sim_plots = cv.MultiSim.merge(msim, base=True)
        fig1 = sim_plots.plot(to_plot=['new_infections', 'new_diagnoses'],
                              do_show=False)
        fig1.savefig(f'infectious_{date}.png')
        # sim_plots.plot(do_show=True)

        pltcal.plot_calibration(msim.sims, 'jun17-optuna', do_save=True)
Ejemplo n.º 14
0
                meta.count = count
                meta.n_sims = n_sims
                meta.inds = [draw, seed]
                meta.vals = sc.objdict(sc.mergedicts(p, dict(seed=seed)))
                ikw.append(sc.dcp(meta.vals))
                ikw[-1].meta = meta

        kwargs = dict(location=location,
                      start_day=start_day,
                      end_day=end_day,
                      data_path=data_path,
                      verbose=verbose,
                      debug=debug)
        sim_configs = sc.parallelize(make_sim, iterkwargs=ikw, kwargs=kwargs)
        if do_save:
            cv.save(filename=sims_file, obj=sim_configs)

        # Run sims
        all_sims = sc.parallelize(run_sim,
                                  iterarg=sim_configs,
                                  kwargs=dict(do_save=do_save))
        sims = np.empty((n_draws, n_seeds), dtype=object)
        for sim in all_sims:  # Unflatten array
            draw, seed = sim.meta.inds
            sims[draw, seed] = sim

        # Convert to msims
        all_sims_semi_flat = []
        for draw in range(n_draws):
            sim_seeds = sims[draw, :].tolist()
            all_sims_semi_flat.append(sim_seeds)
Ejemplo n.º 15
0
                     symp_quar_prob=0.75,
                     asymp_quar_prob=0.5,
                     test_delay=0,
                     quar_policy='both'),
        cv.contact_tracing(start_day=sd, trace_probs=0.9, trace_time=0),
    ],
)

if __name__ == '__main__':

    # Configure sims
    sims = []
    for betakey, beta in betadict.items():
        for scenkey, interventions in scenarios.items():
            for seed in np.arange(n_seeds):
                sim = cv.Sim(p,
                             beta=beta,
                             rand_seed=seed,
                             interventions=interventions,
                             label=f'{betakey}_{scenkey}_{seed}')
                sims += [sim]

    # Run sims
    msim = cv.MultiSim(sims)
    msim.run(par_args={'ncpus': 5})
    sims = msim.sims
    if do_save:
        cv.save(simsfile, sims)

    print('Done.')
    sc.toc(T)
Ejemplo n.º 16
0
    for which in ['actual', 'low', 'high']:

        scenpars = sc.objdict(sc.mergedicts(base, scens[which]))
        kwargs = dict(scenpars=scenpars, from_cache=from_cache, do_shrink=0)

        # Run the simulations
        if ncpus > 1:
            sims = sc.parallelize(cs.run_sim,
                                  iterarg=indices,
                                  ncpus=ncpus,
                                  kwargs=kwargs)
        else:
            sims = [cs.run_sim(index, **kwargs) for index in indices]

        # Merge into a multisim and optionally plot
        msim = cv.MultiSim(sims=sims)
        msim.reduce()
        msims[which] = msim
        if do_plot:
            msim.plot(to_plot='overview', fig_args={'figsize': (38, 19)})

    if do_save:
        print('Saving...')
        for msim in msims.values():
            for sim in msim.sims:
                sim.shrink()
        cv.save(msimsfile, msims)

    sc.toc(T)
    print('Done.')