covasim.interventions module¶
Specify the core interventions available in Covasim. Other interventions can be defined by the user by inheriting from these classes.
-
covasim.interventions.
InterventionDict
(which, pars)¶ Generate an intervention from a dictionary. Although a function, it acts like a class, since it returns a class instance.
Example:
interv = cv.InterventionDict(which='change_beta', pars={'days': 30, 'changes': 0.5, 'layers': None})
-
class
covasim.interventions.
Intervention
(label=None, show_label=True, do_plot=None, line_args=None)¶ Bases:
object
Base class for interventions. By default, interventions are printed using a dict format, which they can be recreated from. To display all the attributes of the intervention, use disp() instead.
- Parameters
label (str) – a label for the intervention (used for plotting, and for ease of identification)
show_label (bool) – whether or not to include the label, if provided, in the legend
do_plot (bool) – whether or not to plot the intervention
line_args (dict) – arguments passed to pl.axvline() whe plotting
-
disp
()¶ Print a detailed representation of the intervention
-
initialize
(sim)¶ Initialize intervention – this is used to make modifications to the intervention that can’t be done until after the sim is created.
-
apply
(sim)¶ Apply intervention
Function signature matches existing intervention definition This method gets called at each timestep and must be implemented by derived classes
- Parameters
sim – The Sim instance
- Returns
None
-
plot
(sim, ax=None, **kwargs)¶ Call function during plotting
This can be used to do things like add vertical lines on days when interventions take place. Can be disabled by setting self.do_plot=False.
- Parameters
sim – the Sim instance
ax – the axis instance
kwargs – passed to ax.axvline()
- Returns
None
-
to_json
()¶ Return JSON-compatible representation
Custom classes can’t be directly represented in JSON. This method is a one-way export to produce a JSON-compatible representation of the intervention. In the first instance, the object dict will be returned. However, if an intervention itself contains non-standard variables as attributes, then its to_json method will need to handle those
- Returns
JSON-serializable representation (typically a dict, but could be anything else)
-
class
covasim.interventions.
dynamic_pars
(pars, **kwargs)¶ Bases:
covasim.interventions.Intervention
A generic intervention that modifies a set of parameters at specified points in time.
The intervention takes a single argument, pars, which is a dictionary of which parameters to change, with following structure: keys are the parameters to change, then subkeys ‘days’ and ‘vals’ are either a scalar or list of when the change(s) should take effect and what the new value should be, respectively.
- Parameters
pars (dict) – described above
kwargs (dict) – passed to Intervention()
Examples:
interv = cv.dynamic_pars({'beta':{'days':[14, 28], 'vals':[0.005, 0.015]}, 'rel_death_prob':{'days':30, 'vals':2.0}}) # Change beta, and make diagnosed people stop transmitting
-
apply
(sim)¶ Loop over the parameters, and then loop over the days, applying them if any are found
-
class
covasim.interventions.
sequence
(days, interventions, **kwargs)¶ Bases:
covasim.interventions.Intervention
This is an example of a meta-intervention which switches between a sequence of interventions.
- Parameters
days (list) – the days on which to start applying each intervention
interventions (list) – the interventions to apply on those days
kwargs (dict) – passed to Intervention()
Example:
interv = cv.sequence(days=[10, 51], interventions=[ cv.test_num(n_tests=[100]*npts), cv.test_prob(symptomatic_prob=0.2, asymptomatic_prob=0.002), ])
-
initialize
(sim)¶ Fix the dates
-
apply
(sim)¶ Apply intervention
Function signature matches existing intervention definition This method gets called at each timestep and must be implemented by derived classes
- Parameters
sim – The Sim instance
- Returns
None
-
class
covasim.interventions.
change_beta
(days, changes, layers=None, **kwargs)¶ Bases:
covasim.interventions.Intervention
The most basic intervention – change beta by a certain amount.
- Parameters
days (int or array) – the day or array of days to apply the interventions
changes (float or array) – the changes in beta (1 = no change, 0 = no transmission)
layers (str or list) – the layers in which to change beta
kwargs (dict) – passed to Intervention()
Examples:
interv = cv.change_beta(25, 0.3) # On day 25, reduce overall beta by 70% to 0.3 interv = cv.change_beta([14, 28], [0.7, 1], layers='s') # On day 14, reduce beta by 30%, and on day 28, return to 1 for schools
-
initialize
(sim)¶ Fix days and store beta
-
apply
(sim)¶ Apply intervention
Function signature matches existing intervention definition This method gets called at each timestep and must be implemented by derived classes
- Parameters
sim – The Sim instance
- Returns
None
-
class
covasim.interventions.
clip_edges
(days, changes, layers=None, **kwargs)¶ Bases:
covasim.interventions.Intervention
Isolate contacts by removing them from the simulation. Contacts are treated as “edges”, and this intervention works by removing them from sim.people.contacts and storing them internally. When the intervention is over, they are moved back.
- Parameters
days (int or array) – the day or array of days to isolate contacts
changes (float or array) – the changes in the number of contacts (1 = no change, 0 = no contacts)
layers (str or list) – the layers in which to isolate contacts (if None, then all layers)
kwargs (dict) – passed to Intervention()
Examples:
interv = cv.clip_edges(25, 0.3) # On day 25, reduce overall contacts by 70% to 0.3 interv = cv.clip_edges([14, 28], [0.7, 1], layers='w') # On day 14, remove 30% of school contacts, and on day 28, restore them
-
initialize
(sim)¶ Initialize intervention – this is used to make modifications to the intervention that can’t be done until after the sim is created.
-
apply
(sim)¶ Apply intervention
Function signature matches existing intervention definition This method gets called at each timestep and must be implemented by derived classes
- Parameters
sim – The Sim instance
- Returns
None
-
class
covasim.interventions.
test_num
(daily_tests, symp_test=100.0, quar_test=1.0, quar_policy=None, subtarget=None, ili_prev=None, sensitivity=1.0, loss_prob=0, test_delay=0, start_day=0, end_day=None, swab_delay=None, **kwargs)¶ Bases:
covasim.interventions.Intervention
Test a fixed number of people per day.
- Parameters
daily_tests (arr) – number of tests per day, can be int, array, or dataframe/series; if integer, use that number every day
symp_test (float) – odds ratio of a symptomatic person testing
quar_test (float) – probability of a person in quarantine testing
quar_policy (str) – policy for testing in quarantine: options are ‘start’, ‘end’, ‘both’ (start and end), ‘daily’
subtarget (dict) – subtarget intervention to people with particular indices ( format : {‘ind’ : array of indices, or function to return indices from the sim, ‘vals’ : value ( s) to apply}
sensitivity (float) – test sensitivity
ili_prev (arr) – Prevalence of influenza-like-illness symptoms in the population; can be float, array, or dataframe/series
loss_prob (float) – probability of the person being lost-to-follow-up
test_delay (int) – days for test result to be known
start_day (int) – day the intervention starts
end_day (int) – day the intervention ends
swab_delay (dict) – distribution for the delay from onset to swab
kwargs (dict) – passed to Intervention ( )
Examples:
interv = cv.test_num(daily_tests=[0.10*n_people]*npts) interv = cv.test_num(daily_tests=[0.10*n_people]*npts, subtarget={'inds': sim.people.age>50, 'vals': 1.2}) # People over 50 are 20% more likely to test interv = cv.test_num(daily_tests=[0.10*n_people]*npts, subtarget={'inds': lambda sim: sim.people.age>50, 'vals': 1.2}) # People over 50 are 20% more likely to test
-
initialize
(sim)¶ Fix the dates and number of tests
-
apply
(sim)¶ Apply intervention
Function signature matches existing intervention definition This method gets called at each timestep and must be implemented by derived classes
- Parameters
sim – The Sim instance
- Returns
None
-
class
covasim.interventions.
test_prob
(symp_prob, asymp_prob=0.0, symp_quar_prob=None, asymp_quar_prob=None, quar_policy=None, subtarget=None, ili_prev=None, test_sensitivity=1.0, loss_prob=0.0, test_delay=0, start_day=0, end_day=None, swab_delay=None, **kwargs)¶ Bases:
covasim.interventions.Intervention
Test as many people as required based on test probability. Probabilities are OR together, so choose wisely.
- Parameters
symp_prob (float) – Probability of testing a symptomatic (unquarantined) person
asymp_prob (float) – Probability of testing an asymptomatic (unquarantined) person
symp_quar_prob (float) – Probability of testing a symptomatic quarantined person
asymp_quar_prob (float) – Probability of testing an asymptomatic quarantined person
quar_policy (str) – Policy for testing in quarantine: options are ‘start’, ‘end’, ‘both’ (start and end), ‘daily’
subtarget (dict) – subtarget intervention to people with particular indices (see test_num() for details)
test_sensitivity (float) – Probability of a true positive
ili_prev (float or array) – Prevalence of influenza-like-illness symptoms in the population
loss_prob (float) – Probability of loss to follow-up
test_delay (int) – How long testing takes
start_day (int) – When to start the intervention
kwargs (dict) – passed to Intervention()
Examples:
interv = cv.test_prob(symp_prob=0.1, asymp_prob=0.01) # Test 10% of symptomatics and 1% of asymptomatics interv = cv.test_prob(symp_quar_prob=0.4) # Test 40% of those in quarantine with symptoms
-
initialize
(sim)¶ Fix the dates
-
apply
(sim)¶ Perform testing
-
class
covasim.interventions.
contact_tracing
(trace_probs=None, trace_time=None, start_day=0, end_day=None, presumptive=False, **kwargs)¶ Bases:
covasim.interventions.Intervention
Contact tracing of positive people.
- Parameters
trace_probs (dict) – probability of tracing, per layer
trace_time (dict) – days required to trace, per layer
start_day (int) – intervention start day
end_day (int) – intervention end day
test_delay (int) – number of days a test result takes
presumptive (bool) – whether or not to begin isolation and contact tracing on the presumption of a positive diagnosis
kwargs (dict) – passed to Intervention()
-
initialize
(sim)¶ Fix the dates and dictionaries
-
apply
(sim)¶ Apply intervention
Function signature matches existing intervention definition This method gets called at each timestep and must be implemented by derived classes
- Parameters
sim – The Sim instance
- Returns
None
-
class
covasim.interventions.
vaccine
(days, prob=1.0, rel_sus=0.0, rel_symp=0.0, subtarget=None, cumulative=False, **kwargs)¶ Bases:
covasim.interventions.Intervention
Apply a vaccine to a subset of the population. In addition to changing the relative susceptibility and the probability of developing symptoms if still infected, this sintervention stores several types of data:
vaccinations
: the number of vaccine doses per personvaccination_dates
: list of dates per personorig_rel_sus
: relative susceptibility per person at the beginning of the simulationorig_symp_prob
: probability of developing symptoms per person at the beginning of the simulationmod_rel_sus
: modifier on default susceptibility due to the vaccinemod_symp_prob
: modifier on default symptom probability due to the vaccine
- Parameters
days (int or array) – the day or array of days to apply the interventions
prob (float) – probability of being vaccinated (i.e., fraction of the population)
rel_sus (float) – relative change in susceptibility; 0 = perfect, 1 = no effect
rel_symp (float) – relative change in symptom probability for people who still get infected; 0 = perfect, 1 = no effect
subtarget (dict) – subtarget intervention to people with particular indices (see test_num() for details)
cumulative (bool) – whether cumulative doses have cumulative effects (default false); can also be an array for efficacy per dose, with the last entry used for multiple doses; thus True = [1] and False = [1,0]
kwargs (dict) – passed to Intervention()
Examples:
interv = cv.vaccine(days=50, prob=0.3, rel_sus=0.5, rel_symp=0.1) interv = cv.vaccine(days=[10,20,30,40], prob=0.8, rel_sus=0.5, cumulative=[1, 0.3, 0.1, 0]) # A vaccine with efficacy up to the 3rd dose
-
initialize
(sim)¶ Fix the dates and store the vaccinations
-
apply
(sim)¶ Perform vaccination