Intervention#
- 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().
- 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 in the legend
do_plot (bool) – whether or not to plot the intervention
line_args (dict) – arguments passed to pl.axvline() when plotting
Methods
- initialize(sim=None)[source]#
Initialize intervention – this is used to make modifications to the intervention that can’t be done until after the sim is created.
- finalize(sim=None)[source]#
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(sim)[source]#
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.
- Parameters:
sim – the Sim instance
- Returns:
None
- shrink(in_place=False)[source]#
Remove any excess stored data from the intervention; for use with sim.shrink().
- Parameters:
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.
- Parameters:
sim – the Sim instance
ax – the axis instance
kwargs – passed to ax.axvline()
- Returns:
None
- to_json()[source]#
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.
- Returns:
JSON-serializable representation (typically a dict, but could be anything else)