Fix morphology.local_maxima for input with any dimension < 3#3447
Fix morphology.local_maxima for input with any dimension < 3#3447stefanv merged 5 commits intoscikit-image:masterfrom
Conversation
local_maxima (and local_minima) failed with an ValueError for images with at least 1 dimension with fewer than 3 elements. This was due to the implementation which applies a structuring element with exactly 3 elements in each dimension to the image in question. This behavior was only visible if `allow_borders` was false, otherwise the image was padded internally. This fix makes these functions work for the mentioned edge cases by testing the image shape at the appropriate time.
Codecov Report
@@ Coverage Diff @@
## master #3447 +/- ##
==========================================
+ Coverage 86.86% 87.21% +0.34%
==========================================
Files 340 342 +2
Lines 27485 27769 +284
==========================================
+ Hits 23875 24218 +343
+ Misses 3610 3551 -59
Continue to review full report at Codecov.
|
|
Thanks @lagru . Should a warning be raised when one of the dimensions is <3 (in order to help the user understand why no maxima are detected)? |
Why not. Good idea! I'll add it. |
The old output for empty arrays if `indices` was true didn't correspond to the output for non-empty arrays and the description in the docstring. This ensures that the output will always be a tuple of empty arrays. If `indices` is false, the returned empty array now machtes the shape of `image` as well.
which is the case for dimensions with fewer than 3 samples. Because `local_minima` wraps `local_maxima` the warning can only set the correct stacklevel for one of these functions. The latter was chosen because it stands to reason that it is / will be used more frequently. If may be possible to suppress and re-raise the warning with the context manager from the warnings module but that would introduce non-thread- safe code and introduce perhaps unnecessary complexity.
| # -> no maxima can exist & structuring element can't be applied | ||
| warn( | ||
| "no maxima can exist for an image with any dimension smaller 3", | ||
| stacklevel=3 |
|
@lagru What's the status of this PR? thank you. |
|
@sciunto As far as I remember I this is ready to merge (test failure is unrelated). However because this PR involved adding a warning I decided to wait until a decision is made how scikit-image wants to raise warnings (see #2097, #3438 and #3467). By the way, because |
|
I restarted the test, it's passing now. |
jni
left a comment
There was a problem hiding this comment.
I've left two minor comments. Happy for this to be merged as-is, or they can be addressed and then merged.
|
Thanks @lagru! |
to reduce visual clutter.
|
Thanks for catching these; I love the guiding warnings and error messages. |
…image#3447) * Fix local_maxima for input with dimensions < 3 local_maxima (and local_minima) failed with an ValueError for images with at least 1 dimension with fewer than 3 elements. This was due to the implementation which applies a structuring element with exactly 3 elements in each dimension to the image in question. This behavior was only visible if `allow_borders` was false, otherwise the image was padded internally. This fix makes these functions work for the mentioned edge cases by testing the image shape at the appropriate time. * Fix output of local_maxima for empty arrays The old output for empty arrays if `indices` was true didn't correspond to the output for non-empty arrays and the description in the docstring. This ensures that the output will always be a tuple of empty arrays. If `indices` is false, the returned empty array now machtes the shape of `image` as well. * Show warning if no maxima are possible which is the case for dimensions with fewer than 3 samples. Because `local_minima` wraps `local_maxima` the warning can only set the correct stacklevel for one of these functions. The latter was chosen because it stands to reason that it is / will be used more frequently. If may be possible to suppress and re-raise the warning with the context manager from the warnings module but that would introduce non-thread- safe code and introduce perhaps unnecessary complexity. * Point user to allow_borders in warning message
|
Manual backport at #3545 |
|
You're welcome! 🙂 |
…-image#3447) * Fix local_maxima for input with dimensions < 3 local_maxima (and local_minima) failed with an ValueError for images with at least 1 dimension with fewer than 3 elements. This was due to the implementation which applies a structuring element with exactly 3 elements in each dimension to the image in question. This behavior was only visible if `allow_borders` was false, otherwise the image was padded internally. This fix makes these functions work for the mentioned edge cases by testing the image shape at the appropriate time. * Fix output of local_maxima for empty arrays The old output for empty arrays if `indices` was true didn't correspond to the output for non-empty arrays and the description in the docstring. This ensures that the output will always be a tuple of empty arrays. If `indices` is false, the returned empty array now machtes the shape of `image` as well. * Show warning if no maxima are possible which is the case for dimensions with fewer than 3 samples. Because `local_minima` wraps `local_maxima` the warning can only set the correct stacklevel for one of these functions. The latter was chosen because it stands to reason that it is / will be used more frequently. If may be possible to suppress and re-raise the warning with the context manager from the warnings module but that would introduce non-thread- safe code and introduce perhaps unnecessary complexity. * Point user to allow_borders in warning message
Description
local_maxima(andlocal_minima) failed with a ValueError for images with at least 1 dimension with fewer than 3 elements. This was due to the implementation which applies a structuring element with exactly 3 elements in each dimension to the image in question. This behavior was only visible ifallow_borderswas false, otherwise the image was padded internally.This fix makes these functions work for the mentioned edge cases by testing the image shape at the appropriate time.
local_maxima([], indices=True)didn't match the output for non-empty arrays and the description of the docstring. This is fixed as well.assert_equalwithassert_array_equalwhich is more restrictive and (I think) more inline with what we actually want to test.Checklist
Gallery example in./doc/examples(new features only)Benchmark in./benchmarks, if your changes aren't covered by an existing benchmarkReferences
Closes #3261
For reviewers
(Don't remove the checklist below.)
later.
Check that new functions are imported in corresponding__init__.py.doc/release/release_dev.rst.@meeseeksdev backport to v0.14.x