Repository navigation
Add implicit NaN mask - #8325
Conversation
Fixes scikit-image#3831. Mask any NaNs in the image. This is a more general fix than in scikit-image#7176. Co-authored-by: Lars Grüter <[email protected]> -- Ported from scikit-image#8193 on pre-megamove-integration (af098de) Assisted-by: claude-code:claude-opus-5
| mask = nan_mask | np.require(np.ma.getmaskarray(image), np.uint8, ['C']) | ||
| else: | ||
| mask = np.zeros_like(image, dtype=np.uint8, order='C') | ||
| mask = nan_mask |
There was a problem hiding this comment.
For an ordinary ndarray, np.isnan creates a Boolean mask, which this branch passes directly to the 2D and 3D Cython backends. Those backends require a C-contiguous unsigned char mask, so normal 2D and 3D calls fail during argument conversion. Convert the mask to the required uint8 representation, as the previous code did.
| nan_mask = np.isnan(image) | ||
| any_nans = np.any(nan_mask) |
There was a problem hiding this comment.
All-NaN input returns sentinel
An all-NaN 2D or 3D image marks every pixel as masked. With no unmasked phase value, the backend leaves its minimum initialized to DBL_MAX and returns approximately 1.8e308 for every pixel instead of a meaningful phase value or an explicit error. Define and test the fully implicit-masked case.
| mask[4:6, 4:8] = True | ||
| image_wrapped_masked = np.ma.array(image_wrapped, mask=mask, fill_value=0.5) | ||
| mask_res = unwrap_phase(image_wrapped_masked, rng=0) | ||
| image_wrapped_nans = image_wrapped.copy() | ||
| image_wrapped_nans[mask] = np.nan | ||
| np.testing.assert_equal(mask_res, unwrap_phase(image_wrapped_nans, rng=0)) |
There was a problem hiding this comment.
Combined masks remain untested
These tests compare an explicitly masked input with a separate plain NaN input, but never pass a masked array that also contains NaNs at different positions. They therefore do not verify the newly documented logical-OR behavior or confirm that both sets of pixels appear in the resulting mask. Add a mixed-mask case here and mirror it in tests/skimage2/restoration/test_unwrap.py.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
|
||
| # NaNs form an implicit mask. | ||
| nan_mask = np.isnan(image) | ||
| any_nans = np.any(nan_mask) |
There was a problem hiding this comment.
This change introduces NaN masking as a new user-visible feature, but the core developer guide requires every new feature to include a gallery example that illustrates and explains it. Add an example demonstrating implicit NaN masking and its interaction with an explicit mask; this repository requirement must be satisfied before merging.
Context Used: Core developer guide (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Issue for API in skimage2 : #8211 . |
matthew-brett
left a comment
There was a problem hiding this comment.
Faithful port of initial PR. Code moved to _skimage2. Tests duplicated.
Fixes #3831.
Mask any NaNs in the image.
This is a more general fix than in #7176.
Co-authored-by: Lars Grüter [email protected]
--
Ported from #8193 on pre-megamove-integration (af098de)
Assisted-by: claude-code:claude-opus-5