From cc0ac2a5dd3eaeff2591639b4b88bf3b342d0c91 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 15:42:58 +0000 Subject: [PATCH 1/4] fix(spec): an absent shift offset reaches no row, whatever the edge Closes #1184 --- linopy/spec/operators.py | 23 ++++++++++++++--------- test/test_spec_operators.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/linopy/spec/operators.py b/linopy/spec/operators.py index a9a7aac9..fa7fc0d6 100644 --- a/linopy/spec/operators.py +++ b/linopy/spec/operators.py @@ -475,23 +475,26 @@ def _gather_by_offset( Selection is by position, so no label is looked up. Out-of-range positions are clipped onto the axis and emptied again, so an edge means - what it does for a scalar shift. + what it does for a scalar shift. Where the offset is absent the shift is + not defined, so no edge speaks for it and it reaches nothing. """ card = int(array.sizes[over]) labels = np.asarray(array.indexes[over]) ordinal = xr.DataArray(np.arange(card), coords={over: labels}, dims=[over]) - source = (ordinal - offset).astype(int) + reached = (ordinal - offset).assign_coords({over: labels}) + defined = reached.notnull() + source = reached.fillna(0).astype(int) def gathered(ordinals: xr.DataArray) -> Array: return _read_at(array, over, labels, ordinals) if wrap: - return gathered(source % card) - inside = ((source >= 0) & (source < card)).assign_coords({over: labels}) + return gathered(source % card).where(defined) + inside = defined & (source >= 0) & (source < card) moved = gathered(source.clip(0, card - 1)).where(inside) if fill is None: return moved - return _vacated(moved, array, over, ~inside, fill) + return _vacated(moved, array, over, defined & ~inside, fill) def per_group(offset: Amount, groups: xr.DataArray) -> Amount: @@ -514,13 +517,15 @@ def _gather_in_groups( """ Translate *array* inside each group rather than along the axis. - A coordinate in no group reaches nothing, which is not the same as - reaching off a group's edge: only the second is what a fill speaks for. + A coordinate in no group, or where the offset is absent, reaches + nothing, which is not the same as reaching off a group's edge: only the + second is what a fill speaks for. """ reached = groups.within - offset if wrap: reached = reached % groups.size - inside = groups.grouped & (reached >= 0) & (reached < groups.size) + defined = groups.grouped & reached.notnull() + inside = defined & (reached >= 0) & (reached < groups.size) def peer(group: np.ndarray, position: np.ndarray) -> np.ndarray: return groups.roster[group, position] @@ -530,7 +535,7 @@ def peer(group: np.ndarray, position: np.ndarray) -> np.ndarray: gathered = _read_at(array, over, labels, source).where(inside) if fill is None: return gathered - return _vacated(gathered, array, over, groups.grouped & ~inside, fill) + return _vacated(gathered, array, over, defined & ~inside, fill) def _read_at( diff --git a/test/test_spec_operators.py b/test/test_spec_operators.py index 0c63a501..fe158403 100644 --- a/test/test_spec_operators.py +++ b/test/test_spec_operators.py @@ -249,6 +249,38 @@ def test_a_missing_shift_amount_is_refused_where_a_live_row_reads_it( ] +@pytest.mark.parametrize( + "within", ["", ", within=season_of[s]"], ids=["axis", "groups"] +) +@pytest.mark.parametrize("edge", ["edge=0", "edge='wrap'"]) +def test_an_absent_entity_offset_shifts_to_no_row( + backend: Model, edge: str, within: str +) -> None: + e = pd.Index(["a", "b"], name="e") + shifted = f"shift(x, along=t, offset=lag, {edge}{within})" + spec = { + "dimensions": { + "t": {"dtype": "int", "ordered": True}, + "s": {"dtype": "str"}, + "e": {"dtype": "str"}, + }, + "relations": {"season_of": {"key": "t", "values": "s"}}, + "parameters": {"lag": {"dims": ["e"], "dtype": "int", "missing": "absent"}}, + "variables": {"x": {"dims": ["t", "e"]}}, + "constraints": {"c": {"dims": ["t", "e"], "expression": f"x + {shifted} >= 0"}}, + } + data = { + "t": TT, + "s": S, + "e": e, + "season_of": pd.DataFrame({"t": TT, "s": ["a", "a", "b", "b"]}), + "lag": pd.DataFrame({"e": ["a"], "lag": [1]}), + } + m = Model.from_spec(spec, data, sparse=backend.sparse) + live = m.constraints["c"].labels != -1 + assert live.sum("t").values.tolist() == [4, 0] + + WINDOW_SPEC: dict[str, Any] = { "dimensions": {"t": {"dtype": "int", "ordered": True}}, "parameters": {"v": {"dims": ["t"]}}, From ba8921f3df03b7e89cfbffbd0646b4662a0d1225 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 15:43:15 +0000 Subject: [PATCH 2/4] doc: release note for #1185 --- doc/release-notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/release-notes.md b/doc/release-notes.md index 67b1bdcb..64c28430 100644 --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -132,6 +132,8 @@ 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` leaves no row where a per-entity `shift` offset is absent; it read `x[t=0]` with `edge='wrap'` and 0 with `edge=`. ([\#1185](https://github.com/PyPSA/linopy/pull/1185)) + - `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)) From 74ee9810c2711893dde288a52b2a987ef8d05183 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 15:43:29 +0000 Subject: [PATCH 3/4] doc: drop stray blank line in release notes --- doc/release-notes.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/release-notes.md b/doc/release-notes.md index 64c28430..70096c95 100644 --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -133,7 +133,6 @@ linopy 1.0 makes the strict arithmetic convention the only behaviour and puts de - `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` leaves no row where a per-entity `shift` offset is absent; it read `x[t=0]` with `edge='wrap'` and 0 with `edge=`. ([\#1185](https://github.com/PyPSA/linopy/pull/1185)) - - `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)) From 648e5bdbcf1065e79296ebe6c4945e198cba9eab Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 15:46:16 +0000 Subject: [PATCH 4/4] test(spec): type the dim_order helpers so mypy passes Master has failed 'Check types' since #1179. --- test/test_spec_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_spec_api.py b/test/test_spec_api.py index d07df714..55545fde 100644 --- a/test/test_spec_api.py +++ b/test/test_spec_api.py @@ -8,7 +8,7 @@ import io import types import warnings -from collections.abc import Callable +from collections.abc import Callable, Hashable, Mapping from pathlib import Path from typing import Any @@ -244,7 +244,7 @@ def curves(method: str) -> dict[str, Any]: } -def framed(default: tuple[str, ...], order: list[str]) -> tuple[str, ...]: +def framed(default: tuple[Hashable, ...], order: list[str]) -> tuple[Hashable, ...]: """The dims of an entry built as *default*, the ones *order* names moved to the front.""" first = tuple(d for d in order if d in default) return (*first, *(d for d in default if d not in first)) @@ -290,7 +290,7 @@ def test_dim_order_solves_to_the_default_up_to_transposition( default = solved(spec, CURVE_SOURCES, sparse=backend.sparse) ordered = solved(spec, CURVE_SOURCES, sparse=backend.sparse, dim_order=order) assert ordered.objective.value == pytest.approx(default.objective.value) - pairs = [ + pairs: list[tuple[Mapping[Any, xr.DataArray], Mapping[Any, xr.DataArray]]] = [ (default.solution, ordered.solution), (default.dual, ordered.dual), (evaluate(spec, default), evaluate(spec, ordered, dim_order=order)),