Repository navigation
fix(spec): check otherwise only where no case applies - #1163
Merged
Merged
Conversation
A case value, and the otherwise value, was evaluated over every row and masked to its region only afterwards, so the finiteness check fired at rows another case claims (e.g. `cap * pu` where a case guards `cap == inf`). Each case value is now evaluated with the context's `rows` narrowed to its region, the mechanism that already scopes the check to a constraint's built rows, both when building and when reading the data under an unbuilt sum over an empty dimension. Fixes #1148 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F4nx8L63c3o8u6QQvr6TGN
Merging this PR will not alter performance1 note
Comparing Footnotes
|
Collaborator
Author
|
Note The following content was generated by AI. Two things this PR does not cover yet. Both are on branch
Generated by Claude Code |
…s elsewhere The rows a constraint's where keeps narrow the finiteness checks of what it reads. A sum, a window, a shift and a lookup read their operand at other coordinates along the axes they move. So the narrowing is widened along those axes to every row there. It stays along the other axes, where they read their own coordinate. Before, where: b > 0 with x <= shift(a / b, edge=0) built x[1] <= inf without a word. The widening reads only ctx.rows, so it applies to any narrowing.
4 of 5 tasks
…o ccr-1868f888-l1tlxg # Conflicts: # doc/release-notes.md # linopy/spec/evaluator.py
A case whose region holds no row is not evaluated, and its uses are consumed unread. A zero over the dimensions of the cases stands in for it, a term when the cases carry a variable. Its data is no longer checked, so a window wider than its axis there no longer raises. Tests pin that a shift or window in a case value is still refused where it reads a non-finite value, and cover the fold path. The release note is one line naming this PR.
FabianHofmann
changed the base branch from
master
to
fix/1171-guard-inside-shift
November 18, 2026 10:12
…d condition reads elsewhere A shift, an at() and a count of a condition read their operand at other coordinates, as their expression forms do. So the rows a where keeps are widened along the axes they move, the same way. Before, where: b > 0 with a case on shift(a / b > 0.1, along=t, offset=1) read the NaN of a / b at t=0 as false and built row t=1 without a word. The widening moves to context.py, so the evaluator and the conditions share it.
… keeps A guard over t and s keeps column p out of a shift and a lookup along t, so dropping the guard fails. The lookup keyed by t and s also fails when it widens along the key column it carries. A shift summed over an empty dimension and a where: t > 1 around a shift are refused, the second as mathspec's literal rule asks.
…o ccr-1868f888-l1tlxg # Conflicts: # doc/release-notes.md # linopy/spec/evaluator.py
FabianHofmann
marked this pull request as ready for review
November 18, 2026 10:54
# Conflicts: # doc/release-notes.md # linopy/spec/evaluator.py
3 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1148.
Changes proposed in this Pull Request
Note
The following content was generated by AI.
#1173 is merged; this PR now targets master.
Model.from_specevaluated each case value,otherwise:included, on every row and masked it to its region only afterwards. The finiteness check fired in that first evaluation, so a guard likecap == infcould not stopotherwise: cap * pufrom being refused att=0(inf * 0).linopy/spec/evaluator.py: each case value is evaluated underrowsnarrowed to its region (_narrowed). With fix(spec): a where does not guard what a shift, window or lookup reads elsewhere #1173,widened(inlinopy/spec/context.py) widens that narrowing again along the axis ashift, window,sumoratmoves. So a value read from another coordinate is still checked there.memo.skipped). A zero over the dimensions of the cases stands in for it, as a term when the cases carry a variable._data_undertakes the cases' path through_regionsand returns early;_data_ofevaluates one operand.test/test_spec_builder.py: the case value is refused where a shift or window readsinf * 0fromt=1(3 new parameters); a nowhere case with a window wider than its axis builds; a nowhere case over a dimension the others lack is zero there; the fold path (linopy.spec.evaluate) folds a guarded expression.doc/release-notes.md: one line.Taken from
135609f(spec/prune-unbuilt):_regionswith the skip viamemo.skipped, the zero fill, the early return in_data_underwith_data_of, and the name_narrowed. Not taken: its "nowhere" test (it also passes without the skip) and its duplicate fixture. Changed: its fill returned data for cases that carry a variable when the variable case was skipped, which crashed the build ('DataArray' object has no attribute 'coord_dims'). Here such a value becomes a constant term, in a build only.The repro from the issue builds with
x[0] <= 1andx[1] <= 5.Validation and reviewer notes
pytest test/test_spec*.py: 584 passed, 6 skipped.ruff check .,ruff format --check .,mypy linopy: clean.5c7ace3(the row att=1was silently dropped, orx[1] <= 4).KeyError: 's') and without the term conversion.rows.any()), notctx.rows & region, so the memo never holds a value that depends on the context.memo.maskedrule). A guardedCasesis therefore rebuilt at each use. This costs time, not correctness.linopy.spec.evaluate),ctx.rowsis None andctx.solvedis set, so only the region narrows and no constant term is made.Checklist
AGENTS.md).doc.doc/release-notes.mdof the upcoming release is included.Generated by Claude Code