def examplew2(): pars = {'use_waning': True, 'n_days': 180} sim = cv.Sim(pars=pars) scenarios = { 'base': { 'name': 'baseline', 'pars': {} }, 'scen1': { 'name': '1 wave', 'pars': { 'interventions': [cv.historical_wave(prob=0.25, days_prior=50)] } }, 'scen2': { 'name': '2 waves', 'pars': { 'interventions': [cv.historical_wave(prob=[0.20, 0.05], days_prior=[360, 50])] } } } metapars = cv.make_metapars() metapars.update({'n_runs': 8}) scens = cv.Scenarios(sim=sim, scenarios=scenarios, metapars=metapars) scens.run() scens.plot()
def test_migration(): sc.heading('Testing migration...') # Create sim and people base = make_sim() base.people.version = version sim = cv.load(filename) sim.people = base.people # Create msim msim = cv.MultiSim(base_sim=sim) del msim.version # To simulate <2.0.0 msim.init_sims() # Create scenarios scens = cv.Scenarios(sim=sim) del scens.version # To simulate <2.0.0 # Try migrations new_sim = cv.migrate(sim, die=True) new_msim = cv.migrate(msim, die=True) new_scens = cv.migrate(scens, die=True) # Try something un-migratable with pytest.raises(TypeError): cv.migrate('Strings are not migratable', die=True) return new_sim, new_msim, new_scens
def test_beds(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading('Test of bed capacity estimation') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 basepars = {'n': 1000} metapars = {'n_runs': n_runs} sim = cv.Sim() # Define the scenarios scenarios = { 'baseline': { 'name': 'No bed constraints', 'pars': { 'n_infected': 100 } }, 'bedconstraint': { 'name': 'Only 10 beds available', 'pars': { 'n_infected': 100, 'n_beds': 10, } }, 'bedconstraint2': { 'name': 'Only 1 bed available, people are 10x more likely to die if not hospitalized', 'pars': { 'n_infected': 100, 'n_beds': 1, 'OR_no_treat': 10., } }, } scens = cv.Scenarios(sim=sim, basepars=basepars, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) if do_plot: to_plot = sc.odict({ 'cum_deaths': 'Cumulative deaths', # 'bed_capacity': 'People needing beds / beds', 'n_severe': 'Number of cases requiring hospitalization', 'n_critical': 'Number of cases requiring ICU', }) scens.plot(to_plot=to_plot, do_save=do_save, do_show=do_show, fig_path=fig_path) return scens
def test_scenarios(do_plot=False): sc.heading('Scenarios test') scens = cv.Scenarios() scens.run() if do_plot: scens.plot() return scens
def test_scenarios(do_plot=False): sc.heading('Scenarios test') basepars = {'n':1000} scens = cv.Scenarios(basepars=basepars) scens.run() if do_plot: scens.plot() return scens
def examplev2(): pars = {'use_waning': True} variants = [cv.variant('b117', days=30, n_imports=10)] sim = cv.Sim(pars=pars, variants=variants) # length of our base campaign duration = 30 # estimate per-day probability needed for a coverage of 30% prob = cv.historical_vaccinate_prob.estimate_prob(duration=duration, coverage=0.30) print('using per-day probability of ', prob) # estimate per-day probability needed for a coverage of 30% prob2 = cv.historical_vaccinate_prob.estimate_prob(duration=2 * duration, coverage=0.30) scenarios = { 'base': { 'name': 'baseline', 'pars': {} }, 'scen1': { 'name': 'historical_vaccinate', 'pars': { 'interventions': [ cv.historical_vaccinate_prob(vaccine='pfizer', days=np.arange(-duration, 0), prob=prob) ] } }, 'scen2': { 'name': 'vaccinate', 'pars': { 'interventions': [ cv.vaccinate_prob(vaccine='pfizer', days=np.arange(0, 30), prob=prob) ] } }, 'scen3': { 'name': 'historical_vaccinate into sim', 'pars': { 'interventions': [ cv.historical_vaccinate_prob(vaccine='pfizer', days=np.arange(-30, 30), prob=prob2) ] } }, } scens = cv.Scenarios(sim=sim, scenarios=scenarios) scens.run() scens.plot()
def test_borderclosure(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading('Test effect of border closures') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 basepars = {'pop_size': 1000} basepars = {'n_imports': 5} metapars = {'n_runs': n_runs} sim = cv.Sim() # Define the scenarios scenarios = { 'baseline': { 'name': 'No border closures', 'pars': {} }, 'borderclosures_day1': { 'name': 'Close borders on day 1', 'pars': { 'interventions': [cv.dynamic_pars({'n_imports': { 'days': 1, 'vals': 0 }})] } }, 'borderclosures_day10': { 'name': 'Close borders on day 10', 'pars': { 'interventions': [cv.dynamic_pars({'n_imports': { 'days': 10, 'vals': 0 }})] } }, } scens = cv.Scenarios(sim=sim, basepars=basepars, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path=fig_path) return scens
def test_turnaround(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading('Test impact of reducing delay time for getting test results') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 base_pars = { 'pop_size': 1000, 'pop_type': 'hybrid', } base_sim = cv.Sim(base_pars) # create sim object n_people = base_sim['pop_size'] npts = base_sim.npts # Define overall testing assumptions testing_prop = 0.1 # Assumes we could test 10% of the population daily (!!) daily_tests = [testing_prop * n_people] * npts # Number of daily tests # Define the scenarios scenarios = { f'{d}dayturnaround': { 'name': f'Symptomatic testing with {d} days to get results', 'pars': { 'interventions': cv.test_num(daily_tests=daily_tests, test_delay=d) } } for d in range(1, 3 + 1, 2) } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) to_plot = ['cum_infections', 'n_infectious', 'new_tests', 'new_diagnoses'] fig_args = dict(figsize=(20, 24)) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path=fig_path, interval=7, fig_args=fig_args, to_plot=to_plot) return scens
def test_run(): sc.heading('Testing run') msim_path = 'run_test.msim' scens_path = 'run_test.scens' # Test creation s1 = cv.Sim(pop_size=100) s2 = s1.copy() msim = cv.MultiSim(sims=[s1, s2]) with pytest.raises(TypeError): cv.MultiSim(sims='not a sim') # Test other properties len(msim) msim.result_keys() msim.base_sim = None with pytest.raises(ValueError): msim.result_keys() msim.base_sim = msim.sims[0] # Restore # Run msim.run(verbose=verbose) msim.reduce(quantiles=[0.1, 0.9], output=True) with pytest.raises(ValueError): msim.reduce(quantiles='invalid') msim.compare(output=True, do_plot=True, log_scale=False) # Plot for i in range(2): if i == 1: msim.reset() # Reset as if reduce() was not called msim.plot() msim.plot_result('r_eff') print('↑ May print some plotting warnings') # Save for keep_people in [True, False]: msim.save(filename=msim_path, keep_people=keep_people) # Scenarios scens = cv.Scenarios(sim=s1, metapars={'n_runs': 1}) scens.run(keep_people=True, verbose=verbose, debug=debug) for keep_people in [True, False]: scens.save(scens_path, keep_people=keep_people) cv.Scenarios.load(scens_path) # Tidy up remove_files(msim_path, scens_path) return
def test_vaccine_1variant_scen(do_plot=False, do_show=True, do_save=False): sc.heading('Run a basic sim with 1 variant, pfizer vaccine') # Define baseline parameters n_runs = 3 base_sim = cv.Sim(use_waning=True, pars=base_pars) # Vaccinate 75+, then 65+, then 50+, then 18+ on days 20, 40, 60, 80 base_sim.vxsubtarg = sc.objdict() base_sim.vxsubtarg.age = [75, 65, 50, 18] base_sim.vxsubtarg.prob = [.05, .05, .05, .05] base_sim.vxsubtarg.days = subtarg_days = [20, 40, 60, 80] pfizer = cv.vaccinate(days=subtarg_days, vaccine='pfizer', subtarget=vacc_subtarg) # Define the scenarios scenarios = { 'baseline': { 'name': 'No Vaccine', 'pars': {} }, 'pfizer': { 'name': 'Pfizer starting on day 20', 'pars': { 'interventions': [pfizer], } }, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run() to_plot = sc.objdict({ 'New infections': ['new_infections'], 'Cumulative infections': ['cum_infections'], 'New reinfections': ['new_reinfections'], # 'Cumulative reinfections': ['cum_reinfections'], }) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path='results/test_basic_vaccination.png', to_plot=to_plot) return scens
def test_waning_vs_not(do_plot=False, do_show=True, do_save=False): sc.heading('Testing waning...') # Define baseline parameters pars = sc.mergedicts(base_pars, { 'pop_size': 10e3, 'pop_scale': 50, 'n_days': 150, 'use_waning': False, }) n_runs = 3 base_sim = cv.Sim(pars=pars) # Define the scenarios scenarios = { 'no_waning': { 'name': 'No waning', 'pars': {} }, 'waning': { 'name': 'Waning', 'pars': { 'use_waning': True, } }, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run() to_plot = sc.objdict({ 'New infections': ['new_infections'], 'New reinfections': ['new_reinfections'], 'Cumulative infections': ['cum_infections'], 'Cumulative reinfections': ['cum_reinfections'], }) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path='results/test_waning_vs_not.png', to_plot=to_plot) return scens
def test_scenarios(do_plot=False): sc.heading('Scenarios test') basepars = {'pop_size': 1000} json_path = 'scen_test.json' xlsx_path = 'scen_test.xlsx' scens = cv.Scenarios(basepars=basepars) scens.run() if do_plot: scens.plot() scens.to_json(json_path) scens.to_excel(xlsx_path) for path in [json_path, xlsx_path]: print(f'Removing {path}') os.remove(path) return scens
def examplev3(): pars = {'use_waning': True} variants = [cv.variant('b117', days=30, n_imports=10)] sim = cv.Sim(pars=pars, variants=variants) # length of our base campaign duration = 30 # estimate per-day probability needed for a coverage of 30% prob = cv.historical_vaccinate_prob.estimate_prob(duration=duration, coverage=0.30) print('using per-day probability of ', prob) scenarios = { 'scen1': { 'name': 'both doses', 'pars': { 'interventions': [ cv.historical_vaccinate_prob(vaccine='pfizer', days=np.arange(-duration, 0), prob=prob) ] } }, 'scen3': { 'name': 'first dose only', 'pars': { 'interventions': [ cv.historical_vaccinate_prob(vaccine='pfizer', days=np.arange(-duration, 0), prob=prob, compliance=[1.0, 0.0]) ] } }, } scens = cv.Scenarios(sim=sim, scenarios=scenarios) scens.run() to_plot = cv.get_default_plots(kind='scenarios') to_plot.pop(2) to_plot.update({'Cumulative doses': ['cum_vaccinated', 'cum_doses']}) scens.plot(to_plot=to_plot)
def test_simple_scenarios(do_plot=do_plot): sc.heading('Simple scenarios test') basepars = {'pop_size': pop_size} json_path = 'scen_test.json' xlsx_path = 'scen_test.xlsx' scens = cv.Scenarios(basepars=basepars) scens.run(verbose=verbose) if do_plot: scens.plot() scens.to_json(json_path) scens.to_excel(xlsx_path) scens.disp() scens.summarize() scens.brief() for path in [json_path, xlsx_path]: print(f'Removing {path}') os.remove(path) return scens
def test_tracedelay(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading( 'Test impact of reducing delay time for finding contacts of positives') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 base_pars = { 'pop_size': 1000, 'pop_type': 'hybrid', } base_sim = cv.Sim(base_pars) # create sim object base_sim['n_days'] = 50 base_sim['beta'] = 0.03 # Increase beta n_people = base_sim['pop_size'] npts = base_sim.npts # Define overall testing assumptions testing_prop = 0.1 # Assumes we could test 10% of the population daily (way too optimistic!!) daily_tests = [testing_prop * n_people] * npts # Number of daily tests # Define the scenarios scenarios = { 'lowtrace': { 'name': 'Poor contact tracing', 'pars': { 'quar_eff': { 'h': 1, 's': 0.5, 'w': 0.5, 'c': 0.25 }, 'quar_period': 7, 'interventions': [ cv.test_num(daily_tests=daily_tests), cv.contact_tracing(trace_probs={ 'h': 0, 's': 0, 'w': 0, 'c': 0 }, trace_time={ 'h': 1, 's': 7, 'w': 7, 'c': 7 }) ] } }, 'modtrace': { 'name': 'Moderate contact tracing', 'pars': { 'quar_eff': { 'h': 0.75, 's': 0.25, 'w': 0.25, 'c': 0.1 }, 'quar_period': 10, 'interventions': [ cv.test_num(daily_tests=daily_tests), cv.contact_tracing(trace_probs={ 'h': 1, 's': 0.8, 'w': 0.5, 'c': 0.1 }, trace_time={ 'h': 0, 's': 3, 'w': 3, 'c': 8 }) ] } }, 'hightrace': { 'name': 'Fast contact tracing', 'pars': { 'quar_eff': { 'h': 0.5, 's': 0.1, 'w': 0.1, 'c': 0.1 }, 'quar_period': 14, 'interventions': [ cv.test_num(daily_tests=daily_tests), cv.contact_tracing(trace_probs={ 'h': 1, 's': 0.8, 'w': 0.8, 'c': 0.2 }, trace_time={ 'h': 0, 's': 1, 'w': 1, 'c': 5 }) ] } }, 'alltrace': { 'name': 'Same-day contact tracing', 'pars': { 'quar_eff': { 'h': 0.0, 's': 0.0, 'w': 0.0, 'c': 0.0 }, 'quar_period': 21, 'interventions': [ cv.test_num(daily_tests=daily_tests), cv.contact_tracing(trace_probs={ 'h': 1, 's': 1, 'w': 1, 'c': 1 }, trace_time={ 'h': 0, 's': 1, 'w': 1, 'c': 2 }) ] } }, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) if do_plot: to_plot = [ 'cum_infections', 'cum_recoveries', 'new_infections', 'n_quarantined', 'new_quarantined' ] fig_args = dict(figsize=(24, 16)) scens.plot(do_save=do_save, do_show=do_show, to_plot=to_plot, fig_path=fig_path, n_cols=2, fig_args=fig_args) return scens
def lift_lockdown_paper_screening_scens(): sim = sc.loadobj( '/Users/robynstuart/Documents/git/covid_apps/nigeria_june/nigeria_lockdown_dec.sim' ) n_adults = sim['pop_size'] / 2 pop_scale = sim['pop_scale'] symp_prob_prelockdown = 0.015 symp_prob_lockdown = 0.02 symp_prob_postlockdown = 0.04 beta_change = 0.5 number_screened = [ int(n_adults * x / 10) for x in range(1, 6, 1) ] # Number that could be screened - 10-50% of the population efficacy = [x / 10 for x in range(1, 6, 1)] # to just run one... #number_screened = [int(n_adults*.5)] #efficacy = [0.5] scenarios = { f'screen{sc}_eff{ef}': { 'name': f'Screen {sc} with {ef} efficacy', 'pars': { 'interventions': [ cv.test_prob(symp_prob=symp_prob_prelockdown, asymp_prob=0, start_day=0, end_day='2020-03-29', do_plot=False), cv.test_prob(symp_prob=symp_prob_lockdown, asymp_prob=0, start_day='2020-03-29', end_day='2020-05-04', do_plot=False), cv.test_prob(symp_prob=symp_prob_postlockdown, asymp_prob=0, start_day='2020-05-05', do_plot=False), cv.contact_tracing(start_day='2020-03-01', trace_probs={ 'h': 1, 's': 0, 'w': 0.8, 'c': 0.0 }, trace_time={ 'h': 1, 's': 7, 'w': 7, 'c': 7 }, do_plot=False), cv.change_beta(days=['2020-03-29'], changes=[0], layers=['s'], do_plot=beta_change < 1.0), cv.change_beta(days=['2020-03-29', '2020-05-04'], changes=[beta_change, 0.8], layers=['c'], do_plot=beta_change < 1.0), cv.change_beta(days=['2020-03-29', '2020-05-04'], changes=[beta_change, 0.8], layers=['w'], do_plot=beta_change < 1.0), quarantine_severe(start_day='2020-05-04'), screen(daily_screens=sc, sensitivity=ef, start_day='2020-05-04'), ] } } for sc in number_screened for ef in efficacy } baseline = { f'noscreen': { 'name': f'No screening', 'pars': { 'interventions': [ cv.test_prob(symp_prob=symp_prob_prelockdown, asymp_prob=0, start_day=0, end_day='2020-03-29', do_plot=False), cv.test_prob(symp_prob=symp_prob_lockdown, asymp_prob=0, start_day='2020-03-29', end_day='2020-05-04', do_plot=False), cv.test_prob(symp_prob=symp_prob_postlockdown, asymp_prob=0, start_day='2020-05-05', do_plot=False), cv.contact_tracing(start_day='2020-03-01', trace_probs={ 'h': 1, 's': 0, 'w': 0.8, 'c': 0.0 }, trace_time={ 'h': 1, 's': 7, 'w': 7, 'c': 7 }, do_plot=False), cv.change_beta(days=['2020-03-29'], changes=[0], layers=['s'], do_plot=beta_change < 1.0), cv.change_beta(days=['2020-03-29', '2020-05-04'], changes=[beta_change, 0.8], layers=['c'], do_plot=beta_change < 1.0), cv.change_beta(days=['2020-03-29', '2020-05-04'], changes=[beta_change, 0.8], layers=['w'], do_plot=beta_change < 1.0), quarantine_severe(start_day='2020-05-04') ] } } } metapars = {'n_runs': 1} allscenarios = sc.mergedicts(baseline, scenarios) scens = cv.Scenarios(sim=sim, scenarios=allscenarios, metapars=metapars) df = scens.run(verbose=verbose, debug=False) return df, scens
def test_presumptive_quar(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading('Test impact of presumptive quarantine') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 base_pars = { 'pop_size' : 10000, 'pop_infected': 10, 'pop_type' : 'synthpops', 'n_days' : 150, 'beta' : 0.02, 'quar_period' : 14, 'quar_factor' : {'h': 0.5, 's': 0.1, 'w': 0.1, 'c': 0.1}, # Bidirectional becaues on transmit and receive, e.g. in home 'iso_factor' : {'h': 0.5, 's': 0.1, 'w': 0.1, 'c': 0.1}, # Worried about diagnosed while in quarantine - double impact! } base_sim = cv.Sim(base_pars) # create sim object # DEFINE INTERVENTIONS test_delay = 2 testing = cv.test_prob(symp_prob=0.03, asymp_prob=0.001, symp_quar_prob=0.5, asymp_quar_prob=0.1, test_delay=test_delay) isokwargs = { 'start_day' : 30, 'trace_probs': {'h': 0, 's': 0, 'w': 0, 'c': 0}, 'trace_time' : {'h': 0, 's': 0, 'w': 0, 'c': 0} } ctkwargs = { 'start_day' : 30, 'trace_probs': {'h': 0.9, 's': 0.7, 'w': 0.7, 'c': 0.2}, 'trace_time' : {'h': 0, 's': 2, 'w': 2, 'c': 3} } baseline = { 'name': 'Baseline', 'pars': { 'interventions': [ testing, ] } } contact_tracing = { 'name': 'Contact tracing', 'pars': { 'interventions': [ testing, cv.contact_tracing(**ctkwargs)] } } quar_on_test = { 'name': 'Quarantine on test', 'pars': { 'interventions': [ testing, cv.contact_tracing(**isokwargs, presumptive=True, test_delay=test_delay)] } } # Only if trace_time < test_delay will presumptive tracing follow contacts of negatives to quarantine presumptive_ct = { 'name': 'Contact tracing (presumptive)', 'pars': { 'interventions': [ testing, cv.contact_tracing(**ctkwargs, presumptive=True, test_delay=test_delay)] } } # Define the scenarios scenarios = { 'Baseline': baseline, 'Quarantine on test': quar_on_test, 'Trace on diagnosis': contact_tracing, 'Trace on test': presumptive_ct, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) if do_plot: to_plot = { 'Number of people currently infectious': [ 'n_infectious', ], 'Number of people in quarantine': [ 'n_quarantined', ], 'Number of newly quarantined': [ 'new_quarantined', ], 'Number diagnosed': [ 'n_diagnosed', ], 'Cum inf': [ 'cum_infections', ], 'Re': [ 'r_eff', ], } fig_args = dict(figsize=(24,16)) scens.plot(do_save=do_save, do_show=do_show, to_plot=to_plot, fig_path=fig_path, n_cols=2, fig_args=fig_args) return scens
def test_tracedelay(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading( 'Test impact of reducing delay time for finding contacts of positives') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 base_pars = { 'pop_size': 5000, 'use_layers': True, } base_sim = cv.Sim(base_pars) # create sim object base_sim['n_days'] = 50 #base_sim['contacts'] = {'h': 4, 's': 10, 'w': 10, 'c': 0} # Turn off community contacts - not working #base_sim['beta'] = 0.02 # Increase beta n_people = base_sim['pop_size'] npts = base_sim.npts # Define overall testing assumptions testing_prop = 0.1 # Assumes we could test 10% of the population daily (way too optimistic!!) daily_tests = [testing_prop * n_people] * npts # Number of daily tests # Define the scenarios scenarios = { 'lowtrace': { 'name': '10% daily testing; poor contact tracing, 50% of contacts self-isolate', 'pars': { 'cont_factor': 0.5, 'interventions': [ cv.test_num(daily_tests=daily_tests), cv.contact_tracing(trace_probs={ 'h': 1, 's': 0.8, 'w': 0.5, 'c': 0.0 }, trace_time={ 'h': 0, 's': 7, 'w': 7, 'c': 0 }) ] } }, 'modtrace': { 'name': '10% daily testing; moderate contact tracing, 75% of contacts self-isolate', 'pars': { 'cont_factor': 0.25, 'interventions': [ cv.test_num(daily_tests=daily_tests), cv.contact_tracing(trace_probs={ 'h': 1, 's': 0.8, 'w': 0.5, 'c': 0.1 }, trace_time={ 'h': 0, 's': 3, 'w': 3, 'c': 8 }) ] } }, 'hightrace': { 'name': '10% daily testing; fast contact tracing, 90% of contacts self-isolate', 'pars': { 'cont_factor': 0.1, 'interventions': [ cv.test_num(daily_tests=daily_tests), cv.contact_tracing(trace_probs={ 'h': 1, 's': 0.8, 'w': 0.8, 'c': 0.2 }, trace_time={ 'h': 0, 's': 1, 'w': 1, 'c': 5 }) ] } }, 'crazy': { 'name': '10% daily testing; same-day contact tracing, 100% of contacts self-isolate', 'pars': { 'cont_factor': 0, 'interventions': [ cv.test_num(daily_tests=daily_tests), cv.contact_tracing(trace_probs={ 'h': 1, 's': 1, 'w': 1, 'c': 1 }, trace_time={ 'h': 0, 's': 0, 'w': 0, 'c': 0 }) ] } }, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path=fig_path) return scens
changes=interv_eff) } }, # 'distance2': { # With noise = 0.0, this should be identical to the above # 'name':'Social distancing, version 2', # 'pars': { # 'interventions': cv.dynamic_pars({'beta':dict(days=interv_day, vals=interv_eff*default_beta)}) # } # }, } if __name__ == "__main__": # Required for parallel processing on Windows sc.tic() # If we're rerunning... if do_run: scens = cv.Scenarios(metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose) if do_save: scens.save(filename=obj_path, keep_sims=keep_sims) # Don't run else: scens = cv.Scenarios.load(obj_path) if do_plot: fig1 = scens.plot(do_show=do_show) sc.toc()
def test_varyingimmunity(do_plot=False, do_show=True, do_save=False): sc.heading('Test varying properties of immunity') # Define baseline parameters n_runs = 3 base_sim = cv.Sim(use_waning=True, n_days=400, pars=base_pars) # Define the scenarios b1351 = cv.variant('b1351', days=100, n_imports=20) scenarios = { 'baseline': { 'name': 'Default Immunity (decay at log(2)/90)', 'pars': { 'nab_decay': dict(form='nab_decay', decay_rate1=np.log(2) / 90, decay_time1=250, decay_rate2=0.001), }, }, 'faster_immunity': { 'name': 'Faster Immunity (decay at log(2)/30)', 'pars': { 'nab_decay': dict(form='nab_decay', decay_rate1=np.log(2) / 30, decay_time1=250, decay_rate2=0.001), }, }, 'baseline_b1351': { 'name': 'Default Immunity (decay at log(2)/90), B1351 on day 100', 'pars': { 'nab_decay': dict(form='nab_decay', decay_rate1=np.log(2) / 90, decay_time1=250, decay_rate2=0.001), 'variants': [b1351], }, }, 'faster_immunity_b1351': { 'name': 'Faster Immunity (decay at log(2)/30), B1351 on day 100', 'pars': { 'nab_decay': dict(form='nab_decay', decay_rate1=np.log(2) / 30, decay_time1=250, decay_rate2=0.001), 'variants': [b1351], }, }, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run(debug=debug) to_plot = sc.objdict({ 'New infections': ['new_infections'], 'New re-infections': ['new_reinfections'], 'Population Nabs': ['pop_nabs'], 'Population Immunity': ['pop_protection'], }) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path='results/test_basic_immunity.png', to_plot=to_plot) return scens
def test_vaccine_2variants_scen(do_plot=False, do_show=True, do_save=False): sc.heading( 'Run a basic sim with b117 variant on day 10, pfizer vaccine day 20') # Define baseline parameters n_runs = 3 base_sim = cv.Sim(use_waning=True, pars=base_pars) # Vaccinate 75+, then 65+, then 50+, then 18+ on days 20, 40, 60, 80 base_sim.vxsubtarg = sc.objdict() base_sim.vxsubtarg.age = [75, 65, 50, 18] base_sim.vxsubtarg.prob = [.01, .01, .01, .01] base_sim.vxsubtarg.days = subtarg_days = [60, 150, 200, 220] jnj = cv.vaccinate(days=subtarg_days, vaccine='j&j', subtarget=vacc_subtarg) b1351 = cv.variant('b1351', days=10, n_imports=20) p1 = cv.variant('p1', days=100, n_imports=100) # Define the scenarios scenarios = { 'baseline': { 'name': 'B1351 on day 10, No Vaccine', 'pars': { 'variants': [b1351] } }, 'b1351': { 'name': 'B1351 on day 10, J&J starting on day 60', 'pars': { 'interventions': [jnj], 'variants': [b1351], } }, 'p1': { 'name': 'B1351 on day 10, J&J starting on day 60, p1 on day 100', 'pars': { 'interventions': [jnj], 'variants': [b1351, p1], } }, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run(debug=debug) to_plot = sc.objdict({ 'New infections': ['new_infections'], 'Cumulative infections': ['cum_infections'], 'New reinfections': ['new_reinfections'], # 'Cumulative reinfections': ['cum_reinfections'], }) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path='results/test_vaccine_b1351.png', to_plot=to_plot) return scens
def test_interventions(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading('Test of testing interventions') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 base_pars = { 'pop_size': 1000, 'pop_type': 'hybrid', } base_sim = cv.Sim(base_pars) # create sim object n_people = base_sim['pop_size'] npts = base_sim.npts # Define overall testing assumptions # Remember that this is the daily % of the population that gets tested. S Korea (one of the highest-testing places) tested # an average of 10000 people/day over March, or 270,000 in total. This is ~200 people per million every day (0.02%).... max_optimistic_testing = 0.1 # ... which means that this is an artificially high number, for testing purposes only!! optimistic_daily_tests = [ max_optimistic_testing * n_people ] * npts # Very best-case scenario for asymptomatic testing # Define the scenarios scenarios = { 'baseline': { 'name': 'Status quo, no testing', 'pars': { 'interventions': None, } }, 'test_skorea': { 'name': 'Assuming South Korea testing levels of 0.02% daily (untargeted); isolate positives', 'pars': { 'interventions': cv.test_num(daily_tests=optimistic_daily_tests) } }, 'floating': { 'name': 'Test with constant probability based on symptoms', 'pars': { 'interventions': cv.test_prob(symp_prob=max_optimistic_testing, asymp_prob=0.0) } }, 'sequence': { 'name': 'Historical switching to probability', 'pars': { 'interventions': cv.sequence( days=[10, 51], interventions=[ cv.test_num(daily_tests=optimistic_daily_tests), cv.test_prob(symp_prob=0.2, asymp_prob=0.002), ]) } }, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) to_plot = ['cum_infections', 'n_infectious', 'new_tests', 'new_diagnoses'] fig_args = dict(figsize=(20, 24)) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path=fig_path, interval=7, fig_args=fig_args, to_plot=to_plot) return scens
def test_beds(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading('Test of bed capacity estimation') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 basepars = {'pop_size': 1000} metapars = {'n_runs': n_runs} sim = cv.Sim() # Define the scenarios scenarios = { 'baseline': { 'name': 'No bed constraints', 'pars': { 'pop_infected': 100 } }, 'bedconstraint': { 'name': 'Only 10 beds available', 'pars': { 'pop_infected': 100, 'n_beds': 10, } }, 'bedconstraint2': { 'name': 'Only 1 bed available', 'pars': { 'pop_infected': 100, 'n_beds': 1, # 'OR_no_treat': 10., # nb. scenarios cannot currently overwrite nested parameters # This prevents overwriting OR_no_treat due to recent refactoring but more generally # there are other nested parameters eg. all of those under pars['dur'] } }, } scens = cv.Scenarios(sim=sim, basepars=basepars, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) if do_plot: to_plot = sc.odict({ 'cum_deaths': 'Cumulative deaths', 'bed_capacity': 'People needing beds / beds', 'n_severe': 'Number of cases requiring hospitalization', 'n_critical': 'Number of cases requiring ICU', }) scens.plot(to_plot=to_plot, do_save=do_save, do_show=do_show, fig_path=fig_path) return scens
def test_interventions(do_plot=False, do_show=True, do_save=False, fig_path=None): sc.heading('Test of testing interventions') sc.heading('Setting up...') sc.tic() n_runs = 3 verbose = 1 base_pars = { 'pop_size': 1000, 'use_layers': True, } base_sim = cv.Sim(base_pars) # create sim object n_people = base_sim['pop_size'] npts = base_sim.npts # Define overall testing assumptions # As the most optimistic case, we assume countries could get to South Korea's testing levels. S Korea has tested # an average of 10000 people/day over March, or 270,000 in total. This is ~200 people per million every day (0.02%). max_optimistic_testing = 0.0002 optimistic_daily_tests = [ max_optimistic_testing * n_people ] * npts # Very best-case scenario for asymptomatic testing # Define the scenarios scenarios = { 'baseline': { 'name': 'Status quo, no testing', 'pars': { 'interventions': None, } }, 'test_skorea': { 'name': 'Assuming South Korea testing levels of 0.02% daily (untargeted); isolate positives', 'pars': { 'interventions': cv.test_num(daily_tests=optimistic_daily_tests) } }, 'tracing': { 'name': 'Assuming South Korea testing levels of 0.02% daily (with contact tracing); isolate positives', 'pars': { 'interventions': [ cv.test_num(daily_tests=optimistic_daily_tests), cv.dynamic_pars({ 'quar_eff': { 'days': 20, 'vals': [{ 'h': 0.1, 's': 0.1, 'w': 0.1, 'c': 0.1 }] } }) ] # This means that people who've been in contact with known positives isolate with 90% effectiveness } }, 'floating': { 'name': 'Test with constant probability based on symptoms', 'pars': { 'interventions': cv.test_prob(symp_prob=max_optimistic_testing, asymp_prob=0.0) } }, # 'historical': { # 'name': 'Test a known number of positive cases', # 'pars': { # 'interventions': cv.test_historical(n_tests=[100]*npts, n_positive = [1]*npts) # } # }, 'sequence': { 'name': 'Historical switching to probability', 'pars': { 'interventions': cv.sequence(days=[10, 51], interventions=[ cv.test_num(daily_tests=[1000] * npts), cv.test_prob(symp_prob=0.2, asymp_prob=0.002), ]) } }, } metapars = {'n_runs': n_runs} scens = cv.Scenarios(sim=base_sim, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose, debug=debug) if do_plot: scens.plot(do_save=do_save, do_show=do_show, fig_path=fig_path) return scens
] } }, 'distance2': { 'name': 'unstable work beta', 'pars': { 'interventions': [ cv.change_beta(days=interv_day, changes=0.75, layers='c'), cv.change_beta(days=s_close, changes=0, layers='s'), cv.change_beta([12, 20, 28, 39, 48, 59, 70, 90, 130], [.4, .8, 0.5, 0.7, 0.6, .5, 0.8, 0.6, 1], layers='w'), ] } }, } # Run the scenarios -- this block is required for parallel processing on Windows if __name__ == "__main__": scens = cv.Scenarios(basepars=basepars, metapars=metapars, scenarios=scenarios) scens.run(verbose=verbose) scens.to_excel(xlsx_path) if do_save: scens.save(scenfile=obj_path) if do_plot: fig1 = scens.plot(do_show=do_show)
} }, 'testelderly': { 'name': 'Test the elderly', 'pars': { 'interventions': cv.test_prob(start_day=start_day, symp_prob=0.1, asymp_prob=0.005, subtarget={ 'inds': sim.people.age > 50, 'val': 2.0 }), } }, } to_plot = [ 'cum_infections', 'new_infections', 'cum_diagnoses', 'n_severe', 'n_critical', 'cum_deaths', ] fig_args = dict(figsize=(24, 16)) scens = cv.Scenarios(sim=sim, scenarios=scenarios, metapars={'n_runs': 1}) scens.run(keep_people=True) scens.plot(to_plot=to_plot, n_cols=2, fig_args=fig_args)