Skip to content

fix(spec): a window sum over absent terms is 0, like sum - #1174

Merged
FabianHofmann merged 3 commits into
masterfrom
fix/1172-window-sum-of-absent
Nov 18, 2026
Merged

FabianHofmann merged 3 commits into
masterfrom
fix/1172-window-sum-of-absent

Conversation

@FabianHofmann

@FabianHofmann FabianHofmann commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1172.

Changes proposed in this Pull Request

Note

The following content was generated by AI.

  • sum_back now sums like sum: each absent term counts as 0, so a defined window of only absent terms is 0 and the row stays. The issue's example builds y[0] <= 0 and y[1] <= 0.
  • A window that is not defined keeps no row, as on master: a member that within= places in no group, or a member whose per-entity width is absent. shift under the same partition leaves that row absent too. A width of 0 is a defined, empty window and gives 0.
  • Behaviour change: a row whose only variable terms sit in an all-absent window used to be dropped silently. It now holds no variable term, so it is refused with SpecDataError ("hold no variable term"), as sum(x, over=t) <= 4 already is on master.
  • A where: that reads sum_back over an absent parameter now sees the sum of the present members (0 for an all-absent window), so the condition can flip from false to true.
  • Unchanged: a partly absent window, the edge at the axis start, and shift without edge=.

Changed in this revision (after review):

  • sum_back masks the result where the window is not defined (partition.grouped & within.notnull()). The two existing-test edits that encoded 0 for those cases are reverted.
  • The deleted sum-back builder case is now a test of the refusal.
  • New test for a where: reading sum_back over an absent parameter.
  • Release note and user-guide sentence name both effects and the undefined-window rule.
Validation and reviewer notes

Fix. operators.sum_back fills each lag with 0 through the shared _filled, as sum_over does, and no longer masks by "any lag reached". It masks only where the window is not defined: where the within= partition holds no group, and where the width is absent.

Tests.

  • test_spec_operators.py::test_a_defined_window_of_absent_terms_sums_to_zero_like_sum: x is masked to t ∈ {0, 3}. Parametrized over the four sum_back variants (plain, edge='wrap', within=, per-group width), plus three cases: a width of 0 gives 0, a member in no group keeps no row, an absent width keeps no row. Checks the built rows, the solved values and the folded named expression. All 7 cases fail on master. The 2 undefined-window cases fail on the previous PR head.
  • test_spec_builder.py::test_a_row_of_nothing_but_an_absent_window_is_refused_like_sum: sum_back(a * x, along=t, window=2) <= 4 with a absent at t=0 raises SpecDataError. Replaces the sum-back case removed from test_an_absent_row_masks_the_row_it_reaches. Fails on master.
  • test_spec_builder.py::test_a_where_reads_a_window_of_absent_rows_as_the_sum_of_the_rest: where: sum_back(p, along=t, window=2) <= 1 with p absent at t=0 keeps x[0]. Fails on master.
  • test_a_member_a_relation_sends_nowhere_reaches_nothing[sum-back-in-groups] and test_a_window_width_no_member_carries_is_a_window_of_nothing are back to their master form.

Compatibility with 1ea671d (spec/prune-unbuilt): that commit turns the refusal into an EmptiedRowWarning and drop where the row holds. This PR changes only sum_back, not the emptied-row check, so the two compose.

Hole check. evaluator._marks already fills the window operand and says absence does not spread "out of a sum or a window". It now agrees with the build.

Separate issue, not fixed here. An absent per-entity shift offset is cast from NaN to int (operators.py, source = (ordinal - offset).astype(int)). Under edge='wrap' every position then reads position 0; under edge=0 the row gets the fill instead of staying absent. This is the same on master and needs its own issue.

Checks run locally (Python 3.13): pytest test/test_spec*.py 573 passed, 6 skipped; ruff check . and ruff format --check . (v0.16.10) clean; mypy linopy clean. The issue's reproducer and the review's scenarios give the expected rows dense and with sparse=True.

Checklist

  • AI-generated content is marked (see AGENTS.md).
  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release-notes.md of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Generated by Claude Code

sum_back left a row absent where every term of its window was absent.
sum over the same terms gives 0. A window now fills each absent term
with 0 and sums, as sum does, so the row stays.

This also holds where the window reaches nothing: a member a partition
places nowhere, or a width no member carries. A shift without edge=
still leaves its row absent.
@FabianHofmann
FabianHofmann force-pushed the fix/1172-window-sum-of-absent branch from 7e2bf7c to 80b3e7e Compare November 18, 2026 10:03
@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing fix/1172-window-sum-of-absent (078a3d3) with master (61583ef)2

Open in CodSpeed

Footnotes

  1. 181 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on master (8dce76b) during the generation of this report, so 61583ef was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

A window sum over absent terms stays 0, like sum.
A member that within= places in no group has no window.
A member whose width is absent has no window either.
Both keep no row, as on master and as shift does.
A width of 0 is a defined, empty window and gives 0.
Test the refusal of a row left with no variable term.
Test a where that reads sum_back over an absent parameter.
@FabianHofmann
FabianHofmann marked this pull request as ready for review November 18, 2026 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Model.from_spec: sum_back over all-absent inputs drops the row, while sum gives 0

2 participants