refactor: move as_dataarray_in_coords to common.py (#723 part 1)#724
Closed
FBumann wants to merge 1 commit into
Closed
refactor: move as_dataarray_in_coords to common.py (#723 part 1)#724FBumann wants to merge 1 commit into
FBumann wants to merge 1 commit into
Conversation
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>
Collaborator
Author
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.
First slice of #723. Stacked on top of #722 — set base to
fix/bounds-coords-broadcast; rebase / change base tomasteronce #722 merges.What changes
Pure relocation, no behavior change.
_as_dataarray_in_coordsand its two helpers (_coords_to_dict,_named_pandas_to_dataarray) move fromlinopy/model.pytolinopy/common.py, alongside theas_dataarraythey parallel.as_dataarray_in_coords(no leading underscore) so other modules can import it when migrating call sites.add_variablesimports + 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 sitesNot migrated in this PR. Per-site notes:
model.py
add_constraints:as_dataarray(sign)andas_dataarray(rhs)pass nocoords— strict semantics don't apply. No migration.add_variables/add_constraintsmask:as_dataarray(mask, coords=data.coords, dims=data.dims).astype(bool)followed bybroadcast_mask(mask, data.labels). The strict helper would handle extra-dim and expand_dims steps, butbroadcast_maskalso emits aFutureWarningfor 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)
coords=self.coords, dims=self.coord_dimsto label scalar/array inputs, then call_align_constantfor the actual alignment with join/reindex. The strict helper would raise on inputs carrying an extra dim that_align_constantwould otherwise broadcast across. Higher risk — defer until we have a test that pins the desired behavior forLinearExpression + DataArray_with_extra_dim.variables.py (line 330)
Follow-up scope
A second PR after this lands:
broadcast_maskNaN-warning logic intoas_dataarray_in_coordsor compose them explicitly).self.coords. Likely needs a tightened semantic in the v1 convention work (feat: v1 semantic convention #717) rather than an unannounced behavior change here.add_variablesandadd_constraintson 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)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.🤖 Generated with Claude Code