Skip to content

Add implicit NaN mask - #8193

Merged
matthew-brett merged 6 commits into
scikit-image:pre-megamove-integrationfrom
matthew-brett:unwrap-with-nans
Jun 17, 2026
Merged

matthew-brett merged 6 commits into
scikit-image:pre-megamove-integrationfrom
matthew-brett:unwrap-with-nans

Conversation

@matthew-brett

@matthew-brett matthew-brett commented May 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3831.

Mask any NaNs in the image.

This is a more general fix than in #7176.

`unwrap_phase` now handles input images with NaN values, by taking the NaN values to be an implicit mask.  Previously,
the NaN values could cause the code to hang.   Closes #3831.

@matthew-brett

matthew-brett commented May 23, 2026 •

Copy link
Copy Markdown
Contributor Author

A quick and dirty benchmark suggests time is almost identical with this change.

#8142 (comment)

There are no memory implications because we were already calculating a default (all-zero) mask. Now we calculate the same mask with np.isnan.

@greptile-apps

greptile-apps Bot commented May 23, 2026 •

Copy link
Copy Markdown
Contributor

Comments Outside Diff (1)

  1. src/skimage/restoration/unwrap.py, line 138-141 (link)

    P1 image_unwrapped is allocated with np.empty_like, so the C extension leaves the masked positions uninitialized. For plain arrays with NaN values the function returns those garbage bytes rather than NaN. Since the docstring describes NaN as "an implicit mask", callers will reasonably expect NaN in those positions in the output — especially when chaining operations that check for NaN downstream.

Reviews (1): Last reviewed commit: "pre-commit reformatting." | Re-trigger Greptile

Comment thread src/skimage/restoration/unwrap.py Resolved
@matthew-brett

Copy link
Copy Markdown
Contributor Author

Comments Outside Diff (1)

  1. src/skimage/restoration/unwrap.py, line 138-141 (link)
    P1 image_unwrapped is allocated with np.empty_like, so the C extension leaves the masked positions uninitialized. For plain arrays with NaN values the function returns those garbage bytes rather than NaN. Since the docstring describes NaN as "an implicit mask", callers will reasonably expect NaN in those positions in the output — especially when chaining operations that check for NaN downstream.

Reviews (1): Last reviewed commit: "pre-commit reformatting." | Re-trigger Greptile

@greptileai - I believe the behavior for NaN pixels is the same as for pixels under the mask in the previous code, which is that the value will be replaced by a default value. Is that correct? In which case, do you agree that the behavior can reasonably be described as an implicit mask, in that the NaNs have the same behavior as a non-zero value for a masked array.

@greptile-apps

greptile-apps Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

You're right to push back on this. Looking at the code more carefully:

  • Line 129: image_not_masked[nan_mask] = 0 — NaN positions are replaced with 0 before the C extension sees them.
  • The mask is passed to the C extension, so it treats those positions the same as explicitly masked pixels.

So the behavior for NaN positions in the output is determined by what the C extension writes to masked positions — not "garbage bytes" as I incorrectly stated. If the C extension writes a consistent default value to all masked positions (as it does for explicitly masked pixels), then NaN and masked pixels do behave identically.

That said, the concern I was trying to raise still has some validity from a usability perspective: for a plain array input with NaN values, the output at those positions will contain whatever default the C extension uses (likely 0 or some phase value) — not NaN. Callers who expect NaN-in → NaN-out may be surprised, especially if they're using NaN presence as a signal downstream.

If the behavior is intentional and equivalent to explicit masking, it might be worth a note in the docstring (or the return value description) clarifying that NaN positions in the output will contain the same fill value as masked positions, not NaN itself. Something like:

If the input contains NaN values, those positions are treated as masked. The corresponding values in the output will match the fill behavior of masked pixels, not necessarily NaN.

But if the current docstring phrasing "implicit mask" is considered sufficient to imply this, I can see why it might be left as-is.

Tip: You can give me more context with .greptile/rules.md so I get this right next time.

@lagru
lagru self-requested a review May 26, 2026 19:38
@lagru lagru added the 🩹 type: Bug fix Fixes unexpected or incorrect behavior label May 26, 2026

