Skip to content

Commit 0280909

Browse files
committed
Move quantity_from_iamc to .report.operator
1 parent 2cd1b05 commit 0280909

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

message_ix_models/model/transport/report.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
if TYPE_CHECKING:
2020
import ixmp
2121
from genno import Computer
22-
from genno.types import AnyQuantity
2322

2423
from message_ix_models import Spec
2524

@@ -519,6 +518,7 @@ def multi(context: Context, targets):
519518
"""Report outputs from multiple scenarios."""
520519
import plotnine as p9
521520

521+
from message_ix_models.report.operator import quantity_from_iamc
522522
from message_ix_models.tools.iamc import _drop_unique
523523

524524
report_dir = context.get_local_path("report")
@@ -573,32 +573,3 @@ def multi(context: Context, targets):
573573
plot.save("debug.pdf")
574574

575575
return data
576-
577-
578-
def quantity_from_iamc(qty: "AnyQuantity", variable: str) -> "AnyQuantity":
579-
"""Extract data for a single measure from `qty` with (at least) dimensions v, u.
580-
581-
.. todo:: Move upstream, to either :mod:`ixmp` or :mod:`genno`.
582-
583-
Parameters
584-
----------
585-
variable : str
586-
Regular expression to match the ``v`` dimension of `qty`.
587-
"""
588-
import re
589-
590-
from genno.operator import relabel, select
591-
592-
expr = re.compile(variable)
593-
variables, replacements = [], {}
594-
for var in qty.coords["v"].data:
595-
if match := expr.fullmatch(var):
596-
variables.append(match.group(0))
597-
replacements[match.group(0)] = match.group(1)
598-
599-
subset = qty.pipe(select, {"v": variables}).pipe(relabel, {"v": replacements})
600-
601-
unique_units = subset.coords["Unit"].data
602-
assert 1 == len(unique_units)
603-
subset.units = unique_units[0]
604-
return subset.sel(Unit=unique_units[0], drop=True)

message_ix_models/report/operator.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from pathlib import Path
2121

2222
from genno import Computer, Key
23+
from genno.types import AnyQuantity
2324
from sdmx.model.v21 import Code
2425

2526
log = logging.getLogger(__name__)
@@ -233,6 +234,33 @@ def from_url(url: str, cls=ixmp.TimeSeries) -> ixmp.TimeSeries:
233234
return ts
234235

235236

237+
def quantity_from_iamc(qty: "AnyQuantity", variable: str) -> "AnyQuantity":
238+
"""Extract data for a single measure from `qty` with (at least) dimensions v, u.
239+
240+
.. todo:: Move upstream, to either :mod:`ixmp` or :mod:`genno`.
241+
242+
Parameters
243+
----------
244+
variable : str
245+
Regular expression to match the ``v`` dimension of `qty`.
246+
"""
247+
from genno.operator import relabel, select
248+
249+
expr = re.compile(variable)
250+
variables, replacements = [], {}
251+
for var in qty.coords["v"].data:
252+
if match := expr.fullmatch(var):
253+
variables.append(match.group(0))
254+
replacements[match.group(0)] = match.group(1)
255+
256+
subset = qty.pipe(select, {"v": variables}).pipe(relabel, {"v": replacements})
257+
258+
unique_units = subset.coords["Unit"].data
259+
assert 1 == len(unique_units)
260+
subset.units = unique_units[0]
261+
return subset.sel(Unit=unique_units[0], drop=True)
262+
263+
236264
# commented: currently unused
237265
# def share_cogeneration(fraction, *parts):
238266
# """Deducts a *fraction* from the first of *parts*."""

0 commit comments

Comments
 (0)