Skip to content

Sort corner_peaks when providing labels - #8167

Merged
blink1073 merged 3 commits into
scikit-image:mainfrom
michaelbratsch:feature/sort_corner_peaks
May 30, 2026
Merged

blink1073 merged 3 commits into
scikit-image:mainfrom
michaelbratsch:feature/sort_corner_peaks

Conversation

@michaelbratsch

@michaelbratsch michaelbratsch commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Fix corner_peaks to return in intensity order when using labels and num_peaks

Fixes: #8143

Release note (optional)

In `skimage.feature.corner_peaks`, return peaks in intensity order 
when using `labels` and `num_peaks` as promised.

@greptile-apps

greptile-apps Bot commented May 16, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Sort corner_peaks even when providing la..." | Re-trigger Greptile

Comment thread src/skimage/feature/corner.py Outdated
Comment on lines +1236 to +1242
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]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 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.

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.

Ah, I see what's happening. When num_peaks is not None, peaks are sorted by intensity in peak_local_max:

if num_peaks is not None and len(coordinates) > num_peaks:
out = np.zeros_like(image, dtype=bool)
out[tuple(coordinates.T)] = True
coordinates = _get_high_intensity_peaks(
image, out, num_peaks, min_distance, p_norm
)

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

if num_peaks is not None and len(coordinates) > num_peaks:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I opened for the sorting of the output a merge request: #8168

@lagru To me it seems that it should not interfere with #8057. If it does we can cherry-pick my commits or do it separately on top.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It also seems that we can actually close this merge request, if we intend to merge #8168. The output will also be sorted for corner_peaks.

@lagru Any opinion on that?

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.

Yes, if we make peak_local_max consistent in all cases, this PR should be obsolete. Let me look into #8168.

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.

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

@lagru lagru 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 @michaelbratsch! I pushed some cosmetic changes but this looks good as is I think.

@lagru lagru added the 🩹 type: Bug fix Fixes unexpected or incorrect behavior label May 16, 2026
@greptile-apps

greptile-apps Bot commented May 16, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "Tweak cosmetics and use `assert_equal`" | Re-trigger Greptile

michaelbratsch added a commit to michaelbratsch/scikit-image that referenced this pull request May 17, 2026
Conflicts:
tests/skimage/feature/test_corner.py
@greptile-apps

greptile-apps Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Merge branch 'main' sort_corner_peaks" | Re-trigger Greptile

@blink1073
blink1073 merged commit 8ce7e09 into scikit-image:main May 30, 2026
25 checks passed
@stefanv stefanv added this to the 0.27 milestone May 30, 2026
matthew-brett added a commit to matthew-brett/scikit-image that referenced this pull request May 30, 2026
* origin/main:
  Sort corner_peaks when providing labels (scikit-image#8167)
matthew-brett added a commit to matthew-brett/scikit-image that referenced this pull request May 31, 2026
* origin/main:
  Sort corner_peaks when providing labels (scikit-image#8167)
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

Development

Successfully merging this pull request may close these issues.

corner_peaks does not return the num_peaks in Intensity order when using labels

4 participants