Repository navigation
Fix tfce - #14286
Merged
Merged
Fix tfce#14286
Conversation
sonoAESS
requested review from
agramfort,
drammock and
larsoner
as code owners
September 8, 2026 17:28
larsoner
reviewed
Sep 8, 2026
| @@ -0,0 +1 @@ | |||
| Fix TFCE calculation errors in ``_find_clusters`` for 1D data and incorrect height accumulation. (:user:\`sonoAESS\`) | |||
Member
There was a problem hiding this comment.
These docs are for end-users not devs so we try to document changes to public functions... so maybe this should refer to
:func:`mne.stats.permutation_cluster_test` and related functions
or similar?
Contributor
Author
There was a problem hiding this comment.
Good point, thanks! I've updated the changelog entry to reference the public functions (permutation_cluster_test, permutation_cluster_1samp_test...) and framed the fix in terms of user-facing behavior. I also made sure the :footcite:SmithNichols2009`` reference is properly included.
|
🎉 Congrats on merging your first pull request! 🥳 Looking forward to seeing more from you in the future! 💪 |
larsoner
added a commit
to ualsbombe/mne-python
that referenced
this pull request
Sep 10, 2026
…rain_signed * upstream/main: (251 commits) Coregistration GUI modernisation (phase 4) (mne-tools#14285) Dipole GUI interactivity and visual enhancements (phase 2) (mne-tools#14291) Width [circle deploy] [skip azp] [skip actions] Post-release deprecations (mne-tools#14290) FIX: Come on [circle deploy] [skip azp] [skip actions] PyVista try again [circle deploy] [skip azp] [skip actions] FIX: PyVista [circle deploy] [skip azp] [skip actions] FIX: Really [circle deploy] [skip azp] [skip actions] FIX: Doc Release commit [circle deploy] MAINT: Update code credit (mne-tools#14289) MAINT: Update pre-commit hook versions (mne-tools#14288) BUG: use fixed normalized CTPS cutoff for automatic ECG detection (mne-tools#14279) Move toward docstacks [circle deploy] [skip azp] [skip actions] Fix tfce (mne-tools#14286) FIX: Improve channel name rendering in `plot_alignment` (mne-tools#13773) Don't shift placeholder proc_history dates in anonymize_info (mne-tools#14248) MRG: Avoid pytest 10 warnings in sklearn estimator checks (mne-tools#14283) MAINT: Update pre-commit hook versions (mne-tools#14282) DOC: Add osl-dynamics to related software list (mne-tools#14268) ...
larsoner
added a commit
to drammock/mne-python
that referenced
this pull request
Sep 11, 2026
* upstream/main: (75 commits) Brain GUI modernization (Phase 7) (mne-tools#14270) Fix bug with SciPy 1.18.0 EEGLAB reading (mne-tools#14293) remove plotting abs as the defauls for plotting volumetric (mne-tools#13989) Coregistration GUI modernisation (phase 4) (mne-tools#14285) Dipole GUI interactivity and visual enhancements (phase 2) (mne-tools#14291) Width [circle deploy] [skip azp] [skip actions] Post-release deprecations (mne-tools#14290) FIX: Come on [circle deploy] [skip azp] [skip actions] PyVista try again [circle deploy] [skip azp] [skip actions] FIX: PyVista [circle deploy] [skip azp] [skip actions] FIX: Really [circle deploy] [skip azp] [skip actions] FIX: Doc Release commit [circle deploy] MAINT: Update code credit (mne-tools#14289) MAINT: Update pre-commit hook versions (mne-tools#14288) BUG: use fixed normalized CTPS cutoff for automatic ECG detection (mne-tools#14279) Move toward docstacks [circle deploy] [skip azp] [skip actions] Fix tfce (mne-tools#14286) FIX: Improve channel name rendering in `plot_alignment` (mne-tools#13773) Don't shift placeholder proc_history dates in anonymize_info (mne-tools#14248) ... # Conflicts: # mne/io/edf/tests/test_edf.py
larsoner
added a commit
to natinew77-creator/mne-python
that referenced
this pull request
Sep 11, 2026
* upstream/main: (22 commits) fix EDF round-trip (mne-tools#14296) Brain GUI modernization (Phase 7) (mne-tools#14270) Fix bug with SciPy 1.18.0 EEGLAB reading (mne-tools#14293) remove plotting abs as the defauls for plotting volumetric (mne-tools#13989) Coregistration GUI modernisation (phase 4) (mne-tools#14285) Dipole GUI interactivity and visual enhancements (phase 2) (mne-tools#14291) Width [circle deploy] [skip azp] [skip actions] Post-release deprecations (mne-tools#14290) FIX: Come on [circle deploy] [skip azp] [skip actions] PyVista try again [circle deploy] [skip azp] [skip actions] FIX: PyVista [circle deploy] [skip azp] [skip actions] FIX: Really [circle deploy] [skip azp] [skip actions] FIX: Doc Release commit [circle deploy] MAINT: Update code credit (mne-tools#14289) MAINT: Update pre-commit hook versions (mne-tools#14288) BUG: use fixed normalized CTPS cutoff for automatic ECG detection (mne-tools#14279) Move toward docstacks [circle deploy] [skip azp] [skip actions] Fix tfce (mne-tools#14286) FIX: Improve channel name rendering in `plot_alignment` (mne-tools#13773) ... # Conflicts: # mne/viz/backends/tests/test_renderer.py
Merged
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
Fixes #14224.
What does this implement/fix?
This PR fixes two bugs in the TFCE score computation inside
_find_clusters(mne/stats/cluster_level.py), and clarifies a third point raised in the issue that is actually by design.Bug 1 — Cluster extent counted as 1 for 1D data
For 1D data without adjacency,
ndimage.find_objectsreturns clusters as(slice,)tuples. In_find_clusters, the branchelif isinstance(c, tuple): len_c = len(c)always yields an extent of 1, regardless of the actual number of samples. Theisinstance(c, slice)branch is currently dead code in all paths.Fix: Compute the extent from the slice bounds:
len_c = c[0].stop - c[0].start.Bug 2 — Per-threshold height uses the wrong formula
The code accumulated
Δh^H · e^Eper threshold, mixing cumulative and incremental forms incorrectly. This does not match the reference TFCE integralTFCE = Σ e(h)^E · h^H · dh(Smith & Nichols, 2009), whose Riemann-sum discretization requires|t_i|^H · Δt · e_i^Eper threshold step.Fix: Set
dh = abs(thresh)for the first threshold, thendh = abs(thresh - thresholds[ti-1]), making the per-threshold contributionabs(thresh)**h_power * dh * len_c**e_power.Point 3 from the issue — By design, unchanged
The per-point cluster list and TFCE-valued
t_obsreturned by_permutation_cluster_testare intentional. For TFCE, each point is evaluated independently based on its local neighborhood, andt_obscontains the enhanced statistic, not the raw t-values. This is documented in the code and consistent with the background stats tutorial.Reproducible ground truth
A bump of height 2 over 3 samples (
tail=1):step=1step=0.5(1,6),step=0.5(The bug was previously hidden because the 2D extent mask branch was already correct, and with
step=1,Δh^Hcoincidentally equals|t|^H · ΔhwhenΔh = t = 1.)Changes
mne/stats/cluster_level.py: 9 insertions, 6 deletions. Only the TFCE block in_find_clusters. No API changes, no new dependencies.mne/stats/tests/test_cluster_level.py: Regression test added totest_tfce_thresholdswith analytic 1D (tuple-slice) and 2D (boolean-mask) ground-truth assertions.doc/changes/dev/14224.bugfix.rst: Changelog entry added.doc/changes/names.inc: Contributor name added.Verification
pytest mne/stats/tests/test_cluster_level.py— all tests passed (including the new regression test).pre-commit run -a— all hooks green.Additional information
AI Assistance Disclosure:
I used an AI coding assistant (OpenCode with the Big Pickle model) to help structure this PR description and refine the mathematical explanation of the bug. However, I personally identified the root cause in the codebase, wrote and verified the ~9-line fix in
_find_clusters, and manually validated the analytical ground truth tests. I take full responsibility for the correctness and scope of this contribution.