Campaign and interventions
A Campaign is a model component that reads a schedule of interventions and dispatches them at the right ticks, nodes, and compartment states. It is added to model.components alongside the epidemiological components.
Quick example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Schedule entry fields
Each entry is a dict with six fields:
| Field | Required? | Type | Meaning |
|---|---|---|---|
who |
required | "*" or list[str] |
Compartment states to target. "*" means all states; ["S", "R"] restricts to those two. |
what |
required | str |
Name of a registered Intervention subclass. |
when |
optional (default "*") |
see below | When to fire. |
where |
required | "*", int, or list[int] |
Node IDs to target. "*" means all nodes. |
parameters |
optional (default {}) |
dict |
Arbitrary key/value pairs forwarded to execute(). |
notes |
optional (default "") |
str |
Free-text annotation forwarded to execute(). |
Note
who and where are required for every entry — omitting them raises
ValueError. Use "*" explicitly to target all states or all nodes;
the Campaign deliberately does not silently default these fields.
when variants
| Value | Behaviour |
|---|---|
"*" |
Fires on every tick. |
30 |
Fires once on tick 30. |
[30, 60, 90] |
Fires once on each listed tick. |
"2020-03-15" |
Fires on the tick corresponding to that date; requires start_date. |
["2020-03-15", "2020-06-01"] |
Fires once on each listed date; requires start_date. |
Integer ticks and date strings cannot be mixed in the same schedule — neither across entries nor within a single list. Dates earlier than start_date raise ValueError.
1 2 | |
Out-of-range ticks (beyond params.nticks) are silently skipped — the model simply never reaches them.
Loading sources
Campaign accepts four source formats:
1 2 3 | |
1 2 3 4 5 | |
1 2 3 4 5 6 | |
1 | |
1 2 3 | |
1 | |
In CSV, list-valued fields (who, where, when) are JSON-encoded strings.
parameters is a JSON object string.
Built-in interventions
Vaccination
Moves a binomial-drawn fraction of each targeted state into a dedicated V (vaccinated) compartment. Declares the V state and a newly_vaccinated node property, so the model allocates both automatically.
1 2 3 4 5 6 7 8 | |
Parameters:
| Key | Type | Default | Meaning |
|---|---|---|---|
coverage |
float |
0.0 |
Probability in [0, 1] that any targeted individual is vaccinated this tick. |
coverage must be in [0, 1]; values outside this range raise ValueError at the tick when the intervention fires.
Compartment transitions:
1 | |
After the campaign fires, model.nodes.newly_vaccinated[tick] holds the per-node count of newly vaccinated individuals.
Writing a custom intervention
Subclass Intervention and implement execute(). Override states and/or properties if your intervention needs new compartments or output arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Register the class before assigning model.components:
1 2 3 4 5 6 7 8 | |
Declaring new states
If your intervention creates a new compartment, declare it via states:
1 2 3 4 5 6 7 | |
Declaring new node properties
If your intervention records per-tick output, declare it via properties:
1 2 3 4 5 6 7 8 9 10 11 | |
Campaign.states and Campaign.properties automatically aggregate these declarations from all interventions referenced in the schedule, so the model allocates everything before setup() runs.
Registration
Intervention classes must be registered before model.components is assigned. Registration uses the class __name__ as the key; that name must match the what field in the schedule.
1 2 | |
Registration is class-level and persistent for the lifetime of the Python process. If you run multiple models in the same session you only need to register once.
An unregistered name in the schedule raises KeyError when the scheduled tick is reached during model.run().