Repository navigation
Add implicit NaN mask - #8193
matthew-brett merged 6 commits into
Conversation
Mask any NaNs in the image.
|
A quick and dirty benchmark suggests time is almost identical with this change. There are no memory implications because we were already calculating a default (all-zero) mask. Now we calculate the same mask with |
|
@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. |
|
You're right to push back on this. Looking at the code more carefully:
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 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:
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. |
|
|
||
| Parameters | ||
| ---------- | ||
| image : (M[, N[, P]]) ndarray or masked array of floats |
There was a problem hiding this comment.
Aside: It's a bit unconventional to take in a masked array and work with its mask.
There was a problem hiding this comment.
Do you think we should add a mask parameter in skimage2 instead? That seems to be our most widely used pattern.
There was a problem hiding this comment.
Yes, that would be a good Skimage2 change.
There was a problem hiding this comment.
I don't think we support masked arrays anywhere else, so agree that it would be best to stick to the library-wide convention.
| 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. |
There was a problem hiding this comment.
Should also note that this implicit mask is combined (logical or) with the mask of image if it exists.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
On the extra cost of 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. |
|
@matthew-brett Let us know when you're done looking at the feedback above, and we'll get this in. |
Co-authored-by: Lars Grüter <[email protected]>
Co-authored-by: Lars Grüter <[email protected]>
|
Reviews (2): Last reviewed commit: "Extend the docstring for the implicit ma..." | Re-trigger Greptile |
|
Reviews (3): Last reviewed commit: "Fixup tests, use assert_equal" | Re-trigger Greptile |
|
I've addressed the comments, ready to go in, from my PoV. |
stefanv
left a comment
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Why is the output under the mask not set to NaN?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@stefanv - is that answer satisfying, to your question? If so, I'll merge.
There was a problem hiding this comment.
(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.)
dbafb0e
into
scikit-image:pre-megamove-integration
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]>
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]>
Fixes #3831.
Mask any NaNs in the image.
This is a more general fix than in #7176.