Skip to content

Preserve input float dtype through resample() (#1467)#1476

Merged
brendancol merged 5 commits intomainfrom
issue-1467
May 5, 2026
Merged

Preserve input float dtype through resample() (#1467)#1476
brendancol merged 5 commits intomainfrom
issue-1467

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

  • resample() picks its working dtype from the input instead of forcing float64 in / float32 out. float64 in keeps float64; float32 in keeps float32; integer / bool in returns float32 (NaN-sentinel resampling needs a float type).
  • All four backend runners (numpy, cupy, dask+numpy, dask+cupy) and their block helpers thread the working/output dtypes through.
  • Aggregation kernels (numba @ngjit) keep a hardcoded float64 accumulator internally; the runner casts the result to the output dtype at the end. That keeps existing float32 aggregation accuracy and avoids re-specialising the kernels.

Closes #1467.

Test plan

  • float64 input gives float64 output, values match a float64 reference (atol=1e-12).
  • float32 input gives float32 output (existing default).
  • int16/int32/int64/uint8/uint16 input gives float32 output.
  • dask paths follow the same dtype rules (numpy backend).
  • int + nodata sentinel path is gated on the nodata parameter being present and skips otherwise (will activate once that parameter lands).
  • Existing 62 resample tests still pass.

resample() now picks its working dtype from the input: float64 in,
float64 out; float32 in, float32 out; integer in, float32 out (since
NaN-sentinel resampling needs a float type). Previously every input was
cast to float64 internally and every output forced to float32, which
silently dropped precision for float64 rasters.

The four backend runners and their dask block helpers thread the
working/output dtypes through. _maybe_astype skips the copy when the
array already has the desired dtype.

The aggregation kernels (numba @ngjit) keep their hardcoded float64
working buffer -- they accumulate in float64 and the runner casts the
result down at the end, which is cheap and matches the existing
behaviour for float32 aggregation accuracy.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label May 4, 2026
@brendancol
Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

Copilot AI and others added 3 commits May 4, 2026 20:32
…nlined dask kernels

Co-authored-by: brendancol <433221+brendancol@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented May 4, 2026

@copilot resolve the merge conflicts in this pull request

Resolved. The merge with origin/main introduced two sets of conflicts:

  1. xrspatial/resample.py_agg_block_np: kept the out_dtype parameter from this branch and the faster inlined numba kernels (_AGG_BLOCK_FUNCS) that landed in main. Also fixed _apply_nodata_mask to promote integer inputs to float32 (not float64) so the integer→float32 dtype rule stays consistent.

  2. xrspatial/tests/test_resample.py — kept both the TestDtypePreservation tests from this branch and the TestDaskAggregateInlined, TestThreeDRasters, TestNodata test classes from main. Removed a duplicate TestTargetResolutionTuple class that appeared twice after the merge. Also corrected the sentinel position in test_int_input_with_nodata_propagates_nan to land on a pixel that nearest-neighbour actually samples.

All 112 tests pass (commit f67015d).

@brendancol
Copy link
Copy Markdown
Contributor Author

@copilot resolve the merge conflicts in this pull request

… inf, dimension, and reproducibility tests

Co-authored-by: brendancol <433221+brendancol@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented May 5, 2026

@copilot resolve the merge conflicts in this pull request

Resolved again (commit 4b27684). The new conflicts came from two more commits that landed on main:

  • xrspatial/resample.py — auto-merged cleanly: updated the cubic depth comment and tightened the NaN weight threshold from 0.010.5.
  • xrspatial/tests/test_resample.py — kept TestDtypePreservation from this branch and merged in the four new test classes from main (TestCoordinateOrientation, TestInfHandling, TestDimensionRejection, TestReproducibility).

All 127 tests pass.

@brendancol brendancol merged commit f6a7759 into main May 5, 2026
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve input float dtype through resample()

2 participants