def save(self, filename=None, keep_people=False, skip_attrs=None, **kwargs): ''' Save to disk as a gzipped pickle. Args: filename (str or None): the name or path of the file to save to; if None, uses stored kwargs: passed to makefilepath() Returns: filename (str): the validated absolute path to the saved file **Example**:: sim.save() # Saves to a .sim file with the date and time of creation by default ''' if filename is None: filename = self.simfile filename = sc.makefilepath(filename=filename, **kwargs) self.filename = filename # Store the actual saved filename if skip_attrs or not keep_people: obj = self.shrink(skip_attrs=skip_attrs, in_place=False) else: obj = self sc.saveobj(filename=filename, obj=obj) return filename
def make_students(sim, save_pop=False, popfile=None, verbose=None, die=True, reset=False): ''' An analog to population.make_people. It borrows some code from this function to ensure the simulation runs smoothly. ''' # Set inputs and defaults pop_size = int(sim['pop_size']) # Shorten if verbose is None: verbose = sim['verbose'] if popfile is None: popfile = sim.popfile if sim.people and not reset: return sim.people # If it's already there, just return elif sim.popdict and not reset: popdict = sim.popdict # Use stored one sim.popdict = None # Once loaded, remove elif sim['pop_type'] == 'campus': popdict = make_campus(sim) else: raise RuntimeWarning( "populationCampus.make_students only supports the value \'campus\' for \'pop_type\'" ) popdict = make_campus(sim) # Ensure prognoses are set if sim['prognoses'] is None: sim['prognoses'] = cvpars.get_prognoses(sim['prog_by_age']) # Actually create the people people = stu.Students( sim, uid=popdict['uid'], age=popdict['age'], sex=popdict['sex'], contacts=popdict['contacts']) # List for storing the people average_age = sum(popdict['age'] / pop_size) sc.printv( f'Created {pop_size} people, average age {average_age:0.2f} years', 2, verbose) if save_pop: if popfile is None: errormsg = 'Please specify a file to save to using the popfile kwarg' raise FileNotFoundError(errormsg) else: filepath = sc.makefilepath(filename=popfile) sc.saveobj(filepath, people) if verbose: print( f'Saved population of type "{pop_type}" with {pop_size:n} people to {filepath}' ) return people
def save_population(self, filename, **kwargs): ''' Save the population dictionary to file. Args: filename (str): name of the file to save to. ''' filepath = sc.makefilepath(filename=filename, **kwargs) sc.saveobj(filepath, self.popdict) return filepath
def save(self, filename=None, folder=None, verbose=2): ''' Save the current project, by default using its name, and without results ''' fullpath = sc.makefilepath(filename=filename, folder=folder, default=[self.filename, self.name], ext='prj', sanitize=True) self.filename = fullpath # Store file path sc.saveobj(fullpath, self, verbose=verbose) return fullpath
def save(self, filename=None, keep_sims=True, keep_people=False, **kwargs): ''' Save to disk as a gzipped pickle. Args: filename (str or None): the name or path of the file to save to; if None, uses stored keep_sims (bool): whether or not to store the actual Sim objects in the Scenarios object keep_people (bool): whether or not to store the population in the Sim objects (NB, very large) keywords: passed to makefilepath() Returns: filename (str): the validated absolute path to the saved file **Example** :: scens.save() # Saves to a .scens file with the date and time of creation by default ''' if filename is None: filename = self.filename filename = sc.makefilepath(filename=filename, **kwargs) self.filename = filename # Store the actual saved filename # Store sims seperately sims = self.sims self.sims = None # Remove for now obj = sc.dcp(self) # This should be quick once we've removed the sims if not keep_people: obj.base_sim.shrink(in_place=True) if keep_sims: if keep_people: if not obj._kept_people: print( 'Warning: there are no people because they were not saved during the run. ' 'If you want people, please rerun with keep_people=True.' ) obj.sims = sims # Just restore the object in full print('Note: saving people, which may produce a large file!') else: obj.sims = sc.objdict() for key in sims.keys(): obj.sims[key] = [] for sim in sims[key]: obj.sims[key].append(sim.shrink(in_place=False)) sc.saveobj(filename=filename, obj=obj) # Actually save self.sims = sims # Restore return filename
def test_basic_api(): ''' Basic SynthPops test ''' sp.logger.info('Testing basic API') pop = sp.make_population(**pars) if regenerate or not os.path.exists(outfile): print('Saving...') sc.saveobj(outfile, pop) else: print('Checking...') pop2 = sc.loadobj(outfile) assert pop == pop2, 'Check failed' print('Check passed') return pop
def download_project(project_id): """ For the passed in project UID, get the Project on the server, save it in a file, minus results, and pass the full path of this file back. """ proj = load_project(project_id, die=True) # Load the project with the matching UID. file_name = '%s.prj' % proj.name # Create a filename containing the project name followed by a .prj suffix. full_file_name = get_path( file_name, proj.webapp.username) # Generate the full file name with path. sc.saveobj(full_file_name, proj) # Write the object to a Gzip string pickle file. print(">> download_project %s" % (full_file_name)) # Display the call information. return full_file_name # Return the full filename.
def save(self, filename, **kwargs): """ Save population to an binary, gzipped object file. **Example**:: pop.save('my-pop.pop') """ return sc.saveobj(filename, self, **kwargs)
def test_api(do_plot=False): ''' More examples of basic API usage ''' pop = sp.Pop(n=settings.pop_sizes.medium, rand_seed=0) # default parameters, 8k people pop.save('test_api.pop') # save as pickle pop.to_json('test_api.json') # save as JSON pop2 = sc.dcp(pop) pop2.load('test_api.pop') sc.saveobj('test_not_pop.obj', []) with pytest.raises(TypeError): pop2.load('test_not_pop.obj') popdict = pop.to_dict() # export from class to standard python object; current default synthpops output if do_plot: pop.plot_people() # equivalent to cv.Sim.people.plot() pop.plot_contacts() # equivalent to sp.plot_contact_matrix(popdict) pop.plot_contacts(density_or_frequency='frequency', logcolors_flag=False, aggregate_flag=False) # test other options return popdict
def save(*args, **kwargs): ''' Convenience method for sc.saveobj() and equivalent to cv.Sim.save() or cv.Scenarios.save(). **Examples**:: cv.save('calib.sim', sim) cv.save(filename='school-closures.scens', folder='schools', obj=scens) ''' filepath = sc.saveobj(*args, **kwargs) return filepath
def save(self, filename=None, **kwargs): ''' Save to disk as a gzipped pickle. Args: filename (str or None): the name or path of the file to save to; if None, uses stored keywords: passed to makefilepath() Returns: filename (str): the validated absolute path to the saved file Example: sim.save() # Saves to a .sim file with the date and time of creation by default ''' if filename is None: filename = self.filename filename = sc.makefilepath(filename=filename, **kwargs) self.filename = filename # Store the actual saved filename sc.saveobj(filename=filename, obj=self) return filename
def save(self, filename=None, keep_people=False, **kwargs): ''' Save to disk as a gzipped pickle. Load with cv.load(filename). Args: filename (str or None): the name or path of the file to save to; if None, uses default keep_people (bool): whether or not to store the population in the Sim objects (NB, very large) keywords: passed to makefilepath() Returns: scenfile (str): the validated absolute path to the saved file **Example**:: msim.save() # Saves to an .msim file ''' if filename is None: filename = 'covasim.msim' scenfile = sc.makefilepath(filename=filename, **kwargs) self.filename = filename # Store the actual saved filename # Store sims seperately sims = self.sims self.sims = None # Remove for now obj = sc.dcp(self) # This should be quick once we've removed the sims if keep_people: obj.sims = sims # Just restore the object in full print('Note: saving people, which may produce a large file!') else: obj.base_sim.shrink(in_place=True) obj.sims = [] for sim in sims: obj.sims.append(sim.shrink(in_place=False)) sc.saveobj(filename=scenfile, obj=obj) # Actually save self.sims = sims # Restore return scenfile
def train(beta: float = 0.015, pop_infected: int = 10, rel_death_prob: float = 1.0, rel_severe_prob: float = 1.0, rel_crit_prob: float = 1.0, start_day: str = '2019-12-25', datafile='tests/example_data.csv') -> None: """ Perform hyperparameter sweep with Weights and Biases https://docs.wandb.com/sweeps """ sc.makefilepath(datafile, checkexists=True) pars = dict( beta=beta, pop_infected=pop_infected, rel_death_prob=rel_death_prob, rel_crit_prob=rel_crit_prob, start_day=start_day, ) # instantiate wandb run wb_handle = wandb.init(config=pars, project="covasim") run_id = wandb.run.id # Create and run the simulation sc.heading('Hyperparmeter Sweep') sim = cv.Sim(pars=pars, datafile=datafile) sim.run(verbose=False) likelihood = sim.likelihood() # log relevant metrics and artifacts wandb.log({'likelihood': likelihood}) sim.plot(do_show=False, do_save=True, fig_path=sc.makefilepath(folder=wandb.run.dir, filename=f'{run_id}.png')) wandb.save(datafile) sc.saveobj(folder=wandb.run.dir, filename=f'pars_{run_id}.pkl', f)
def test_basic_api(): ''' Basic SynthPops test ''' sp.logger.info('Testing basic API') pop = sp.make_population(**pars) age_distr = sp.read_age_bracket_distr(sp.datadir, country_location='usa', state_location='Washington', location='seattle_metro') assert len( age_distr ) == 20, f'Check failed, len(age_distr): {len(age_distr)}' # will remove if this passes in github actions test if regenerate or not os.path.exists(outfile): print('Saving...') sc.saveobj(outfile, pop) else: print('Checking...') pop2 = sc.loadobj(outfile) print(len(pop), len(pop2)) assert pop == pop2, 'Check failed' print('Check passed') return pop
def save(self, filename=None, keep_people=None, skip_attrs=None, **kwargs): ''' Save to disk as a gzipped pickle. Args: filename (str or None): the name or path of the file to save to; if None, uses stored kwargs: passed to makefilepath() Returns: filename (str): the validated absolute path to the saved file **Example**:: sim.save() # Saves to a .sim file with the date and time of creation by default ''' # Set keep_people based on whether or not we're in the middle of a run if keep_people is None: if self.initialized and not self.results_ready: keep_people = True else: keep_people = False # Handle the filename if filename is None: filename = self.simfile filename = sc.makefilepath(filename=filename, **kwargs) self.filename = filename # Store the actual saved filename # Handle the shrinkage and save if skip_attrs or not keep_people: obj = self.shrink(skip_attrs=skip_attrs, in_place=False) else: obj = self sc.saveobj(filename=filename, obj=obj) return filename
def save(*args, **kwargs): ''' Convenience method for sc.saveobj() and equivalent to cv.Sim.save() or cv.Scenarios.save(). Args: filename (str): file to save to obj (object): object to save args (list): passed to sc.saveobj() kwargs (dict): passed to sc.saveobj() Returns: Filename the object is saved to **Examples**:: cv.save('calib.sim', sim) # Equivalent to sim.save('calib.sim') cv.save(filename='school-closures.scens', folder='schools', obj=scens) ''' filepath = sc.saveobj(*args, **kwargs) return filepath
print(f'Beta: {beta}... ') print('---------------\n') s0 = make_sim(seed=1, beta=beta, end_day=data_end) sims = [] for seed in range(n_runs): sim = s0.copy() sim['rand_seed'] = seed sim.set_seed() sim.label = f"Sim {seed}" sims.append(sim) msim = cv.MultiSim(sims) msim.run() fitsummary.append( [sim.compute_fit().mismatch for sim in msim.sims]) sc.saveobj(f'{resfolder}/fitsummary.obj', fitsummary) # Run calibration with best-fitting seeds and parameters elif whattorun == 'finialisefit': sims = [] fitsummary = sc.loadobj(f'{resfolder}/fitsummary.obj') for bn, beta in enumerate(betas): goodseeds = [i for i in range(n_runs) if fitsummary[bn][i] < 275] #351.5=100, 275=10 sc.blank() print('---------------\n') print(f'Beta: {beta}, goodseeds: {len(goodseeds)}') print('---------------\n') if len(goodseeds) > 0: s0 = make_sim(seed=1, beta=beta, end_day=data_end, verbose=0.1) for seed in goodseeds:
def make_population(pop_size, rand_seed=1, max_pop_seeds=None, do_save=True, popfile=None, cohorting=True, n_brackets=20, community_contacts=20, **kwargs): ''' Generate the synthpops population. Args: pop_size (int): number of people in the model rand_seed (int): random seed to use for generating the population max_pop_seeds (int): if supplied, take the random seed as modulus of this to limit number of populations generated do_save (bool): whether to save the population popfile (str): if so, where to save it to cohorting (bool): whether to use cohorting n_brackets (int): whether to use 16- or 20-bracket age bins community_contacts (int): how many community contacts there are kwargs (dict): passed to sp.make_population() ''' sp.set_nbrackets( n_brackets) # Essential for getting the age distribution right pars = sc.objdict( n=pop_size, rand_seed=rand_seed, with_facilities=True, use_two_group_reduction=True, average_LTCF_degree=20, ltcf_staff_age_min=20, ltcf_staff_age_max=60, with_school_types=True, average_class_size=20, inter_grade_mixing=0.1, average_student_teacher_ratio=20, average_teacher_teacher_degree=3, teacher_age_min=25, teacher_age_max=75, with_non_teaching_staff=True, # if with_non_teaching_staff is False, but generate is True, then ,average_all_staff_ratio should be average_student_teacher_ratio or 0 average_student_all_staff_ratio=11, average_additional_staff_degree=20, staff_age_min=20, staff_age_max=75, ) pars.update(kwargs) # Update any parameters # For reference re: school_types # school_mixing_type = 'random' means that students in the school have edges randomly chosen from other students, teachers, and non teaching staff across the school. Students, teachers, and non teaching staff are treated the same in terms of edge generation. # school_mixing_type = 'age_clustered' means that students in the school have edges mostly within their own age/grade, with teachers, and non teaching staff. Strict classrooms are not generated. Teachers have some additional edges with other teachers. # school_mixing_type = 'age_and_class_clustered' means that students are cohorted into classes of students of the same age/grade with at least 1 teacher, and then some have contact with non teaching staff. Teachers have some additional edges with other teachers. if cohorting: strategy = 'clustered' # students in pre-k, elementary, and middle school are cohorted into strict classrooms pars.school_mixing_type = { 'pk': 'age_and_class_clustered', 'es': 'age_and_class_clustered', 'ms': 'age_and_class_clustered', 'hs': 'random', 'uv': 'random' } else: strategy = 'normal' pars.school_mixing_type = { 'pk': 'age_clustered', 'es': 'age_clustered', 'ms': 'age_clustered', 'hs': 'random', 'uv': 'random' } if popfile is None: popfile = os.path.join( 'inputs', f'kc_{strategy}_{int(pars.n)}_seed{pars.rand_seed}.ppl') T = sc.tic() print('Making population...') # Make the population population = sp.make_population(**pars) # Convert to a popdict popdict = cv.make_synthpop(population=sc.dcp(population), community_contacts=community_contacts) school_ids = [None] * int(pop_size) teacher_flag = [False] * int(pop_size) staff_flag = [False] * int(pop_size) student_flag = [False] * int(pop_size) school_types = {'pk': [], 'es': [], 'ms': [], 'hs': [], 'uv': []} school_type_by_person = [None] * int(pop_size) schools = dict() for uid, person in population.items(): if person['scid'] is not None: school_ids[uid] = person['scid'] school_type_by_person[uid] = person['sc_type'] if person['scid'] not in school_types[person['sc_type']]: school_types[person['sc_type']].append(person['scid']) if person['scid'] in schools: schools[person['scid']].append(uid) else: schools[person['scid']] = [uid] if person['sc_teacher'] is not None: teacher_flag[uid] = True elif person['sc_student'] is not None: student_flag[uid] = True elif person['sc_staff'] is not None: staff_flag[uid] = True popdict['school_id'] = np.array(school_ids) popdict['schools'] = schools popdict['teacher_flag'] = teacher_flag popdict['student_flag'] = student_flag popdict['staff_flag'] = staff_flag popdict['school_types'] = school_types popdict['school_type_by_person'] = school_type_by_person assert sum( popdict['teacher_flag'] ), 'Uh-oh, no teachers were found: as a school analysis this is treated as an error' assert sum( popdict['student_flag'] ), 'Uh-oh, no students were found: as a school analysis this is treated as an error' # Actually create the people people_pars = dict( pop_size=pars.n, beta_layer={k: 1.0 for k in 'hswcl' }, # Since this is used to define hat layers exist beta= 1.0, # TODO: this is required for plotting (people.plot()), but shouldn't be ) people = cv.People(people_pars, strict=False, uid=popdict['uid'], age=popdict['age'], sex=popdict['sex'], contacts=popdict['contacts'], school_id=popdict['school_id'], schools=popdict['schools'], school_types=popdict['school_types'], student_flag=popdict['student_flag'], teacher_flag=popdict['teacher_flag'], staff_flag=popdict['staff_flag'], school_type_by_person=popdict['school_type_by_person']) if do_save: print(f'Saving to "{popfile}"...') sc.saveobj(popfile, people) sc.toc(T) print('Done') return people
def make_people(sim, save_pop=False, popfile=None, verbose=None, die=True, reset=False): ''' Make the actual people for the simulation. Args: sim (Sim): the simulation object verbose (bool): level of detail to print id_len (int): length of ID for each person (default: calculate required length based on the number of people) die (bool): whether or not to fail if synthetic populations are requested but not available reset (bool): whether to force population creation even if self.popdict exists Returns: None. ''' # Set inputs and defaults pop_size = int(sim['pop_size']) # Shorten pop_type = sim['pop_type'] # Shorten if verbose is None: verbose = sim['verbose'] if popfile is None: popfile = sim.popfile # Check which type of population to produce if pop_type == 'synthpops': if not cvreq.check_synthpops(): errormsg = f'You have requested "{pop_type}" population, but synthpops is not available; please use random, clustered, or hybrid' if die: raise ValueError(errormsg) else: print(errormsg) pop_type = 'random' location = sim['location'] if location: print( f'Warning: not setting ages or contacts for "{location}" since synthpops contacts are pre-generated' ) # Actually create the population if sim.popdict and not reset: popdict = sim.popdict # Use stored one layer_keys = list(popdict['contacts'] [0].keys()) # Assume there's at least one contact! sim.popdict = None # Once loaded, remove else: # Create the population if pop_type in ['random', 'clustered', 'hybrid']: popdict, layer_keys = make_randpop(sim, microstructure=pop_type) elif pop_type == 'synthpops': popdict, layer_keys = make_synthpop(sim) else: errormsg = f'Population type "{pop_type}" not found; choices are random, clustered, hybrid, or synthpops' raise ValueError(errormsg) # Ensure prognoses are set if sim['prognoses'] is None: sim['prognoses'] = cvpars.get_prognoses(sim['prog_by_age']) # Actually create the people sim.layer_keys = layer_keys people = cvppl.People(sim.pars, **popdict) # List for storing the people sim.people = people average_age = sum(popdict['age'] / pop_size) sc.printv( f'Created {pop_size} people, average age {average_age:0.2f} years', 2, verbose) if save_pop: if popfile is None: errormsg = 'Please specify a file to save to using the popfile kwarg' raise FileNotFoundError(errormsg) else: filepath = sc.makefilepath(filename=popfile) sc.saveobj(filepath, popdict) if verbose: print( f'Saved population of type "{pop_type}" with {pop_size:n} people to {filepath}' ) return
sim2['quarantine_eff'] = 0.0 sim2.run(verbose=verbose) # No quarantine sim3 = covid_abm.Sim() sim3.set_seed(5) sim3['quarantine_eff'] = 1.0 sim3.run(verbose=verbose) data = sc.odict({'Status quo': sim0, 'No disembarkation': sim1, '100% effective quarantine':sim2, 'No quarantine':sim3 }) sc.saveobj('scenario_data.obj', data) else: data = sc.loadobj('scenario_data.obj') font_size = 18 fig = pl.figure(figsize=(26,12)) pl.rcParams['font.size'] = font_size plot_args = {'lw':4, 'alpha':0.7} for key,datum in data.items(): if key != 'No quarantine': pl.plot(datum.results['t'], datum.results['cum_exposed'], label=key, **plot_args) pl.ylabel('Count') pl.xlabel('Days since index case')
if data['mismatch'] is None: failed_trials.append(data['index']) else: results.append(data) print(f'Processed {len(study.trials)} trials; {len(failed_trials)} failed') sc.heading('Making data structure...') keys = ['index', 'mismatch'] + list(best.keys()) data = sc.objdict().make(keys=keys, vals=[]) for i, r in enumerate(results): for key in keys: if key not in r: print(f'Warning! Key {key} is missing from trial {i}, replacing with default') r[key] = best[key] data[key].append(r[key]) df = pd.DataFrame.from_dict(data) if save_json: order = np.argsort(df['mismatch']) json = [] for o in order: row = df.iloc[o,:].to_dict() rowdict = dict(index=row.pop('index'), mismatch=row.pop('mismatch'), pars={}) for key,val in row.items(): rowdict['pars'][key] = val json.append(rowdict) sc.savejson(f'{name}.json', json, indent=2) saveobj = False if saveobj: # Smaller file, but less portable sc.saveobj(f'{name}.obj', json)
################################################################################################ if process: for thisfig in [resultsfolder,sensfolder]: results = {'cum_infections': {}, 'r_eff': {}, 'new_infections':{}, 'cum_quarantined':{}} for future_test_prob in tlevels: for name in ['cum_infections', 'r_eff', 'new_infections','cum_quarantined']: results[name][future_test_prob] = {} for venue_trace_prob in vlevels: for name in ['cum_infections', 'r_eff', 'new_infections','cum_quarantined']: results[name][future_test_prob][venue_trace_prob] = [] for mask_uptake in mlevels: print(f'mask_uptake: {mask_uptake}, venue_trace_prob: {venue_trace_prob}, future_test_prob: {future_test_prob}') msim = sc.loadobj(f'{thisfig}/nsw_tracingsweeps_T{int(future_test_prob * 100)}_M{int(mask_uptake * 100)}_V{int(venue_trace_prob * 100)}.obj') results['cum_quarantined'][future_test_prob][venue_trace_prob].append(msim.results['cum_quarantined'].values[-1]-msim.results['cum_quarantined'].values[244]) results['cum_infections'][future_test_prob][venue_trace_prob].append(msim.results['cum_infections'].values[-1]-msim.results['cum_infections'].values[244]) results['r_eff'][future_test_prob][venue_trace_prob].append(msim.results['r_eff'].values[-1]) results['new_infections'][future_test_prob][venue_trace_prob].append(msim.results['new_infections'].values) sc.saveobj(f'{thisfig}/nsw_sweep_results.obj', results) #else: # results = sc.loadobj(f'{resultsfolder}/nsw_sweep_results.obj') ################################################################################################################ # Figure 2 and S2: grids of new infections ################################################################################################################ for thisfig in [resultsfolder, sensfolder]: # Fonts and sizes fig = pl.figure(figsize=(24,16)) results = sc.loadobj(f'{thisfig}/nsw_sweep_results.obj')
msim.reduce() # "Reduce" the sims into the statistical representation #to produce mean cumulative infections and deaths for barchart figure print('Mean cumulative values:') print('Deaths: ', msim.results['cum_deaths'][-1]) print('Infections: ', msim.results['cum_infections'][-1]) try: os.makedirs("%s" % args.scenario) except: pass outfile = "%s/test%s-trace%s.obj" % (args.scenario, args.test, args.trace) msim.args = args sc.saveobj( outfile, sc.objdict((("args", args), ("msim", msim.results), ("sims", list(sim.results for sim in msim.sims))))) # Save the key figures plot_customizations = dict( interval=90, # Number of days between tick marks dateformat='%m/%Y', # Date format for ticks fig_args={'figsize': (14, 8)}, # Size of the figure (x and y) axis_args={'left': 0.15}, # Space on left side of plot ) msim.plot_result('r_eff', **plot_customizations) #sim.plot_result('r_eff') pl.axhline(1.0, linestyle='--', c=[0.8, 0.4, 0.4], alpha=0.8, lw=4) # Add a line for the R_eff = 1 cutoff pl.title('')
sc.blank() print('---------------\n') print(f'Beta: {beta}, change: {change}... ') print('---------------\n') s0 = make_sim(seed=1, beta=beta, change=change, end_day=today) sims = [] for seed in range(n_runs): sim = s0.copy() sim['rand_seed'] = seed sim.set_seed() sims.append(sim) msim = cv.MultiSim(sims) msim.run() fitsummary.append([sim.compute_fit().mismatch for sim in msim.sims]) sc.saveobj(f'{resfolder}/fitsummary{change}.obj', fitsummary) # Run calibration with best-fitting seeds and parameters elif whattorun == 'finialisecalibration': sims = [] fitsummary = sc.loadobj(f'{resfolder}/fitsummary{change}.obj') for bn, beta in enumerate(betas): goodseeds = [i for i in range(n_runs) if fitsummary[bn][i] < 82] sc.blank() print('---------------\n') print(f'Beta: {beta}, change: {change}, goodseeds: {len(goodseeds)}') print('---------------\n') if len(goodseeds) > 0: s0 = make_sim(seed=1, beta=beta, change=change, end_day=today) for seed in goodseeds: sim = s0.copy()
# Test spreadsheet saving if check('Spreadsheet'): S = sc.Spreadsheet(files.excel) S.writecells(cells=['A6','B7','C8','D9'], vals=['This','is','a','test']) # Method 1 S.writecells(cells=[pl.array([7,1])+i for i in range(4)], vals=['And','so','is','this']) # Method 2 newdata = (pl.rand(3,3)*100).round() S.writecells(startrow=14, startcol=1, vals=newdata, verbose=True) # Method 3 S.save() data = S.readcells(header=False) print(S) sc.pp(data) if check('saveobj', ['loadobj']): sc.saveobj(files.binary, testdata) if check('loadobj'): obj = sc.loadobj(files.binary) print(obj) if check('savetext', ['loadtext', 'savezip']): sc.savetext(files.text, testdata) if check('loadtext'): obj = sc.loadtext(files.text) print(obj)
dosave = True savedir = '../data' sc.tic() state_location = 'Washington' location = 'seattle_metro' country_location = 'usa' options_args = {'use_microstructure': True} folder = os.path.join(sp.datadir, 'demographics', 'contact_matrices_152_countries', 'Washington') datasets = sc.objdict() for Npop in sp.api.popsize_choices: print(f'Loading {Npop}...') popdict1 = sp.make_contacts_from_microstructure(sp.datadir,location,state_location,country_location,Npop,use_bayesian=True) data = load_data_files(sp.datadir,location,state_location,country_location,Npop,use_bayesian=True) popdict2 = rehydrate(data) datasets[f'n{Npop}'] = data if dosave: sc.saveobj(f'{savedir}/synthpop_{Npop}.pop', data) sc.toc() print('Done.')
sim['interventions'] += [sm] sim.run(keep_people=keep_people) return sim if do_run: if parallelize: sc.heading('Running in parallel...') raw_sims = sc.parallelize(run_sim, all_scens.values()) sims = sc.odict({k:scen for k,scen in zip(all_scens.keys(), raw_sims)}) else: sc.heading('Running in serial...') sims = sc.odict() for k,scen in all_scens: sims[k] = run_sim(scen) if do_save: sc.saveobj(sims_file, sims) else: sc.heading('Loading from disk...') sims = sc.loadobj(sims_file) #%% Analysis sc.heading('Analyzing...') res = sc.objdict() def arr(): return np.zeros((n_scens, n_testings)) res.cum_infections = arr() res.cum_tests = arr()
ext1 = '_1.xlsx' ext2 = '_2.xlsx' files1 = sc.getfilelist(folder, pattern=f'*{ext1}') files2 = sc.getfilelist(folder, pattern=f'*{ext2}') basefilenames = [f[len(folder) + 1:-len(ext1)] for f in files1] # 7 is the length of the extension datadict = {} for fn in basefilenames: datadict[fn] = {} for fn in basefilenames: for i, ext in enumerate([ext1, ext2]): thisfile = folder + os.sep + fn + ext print(f'Working on {thisfile}...') xls = pd.ExcelFile(thisfile) for sheet_name in xls.sheet_names: if i == 0: header = 0 else: header = None df = pd.read_excel(xls, sheet_name=sheet_name, header=header) arr = np.array(df) datadict[fn][sheet_name] = arr for fn, data in datadict.items(): thisfile = folder + os.sep + fn + '.obj' sc.saveobj(thisfile, data) print('Done.')
country = country_data['name'][c] print(f' Working on {country} ({c+1}/{len(country_data)})...') D[country].makepackage(verbose=False) meta = country_data.findrow(country, asdict=True) alloc = [] dalys = [] for spend in spendings: D[country].package().optimize(budget=spend*meta['population']) df = D[country].package().data alloc.append(sc.dcp(df['opt_spend'][:])) dalys.append(sc.dcp(df['opt_dalys_averted'][:])) meta['interv_names'] = sc.dcp(df['shortname'][:]) result = sc.odict({'meta':meta, 'alloc':pl.array(alloc), 'dalys':pl.array(dalys), 'package':D[country].package()}) return result results = sc.parallelize(optimize, iterkwargs={'c':list(range(len(country_data)))}, kwargs={'D':D, 'country_data':country_data}) for r,result in enumerate(results): R[country_data['name'][r]] = result # Saving if dosave: sc.heading('Saving...') sc.saveobj('results/rapid_data.obj', D) sc.saveobj('results/rapid_results.obj', R) sc.toc() print('Done.')
big_msim = cv.MultiSim.merge(*msims) big_msim.run() # Run with uncertainty # Separate the scenarios out and save them final_msims = sc.objdict() final_sims = sc.objdict() keys = set([sim.label for sim in big_msim.sims]) for key in keys: final_sims[key] = [] for sim in big_msim.sims: if sim.label == key: final_sims[key].append(sim) for key in keys: final_msims[key] = cv.MultiSim(sims=final_sims[key]) final_msims[key].reduce() sc.saveobj(filename='uk_scens.obj', obj=final_msims) else: final_msims = sc.loadobj(filename='uk_scens.obj') # Save the key figures if do_plot: # Make individual plots plot_customizations = dict( interval = 90, # Number of days between tick marks dateformat = '%m/%Y', # Date format for ticks fig_args = {'figsize':(14,8)}, # Size of the figure (x and y) axis_args = {'left':0.15}, # Space on left side of plot ) for mkey, msim in final_msims.items():