Skip to content

refactor: move as_dataarray_in_coords to common.py (#723 part 1)#724

Closed
FBumann wants to merge 1 commit into
fix/bounds-coords-broadcastfrom
refactor/as-dataarray-in-coords
Closed

refactor: move as_dataarray_in_coords to common.py (#723 part 1)#724
FBumann wants to merge 1 commit into
fix/bounds-coords-broadcastfrom
refactor/as-dataarray-in-coords

Conversation

@FBumann
Copy link
Copy Markdown
Collaborator

@FBumann FBumann commented May 23, 2026

First slice of #723. Stacked on top of #722 — set base to fix/bounds-coords-broadcast; rebase / change base to master once #722 merges.

What changes

Pure relocation, no behavior change.

  • _as_dataarray_in_coords and its two helpers (_coords_to_dict, _named_pandas_to_dataarray) move from linopy/model.py to linopy/common.py, alongside the as_dataarray they parallel.
  • The main helper is renamed as_dataarray_in_coords (no leading underscore) so other modules can import it when migrating call sites.
  • add_variables imports + uses the relocated function; no other call sites change in this PR.

Diff is 134 / -132 lines, almost all of which is the relocation.

Audit of remaining as_dataarray(arr, coords=...) call sites

Not migrated in this PR. Per-site notes:

model.py

  • add_constraints: as_dataarray(sign) and as_dataarray(rhs) pass no coords — strict semantics don't apply. No migration.
  • add_variables / add_constraints mask: as_dataarray(mask, coords=data.coords, dims=data.dims).astype(bool) followed by broadcast_mask(mask, data.labels). The strict helper would handle extra-dim and expand_dims steps, but broadcast_mask also emits a FutureWarning for sparse-coord NaNs. Migration needs that warning path threaded through (or moved into the helper). Deferred — follow-up commit.

expressions.py (lines 584, 613, 1105, 1668, 2154)

  • Arithmetic ops pass coords=self.coords, dims=self.coord_dims to label scalar/array inputs, then call _align_constant for the actual alignment with join/reindex. The strict helper would raise on inputs carrying an extra dim that _align_constant would otherwise broadcast across. Higher risk — defer until we have a test that pins the desired behavior for LinearExpression + DataArray_with_extra_dim.

variables.py (line 330)

  • Same shape as the expression arithmetic sites. Deferred with them.

Follow-up scope

A second PR after this lands:

  1. Migrate the mask paths (move the broadcast_mask NaN-warning logic into as_dataarray_in_coords or compose them explicitly).
  2. Decide whether expression arithmetic should validate against self.coords. Likely needs a tightened semantic in the v1 convention work (feat: v1 semantic convention #717) rather than an unannounced behavior change here.
  3. Benchmark add_variables and add_constraints on a representative model (PyPSA earth?) and compare before/after.

Test plan

  • test/test_variable.py — 54/54 pass (incl. the MultiIndex + reindex coverage from fix: broadcast pandas/DataArray bounds in coords and preserve dim order #722)
  • Broader sweep: test/test_model.py, test/test_linear_expression.py, test/test_constraint.py, test/test_constraints.py, test/test_piecewise_constraints.py, test/test_variable_assignment.py, test/test_variables.py, test/test_quadratic_expression.py, test/test_scalar_*.py, test/test_sos_constraints.py — 838 pass / 4 skipped, no regressions.
  • Pre-commit (ruff/format/blackdoc/codespell) clean.

🤖 Generated with Claude Code

Relocate `_as_dataarray_in_coords` and its helpers
(`_coords_to_dict`, `_named_pandas_to_dataarray`) from `model.py`
into `common.py`, alongside the existing `as_dataarray` they
parallel. Rename to `as_dataarray_in_coords` (no leading underscore)
since it is no longer file-local — other modules can import the
strict-coords variant when migrating call sites.

Pure relocation: no behavior change, no call-site changes beyond
`add_variables`'s import. Refs #723.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented May 24, 2026

Folded into #722 — the relocation + the cleaner stack(level=…) form for _named_pandas_to_dataarray are now part of that PR (commits bca89e7 and b28f3df). Closing as superseded.

@FBumann FBumann closed this May 24, 2026
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.

1 participant