Skip to content

Commit 0546efe

Browse files
committed
Use .plot.prepare_computer() in .transport.build
- Drop similar/parallel code for adding plots.
1 parent 4128f96 commit 0546efe

File tree

1 file changed

+17
-43
lines changed
  • message_ix_models/model/transport

1 file changed

+17
-43
lines changed

message_ix_models/model/transport/build.py

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import genno
1111
import pandas as pd
12-
from genno import Computer, KeyExistsError, quote
12+
from genno import Computer, Key, KeyExistsError, Keys, quote
1313
from message_ix import Scenario
1414

1515
from message_ix_models import Context, ScenarioInfo
@@ -29,7 +29,6 @@
2929
from .structure import get_technology_groups
3030

3131
if TYPE_CHECKING:
32-
import pathlib
3332
from typing import TypedDict
3433

3534
from message_ix_models.tools.exo_data import ExoDataSource
@@ -42,8 +41,8 @@
4241

4342
def add_debug(c: Computer) -> None:
4443
"""Add tasks for debugging the build."""
45-
from genno import Key, KeySeq
4644

45+
from . import plot
4746
from .key import gdp_cap, ms, pdt_nyt
4847

4948
context: Context = c.graph["context"]
@@ -65,62 +64,37 @@ def add_debug(c: Computer) -> None:
6564
c.graph["config"]["transport build debug dir"] = output_dir
6665

6766
# FIXME Duplicated from base.prepare_reporter()
68-
e_iea = Key("energy:n-y-product-flow:iea")
69-
e_fnp = KeySeq(e_iea.drop("y"))
70-
e_cnlt = Key("energy:c-nl-t:iea+0")
67+
e = Keys(iea="energy:n-y-product-flow:iea", cnlt="energy:c-nl-t:iea+0")
68+
e.fnp = Key(e.iea / "y")
7169
# Transform IEA EWEB data for comparison
72-
c.add(e_fnp[0], "select", e_iea, indexers=dict(y=2020), drop=True)
73-
c.add(e_fnp[1], "aggregate", e_fnp[0], "groups::iea to transport", keep=False)
74-
c.add(e_cnlt, "rename_dims", e_fnp[1], quote(dict(flow="t", n="nl", product="c")))
70+
c.add(e.fnp[0], "select", e.iea, indexers=dict(y=2020), drop=True)
71+
c.add(e.fnp[1], "aggregate", e.fnp[0], "groups::iea to transport", keep=False)
72+
c.add(e.cnlt, "rename_dims", e.fnp[1], quote(dict(flow="t", n="nl", product="c")))
7573

7674
# Write some intermediate calculations from the build process to file
77-
debug_keys = []
75+
k_debug = Key("transport debug")
7876
for i, (key, stem) in enumerate(
7977
(
8078
(gdp_cap, "gdp-ppp-cap"),
8179
(pdt_nyt, "pdt"),
8280
(pdt_nyt + "capita+post", "pdt-cap"),
8381
(ms, "mode-share"),
84-
(e_fnp[0], "energy-iea-0"),
85-
(e_cnlt, "energy-iea-1"),
82+
(e.fnp[0], "energy-iea-0"),
83+
(e.cnlt, "energy-iea-1"),
8684
)
8785
):
88-
debug_keys.append(f"transport debug {i}")
89-
c.add(
90-
debug_keys[-1],
91-
"write_report_debug",
92-
key,
93-
output_dir.joinpath(f"{stem}.csv"),
94-
)
95-
96-
def _(*args) -> "pathlib.Path":
97-
"""Do nothing with the computed `args`, but return `output_path`."""
98-
return output_dir
99-
100-
debug_plots = (
101-
"demand-exo demand-exo-capita demand-exo-capita-gdp inv_cost"
102-
# FIXME The following currently don't work, as their required/expected input
103-
# keys (from the post-solve/report step) do not exist in the build step
104-
# " var-cost fix-cost"
105-
).split()
86+
c.add(k_debug[i], "write_report_debug", key, output_dir.joinpath(f"{stem}.csv"))
10687

107-
c.add(
108-
"transport build debug",
109-
_,
110-
# NB To omit some or all of these calculations / plots from the debug outputs
111-
# for individuals, comment 1 or both of the following lines
112-
*debug_keys,
113-
*[f"plot {p}" for p in debug_plots],
114-
)
115-
# log.info(c.describe("transport build debug"))
88+
c.add("transport build debug", "summarize", *k_debug.generated)
89+
plot.prepare_computer(c, kind=plot.Kind.BUILD, target="transport build debug")
11690

11791
# Also generate these debugging outputs when building the scenario
11892
c.graph["add transport data"].append("transport build debug")
11993

12094

12195
def debug_multi(context: Context, *paths: Path) -> None:
12296
"""Generate plots comparing data from multiple build debug directories."""
123-
from .plot import ComparePDT, ComparePDTCap0, ComparePDTCap1
97+
from . import plot
12498

12599
if isinstance(paths[0], Scenario):
126100
# Workflow was called with --from="…", so paths from the previous step are not
@@ -134,9 +108,9 @@ def debug_multi(context: Context, *paths: Path) -> None:
134108
c = Computer(config={"transport build debug dir": paths[0].parent})
135109
c.require_compat("message_ix_models.report.operator")
136110

137-
for cls in (ComparePDT, ComparePDTCap0, ComparePDTCap1):
138-
key = c.add(f"compare {cls.basename}", cls, *paths)
139-
c.get(key)
111+
plot.prepare_computer(c, kind=plot.Kind.BUILD_MULTI, target="debug multi")
112+
113+
c.get("debug multi")
140114

141115

142116
def add_exogenous_data(c: Computer, info: ScenarioInfo) -> None:

0 commit comments

Comments
 (0)