Skip to content

Commit

Permalink
Add extend_y(…, dim="y") keyword argument
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Aug 7, 2024
1 parent 09252c8 commit 47b89a5
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions message_ix_models/model/transport/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,22 +335,22 @@ def duration_period(info: "ScenarioInfo") -> "AnyQuantity":
).pipe(unique_units_from_dim, "unit")


def extend_y(qty: "AnyQuantity", y: List[int]) -> "AnyQuantity":
def extend_y(qty: "AnyQuantity", y: List[int], *, dim: str = "y") -> "AnyQuantity":
"""Extend `qty` along the "y" dimension to cover `y`."""
y_ = set(y)

# Subset of `y` appearing in `qty`
y_qty = sorted(set(qty.to_series().reset_index()["y"].unique()) & y_)
y_qty = sorted(set(qty.to_series().reset_index()[dim].unique()) & y_)

Check warning on line 343 in message_ix_models/model/transport/operator.py

View check run for this annotation

Codecov / codecov/patch

message_ix_models/model/transport/operator.py#L343

Added line #L343 was not covered by tests
# Subset of `target_years` to fill forward from the last period in `qty`
y_to_fill = sorted(filter(partial(lt, y_qty[-1]), y_))

log.info(f"{qty.name}: extend from {y_qty[-1]}{y_to_fill}")
log.info(f"{qty.name}: extend '{dim}' from {y_qty[-1]}{y_to_fill}")

Check warning on line 347 in message_ix_models/model/transport/operator.py

View check run for this annotation

Codecov / codecov/patch

message_ix_models/model/transport/operator.py#L347

Added line #L347 was not covered by tests

# Map existing labels to themselves, and missing labels to the last existing one
y_map = [(y, y) for y in y_qty] + [(y_qty[-1], y) for y in y_to_fill]
# - Forward-fill *within* `qty` existing values.
# - Use message_ix_models MappingAdapter to do the rest.
return MappingAdapter({"y": y_map})(qty.ffill("y")) # type: ignore [attr-defined]
return MappingAdapter({dim: y_map})(qty.ffill(dim)) # type: ignore [attr-defined]

Check warning on line 353 in message_ix_models/model/transport/operator.py

View check run for this annotation

Codecov / codecov/patch

message_ix_models/model/transport/operator.py#L353

Added line #L353 was not covered by tests


def factor_fv(n: List[str], y: List[int], config: dict) -> "AnyQuantity":
Expand Down

0 comments on commit 47b89a5

Please sign in to comment.