Skip to content

Fix weights_input alignment in conservative regridding#188

Merged
roytsmart merged 1 commit into
mainfrom
fix-weights-input-alignment
Jun 26, 2026
Merged

Fix weights_input alignment in conservative regridding#188
roytsmart merged 1 commit into
mainfrom
fix-weights-input-alignment

Conversation

@roytsmart

Copy link
Copy Markdown
Contributor

Summary

na.regridding.weights (and transpose_weights_conservative) aligned the weights_input array to the output grid shape before passing it to the low-level regridding kernels. But those kernels broadcast weights_input to 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:53np.broadcast_to(weights_input, shape_values_input)
  • regridding/_weights/_weights_transposed/_weights_transposed.py:207np.broadcast_to(weights_input, shape_input)

The mismatch only surfaced when weights_input was non-scalar and the input/output axes had different names: aligning to shape_output raised ValueError: axes is missing axes present in the input array because the input axes were absent from the output shape. A scalar weights_input (the only case the tests previously exercised) broadcasts to any shape, hiding the bug.

Change

  • Align weights_input to shape_input in both regridding_weights and regridding_transpose_weights_conservative.
  • Extend test_regrid_conservative_2d with a non-scalar weights_input over distinct output axes, asserting that passing weights_input is equivalent to folding it into the input values before regridding (Σ overlapᵢⱼ·wᵢ·vᵢ). Verified the new case fails without the fix (raises the ValueError) and passes with it.

Notes

  • test_transpose_weights_conservative is flaky on main independent of this change (random perturb resolving degenerate-grid ties differently across calls); not addressed here.

🤖 Generated with Claude Code

`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

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.24%. Comparing base (d60267c) to head (c355ef7).

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           
Flag Coverage Δ
unittests 97.24% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@roytsmart roytsmart merged commit 7fe1ce0 into main Jun 26, 2026
20 checks passed
@roytsmart roytsmart deleted the fix-weights-input-alignment branch June 26, 2026 18:39
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