diff --git a/doc/release-notes.md b/doc/release-notes.md index 5aae1f38..e003d276 100644 --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -131,6 +131,7 @@ linopy 1.0 makes the strict arithmetic convention the only behaviour and puts de - `Model.from_spec` builds a linear objective when the data sets all quadratic coefficients to zero. ([\#1141](https://github.com/PyPSA/linopy/pull/1141)) - `Model.from_spec` no longer refuses an infinite parameter times a variable absent there, where no row is built. ([\#1143](https://github.com/PyPSA/linopy/pull/1143)) - `Model.from_spec` adds no constraint where every variable of it is masked, where it raised that both sides are constant. ([\#1154](https://github.com/PyPSA/linopy/pull/1154)) +- `Model.from_spec` reads a `sum_back` window that holds only absent terms as 0, like `sum`, in a constraint and in a `where:`, where it was absent; a row left with no variable term is then refused, as for `sum`. ([\#1174](https://github.com/PyPSA/linopy/pull/1174)) - `Model.from_spec` no longer lets a `where` guard what a `shift`, window, `sum`, `count` or `at()` lookup reads at another coordinate, so a non-finite value there raises instead of entering a row or a `when:`. ([\#1173](https://github.com/PyPSA/linopy/pull/1173)) - `Model.from_spec` checks a case's value, `otherwise:` included, only where that case applies, and skips a case that applies nowhere, so a guard like `cap == inf` no longer leaves `otherwise:` refused there. ([\#1163](https://github.com/PyPSA/linopy/pull/1163)) - `LinearExpression(data, model)` no longer raises on a `.sel` over full coordinates. ([\#1144](https://github.com/PyPSA/linopy/pull/1144)) diff --git a/doc/user-guide/spec-data.md b/doc/user-guide/spec-data.md index 91c2fa95..c6f4477b 100644 --- a/doc/user-guide/spec-data.md +++ b/doc/user-guide/spec-data.md @@ -297,6 +297,9 @@ The other operators read past an edge in their own way: - `sum_back(p, along=s, window=3)` is a sum, so it shortens its window at the start rather than dropping the row: the first `s` reads `p[0]` alone. + A window that holds only absent terms is 0, as a `sum` of them is. + A member that `within=` places in no group, or one with no width, has + no window, and the row goes with it. `edge='wrap'` reaches back into the end, `within=rel[col]` stops at each group's edge. - `at(p, by=rel[col])` reads nothing for a member the relation maps nowhere, diff --git a/linopy/spec/operators.py b/linopy/spec/operators.py index 6217d28d..a9a7aac9 100644 --- a/linopy/spec/operators.py +++ b/linopy/spec/operators.py @@ -367,8 +367,11 @@ def sum_back( """ Sum *array* over a trailing window along *over*: positions ``t - within + 1`` through ``t``. - A position the window cannot reach contributes a zero; a window that - reaches nothing keeps no row. *by* stops the window at each group's edge. + A position the window cannot reach contributes a zero, as an absent term + does, so a window of nothing but such positions is 0, as :func:`sum_over` + is. A window that is not defined there, at a member *by* places in no + group or where the width is absent, keeps no row. *by* stops the window + at each group's edge. With *wrap* the window continues from the other end of *over*, so a width above the axis size still reads every position and is taken as the whole @@ -386,15 +389,16 @@ def sum_back( ) widest = max(1, min(asked, size)) lagged_terms: list[Array] = [] - reached: list[xr.DataArray] = [] for lag in range(widest): - lagged = _gather(array, over, lag, partition, wrap, None) - live, term = ~lagged.isnull(), _filled(lagged, 0.0) + term = _filled(_gather(array, over, lag, partition, wrap, None), 0.0) if isinstance(within, xr.DataArray): - live, term = live & (within > lag), term * (within > lag).astype(float) + term = term * (within > lag).astype(float) lagged_terms.append(term) - reached.append(live) - return _merged(lagged_terms).where(reduce(operator.or_, reached)) + defined: list[xr.DataArray] = [] if partition is None else [partition.grouped] + if isinstance(within, xr.DataArray): + defined.append(within.notnull()) + summed = _merged(lagged_terms) + return summed.where(reduce(operator.and_, defined)) if defined else summed def _widest(within: Amount) -> int: diff --git a/test/test_spec_builder.py b/test/test_spec_builder.py index 3d2a937a..f42404e4 100644 --- a/test/test_spec_builder.py +++ b/test/test_spec_builder.py @@ -345,7 +345,6 @@ def absent_row(expression: str) -> dict[str, Any]: pytest.param("x <= a", [1, 2], id="constant-side"), pytest.param("x <= shift(a, along=t, offset=1, edge=0)", [0, 2], id="shift"), pytest.param("x <= at(z, by=grp[s])", [2], id="at"), - pytest.param("sum_back(a * x, along=t, window=2) <= 4", [1, 2], id="sum-back"), ], ) def test_an_absent_row_masks_the_row_it_reaches( @@ -355,6 +354,12 @@ def test_an_absent_row_masks_the_row_it_reaches( assert T[(row.labels != -1).to_numpy()].tolist() == kept +def test_a_row_of_nothing_but_an_absent_window_is_refused_like_sum() -> None: + spec = absent_row("sum_back(a * x, along=t, window=2) <= 4") + with pytest.raises(SpecDataError, match="1 row.*hold no variable term.*at t=0"): + Model.from_spec(spec, ABSENT_DATA) + + def test_an_absent_summand_is_dropped() -> None: spec = with_( ABSENT_SPEC, @@ -1013,6 +1018,23 @@ def test_a_missing_row_in_a_where_reads_the_way_missing_says( assert (m.variables["x"].labels != -1).values.tolist() == [True, kept] +def test_a_where_reads_a_window_of_absent_rows_as_the_sum_of_the_rest() -> None: + spec = { + "dimensions": {"t": {"dtype": "int", "ordered": True}}, + "parameters": {"p": {"dims": ["t"], "missing": "absent"}}, + "variables": { + "x": { + "dims": ["t"], + "where": "sum_back(p, along=t, window=2) <= 1", + "bounds": {"lower": 0, "upper": 1}, + } + }, + } + p = pd.DataFrame({"t": [1, 2], "p": [1.0, 5.0]}) + m = Model.from_spec(spec, {"t": T, "p": p}) + assert (m.variables["x"].labels != -1).values.tolist() == [True, True, False] + + @pytest.mark.parametrize( ("missing", "rows", "x"), [ diff --git a/test/test_spec_operators.py b/test/test_spec_operators.py index bb9d3f58..0c63a501 100644 --- a/test/test_spec_operators.py +++ b/test/test_spec_operators.py @@ -278,6 +278,61 @@ def test_a_trailing_sum_wider_than_its_axis_wraps_onto_the_whole_axis() -> None: ) +@pytest.mark.parametrize( + ("key", "data", "expected"), + [ + pytest.param("sum-back", {}, [1, 1, 0, 8], id="sum-back"), + pytest.param("sum-back-wrap", {}, [9, 1, 0, 8], id="sum-back-wrap"), + pytest.param("sum-back-in-groups", {}, [1, 1, 0, 8], id="sum-back-in-groups"), + pytest.param( + "sum-back-group-width", {}, [1, 0, 0, 8], id="sum-back-group-width" + ), + pytest.param( + "sum-back-group-width", + {"width": pd.DataFrame({"s": S, "width": [1, 0]})}, + [1, 0, 0, 0], + id="width-of-zero-is-zero", + ), + pytest.param( + "sum-back-in-groups", + {"season_of": pd.DataFrame({"t": TT[:3], "s": list("aab")})}, + [1, 1, 0, np.nan], + id="member-in-no-group-keeps-no-row", + ), + pytest.param( + "sum-back-group-width", + {"width": pd.DataFrame({"s": S[:1], "width": [1]})}, + [1, 0, np.nan, np.nan], + id="absent-width-keeps-no-row", + ), + ], +) +def test_a_defined_window_of_absent_terms_sums_to_zero_like_sum( + key: str, data: dict[str, Any], expected: list[float] +) -> None: + live = xr.DataArray([True, False, False, True], coords=[TT]) + x = {**operator_spec()["variables"]["x"], "where": "live"} + spec = with_( + operator_spec(), + parameters={ + "live": {"dims": ["t"], "dtype": "bool"}, + "width": {"dims": ["s"], "dtype": "int", "missing": "absent"}, + }, + variables={"x": x}, + ) + m = solved(spec, {**OPERATOR_DATA, "live": live, **data}) + name = key.replace("-", "_") + want = xr.DataArray(expected, coords=[TT], name=f"probe_{name}") + built = m.constraints[f"link_{name}"].labels != -1 + assert built.values.tolist() == want.notnull().values.tolist() + xr.testing.assert_allclose( + m.solution[f"y_{name}"].where(built), want.rename(f"y_{name}") + ) + xr.testing.assert_allclose( + evaluate(spec, m, f"probe_{name}")[f"probe_{name}"], want + ) + + # --------------------------------------------------------------------------- # grouped sum # ---------------------------------------------------------------------------