Parameters
----------
image : (M[, N[, P]]) ndarray or masked array of floats

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Aside: It's a bit unconventional to take in a masked array and work with its mask.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you think we should add a mask parameter in skimage2 instead? That seems to be our most widely used pattern.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, that would be a good Skimage2 change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we support masked arrays anywhere else, so agree that it would be best to stick to the library-wide convention.

Comment thread src/skimage/restoration/unwrap.py Outdated
will not be used to guide the unwrapping of neighboring, unmasked
values. Masked 1D arrays are not allowed, and will raise a
`ValueError`.
`ValueError`. NaN values in the image form an implicit mask.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should also note that this implicit mask is combined (logical or) with the mask of image if it exists.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

import numpy as np
from skimage import data, img_as_float, color, exposure
from skimage.restoration import unwrap_phase
image = color.rgb2gray(img_as_float(data.chelsea()))
image = exposure.rescale_intensity(image, out_range=(0, 4 * np.pi))
image_wrapped = np.angle(np.exp(1j * image))

%timeit unwrap_phase(image_wrapped)

On main:
34.7 ms ± 427 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)

This PR:
35.3 ms ± 487 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Performance impact indeed seems negligible.

@lagru lagru left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've left a few nitpicks but nothing blocking. Looks good.

I'll close #7176 since this fixes things in Python land. Which is a bit less messy than propagating back an error via a returned int in C land.

But in general I don't like taking the cost of np.isnan(image) if there are cheaper options.

@matthew-brett

Copy link
Copy Markdown
Contributor Author

On the extra cost of np.isnan:

[ins] In [10]: img = np.ones((1024, 1024))

[ins] In [15]: timeit np.isnan(img)
168 μs ± 880 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

[ins] In [16]: timeit np.zeros((1024, 1024))
128 μs ± 3.04 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

So, the excess cost for a 1024 x 1024 image is about 40 μs - I suspect that's why the benchmarks show no convincing difference.

@stefanv

stefanv commented Jun 2, 2026

Copy link
Copy Markdown
Member

@matthew-brett Let us know when you're done looking at the feedback above, and we'll get this in.

@greptile-apps

greptile-apps Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "Extend the docstring for the implicit ma..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Fixup tests, use assert_equal" | Re-trigger Greptile

@matthew-brett

Copy link
Copy Markdown
Contributor Author

I've addressed the comments, ready to go in, from my PoV.

@stefanv stefanv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have one last question, but maybe it's obvious or has been discussed before; I approved so you can merge regardless.

`ValueError`. NaN values in the image form an implicit mask. We
combine this implicit mask with any mask from a masked array, with
logical OR. The output values under the resulting mask are set to the
minimum of the unwrapped phase.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is the output under the mask not set to NaN?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was going for compatibility with the existing mask behaviour; it seemed confusing to have one mask behavior for NaN and another for an explicit mask.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@stefanv - is that answer satisfying, to your question? If so, I'll merge.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(I think we can discuss tomorrow what the ideal behavior is here; we have the option to change for 2.0. Ultimately, it's not that big a deal since the user can do that as a post-processing step.)

@matthew-brett
matthew-brett changed the base branch from main to pre-megamove-integration June 15, 2026 12:45
@matthew-brett
matthew-brett merged commit dbafb0e into scikit-image:pre-megamove-integration Jun 17, 2026
26 checks passed
@stefanv stefanv mentioned this pull request Sep 9, 2026
matthew-brett added a commit that referenced this pull request Oct 29, 2026
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

Co-authored-by: Matthew Brett <[email protected]>
mkcor pushed a commit to mkcor/scikit-image that referenced this pull request Nov 13, 2026
Fixes scikit-image#3831.

Mask any NaNs in the image.

This is a more general fix than in
scikit-image#7176.

```release-note
`unwrap_phase` now handles input images with NaN values, by taking the NaN values to be an implicit mask.  Previously,
the NaN values could cause the code to hang.   Closes scikit-image#3831.
```

---------

Co-authored-by: Lars Grüter <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🩹 type: Bug fix Fixes unexpected or incorrect behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Freeze while passing NaN to unwrap_phase

3 participants