Skip to content

Commit ee62dcc

Browse files
refac: introduce consistent convention for linopy operations with subsets and supersets (#572)
* refac: introduce consistent convention for linopy operations with subsets and supersets * move scalar addition to add_constant * add overwriting logic to add constant * add join parameter to control alignment in operations * Add le, ge, eq methods with join parameter for constraints Add le(), ge(), eq() methods to LinearExpression and Variable classes, mirroring the pattern of add/sub/mul/div methods. These methods support the join parameter for flexible coordinate alignment when creating constraints. * Extract constant alignment logic into _align_constant helper Consolidate repetitive alignment handling in _add_constant and _apply_constant_op into a single _align_constant method. This eliminates code duplication and makes the alignment behavior (handling join parameter, fill_value, size-aware defaults) testable and maintainable in one place. * update notebooks * update release notes * fix types * add regression test * fix numpy array dim mismatch in constraints and add RHS dim tests numpy_to_dataarray no longer inflates ndim beyond arr.ndim, fixing lower-dim numpy arrays as constraint RHS. Also reject higher-dim constant arrays (numpy/pandas) consistently with DataArray behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * remove pandas reindexing warning * Fix mypy errors: type ignores for xr.align/merge, match override signature, add test type hints * remove outdated warning tests * reintroduce expansions of extra rhs dims, fix multiindex alignment * refactor test fixtures and use sign constants * add tests for pandas series subset/superset * test: add TestMissingValues for same-shape constants with NaN entries * Fix broken test imports, stray docstring char, and incorrect test assertion from fixture refactor * Fill NaN with neutral elements in expression arithmetic, preserve NaN as 'no constraint' in RHS - Fill NaN with 0 (add/sub) or fill_value (mul/div) in _add_constant/_apply_constant_op - Fill NaN coefficients with 0 in Variable.to_linexpr - Restore NaN mask in to_constraint() so subset RHS still signals unconstrained positions * Fix CI doctest collection by deferring linopy import in test/conftest.py --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4f999c8 commit ee62dcc

18 files changed

+2032
-233
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ benchmark/scripts/leftovers/
5050
# direnv
5151
.envrc
5252
AGENTS.md
53+
coverage.xml

CLAUDE.md

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,6 @@ When modifying the codebase, maintain consistency with these patterns and ensure
110110
* Always create a feature branch for new features or bug fixes.
111111
* Use the github cli (gh) to interact with the Github repository.
112112

113-
### GitHub Claude Code Integration
114-
115-
This repository includes Claude Code GitHub Actions for automated assistance:
116-
117-
1. **Automated PR Reviews** (`claude-code-review.yml`):
118-
- Automatically reviews PRs only when first created (opened)
119-
- Subsequent reviews require manual `@claude` mention
120-
- Focuses on Python best practices, xarray patterns, and optimization correctness
121-
- Can run tests and linting as part of the review
122-
- **Skip initial review by**: Adding `[skip-review]` or `[WIP]` to PR title, or using draft PRs
123-
124-
2. **Manual Claude Assistance** (`claude.yml`):
125-
- Trigger by mentioning `@claude` in any:
126-
- Issue comments
127-
- Pull request comments
128-
- Pull request reviews
129-
- New issue body or title
130-
- Claude can help with bug fixes, feature implementation, code explanations, etc.
131-
132-
**Note**: Both workflows require the `ANTHROPIC_API_KEY` secret to be configured in the repository settings.
133-
134113

135114
## Development Guidelines
136115

@@ -140,3 +119,4 @@ This repository includes Claude Code GitHub Actions for automated assistance:
140119
4. Use type hints and mypy for type checking.
141120
5. Always write tests into the `test` directory, following the naming convention `test_*.py`.
142121
6. Always write temporary and non git-tracked code in the `dev-scripts` directory.
122+
7. In test scripts use linopy assertions from the testing.py module where useful (assert_linequal, assert_varequal, etc.)

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ This package is published under MIT license.
111111
creating-variables
112112
creating-expressions
113113
creating-constraints
114+
coordinate-alignment
114115
sos-constraints
115116
piecewise-linear-constraints
116117
piecewise-linear-constraints-tutorial

doc/release_notes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Release Notes
44
Upcoming Version
55
----------------
66

7+
* Harmonize coordinate alignment for operations with subset/superset objects:
8+
- Multiplication and division fill missing coords with 0 (variable doesn't participate)
9+
- Addition and subtraction of constants fill missing coords with 0 (identity element) and pin result to LHS coords
10+
- Comparison operators (``==``, ``<=``, ``>=``) fill missing RHS coords with NaN (no constraint created)
11+
- Fixes crash on ``subset + var`` / ``subset + expr`` reverse addition
12+
- Fixes superset DataArrays expanding result coords beyond the variable's coordinate space
713
* Add ``add_piecewise_constraints()`` with SOS2, incremental, LP, and disjunctive formulations (``linopy.piecewise(x, x_pts, y_pts) == y``).
814
* Add ``linopy.piecewise()`` to create piecewise linear function descriptors (`PiecewiseExpression`) from separate x/y breakpoint arrays.
915
* Add ``linopy.breakpoints()`` factory for convenient breakpoint construction from lists, Series, DataFrames, DataArrays, or dicts. Supports slopes mode.

0 commit comments

Comments
 (0)