Building models (model.build)
apply_spec() can be used to be build (compose, assemble, construct, …) models given three pieces of information:
A
message_ix.Scenarioto be used as a base. This scenario may be empty.A specification, or
Spec, which is trio ofScenarioInfoobjects; see below.An optional function that adds or produces data to add to the scenario.
The spec is applied as follows:
For each
ixmpset that exists in scenario:Required elements from
Spec.require, if any, are checked.If they are missing,
apply_spec()raisesValueError. This indicates that spec is not compatible with the given scenario.Elements from
Spec.remove, if any, are removed.Any parameter values which reference these set elements are also removed, using
strip_par_data().New set elements from
Spec.add, if any, are added.
Elements in
spec.add.set["unit"]are added to the Platform on which scenario is stored.The data argument, a function, is called with scenario as the first argument, and a keyword argument dry_run from
apply_spec(). data may either add to scenario directly (by callingScenario.add_par()and similar methods); or it can return adictthat can be passed toadd_par_data().
The following modules use this workflow and can be examples for developing similar code:
message_data.model.transport
Code reference
- message_ix_models.model.build.apply_spec(scenario: Scenario, spec: Spec | Mapping[str, ScenarioInfo], data: Callable | None = None, **options)[source]
Apply spec to scenario.
- Parameters:
spec (
Spec) – Specification of changes to make to scenario.data (
callable, optional) – Function to add data to scenario. data can either manipulate the scenario directly, or return adictcompatible withadd_par_data().dry_run (
bool) – Don’t modify scenario; only show what would be done. DefaultFalse. Exceptions will still be raised if the elements fromspec['required']are missing; this serves as a check that the scenario has the required features for applying the spec.fast (
bool) – Do not remove existing parameter data; increases speed on large scenarios.quiet (
bool) – Only show log messages at levelERRORand higher. IfFalse(default), show log messages at levelDEBUGand higher.message (
str) – Commit message.
See also