Introduction
This page introduces basic concepts used by the pipeline: the pipeline script, the pipeline GUI, state parameters, and caching.
Workflow
Working with a Pipeline typically involves 3 different components or workflows:
Setting up the
PipelinescriptData preparation
Analysis
These can be achieved in different ways, but the following are the recommended steps.
The Pipeline script
Pipeline is a template for the pipeline.
This template is adapted to a specific experiment by specifying properties of the experiment as attributes (technically, by creating a subclass).
The recommended workflow for this is to write a separate script containing this subclass (e.g., pipeline.py).
This provides a stable record of global analysis settings.
An instance of this pipeline then provides access to different analysis stages through its methods:
.load_...methods are for loading data and results. Most of these return Eelbrain data types by default, but they can be used to loadmneobjects by settingndvar=False(e.g.,Pipeline.load_epochs()).
.show_...methods are for retrieving and displaying information at different stages.
.plot_...methods are for generating plots of the data.
.make_...methods are for programmatically accessing processing steps that require user input, like ICA component selection, and caching some intermediate results.
For example, Pipeline.load_test() can be used to directly load a mass-univariate test result, without a need to explicitly load data at any intermediate stage.
On the other hand, Pipeline.load_epochs() can be used to load the corresponding data epochs, for example to perform a different analysis that may not be implemented in the pipeline.
It is recommended to organize analysis scripts in a dedicated folder, separate from the dataset.
For example, code in ~/Code/MyProject for a dataset at ~/Data/MyProject.
Version-controlling a separate code folder (e.g., with Git) makes it easy to track the history of your analysis.
The project folder typically contains:
A
Pipelinesubclass that describes the experiment structure — by convention inpipeline.py.Analysis scripts and/or notebooks that import the pipeline.
A minimal MyProject/pipeline.py looks like this:
from eelbrain.pipeline import *
ROOT = "~/Data/MyProject" # Where the data is stored
class MyExperiment(Pipeline):
# Define experiment attributes here
Note
If your project contains Jupyter Notebooks, consider Jupytext to efficiently track those notebooks in Git.
Data preparation
Data preparation involves steps that require visual inspection and human decisions, like bad-channel marking, ICA component selection, trial rejection, and MRI coregistration. The preferred tool for all of these is the pipeline GUI, launched from the command line:
$ cd ~/Code/MyProject
$ eelbrain
The GUI shows the status for every subject in a single table and opens the relevant sub-GUI (ICA component browser, epoch rejection viewer, MNE coregistration tool) on double-click. It also lets you compute ICA decompositions for all missing subjects in one click.
The same steps can alternatively be performed programmatically from an interactive Python session (iPython, a Jupyter notebook, or a terminal), which is useful for scripting or automation:
>>> e = eelbrain.load_pipeline("~/Code/MyProject")
>>> e.make_ica_selection() # opens ICA GUI for current subject
>>> e.next() # advance to next subject
>>> e.make_epoch_rejection() # opens epoch rejection GUI
Analysis
Once data preparation is complete, statistical analysis and visualization are best done in notebooks or analysis scripts that can be re-run as needed:
import eelbrain
e = eelbrain.load_pipeline()
result = e.load_test('my_test', tstart=0.1, tstop=0.3)
eelbrain.plot.brain.cluster(result.clusters[0], ...)
Notebooks and scripts typically live in the project code directory alongside pipeline.py and can be version-controlled together with the pipeline definition.
Loading the pipeline: eelbrain.load_pipeline()
eelbrain.load_pipeline() is the recommended way to instantiate a pipeline from any location — the command line, a Jupyter notebook, or an interactive Python session.
It searches for pipeline.py (and then experiment.py) when given a directory, and reads the root variable and the Pipeline subclass automatically:
>>> import eelbrain
>>> e = eelbrain.load_pipeline("~/Code/MyProject")
If you are already working inside the project directory, omit the path entirely - this allows relative imports for analysis scripts in the project directory:
>>> # from MyProject/my_analysis.py:
>>> e = eelbrain.load_pipeline()
>>> # from MyProject/analysis/my_analysis.py:
>>> e = eelbrain.load_pipeline('..')
For advanced Python workflows, you can also import the class directly:
>>> from my_experiment import MyExperiment
>>> e = MyExperiment("~/Data/Experiment")
The pipeline GUI
The pipeline GUI is the recommended tool for all data-preparation steps.
Launch it from the command line by pointing it at the project directory (or any path accepted by eelbrain.load_pipeline()):
$ eelbrain ~/Code/MyProject
With no argument it uses the current working directory:
$ cd ~/Code/MyProject
$ eelbrain
The GUI opens a window with a Task dropdown that gives access to:
- Bad Channels
Shows and allows modifying bad channels. Double-click on a row to open a visualization of the raw data. Right-click to get bad channels as text.
- ICA
Shows the ICA status (missing / selected / number of components rejected) for every subject. Double-clicking a row opens the ICA component selection browser for that subject. If the ICA decomposition is missing, it is computed first, which can take some time. The Make ICA button computes ICA decompositions for all subjects that are still missing one.
- Epoch rejection
Shows the trial-rejection status (done / missing) for the selected epoch, rejection method, and raw pipeline combination. Double-clicking opens the epoch rejection GUI for that subject. For automatic rejection methods, the GUI is read-only and the Compute rejection button generates missing rejection files.
- MRI
Shows whether each subject has a FreeSurfer reconstruction (full recon, scaled template, or missing) and whether the common brain (fsaverage) is present. Double-clicking the common-brain row when it is missing offers to download fsaverage automatically.
- Coregistration
Shows the coregistration status (OK / missing) for each subject–session combination. Double-clicking opens the MNE coregistration GUI pre-loaded with the subject’s raw file and, if one already exists, the current transformation. For subjects without a FreeSurfer reconstruction the GUI opens against the template brain so the user can use MNE’s “Scale MRI” feature to create a scaled copy.
State parameters
A Pipeline instance has a state, which determines what data and settings it is currently using.
Not all settings are always relevant.
For example, subject is relevant for steps applied separately to each subject, like load_trf(), whereas group defines the group of subjects in group level analysis, such as in load_trfs() or load_model_test().
State parameters can be set after a Pipeline has been initialized to affect the analysis, for example:
>>> my_experiment = eelbrain.load_pipeline()
>>> my_experiment.set(raw='ica', epoch='story')
sets up my_experiment to use the "ica" node of the Raw pipeline, and the "story" epoch (defined in Pipeline.epochs). Most methods also accept state parameters, so Pipeline.set() does not have to be used separately:
>>> trf = my_experiment.load_trf(..., raw='ica', epoch='story')
Each state parameter is further documented in the section of this guide it belongs to:
Parameter |
Selects |
Section |
|---|---|---|
The current subject |
||
The recording session to work with |
||
The task to work with (usually set through |
||
The BIDS acquisition parameter set to analyze |
||
The run to work with |
||
The preprocessing pipeline for continuous data |
||
The data epoch for analysis |
||
The epoch-level artifact rejection method |
||
The EEG re-reference applied to epochs |
||
The MEG/EEG-subject to MRI-subject mapping |
||
The noise covariance estimation method |
||
The source space |
||
The inverse solution |
||
The brain parcellation |
||
Channel adjacency for cluster-based tests |
||
The subject group for group-level analysis |
||
Equalizing epoch counts across conditions |
Basic configuration
Determines the amount of information displayed on the screen while using
a Pipeline (see logging).
This class attribute is used as the default for the screen_log_level
initialization parameter.
The defaults dictionary can contain default settings for experiment analysis parameters (see State parameters), e.g.:
defaults = {
'epoch': 'my_epoch',
'cov': 'ad_hoc',
'raw': '1-40',
}
Caching
Pipeline caches intermediate results and validates them when they are
loaded. Stale intermediate cache entries are recomputed on demand. Files
stored outside cache-dir are treated as user-managed outputs and are not
overwritten automatically when they become stale; the corresponding error or GUI
dialog explains whether to recompute, delete, or explicitly accept the existing
file.
Cache files become stale when the relevant pipeline definitions or data sources change.
Such files are not automatically deleted (and can be accessed again by restoring the relevant definitions).
Use Pipeline.clean_cache() to scan the cache and delete files that are stale.
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 of the corresponding node definition:
set cache=False to avoid caching.
To remove files that have already been cached, set cache=False and then use Pipeline.clean_cache().
Example
The following is a complete example for an experiment class definition file
(the source file can be found in the Eelbrain examples folder at
examples/imagenet/pipeline.py):
# skip test: data unavailable
from eelbrain.pipeline import Pipeline, RawFilter, RawICA, LabelVar, PrimaryEpoch, SecondaryEpoch, TTestOneSample, TTestRelated, ANOVA
root = '~/Data/ds005810'
class ImageNet(Pipeline):
preload = True
ignore_entities = {
'ignore_subjects': ('02', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', 'emptyroom'),
'ignore_sessions': ('ImageNet02', 'ImageNet03', 'ImageNet04', 'MRI'),
'ignore_runs': ('02'),
}
raw = {
'1-40': RawFilter('raw', 1, 40),
'ica': RawICA('1-40', 'ImageNet', n_components=0.99),
}
variables = {
'position': LabelVar('value', {1: 'begin', 2: 'end', (3, 4): 'middle'}),
'event': LabelVar('value', {(1, 2): 'unused', 3: 'resp', 4: 'stim_on'}),
}
epochs = {
'used': PrimaryEpoch('ImageNet', "position == 'middle'", samplingrate=200),
'resp': SecondaryEpoch('used', "event == 'resp'"),
'stim_on': SecondaryEpoch('used', "event == 'stim_on'"),
'cov': SecondaryEpoch('used', tmax=0),
}
tests = {
'=0': TTestOneSample(),
'connection': TTestRelated('event', 'stim_on', 'resp'),
'anova': ANOVA('event * subject'),
}
The event structure is illustrated by looking at the first few events:
>>> e = load_pipeline()
>>> data = e.load_events()
>>> data.head()
# sample value event onset SOA subject position
-------------------------------------------------------------------------
0 2814 1 unused 2.345 5.0392 01 begin
1 8861 4 stim_on 7.3842 1.0242 01 middle
2 10090 3 resp 8.4083 0.2925 01 middle
3 10441 4 stim_on 8.7008 0.915 01 middle
4 11539 3 resp 9.6158 0.63417 01 middle
5 12300 4 stim_on 10.25 0.90167 01 middle
6 13382 3 resp 11.152 0.64833 01 middle