Skip to content

Preserve sparse expressions during metadata reads - #1003

Merged
FabianHofmann merged 2 commits into
PyPSA:masterfrom
MaykThewessen:codex/preserve-sparse-pypsa-build
Oct 5, 2026
Merged

FabianHofmann merged 2 commits into
PyPSA:masterfrom
MaykThewessen:codex/preserve-sparse-pypsa-build

Conversation

@MaykThewessen

Copy link
Copy Markdown
Contributor

Note

This pull request was prepared with AI assistance.

A PyPSA network build with Model(sparse=True) densifies CSR expressions while reading has_terms for nodal balance and dropping auxiliary snapshot coordinates for storage. Read term presence from CSR row counts and drop auxiliary coordinates on the coordinate grid instead. Explicit-zero terms and absent cells retain their dense-path semantics; dimension, data-variable and callable-selector operations retain the existing fallback.

Related to #969 and #972. This is separate from #992's handoff, solution-readback and merge optimizations.

Ten newly exercised sparse cases fail before this change and pass afterwards. They cover grouped expressions, auxiliary coordinates, absent cells, scalar grids, missing-coordinate errors and explicit-zero coefficients.

BirdFlow screening benchmark

Three sequential fresh-process repetitions per mode on the first 168 snapshots of an existing BirdFlow network, using the same installed PyPSA and numeric dependencies. This measures core model build and matrix export only, without a solve or BirdFlow extra-functionality. The local network is not included as a public benchmark input.

Mode Median build, s Median matrix export, s Median whole-process peak RSS, GiB
Linopy 0.9.1, dense 1.048 0.120 1.959
Current master, dense 1.063 0.114 1.976
Current master, sparse 1.054 0.031 1.763
This change, sparse 1.028 0.030 1.740

The matrix, objective, bounds and signs have identical fingerprints across these builds. RHS numeric values are also exactly equal, with 19,320 signed-zero representation differences explaining a raw RHS hash difference. Whole-process RSS includes imports, input loading, export and benchmark diagnostics. Build timing differences are small and do not establish a solve speedup.

The two has_terms densifications disappear, as does the auxiliary-coordinate drop. A later, previously hidden fallback remains at the coordinate-dimension merge(..., dim='snapshot', join='outer'); this PR does not implement that merge path.

Test validation and limitations

python -m pytest test -q -n 4 -o addopts='' --junitxml=/tmp/linopy-sparse-full-suite.xml: 5418 passed, 80 failed, 2234 skipped. Exact failure-set comparison found no new failures; the 80 failures also occur on unchanged master in this reused environment. They comprise 74 OETC tests missing optional cloud dependencies and six tests requiring SOS support unavailable through HiGHS.

Ruff and whitespace checks pass. Mypy was not installed. Independent read-only review found no correctness blocker.

Note: AI-assisted (model identity unavailable).

MaykThewessen and others added 2 commits October 4, 2026 21:20
Serve has_terms from CSR rows and drop auxiliary coordinates without materializing terms. Preserve xarray errors and existing fallback behavior for dimensions, data variables, and callable selectors.

Validate with python -m pytest test -q -n 4 -o addopts= --junitxml=/tmp/linopy-sparse-full-suite.xml: 5418 passed,80 unchanged baseline failures,2234 skipped. Ten sparse regressions fail before the fix and pass after it. Ruff passes. A 168h BirdFlow model-build comparison retains exact numerical LP values, with only signed-zero RHS representation differences; raw benchmark artifacts are retained separately.
@codspeed

codspeed Bot commented Oct 4, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing MaykThewessen:codex/preserve-sparse-pypsa-build (fa770b7) with master (1b2ea76)

Open in CodSpeed

Footnotes

  1. 181 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@FabianHofmann

Copy link
Copy Markdown
Collaborator

Thanks @MaykThewessen

@FabianHofmann
FabianHofmann marked this pull request as ready for review October 5, 2026 09:58
@FabianHofmann
FabianHofmann merged commit f665a26 into PyPSA:master Oct 5, 2026
25 checks passed
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