Skip to content

Commit

Permalink
Add ScenarioInfo.substitute_codes()
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Nov 18, 2024
1 parent 3d2e02d commit ce1c65f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions message_ix_models/util/scenarioinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ScenarioInfo:
"""Information about a :class:`.Scenario` object.
Code that prepares data for a target Scenario can accept a ScenarioInfo instance.
This avoids the need to create or load an actual Scenario, which can be slow under
some conditions.
This avoids the need to create or load an actual Scenario or its data, which can be
a performance-limiting step.
ScenarioInfo objects can also be used (for instance, by :func:`.apply_spec`) to
describe the contents of a Scenario *before* it is created.
Expand Down Expand Up @@ -293,6 +293,28 @@ def io_units(
)
return c / t

def substitute_codes(self) -> None:
"""Update the members of :attr:`set` using :func:`.get_codelist`.
Members of the following set(s) that are :class:`str` are replaced with codes
loaded from the corresponding code list(s), including all annotations:
- ``commodity``
.. todo:: Extend for other sets and cases where there are multiple code lists
(for instance ``year``, ``region``).
"""
from message_ix_models.model.structure import get_codelist

for cl_id in ("commodity",):
cl = get_codelist(cl_id)
for i, value in enumerate(self.set[cl_id]):
if isinstance(value, str):
try:
self.set[cl_id][i] = cl[value]
except KeyError:
pass

def year_from_codes(self, codes: List[sdmx_model.Code]):
"""Update using a list of `codes`.
Expand Down

0 comments on commit ce1c65f

Please sign in to comment.