Fix weights_input alignment in conservative regridding#188
Merged
Conversation
`na.regridding.weights` (and `transpose_weights_conservative`) aligned the
`weights_input` array to the *output* grid shape before handing it to the
low-level `regridding` kernels. Those kernels broadcast `weights_input` to
the *input* cell shape (`_weights_conservative.py` and
`_weights_transposed.py` both `np.broadcast_to(weights_input, shape_input)`)
and apply it per input cell, matching the docstring ("applied to the values
of the input grid").
The mismatch only surfaced when `weights_input` was non-scalar and the input
and output axes had different names: aligning to `shape_output` raised a
`ValueError` because the input axes were absent from the output shape. A
scalar `weights_input` (the only case previously exercised) broadcasts to any
shape and hid the bug.
Align `weights_input` to `shape_input` instead, and extend
`test_regrid_conservative_2d` with a non-scalar `weights_input` over distinct
output axes, asserting equivalence to folding the weights into the input
values before regridding.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0165qZTJA55LS3CwGPVhrfoX
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #188 +/- ##
=======================================
Coverage 97.24% 97.24%
=======================================
Files 105 105
Lines 14210 14214 +4
=======================================
+ Hits 13819 13823 +4
Misses 391 391
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
na.regridding.weights(andtranspose_weights_conservative) aligned theweights_inputarray to the output grid shape before passing it to the low-levelregriddingkernels. But those kernels broadcastweights_inputto the input cell shape and apply it per input cell — matching the docstring ("Weights applied to the values of the input grid before resampling"):regridding/_weights/_weights_conservative.py:53→np.broadcast_to(weights_input, shape_values_input)regridding/_weights/_weights_transposed/_weights_transposed.py:207→np.broadcast_to(weights_input, shape_input)The mismatch only surfaced when
weights_inputwas non-scalar and the input/output axes had different names: aligning toshape_outputraisedValueError: axes is missing axes present in the input arraybecause the input axes were absent from the output shape. A scalarweights_input(the only case the tests previously exercised) broadcasts to any shape, hiding the bug.Change
weights_inputtoshape_inputin bothregridding_weightsandregridding_transpose_weights_conservative.test_regrid_conservative_2dwith a non-scalarweights_inputover distinct output axes, asserting that passingweights_inputis equivalent to folding it into the input values before regridding (Σ overlapᵢⱼ·wᵢ·vᵢ). Verified the new case fails without the fix (raises theValueError) and passes with it.Notes
test_transpose_weights_conservativeis flaky onmainindependent of this change (randomperturbresolving degenerate-grid ties differently across calls); not addressed here.🤖 Generated with Claude Code