Model API¶
The validated model that ModelBuilder.build returns: a header, the process
written once, the structure that lays it out (Aggregate, with its neighbors
Complete and Groups), the entries of Aggregate.initial_state(), the
SeriesValue of a series parameter, and UnitConsistencyError, the error of
Model.check_unit_consistency.
Guide: Core Concepts,
Building Models,
Describing a Model,
Units. The tree of types is in
Model Structure.
Model ¶
Bases: BaseModel, Generic[StructureT]
A model of any kind: a header, a process, a structure and an engine.
The process (bins, stratifications, transitions) is written once; the
structure lays it out and changes between kinds of model, so
Model[Aggregate] is a model of amounts per compartment.
ModelBuilder.build joins its process with a structure. A model loaded
without a structure class reads the class from the kind and the
variant of its structure.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
A unique name that identifies the model. |
description |
str | None
|
A human-readable description of the model's purpose and function. |
version |
str | None
|
The version number of the model. |
parameters |
list[Parameter]
|
A list of global model parameters. |
engine |
ModelEngine
|
The engine that runs the model, and its settings. |
process |
Process
|
What changes and how: the bins, accumulators, stratifications and transitions, written once for every structure. |
structure |
StructureT
|
How the process is laid out, which changes between kinds of model,
such as the amounts per compartment of an |
step_unit |
str | None
|
Unit of one step on the axis, of any single dimension ( |
origin |
datetime | None
|
Calendar moment of position 0, with no time zone, for the dates of
|
Source code in commol/context/model.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 169 170 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
Methods:¶
check_origin_unit ¶
Check that an origin has a step unit of fixed calendar length.
read_structure_kind
classmethod
¶
read_structure_kind(value: JsonValue) -> JsonValue | ModelStructure
Read a structure mapping as the class of its kind and variant.
Source code in commol/context/model.py
from_json
classmethod
¶
Loads a model from a JSON file.
The method reads the specified JSON file as UTF-8, the encoding of JSON, parses its content, and validates it against the Model schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
The path to the JSON file. |
required |
Returns:
| Type | Description |
|---|---|
Model
|
A validated Model instance. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file at |
ValidationError
|
If the JSON content does not conform to the Model schema. |
Source code in commol/context/model.py
to_json ¶
Save the model to a JSON file.
Every field is written, including those left unset, so the file always
reloads through :meth:from_json to an equal model.
Long numeric arrays, such as a series parameter, are written on a single line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path of the file to write. |
required |
indent
|
int
|
Indentation width for nested structures. |
2
|
Source code in commol/context/model.py
validate_engine_runs_structure ¶
Ask the engine whether it runs the kind of the structure.
validate_unique_parameter_ids ¶
Validates that parameter IDs are unique.
Source code in commol/context/model.py
validate_structure ¶
update_parameters ¶
Update parameter values in the model.
The model takes every value or none: the values pass the checks of
Parameter and of the model, as when the model is made, before any is
written.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameter_values
|
Mapping[str, ParameterUpdate]
|
Each parameter id mapped to its new value: a number, a formula, a
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an id is not a parameter of the model, a list is given for a parameter that is not a series, a value is not valid (the message names the parameter), or the model rejects the new values. |
Source code in commol/context/model.py
get_uncalibrated_parameters ¶
Get a list of parameter IDs that have None values (need calibration).
Returns:
| Type | Description |
|---|---|
list[str]
|
List of parameter IDs that require calibration. |
Source code in commol/context/model.py
apply_calibration_parameters ¶
apply_calibration_parameters(result: CalibrationResult | Mapping[str, float], problem: CalibrationProblem) -> None
Write calibrated values back onto the model.
Each value is routed by the parameter_type declared for it in
problem: parameter values update model parameters, factor
and noise values are ignored because they apply to observations
rather than to the model, and the structure writes back the values of its kind
(ModelStructure.with_calibrated_values). An aggregate structure
writes initial_condition values so that the model rebuilds the
initial state the engine scored for them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
CalibrationResult | Mapping
|
The calibration result, or a mapping of parameter id to value such
as one draw of a |
required |
problem
|
CalibrationProblem
|
The problem the result came from, used to resolve parameter types. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an id is not declared in |
TypeError
|
If |
Source code in commol/context/model.py
applied_parameters ¶
applied_parameters(values: CalibrationResult | Mapping[str, float], problem: CalibrationProblem | None = None, sample: ParameterSample | None = None, confidence_level: float | None = None) -> AppliedParametersReport
Report every parameter of the model, and the values of its kind.
Call it on the model after apply_calibration_parameters. Each
entry carries the value the model holds and whether calibration set
it, so entries the calibration left alone appear alongside the fitted
ones. The structure of the model adds the sections of its kind
(ModelStructure.report_sections); an aggregate model adds
initial_amounts and category_splits.
Parameters that are not a single number, such as a formula or a
series, are omitted: they are inputs rather than fitted values.
factor and noise parameters have no place in the model, so
they are reported from problem when it is given.
initial_amounts gives each bin and each overridden compartment of
an aggregate model its amount the engine builds and the unit
of its bin, so bins of different units are reported side by side; no
row is a share of a total. A bin left uncalibrated keeps a None
amount as its "needs calibration" marker even when overrides touch
some of its compartments. category_splits gives each category its
fraction of the group it splits, and that group: the
categories a conditional stratification is conditioned on, empty when
it splits every bin.
With a sample each entry gains an interval holding the weighted
statistics of its draws (ParameterSample.summary). Every entry is in
the unit it was calibrated in: a parameter in its own, an amount in
the unit of its bin, and a category split as a fraction of its group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
CalibrationResult | Mapping
|
The calibration result, or a mapping of parameter id to value such as one draw of a sample. |
required |
problem
|
CalibrationProblem | None
|
The problem the values came from, used to report factor and noise parameters. |
None
|
sample
|
ParameterSample | None
|
Draws of the parameters, such as the sample of an uncertainty method. |
None
|
confidence_level
|
float | None
|
Confidence level of the intervals; required with a sample. |
None
|
Returns:
| Type | Description |
|---|---|
AppliedParametersReport
|
Key |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a sample is given without a confidence level. |
Source code in commol/context/model.py
describe ¶
describe(layout: Layout = 'default') -> ModelDescription
A description of the model, as data.
The description is a list of sections of typed blocks: tables,
equations, state graphs and text. ModelDescription.render turns it
into text, Markdown, LaTeX or JSON, and ModelDescription.save writes
it to a file; in a notebook it shows as Markdown, with the state graph
of the model drawn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
(default, odd)
|
|
"default"
|
Returns:
| Type | Description |
|---|---|
ModelDescription
|
The description. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the layout is not one of these. |
Examples:
>>> description = model.describe()
>>> print(description.render("text"))
>>> description.save("model.md")
>>> model.describe(layout="odd").save("model.tex")
Source code in commol/context/model.py
axis_units ¶
axis_units() -> AxisUnits
The unit of one step of the model, and its engine, to convert values.
The step unit is step_unit or, when the model declares none,
step, the default increment of any axis. The origin is
origin, for the dates of the axis. The engine is the one of the
model, so the rates of AxisUnits mean what this engine reads.
Returns:
| Type | Description |
|---|---|
AxisUnits
|
The converter of the model. |
Examples:
>>> units = model.axis_units()
>>> units.steps(2, "week") # with step_unit="day"
14.0
>>> gamma = units.rate_from_mean_span(5, "day")
Source code in commol/context/model.py
check_unit_consistency ¶
Check unit consistency of all equations in the model.
The structure checks them (ModelStructure.check_unit_consistency).
For an aggregate model, the engine converts no value between units.
So the check uses one step unit, step_unit or else step, and
every flow must be exactly <bin unit>/<step unit>, such as
item/day. The unit is the same for every engine; what the value of
a flow means is the engine's.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
bool
|
If True, prints a success message when all units are consistent. |
False
|
Raises:
| Type | Description |
|---|---|
UnitConsistencyError
|
If a constant parameter or a bin has no unit, if |
ValueError
|
If the model type doesn't support unit checking. |
NotImplementedError
|
If the kind of the model has no unit check. |
Notes
- Predefined variables (total, total_g1, etc.) have the bin unit
t, a position on the axis, has the step unit; a number added to, subtracted from or compared with it is read in that unitstep, a count of increments, is dimensionless- Mathematical constants (pi, e) are dimensionless
Source code in commol/context/model.py
Process ¶
Bases: BaseModel
What changes in a model and how, written once for every structure.
Attributes:
| Name | Type | Description |
|---|---|---|
bins |
list[Bin]
|
The states of the process. Stratifications expand each one into compartments. |
accumulators |
list[Accumulator]
|
Counters of the flow of the transitions that name them. |
stratifications |
list[Stratification]
|
Categorical subdivisions of the bins, in expansion order. |
transitions |
list[Transition]
|
Flows between bins, or between the categories of one stratification. |
Source code in commol/context/process.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 | |
Methods:¶
validate_unique_ids ¶
Validate that bin, stratification and transition ids are unique.
Source code in commol/context/process.py
validate_stratified_rates ¶
Validate that stratified rates name stratifications and categories.
Source code in commol/context/process.py
validate_transition_ids ¶
Check that each transition moves between bins, or between the categories of one stratification.
Source code in commol/context/process.py
is_empty ¶
Whether the process holds nothing.
Returns:
| Type | Description |
|---|---|
bool
|
True when it has no bin, accumulator, stratification or transition. |
Source code in commol/context/process.py
ModelStructure ¶
Bases: BaseModel, ABC
How the process of a model is laid out: the part that depends on its kind.
Each kind subclasses it and sets kind to a Literal default, its
name in the engine's list of kinds. A variant subclasses the structure of
its kind and sets variant to a Literal default. A subclass that
does not implement an abstract method cannot be made: the error names
the method.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
str
|
Name of the model kind, the tag of the structure in JSON. |
variant |
str | None
|
Name of the variant, or None for the structure of the kind itself. |
Source code in commol/context/structure.py
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 169 170 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 | |
Methods:¶
__pydantic_init_subclass__
classmethod
¶
Register a concrete subclass under its kind and variant.
Source code in commol/context/structure.py
of_kind
classmethod
¶
of_kind(kind: str, variant: str | None = None) -> type[ModelStructure]
The structure class of a model kind, or of one of its variants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
str
|
Name of the kind, such as |
required |
variant
|
str | None
|
Name of a variant of the kind, or None for the kind itself. |
None
|
Returns:
| Type | Description |
|---|---|
type[ModelStructure]
|
The subclass registered for them. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no structure class has that kind and variant. |
Source code in commol/context/structure.py
check_model
abstractmethod
¶
check_model(model: Model[Self]) -> None
Check the structure against the rest of its model.
The model calls it when it is made, so a model that cannot be run as written is rejected then.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds this structure. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the structure does not agree with the parameters or the engine. |
Source code in commol/context/structure.py
outputs_by_source
abstractmethod
¶
outputs_by_source(model: Model[Self]) -> dict[str, list[str]]
The output names of the model, grouped by the id they come from.
Reports and plots name a group of outputs by this id and sum them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds this structure. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
Each id mapped to its output names, in engine order. |
Source code in commol/context/structure.py
series_label ¶
series_label(model: Model[Self], series_id: str) -> SeriesLabel
How a plot names a series and its unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds this structure. |
required |
series_id
|
str
|
An output name or an id of |
required |
Returns:
| Type | Description |
|---|---|
SeriesLabel
|
The id itself and no unit, unless the kind knows better. |
Source code in commol/context/structure.py
calibration_value_types ¶
Types of value of this kind that calibration may set.
parameter and factor are not here: every kind accepts them.
The engine's list of kinds declares the others.
Returns:
| Type | Description |
|---|---|
frozenset[CalibrationParameterType]
|
For example |
Source code in commol/context/structure.py
uncalibrated_values ¶
Ids of the values of this kind that need calibration before a run.
Returns:
| Type | Description |
|---|---|
list[str]
|
Empty unless the kind has values left unset. |
check_calibration_values ¶
check_calibration_values(model: Model[Self], engine: EngineProtocol, parameters: Sequence[CalibrationParameter], remainder: str | None) -> None
Check the values of this kind that a calibration or a sensitivity analysis sets.
The engine has resolved each value and the remainder when this runs; a kind checks here what only the Python definition knows, such as the units of the bins. The default checks nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model the values are set on. |
required |
engine
|
EngineProtocol
|
The engine built from it. |
required |
parameters
|
Sequence[CalibrationParameter]
|
The values set, with their types and bounds. |
required |
remainder
|
str | None
|
The id that takes what the initial conditions leave of the sum the kind keeps, if any. |
required |
Source code in commol/context/structure.py
with_calibrated_values ¶
with_calibrated_values(model: Model[Self], problem: CalibrationProblem, values: Mapping[str, float]) -> Self
The structure with the calibrated values of its kind written back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model the problem was built on. It is not changed. |
required |
problem
|
CalibrationProblem
|
The problem, for the types of its parameters. |
required |
values
|
Mapping[str, float]
|
Calibrated value per parameter id. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A structure; this one when the kind sets no values of its own. |
Source code in commol/context/structure.py
report_sections ¶
report_sections(model: Model[Self], calibrated_ids: set[str], parameter_statistics: Mapping[str, ParameterSummary] | None) -> ReportSections
Sections of this kind in a report of applied values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds the applied values. |
required |
calibrated_ids
|
set[str]
|
Ids that calibration set. |
required |
parameter_statistics
|
Mapping[str, ParameterSummary] | None
|
Statistics of each id from a sample, or None without one. |
required |
Returns:
| Type | Description |
|---|---|
ReportSections
|
Each section by its key in the report; none by default. |
Source code in commol/context/structure.py
describe ¶
The sections of a description that this kind fills.
Model.describe adds them to the sections every kind shares
(overview, schedule, parameters, outputs). A kind fills the sections
it knows, by key: entities (what holds the state), dynamics
(how it changes), initialization (the state it starts from) and
design_concepts (a Table of elements of the ODD protocol and
their text). A section with no blocks is left out of a rendering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds this structure. |
required |
Returns:
| Type | Description |
|---|---|
list[Section]
|
The sections of the kind; none by default. |
Source code in commol/context/structure.py
Aggregate ¶
Bases: ModelStructure, InitialConditions
Amounts per compartment: how much starts where, and who the neighbors of a unit are.
The bins of the process expand over its stratifications into
compartments. bin_amounts gives the amount of each bin at the start,
in the unit of the bin, stratification_fractions splits it among the
categories, and compartment_amounts overrides single compartments.
Bins of different units, such as a count and a concentration, need no
total over them.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['aggregate']
|
The model kind. |
variant |
str | None
|
The variant of the structure (see |
bin_amounts |
dict[str, float | None]
|
The amount of each bin at the start, by bin id, in the unit of the
bin, such as |
stratification_fractions |
dict[str, dict[str, float]]
|
The fraction of each category, by stratification id, such as
|
compartment_amounts |
dict[str, float]
|
Amounts of single expanded compartments, such as |
neighbors |
Complete | Groups | None
|
Who the neighbors of a unit are, for the rate variables
|
Source code in commol/context/aggregate.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 169 170 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
Methods:¶
check_model ¶
check_model(model: Model[Self]) -> None
Check the structure against the process and the parameters.
The process needs a bin and a transition, the amounts must name every bin and the fractions every category of the process, and the engine must plan every transition, accept every id (no parameter id is an engine variable, a bin, a compartment or a subgroup total), resolve the neighbors, find every variable a rate or a formula parameter reads, and build the initial state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds this structure. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If one of these checks fails. |
Source code in commol/context/aggregate.py
outputs_by_source ¶
outputs_by_source(model: Model[Self]) -> dict[str, list[str]]
Map each bin and accumulator id to the output names it expands into.
A bin or accumulator produces one output per combination of the stratification categories that apply to it. Without stratifications it produces a single output named after the id itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds this structure. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
Bin and accumulator ids, each mapped to its output names in declaration order. |
Source code in commol/context/aggregate.py
series_label ¶
series_label(model: Model[Self], series_id: str) -> SeriesLabel
The name and unit of a bin, for a plot of its series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds this structure. |
required |
series_id
|
str
|
A bin id, or any other series id. |
required |
Returns:
| Type | Description |
|---|---|
SeriesLabel
|
The bin's name and unit, or the id and no unit when no bin has it. |
Source code in commol/context/aggregate.py
uncalibrated_values ¶
Ids of the bins whose initial amount needs calibration.
Returns:
| Type | Description |
|---|---|
list[str]
|
Bin ids with a None amount. |
check_calibration_values ¶
check_calibration_values(model: Model[Self], engine: EngineProtocol, parameters: Sequence[CalibrationParameter], remainder: str | None) -> None
Check that the remainder has the unit of every amount it gives.
The remainder takes what the amounts set leave of the sum it keeps, so a bin, prefix or compartment value must be in the unit of the remainder; a category split has no unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model the values are set on. |
required |
engine
|
EngineProtocol
|
The engine built from it. |
required |
parameters
|
Sequence[CalibrationParameter]
|
The values set, with their types and bounds. |
required |
remainder
|
str | None
|
The bin or compartment that takes what the initial conditions leave, if any. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an amount set is in another unit than the remainder. |
Source code in commol/context/aggregate.py
with_calibrated_values ¶
with_calibrated_values(model: Model[Self], problem: CalibrationProblem, values: Mapping[str, float]) -> Self
The structure with calibrated initial conditions written back.
The written structure rebuilds the initial state the engine scored. Bin values update the amounts they declare, in the unit of the bin, and category values the splits. Every compartment those do not reproduce, such as an exact compartment value or a value that overlaps a per-compartment override, is written as a per-compartment override.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model the problem was built on. It is not changed. |
required |
problem
|
CalibrationProblem
|
The problem, for its parameters and remainder. |
required |
values
|
Mapping[str, float]
|
Calibrated value per parameter id. |
required |
Returns:
| Type | Description |
|---|---|
Aggregate
|
A copy that holds the calibrated initial conditions. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the engine cannot resolve an initial-condition id or the remainder, or the values leave the remainder below zero. |
Source code in commol/context/aggregate.py
report_sections ¶
report_sections(model: Model[Self], calibrated_ids: set[str], parameter_statistics: Mapping[str, ParameterSummary] | None) -> ReportSections
The initial_amounts and category_splits sections of a report.
See Model.applied_parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds the applied values. |
required |
calibrated_ids
|
set[str]
|
Ids that calibration set. |
required |
parameter_statistics
|
Mapping[str, ParameterSummary] | None
|
Statistics of each id from a sample, or None without one. |
required |
Returns:
| Type | Description |
|---|---|
ReportSections
|
|
Source code in commol/context/aggregate.py
describe ¶
The entities, dynamics, initialization and design concepts.
entities: the bins, accumulators, stratifications and expanded
compartments. dynamics: the state graphs, the compact and expanded
forms of the equations the engine simulates, the impulses and the
neighbors. initialization: the bin amounts, the category splits,
the overrides and the initial state the engine builds. design_concepts: the
interaction of the units, when the structure says who their neighbors
are.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model that holds this structure. |
required |
Returns:
| Type | Description |
|---|---|
list[Section]
|
The sections of an aggregate model. |
Source code in commol/context/aggregate.py
check_unit_consistency ¶
check_unit_consistency(model: Model[Self], verbose: bool) -> None
Check the units of the model; see Model.check_unit_consistency.
Source code in commol/context/aggregate.py
update_compartment_amounts ¶
update_compartment_amounts(process: Process, amounts: Mapping[str, float]) -> None
Upsert per-compartment initial condition overrides.
A compartment already declared has its amount replaced; a new one is appended. The engine validates the result against the expansion of the process before it is stored, so a rejected call leaves the structure unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Process
|
The process of the model, such as |
required |
amounts
|
Mapping[str, float]
|
Dictionary mapping expanded compartment names to their new amount, in the unit of their bin. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a name is not an expanded compartment of the model, or an amount is not finite or is negative. |
Source code in commol/context/aggregate.py
initial_state ¶
initial_state(process: Process) -> list[InitialCompartment]
Expanded compartments and the amount each one starts with.
The engine builds this state from the bin amounts, the stratification fractions and the per-compartment overrides; it is the state a simulation starts from.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Process
|
The process of the model, such as |
required |
Returns:
| Type | Description |
|---|---|
list[InitialCompartment]
|
One entry per expanded compartment, in engine order. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an amount is not valid or an override names no compartment of the structure. |
Source code in commol/context/aggregate.py
get_conditioning_categories ¶
get_conditioning_categories(process: Process, stratification_id: str) -> tuple[str, ...]
Get the categories a stratification is conditioned on.
An unconditional stratification subdivides the whole model and has no conditioning categories. A conditional one subdivides only the subgroup its conditions select, and its fractions are relative to that subgroup rather than to the whole model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Process
|
The process of the model, such as |
required |
stratification_id
|
str
|
Id of a stratification of the process. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
Ids of the categories selecting the subgroup this stratification
subdivides, empty for an unconditional stratification. A category
name another stratification also declares is qualified, as in
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the stratification is not found. |
Source code in commol/context/aggregate.py
subgroup_total ¶
subgroup_total(process: Process, categories: Iterable[str] = ()) -> float
Get the initial sum of the compartments of a subgroup.
The sum is taken of the state the engine builds, overrides included, over every compartment that has each of the categories. Passing no categories gives the sum of every compartment. It adds every bin it covers, so over bins of different units it has no unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Process
|
The process of the model, such as |
required |
categories
|
Iterable[str]
|
Category ids, bare or qualified ( |
()
|
Returns:
| Type | Description |
|---|---|
float
|
The sum of the compartments of the subgroup at step 0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a category is not found or a bare name is ambiguous. |
Source code in commol/context/aggregate.py
Complete ¶
Bases: BaseModel
Every unit is a neighbor of every other.
neighbors_<bin> is the bin total and neighbors is total, so
k1 * neighbors_B / neighbors from A moves k1 * A * B / total.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['complete']
|
The tag of the neighbors in JSON. |
Source code in commol/context/neighbors.py
Groups ¶
Bases: BaseModel
Units are neighbors by the categories of one stratification.
In a compartment of category g, neighbors_<bin> is
Σ_g' w[g][g'] × (amount of the bin in g') and neighbors is
Σ_g' w[g][g'] × (amount of every bin in g').
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['groups']
|
The tag of the neighbors in JSON. |
stratification |
str
|
Id of the stratification whose categories group the units. |
weights |
dict[str, dict[str, float]] | None
|
|
Source code in commol/context/neighbors.py
Methods:¶
__init__ ¶
__init__(stratification: str, weights: Mapping[str, Mapping[str, float]] | None = None, *, type: Literal['groups'] = 'groups') -> None
Group the neighbors of a unit by the categories of a stratification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stratification
|
str
|
Id of the stratification whose categories group the units. |
required |
weights
|
Mapping[str, Mapping[str, float]] | None
|
|
None
|
type
|
groups
|
The kind of neighbors, as JSON holds it. |
"groups"
|
Source code in commol/context/neighbors.py
InitialCompartment ¶
Bases: NamedTuple
One expanded compartment and the amount it starts with.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Expanded compartment name, for example |
bin |
str
|
Bin the compartment expands from. |
categories |
dict[str, str]
|
Category applied to the compartment, keyed by stratification id. A conditional stratification whose conditions the compartment does not meet is absent. |
amount |
float
|
Initial amount, including per-compartment overrides. |
Source code in commol/context/_model/engine_state.py
SeriesValue ¶
Bases: BaseModel
An empirical series for a parameter value.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
list[tuple[float, float]]
|
(position, value) points on the axis. Need not be pre-sorted; the engine sorts them. |
mode |
str
|
How the series is read between its points: |
Source code in commol/context/parameter.py
Methods:¶
validate_data
classmethod
¶
Check the points with the rule of the engine (check_series of
crates/commol-model/src/values.rs): at least one point, each
position finite, not negative and once, each value finite.
Source code in commol/context/parameter.py
UnitConsistencyError ¶
Descriptions¶
Model.describe returns a ModelDescription; its blocks are in
commol.context.description.
ModelDescription ¶
Bases: BaseModel
A description of a model, as data.
Model.describe builds it. render turns it into a string, save writes
it to a file, and a notebook shows its Markdown rendering.
Attributes:
| Name | Type | Description |
|---|---|---|
title |
str
|
The title, the name of the model. |
layout |
str
|
The layout the sections follow: |
sections |
list[Section]
|
The sections, in order. |
Source code in commol/context/description.py
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 | |
Methods:¶
section ¶
section(key: str) -> Section
The section with a key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key of the section, such as |
required |
Returns:
| Type | Description |
|---|---|
Section
|
The section. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the description has no section with that key; the message lists the keys. |
Source code in commol/context/description.py
graphs ¶
graphs() -> list[StateGraph]
Every state graph of the description, in order.
Returns:
| Type | Description |
|---|---|
list[StateGraph]
|
The graphs, the expanded ones included. |
Source code in commol/context/description.py
render ¶
The description as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
(text, markdown, latex, json, dot)
|
|
"text"
|
Returns:
| Type | Description |
|---|---|
str
|
The rendering. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is not one of these. |
Source code in commol/context/description.py
save ¶
Write the description to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The file to write. |
required |
format
|
(text, markdown, latex, json, dot)
|
The format; None takes it from the suffix of |
"text"
|
Returns:
| Type | Description |
|---|---|
Path
|
The path written. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the format is None and the suffix is not one of these, or the format is not valid. |
Source code in commol/context/description.py
Section ¶
Bases: BaseModel
One section of a description.
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
The key of the section, such as |
title |
str
|
The title a rendering shows. |
blocks |
list[Block]
|
The blocks, in order. A section with no blocks is left out of every rendering. |
Source code in commol/context/description.py
Text ¶
Bases: BaseModel
A paragraph.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['text']
|
The tag of the block. |
heading |
str | None
|
A heading above the block. |
text |
str
|
The paragraph, in plain text. |
Source code in commol/context/description.py
Table ¶
Bases: BaseModel
A table: named columns and rows of cells.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['table']
|
The tag of the block. |
heading |
str | None
|
A heading above the block. |
columns |
list[str]
|
The column names. |
rows |
list[list[Cell]]
|
The rows, each with one cell per column. |
Source code in commol/context/description.py
Methods:¶
check_row_lengths ¶
Each row has one cell per column.
Source code in commol/context/description.py
Equations ¶
Bases: BaseModel
A system of equations or of flows.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['equations']
|
The tag of the block. |
heading |
str | None
|
A heading above the block. |
title |
str | None
|
A line that introduces the groups, such as |
groups |
list[EquationGroup]
|
The groups of equations, in order. |
Source code in commol/context/description.py
EquationGroup ¶
Bases: BaseModel
Equations under one heading, such as the flows of one transition.
Attributes:
| Name | Type | Description |
|---|---|---|
heading |
str | None
|
The heading of the group, or None for equations with no heading. |
equations |
list[Equation]
|
The equations, in order. A group with a heading and no equations
renders as |
Source code in commol/context/description.py
Equation ¶
Bases: BaseModel
One equation, or one flow, in plain text and in LaTeX.
Attributes:
| Name | Type | Description |
|---|---|---|
left |
str
|
The left side in plain text, such as |
right |
str
|
The right side in plain text. |
left_latex |
str
|
The left side in LaTeX, without math delimiters. |
right_latex |
str
|
The right side in LaTeX, without math delimiters. |
relation |
Literal['equals', 'flow']
|
|
Source code in commol/context/description.py
StateGraph ¶
Bases: BaseModel
The states of a model and the moves between them.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['state_graph']
|
The tag of the block. |
heading |
str | None
|
A heading above the block. |
name |
str
|
The name of the graph, such as |
nodes |
list[GraphNode]
|
The states. |
edges |
list[GraphEdge]
|
The moves between them. |
expanded |
bool
|
Whether it is the graph of the expanded compartments. The Markdown rendering draws only a graph that is not expanded: an expanded graph can pass what a Mermaid diagram draws. |
Source code in commol/context/description.py
GraphNode ¶
Bases: BaseModel
A node of a StateGraph.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
The id of the node, unique in its graph. |
label |
str
|
The label a diagram shows. |
group |
str | None
|
What the node is, such as |
Source code in commol/context/description.py
GraphEdge ¶
Bases: BaseModel
An edge of a StateGraph: an amount that moves from a node to another.
Attributes:
| Name | Type | Description |
|---|---|---|
source |
str | None
|
The id of the node the amount leaves, or None for an inflow. |
target |
str | None
|
The id of the node the amount enters, or None for an outflow. |
label |
str
|
The label a diagram shows, such as the rate. |
Source code in commol/context/description.py
Matrix ¶
Bases: BaseModel
A matrix with labelled rows and columns, such as the weights of groups.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['matrix']
|
The tag of the block. |
heading |
str | None
|
A heading above the block. |
row_labels |
list[str]
|
The label of each row. |
column_labels |
list[str]
|
The label of each column. |
values |
list[list[float]]
|
One row of values per row label, one value per column label. |
Source code in commol/context/description.py
Methods:¶
check_shape ¶
One row of values per row label, one value per column label.