Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default level parameter in measure.find_contours #4862

Merged
merged 10 commits into from
Aug 6, 2020

Conversation

rubywerman
Copy link
Contributor

@rubywerman rubywerman commented Jul 28, 2020

Description

Fixes #4813
This PR adds the median as the default level parameter in find_contours. After reading the discussion in #4813 and looking at different image arrays [see notebook], I thought the median could be a good default value.

Checklist

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.

@pep8speaks
Copy link

pep8speaks commented Jul 28, 2020

Hello @rubywerman! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-08-06 05:42:59 UTC

@rubywerman rubywerman changed the title Add default level parameter in measure.find_contours [WIP] Add default level parameter in measure.find_contours Jul 28, 2020
@@ -9,7 +9,7 @@


@deprecate_kwarg({'array': 'image'}, removed_version="0.20")
def find_contours(image, level,
def find_contours(image, level='median',
Copy link
Member

Choose a reason for hiding this comment

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

Setting the default to a string may suggest that other string values are accepted ('mean' for example)

Suggested change
def find_contours(image, level='median',
def find_contours(image, level=None,

@rubywerman
Copy link
Contributor Author

Thank you for your feedback @rfezzani! I updated the parameter as you suggested :)

@emmanuelle
Copy link
Member

Thanks for the PR @rubywerman! The discussion about which default level to choose was not completely settled in #4813, there is also the possibility in taking (max - min)/2. The latter choice is mostly useful for binary arrays while the median makes more sense for float arrays. I'd argue that binary arrays are the typical case where one does not want to specify the value of the level, because there is really only one value which makes sense. So I have a slight preference for (max - min)/2 but the median also makes sense.

Please also add a test which executes find_contours without passing the level value, so that the new API is tested.

@rubywerman
Copy link
Contributor Author

rubywerman commented Jul 29, 2020

Thanks for the feedback @emmanuelle! I will add the test. Also, could it be beneficial to have two default values, (max - min) / 2 and median, depending on whether it's a binary or float array?

@jni
Copy link
Member

jni commented Jul 30, 2020

I agree with @sciunto that having different behaviour based on the input type isn't ideal. I have commented on the original issue, but my suggestion would be to have np.mean(np.quantile(image, (0.005, 0.995))) as the default. Thoughts?

@emmanuelle
Copy link
Member

I have reconsidered my initial doubts. Given the fact that there is the possibility to pass a value, and that for a binary image other contours than 0.5 (in fact anything strictly between 0 and 1) will also good give results, we have to accept that there are several possible good solutions and just choose one of them. The one you suggest @jni is a good one, in fact the original median value proposed by @rubywerman is also good (and simple to explain).

@@ -24,7 +24,8 @@ def find_contours(image, level,
image : 2D ndarray of double
Input image in which to find contours.
level : float
Value along which to find contours in the array.
Value along which to find contours in the array. By default, the level
is set to the median value of the array.
Copy link
Member

Choose a reason for hiding this comment

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

please also change the docstring :-)

@@ -130,3 +130,40 @@ def test_invalid_input():
find_contours(r, 0.5, 'foo', 'bar')
with testing.raises(ValueError):
find_contours(r[..., None], 0.5)

Copy link
Member

Choose a reason for hiding this comment

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

thanks for adding all these tests :-)

@emmanuelle emmanuelle changed the title [WIP] Add default level parameter in measure.find_contours Add default level parameter in measure.find_contours Aug 5, 2020
Copy link
Member

@emmanuelle emmanuelle left a comment

Choose a reason for hiding this comment

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

Thanks @rubywerman ! I think the PR is ready now.

if image.ndim > 2:
raise ValueError("Input must be a 2D image")
if image.ndim > 3:
raise ValueError("Input must be a 2D or 3D image")
Copy link
Member

Choose a reason for hiding this comment

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

Why this change in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my mistake– it shouldn't. fixing now

Copy link
Member

Choose a reason for hiding this comment

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

Thank you!

Copy link
Member

Choose a reason for hiding this comment

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

I thought @rubywerman was fixing #4865 at the same time :-) (but it can be in another PR, with a test ;-))

@sciunto
Copy link
Member

sciunto commented Aug 6, 2020

I approve it as well, I'm just waiting for the CI. Thank you for your contribution.

@emmanuelle
Copy link
Member

Merging, thank you @rubywerman !

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.

Make level parameter optional in measure.find_contours
6 participants