Skip to content

Feature/sort peak local max - #8168

Open
michaelbratsch wants to merge 10 commits into
scikit-image:pre-megamove-integrationfrom
michaelbratsch:feature/sort_peak_local_max
Open

michaelbratsch wants to merge 10 commits into
scikit-image:pre-megamove-integrationfrom
michaelbratsch:feature/sort_peak_local_max

Conversation

@michaelbratsch

@michaelbratsch michaelbratsch commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

The merge request does the following:

  • Sort the output coordinates of peak_local_max by descending peak intensity when providing labels
  • Do not enforce spacing over all labels when being above num_peaks
    • Can be regarded as a bug to not respect the labels for spacing
    • Updated doc strings for clarification
  • Improve the performance of _batched_ensure_spacing

Relates to: #8167
Relates to: #8057

Fixes: #8143

Release note (optional)

Sort the output coordinates of `peak_local_max` by descending peak intensity and respect labels when applying the spacing

@greptile-apps

greptile-apps Bot commented May 16, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "Add unit test for sorted intensities" | Re-trigger Greptile

Comment thread src/_skimage2/feature/_peaks.py Outdated
Comment on lines 434 to 440
if len(coordinates):
# Globally sort by descending intensity so the labels path
# returns peaks in the same order as the labels=None path.
intensities = image[tuple(coordinates.T)]
coordinates = _filter_peaks(
coordinates, intensities, num_peaks, min_distance, p_norm
)

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.

P1 Global spacing enforcement across labels is a breaking behavior change

The new code unconditionally calls _filter_peaks (which runs _ensure_spacing) whenever len(coordinates) > 0 and min_distance > 1. The old code only applied global spacing when num_peaks was set and the count exceeded it. This means peaks from adjacent labels that are within min_distance of each other can now be silently dropped even when num_peaks is None, which was not the case before. A caller using labeled regions (e.g., individual cells) with min_distance=5 would previously keep one peak per label regardless of inter-label distance; now the globally-highest-intensity label's peak can suppress a neighboring label's peak.

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 will look into this.

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.

The old code only applied global spacing when num_peaks was set and the count exceeded it

To me it seems that this is rather incoherent behaviour which is also not documented. I would assume that if one specifies labels then one wants the spacing applied for each label and not overall just because the num_peaks was exceeded.

I implemented it such that the output is sorted by intensity and output at most num_peaks if set. This means min_distance now has a strictly per-label scope when labels is set.

Needs to be discussed as this is a functional change!

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.

Giving you a bit of time to look into this before reviewing. Feel free to ping me once resolved!

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 has been discussed in the community meeting to only enforce the spacing for each label separately. I also updated the doc string accordingly and added a unit test.

@lagru From my side this PR looks good to me.

There has also been the discussion to remove the labels parameter from the peak_local_max function and put that functionality in some wrapper function. I think this should happen in a separate PR on top.

Comment thread tests/skimage2/feature/test_peaks.py Resolved
Comment thread src/_skimage2/feature/_peaks.py Resolved
@lagru
lagru self-requested a review May 16, 2026 15:33
@greptile-apps

greptile-apps Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "Do not enforce min_distance when labels ..." | Re-trigger Greptile

@michaelbratsch
michaelbratsch force-pushed the feature/sort_peak_local_max branch from b37ce71 to 3e44547 Compare May 17, 2026 10:07
@greptile-apps

greptile-apps Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

Reviews (3): Last reviewed commit: "Remove skimage 2 test dependency on skim..." | Re-trigger Greptile

@greptile-apps

greptile-apps Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

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

if len(cand):
diff = coord_batch[cand] - coord_batch[idx]
dist = np.linalg.norm(diff, ord=p_norm, axis=1)
rejected[cand[dist < spacing]] = True

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.

Is this strictly a performance optimization or is this necessary to preserve the ordering by intensity?

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.

This change is not necessary to preserve the ordering by intensity. So I do not mind if we kick it out of the pull request.

I did a small performance check on data.human_mitosis() and it performs good if there are many neighbours. Otherwise the KD-tree query will dominate.

min_distance old (ms) new (ms) speedup #peaks
1 5.50 5.47 1.00x 65797
2 1066.46 790.95 1.35x 18376
3 348.85 228.83 1.52x 7744
5 81.94 60.17 1.36x 2684
10 23.42 17.60 1.33x 670
20 6.55 6.20 1.06x 173
50 4.46 4.42 1.01x 21

@stefanv

stefanv commented May 19, 2026

Copy link
Copy Markdown
Member

Thanks for the PR, @michaelbratsch! Could you update the PR description to outline the motivation for this change?

@stefanv

stefanv commented May 19, 2026

Copy link
Copy Markdown
Member

(Looks like it simply is: this is what we promise to do in the docs. Why; is it useful to have them sorted? If so, looks like an innocuous change.)

@michaelbratsch

Copy link
Copy Markdown
Contributor Author

Thanks for the PR, @michaelbratsch! Could you update the PR description to outline the motivation for this change?

I updated the PR description so that it's easier to follow what has been modified and why it was done.

@greptile-apps

greptile-apps Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Reviews (5): Last reviewed commit: "Add test to respect labels and not do gl..." | Re-trigger Greptile

@michaelbratsch

Copy link
Copy Markdown
Contributor Author

The PR can't be merged due to some merge conflict. Normally I would rebase the branch on main because that seems the cleanest to me.

@lagru If I remember correctly, you rather prefer me to merge the main branch into the feature branch so that the review is easier. Is this true?

@matthew-brett
matthew-brett changed the base branch from main to pre-megamove-integration June 19, 2026 19:59
@matthew-brett

Copy link
Copy Markdown
Contributor

We have changed the base branch for this PR from the main branch to the pre-megamove-integration branch.

Summary - what this means for your PR

You can continue to work on your PR, now based on pre-megamove-integration. You should update your branch to the current state of pre-megamove-integration (this is what the Github "Update branch" button does). Please do not merge the scikit-image main branch into your PR branch.

Once we've merged your PR into the pre-megamove-integration branch, we'll update your changes for Megamove, and merge into the main branch.

In addition - our automated script detected that your branch had merge conflicts with the pre-Megamove branch. Please make sure to merge in the pre-megamove-integration branch, and resolve the merge conflicts in the way that you want.

Read on for the Megamove rationale, and more detailed instructions.

Details

Rationale

We've changed the base branch to make it easier and smoother to continue to work on your PR, while we work through a rather large change in the directory structure of the repository - a change that we have called Megamove.

See the Megamove explanation issue for more detail.

Changing the base branch means you do not see merge conflicts related to the Megamove change, and can continue to work with the repository in its original state, without worrying about porting those changes across to the new post-Megamove structure.

As you'll see in the linked issue, you can continue to work on your PR as before, and you can continue to use the Github GUI to "Update branch" - this updates from the new pre-megamove-integration branch. However, there is one caveat: please, do not try and merge the scikit-image main branch into this one, but instead, keep track of the pre-megamove-integration branch. The issue explains more.

To emphasize again, do not try and merge the scikit-image main branch. You will get Megamove related merge conflicts that are rather tricky to resolve correctly.

When we've approved your PR, we'll merge into the pre-megamove-integration branch, and then process it so we can merge it into the main branch. We'll let you know when we've merged your PR changes into main.

Any questions, please reply here.

This branch has not been deployed

No deployments
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.

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

4 participants