-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOC: Add epoch quality example #13710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
larsoner
merged 23 commits into
mne-tools:main
from
aman-coder03:enh-epoch-score-quality
Apr 8, 2026
Merged
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
592f079
add Epochs.score_quality() for data-driven epoch quality scoring
aman-coder03 db8a176
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c846793
DOC: Fix encoding of changelog file
aman-coder03 c76aa84
adding example for exploring epoch quality before rejection
aman-coder03 518b6b1
updating newfeature.rst file
aman-coder03 d7b5581
remove score_quality method, keep example only per review feedback
aman-coder03 926f501
updating .rst file
aman-coder03 3fdddec
rename changelog file to match PR number
aman-coder03 08d9cf8
add footcite references and update bib
aman-coder03 d1f02d8
Merge branch 'main' into enh-epoch-score-quality
aman-coder03 7d8f333
build docs
tsbinns c8b6554
Merge branch 'main' into enh-epoch-score-quality
aman-coder03 460c466
restructure as how-to guide
aman-coder03 00580c2
Merge branch 'main' into enh-epoch-score-quality
aman-coder03 0559a7f
Merge branch 'main' into enh-epoch-score-quality
aman-coder03 36edf2e
switching to EEGBCI
aman-coder03 4ba980a
update thresholds
aman-coder03 7c48140
address review comments
aman-coder03 efa9e2d
Merge branch 'main' into enh-epoch-score-quality
aman-coder03 6dd7c67
Merge branch 'main' into enh-epoch-score-quality
aman-coder03 f81a04c
Update thresholds
tsbinns 3d5628a
Merge branch 'main' into enh-epoch-score-quality
aman-coder03 412ec98
Minor text update [skip azp][skip actions]
tsbinns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add a preprocessing example showing how to explore epoch quality before rejection using robust statistics (peak-to-peak amplitude, variance, and kurtosis) inspired by FASTER (Nolan et al., 2010) and Delorme et al. (2007), by `Aman Srivastava`_. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| """ | ||
| .. _ex-epoch-quality: | ||
|
|
||
| ========================================= | ||
| Exploring epoch quality before rejection | ||
| ========================================= | ||
|
|
||
| This example shows how to identify potentially artifactual epochs before | ||
| calling :meth:`mne.Epochs.drop_bad`. We compute per-epoch outlier scores | ||
| from peak-to-peak amplitude, variance, and kurtosis — inspired by FASTER | ||
| :footcite:t:`NolanEtAl2010` and :footcite:t:`DelormeEtAl2007` — and use | ||
| them to rank epochs from cleanest to noisiest before making any rejection | ||
| decisions. | ||
|
tsbinns marked this conversation as resolved.
Outdated
|
||
| """ | ||
| # Authors: Aman Srivastava | ||
| # | ||
| # License: BSD-3-Clause | ||
| # Copyright the MNE-Python contributors. | ||
|
|
||
| # %% | ||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
| from scipy.stats import kurtosis | ||
|
|
||
| import mne | ||
| from mne.datasets import eegbci | ||
|
|
||
| print(__doc__) | ||
|
|
||
| # %% | ||
| # Load the EEGBCI dataset and create epochs | ||
| # ------------------------------------------ | ||
|
tsbinns marked this conversation as resolved.
Outdated
|
||
| raw_fname = eegbci.load_data(subjects=3, runs=(3,))[0] | ||
| raw = mne.io.read_raw(raw_fname, preload=True) | ||
| eegbci.standardize(raw) | ||
| montage = mne.channels.make_standard_montage("standard_1005") | ||
| raw.set_montage(montage) | ||
|
|
||
| events, event_id = mne.events_from_annotations(raw) | ||
| epochs = mne.Epochs(raw, events, tmin=-0.2, tmax=0.5, preload=True, baseline=(None, 0)) | ||
|
|
||
| # %% | ||
| # Compute per-epoch outlier scores | ||
| # --------------------------------- | ||
| # Peak-to-peak amplitude, variance, and kurtosis are computed per epoch. | ||
| # Each feature is z-scored robustly using median absolute deviation (MAD) | ||
|
CarinaFo marked this conversation as resolved.
Outdated
|
||
| # across epochs and averaged into a single outlier score, normalised | ||
| # between [0, 1]. Scores close to 1 indicate likely artifacts. | ||
|
tsbinns marked this conversation as resolved.
Outdated
|
||
|
|
||
| data = epochs.get_data() # (n_epochs, n_channels, n_times) | ||
|
|
||
| ptp = np.ptp(data, axis=-1).mean(axis=-1) | ||
| var = data.var(axis=-1).mean(axis=-1) | ||
| kurt = np.array([kurtosis(data[i].ravel()) for i in range(len(data))]) | ||
|
|
||
| features = np.column_stack([ptp, var, kurt]) | ||
| median = np.median(features, axis=0) | ||
| mad = np.median(np.abs(features - median), axis=0) + 1e-10 | ||
| z = np.abs((features - median) / mad) | ||
|
|
||
| raw_score = z.mean(axis=-1) | ||
| scores = (raw_score - raw_score.min()) / (raw_score.max() - raw_score.min() + 1e-10) | ||
|
|
||
| # %% | ||
| # Plot epoch quality scores | ||
| # -------------------------- | ||
| # Epochs are ranked from cleanest to noisiest. The dashed lines show two | ||
| # example thresholds — demonstrating the quality-quantity trade-off when | ||
| # deciding how many epochs to reject. | ||
|
tsbinns marked this conversation as resolved.
Outdated
|
||
| fig, ax = plt.subplots(layout="constrained") | ||
| sorted_idx = np.argsort(scores) | ||
| ax.bar(np.arange(len(scores)), scores[sorted_idx], color="steelblue") | ||
| ax.axhline(0.8, color="red", linestyle="--", label="Strict threshold (0.8)") | ||
| ax.axhline(0.6, color="orange", linestyle="--", label="Lenient threshold (0.6)") | ||
|
tsbinns marked this conversation as resolved.
Outdated
|
||
| ax.set( | ||
| xlabel="Epoch (sorted by score)", | ||
| ylabel="Outlier score", | ||
| title="Epoch quality scores (0 = clean, 1 = likely artifact)", | ||
| ) | ||
| ax.legend() | ||
|
|
||
| # %% | ||
| # Identify and handle suspicious epochs | ||
| # --------------------------------------- | ||
| # Epochs scoring above the threshold can be inspected visually using | ||
| # :meth:`mne.Epochs.plot`, or dropped directly using | ||
| # :meth:`mne.Epochs.drop`. The threshold should be adapted based on | ||
| # your data and how many epochs you can afford to lose. | ||
|
tsbinns marked this conversation as resolved.
Outdated
|
||
| for threshold in [0.8, 0.6]: | ||
| bad_epochs = np.where(scores > threshold)[0] | ||
| print( | ||
| f"Threshold {threshold}: {len(bad_epochs)} epochs flagged " | ||
| f"out of {len(epochs)} total" | ||
| ) | ||
|
|
||
| # %% | ||
| # Plot epochs at different thresholds | ||
| # ------------------------------------- | ||
| # The worst-scoring epoch (strict threshold) clearly contains an artifact. | ||
| # An epoch from the lenient threshold may look less obvious — illustrating | ||
| # why tuning the threshold matters for the quality-quantity trade-off. | ||
| worst_idx = np.argmax(scores) | ||
| epochs[worst_idx].plot( | ||
| title=f"Strict threshold — worst epoch " | ||
| f"(index {worst_idx}, score={scores[worst_idx]:.2f})", | ||
| scalings=dict(eeg=100e-6), | ||
| ) | ||
|
|
||
| lenient_idx = np.where(scores > 0.6)[0] | ||
| lenient_idx = lenient_idx[lenient_idx != worst_idx][0] | ||
| epochs[lenient_idx].plot( | ||
| title=f"Lenient threshold — borderline epoch " | ||
| f"(index {lenient_idx}, score={scores[lenient_idx]:.2f})", | ||
| scalings=dict(eeg=100e-6), | ||
| ) | ||
|
tsbinns marked this conversation as resolved.
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now there should be a brief point on actually dropping the flagged epochs. |
||
| # %% | ||
| # References | ||
| # ---------- | ||
| # .. footbibliography:: | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.