Skip to content

fix: make assert_linequal term-order invariant#639

Merged
FBumann merged 1 commit intoPyPSA:perf/matrix-accessor-rewritefrom
FBumann:fix/assert-linequal-term-order
Apr 1, 2026
Merged

fix: make assert_linequal term-order invariant#639
FBumann merged 1 commit intoPyPSA:perf/matrix-accessor-rewritefrom
FBumann:fix/assert-linequal-term-order

Conversation

@FBumann
Copy link
Copy Markdown
Collaborator

@FBumann FBumann commented Apr 1, 2026

Summary

  • assert_linequal now sorts both sides by variable labels along _term before comparing, so expressions with different term orderings are correctly recognized as semantically equal.
  • Fixes false failures when comparing constraints that went through the CSR round-trip (freeze_constraints=True) against freshly built expressions, since csr.sum_duplicates() sorts terms by variable label.

Reproducer (from #630 comment)

from linopy import Model
from linopy.testing import assert_linequal
import pandas as pd

m = Model(freeze_constraints=True)
coords = pd.RangeIndex(3, name="time")
x = m.add_variables(coords=[coords], name="x")
y = m.add_variables(coords=[coords], name="y")

m.add_constraints(y - x == 0, name="con")

desired = y == x
assert_linequal(m.constraints["con"].lhs, desired.lhs)  # was failing, now passes

Test plan

  • Reproducer passes
  • All 2665 existing tests pass
  • Linter and pre-commit hooks pass

🤖 Generated with Claude Code

Sort both sides by variable labels along _term before comparing, so
expressions with different term orderings (e.g. from CSR round-trip
with freeze_constraints=True) are correctly recognized as equal.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@FBumann FBumann requested a review from FabianHofmann April 1, 2026 09:51
@FabianHofmann
Copy link
Copy Markdown
Collaborator

@FBumann would you mind raising this pr from a branch on linopy and not on fork? as for now, the CI is not triggered fully

@FBumann FBumann merged commit 4b10d0a into PyPSA:perf/matrix-accessor-rewrite Apr 1, 2026
2 checks passed
@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Apr 1, 2026

@FabianHofmann I merged that by accident.
Ill open a PR. Can you revert it? Or maybe leave it if you find it passing...?

@FabianHofmann
Copy link
Copy Markdown
Collaborator

@FabianHofmann I merged that by accident. Ill open a PR. Can you revert it? Or maybe leave it if you find it passing...?

all good, no problem. not important here. but for prs against master it would be

@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented Apr 1, 2026

@FabianHofmann definitely...

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.

2 participants