Skip to content

Fix morphology.local_maxima for input with any dimension < 3#3447

Merged
stefanv merged 5 commits intoscikit-image:masterfrom
lagru:fix-extrema
Nov 13, 2018
Merged

Fix morphology.local_maxima for input with any dimension < 3#3447
stefanv merged 5 commits intoscikit-image:masterfrom
lagru:fix-extrema

Conversation

@lagru
Copy link
Copy Markdown
Member

@lagru lagru commented Oct 6, 2018

Description

  • local_maxima (and local_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 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.
  • The output of local_maxima([], indices=True) didn't match the output for non-empty arrays and the description of the docstring. This is fixed as well.
  • Replaced assert_equal with assert_array_equal which is more restrictive and (I think) more inline with what we actually want to test.

Checklist

References

Closes #3261

For reviewers

(Don't remove the checklist below.)

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.
  • Consider backporting the PR with @meeseeksdev backport to v0.14.x

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-io
Copy link
Copy Markdown

codecov-io commented Oct 6, 2018

Codecov Report

Merging #3447 into master will increase coverage by 0.34%.
The diff coverage is 97.77%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
skimage/morphology/tests/test_extrema.py 99.48% <100%> (+0.07%) ⬆️
skimage/morphology/extrema.py 95.14% <93.75%> (+0.24%) ⬆️
skimage/io/_plugins/gtk_plugin.py 25% <0%> (-5.56%) ⬇️
skimage/future/manual_segmentation.py 10.78% <0%> (-2.09%) ⬇️
skimage/io/_plugins/util.py 59.39% <0%> (-1.82%) ⬇️
skimage/draw/draw.py 98.11% <0%> (-0.84%) ⬇️
skimage/measure/tests/test_regionprops.py 99.64% <0%> (-0.36%) ⬇️
skimage/segmentation/active_contour_model.py 98.96% <0%> (-0.02%) ⬇️
skimage/color/colorconv.py 99.08% <0%> (-0.01%) ⬇️
skimage/restoration/unwrap.py 100% <0%> (ø) ⬆️
... and 33 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update af8a78e...bd72727. Read the comment docs.

@emmanuelle
Copy link
Copy Markdown
Member

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)?

@lagru
Copy link
Copy Markdown
Member Author

lagru commented Oct 8, 2018

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.

lagru added 2 commits October 9, 2018 11:26
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
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@sciunto sciunto added this to the 0.15 milestone Nov 11, 2018
@sciunto
Copy link
Copy Markdown
Member

sciunto commented Nov 11, 2018

@lagru What's the status of this PR?

thank you.

@lagru
Copy link
Copy Markdown
Member Author

lagru commented Nov 11, 2018

@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 local_minima wraps local_maxima the warning can only have the correct stacklevel for one of these functions (currently the latter).

@sciunto
Copy link
Copy Markdown
Member

sciunto commented Nov 11, 2018

I restarted the test, it's passing now.

Copy link
Copy Markdown
Member

@jni jni left a comment

Choose a reason for hiding this comment

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

I've left two minor comments. Happy for this to be merged as-is, or they can be addressed and then merged.

@jni
Copy link
Copy Markdown
Member

jni commented Nov 11, 2018

Thanks @lagru!

@stefanv
Copy link
Copy Markdown
Member

stefanv commented Nov 13, 2018

Thanks for catching these; I love the guiding warnings and error messages.

@stefanv stefanv merged commit 29a22e1 into scikit-image:master Nov 13, 2018
@lumberbot-app lumberbot-app bot added the Still Needs Manual Backport MrMeeseeks-managed label label Nov 13, 2018
@scikit-image scikit-image deleted a comment from lumberbot-app bot Nov 13, 2018
@scikit-image scikit-image deleted a comment from lumberbot-app bot Nov 13, 2018
stefanv pushed a commit to stefanv/scikit-image that referenced this pull request Nov 13, 2018
…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
@stefanv
Copy link
Copy Markdown
Member

stefanv commented Nov 13, 2018

Manual backport at #3545

@lagru
Copy link
Copy Markdown
Member Author

lagru commented Nov 14, 2018

You're welcome! 🙂

@lagru lagru deleted the fix-extrema branch November 14, 2018 08:02
lagru added a commit to lagru/scikit-image that referenced this pull request Nov 14, 2018
…-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
jni added a commit that referenced this pull request Nov 14, 2018
Backport #3022 & #3447: Rewrite of local_maxima with flood-fill & Cython + bugfix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Still Needs Manual Backport MrMeeseeks-managed label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: ValueError for input [1, 1] in morphology.local_maxima when [0, 0] is expected

6 participants