Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NaNs when clipping in transform.resize #6852

Merged
merged 2 commits into from Mar 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions skimage/transform/_warps.py
Expand Up @@ -162,9 +162,6 @@ def resize(image, output_shape, order=None, mode='reflect', cval=0, clip=True,
if order > 0:
image = convert_to_float(image, preserve_range)

# Save input value range for clip
img_bounds = np.array([image.min(), image.max()]) if clip else None

# Translate modes used by np.pad to those used by scipy.ndimage
ndi_mode = _to_ndimage_mode(mode)
if anti_aliasing:
Expand All @@ -179,14 +176,16 @@ def resize(image, output_shape, order=None, mode='reflect', cval=0, clip=True,
elif np.any((anti_aliasing_sigma > 0) & (factors <= 1)):
warn("Anti-aliasing standard deviation greater than zero but "
"not down-sampling along all axes")
image = ndi.gaussian_filter(image, anti_aliasing_sigma,
cval=cval, mode=ndi_mode)
filtered = ndi.gaussian_filter(image, anti_aliasing_sigma,
cval=cval, mode=ndi_mode)
else:
filtered = image

zoom_factors = [1 / f for f in factors]
out = ndi.zoom(image, zoom_factors, order=order, mode=ndi_mode,
out = ndi.zoom(filtered, zoom_factors, order=order, mode=ndi_mode,
cval=cval, grid_mode=True)

_clip_warp_output(img_bounds, out, mode, cval, clip)
_clip_warp_output(image, out, mode, cval, clip)

return out

Expand Down
4 changes: 3 additions & 1 deletion skimage/transform/tests/test_warps.py
Expand Up @@ -446,10 +446,12 @@ def test_resize_clip(order, preserve_range, anti_aliasing, dtype):
x = np.ones((5, 5), dtype=dtype)
if dtype == np.uint8:
x *= 255
else:
x[0, 0] = np.NaN
resized = resize(x, (3, 3), order=order, preserve_range=preserve_range,
anti_aliasing=anti_aliasing)

assert resized.max() == expected_max
assert np.nanmax(resized) == expected_max


@pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
Expand Down