Skip to content

[MRG] EHN: median filters will accept floating image - #3616

Merged
sciunto merged 6 commits into
scikit-image:masterfrom
glemaitre:is/1419
Dec 21, 2018
Merged

sciunto merged 6 commits into
scikit-image:masterfrom
glemaitre:is/1419

Conversation

@glemaitre

@glemaitre glemaitre commented Dec 19, 2018 •

Copy link
Copy Markdown
Contributor

Description

Support floating image in skimage.filters.median. This PR will controversial, but let's start with a potential solution.

Things to considered:

  • Floating images cannot be handled by skimage.filters.rank.median
  • scipy.ndimage.median_filter would be a good candidate for this use case

However,

  • scipy.ndimage.median_filter does not accept parameter such as mask or shift_*
  • there is no way to go back to the behavior of skimage.filters.rank.median using scipy.ndimage.median_filter: (i) skimage discard the pixel outside of the structural element when computing the median while scipy will pad the image beforehand and (ii) the tie breaking is done differently --- floor for skimage and ceiling for scipy.

Therefore my proposal is the following: introduce a parameter behavior allowing to switch to the scipy implementation using all the available parameters. By default, we will fall back on the previous behavior for 2 releases. In 2 releases, the scipy function will be the default and in 4 releases, the parameters of the old behavior will be removed at the same time than the behavior parameter.

I could not come with a cleaner design than those deprecation cycles.

Checklist

References

closes #1419
closes #1422
closes #2943

For reviewers

  • 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

@stefanv

stefanv commented Dec 20, 2018

Copy link
Copy Markdown
Member

Perhaps the following deprecation pathway:

  1. Add a behavior flag, set to 'rank' for now, but with the option to set it to 'ndimage'. If the user does not specify this flag, warn that we will switch behavior to ndimage in the future.
  2. After one release, switch the default to ndimage. If the flag is not set, warn the user that this function has changed, and that they should adapt their code.
  3. After one more release, remove the behavior flag and make ndimage the default.

In addition, update the docstring See Also section with a reference to the rank filters, so that users know that this fast option is available for integer images.

Add a docstring to either filters or filters.rank, describing what these are, so that they are more discoverable.

In general, we should consider adding paragraph descriptions to each of the submodule __init__.py.

@stefanv

stefanv commented Dec 20, 2018

Copy link
Copy Markdown
Member

Ah, @glemaitre I did not read your PR description fully before commenting, so, yes, I guess we are in agreement. Except that I wanted us to move a little bit more quickly.

@stefanv

stefanv commented Dec 20, 2018

Copy link
Copy Markdown
Member

For reference, our deprecation cycle documentation.

@glemaitre

Copy link
Copy Markdown
Contributor Author

Ah, @glemaitre I did not read your PR description fully before commenting, so, yes, I guess we are in agreement. Except that I wanted us to move a little bit more quickly.

I think that this is a pretty good idea to move faster for this case.

@pep8speaks

Copy link
Copy Markdown

Hello @glemaitre! Thanks for updating the PR.

Line 21:80: E501 line too long (80 > 79 characters)

Line 28:55: E128 continuation line under-indented for visual indent

@glemaitre

Copy link
Copy Markdown
Contributor Author

@stefanv @emmanuelle @jni @sciunto This should be ready for a first review.

@sciunto sciunto 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.

Excellent! I made only few minor comments, this looks great. Thank you for tackling that during the sprint.

Comment thread doc/release/release_dev.rst Outdated
identical behavior as ``scipy.ndimage.median_filter``. This behavior can be
set already using ``behavior='ndimage'``. In 0.16, it will be the default
behavior and in 0.17, the parameter of the previous behavior (i.e., ``mask``,
``shift_x``, ``shift_y``) will be removed.

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.

as well as behavior I guess...

Comment thread skimage/filters/_median.py Outdated
----------
image : array-like
Input image.
selem : ndarray

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.

Suggested change
selem : ndarray
selem : ndarray, optional

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.

damn, it looks like github added a space before the variable. I didn't intend to do that. Just add the optional information....

Comment thread skimage/filters/_median.py Outdated
with the same number of dimension than ``image``.
If None, ``selem`` will be a N-D array with 3 elements for each
dimension (e.g., vector, square, cube, etc.)
out : ndarray, (same dtype as image)

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.

Suggested change
out : ndarray, (same dtype as image)
out : ndarray, (same dtype as image), optional

Comment thread skimage/filters/_median.py Outdated
dimension (e.g., vector, square, cube, etc.)
out : ndarray, (same dtype as image)
If None, a new array is allocated.
mask : ndarray

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.

Suggested change
mask : ndarray
mask : ndarray, optional

Comment thread skimage/filters/_median.py Outdated

.. deprecated:: 0.16
``mask`` is deprecated in 0.16 and will be removed 0.17.
shift_x, shift_y : int

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.

Suggested change
shift_x, shift_y : int
shift_x, shift_y : int, optional

Glad you used the deprecated flags :)

Comment thread skimage/filters/_median.py Outdated
See also
--------
skimage.filters.rank.median : Rank-based implementation of the median
filtering offering more flexibility.

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.

perhaps, say that it does not offer the possibility to pass floats. Otherwise, it sounds like rank must be the default :)

@emmanuelle emmanuelle 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.

Thanks for the PR! I like your solution. Would it be possible to add one test with a floating point array, and one test with a 3D image? Also, when the input is a floating point array and behavior='rank', a warning should be raised I think.

Comment thread skimage/filters/_median.py Outdated
``mask`` is deprecated in 0.16 and will be removed 0.17.
shift_x, shift_y : int
Offset added to the structuring element center point. Shift is bounded
to the structuring element sizes (center must be inside the given

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.

bounded by

Comment thread skimage/filters/_median.py Outdated
shift_x, shift_y : int
Offset added to the structuring element center point. Shift is bounded
to the structuring element sizes (center must be inside the given
structuring element). Only valid when ``behavior='ndimage'``.

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.

when behavior='rank' (if I understand well??)

@glemaitre

Copy link
Copy Markdown
Contributor Author

Also, when the input is a floating point array and behavior='rank', a warning should be raised I think.

It will be already the case as the other rank functions.

@emmanuelle

Copy link
Copy Markdown
Member

@glemaitre I was thinking of a warning advising to use the ndimage mode, but maybe the existing warning is enough.

@emmanuelle

Copy link
Copy Markdown
Member

Looks good to me!

@sciunto sciunto 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.

Thank you!

@sciunto
sciunto merged commit 60ee580 into scikit-image:master Dec 21, 2018
PinkFloyded pushed a commit to PinkFloyded/scikit-image that referenced this pull request Dec 24, 2018
* EHN: median filters will accept floating image
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.

Median filter does not preserve dtype median filter is only available for integer-type images

5 participants