Skip to content

Migrate to linopy 0.9 - #191

Merged
edwardxtg merged 2 commits into
mainfrom
linopy-0.7-upgrade
Jul 28, 2026
Merged

edwardxtg merged 2 commits into
mainfrom
linopy-0.7-upgrade

Conversation

@edwardxtg

@edwardxtg edwardxtg commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

What

Moves tz-osemosys from linopy==0.5.5 to linopy>=0.9,<0.10 (latest linopy). This is the cross-repo prerequisite for the tz-mod PyPSA v1 / linopy 0.9 upgrade.

Target updated from linopy 0.7 → 0.9: 0.7.x had a single release and is already superseded (0.8, 0.9); pypsa's development branch already requires linopy>=0.8.0, so 0.7 would be an immediate dead-end. Going to 0.9 adds only the add_variables coords change below.

Changes

  • pyproject.toml: linopy==0.5.5linopy>=0.9,<0.10.
  • .sum(dims=…).sum(dim=…) — 35 shipping-code sites + 1 in bin/memory_profile.py (linopy aligned the kwarg with xarray). The xr.DataArray(…, dims="YRTS") constructor in constraints/storage.py is intentionally unchanged.
  • add_variables coords (linopy 0.8+) — linopy no longer accepts xarray DataArray coord entries; it requires a pd.Index. Changed ds.coords[...]ds.indexes[...] in the coordinate lists in variables/{activity,capacity,storage}.py (6 lists, 3 files).
  • Trade solve tests — linopy changed the solved NetTrade solution dim order to (REGION, TIMESLICE, FUEL, YEAR), so the old positional .values[0][2][0][0] walked off an axis (IndexError). test_simple_trade / test_simple_trade_forced_min_activity now use order-independent labelled access: .sel(REGION="R1", YEAR=2022).item().

Verification

Full suite on linopy 0.9.0: 90 passed, 2 skipped. (Also verified green on 0.7.0 earlier; the mask= breakage anticipated for 0.8+ did not materialise.)

Notes

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates tz-osemosys to support linopy 0.7 by bumping the dependency pin and applying the corresponding API change for summation across dimensions, plus making trade-solve tests robust to the new solution dimension ordering.

Changes:

  • Bump dependency from linopy==0.5.5 to linopy>=0.7,<0.8.
  • Replace .sum(dims=...) with .sum(dim=...) across model expressions/constraints and memory profiling script.
  • Update trade solve assertions to use labelled xarray access (.sel(...).item()) instead of positional .values[...] indexing.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated no comments.

Show a summary per file
File Description
pyproject.toml Updates the linopy dependency constraint to >=0.7,<0.8.
tz/osemosys/model/objective.py Switches objective expression aggregation to .sum(dim=...) for linopy 0.7 compatibility.
tz/osemosys/model/linear_expressions/activity.py Updates .sum(...) kwarg usage in activity linear expressions.
tz/osemosys/model/linear_expressions/emissions.py Updates emissions expression reductions to .sum(dim=...).
tz/osemosys/model/linear_expressions/financials.py Updates financials expression reductions to .sum(dim=...).
tz/osemosys/model/linear_expressions/production.py Updates production/use expression reductions to .sum(dim=...).
tz/osemosys/model/linear_expressions/re_production.py Updates RE production expression reductions to .sum(dim=...).
tz/osemosys/model/linear_expressions/regiongroup.py Updates region-group expression reductions to .sum(dim=...).
tz/osemosys/model/linear_expressions/reserve_margin.py Updates reserve margin expression reductions to .sum(dim=...).
tz/osemosys/model/constraints/capacity_adequacy_b.py Updates constraint expression reductions to .sum(dim=...).
tz/osemosys/model/constraints/re_targets.py Updates production target constraint reductions to .sum(dim=...).
tz/osemosys/model/constraints/storage.py Updates storage balance constraint reduction to .sum(dim=...).
tests/test_solve/test_solve.py Makes NetTrade assertions order-independent via .sel(...).item().
bin/memory_profile.py Updates memory profiling objective aggregation to .sum(dim=...).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 14 changed files in this pull request and generated no new comments.

Bumps the linopy pin to `>=0.9,<0.10` and adapts the model code and trade
solve tests to the linopy 0.7–0.9 API changes:

- pyproject.toml: `linopy==0.5.5` -> `linopy>=0.9,<0.10`.
- `.sum(dims=...)` -> `.sum(dim=...)`: 35 shipping-code sites + one in
  `bin/memory_profile.py` (linopy aligned the kwarg with xarray; `dims=` is
  a hard error in newer versions).
- `add_variables` coords: linopy 0.8+ no longer accepts xarray DataArray
  coords entries; pass a pd.Index instead. Changed `ds.coords[...]` ->
  `ds.indexes[...]` in the coordinate lists in
  variables/{activity,capacity,storage}.py. The
  `xr.DataArray(..., dims="YRTS")` constructor in constraints/storage.py is
  left as-is (not a coords list).
- Trade tests: linopy changed the solved `NetTrade` solution dim order to
  (REGION, TIMESLICE, FUEL, YEAR), breaking positional `.values[0][2][0][0]`
  access. test_simple_trade / test_simple_trade_forced_min_activity now use
  order-independent `.sel(REGION="R1", YEAR=2022).item()`.

Full suite green on linopy 0.9.0: 90 passed, 2 skipped.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@edwardxtg
edwardxtg force-pushed the linopy-0.7-upgrade branch from fb564c3 to ad29511 Compare July 27, 2026 10:59
@edwardxtg edwardxtg changed the title Migrate to linopy 0.7 Migrate to linopy 0.9 Jul 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 17 changed files in this pull request and generated no new comments.

@edwardxtg
edwardxtg requested review from djwels and joel1120 July 27, 2026 11:23
@edwardxtg
edwardxtg merged commit ad452ff into main Jul 28, 2026
4 checks passed
edwardxtg pushed a commit that referenced this pull request Sep 1, 2026
Resolves the conflict in capacity_adequacy_b.py: keeps this branch's RHS
(sub-annual CapacityFactor removed from CAb1_PlannedMaintenance) with
main's renamed linopy kwarg from #191 (.sum(dims=) -> .sum(dim=)).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

This branch was previously deployed

1 inactive deployment
cicd 858668b3 Deployed Jul 27, 2026 by edwardxtg via run-tests #526
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.

3 participants