feat(spec): xarray data binder for math-spec programs - #921
feat(spec): xarray data binder for math-spec programs#921FabianHofmann wants to merge 7 commits into
Conversation
Turns a lowered math-spec Program plus user data into master coordinates, padded lookups and on-demand parameter arrays under the three binding rules. Missing rows stay NaN for the builder. Sources are pulled by key, never iterated, and aligned arrays keep their buffer.
…s under the right dimension
Validate retain, check a scalar's dtype before casting, bind empty sources as all-NaN, check label-space lookup dtypes, report unknown labels in source order on every path, re-stamp coordinates onto the master dtype without copying, and pin the remaining binder rules.
Build cost — v1 vs legacyv1 build peak & time relative to legacy, on this commit — not a comparison against master (that is CodSpeed).
Full table (time + peak, mean)📊 Interactive plots + CSV: download the semantics-report-v1-vs-legacy artifact from this run. Report-only · not a gate · refreshed on every push · obsolete once legacy is dropped. |
Merging this PR will degrade performance by 8.46%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ❌ | test_to_lp[expression_arithmetic-n=10] |
718.4 KB | 1,338.9 KB | -46.34% |
| ❌ | test_to_lp[qp-n=1000] |
2 MB | 2.6 MB | -22.48% |
| ⚡ | test_to_lp[storage-n=250] |
37 MB | 29.4 MB | +25.68% |
| ⚡ | test_to_lp[nodal_balance-severity=50] |
3.7 MB | 3.3 MB | +11.62% |
| ⚡ | test_to_lp[expression_arithmetic-n=250] |
45.3 MB | 41.1 MB | +10.15% |
| 🆕 | test_build[nodal_balance_sparse-severity=0] |
N/A | 1.5 MB | N/A |
| 🆕 | test_build[nodal_balance_sparse-severity=100] |
N/A | 1.5 MB | N/A |
| 🆕 | test_build[nodal_balance_sparse-severity=50] |
N/A | 1.5 MB | N/A |
| 🆕 | test_to_lp[nodal_balance_sparse-severity=0] |
N/A | 3.3 MB | N/A |
| 🆕 | test_to_lp[nodal_balance_sparse-severity=100] |
N/A | 3.7 MB | N/A |
| 🆕 | test_to_lp[nodal_balance_sparse-severity=50] |
N/A | 3.1 MB | N/A |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing spec-binder (494becf) with master (6b133d5)2
Footnotes
-
175 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. ↩
-
No successful run was found on
master(b71e9a9) during the generation of this report, so 6b133d5 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩
…ype the test spec dicts
… group math-spec is not on PyPI and needs Python >= 3.12. A PEP 735 dependency group keeps the git pin out of the wheel metadata; the 3.12 and 3.13 test jobs install it so the binder tests and their coverage run in CI.
|
include diff in #922 |
Step 2 of the plan in #919: the data layer that
Model.from_spec/Model.add_specwill build on. No public model API yet;linopy.specis importable only when math-spec is installed andimport linopynever loads it.Note
The following content was generated by AI.
Changes proposed in this Pull Request
linopy/spec/binder.pyturns a loweredmath_spec.Programplus user data into what a builder needs: master coordinates, lookup arrays and on-demand parameter arrays.bind(program, sources, retain=...)returns aBoundwithcoords,lookups,parameter(name)andretained(). Parameters are resolved fromsourceson demand and never cached;retainselects whatretained()hands back for persistence:"report"(default, the parameter closure of the named expressions),"all"or"none".sourcesis anyMappingpulled by key and never iterated, so a lazy view over PyPSA'sn.c.<component>.daaccessor is a valid source. A singlexr.Datasetis accepted too.SpecDataError. A missing row is not an error: it becomes NaN (bool: False) and the builder decides later.reindexruns only on a real mismatch. An aligned array keeps its buffer, asserted withnp.shares_memory. A matching index with a different dtype is re-stamped onto the master dtype without copying.xr.DataArray,pd.Series, tidy or widepd.DataFrame, dict. Lookups: Series, dict or DataArray over the dimension.test_data_parity.pyare ported and give the same verdicts.uv run --with <math-spec checkout> pytest test/test_spec_binder.py.Left for the model integration step: object-dtype string lookups through netcdf, float promotion of partly mapped lookups into integer dimensions, and derived piecewise parameters.
Verification
Checklist
AGENTS.md).doc.doc/release_notes.rstof the upcoming release is included.