poliosim.interventions module

Specify the core interventions available in Poliosim. Other interventions can be defined by the user by inheriting from these classes.

InterventionDict(which, pars)[source]

Generate an intervention from a dictionary. Although a function, it acts like a class, since it returns a class instance.


interv = ps.InterventionDict(which='change_beta', pars={'days': 30, 'changes': 0.5, 'layers': None})
class Intervention(label=None, show_label=False, do_plot=None, line_args=None)[source]

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.

To retrieve a particular intervention from a sim, use sim.get_intervention().

  • 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 in the legend

  • do_plot (bool) – whether or not to plot the intervention

  • line_args (dict) – arguments passed to pl.axvline() when plotting


Print a detailed representation of the intervention


Initialize intervention – this is used to make modifications to the intervention that can’t be done until after the sim is created.


Finalize intervention

This method is run once as part of sim.finalize() enabling the intervention to perform any final operations after the simulation is complete (e.g. rescaling)


Apply the intervention. This is the core method which each derived intervention class must implement. This method gets called at each timestep and can make arbitrary changes to the Sim object, as well as storing or modifying the state of the intervention.


sim – the Sim instance




Remove any excess stored data from the intervention; for use with sim.shrink().


in_place (bool) – whether to shrink the intervention (else shrink a copy)

plot_intervention(sim, ax=None, **kwargs)[source]

Plot the intervention

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.

Note 1: you can modify the plotting style via the line_args argument when creating the intervention.

Note 2: By default, the intervention is plotted at the days stored in self.days. However, if there is a self.plot_days attribute, this will be used instead.

  • sim – the Sim instance

  • ax – the axis instance

  • kwargs – passed to ax.axvline()




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.

Note that simply printing an intervention will usually return a representation that can be used to recreate it.


JSON-serializable representation (typically a dict, but could be anything else)

class change_beta(days, changes, layers=None, **kwargs)[source]

Bases: poliosim.interventions.Intervention

The most basic intervention – change beta by a certain amount.

  • 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()


interv = ps.change_beta(25, 0.3) # On day 25, reduce overall beta by 70% to 0.3
interv = ps.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

Fix days and store beta

class test_prob(symp_prob, asymp_prob=0.0, symp_quar_prob=None, asymp_quar_prob=None, quar_policy=None, subtarget=None, test_sensitivity=1.0, loss_prob=0.0, test_delay=0, start_day=0, end_day=None, **kwargs)[source]

Bases: poliosim.interventions.Intervention

Test as many people as required based on test probability. Probabilities are OR together, so choose wisely.

  • 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

  • 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()


interv = ps.test_prob(symp_prob=0.1, asymp_prob=0.01) # Test 10% of symptomatics and 1% of asymptomatics
interv = ps.test_prob(symp_quar_prob=0.4) # Test 40% of those in quarantine with symptoms

Fix the dates


Perform testing

class contact_tracing(trace_probs=None, trace_time=None, start_day=0, end_day=None, presumptive=False, **kwargs)[source]

Bases: poliosim.interventions.Intervention

Contact tracing of positive people.

  • 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()


Fix the dates and dictionaries

class symptomatic_triggered_surveillance(num_sample_mu=10.0, num_sample_sigma=0.0, test_sensitivity=1.0, test_delay=0, **kwargs)[source]

Bases: poliosim.interventions.test_prob

Trigger surveillance whenever one or more newly symptomatic cases occur in a timestep. Surveillance is implemented as randomly choosing a set of individuals in the population, as configured by parameter. The other parameters associated with test_priob are also available.