Skip to content

laser.measles.abm.snapshot

laser.measles.abm.snapshot

Snapshot save/load for the laser-measles ABM.

Snapshots capture the full population and patch state at a given point in time and allow the simulation to be resumed exactly from that point.

Typical usage::

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import laser.measles as lm

# --- Segment 1 ---
model1 = lm.ABMModel(scenario, params1)
model1.components = [lm.VitalDynamicsProcess, lm.InfectionProcess]
model1.run()
lm.save_snapshot(model1, "checkpoint.h5")

# --- Segment 2 ---
params2 = lm.ABMParams(start_time="2001-01", num_ticks=365)
model2 = lm.load_snapshot("checkpoint.h5", params2,
                           components=[lm.VitalDynamicsProcess, lm.InfectionProcess])
model2.run()

Notes

  • save_snapshot modifies the model in place: R agents are squashed and vaccination dates are normalized. Do not continue using the model after saving.
  • The caller is responsible for setting params.start_time in the resumed segment to match the snapshot date (printed by load_snapshot).
  • WPPVitalDynamicsProcess is supported with the same guard mechanism.

laser.measles.abm.snapshot.load_snapshot(path, params, components=None, verbose=True)

Load an ABM from an HDF5 snapshot file and return it ready to run.

Restores the population, patch state, scenario, and metadata saved by save_snapshot. Components that modify the people frame (e.g. VitalDynamicsProcess) detect the snapshot context via model._from_snapshot and skip frame setup. Set params.start_time to the snapshot date printed by save_snapshot.

Parameters:

Name Type Description Default
path str | Path

Path to the HDF5 snapshot file written by save_snapshot.

required
params ABMParams

ABMParams for the resumed segment. Set start_time to the snapshot date and num_ticks to the remaining duration.

required
components list | None

Ordered list of component classes to attach — same list as used when building the original model.

None
verbose bool

Print a loading summary.

True

Returns:

Type Description
ABMModel

A configured ABMModel instance. Call model.run() to continue the simulation.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
```python
import laser.measles as lm

params2 = lm.ABMParams(num_ticks=1825, seed=42, start_time="2009-12")
model2 = lm.load_snapshot(
    "checkpoint.h5",
    params2,
    components=[lm.VitalDynamicsProcess, lm.InfectionProcess],
)
model2.run()
```

laser.measles.abm.snapshot.save_snapshot(model, path, squash_recovered=True, verbose=True)

Save ABM state to an HDF5 snapshot file.

Call this after ABMModel.run() to persist the full population and patch state. The resulting HDF5 file can be resumed with load_snapshot to continue the simulation from exactly where it left off — useful for warm-start parameter sweeps, segmented cluster jobs, or reproducible checkpoints.

Warning

This function mutates model: recovered agents are squashed (if squash_recovered=True) and future vaccination dates are normalized. Do not continue running the model after calling this function.

Parameters:

Name Type Description Default
model ABMModel

A fully-run (or mid-run) ABMModel instance.

required
path str | Path

Destination HDF5 file path (created or overwritten).

required
squash_recovered bool

If True (default), remove recovered agents before saving. This dramatically reduces file size for long measles runs.

True
verbose bool

Print a progress summary.

True

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
```python
import laser.measles as lm

params = lm.ABMParams(num_ticks=3650, seed=42, start_time="2000-01")
model = lm.ABMModel(scenario, params)
model.components = [lm.VitalDynamicsProcess, lm.InfectionProcess]
model.run()

lm.save_snapshot(model, "checkpoint.h5")
# Do not use model after this point.
```