Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,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` leaves no row where a per-entity `shift` offset is absent; it read `x[t=0]` with `edge='wrap'` and 0 with `edge=<number>`. ([\#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))
Expand Down
23 changes: 14 additions & 9 deletions linopy/spec/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions test/test_spec_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)),
Expand Down
32 changes: 32 additions & 0 deletions test/test_spec_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}},
Expand Down
Loading