model
laser.cholera.metapop.model
¶
Metapopulation model orchestration: Model, RInterface, and the cli_run / run_model entry points.
Contains the runtime glue that turns a parameter set plus an ordered list of component classes into a complete simulation run:
Model— owns the PRNG, thepeople/patchesLaserFrameinstances, and the per-phase timing metrics. Itscomponentssetter instantiates each component against the model, registers__call__-bearing ones as run phases, and runs each component'scheck()before the first tick.RInterface— a thin read-only view object exposing each compartment / patch series trimmed of thet=0slot and transposed into the[location, time]layout the R reference implementation expects.cli_run— the click-decoratedmetapopconsole entry point.run_model— the canonical Python entry point used by tests and bycli_run.
RInterface
¶
A simple interface to store results trimmed and transposed for R.
Source code in src/laser/cholera/metapop/model.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
__init__
¶
Snapshot every per-tick / per-patch series as an R-style trimmed-and-transposed view.
For each available compartment in model.people and each
per-patch property in model.patches, attaches a view onto
self named after the property. Time-series (shape
(nticks + 1, npatches)) are sliced [1:, :] to drop the
t=0 seed row and then transposed to [npatches, nticks].
Dose-count vectors (dose_one_doses, dose_two_doses) are
already nticks-shaped, so they are only transposed.
Per-tick environmental / seasonality matrices (beta_jt_env,
beta_jt_human, delta_jt) are likewise just transposed.
The pi_ij and coupling matrices are passed through
un-transposed (the latter is symmetric).
These are views, not copies — the underlying buffers are the ones the simulation writes into.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The completed (or in-progress) |
required |
Source code in src/laser/cholera/metapop/model.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
Model
¶
Top-level metapopulation simulation object: owns state, the PRNG, and the run loop.
A Model is constructed from a PropertySetEx of parameters; the
caller then assigns a list of component classes to model.components
(which instantiates each, registers __call__-bearing instances as
run phases, and validates prerequisites via each component's
check()). Calling model.run() advances the simulation
nticks times, building up per-phase timing metrics and the R-style
RInterface view on
model.results.
Attributes:
| Name | Type | Description |
|---|---|---|
params |
The full parameter |
|
name |
Human-readable label used in logs and the PDF filename. |
|
scenario |
The MOSAIC scenario |
|
prng |
Seeded LASER core PRNG instance shared by every component. |
|
people |
|
|
patches |
|
|
tinit |
/ tstart / tfinish
|
Wall-clock timestamps for model
construction, run start, and run finish respectively
( |
metrics |
/ tstart / tfinish
|
After |
results |
/ tstart / tfinish
|
After |
Source code in src/laser/cholera/metapop/model.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
components
property
writable
¶
Retrieve the list of model components.
Returns:
list: A list containing the components.
__init__
¶
Construct a fresh model: seed the PRNG and allocate people / patches frames.
Does NOT install components — assign model.components = [...]
after construction to wire up the pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
PropertySet
|
A validated |
required |
name
|
str
|
Display name used in logging and visualization output. |
'Cholera Metapop'
|
Source code in src/laser/cholera/metapop/model.py
run
¶
Execute the model for a specified number of ticks, recording the time taken for each phase.
This method initializes the start time, iterates over the number of ticks specified in the model parameters, and for each tick, it executes each phase of the model while recording the time taken for each phase.
The metrics for each tick are stored in a list. After completing all ticks, it records the finish time and, logs a summary of the timing metrics.
Attributes:
tstart (datetime): The start time of the model execution.
tfinish (datetime): The finish time of the model execution.
metrics (list): A list of timing metrics for each tick and phase.
Returns:
None
Source code in src/laser/cholera/metapop/model.py
visualize
¶
Visualize each compoonent instances either by displaying plots or saving them to a PDF file.
Parameters:
pdf (bool): If True, save the plots to a PDF file. If False, display the plots interactively. Default is True.
Returns:
None
Source code in src/laser/cholera/metapop/model.py
plot
¶
Yield two top-level scenario figures: patch map and per-phase timing pie.
The first figure overlays a scatter of patch centroids
(sized / colored by population) on the scenario GeoDataFrame
polygons. The second figure pie-charts the total microseconds
spent in each run phase across the entire simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Optional existing Matplotlib |
None
|
Yields:
| Type | Description |
|---|---|
str
|
Two labels in order: |
str
|
|
str
|
|
str
|
formatted total). |
Source code in src/laser/cholera/metapop/model.py
cli_run
¶
Run the cholera model simulation with the given parameters.
Initializes the model, sets up the default component pipeline, seeds initial infections, runs the simulation, and optionally renders visualizations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Path | None
|
Path to a parameters JSON file (or |
required |
**kwargs
|
object
|
Click-bound options forwarded to
|
{}
|
Raises:
| Type | Description |
|---|---|
UsageError
|
When |
ValueError
|
When |
Source code in src/laser/cholera/metapop/model.py
run_model
¶
Build and run the default cholera metapopulation simulation.
The canonical Python entry point. Loads parameters via
get_parameters,
constructs a Model wired with
the full default component pipeline (Susceptible → Exposed →
Recovered → Infectious → Vaccinated → Census → HumanToHuman
→ EnvToHuman → Environmental → DerivedValues → Analyzer →
Recorder → Parameters), runs the simulation to completion, and
optionally renders visualizations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paramfile
|
str | Path | dict | None
|
Parameter source — anything
|
required |
**kwargs
|
dict | None
|
Forwarded as |
{}
|
Returns:
| Type | Description |
|---|---|
Model
|
The completed |
Model
|
per-tick state populated on |
Model
|
and the final-tick log-likelihood (when |
Model
|
in params) on |
Model
|
or |
Model
|
the rendered output path. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
Run with defaults and inspect the final-tick susceptible counts:
from laser.cholera.metapop.model import run_model # doctest: +SKIP model = run_model(None) # doctest: +SKIP model.people.S[-1].sum() > 0 # doctest: +SKIP True