Skip to content

laser.measles.components.results_writer

laser.measles.components.results_writer

ResultsWriter component — opt-in JSON results dump at end of run.

Adding ResultsWriter to model.components produces the canonical JSON summary file at end of run. The component owns both the dict- building logic and the file write, so nothing on the model class needs to know about results output. Calibration loops and other contexts that don't want a per-run JSON file on disk simply omit this component.

laser.measles.components.results_writer.ResultsWriter(model, params=None)

Bases: BaseComponent

Write standard JSON results at end of run.

Requires a StateTracker somewhere in model.components — that's where the per-tick state arrays come from.

Schema (top-level keys): model_type: class name of the model (e.g. "ABMModel") num_ticks: int num_groups: int (group count from the tracker; 1 if global) group_ids: list[str] (matches tracker.group_ids; may be scenario row IDs at leaf aggregation, or higher-level keys like "cluster_1" when aggregation_level rolls up) group_aggregation_level: int (the tracker's aggregation_level: -1 means global, 0+ means grouped at that hierarchy depth — consumers branch on this to know whether the _per_group arrays are at patch granularity or rolled up) states: list[str] (e.g. ["S","E","I","R"]) summary: peak_infectious_global: int peak_tick: int (tick index of global peak; consumers convert to calendar time using the model's tick→day mapping) attack_rate_global: float | None (fraction of initial susceptibles globally that left the S compartment; in [0, 1]; null when S isn't in model.params.states) attack_rate_per_group: list[float] | None (per-group version of attack_rate_global; in [0, 1] per entry) final_state_global: dict[str, int] peak_infectious_per_group: list[int] | None final_state_per_group: dict[str, list[int]] | None

Example::

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from laser.measles.components import ResultsWriter, ResultsWriterParams
from laser.measles.components import create_component

model.components = [
    ...
    StateTracker,                         # collects time-series
    ResultsWriter,                        # writes results.json at end
    # or with a custom path:
    create_component(
        ResultsWriter,
        params=ResultsWriterParams(path="run_42.json"),
    ),
]

laser.measles.components.results_writer.ResultsWriter.finalize(model)

Called by BaseLaserModel.run() after the tick loop completes.

laser.measles.components.results_writer.ResultsWriterParams

Bases: BaseModel

Parameters for ResultsWriter.

Attributes:

Name Type Description
path str

Destination file. Defaults to "results.json" in cwd.