Skip to content

feat(spec): dim_order sets the axis order of a spec build - #1179

Merged
FabianHofmann merged 2 commits into
masterfrom
spec-dim-order
Nov 18, 2026
Merged

FabianHofmann merged 2 commits into
masterfrom
spec-dim-order

Conversation

@FabianHofmann

Copy link
Copy Markdown
Collaborator

Closes item 4 of PyPSA/PyPSA#2139.

Note

The following content was generated by AI.

Changes proposed in this Pull Request

Model.from_spec(spec, sources, dim_order=None) sets the axis order of every variable, constraint and named expression a spec builds. PyPSA can then ask for (store, snapshot) directly, instead of relying on capital-letter dim names to sort first (PyPSA/PyPSA#2173).

Rule. The dims dim_order names come first, in its order. The rest keep the layout None gives: name order, except that a dim only the data side of a constraint carries (e.g. the breakpoint dim of piecewise domain_lo/hi rows) comes last, as on master. A name an entry lacks is ignored. None builds exactly what master builds.

How. One helper, Context.frame(dims), used wherever declared dims set a layout: variable coords, _framed (constraints and named expressions), fold, and the NaN dual of a skipped constraint. The order is set before labels are allocated; nothing is relabelled or transposed after the build. If dim_order names a dim the variable side of a row lacks, _framed spreads the term onto it first; sparse stays sparse. SOS sets and piecewise expansions follow their variables and rows.

linopy.spec.evaluate takes the same dim_order, as it does rename. A str or a repeated name is refused.

Tests (test/test_spec_api.py, dense and sparse): layout of every entry for the lp, sos2 and incremental piecewise methods; a dim_order that agrees with name order gives a model equal to the default; solution, duals and folded expressions equal the default up to transposition.

Build time is within noise. Median of 15 alternating builds:

spec size default dim_order
commitment.yaml, ["snapshot"] 2.0M vars, 3.0M cons 271 ms 269 ms
piecewise_lp.yaml, ["snapshot", "bp"] (spreads bp) 7.2k vars, 220k cons 73 ms 73 ms
Verification details
  • Full suite: 6367 passed. Two Gurobi failures (test_quadratic_model_wo_constraint[gurobi-mps-*], MPS read error) also fail on master.
  • With MATHSPEC_EXAMPLES set: 644 spec tests pass.
  • Every mathspec example (pinned commit), built with dim_order = all its dims in reverse name order, solved and compared with the default: layout rule holds; objective, solution and folded expressions equal up to transposition. 28 pass; the 2 the suite already marks as refused on synthetic data are skipped.
  • pypsa.yaml was not benchmarked: synthetic data breaks several of its assumptions.

All benchmark rows:

commitment.yaml n=100:  20k vars,   30k cons   default 39.9 ms   dim_order 39.9 ms
commitment.yaml n=300:  180k vars,  270k cons  default 57.9 ms   dim_order 57.1 ms
commitment.yaml n=1000: 2.0M vars,  3.0M cons  default 271.4 ms  dim_order 268.6 ms
piecewise_lp.yaml n=30, [snapshot]:     default 57.9 ms  dim_order 59.1 ms
piecewise_lp.yaml n=30, [snapshot, bp]: default 57.5 ms  dim_order 59.2 ms
piecewise_lp.yaml n=60, [snapshot]:     default 70.9 ms  dim_order 70.8 ms
piecewise_lp.yaml n=60, [snapshot, bp]: default 72.6 ms  dim_order 73.1 ms

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

Generated by Claude Code

The dims dim_order names come first in every variable, constraint and
named expression the spec builds, before labels are allocated; the rest
keep today's layout. None keeps the name order exactly.
@FabianHofmann
FabianHofmann marked this pull request as ready for review November 18, 2026 12:29
@FabianHofmann
FabianHofmann merged commit ac3eaf7 into master Nov 18, 2026
18 of 20 checks passed
@FabianHofmann
FabianHofmann deleted the spec-dim-order branch November 18, 2026 12:30
@codspeed

codspeed Bot commented Nov 18, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 note

ℹ️ 181 benchmarks have no flamegraph

The flamegraph wasn't generated: the symbols of the profiling trace could not be resolved, because no information about the language frames was collected.

See affected benchmarks

✅ 181 untouched benchmarks
⏩ 181 skipped benchmarks1


Comparing spec-dim-order (2abf495) with master (b8bb45e)

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 pushed a commit to PyPSA/PyPSA that referenced this pull request Nov 18, 2026
… dim_order

linopy's dim_order (PyPSA/linopy#1179) puts each component's unit dim
first in every entry. Before, the order relied on the capitalised unit
dim names sorting before lower-case dims. The layout is unchanged.
FabianHofmann pushed a commit that referenced this pull request Nov 18, 2026
Master has failed 'Check types' since #1179.
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