Repository navigation
Sort corner_peaks when providing labels - #8167
Conversation
|
Reviews (1): Last reviewed commit: "Sort corner_peaks even when providing la..." | Re-trigger Greptile |
| if num_peaks is not None and len(coords) > num_peaks: | ||
| # Sort by intensity (highest first) before applying the `num_peaks` limit. Without labels, | ||
| # `peak_local_max` already returns peaks in intensity order, but with labels the peaks | ||
| # are grouped per label, so taking the first `num_peaks` would bias toward the lowest label IDs. | ||
| intensities = image[tuple(coords.T)] | ||
| order = np.argsort(-intensities, stable=True)[:num_peaks] | ||
| coords = coords[order] |
There was a problem hiding this comment.
Sort skipped when
len(coords) == num_peaks with labels
The sort only fires when len(coords) > num_peaks. When the number of remaining peaks equals num_peaks exactly (e.g. three labels each contributing one peak and num_peaks=3), the condition is False and coords is returned in label-grouped order. A caller using indices=True with labels set would get inconsistent ordering depending on whether trimming was actually needed — intensity-sorted when trimming occurs, label-grouped when it does not.
There was a problem hiding this comment.
Ah, I see what's happening. When num_peaks is not None, peaks are sorted by intensity in peak_local_max:
scikit-image/src/_skimage2/feature/_peaks.py
Lines 429 to 434 in f7aeef5
But corner_peaks wraps that function and passes num_peaks=None. So corner_peaks has to take care of the sorting itself, which wasn't happening.
In general I'd agree with the LLM here that the returned peaks should always be sorted by intensity, regardless of num_peaks= in both functions.
However, we aren't keeping corner_peaks in skimage (see #8057), so I think it's the right call to only fix the promised behavior:
When the number of peaks exceeds num_peaks, return num_peaks peaks based on highest peak intensity.
But after merging this, we should strongly consider if we want to always sort the output of peak_local_max.
There was a problem hiding this comment.
Thanks for linking #8057, I also started to look into the duplicated code. Good to know there is already quite some discussion going on.
But after merging this, we should strongly consider if we want to always sort the output of peak_local_max.
It seems to me that only the labels=None path is reliably intensity-sorted. The labels with num_peaks path is sorted only when truncation kicks in, see
scikit-image/src/_skimage2/feature/_peaks.py
Line 429 in 23c26f1
There was a problem hiding this comment.
Yes, if we make peak_local_max consistent in all cases, this PR should be obsolete. Let me look into #8168.
There was a problem hiding this comment.
It seems to me that only the labels=None path is reliably intensity-sorted.
We need to make sure that this is the case. Otherwise, we are looking at changed behavior for the default case and need to decide if we'd consider it a bug (likely not).
NumPy lightly recommends using `assert_equal` for new code [1]. Other changes are just cosmetics for readability. [1] https://numpy.org/devdocs/reference/generated/numpy.testing.assert_array_equal.html#:~:text=consider%20using%20only%20assert_equal
lagru
left a comment
There was a problem hiding this comment.
Thanks @michaelbratsch! I pushed some cosmetic changes but this looks good as is I think.
|
Reviews (2): Last reviewed commit: "Tweak cosmetics and use `assert_equal`" | Re-trigger Greptile |
Conflicts: tests/skimage/feature/test_corner.py
|
Reviews (3): Last reviewed commit: "Merge branch 'main' sort_corner_peaks" | Re-trigger Greptile |
* origin/main: Sort corner_peaks when providing labels (scikit-image#8167)
* origin/main: Sort corner_peaks when providing labels (scikit-image#8167)
Fix
corner_peaksto return in intensity order when usinglabelsandnum_peaksFixes: #8143
Release note (optional)