Pipeline Details#
MEGPrep is a Nextflow workflow that combines structural MRI processing, continuous MEG preprocessing, artifact detection, ICA cleaning, optional epoching, covariance estimation, MEG-MRI coregistration, forward modeling, source reconstruction, and static quality-control reporting.
The main workflow is implemented in nextflow/meg_anat_pipeline_for_docker.nf.
Configuration is supplied through nextflow.config and can be overridden by
selected command-line options. See Configuration Reference for the
complete configuration reference.
Execution Modes#
The workflow is controlled by params.steps:
Mode |
Processing scope |
|---|---|
|
MRI import, FreeSurfer or DeepPrep reconstruction, head surface, and BEM. |
|
MEG import, continuous preprocessing, bad-channel and bad-segment detection, then static report. |
|
|
|
|
|
Full MEG workflow using an existing |
|
Structural MRI workflow plus full MEG workflow. |
|
Static HTML report only, using existing outputs. |
High-Level Flow#
The complete meg_all or all execution order is:
MEG import
-> continuous preprocessing
-> artifact detection
-> ICA fit
-> ICA labeling
-> ICA application
-> epoching
-> covariance estimation
-> MEG-MRI coregistration
-> forward solution
-> source reconstruction
-> static HTML report
When anatomy is enabled, structural processing runs before the downstream coregistration and source reconstruction steps:
MRI import
-> FreeSurfer or DeepPrep reconstruction
-> head surface
-> BEM model
-> MEG-MRI coregistration dependencies
Continuous Core Preprocessing#
The continuous MEG core is task independent and applies to both resting-state and task-based recordings.
import_MEG_datasetdiscovers input recordings. BIDS input is filtered bymeg_import_configentities. Raw input is selected byfile_suffixand optionalraw_include_keywords/raw_exclude_keywords. Raw discovery matches both files and directories, so CTF.dsfolders are supported.meg_preproc_oslcallsmeg_preproc_osl.py, which passespreproc_configto OSL-Ephysrun_proc_batch. The listed preprocessing steps are executed in order. Common steps include Maxwell/tSSS for Elekta/MEGIN data, band-pass filtering, notch filtering, and resampling. Resampling is the current configurable downsampling mechanism.detect_Artifactscallsmeg_detect_artifacts.py. It detects bad channels and bad time spans using the configured PyPREP, PSD, OSL, and MNE methods. It writes*_bad_channels.txtand*_bad_segments.txtand can generate waveform images for manual review.run_ICAloads the preprocessed raw file plus the artifact sidecars. Bad channels are excluded from picks, and bad annotations are ignored during ICA fitting throughreject_by_annotation=True.run_IC_labellabels artifact-related ICA components using the configured ECG, EOG, MNE-ICLabel, and rule-based settings.apply_ICAloads the marked components, applies the ICA solution, and saves*_clean_raw.fif. The cleaned continuous file keeps the bad-channel and bad-segment metadata.
Interactive Edits and Resume#
Some sidecar files can be edited after a Nextflow run through the interactive
reports. MEGPrep includes content hashes of those files in the relevant
Nextflow task inputs so -resume can invalidate only the affected downstream
tasks:
Editing
artifact_report/*/*_bad_channels.txtorartifact_report/*/*_bad_segments.txtinvalidates ICA fitting and later steps for that recording.Editing
ica_report/*/marked_components.txtinvalidates ICA application and later steps for that recording.Editing
trans/*/coreg-trans.fifinvalidates forward modelling and source reconstruction for that recording.
Bad Segments: Marking vs Exclusion#
Artifact detection marks bad segments as MNE annotations. This does not cut samples out of the continuous raw file. Downstream steps decide whether the annotations should exclude data:
ICA fitting ignores annotated spans when estimating ICA components.
ICA application writes a cleaned raw file with annotations attached.
Epoching drops epochs overlapping annotations only when
epoch_config.epochs.reject_by_annotationis true.Additional epoch rejection can come from
epoch_config.epochs.rejector optionalautoreject.
Resting-State and Task-Based Epochs#
Epoching is optional and happens after the continuous core. epoch_config
selects how epochs are built:
task_type: restingcreates fixed-length events withresting.fixed_length_duration.task_type: taskwithevent_source: find_eventsuses MNEfind_eventsand thefind_eventsconfig block.task_type: taskwithevent_source: event_filereads BIDS*_events.tsvfiles and applies theevent_filefilters or label-to-id mappings.exclude_event_idcan be set to one id or a list of ids to remove those events before epoching. Withepochs.event_id: null, MEGPrep keeps all remaining event ids.
The resulting epoch FIF file and rejection log are written under
preprocessed/epochs/<recording>/.
Covariance and Empty-Room Style Records#
Covariance is computed only in the full MEG stage. Two modes are available:
covar_type = "epochs"estimates noise covariance from baseline epochs created from each cleaned experimental recording.covar_type = "raw"estimates noise covariance from a continuous raw recording selected byraw_covariance_task_id.
For raw covariance, MEGPrep pairs experimental recordings with a noise or
baseline recording by replacing the BIDS task-... part of the filename with
task-${params.raw_covariance_task_id}. This is the current mechanism for
empty-room or empty-room-like recordings. For example, if
raw_covariance_task_id = "emptyroom", an experimental file with
task-aef is paired with a file whose matching name contains
task-emptyroom when that file exists.
Coregistration, Forward Model, and Source Reconstruction#
coregistration or coregistrations aligns MEG sensor space to the
subject anatomy. The process uses fiducial fitting, ICP, and a fine-tuned ICP
stage controlled by core_config. It writes coreg-trans.fif,
coregistration figures, and distance summaries.
forward_solution builds the forward model using the epoch file, transform,
FreeSurfer subject directory, and fwd_config.
source_imaging consumes either epochs or raw data according to src_type.
It loads the forward model and noise covariance and then applies the configured
source methods in src_config.
Static HTML Report#
At the end of each selected MEG milestone, generate_static_html_report scans
the existing outputs and writes a portable report under
params.output_dir/static_html_report. The report includes a workflow
manifest, a config snapshot when available, subject pages, dataset summaries,
and evidence files. See Reports and
Quality Control Metrics for details.
Primary Outputs by Step#
Step |
Output location |
Main outputs |
|---|---|---|
Continuous preprocessing |
|
|
Artifact detection |
|
Bad-channel text file, bad-segment annotations, review images. |
ICA |
|
ICA FIF, source FIF, marked components, ECG/EOG scores, plots. |
ICA-clean raw |
|
|
Epochs |
|
|
Covariance |
|
|
Coregistration |
|
|
Forward model |
|
Forward solution FIF and head-model figures. |
Source reconstruction |
|
Source estimate files and visualization figures. |
Static report |
|
Dataset dashboard, subject pages, JSON/CSV summaries. |