BaseSim#
- class BaseSim(*args, **kwargs)[source]#
Bases:
ParsObj
The BaseSim class stores various methods useful for the Sim that are not directly related to simulating the epidemic. It is not used outside of the Sim object, so the separation of methods into the BaseSim and Sim classes is purely to keep each one of manageable size.
Attributes
Count the number of people -- if it fails, assume none
Methods
- update_pars(pars=None, create=False, **kwargs)[source]#
Ensure that metaparameters get used properly before being updated
- property n#
Count the number of people – if it fails, assume none
- get_t(dates, exact_match=False, return_date_format=None)[source]#
Convert a string, date/datetime object, or int to a timepoint (int).
- Parameters:
date (str, date, int, or list) – convert any of these objects to a timepoint relative to the simulation’s start day
exact_match (bool) – whether or not to demand an exact match to the requested date
return_date_format (None, str) – if None, do not return dates; otherwise return them as strings or floats as requested
- Returns:
the time point in the simulation cloesst to the requested date
- Return type:
t (int or str)
Examples:
sim.get_t('2015-03-01') # Get the closest timepoint to the specified date sim.get_t(3) # Will return 3 sim.get_t('2015') # Can use strings sim.get_t(['2015.5', '2016.5']) # List of strings, will match as close as possible sim.get_t(['2015.5', '2016.5'], exact_match=True) # Raises an error since these dates aren't directly simulated
- result_keys(which='all')[source]#
Get the actual results objects, not other things stored in sim.results.
If which is ‘total’, return only the main results keys. If ‘genotype’, return only genotype keys. If ‘all’, return all keys.
- result_types(reskeys)[source]#
Figure out what kind of result it is, which determines what plotting style to use
- export_results(for_json=True, filename=None, indent=2, *args, **kwargs)[source]#
Convert results to dict – see also to_json().
The results written to Excel must have a regular table shape, whereas for the JSON output, arbitrary data shapes are supported.
- Parameters:
for_json (bool) – if False, only data associated with Result objects will be included in the converted output
filename (str) – filename to save to; if None, do not save
indent (int) – indent (int): if writing to file, how many indents to use per nested level
args (list) – passed to savejson()
kwargs (dict) – passed to savejson()
- Returns:
dictionary representation of the results
- Return type:
resdict (dict)
- export_pars(filename=None, indent=2, *args, **kwargs)[source]#
Return parameters for JSON export – see also to_json().
This method is required so that interventions can specify their JSON-friendly representation.
- Parameters:
filename (str) – filename to save to; if None, do not save
indent (int) – indent (int): if writing to file, how many indents to use per nested level
args (list) – passed to savejson()
kwargs (dict) – passed to savejson()
- Returns:
a dictionary containing all the parameter values
- Return type:
pardict (dict)
- to_json(filename=None, keys=None, tostring=False, indent=2, verbose=False, *args, **kwargs)[source]#
Export results and parameters as JSON.
- Parameters:
filename (str) – if None, return string; else, write to file
keys (str or list) – attributes to write to json (default: results, parameters, and summary)
tostring (bool) – if not writing to file, whether to write to string (alternative is sanitized dictionary)
indent (int) – if writing to file, how many indents to use per nested level
verbose (bool) – detail to print
args (list) – passed to savejson()
kwargs (dict) – passed to savejson()
- Returns:
A unicode string containing a JSON representation of the results, or writes the JSON file to disk
Examples:
json = sim.to_json() sim.to_json('results.json') sim.to_json('summary.json', keys='summary')
- to_df(date_index=False)[source]#
Export results to a pandas dataframe
- Parameters:
date_index (bool) – if True, use the date as the index
- to_excel(filename=None, skip_pars=None)[source]#
Export parameters and results as Excel format
- Parameters:
filename (str) – if None, return string; else, write to file
skip_pars (list) – if provided, a custom list parameters to exclude
- Returns:
An sc.Spreadsheet with an Excel file, or writes the file to disk
- shrink(skip_attrs=None, in_place=True)[source]#
“Shrinks” the simulation by removing the people and other memory-intensive attributes (e.g., some interventions and analyzers), and returns a copy of the “shrunken” simulation. Used to reduce the memory required for RAM or for saved files.
- Parameters:
skip_attrs (list) – a list of attributes to skip (remove) in order to perform the shrinking; default “people”
in_palce (bool) – whether to perform the shrinking in place (default), or return a shrunken copy instead
- Returns:
a Sim object with the listed attributes removed
- Return type:
shrunken (Sim)
- save(filename=None, keep_people=None, skip_attrs=None, **kwargs)[source]#
Save to disk as a gzipped pickle.
- Parameters:
filename (str or None) – the name or path of the file to save to; if None, uses stored
kwargs – passed to sc.makefilepath()
- Returns:
the validated absolute path to the saved file
- Return type:
filename (str)
Example:
sim.save() # Saves to a .sim file
- static load(filename, *args, **kwargs)[source]#
Load from disk from a gzipped pickle.
- Parameters:
filename (str) – the name or path of the file to load from
kwargs – passed to hpv.load()
- Returns:
the loaded simulation object
- Return type:
sim (Sim)
Example:
sim = hpv.Sim.load('my-simulation.sim')
- get_interventions(label=None, partial=False, as_inds=False)[source]#
Find the matching intervention(s) by label, index, or type. If None, return all interventions. If the label provided is “summary”, then print a summary of the interventions (index, label, type).
- Parameters:
label (str, int, Intervention, list) – the label, index, or type of intervention to get; if a list, iterate over one of those types
partial (bool) – if true, return partial matches (e.g. ‘beta’ will match all beta interventions)
as_inds (bool) – if true, return matching indices instead of the actual interventions
Examples:
tp = hpv.test_prob(symp_prob=0.1) cb1 = hpv.change_beta(days=5, changes=0.3, label='NPI') cb2 = hpv.change_beta(days=10, changes=0.3, label='Masks') sim = hpv.Sim(interventions=[tp, cb1, cb2]) cb1, cb2 = sim.get_interventions(hpv.change_beta) tp, cb2 = sim.get_interventions([0,2]) ind = sim.get_interventions(hpv.change_beta, as_inds=True) # Returns [1,2] sim.get_interventions('summary') # Prints a summary
- get_intervention(label=None, partial=False, first=False, die=True)[source]#
Like get_interventions(), find the matching intervention(s) by label, index, or type. If more than one intervention matches, return the last by default. If no label is provided, return the last intervention in the list.
- Parameters:
label (str, int, Intervention, list) – the label, index, or type of intervention to get; if a list, iterate over one of those types
partial (bool) – if true, return partial matches (e.g. ‘beta’ will match all beta interventions)
first (bool) – if true, return first matching intervention (otherwise, return last)
die (bool) – whether to raise an exception if no intervention is found
Examples:
tp = hpv.test_prob(symp_prob=0.1) cb = hpv.change_beta(days=5, changes=0.3, label='NPI') sim = hpv.Sim(interventions=[tp, cb]) cb = sim.get_intervention('NPI') cb = sim.get_intervention('NP', partial=True) cb = sim.get_intervention(hpv.change_beta) cb = sim.get_intervention(1) cb = sim.get_intervention() tp = sim.get_intervention(first=True)