Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Only add "y::model", "y0" if not already defined
  • Loading branch information
khaeru committed Sep 2, 2023
1 parent 2f62b67 commit b625672
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
5 changes: 0 additions & 5 deletions message_ix_models/data/report/global.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,6 @@ combine:


general:
# List of model periods only, excluding those before or after
- key: "y::model"
comp: model_periods
inputs: [ "y", "cat_year" ]

- key: Liquids:nl-ya
comp: apply_units
inputs: [liquids:nl-ya]
Expand Down
63 changes: 36 additions & 27 deletions message_ix_models/report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
import genno.config
import yaml
from dask.core import literal
from genno import Key
from genno import Key, KeyExistsError
from genno.compat.pyam import iamc as handle_iamc
from message_ix import Reporter, Scenario

from message_ix_models import Context
from message_ix_models.model.structure import get_codes
from message_ix_models.util import local_data_path, package_data_path
from message_ix_models.util._logging import mark_time

from .util import add_replacements

__all__ = [
"prepare_reporter",
"register",
Expand Down Expand Up @@ -306,17 +303,6 @@ def prepare_reporter(
) -> Tuple[Reporter, Key]:
"""Return a :class:`.Reporter` and `key` prepared to report a :class:`.Scenario`.
Parameters
----------
context : Context
Containing settings in the ``report/*`` tree.
scenario : message_ix.Scenario, optional
Scenario to report. If not given, :meth:`.Context.get_scenario` is used to
retrieve a Scenario.
reporter : .Reporter, optional
Existing reporter to extend with computations. If not given, it is created
using :meth:`.Reporter.from_scenario`.
The code responds to the following settings on `context`:
.. list-table::
Expand Down Expand Up @@ -345,6 +331,17 @@ def prepare_reporter(
- Path to write reporting outputs. If given, a computation ``cli-output`` is
added to the Reporter which writes ``report/key`` to this path.
Parameters
----------
context : Context
Containing settings in the ``report/*`` tree.
scenario : message_ix.Scenario, optional
Scenario to report. If not given, :meth:`.Context.get_scenario` is used to
retrieve a Scenario.
reporter : .Reporter, optional
Existing reporter to extend with computations. If not given, it is created
using :meth:`.Reporter.from_scenario`.
Returns
-------
.Reporter
Expand Down Expand Up @@ -419,18 +416,6 @@ def prepare_reporter(
# Pass configuration to the reporter
rep.configure(**config, fail="raise" if has_solution else logging.NOTSET)

# TODO perhaps move all default reporting computations for message_ix_models to a
# `CALLBACK` that is included by default. This would avoid defining any
# tasks in this function.
# Add mappings for conversions to IAMC data structures
add_replacements("c", get_codes("commodity"))
add_replacements("t", get_codes("technology"))

# Ensure "y::model" and "y0" are present
# TODO move upstream, e.g. to message_ix
rep.add("model_periods", "y::model", "y", "cat_year")
rep.add("y0", itemgetter(0), "y::model")

# Apply callbacks for other modules which define additional reporting computations
for callback in CALLBACKS:
callback(rep, context)
Expand All @@ -457,3 +442,27 @@ def prepare_reporter(
log.info("…done")

return rep, key


def defaults(rep: Reporter, context: Context) -> None:
from message_ix_models.model.structure import get_codes

from .util import add_replacements

# Add mappings for conversions to IAMC data structures
add_replacements("c", get_codes("commodity"))
add_replacements("t", get_codes("technology"))

# Ensure "y::model" and "y0" are present
# TODO remove this once message-ix-models depends on message_ix > 3.7.0 at minimum
for comp in (
("y::model", "model_periods", "y", "cat_year"),
("y0", itemgetter(0), "y::model"),
):
try:
rep.add(*comp, strict=True)
except KeyExistsError:
pass # message_ix > 3.7.0; these are already defined


register(defaults)

0 comments on commit b625672

Please sign in to comment.