Skip to content

Commit 9faf106

Browse files
committed
Address code review: edge-case tests and threshold comment (#1086)
- Test nearest-neighbor at exact boundary r=-0.5 - Test cubic fallback at far (bottom-right) edge - Comment the 1e-6 NaN threshold rationale
1 parent 95c7909 commit 9faf106

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

xrspatial/reproject/_interpolate.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,9 @@ def _resample_cupy(source_window, src_row_coords, src_col_coords,
835835
nan_mask.astype(cp.float64), coords,
836836
order=order, mode='constant', cval=1.0
837837
).reshape(src_row_coords.shape)
838+
# Any nonzero weight means NaN pixels contributed to the output.
839+
# Use 1e-6 instead of 0.0 to absorb floating-point interpolation
840+
# noise from map_coordinates on the binary mask.
838841
oob = oob | (nan_weight > 1e-6)
839842

840843
result[oob] = nodata

xrspatial/tests/test_reproject.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ def test_nearest_negative_rounding(self):
212212
assert result2[0, 0] == src[0, 1], (
213213
f"r=-0.4 should map to pixel 0, got {result2[0, 0]}"
214214
)
215+
# r = -0.5 is exactly on the half-pixel boundary: floor(-0.5+0.5)=0 -> pixel 0
216+
rows3 = np.array([[-0.5]])
217+
result3 = _resample_numpy(src, rows3, cols, resampling='nearest', nodata=-999)
218+
assert result3[0, 0] == src[0, 1], (
219+
f"r=-0.5 should map to pixel 0, got {result3[0, 0]}"
220+
)
215221

216222
def test_cubic_oob_fallback(self):
217223
"""Cubic must fall back to bilinear when stencil extends outside source (#1086)."""
@@ -238,6 +244,17 @@ def test_cubic_oob_fallback(self):
238244
# but the point is the code path exercises the non-fallback branch
239245
assert cubic_int[0, 0] != -999
240246

247+
def test_cubic_oob_fallback_far_edge(self):
248+
"""Cubic at bottom-right boundary: stencil needs row sh, same fallback (#1086)."""
249+
from xrspatial.reproject._interpolate import _resample_numpy
250+
src = np.arange(36, dtype=np.float64).reshape(6, 6)
251+
# r=4.5: cubic stencil needs row 6 (= sh), which is OOB
252+
rows = np.array([[4.5]])
253+
cols = np.array([[4.5]])
254+
cubic = _resample_numpy(src, rows, cols, resampling='cubic', nodata=-999)
255+
bilinear = _resample_numpy(src, rows, cols, resampling='bilinear', nodata=-999)
256+
np.testing.assert_allclose(cubic, bilinear, atol=1e-10)
257+
241258
def test_cubic_oob_bilinear_fallback_renormalizes(self):
242259
"""Cubic at (-0.8,-0.8): stencil OOB triggers bilinear, which
243260
finds pixel (0,0) as the only valid neighbor and returns it (#1086)."""

0 commit comments

Comments
 (0)