Skip to content

BUG: use fixed normalized CTPS cutoff for automatic ECG detection - #14279

Merged
larsoner merged 6 commits into
mne-tools:mainfrom
AtomicGlance:fix-ctps-trial-threshold
Sep 9, 2026
Merged

larsoner merged 6 commits into
mne-tools:mainfrom
AtomicGlance:fix-ctps-trial-threshold

Conversation

@AtomicGlance

@AtomicGlance AtomicGlance commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

ICA.find_bads_ecg(method="ctps", threshold="auto") now uses a fixed cutoff of 0.3 on the maximum normalized Kuiper score, following the JuMEG example linked in the review. The cutoff no longer depends on sampling frequency or trial count. The normalized scores themselves can still vary with the input data; this does not introduce an effect-size statistic.

Removed the threshold-calculation helpers and updated the docstring and changelog. Regression tests compare automatic detection with an explicit threshold=0.3 across trial counts and sampling rates, with coverage for both Raw and Epochs inputs.

Local validation: 6 tests passed; the optional Picard case was skipped because Picard is not installed. Ruff lint, formatting, and changelog-name checks passed.

AI assistance: Codex assisted with implementation and regression tests, including the revision to the fixed cutoff.

@larsoner larsoner 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.

Can you check the units of _prob_kuiper vs the threshold? I think _prob_kuiper tends toward V**2 and thresold V.

I think it might be worth trying to make the threshold based on effect size, which I think might have been what we were trying to do before the logic changes in #7819

Are you up for looking into these? If not I could try and push

Comment thread mne/preprocessing/tests/test_ica.py Resolved
@AtomicGlance
AtomicGlance marked this pull request as ready for review September 8, 2026 21:22
@larsoner

larsoner commented Sep 8, 2026

Copy link
Copy Markdown
Member

I think it might be worth trying to make the threshold based on effect size, which I think might have been what we were trying to do before the logic changes in #7819

This part I think is still not done, this uses significance still right? Significance is going to be driven by trial count I think, so for example having 20 trials you might get no components but at 1000 trials you might get a dozen, etc. Does that make sense?

@larsoner

larsoner commented Sep 8, 2026

Copy link
Copy Markdown
Member

See for example the plain threshold on ctps in jumeg (BSD 3-clause) uses a fixed cutoff of 0.3 on the max normalized score (we could use 0.25 but I'd just go with what they use actually)... not an effect size but shouldn't change with the number of trials

https://github.com/jdammers/jumeg/blob/36b3b4144495819b9282207a74f63dbdb7a32c89/jumeg/jumeg_preprocessing.py#L379-L414

@AtomicGlance AtomicGlance changed the title BUG: use trial count for automatic CTPS threshold BUG: use fixed normalized CTPS cutoff for automatic ECG detection Sep 9, 2026
@AtomicGlance

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. The update in a482858 uses a fixed 0.3 cutoff on the maximum normalized CTPS score, as in the JuMEG example you linked. The significance-based threshold calculation is removed.

Tests compare "auto" with an explicit 0.3 across trial counts and sampling rates, and cover Raw and Epochs inputs. Locally, 6 tests passed and the optional Picard case was skipped; lint and formatting passed too. The docstring and changelog now describe the fixed cutoff.

@AtomicGlance AtomicGlance left a comment

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 think I took care of things

@AtomicGlance

Copy link
Copy Markdown
Contributor Author

The Windows prerelease failure appears separate from the CTPS change. In Azure build 39927, all four test_ctps_auto_threshold cases passed. The eight failures are in montage/NEDF readers and trace back to AttributeError: module 'defusedxml' has no attribute 'ElementTree' (including two expected-warning failures caused by that exception).

The affected readers and _soft_import are unchanged from the upstream base. Calling _parse_nedf_header(b'<NEDF/>\x00') in a fresh local Python process reproduces the same missing-submodule error.

The focused ICA/CTPS tests also pass locally (6 passed, optional Picard case skipped), with all three added CTPS implementation lines covered. Both Codecov checks are passing; the targets in codecov.yml are informational.

Could a maintainer take a look at the XML import failure and approve the pending GitHub workflows? No additional code changes have been added to this PR for it.

@larsoner larsoner 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.

Pushed a commit to fix the defusedxml issue and touch the ICA example so it runs in CIs. Will mark for merge-when-green once I double check the example output. Thanks in advance @AtomicGlance !

@larsoner
larsoner enabled auto-merge (squash) September 9, 2026 11:58
@larsoner
larsoner disabled auto-merge September 9, 2026 11:58
@larsoner
larsoner enabled auto-merge (squash) September 9, 2026 11:59
@larsoner
larsoner merged commit 1d1ae05 into mne-tools:main Sep 9, 2026
26 checks passed
@welcome

welcome Bot commented Sep 9, 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
larsoner added a commit to natinew77-creator/mne-python that referenced this pull request Sep 11, 2026
* upstream/main: (25 commits)
  ENH: add the JupyterLite notebook setup cell (JupyterLite split 4/5) (mne-tools#14150)
  FIX: Ver [circle deploy] [skip azp] [skip actions]
  Hotfix
  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)
  ...
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.

2 participants