Skip to content

fix(constraints): freeze empty constraint groups without reshape error - #783

Merged
FabianHofmann merged 2 commits into
PyPSA:masterfrom
Ketchp:fix/csr-constraint-empty-rows
Jun 23, 2026
Merged

fix(constraints): freeze empty constraint groups without reshape error#783
FabianHofmann merged 2 commits into
PyPSA:masterfrom
Ketchp:fix/csr-constraint-empty-rows

Conversation

@Ketchp

Copy link
Copy Markdown
Contributor

Note

Code and test in this PR was AI aided.

Problem description:

CSRConstraint.from_mutable reshaped con.vars with an inferred -1 dimension. For an empty constraint group (zero rows and zero terms) the vars array has size 0, and NumPy refuses to infer a (0, -1) reshape, raising "cannot reshape array of size 0 into shape (0,newaxis)".

This is a degenerate edge-case, but can be encountered when slicing or masking is used.

Changes proposed in this Pull Request

Pass the explicit _term count instead of -1; NumPy accepts a (0, 0) reshape and the rest of the method already handles zero-row input.

Minimal reproducible example:

importlinopym=linopy.Model(freeze_constraints=True) # does not work# m = linopy.Model(freeze_constraints=False) # worksx=m.add_variables(coords=[range(3)], dims=["t"], name="x")
m.add_constraints(x.isel(t=range(0)) ==0) # raises: "ValueError: cannot reshape array of size 0 into shape (0,newaxis)"

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.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

CSRConstraint.from_mutable reshaped con.vars with an inferred -1
dimension. For an empty constraint group (zero rows and zero terms) the
vars array has size 0, and NumPy refuses to infer a (0, -1) reshape,
raising "cannot reshape array of size 0 into shape (0,newaxis)". This
broke the documented lossless freeze round-trip for legitimately empty
groups (e.g. shifted-time difference constraints at n_time == 1).
Pass the explicit _term count instead of -1; NumPy accepts a (0, 0)
reshape and the rest of the method already handles zero-row input.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@FabianHofmannFabianHofmann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Ketchp for the quick bug fix. some requests below

Comment threadlinopy/constraints.py Outdated
Comment threadtest/test_constraint.py Outdated

@FabianHofmannFabianHofmann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thanks. pypsa-eur test are failing. I need to take a closer look at this first (not related to your changes), but will merge afterwards

@FabianHofmann
FabianHofmann merged commit 8dfa6d4 into PyPSA:masterJun 23, 2026
23 of 24 checks passed
FabianHofmann added a commit that referenced this pull request Jul 24, 2026
Trace each previously unreferenced entry in the Upcoming Version
section to its introducing PR and append the linked short-ref,
so every bullet is traceable (#718, #566, #780, #783, #790,
#801, #802, #824, #860).
FabianHofmann added a commit that referenced this pull request Jul 24, 2026
* docs: harmonize PR/issue refs in release notes
Convert bare GitHub URLs and mixed RST link forms to a single
anonymous linked short-ref form (`#NNN <url>`__) across the Upcoming
and 0.8.0 sections, linkify plain #580 mentions, and fix a stray
Markdown link in the 0.1.4 notes.
* docs: add PR refs to remaining upcoming release-note entries
Trace each previously unreferenced entry in the Upcoming Version
section to its introducing PR and append the linked short-ref,
so every bullet is traceable (#718, #566, #780, #783, #790,
#801, #802, #824, #860).
Sign up for freeto 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

@Ketchp@FabianHofmann