Preprocessing and epochs
This section covers the steps that turn continuous raw recordings into clean data epochs: the raw preprocessing pipeline, event handling, epoch definitions, artifact rejection, and EEG re-referencing. These steps are shared between Evoked analysis and TRF analysis.
Raw pipeline
- Pipeline.raw
Define a pre-processing pipeline as a series of linked processing steps
(mne refers to continuous data that is not time-locked to a specific event as Raw, with filenames matching *_raw.fif):
|
Raw data source |
|
Filter raw pipe |
|
ICA raw pipe |
|
Apply ICA estimated in a |
|
Maxwell filter raw pipe. |
|
Oversampled temporal projection: see |
|
Re-reference EEG data as preprocessing step |
Each preprocessing step is defined as a named entry with its input as first argument (source).
The raw data that constitutes the input to the pipeline can be accessed as "raw".
For example, the following definition sets up a pipeline for MEG, using TSSS, a band-pass filter and ICA:
class Experiment(Pipeline):
raw = {
'tsss': RawMaxwell('raw', st_duration=10., ignore_ref=True, st_correlation=0.9, st_only=True),
'1-40': RawFilter('tsss', 1, 40),
'ica': RawICA('1-40', 'task', 'extended-infomax', n_components=0.99),
}
To use the raw --> TSSS --> 1-40 Hz band-pass pipeline, use e.set(raw="1-40").
To use raw --> TSSS --> 1-40 Hz band-pass --> ICA, select e.set(raw="ica").
For data recorded with continuous HPI, RawMaxwell can compensate for head movement with head_pos=True:
class Experiment(Pipeline):
raw = {
'tsss': RawMaxwell('raw', st_duration=10., head_pos=True),
}
Head positions are estimated once per recording with mne.chpi.compute_head_pos(), cached, and used for both bad channel detection and Maxwell filtering.
They can be retrieved with Pipeline.load_head_position() for inspection with mne.viz.plot_head_positions().
The setting has no effect on recordings without continuous HPI, and empty room data is never affected.
Pipeline.show_head_position_overview() marks recordings with continuous HPI with †.
For HPI coils driven at known frequencies (Neuromag), the cHPI signals and line noise are removed with mne.chpi.filter_chpi() before Maxwell filtering.
This is controlled by the filter_chpi parameter, which defaults to head_pos but can also be enabled on its own, e.g. for a st_only pipeline.
Segments with excessive movement can be marked with BAD_mov_* annotations through the rotation_velocity_limit, translation_velocity_limit and mean_distance_limit parameters (see mne.preprocessing.annotate_movement()).
When a subject has several recordings (tasks or runs) at different head positions, Maxwell filtering moves all of them to a common position (the duration-weighted average, see Pipeline.show_head_position_overview()), so that they share one forward solution.
This requires the full SSS reconstruction: with st_only=True, only the temporal projection is applied and the data keep their original head position.
Source estimates then require that all recordings of a subject were made at the same head position, so use st_only=False when head positions differ between recordings.
The following is an example for EEG using band-pass filter and ICA:
class Experiment(Pipeline):
raw = {
'1-20': RawFilter('raw', 1, 20, cache=False),
'ica': RawICA('1-20', 'stories'),
# Use the same ICA, but with a high pass filter with a lower cutoff frequency:
'0.2-20': RawFilter('raw', 0.2, 20, cache=False),
'0.2-20ica': RawApplyICA('0.2-20', 'ica'),
}
Note
Continuous files take up a lot of hard drive space.
By default, files for many pre-processing steps are cached.
This can be controlled with the cache parameter: set cache=False to avoid caching.
To remove files that have already been cached, set cache=False and then use Pipeline.clean_cache().
To inspect raw data for a given pre-processing step use:
>>> e.set(raw='1-40')
>>> y = e.load_raw(ndvar=True)
>>> p = plot.TopoButterfly(y, xlim=10, w=0)
Which will plot a 10 s excerpt and allow scrolling through the rest of the data.
For EEG, make sure the montage and adjacency are defined correctly.
They can customized by adding RawSource to Pipeline.raw.
These can be tested with plot.SensorMap:
>>> raw = e.load_raw(raw='raw')
>>> plot.SensorMap(raw, adjacency=True)
plot.SensorMap is also useful for determining sensor names for
Pipeline.references.
Bad channels
Flat channels are automatically excluded from the analysis.
An initial check for noisy channels can be done by looking at the raw data (see
Raw pipeline above), or through the Bad Channels task of the The pipeline GUI.
If this inspection reveals bad channels, they can be excluded using
Pipeline.make_bad_channels().
Another good check for bad channels is plotting the average evoked response, and looking for channels which are uncorrelated with neighboring channels. To plot the average before trial rejection, use:
>>> data = e.load_epochs(epoch='epoch', reject=False)
>>> plot.TopoButterfly('meg', data=data)
The neighbor correlation can also be quantified, using:
>>> nc = neighbor_correlation(concatenate(data['meg']))
# Plot topographical map of the neighbor correlation
>>> plot.Topomap(nc)
# Check for channels whose average correlation with its neighbors is < 0.3
>>> nc.sensor.names[nc < 0.3]
Datalist(['MEG 099'])
# Remove that channel
>>> e.make_bad_channels(['MEG 099'])
A simple way to cycle through subjects when performing a manual pre-processing
step is Pipeline.next().
If a general threshold is adequate, the selection of bad channels based on
neighbor-correlation can be automated using the
Pipeline.make_bad_channels_neighbor_correlation() method:
>>> for subject in e:
... e.make_bad_channels_neighbor_correlation(0.3)
ICA
If preprocessing includes ICA, each subject’s ICA decomposition must be computed and unwanted components must be selected for removal.
The preferred workflow is the The pipeline GUI. Open it, select the ICA task from the Task dropdown, then:
Click Make ICA to compute decompositions for all subjects that are still missing one (runs in the background).
Double-click a subject row to open the ICA component browser and mark components for removal.
Alternatively, the same steps can be performed programmatically.
The raw state must be set to the ICA stage before calling Pipeline.make_ica_selection():
>>> e.set(raw='ica')
>>> e.make_ica_selection()
To cycle through subjects:
>>> e.make_ica_selection(epoch='epoch', decim=10)
>>> e.next()
subject: 'R1801' -> 'R2079'
>>> e.make_ica_selection(epoch='epoch', decim=10)
...
See Pipeline.make_ica_selection() for display options.
Events
By default, events are read from BIDS side-car files.
Triggers in raw data files provide a fallback.
If needed, set Pipeline.merge_triggers to handle spurious events.
Use the Pipeline.variables settings to add event labels.
Events are represented as Dataset objects and can be inspected with
corresponding methods and functions, for example:
>>> e = MyExperiment("~/Data/Experiment")
>>> data = e.load_events()
>>> data.head()
>>> print(table.frequencies('value', data=data))
For more complex designs and variables, you can override methods that provide complete control over the events. These are the transformations applied to events from BIDS side-cars or from raw-file triggers (in this order):
Pipeline.fix_events(): Change event order, timing and remove/add events
Pipeline.variables: Add labels based on triggers
Pipeline.label_events(): Add any more complex labels
Event variables
- Pipeline.variables
Event variables add labels and variables to the events:
|
Variable assigning labels to values |
|
Variable based on evaluating a statement |
|
Group membership for each subject |
Most of the time, the main purpose of this attribute is to turn trigger values
(the value column in the events Dataset) into meaningful labels:
class Mouse(Pipeline):
variables = {
'stimulus': LabelVar('value', {(162, 163): 'target', (166, 167): 'prime'}),
'prediction': LabelVar('value', {162: 'expected', 163: 'unexpected'}),
}
This defines a variable called “stimulus”, and on this variable all events
that have triggers 162 and 163 have the value "target", and events with
trigger 166 and 167 have the value "prime".
The “prediction” variable only labels triggers 162 and 163.
Unmentioned trigger values are assigned the empty string ('').
Some column names are reserved, because the pipeline writes them itself and a
variable of the same name would be overwritten: the BIDS entities (subject,
session, task, acquisition, run), the event columns sample,
value, onset, index, epoch, accept,
interpolate_channels and interpolate_windows, the data columns
epochs, evoked, src, stc, label_tc and model, and
epoch_time, events and tmax (used by
ContinuousEpoch). Using one of these as a variable name raises an
error.
Variables come in two kinds, which differ in where they are added:
Event variables are computed from the events themselves, and are present in all data. This covers
EvalVarand mostLabelVardefinitions.Across-subject variables have definitions that span subjects, and are only added where different subjects’ data are combined. This covers
GroupVar, aLabelVaron'subject', and any variable derived from either, such asEvalVar("diagnosis == 'patient'").
An across-subject variable is thus only present in data that spans subjects
(e.g. e.load_selected_events('all'), but not e.load_selected_events('01')), and
can not be used where data is processed one subject at a time, such as in an
epoch sel expression or as an evoked model. Use a GroupVar to
compare groups (see Subject groups).
Where subjects are combined, each variable is added if the combined data still provides what it is computed from. A variable keyed on the subject, such as a behavioral score, therefore reaches group analyses even where the individual events are no longer present:
variables = {
'score': LabelVar('subject', {'S001': 3.2, 'S002': 4.5}),
}
Variables are applied in the order they are defined, so a variable that builds on another has to come after it.
Whether to cache the output of Pipeline.label_events() (default True).
Set to False if Pipeline.label_events() reads from external files whose changes should trigger cache invalidation.
Epochs
- Pipeline.epochs
Once events are properly labeled, define data epochs in Pipeline.epochs.
Epochs are specified as a {name: epoch_definition} dictionary. Names are
str, and epoch_definition are instances of the classes
described below:
|
Epoch based on selecting events from a raw file |
|
Epoch inheriting events from another epoch |
|
Combine several other epochs |
|
Epoch spanning multiple events for continuous analysis |
|
A collection of epochs that are loaded separately. |
Examples:
epochs = {
# some primary epochs:
'picture': PrimaryEpoch('words', "stimulus == 'picture'"),
'word': PrimaryEpoch('words', "stimulus == 'word'"),
# the picture baseline (e.g., for the noise covariance estimate)
'baseline': SecondaryEpoch('picture', tmax=0),
# another secondary epoch:
'animal_words': SecondaryEpoch('noun', sel="word_type == 'animal'"),
# a superset-epoch:
'all_stimuli': SuperEpoch(('picture', 'word')),
# estimate one TRF for each member epoch:
'stimuli_separate': EpochCollection(('picture', 'word')),
}
The epoch selection is determined by evaluating the
epoch’s sel expression in the events Dataset.
In order to find the right sel parameter, it can be useful to actually
load the events with Pipeline.load_events() and test different
selection strings:
>>> data = e.load_events()
>>> print(data.sub("event == 'value'"))
For datasets with a run entity, PrimaryEpoch combines all runs for
the selected subject/session/task/acquisition by default. To analyze a single run, set the
epoch’s run parameter, for example PrimaryEpoch('task', run='1').
ContinuousEpoch extracts continuous data segments spanning multiple
events, which is mainly useful for TRF analysis of continuous stimuli
(see Epochs and predictor generation for how predictors are generated for the different epoch types).
Epoch rejection
- Pipeline.epoch_rejection
Different methods for artifact rejection in epoched data
can be defined in Pipeline.epoch_rejection as a {name: EpochRejection}
dictionary of trial-rejection settings, selected through the
epoch_rejection state.
|
Rejection from a manually created selection file. |
|
Automatically generated rejection using a |
Bad trials can be manually rejected with ManualRejection, or detected
automatically with ChannelModelRejection.
Automatic rejection can also mark bad EEG channels for interpolation within an
epoch, or within shorter windows for long and variable-length epochs.
Rejections are always specific to a given raw state, primary epoch, and
epoch_rejection setting.
For example:
class Experiment(Pipeline):
epoch_rejection = {
'manual': ManualRejection(),
'auto': ChannelModelRejection(max_interpolate=5),
}
The empty rejection name (epoch_rejection='') is always available and means
that no epoch-level rejection is applied.
In the The pipeline GUI, select the Epoch rejection task, choose the epoch and raw pipeline from the dropdowns, and double-click a subject row to open the rejection GUI for that subject. For automatic rejection, click Compute rejection to generate missing files and double-click rows to inspect them.
Alternatively, cycle through subjects programmatically:
>>> e.set(raw='ica1-40', epoch='word', epoch_rejection='manual')
>>> e.make_epoch_rejection()
>>> e.next()
subject: 'R1801' -> 'R2079'
>>> e.make_epoch_rejection()
...
To reject trials based on a pre-determined amplitude threshold:
>>> for subject in e:
... e.make_epoch_rejection(auto=1e-12)
...
References (re-referencing)
- Pipeline.references
EEG re-referencing applied to epochs after channel interpolation (so that bad
channels do not contaminate the reference). References are defined as a
{name: reference_definition} dictionary and selected through the
reference (EEG re-referencing) state:
|
Re-reference EEG data after epoching and channel interpolation |
An 'average' reference (Reference('average')) is always available. It can
be overridden, for example to reconstruct an implicit recording reference channel
(a channel such as Cz that was the recording reference is absent from the data
but can be reconstructed as zeros before averaging):
references = {
# override the built-in 'average' to reconstruct the implicit Cz reference:
'average': Reference('average', add='Cz'),
# mastoid reference:
'mastoid': Reference(['M1', 'M2']),
}
This differs from RawReReference, which re-references the continuous raw
data before epoching and interpolation. references is orthogonal to
raw, epoch and epoch_rejection, so different references can be compared with
e.set(reference=...) without duplicating epoch definitions.
Note
The reference is only applied to EEG channels. Loading data that contains no
EEG channels with a non-empty reference raises an error; use
reference='' for such data. Source localization handles EEG referencing
internally (via MNE’s average-reference projector) and always uses
reference='' regardless of the current state.
State parameters
raw
Select the preprocessing pipeline applied to the continuous data. Options are
all the processing steps defined in Pipeline.raw, as well as
"raw" for using unprocessed raw data.
epoch
Any epoch defined in Pipeline.epochs. Specify the epoch on which
the analysis should be conducted.
epoch_rejection
Selects an entry from Pipeline.epoch_rejection.
e.set(epoch_rejection='') is always available and disables epoch-level
rejection. Other values correspond to user-defined entries such as
ManualRejection or ChannelModelRejection settings.
reference (EEG re-referencing)
Selects an EEG re-reference defined in Pipeline.references, applied to
epochs after channel interpolation. e.set(reference='') (the default) applies
no epoch-stage re-referencing; e.set(reference='average') applies the
corresponding Reference. Loading sensor-space data that contains no EEG
channels with a non-empty reference raises an error. Source localization
handles EEG referencing internally.