From 48d3dec84bfebc61ea47e27630d33fa2478dd229 Mon Sep 17 00:00:00 2001 From: Mayk Thewessen Date: Wed, 28 Oct 2026 15:42:09 +0100 Subject: [PATCH 1/2] Support multidimensional slope-to-breakpoint conversion Slopes.to_breakpoints() accepts slopes and x-points with more than one entity dimension, with a scalar or DataArray y0, exact entity-coordinate alignment and trailing NaN padding. The single-entity path is unchanged. Co-Authored-By: Claude Opus 5.5 --- doc/release-notes.md | 1 + linopy/piecewise.py | 64 ++++++++++- test/test_piecewise_snapshot_slopes.py | 141 +++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 test/test_piecewise_snapshot_slopes.py diff --git a/doc/release-notes.md b/doc/release-notes.md index 67b1bdcb3..3dd557ad7 100644 --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -38,6 +38,7 @@ linopy 1.0 makes the strict arithmetic convention the only behaviour and puts de - `LinearExpression.from_coo(model, coords, rows, vars, coeffs)` builds an expression from COO triplets (cell position, variable label, coefficient) on a coordinate grid. - `LinearExpression.to_coo()` returns the live terms of an expression as COO triplets, the counterpart of `from_coo`. A sparse expression is read without a dense copy. ([\#1156](https://github.com/PyPSA/linopy/pull/1156)) - `@`/`dot` contract a `DataArray` backed by a [pydata/sparse](https://sparse.pydata.org) array without densifying. Where no sparse path exists, e.g. for a `QuadraticExpression`, it densifies with a `PerformanceWarning`; `warn_on_densify=False` silences it. +- [`Slopes.to_breakpoints()`][linopy.piecewise.Slopes.to_breakpoints] accepts slopes and x-points with more than one entity dimension, e.g. snapshot and component, with a scalar or `DataArray` `y0` over those dimensions. Entity coordinates must match exactly; ragged curves use trailing NaN padding. ([\#1013](https://github.com/PyPSA/linopy/pull/1013)) - `linopy.options["warn_on_absorbed_terms"] = True` emits an `AbsorbedTermsWarning` wherever an absent operand drops the present terms of another, e.g. in `p - r.reindex(unit=units)`. ([\#1135](https://github.com/PyPSA/linopy/issues/1135)) - `linopy.testing.assert_equivalent` compares two linear expressions independently of their backing (dense or sparse) and term layout. ([\#1044](https://github.com/PyPSA/linopy/issues/1044)) - The `Variable` methods that wrap xarray, e.g. `sel`, `isel` and `shift`, carry typed signatures and return `Variable` for type checkers and IDEs. ([\#1029](https://github.com/PyPSA/linopy/pull/1029)) diff --git a/linopy/piecewise.py b/linopy/piecewise.py index f00df1501..171057c83 100644 --- a/linopy/piecewise.py +++ b/linopy/piecewise.py @@ -112,7 +112,9 @@ class Slopes: ---------- values : BreaksLike Per-piece slopes. 1D for shared breakpoints; 2D (DataFrame / - dict / DataArray with entity dim) for per-entity slopes. + dict / DataArray with entity dim) for per-entity slopes; a + DataArray with several entity dims (e.g. snapshot and component) + takes a scalar or DataArray ``y0``. y0 : float, dict, pd.Series, or DataArray, default 0.0 y-value at the first breakpoint. Scalar broadcasts to all entities; dict/Series/DataArray provides per-entity values. @@ -527,9 +529,63 @@ def _breakpoints_from_slopes( # Multi-dim case: per-entity slopes entity_dims = [d for d in slopes_arr.dims if d != BREAKPOINT_DIM] - if len(entity_dims) != 1: - raise ValueError( - f"Expected exactly one entity dimension in slopes, got {entity_dims}" + if len(entity_dims) > 1: + if xp_arr.sizes[BREAKPOINT_DIM] == 0: + raise ValueError("Multidimensional slopes require at least one x_point") + # Align entity coordinates exactly, while breakpoint positions remain + # positional: pieces and points intentionally have different lengths. + slopes_arr, xp_arr = xr.align( + slopes_arr, xp_arr, join="exact", exclude={BREAKPOINT_DIM} + ) + template = xr.broadcast( + slopes_arr.sum(BREAKPOINT_DIM), + xp_arr.sum(BREAKPOINT_DIM), + )[0] + slopes_arr = slopes_arr.broadcast_like(template) + xp_arr = xp_arr.broadcast_like(template) + for values in (slopes_arr, xp_arr): + if bool(np.isinf(values).any()): + raise ValueError("Slopes and x_points must be finite or trailing NaN") + if not _has_trailing_nan_only(values): + raise ValueError( + "Slopes and x_points may only have trailing NaN padding" + ) + if bool( + (slopes_arr.count(BREAKPOINT_DIM) != xp_arr.count(BREAKPOINT_DIM) - 1).any() + ): + raise ValueError("Slope count must be x_point count minus one per entity") + if bool((xp_arr.count(BREAKPOINT_DIM) < 1).any()): + raise ValueError("At least one x_point is required per entity") + if isinstance(y0, Real): + initial = xr.full_like(template, float(y0), dtype=float) + elif isinstance(y0, DataArray): + if not set(y0.dims).issubset(template.dims): + raise ValueError("y0 dimensions must be entity dimensions") + initial, _ = xr.align(y0, template, join="exact") + initial = initial.broadcast_like(template) + else: + raise TypeError("Multidimensional slopes require scalar or DataArray y0") + if not bool(np.isfinite(initial).all()): + raise ValueError("y0 must be finite") + count = xp_arr.sizes[BREAKPOINT_DIM] + xp_arr = xp_arr.assign_coords({BREAKPOINT_DIM: np.arange(count)}) + slopes_arr = slopes_arr.assign_coords( + {BREAKPOINT_DIM: np.arange(slopes_arr.sizes[BREAKPOINT_DIM])} + ).reindex({BREAKPOINT_DIM: np.arange(count - 1)}) + widths = xp_arr.diff(BREAKPOINT_DIM).assign_coords( + {BREAKPOINT_DIM: np.arange(count - 1)} + ) + cumulative = (widths * slopes_arr).cumsum(BREAKPOINT_DIM, skipna=False) + cumulative = (cumulative + initial).assign_coords( + {BREAKPOINT_DIM: np.arange(1, count)} + ) + return ( + xr.concat( + [initial.expand_dims({BREAKPOINT_DIM: [0]}), cumulative], + dim=BREAKPOINT_DIM, + ) + .transpose(*template.dims, BREAKPOINT_DIM) + .where(xp_arr.notnull()) ) entity_dim = str(entity_dims[0]) entity_keys = slopes_arr.coords[entity_dim].values diff --git a/test/test_piecewise_snapshot_slopes.py b/test/test_piecewise_snapshot_slopes.py new file mode 100644 index 000000000..ff29f4a99 --- /dev/null +++ b/test/test_piecewise_snapshot_slopes.py @@ -0,0 +1,141 @@ +""" +Public multidimensional slope integration contracts. + +Failure list: snapshot/name coordinates are lost; shared x or y0 fails to +broadcast; leading alignment shifts a piece; ragged padding integrates a +missing piece; malformed counts, interior holes or infinity go unnoticed. +Existing tests cover one entity dimension, not snapshot/name arrays. These +independent arithmetic expectations require no production test seam. +""" + +from __future__ import annotations + +from typing import Literal + +import numpy as np +import pandas as pd +import pytest +import xarray as xr + +from linopy import Slopes +from linopy.constants import BREAKPOINT_DIM + + +def arrays() -> tuple[xr.DataArray, xr.DataArray]: + coords = { + "snapshot": pd.Index([2, 7]), + "name": pd.Index(["a", "b"], dtype="string"), + BREAKPOINT_DIM: [0, 1, 2], + } + x = xr.DataArray( + [[[0, 1, 3], [0, 2, 4]], [[0, 2, 3], [0, 1, np.nan]]], + dims=["snapshot", "name", BREAKPOINT_DIM], + coords=coords, + ) + slopes = xr.DataArray( + [[[2, 4], [3, 5]], [[6, 8], [7, np.nan]]], + dims=x.dims, + coords={**coords, BREAKPOINT_DIM: [0, 1]}, + ) + return x, slopes + + +@pytest.mark.parametrize("leading", [False, True]) +def test_snapshot_name_slopes_preserve_coordinates_and_padding(leading: bool) -> None: + x, slopes = arrays() + if leading: + first = xr.full_like( + slopes.isel({BREAKPOINT_DIM: 0}, drop=True), np.nan + ).expand_dims({BREAKPOINT_DIM: [0]}) + slopes = xr.concat( + [first, slopes.assign_coords({BREAKPOINT_DIM: [1, 2]})], dim=BREAKPOINT_DIM + ) + y = Slopes(slopes, y0=1, align="leading" if leading else "pieces").to_breakpoints(x) + expected = xr.DataArray( + [[[1, 3, 11], [1, 7, 17]], [[1, 13, 21], [1, 8, np.nan]]], + dims=x.dims, + coords=x.coords, + ) + xr.testing.assert_equal(y.transpose(*x.dims), expected) + + +def test_static_x_and_per_snapshot_y0_broadcast() -> None: + x, slopes = arrays() + slopes = slopes.fillna(9) + static_x = xr.DataArray([0, 1, 2], dims=[BREAKPOINT_DIM]) + y0 = xr.DataArray([10, 20], dims=["snapshot"], coords={"snapshot": x.snapshot}) + y = Slopes(slopes, y0=y0).to_breakpoints(static_x) + expected = xr.DataArray( + [[[10, 12, 16], [10, 13, 18]], [[20, 26, 34], [20, 27, 36]]], + dims=x.dims, + coords=x.coords, + ) + xr.testing.assert_equal(y.transpose(*x.dims), expected) + + +@pytest.mark.parametrize( + ("invalid", "message"), + [ + ("count", "count"), + ("interior", "trailing"), + ("infinite", "finite"), + ("leading", "first slope"), + ], +) +def test_invalid_snapshot_slopes_raise(invalid: str, message: str) -> None: + x, slopes = arrays() + align: Literal["pieces", "leading"] = "pieces" + if invalid == "count": + slopes[0, 0, 1] = np.nan + elif invalid == "interior": + x[0, 0, 1] = np.nan + elif invalid == "infinite": + slopes[0, 0, 0] = np.inf + else: + align = "leading" + with pytest.raises(ValueError, match=message): + Slopes(slopes, align=align).to_breakpoints(x) + + +def test_empty_slopes_with_one_x_point_keeps_initial_value() -> None: + x, slopes = arrays() + points = x.isel({BREAKPOINT_DIM: slice(0, 1)}) + empty_slopes = slopes.isel({BREAKPOINT_DIM: slice(0, 0)}) + result = Slopes(empty_slopes, y0=3).to_breakpoints(points) + xr.testing.assert_equal(result, xr.full_like(points, 3)) + + +def test_empty_x_points_raise_owned_error() -> None: + x, slopes = arrays() + with pytest.raises(ValueError, match="at least one x_point"): + Slopes(slopes).to_breakpoints(x.isel({BREAKPOINT_DIM: slice(0, 0)})) + + +def test_mismatched_entity_coordinates_are_rejected() -> None: + x, slopes = arrays() + with pytest.raises(ValueError, match="align.*exact"): + Slopes(slopes).to_breakpoints(x.assign_coords(snapshot=[2, 8])) + + +@pytest.mark.parametrize( + ("invalid", "message"), + [ + ("unknown-axis", "entity dimensions"), + ("missing-coordinate", "align.*exact"), + ("nan", "finite"), + ("infinite", "finite"), + ], +) +def test_invalid_initial_values_are_rejected(invalid: str, message: str) -> None: + x, slopes = arrays() + y0: xr.DataArray | float + if invalid == "unknown-axis": + y0 = xr.DataArray([1], dims=["unrelated"]) + elif invalid == "missing-coordinate": + y0 = xr.DataArray([1], dims=["snapshot"], coords={"snapshot": [2]}) + elif invalid == "nan": + y0 = np.nan + else: + y0 = np.inf + with pytest.raises(ValueError, match=message): + Slopes(slopes, y0=y0).to_breakpoints(x) From 6db1c11daeef71b715aad386bc7c4e52e8ec38bb Mon Sep 17 00:00:00 2001 From: Mayk Thewessen Date: Wed, 18 Nov 2026 21:20:17 +0100 Subject: [PATCH 2/2] Drop unreachable per-entity x_point check, test mapping y0 An entity without x-points already fails the slope-count check, so the second check never fired. A dict or Series y0 on multidimensional slopes now has a test for its TypeError. Co-Authored-By: Claude Opus 5.5 --- linopy/piecewise.py | 2 -- test/test_piecewise_snapshot_slopes.py | 9 ++++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/linopy/piecewise.py b/linopy/piecewise.py index 171057c83..d0bf098ed 100644 --- a/linopy/piecewise.py +++ b/linopy/piecewise.py @@ -554,8 +554,6 @@ def _breakpoints_from_slopes( (slopes_arr.count(BREAKPOINT_DIM) != xp_arr.count(BREAKPOINT_DIM) - 1).any() ): raise ValueError("Slope count must be x_point count minus one per entity") - if bool((xp_arr.count(BREAKPOINT_DIM) < 1).any()): - raise ValueError("At least one x_point is required per entity") if isinstance(y0, Real): initial = xr.full_like(template, float(y0), dtype=float) elif isinstance(y0, DataArray): diff --git a/test/test_piecewise_snapshot_slopes.py b/test/test_piecewise_snapshot_slopes.py index ff29f4a99..a98ebbc45 100644 --- a/test/test_piecewise_snapshot_slopes.py +++ b/test/test_piecewise_snapshot_slopes.py @@ -3,7 +3,8 @@ Failure list: snapshot/name coordinates are lost; shared x or y0 fails to broadcast; leading alignment shifts a piece; ragged padding integrates a -missing piece; malformed counts, interior holes or infinity go unnoticed. +missing piece; malformed counts, interior holes, infinity or a mapping y0 +go unnoticed. Existing tests cover one entity dimension, not snapshot/name arrays. These independent arithmetic expectations require no production test seam. """ @@ -139,3 +140,9 @@ def test_invalid_initial_values_are_rejected(invalid: str, message: str) -> None y0 = np.inf with pytest.raises(ValueError, match=message): Slopes(slopes, y0=y0).to_breakpoints(x) + + +def test_mapping_initial_values_are_rejected() -> None: + x, slopes = arrays() + with pytest.raises(TypeError, match="scalar or DataArray y0"): + Slopes(slopes, y0={"a": 1.0, "b": 2.0}).to_breakpoints(x)