Speed up cluster index list construction in permutation cluster tests - #14217
Merged
Conversation
Group the active indices by component label with a single stable sort instead of masking the labels once per cluster. Output is unchanged.
Lucakaemmer
requested review from
agramfort,
drammock and
larsoner
as code owners
August 25, 2026 08:57
larsoner
approved these changes
Aug 25, 2026
larsoner
left a comment
Member
There was a problem hiding this comment.
Makes sense that this would speed up TFCE since it has so many independent "clusters" (treating each point as one potentially). LGTM, marking for merge-when-green and I approved CIs. Thanks in advance @Lucakaemmer !
larsoner
enabled auto-merge (squash)
August 25, 2026 09:38
|
🎉 Congrats on merging your first pull request! 🥳 Looking forward to seeing more from you in the future! 💪 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference issue (if any)
Follow-up to #13731 and #14057.
What does this implement/fix?
Since #13731/#14057,
_get_clusters_stand_get_componentsbuild the list of cluster index arrays withwhich goes through
labelsonce per cluster. With many clusters this is the main cost of a permutation, especially for TFCE, where it happens at every threshold. This PR replaces it with a stable argsort followed bynp.split, in a small helper used by both functions. The result is the same list (same clusters, same order), just built in one pass. A small test for the helper is included.Timings on a 12 x 143 x 143 lattice (245,388 points, 12 "subjects" of Gaussian noise,
tail=1; Apple M3, numpy 2.5.2, scipy 1.18.1, numba 0.67.0):_find_clusters, TFCE (start=0, step=0.2), global adjacency_find_clusters, TFCE, spatial adjacency withmax_step=1_find_clusters,threshold=0.5(14,788 clusters)permutation_cluster_1samp_test, TFCE, 20 permutationsCluster indices, cluster sums, TFCE scores,
T_obs,H0and p-values are identical betweenmainand this branch on these inputs.Additional information
Drafted and tested the initial implementation with Claude Fable 5.