Fix coordinate misalignment in expression merge#550
Draft
FBumann wants to merge 11 commits intoPyPSA:masterfrom
Draft
Fix coordinate misalignment in expression merge#550FBumann wants to merge 11 commits intoPyPSA:masterfrom
FBumann wants to merge 11 commits intoPyPSA:masterfrom
Conversation
…ing logic - Mark defensive code paths with pragma: no cover (unreachable in practice)
Contributor
Author
|
Im not entirely sure if this affects performance, but its at least worth raising i thought |
…ion-based approach. For each non-helper dimension, it computes the union of coordinate values across all datasets. If any mismatch is found, all datasets are reindexed to the union coordinates with FILL_VALUE defaults. This handles both the reordered-coords case and the overlapping-subset case. test/test_linear_expression.py: Added test_merge_with_overlapping_coords that creates variables with ["alice", "bob"] and ["bob", "charlie"], merges them, and verifies correct alignment — bob gets both terms, alice and charlie get only their respective term with fill values for the missing one.
…es) and then patching up mismatches with reindexing, _check_coords_match now checks that actual coordinate values and order are identical. When they're not, override is False and xarray's join='outer' handles alignment correctly. The entire reindex block is gone.
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.
Summary
Fixes silent data corruption when adding expressions whose coordinates have the same values but in different order (e.g.
['costs', 'Penalty']vs['Penalty', 'costs']). Previously,merge()used positional concatenation viajoin='override', ignoring labels entirely.What changed
When
merge()detects that two datasets share the same coordinate values in a different order, it now reindexes to the first dataset's order before concatenation. This only triggers when the coordinate value sets are identical — different subsets or different sizes are unaffected.What is NOT affected
v.loc[:9] + v.loc[10:]— different value sets, override preservedBug reproduction script
Benchmark script (
dev-scripts/benchmark_merge_alignment.py)Checklist
doc/release_notes.rstis included