Skip to content

Fix tfce - #14286

Merged
larsoner merged 3 commits into
mne-tools:mainfrom
sonoAESS:fix-tfce
Sep 8, 2026
Merged

Fix tfce#14286
larsoner merged 3 commits into
mne-tools:mainfrom
sonoAESS:fix-tfce

Conversation

@sonoAESS

@sonoAESS sonoAESS commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

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_objects returns clusters as (slice,) tuples. In _find_clusters, the branch elif isinstance(c, tuple): len_c = len(c) always yields an extent of 1, regardless of the actual number of samples. The isinstance(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^E per threshold, mixing cumulative and incremental forms incorrectly. This does not match the reference TFCE integral TFCE = Σ e(h)^E · h^H · dh (Smith & Nichols, 2009), whose Riemann-sum discretization requires |t_i|^H · Δt · e_i^E per threshold step.
Fix: Set dh = abs(thresh) for the first threshold, then dh = abs(thresh - thresholds[ti-1]), making the per-threshold contribution abs(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_obs returned by _permutation_cluster_test are intentional. For TFCE, each point is evaluated independently based on its local neighborhood, and t_obs contains 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):

Scenario Old value Correct value
1D, step=1 1.000 √3 ≈ 1.732
1D, step=0.5 0.750 (0.5²+1²+1.5²)·0.5·√3 ≈ 3.031
2D (1,6), step=0.5 0.750 3.031

(The bug was previously hidden because the 2D extent mask branch was already correct, and with step=1, Δh^H coincidentally equals |t|^H · Δh when Δ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 to test_tfce_thresholds with 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.

Comment thread doc/changes/dev/14224.bugfix.rst Outdated
@@ -0,0 +1 @@
Fix TFCE calculation errors in ``_find_clusters`` for 1D data and incorrect height accumulation. (:user:\`sonoAESS\`)

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.

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?

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.

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.

@larsoner larsoner modified the milestone: 1.13 Sep 8, 2026
@larsoner
larsoner enabled auto-merge (squash) September 8, 2026 21:52
@larsoner larsoner added this to the 1.13 milestone Sep 8, 2026
@larsoner
larsoner merged commit 44ac743 into mne-tools:main Sep 8, 2026
26 checks passed
@welcome

welcome Bot commented Sep 8, 2026

Copy link
Copy Markdown

🎉 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
@CarinaFo CarinaFo mentioned this pull request Sep 13, 2026
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.

TFCE Calculation Error

2 participants