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

Refactor and fix peak_local_max #4760

Merged
merged 43 commits into from Nov 7, 2020

Conversation

rfezzani
Copy link
Member

@rfezzani rfezzani commented May 27, 2020

Description

In my attempt to fix #2592, I made a review of the code and found many issues (see #4756, #4749, #4752 and #3990 (comment)). It appeared to me that a global refactoring of peak_local_max is necessary.

Multiple changes are applied in this PR:

If accepted, this PR allows to deprecate corner_peak that becomes a duplicate of peak_local_max.

Fixes #2592. Fixes #4756. Fixes #4756. Closes #3990. Fixes #5047 .

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.

@rfezzani rfezzani added the 🩹 type: Bug fix Fixes unexpected or incorrect behavior label May 28, 2020
skimage/feature/peak.py Outdated Show resolved Hide resolved
rfezzani and others added 2 commits July 27, 2020 14:01
Co-authored-by: Marianne Corvellec <marianne.corvellec@ens-lyon.org>
@rfezzani
Copy link
Member Author

Thank you @mkcor for your review ;)

@rfezzani rfezzani changed the title Refactor and fix peack_local_max Refactor and fix peak_local_max Aug 27, 2020
@sciunto sciunto self-requested a review August 29, 2020 07:59
@sciunto sciunto self-assigned this Aug 29, 2020
@rfezzani
Copy link
Member Author

Can we plan this PR for 0.18 or is the schedule too short?

@rfezzani
Copy link
Member Author

rfezzani commented Nov 6, 2020

@jni, here is the error reported by @emmanuelle: in corner_peaks doctest we have

>>> from skimage.feature import peak_local_max
>>> response = np.zeros((5, 5))
>>> response[2:4, 2:4] = 1
>>> response
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 1., 1., 0.],
       [0., 0., 1., 1., 0.],
       [0., 0., 0., 0., 0.]])
>>> peak_local_max(response)
array([[2, 2]])

but with the last fix

>>> peak_local_max(response)
array([[2, 2],
       [2, 3],
       [3, 2],
       [3, 3]])

that's the result with min_distance=1 which is equivalent to min_distance=0!!!

I think that in this case, peak_local_max(response, min_distance=1) must be equal to array([[2, 2]]). Am I wrong?

@rfezzani
Copy link
Member Author

rfezzani commented Nov 6, 2020

This is again a pixel centred VS grid centred point of view problem I think!

@jni
Copy link
Member

jni commented Nov 6, 2020

This is again a pixel centred VS grid centred point of view problem I think!

I don't think so? In either case the distance to the adjacent pixels is exactly 1 or sqrt(2)?

@jni
Copy link
Member

jni commented Nov 6, 2020

I think the new result is what I would expect. It's unclear to me why it wasn't that before. OR, if you want, change it to use min_distance=2 and the problem should be solved! =)

@rfezzani
Copy link
Member Author

rfezzani commented Nov 6, 2020

This is again a pixel centred VS grid centred point of view problem I think!

I don't think so? In either case the distance to the adjacent pixels is exactly 1 or sqrt(2)?

I say that in the sens that pixel (0, 1) is adjacent to pixel (0, 2) (the distance between them is 0 because they join at (0, 0.5)). But the points (0, 1) is separated from the point (0, 2) by a distance of 1.

@rfezzani
Copy link
Member Author

rfezzani commented Nov 6, 2020

In other words, do we consider that a pixel is a point or "a little square"? I know, pixels are not squares, but it can be seen as such 😄 (grid centred VS cell centred)

@rfezzani
Copy link
Member Author

rfezzani commented Nov 6, 2020

BTW, corner_peaks returns array([[2, 2]]) on this example, and this PR mimic the strategy implemented in corner_peaks.
A wanted to propose a PR to deprecate corner_peaks once this one is merged...

@jni
Copy link
Member

jni commented Nov 6, 2020

I say that in the sens that pixel (0, 1) is adjacent to pixel (0, 2) (the distance between them is 0 because they join at (0, 0.5)). But the points (0, 1) is separated from the point (0, 2) by a distance of 1.

Well, I think this is a can of worms that we do not want to open, since this definition no longer satisfies the definition of a metric between pixels.

What do you think of my proposal to add min_distance=2 to the example so we can merge this? ;)

@jni
Copy link
Member

jni commented Nov 6, 2020

A wanted to propose a PR to deprecate corner_peaks once this one is merged...

I'd be into that, I was always confused by that function. But probably for 0.19 as that is a major API change.

@rfezzani
Copy link
Member Author

rfezzani commented Nov 6, 2020

OK, let's keep the old behavior considering that consecutive pixels are separated by a distance of 1 for now 😉.

I'd be into that, I was always confused by that function. But probably for 0.19 as that is a major API change.

Let's go for that too, but we will then need to decide which behavior to keep (peak_local_max or corner_peaks)

@jni
Copy link
Member

jni commented Nov 7, 2020

Ok, as far as I can tell, the failures have nothing to do with this PR. @scikit-image/core anyone else ready to push the green button?

Copy link
Contributor

@grlee77 grlee77 left a comment

Choose a reason for hiding this comment

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

I will approve now, but possibly add something about these changes as in the criteria:

  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.

skimage/_shared/tests/test_coord.py Outdated Show resolved Hide resolved
@rfezzani
Copy link
Member Author

rfezzani commented Nov 7, 2020

Thank you @grlee77, I added the changes to the release note as you advised 😉.

@grlee77 grlee77 merged commit a04bd6b into scikit-image:master Nov 7, 2020
@rfezzani
Copy link
Member Author

rfezzani commented Nov 7, 2020

🎉 thank you all for your reviews! I am happy to see this in 0.18 😄

@stefanv
Copy link
Member

stefanv commented Nov 9, 2020

@rfezzani Just wanted to give you a personal shout-out: the peak finder was performing poorly, and when I looked into what should be done to fixed it I discovered you had already done the work. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🩹 type: Bug fix Fixes unexpected or incorrect behavior
Projects
None yet
9 participants