def test_import2strains(do_plot=False, do_show=True, do_save=False): sc.heading('Test introducing 2 new strains partway through a sim') b117 = cv.strain('b117', days=1, n_imports=20) p1 = cv.strain('sa variant', days=2, n_imports=20) sim = cv.Sim(use_waning=True, strains=[b117, p1], label='With imported infections', **base_pars) sim.run() return sim
def test_baseline(): ''' Compare the current default sim against the saved baseline ''' # Load existing baseline filepath = sc.makefilepath(filename=baseline_filename, folder=sc.thisdir(__file__)) baseline = sc.loadjson(filepath) old = baseline[baseline_key] # Calculate new baseline sim = cv.Sim(verbose=0) sim.run() new = sim.summary # Compare keys errormsg = '' old_keys = set(old.keys()) new_keys = set(new.keys()) if old_keys != new_keys: errormsg = f"Keys don't match!\n" missing = old_keys - new_keys extra = new_keys - old_keys if missing: errormsg += f' Missing old keys: {missing}\n' if extra: errormsg += f' Extra new keys: {extra}\n' mismatches = {} for key in old_keys.union(new_keys): old_val = old[key] if key in old else 'not present' new_val = new[key] if key in new else 'not present' if old_val != new_val: mismatches[key] = {'old': old_val, 'new': new_val} if len(mismatches): errormsg += '\nMismatches:\n' space = ' ' * 17 for mkey, mval in mismatches.items(): errormsg += f' {mkey}:\n' errormsg += f'{space}old = {mval["old"]}\n' errormsg += f'{space}new = {mval["new"]}\n' # Raise an error if mismatches were found if errormsg: prefix = '\nThe following values have changed between the previous baseline and now!\n' prefix += 'If this is intentional, please rerun "update_baseline" and commit.\n\n' err = prefix + errormsg raise ValueError(err) else: print('Baseline matches') return new
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 = 2 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 50 beds available', 'pars': { 'pop_infected': 100, 'n_beds': 50, } }, 'bedconstraint2': { 'name': 'Only 10 beds available', 'pars': { 'pop_infected': 100, 'n_beds': 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({ 'Cumulative deaths': 'cum_deaths', 'People needing beds / beds': 'bed_capacity', 'Number of cases requiring hospitalization': 'n_severe', 'Number of cases requiring ICU': 'n_critical', }) scens.plot(to_plot=to_plot, do_save=do_save, do_show=do_show, fig_path=fig_path) return scens
def test_variants(do_plot=False): sc.heading('Testing variants...') b117 = cv.variant('b117', days=10, n_imports=20) p1 = cv.variant('sa variant', days=20, n_imports=20) cust = cv.variant(label='Custom', days=40, n_imports=20, variant={'rel_beta': 2, 'rel_symp_prob': 1.6}) sim = cv.Sim(base_pars, use_waning=True, variants=[b117, p1, cust]) sim.run() if do_plot: sim.plot('overview-variant') return sim
def test_vaccine_1variant(do_plot=False, do_show=True, do_save=False): sc.heading('Test vaccination with a single variant') pars = sc.mergedicts(base_pars, { 'beta': 0.015, 'n_days': 120, }) pfizer = cv.vaccinate(days=[20], vaccine='pfizer') sim = cv.Sim(use_waning=True, pars=pars, interventions=pfizer) sim.run() return sim
def save_baseline(do_save=do_save): ''' Refresh the baseline results ''' print('Updating baseline values...') sim = cv.Sim(verbose=0) sim.run() if do_save: sim.to_json(filename=baseline_filename, keys=baseline_key) sim.export_pars(filename=parameters_filename) print('Done.') return sim
def test_singlerun(): sc.heading('Single run test') iterpars = { 'beta': 0.035, } sim = cv.Sim() sim['n_days'] = 20 sim['pop_size'] = 1000 sim = cv.single_run(sim=sim, **iterpars) return sim
def make_sim(use_defaults=False, do_plot=False, **kwargs): ''' Define a default simulation for testing the baseline -- use hybrid and include interventions to increase coverage. If run directly (not via pytest), also plot the sim by default. ''' # Define the interventions cb = cv.change_beta(days=40, changes=0.5) tp = cv.test_prob(start_day=20, symp_prob=0.1, asymp_prob=0.01) ct = cv.contact_tracing(trace_probs=0.3, start_day=50) # Define the parameters pars = dict( pop_size=20e3, # Population size pop_infected= 100, # Number of initial infections -- use more for increased robustness pop_type= 'hybrid', # Population to use -- "hybrid" is random with household, school,and work structure n_days=60, # Number of days to simulate verbose=0, # Don't print details of the run rand_seed=2, # Set a non-default seed interventions=[cb, tp, ct], # Include the most common interventions ) pars = sc.mergedicts(pars, kwargs) # Create the sim if use_defaults: sim = cv.Sim() else: sim = cv.Sim(pars) # Optionally plot if do_plot: s2 = sim.copy() s2.run() s2.plot() return sim
def test_start_stop(): # If being run via pytest, turn off sc.heading('Test starting and stopping') pars = {'pop_size': 1000} # Create and run a basic simulation sim1 = cv.Sim(pars) sim1.run(verbose=0) # Test that step works sim2 = cv.Sim(pars) sim2.initialize() for n in range(sim2.npts): sim2.step() sim2.finalize() # Compare results key = 'cum_infections' assert (sim1.results[key][:] == sim2.results[key][:] ).all(), 'Next values do not match' return sim2
def plot_people(self, *args, **kwargs): """Placeholder example of plotting the people in a population.""" import covasim as cv # Optional import pars = dict( pop_size = self.n, pop_type = 'synthpops', beta_layer = {k: 1 for k in 'hscwl'}, ) sim = cv.Sim(pars, popfile=self.popdict) ppl = cv.make_people(sim) # Create the corresponding population fig = ppl.plot(*args, **kwargs) return fig
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 plot_contact_matrix_after_intervention(n, n_days, interventions, intervention_name, location='seattle_metro', state_location='Washington', country_location='usa', aggregate_flag=True, logcolors_flag=True, density_or_frequency='density', setting_code='H', cmap='cmr.freeze_r', fontsize=16, rotation=50): """ Args: intervention (cv.intervention): a single intervention """ pars = sc.objdict(pop_size=n, n_days=n_days, pop_type='synthpops') # sim = sc.objdict() sim = cv.Sim(pars=pars, interventions=interventions) sim.run() age_brackets = sp.get_census_age_brackets( sp.datadir, state_location=state_location, country_location=country_location) age_by_brackets_dic = sp.get_age_by_brackets_dic(age_brackets) ages = sim.people.age ages = np.round(ages, 1) ages = ages.astype(int) max_age = max(ages) age_count = Counter(ages) age_count = dict(age_count) for i in range(max_age + 1): if i not in age_count: age_count[i] = 0 aggregate_age_count = sp.get_aggregate_ages(age_count, age_by_brackets_dic) matrix = calculate_contact_matrix(sim, density_or_frequency, setting_code) fig = sp.plot_contact_matrix(matrix, age_count, aggregate_age_count, age_brackets, age_by_brackets_dic, setting_code, density_or_frequency, logcolors_flag, aggregate_flag, cmap, fontsize, rotation) return fig
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 mrun(): ''' Split these out explicitly since memory is counted per line ''' print('Dummy run to load any missing libraries') sim = cv.Sim(verbose=0) sim.run() popsize = popsizes[0] print(f'Working on {popsize} for {cv.defaults.default_int}...') sim = cv.Sim(pop_size=popsize, verbose=0) sim.run() popsize = popsizes[1] print(f'Working on {popsize} for {cv.defaults.default_int}...') sim = cv.Sim(pop_size=popsize, verbose=0) sim.run() popsize = popsizes[2] print(f'Working on {popsize} for {cv.defaults.default_int}...') sim = cv.Sim(pop_size=popsize, verbose=0) sim.run() return sim
def test_microsim(): sc.heading('Minimal sim test') sim = cv.Sim() pars = { 'pop_size': 10, 'pop_infected': 1, 'n_days': 10, 'contacts': 2, } sim.update_pars(pars) sim.run() return sim
def examplew1(): # run single sim pars = {'use_waning': True, 'rand_seed': 1} variants = [cv.variant('delta', days=15, n_imports=10)] sim = cv.Sim(pars=pars, variants=variants) sim['interventions'] += [ cv.historical_wave(variant='wild', prob=[0.05, 0.05], days_prior=[150, 50]) ] sim.run() sim.plot() sim.plot('variants')
def test_multisim_reduce(do_plot=do_plot): # If being run via pytest, turn off sc.heading('Combine results test') n_runs = 3 pop_infected = 10 sim = cv.Sim(pop_size=pop_size, pop_infected=pop_infected) msim = cv.MultiSim(sim, n_runs=n_runs, noise=0.1) msim.run(verbose=verbose, reduce=True) if do_plot: msim.plot() return msim
def test_snapshot(): sc.heading('Testing snapshot analyzer') sim = cv.Sim(pars, analyzers=cv.snapshot('2020-04-04', '2020-04-14')) sim.run() snapshot = sim.get_analyzer() people1 = snapshot.snapshots[0] # Option 1 people2 = snapshot.snapshots['2020-04-04'] # Option 2 people3 = snapshot.get('2020-04-14') # Option 3 people4 = snapshot.get(34) # Option 4 people5 = snapshot.get() # Option 5 assert people1 == people2, 'Snapshot options should match but do not' assert people3 != people4, 'Snapshot options should not match but do' return people5
def test_combine(do_plot=False): # If being run via pytest, turn off sc.heading('Combine results test') n_runs = 5 n = 2000 n_infected = 100 print('Running first sim...') sim = cv.Sim({'n': n, 'n_infected': n_infected}) sim = cv.multi_run(sim=sim, n_runs=n_runs, combine=True) assert len(sim.people) == n * n_runs print( 'Running second sim, results should be similar but not identical (stochastic differences)...' ) sim2 = cv.Sim({'n': n * n_runs, 'n_infected': n_infected * n_runs}) sim2.run() if do_plot: sim.plot() sim2.plot() return sim
def cova(): sc.tic() s1 = cv.Sim(pop_size=ps2, pop_type='random') s1.initialize() sc.toc() sc.tic() s2 = cv.Sim(pop_size=ps2, pop_type='hybrid') s2.initialize() sc.toc() sc.tic() s3 = cv.Sim(pop_size=ps2, pop_type='synthpops') s3.initialize() sc.toc() sc.tic() s4 = cv.Sim(pop_size=ps1, pop_type='synthpops') s4.initialize() sc.toc() return [s1, s2, s3, s4]
def test_freq_dial(self): is_debugging = False SIM_PARAMS8 = { 'pop_size': 10e3, 'pop_type': 'synthpops', 'pop_infected': 5e3, 'verbose': 1, 'start_day': '2020-08-01', 'end_day': '2020-08-30', 'rand_seed': 0, } sim = cv.Sim(SIM_PARAMS8, popfile=popfile, load_pop=True) # Testing daily, bidaily, and every third day testing, trace set to 0 so it doesn't mess with validation # since if you have teachers tested and traced then infectious kiddos would be sent home and the infection rate would # be substantially lower results = [] for i in range(1, 3): reopen_schools = cv.reopen_schools(start_day={ 'pk': '2020-08-05', 'es': '2020-08-05', 'ms': '2020-08-05', 'hs': '2020-08-05', 'uv': '2020-08-05' }, test_freq=i, trace=0, ili_prev=0, label='reopen_schools') interventions = [ cv.close_schools(day_schools_closed='2020-08-01', label='close_schools'), reopen_schools ] sim['interventions'] = interventions sim.run() # May want to load school_info from differently saved json #sim.to_json(output_filename=f"DEBUG_test_freq_{i}.json") #with open(f"json_test_allinfected.json") as f: # json_format = json.load(f) results.append(sim.school_info['num_teachers_tested']) # This requires a bit of fenegling and testing to determine reasonable relative # values, but more frequent testing should lead to more diagnoses in absence of tracing self.assertGreater(results[0], results[1]) self.assertGreater(results[1], results[2])
def test_default_death_prob_one(self): """ Infect lots of people with cfr one and short time to die duration. Verify that everyone dies, no recoveries. """ pop_size = 200 n_days = 90 sim = cv.Sim(pop_size=pop_size, pop_infected=pop_size, n_days=n_days) for key in [ 'rel_symp_prob', 'rel_severe_prob', 'rel_crit_prob', 'rel_death_prob' ]: sim[key] = 1e6 sim.run() assert sim.summary.cum_deaths == pop_size
def test_fit(): sc.heading('Testing fitting function') # Create a testing intervention to ensure some fit to data tp = cv.test_prob(0.1) sim = cv.Sim(pars, rand_seed=1, interventions=tp, datafile="example_data.csv") sim.run() # Checking that Fit can handle custom input custom_inputs = { 'custom_data': { 'data': np.array([1, 2, 3]), 'sim': np.array([1, 2, 4]), 'weights': [2.0, 3.0, 4.0] } } fit1 = sim.compute_fit(custom=custom_inputs, compute=True) # Test that different seed will change compute results sim2 = cv.Sim(pars, rand_seed=2, interventions=tp, datafile="example_data.csv") sim2.run() fit2 = sim2.compute_fit(custom=custom_inputs) assert fit1.mismatch != fit2.mismatch, "Differences between fit and data remains unchanged after changing sim seed" if do_plot: fit1.plot() return fit1
def create_sim(self, x, verbose=0): ''' Create the simulation from the parameters ''' if isinstance(x, dict): pars, pkeys = self.get_bounds() # Get parameter guesses x = [x[k] for k in pkeys] self.calibration_parameters = x pop_infected = math.exp(x[0]) beta = math.exp(x[1]) # NB: optimization over only two parameters # beta_day = x[2] # beta_change = x[3] # symp_test = x[4] beta_day = 50 beta_change = 0.5 symp_test = 30 # Create parameters pars = dict( pop_size=self.pop_size, pop_scale=self.total_pop / self.pop_size, pop_infected=pop_infected, beta=beta, start_day=self.start_day, end_day=self.end_day, rescale=True, verbose=verbose, ) # Create the sim sim = cv.Sim(pars, datafile=self.datafile) # Add interventions interventions = [ cv.change_beta(days=beta_day, changes=beta_change), cv.test_num(daily_tests=sim.data['new_tests'].dropna(), symp_test=symp_test), ] # Update sim.update_pars(interventions=interventions) self.sim = sim return sim
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_change_beta_layers_clustered(self): # Define the interventions days = dict(h=30, s=35, w=40, c=45) interventions = [] for key, day in days.items(): interventions.append( cv.change_beta(days=day, changes=0, layers=key)) # Create and run the sim sim = cv.Sim(pop_size=AGENT_COUNT, pop_type='hybrid', n_days=60, interventions=interventions) sim.run() assert sim.results['new_infections'].values[days['c']:].sum() == 0 return
def test_multisim_reduce(do_plot=False): # If being run via pytest, turn off sc.heading('Combine results test') n_runs = 3 pop_size = 1000 pop_infected = 10 print('Running first sim...') sim = cv.Sim(pop_size=pop_size, pop_infected=pop_infected) msim = cv.MultiSim(sim, n_runs=n_runs, noise=0.1) msim.run(verbose=verbose) msim.reduce() if do_plot: msim.plot() return msim
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 = 2 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_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_data_interventions(): sc.heading('Testing data interventions and other special cases') # Create sim sim = cv.Sim(pop_size=100, n_days=60, datafile=csv_file, verbose=verbose) # Intervention conversion ce = cv.InterventionDict(**{ 'which': 'clip_edges', 'pars': { 'days': [10, 30], 'changes': [0.5, 1.0] } }) print(ce) with pytest.raises(sc.KeyNotFoundError): cv.InterventionDict(**{ 'which': 'invalid', 'pars': { 'days': 10, 'changes': 0.5 } }) # Test numbers and contact tracing tn1 = cv.test_num(10, start_day=3, end_day=20, ili_prev=0.1, swab_delay={ 'dist': 'uniform', 'par1': 1, 'par2': 3 }) tn2 = cv.test_num(daily_tests='data', quar_policy=[0, 5], subtarget={ 'inds': lambda sim: cv.true(sim.people.age > 50), 'vals': 1.2 }) ct = cv.contact_tracing() # Create and run sim['interventions'] = [ce, tn1, tn2, ct] sim.run() return
def test_sim(do_plot=False, do_save=False, do_show=False): # If being run via pytest, turn off sc.heading('Basic sim test') # Settings seed = 1 verbose = 1 # Create and run the simulation sim = cv.Sim() sim.set_seed(seed) sim.run(verbose=verbose) # Optionally plot if do_plot: sim.plot(do_save=do_save, do_show=do_show) return sim