Skip to content

Add implicit NaN mask - #8325

Merged
matthew-brett merged 1 commit into
scikit-image:mainfrom
stefanv:port/pr-8193
Oct 29, 2026
Merged

matthew-brett merged 1 commit into
scikit-image:mainfrom
stefanv:port/pr-8193

Conversation

@stefanv

@stefanv stefanv commented Sep 9, 2026

Copy link
Copy Markdown
Member

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

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
@greptile-apps

greptile-apps Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in Greptile

The PR is not safe to merge until the mask passed to the Cython backends uses the required byte dtype and the explicit gallery-example requirement is satisfied.

Findings

  1. P1 Boolean mask breaks backends ▶
  2. P2 All-NaN input returns sentinel ▶
  3. P2 Combined masks remain untested ▶
  4. P2 Gallery example is missing ▶

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Boolean mask breaks backends

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.

Comment on lines +104 to +105
nan_mask = np.isnan(image)
any_nans = np.any(nan_mask)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Comment on lines +84 to +89
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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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!

Comment on lines +102 to +105

# NaNs form an implicit mask.
nan_mask = np.isnan(image)
any_nans = np.any(nan_mask)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Gallery example is missing

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!

@matthew-brett

Copy link
Copy Markdown
Contributor

Issue for API in skimage2 : #8211 .

@matthew-brett matthew-brett left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faithful port of initial PR. Code moved to _skimage2. Tests duplicated.

@matthew-brett
matthew-brett merged commit 57675d3 into scikit-image:main Oct 29, 2026
35 checks passed
@github-actions github-actions Bot added this to the 0.27 milestone Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Freeze while passing NaN to unwrap_phase

2 participants