Round trip spec-built models through netcdf and benchmark from_spec - #923
Merged
Conversation
Persist the spec text, the master coordinates and the lookups alongside the model, re-lowering the program from the text on read; math-spec is imported only for a file that carries a spec. Lookups and arrays of labels are stored as codes into a category table, so partial maps keep their holes and dtypes.
Write the in-memory dtype of every parameter and cast it back on read, and stamp the master coordinates onto every container, so no engine leaves a model disagreeing with itself. assert_model_equal now compares dataset dtypes, and synthetic_sources moves to linopy/spec/testing.py for both users.
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. |
Collaborator
Author
|
Folded into #922 (fast-forwarded |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Step 4 of the plan in #919, stacked on #922: a spec-built model survives
to_netcdf/read_netcdfandModel.copy(), andModel.from_specgets a codspeed benchmark.Note
The following content was generated by AI.
Changes proposed in this Pull Request
to_netcdfwrites the spec YAML text as the top-level attribute_linopy_spec, next to_linopy_version, following the relaxed-registry and piecewise-formulation precedent.read_netcdfre-lowers theProgramfrom that text; a file without a spec loads without math-spec installed.spec-carries whatmodel.parameterscannot round-trip on its own: master coordinates ascoords__<dim>data variables (an index coordinate no retained parameter reaches would otherwise be dropped on read), and lookups plus every string-kind parameter aspandas.factorizeoutput,codes__<name>(int32, -1 for a hole) pluscats__<name>with the in-memory dtype in a variable attribute. No netcdf type carries an object array of strings with NaN: netCDF4 returns<Uand turns a hole into an empty string, scipy narrows int64 to int32. Codes plus categories round-trip str->str, str->int, int->str and int->int lookups, full and partial, exactly on both engines. This resolves the two lookup items left open in feat(spec): xarray data binder for math-spec programs #921.Model.copy()carries the spec; the accessor is rebound to the copy. On read, every parameter is cast back to its in-memory dtype and the master coordinates are stamped onto all containers, so one dimension has one dtype even after the scipy engine narrows int64 to int32.assert_model_equalnow compares parameters with dtypes.linopy/spec/testing.pyholds thesynthetic_sourcesgenerator shared by tests and benchmark.assert_model_equalcompares the spec text; parameters, coordinates and lookups are already covered throughmodel.parameters.test/test_spec_io.py: round trips forretain="report"|"all"|"none"on both engines, a solved model whose named expression is identical before and after,evaluateon a restoredretain="none"model, exact-dtype assertions per lookup kind, copy semantics, and math-spec'sexamples/pypsa.yamlwith synthetic data whenMATH_SPEC_EXAMPLESpoints at a math-spec checkout.benchmarks/models/spec_pypsa.py:from_speconexamples/pypsa.yamlplus the two netcdf phases; skips without math-spec orMATH_SPEC_EXAMPLES. Running it in CI needs math-spec installed and the examples directory available in the codspeed workflow, which waits for a math-spec PyPI release.Verification
Checklist
AGENTS.md).doc.doc/release_notes.rstof the upcoming release is included.