Skip to content
Merged
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 @@ -133,6 +133,7 @@ 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=<number>`. ([\#1185](https://github.com/PyPSA/linopy/pull/1185))
- `Model.from_spec` on a sparse model no longer warns of a dense grid when `at` reads along its own key, a grid the build needs. ([\#1192](https://github.com/PyPSA/linopy/pull/1192))
- `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
6 changes: 5 additions & 1 deletion linopy/spec/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import annotations

import operator
import warnings
from collections.abc import Hashable, Mapping
from dataclasses import dataclass
from functools import reduce
Expand Down Expand Up @@ -96,7 +97,10 @@ def grouped_sum(
if isinstance(picked, LinearExpression) and not picked.is_sparse:
return _gathered(picked, grouped, onto)
keyed = picked.assign_coords({d: (_ROW, labels) for d, labels in grouped.items()})
return keyed.groupby(list(grouped)).sum().reindex(onto).fillna(0)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Grouping a LinearExpression", UserWarning)
summed = keyed.groupby(list(grouped)).sum()
return summed.reindex(onto).fillna(0)


def _gathered(
Expand Down
16 changes: 16 additions & 0 deletions test/test_spec_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,3 +1082,19 @@ def test_a_lookup_into_an_empty_dimension_reads_nothing() -> None:
}
m = Model.from_spec(spec, sources)
assert int((m.constraints["cap"].labels != -1).sum()) == 1


def test_a_grouped_sum_onto_a_mostly_empty_grid_does_not_warn() -> None:
from linopy.spec.operators import grouped_sum

rows = np.arange(200)
onto = {"a": pd.Index(rows, name="a"), "p": pd.Index(range(101), name="p")}
grouped = {"a": rows, "p": rows % 101}
summed = {}
for sparse in (True, False):
x = Model(sparse=sparse).add_variables(coords=[onto["a"]], name="x")
with warnings.catch_warnings():
warnings.simplefilter("error")
summed[sparse] = grouped_sum(1 * x, {"a": rows}, grouped, onto)
assert summed[True].is_sparse
assert_linequal(summed[True], summed[False])
Loading