Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions xrspatial/geotiff/_backends/dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _read_geotiff_dask(source: str, *,
allow_experimental_codecs: bool = False,
allow_internal_only_jpeg: bool = False,
band_nodata: str | None = None,
mask_nodata: bool = True,
mask_nodata: bool = False,
mask_and_scale: bool = False,
parse_coordinates: bool = True) -> xr.DataArray:
"""Read a GeoTIFF as a dask-backed DataArray for out-of-core processing.
Expand Down Expand Up @@ -99,14 +99,15 @@ def _read_geotiff_dask(source: str, *,
mixed-band-metadata check. Forwarded
verbatim to ``_read_vrt`` when the source is a ``.vrt`` file.
Passing it with a non-VRT GeoTIFF source raises ``ValueError``.
mask_nodata : bool, default True
mask_nodata : bool, default False
[stable] If True, replace the nodata sentinel with NaN per
chunk (integer rasters get promoted to ``float64``). If False,
skip the sentinel-to-NaN step so the source dtype survives.
The raw sentinel is still carried on ``attrs['nodata']``
either way. Pass ``mask_nodata=False`` together with
``dtype=<integer>`` to keep an integer source dtype; the
default promotes to ``float64`` and the cast then raises.
either way. The default matches ``open_geotiff`` (unmasked,
rioxarray-compatible): a bare ``_read_geotiff_dask(path)`` keeps
the source dtype and leaves the sentinel pixels untouched. Pass
``mask_nodata=True`` to restore the promote-to-NaN behaviour.
mask_and_scale : bool, default False
[advanced] If True, apply the source's GDAL ``SCALE`` / ``OFFSET``
(``data * scale + offset``) lazily on the assembled dask array and
Expand Down
13 changes: 7 additions & 6 deletions xrspatial/geotiff/_backends/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _read_geotiff_gpu(source: str, *,
allow_experimental_codecs: bool = False,
allow_internal_only_jpeg: bool = False,
band_nodata: str | None = None,
mask_nodata: bool = True,
mask_nodata: bool = False,
gpu: str = _GPU_DEPRECATED_SENTINEL,
) -> xr.DataArray:
"""Read a GeoTIFF with GPU-accelerated decompression via Numba CUDA.
Expand Down Expand Up @@ -180,14 +180,15 @@ def _read_geotiff_gpu(source: str, *,
with values ``'auto'`` / ``'strict'`` and was easy to confuse
with the boolean ``gpu=`` kwarg on ``open_geotiff`` /
``to_geotiff`` / ``_read_vrt``.
mask_nodata : bool, default True
mask_nodata : bool, default False
[experimental] If True, replace the nodata sentinel with NaN
(integer rasters get promoted to ``float64`` first). If False,
keep the source dtype and leave the raw sentinel in the data.
``attrs['nodata']`` carries the sentinel either way. Pass
``mask_nodata=False`` together with ``dtype=<integer>`` to
preserve an integer source dtype on a file with a matching
sentinel.
``attrs['nodata']`` carries the sentinel either way. The default
matches ``open_geotiff`` (unmasked, rioxarray-compatible): a bare
``_read_geotiff_gpu(path)`` keeps the source dtype and the raw
sentinel. Pass ``mask_nodata=True`` to restore the
promote-to-NaN behaviour.
allow_rotated : bool, default False
[experimental] Read-side opt-in for rotated / sheared
``ModelTransformationTag`` files. Forwarded through both GPU
Expand Down
13 changes: 7 additions & 6 deletions xrspatial/geotiff/_backends/vrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def _read_vrt(source: str, *,
allow_experimental_codecs: bool = False,
allow_internal_only_jpeg: bool = False,
band_nodata: str | None = None,
mask_nodata: bool = True) -> xr.DataArray:
mask_nodata: bool = False) -> xr.DataArray:
"""Read a GDAL Virtual Raster Table (.vrt) into an xarray.DataArray.

Release-contract tier (see
Expand Down Expand Up @@ -233,14 +233,15 @@ def _read_vrt(source: str, *,
to keep the legacy flatten-to-band-0 semantics explicitly. Any
other value raises ``ValueError`` at the boundary so typos
surface up front instead of degrading silently into strict mode.
mask_nodata : bool, default True
mask_nodata : bool, default False
[advanced] If True, run the integer-sentinel-to-NaN promotion
on the assembled mosaic. If False, skip it and keep the source
dtype with the raw sentinel still in the data. ``attrs['nodata']``
carries the sentinel either way. Pass ``mask_nodata=False``
together with ``dtype=<integer>`` when you need to preserve an
integer source dtype on a VRT whose declared sentinel matches
actual pixels. Float source bands are NaN-aware
carries the sentinel either way. The default matches
``open_geotiff`` (unmasked, rioxarray-compatible): a bare
``_read_vrt(path)`` keeps the source dtype and the raw sentinel.
Pass ``mask_nodata=True`` to restore the promote-to-NaN
behaviour on integer mosaics. Float source bands are NaN-aware
by virtue of how the internal reader handles them, so this kwarg
is most useful for integer-dtype mosaics.
allow_rotated : bool, default False
Expand Down
3 changes: 3 additions & 0 deletions xrspatial/geotiff/tests/gpu/test_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,8 +1172,11 @@ def _read_cpu_gpu_lerc(path):
from xrspatial.geotiff._reader import read_to_array

cpu, _geo = read_to_array(path, allow_experimental_codecs=True)
# The backend default is unmasked (#2976); request masking so the
# GPU read promotes the sentinel to NaN to match the CPU mask.
gpu_da = _read_geotiff_gpu(
path, gpu='strict', allow_experimental_codecs=True,
mask_nodata=True,
)
gpu_host = gpu_da.data.get()
return cpu, gpu_host
Expand Down
59 changes: 35 additions & 24 deletions xrspatial/geotiff/tests/gpu/test_kernels_and_kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,16 +1643,18 @@ def invalid_pred(a):
write(arr, path, compression="lerc", tiled=True, tile_size=8,
nodata=-9999.0)

# The backend default is unmasked (#2976); request masking on both
# the eager and chunked GPU reads so the sentinel promotes to NaN.
eager = _read_geotiff_gpu(
path, on_gpu_failure='strict',
allow_experimental_codecs=True,
allow_experimental_codecs=True, mask_nodata=True,
).data.get()

ifd, geo_info, header = _parse_for_gds_1896(path)
chunked_da = _read_geotiff_gpu_chunked_gds(
path, ifd, geo_info, header,
dtype=None, chunks=4, window=None, band=None,
name=None, max_pixels=None,
name=None, max_pixels=None, mask_nodata=True,
)
chunked = chunked_da.data.compute().get()

Expand Down Expand Up @@ -2459,19 +2461,20 @@ def test_read_geotiff_gpu_mask_nodata_false_preserves_uint16_2052(


@_gpu_only
def test_read_geotiff_gpu_default_mask_nodata_true_still_promotes_2052(
def test_read_geotiff_gpu_default_unmasked_keeps_uint16_2976(
uint16_with_matching_sentinel_2052):
"""The GPU default is unchanged: ``mask_nodata=True`` promotes."""
"""The GPU default matches ``open_geotiff`` (unmasked): a bare
``_read_geotiff_gpu(path)`` keeps the source dtype and sentinel."""
import cupy

from xrspatial.geotiff import _read_geotiff_gpu

path, _ = uint16_with_matching_sentinel_2052
path, arr = uint16_with_matching_sentinel_2052
da = _read_geotiff_gpu(path)

assert da.dtype == np.float64
nan_count = int(cupy.isnan(da.data).sum().get())
assert nan_count == len(_SENTINEL_POS_2052)
assert da.dtype == np.uint16
assert int(cupy.isnan(da.data.astype(cupy.float64)).sum().get()) == 0
np.testing.assert_array_equal(da.data.get(), arr)


@_gpu_only
Expand Down Expand Up @@ -2519,20 +2522,23 @@ def test_read_geotiff_gpu_dask_mask_nodata_false_preserves_uint16_2052(


@_gpu_only
def test_read_geotiff_gpu_dask_default_mask_nodata_true_still_promotes_2052(
def test_read_geotiff_gpu_dask_default_unmasked_keeps_uint16_2976(
uint16_with_matching_sentinel_2052):
"""The dask+GPU default still promotes the graph dtype to float64."""
"""The dask+GPU default matches ``open_geotiff`` (unmasked): the
graph dtype stays uint16 and the sentinel survives."""
import cupy

from xrspatial.geotiff import _read_geotiff_gpu

path, _ = uint16_with_matching_sentinel_2052
path, arr = uint16_with_matching_sentinel_2052
da = _read_geotiff_gpu(path, chunks=2)

assert da.dtype == np.float64
assert da.dtype == np.uint16
computed = da.compute()
nan_count = int(cupy.isnan(computed.data).sum().get())
assert nan_count == len(_SENTINEL_POS_2052)
assert computed.dtype == np.uint16
assert int(cupy.isnan(
computed.data.astype(cupy.float64)).sum().get()) == 0
np.testing.assert_array_equal(computed.data.get(), arr)


@_gpu_only
Expand Down Expand Up @@ -2565,16 +2571,18 @@ def test_read_vrt_mask_nodata_false_preserves_uint16_2052(
assert da.attrs["nodata"] == 0


def test_read_vrt_default_mask_nodata_true_still_promotes_2052(
def test_read_vrt_default_unmasked_keeps_uint16_2976(
uint16_vrt_with_matching_sentinel_2052):
"""The VRT default unchanged: ``mask_nodata=True`` promotes."""
"""The VRT default matches ``open_geotiff`` (unmasked): a bare
``_read_vrt(path)`` keeps the source dtype and sentinel."""
from xrspatial.geotiff import _read_vrt

vrt_path, _ = uint16_vrt_with_matching_sentinel_2052
vrt_path, arr = uint16_vrt_with_matching_sentinel_2052
da = _read_vrt(vrt_path)

assert da.dtype == np.float64
assert int(np.isnan(da.values).sum()) == len(_SENTINEL_POS_2052)
assert da.dtype == np.uint16
assert int(np.isnan(np.asarray(da.values, dtype=float)).sum()) == 0
np.testing.assert_array_equal(np.asarray(da.values), arr)


def test_open_geotiff_vrt_mask_nodata_false_threads_through_2052(
Expand Down Expand Up @@ -2617,17 +2625,20 @@ def test_read_vrt_chunked_mask_nodata_false_preserves_uint16_2052(
np.testing.assert_array_equal(np.asarray(computed.values), arr)


def test_read_vrt_chunked_default_mask_nodata_true_still_promotes_2052(
def test_read_vrt_chunked_default_unmasked_keeps_uint16_2976(
uint16_vrt_with_matching_sentinel_2052):
"""The chunked-VRT default still promotes to float64."""
"""The chunked-VRT default matches ``open_geotiff`` (unmasked): the
graph dtype stays uint16 and the sentinel survives."""
from xrspatial.geotiff import _read_vrt

vrt_path, _ = uint16_vrt_with_matching_sentinel_2052
vrt_path, arr = uint16_vrt_with_matching_sentinel_2052
da = _read_vrt(vrt_path, chunks=2)

assert da.dtype == np.float64
assert da.dtype == np.uint16
computed = da.compute()
assert int(np.isnan(computed.values).sum()) == len(_SENTINEL_POS_2052)
assert computed.dtype == np.uint16
assert int(np.isnan(np.asarray(computed.values, dtype=float)).sum()) == 0
np.testing.assert_array_equal(np.asarray(computed.values), arr)


def test_open_geotiff_vrt_chunked_mask_nodata_false_threads_through_2052(
Expand Down
5 changes: 3 additions & 2 deletions xrspatial/geotiff/tests/gpu/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,12 +1472,13 @@ def test_chunked_gpu_dtype_matches_cpu_dask_1909(
from xrspatial.geotiff import _read_geotiff_dask
from xrspatial.geotiff._backends.gpu import _read_geotiff_gpu_chunked_gds

cpu = _read_geotiff_dask(uint16_no_sentinel_path_1909, chunks=4)
cpu = _read_geotiff_dask(
uint16_no_sentinel_path_1909, chunks=4, mask_nodata=True)
ifd, geo_info, header = _parse_for_gds_1909(uint16_no_sentinel_path_1909)
gpu = _read_geotiff_gpu_chunked_gds(
uint16_no_sentinel_path_1909, ifd, geo_info, header,
dtype=None, chunks=4, window=None, band=None,
name=None, max_pixels=None,
name=None, max_pixels=None, mask_nodata=True,
)
assert cpu.data.dtype == gpu.data.dtype, (
f"CPU dask declared {cpu.data.dtype} but GPU dask declared "
Expand Down
2 changes: 1 addition & 1 deletion xrspatial/geotiff/tests/parity/test_backend_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@ def _ap_open_vrt(path, meta):
)
with open(vrt_path, 'w') as f:
f.write(xml)
return _read_vrt(vrt_path)
return _read_vrt(vrt_path, mask_nodata=True)


_AP_BACKENDS = (
Expand Down
2 changes: 1 addition & 1 deletion xrspatial/geotiff/tests/parity/test_finalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ def test_dtype_cast_absent_without_caller_dtype(tmp_path, opener):
when masking auto-promotes the graph dtype to float64."""
path = str(tmp_path / "tmp_2178_no_cast.tif")
_make_int_with_nodata_tiff(path)
out = opener(path)
out = opener(path, mask_nodata=True)
# Masking promoted the int source to float64 on the graph dtype,
# but the caller did not ask for a cast.
assert out.dtype == np.float64
Expand Down
Loading
Loading