-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add QC + Full MNE Report tutorial #13532
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
Open
varshaa-1616
wants to merge
10
commits into
mne-tools:main
Choose a base branch
from
varshaa-1616:qc-full-report
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+389
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6be9847
Add QC + Full MNE Report tutorial
varshaa-1616 e980448
Remove unintended files from commit
varshaa-1616 26c8247
Merge branch 'main' into qc-full-report
varshaa-1616 7657bb8
Remove unintended generated and unrelated files
varshaa-1616 a58b2db
Revert changes to existing report tutorial
varshaa-1616 0068e70
Merge branch 'main' into qc-full-report
varshaa-1616 0f77772
Wrap forbidden code for docs build; instructions for local execution
varshaa-1616 df423a8
DOC: add QC-focused tutorial using mne.Report
varshaa-1616 1df3937
Merge branch 'main' into qc-full-report
larsoner fc6036a
[autofix.ci] apply automated fixes
autofix-ci[bot] 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,389 @@ | ||
| """ | ||
| .. _tut-qc-report: | ||
|
|
||
| ============================================= | ||
| Quality control (QC) reports with mne.Report | ||
| ============================================ | ||
|
|
||
| Quality control (QC) is the process of systematically inspecting M/EEG data | ||
| **throughout all stages of an analysis pipeline**, including raw data, | ||
| intermediate preprocessing steps, and derived results. | ||
|
|
||
| While QC often begins with an initial inspection of the raw recording, | ||
| it is equally important to verify that signals continue to "look reasonable" | ||
| after operations such as filtering, artifact correction, epoching, and | ||
| averaging. Issues introduced or missed at any stage can propagate downstream | ||
| and invalidate later analyses. | ||
|
|
||
| This tutorial demonstrates how to create a **single, narrative QC report** | ||
| using :class:`mne.Report`, focusing on **what should be inspected and how the | ||
| results should be interpreted**, rather than exhaustively covering the API. | ||
|
|
||
| For clarity and reproducibility, the examples below focus on common QC checks | ||
| applied at representative stages of an analysis pipeline. The same reporting | ||
| approach can—and should—be reused whenever new processing steps are applied. | ||
|
|
||
| We use the MNE sample dataset for demonstration. Not all QC sections are | ||
| applicable to every dataset (e.g., continuous head-position tracking), and | ||
| this tutorial explicitly handles such cases. | ||
|
|
||
| Note: | ||
| The generated HTML report is intended to be opened directly in a browser. | ||
| Some interactive elements (e.g., sliders) may not function correctly | ||
| when the file is served via a local HTTP server. | ||
|
|
||
|
|
||
| """ | ||
|
|
||
| # Authors: The MNE-Python contributors | ||
| # License: BSD-3-Clause | ||
| # Copyright the MNE-Python contributors. | ||
|
|
||
| # %% | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import mne | ||
| from mne.preprocessing import ICA, create_eog_epochs | ||
|
|
||
| # %% | ||
| # Load the sample dataset | ||
| # ---------------------- | ||
| # We load a pre-filtered MEG/EEG recording from the MNE sample dataset. | ||
| # Only channels relevant for QC (MEG, EEG, EOG, stimulus) are retained. | ||
|
|
||
| data_path = Path(mne.datasets.sample.data_path(verbose=False)) | ||
| sample_dir = data_path / "MEG" / "sample" | ||
| subjects_dir = data_path / "subjects" | ||
|
|
||
| raw_path = sample_dir / "sample_audvis_filt-0-40_raw.fif" | ||
|
|
||
|
|
||
| raw = mne.io.read_raw(raw_path, preload=True) | ||
|
|
||
| # Retain only channels relevant for QC to simplify visualization and | ||
| # focus inspection on signals typically reviewed during data quality checks. | ||
| raw.pick(["meg", "eeg", "eog", "stim"]) | ||
|
|
||
| sfreq = raw.info["sfreq"] # Sampling Frequency (Hz) | ||
|
|
||
| # %% | ||
| # Create the QC report | ||
| # ------------------- | ||
| # The report acts as a container that collects figures, tables, and text | ||
| # into a single HTML document. | ||
|
|
||
| report = mne.Report( | ||
| title="Sample dataset – Quality Control report", | ||
| subject="sample", | ||
| subjects_dir=subjects_dir, | ||
| ) | ||
|
|
||
| # %% | ||
| # Dataset overview | ||
| # ---------------- | ||
| # A brief overview helps the reviewer immediately understand the scale and | ||
| # basic properties of the dataset. | ||
|
|
||
| html_overview = f""" | ||
|
|
||
| <ul> | ||
| <li><b>Sampling frequency:</b> {sfreq:.1f} Hz</li> | ||
| <li><b>Duration:</b> {raw.times[-1]:.1f} s</li> | ||
| <li><b>Number of channels:</b> {len(raw.ch_names)}</li> | ||
| </ul> | ||
| <p> | ||
| These values should be checked for consistency with the experimental design. | ||
| Unexpected sampling rates, unusually short recordings, or missing channel | ||
| classes often indicate acquisition or conversion problems. | ||
| </p> | ||
| """ | ||
|
|
||
| report.add_html( | ||
| title="Overview", | ||
| html=html_overview, | ||
| tags=("qc", "overview"), | ||
| ) | ||
|
|
||
| # %% | ||
| # Raw data inspection | ||
| # ------------------- | ||
| # Visual inspection of raw data is the single most important QC step. | ||
| # Here we inspect both the time series and the power spectral density (PSD). | ||
|
|
||
| report.add_raw( | ||
| raw, | ||
| title="Raw data overview", | ||
| psd=True, | ||
| tags=("qc", "raw"), | ||
| ) | ||
|
|
||
| # Interpretation: | ||
| # - Look for channels with unusually large amplitudes or flat signals. | ||
| # - In the PSD, check for excessive low-frequency drift, strong line noise, | ||
| # or abnormal spectral shapes compared to neighboring channels. | ||
|
|
||
| # %% | ||
| # Events and stimulus timing | ||
| # -------------------------- | ||
| # Correct event detection is crucial for all subsequent epoch-based analyses. | ||
|
|
||
| events = mne.find_events(raw) | ||
|
|
||
| report.add_events( | ||
| events, | ||
| sfreq=sfreq, | ||
| title="Detected events", | ||
| tags=("qc", "events"), | ||
| ) | ||
|
|
||
| # Interpretation: | ||
| # - Verify that the number of events matches expectations. | ||
| # - Check that event timing is plausible and evenly distributed. | ||
| # - Missing or duplicated events often indicate trigger channel issues. | ||
| # %% | ||
|
|
||
| # Epoching and rejection statistics | ||
| # -------------------------------- | ||
| # Epoching allows inspection of data segments time-locked to events, along | ||
| # with automated rejection based on amplitude thresholds. | ||
|
|
||
| event_id = { | ||
| "auditory/left": 1, | ||
| "auditory/right": 2, | ||
| "visual/left": 3, | ||
| "visual/right": 4, | ||
| } | ||
|
|
||
| epochs = mne.Epochs( | ||
| raw, | ||
| events, | ||
| event_id=event_id, | ||
| tmin=-0.2, | ||
| tmax=0.5, | ||
| baseline=(None, 0), | ||
| reject=dict(eeg=150e-6), | ||
| preload=True, | ||
| ) | ||
|
|
||
| report.add_epochs( | ||
| epochs, | ||
| title="Epochs and rejection statistics", | ||
| tags=("qc", "epochs"), | ||
| ) | ||
|
|
||
| # Interpretation: | ||
| # - Excessive rejection rates suggest noisy data or overly strict thresholds. | ||
| # - Rejected epochs should be visually inspected to confirm true artifacts. | ||
|
|
||
| # %% | ||
| # Evoked responses | ||
| # ---------------- | ||
| # Averaged responses should show physiologically plausible waveforms and | ||
| # reasonable signal-to-noise ratios. | ||
|
|
||
| cov_path = sample_dir / "sample_audvis-cov.fif" | ||
| evokeds = mne.read_evokeds( | ||
| sample_dir / "sample_audvis-ave.fif", | ||
| baseline=(None, 0), | ||
| ) | ||
|
|
||
| report.add_evokeds( | ||
| evokeds=evokeds[:2], | ||
| noise_cov=cov_path, | ||
| n_time_points=5, | ||
| tags=("qc", "evoked"), | ||
| ) | ||
|
|
||
| # Interpretation: | ||
| # - Check that evoked responses have the expected polarity and timing. | ||
| # - Absence of clear evoked structure may indicate poor data quality or | ||
| # incorrect event definitions. | ||
|
|
||
|
|
||
| # %% | ||
| # ICA for artifact inspection | ||
| # --------------------------- | ||
| # Independent Component Analysis (ICA) can be used during QC to identify | ||
| # stereotypical artifacts such as eye blinks and eye movements. | ||
| # | ||
| # For QC purposes, ICA is typically run with a lightweight configuration | ||
| # (e.g., fewer components or temporal decimation) to provide rapid feedback | ||
| # on data quality, rather than an optimized decomposition for final analysis. | ||
|
|
||
| ica = ICA( | ||
| n_components=15, | ||
| random_state=97, | ||
| max_iter="auto", | ||
| ) | ||
|
|
||
| # Fit ICA using a decimated signal for speed | ||
| ica.fit(raw, picks=("meg", "eeg"), decim=3) | ||
|
|
||
|
|
||
| # Identify EOG-related components | ||
| eog_epochs = create_eog_epochs(raw) | ||
| eog_inds, eog_scores = ica.find_bads_eog(eog_epochs) | ||
| ica.exclude = eog_inds | ||
|
|
||
| report.add_ica( | ||
| ica=ica, | ||
| inst=epochs, | ||
| title="ICA components (artifact inspection)", | ||
| tags=("qc", "ica"), | ||
| ) | ||
|
|
||
| # Interpretation: | ||
| # - Use the topographic maps to identify spatial patterns characteristic | ||
| # of artifacts (e.g., frontal patterns for eye blinks). | ||
| # - The component property viewer is intended for detailed inspection of | ||
| # individual components and is most informative when combined with | ||
| # qe epoched data or explicit artifact scoring. | ||
| # - Components correlated with EOG should show frontal topographies and | ||
| # stereotyped time courses. | ||
| # - Only components clearly associated with artifacts should be excluded. | ||
|
|
||
| # %% | ||
| # Head position / HPI quality control | ||
| # ---------------------------------- | ||
| # Continuous head-position tracking (cHPI) allows monitoring subject movement | ||
| # during MEG acquisition. Not all datasets contain usable cHPI information. | ||
| # This sample dataset does not contain usable cHPI information. | ||
|
|
||
| report.add_html( | ||
| title="Head position / HPI (run locally)", | ||
| html=""" | ||
| <p> | ||
| Continuous head-position tracking (cHPI) estimation can be computationally | ||
| expensive and is therefore typically run selectively during QC. | ||
| If your dataset contains cHPI information, | ||
| you can run the following code locally: | ||
| </p> | ||
|
|
||
| <pre><code class="python"> | ||
| head_pos = mne.chpi.compute_head_pos(raw.info, raw) | ||
| fig = mne.viz.plot_head_positions( | ||
| head_pos, | ||
| mode="traces", | ||
| show=True, | ||
| ) | ||
| </code></pre> | ||
|
|
||
| <p> | ||
| Stable traces indicate minimal head movement. Large drifts suggest | ||
| movement-related artifacts. | ||
| </p> | ||
| """, | ||
| tags=("qc", "hpi"), | ||
| ) | ||
|
|
||
|
|
||
| # Interpretation: | ||
| # - Stable traces indicate minimal head movement. | ||
| # - Large translations or rotations suggest movement-related artifacts and | ||
| # may motivate movement compensation or data exclusion. | ||
|
|
||
| # %% | ||
| # MEG–MRI coregistration | ||
| # --------------------- | ||
| # Accurate coregistration is critical for source localization. | ||
|
|
||
| report.add_html( | ||
| title="MEG–MRI coregistration (run locally)", | ||
| html=""" | ||
| <p> | ||
| Coregistration visualization requires access to MRI surfaces and interactive | ||
| rendering, which require MRI surfaces and interactive 3D visualization. | ||
| Run the following code locally to inspect coregistration quality: | ||
| </p> | ||
|
|
||
| <pre><code class="python"> | ||
| trans_path = sample_dir / "sample_audvis_raw-trans.fif" | ||
| report.add_trans( | ||
| trans=trans_path, | ||
| info=raw_path, | ||
| subject="sample", | ||
| subjects_dir=subjects_dir, | ||
| ) | ||
| </code></pre> | ||
| """, | ||
| tags=("qc", "coreg"), | ||
| ) | ||
|
|
||
|
|
||
| # Interpretation: | ||
| # - Head shape points should align well with the MRI scalp surface. | ||
| # - Systematic misalignment indicates digitization or transformation errors. | ||
|
|
||
| # %% | ||
| # MRI and BEM surfaces | ||
| # ------------------- | ||
| # Boundary Element Method (BEM) surfaces define the head model used for | ||
| # forward and inverse solutions. | ||
|
|
||
| report.add_html( | ||
| title="MRI and BEM surfaces (run locally)", | ||
| html=""" | ||
| <p> | ||
| BEM surface visualization is typically performed interactively when preparing | ||
| source-space analyses. | ||
| To inspect BEM surfaces locally, run: | ||
| </p> | ||
|
|
||
| <pre><code class="python"> | ||
| report.add_bem( | ||
| subject="sample", | ||
| subjects_dir=subjects_dir, | ||
| decim=40, | ||
| ) | ||
| </code></pre> | ||
| """, | ||
| tags=("qc", "bem"), | ||
| ) | ||
|
|
||
|
|
||
| # Interpretation: | ||
| # - Surfaces should be smooth, closed, and non-intersecting. | ||
| # - Poorly formed surfaces can severely degrade source estimates. | ||
|
|
||
| # %% | ||
| # Summary | ||
| # ------- | ||
| # A concise summary provides a checklist-style confirmation of completed QC. | ||
|
|
||
| html_summary = """ | ||
|
|
||
| <ul> | ||
| <li>Raw data and spectra inspected</li> | ||
| <li>Events and epoch rejection verified</li> | ||
| <li>Evoked responses checked for plausibility</li> | ||
| <li>ICA components reviewed for artifacts</li> | ||
| <li>Head position stability assessed (if available)</li> | ||
| <li>Coregistration and BEM validated</li> | ||
| </ul> | ||
| <p> | ||
| For automated, large-scale QC across BIDS datasets, see the reports generated | ||
| by <code>mne-bids-pipeline</code>, which follow a similar philosophy but operate | ||
| at scale. | ||
| </p> | ||
| """ | ||
|
|
||
| report.add_html( | ||
| title="QC summary", | ||
| html=html_summary, | ||
| tags=("qc", "summary"), | ||
| ) | ||
|
|
||
| # %% | ||
| # Save report | ||
| # ----------- | ||
|
|
||
| # %% | ||
| # Save report | ||
| # ---------------------------- | ||
| # Run this script locally to generate the HTML report. | ||
| report.save( | ||
| "qc_report.html", | ||
| overwrite=True, | ||
| open_browser=False, | ||
| ) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to do this? If so -- or if it helps in some way -- a code comment saying why would help users understand