Preprocessing Pipeline#

This page describes a practical preprocessing order in EEGPrep. It is not a mandatory recipe for every experiment. Adapt thresholds and review steps to your data, task, and lab standards.

Start From Sample Data#

from pathlib import Path
from eegprep import pop_loadset

EEG = pop_loadset(Path("sample_data") / "eeglab_data.set")
print(EEG["data"].shape, EEG["srate"], len(EEG["event"]))

Select Data and Channels#

Use pop_select for user-facing channel, point, time, trial, and event selection:

from eegprep import pop_select

EEG, com = pop_select(EEG, channel=[1, 2, 3, 4, 5], return_com=True)

GUI path: Edit > Select data. Channel/component values in GUI dialogs are EEGLAB-facing one-based values. pop_select then checks event consistency: a leading boundary whose duration is less than one sample is removed, a leading boundary latency in (0, 1) is set to 0.5, events with an epoch number outside the dataset are removed, and the remaining events are sorted by epoch, then latency. Once any event has a duration, an empty or missing duration on another event is 0. A NaN duration is kept. Consecutive boundary-list entries that share a latency, taken in event order before that sort, chain their durations into the last entry of the run. A later boundary at the same latency stays separate when a different latency stands between the two entries. Boundary latencies 20, 20, 30, and 20 with durations 4, 1, 8, and 2 sort to latency 20 duration 5, latency 20 duration 2, and latency 30 duration 8.

Filter#

The usual FIR filtering entry point is pop_eegfiltnew:

from eegprep import pop_eegfiltnew

EEG, com = pop_eegfiltnew(
    EEG,
    locutoff=1.0,
    hicutoff=40.0,
    plotfreqz=False,
    return_com=True,
)

GUI path: Tools > Filter the data. The wrapper uses bundled FIRFilt-style Hamming-window defaults, respects continuous-data boundary events, clears stale ICA activations, and returns a replayable history command. minphase=True converts the FIR to minimum phase with the real cepstrum before causal filtering. usefftfilt=True filters each segment by overlap-add, so a missing sample stays inside the FFT block that contains it. Each applied design is appended to EEG["etc"]["eegfiltnew"]: sampling rate, 1-based channels, phase, passband edges, -6 dB cutoffs, order, transition width, revfilt, and the coefficients. A missing passband edge is NaN. A highpass is stored as the reversed lowpass that was designed. Design the same FIR without filtering by calling design_eegfiltnew. The history command still records the options you requested.

Use FIRFilt helpers for custom filter design:

from eegprep import pop_firws, pop_firwsord, pop_kaiserbeta

beta = pop_kaiserbeta(0.001)
order = pop_firwsord("kaiser", EEG["srate"], 2, 0.001)
EEG, com = pop_firws(
    EEG,
    fcutoff=[1, 40],
    ftype="bandpass",
    wtype="kaiser",
    warg=beta,
    forder=order,
    return_com=True,
)

Clean Continuous Data#

Use pop_clean_rawdata when you want the interactive wrapper and history string:

from eegprep import pop_clean_rawdata

EEG, com = pop_clean_rawdata(
    EEG,
    FlatlineCriterion=5,
    ChannelCriterion=0.8,
    LineNoiseCriterion=4,
    Highpass=(0.25, 0.75),
    BurstCriterion=20,
    WindowCriterion=0.25,
    return_com=True,
)

Use clean_artifacts when you need lower-level state outputs:

from eegprep import clean_artifacts

clean_eeg, highpass_state, burst_state, removed_channels = clean_artifacts(
    EEG,
    FlatlineCriterion=5,
    ChannelCriterion=0.8,
    LineNoiseCriterion=4,
    Highpass=(0.25, 0.75),
    BurstCriterion=20,
    WindowCriterion=0.25,
)

clean_artifacts returns four values. Scripts that only need the cleaned EEG should use the first value or call pop_clean_rawdata.

Sampling Rates#

ASR calibration designs its spectral-shaping filter for the dataset’s sampling rate, so ordinary clinical rates, including non-integer rates such as 258 Hz, need no resampling first. A rate whose default frequency breakpoints are not monotonic, such as 32 Hz, cannot be calibrated. clean_asr raises an error that names the rate. clean_artifacts and pop_clean_rawdata warn and leave that recording unchanged.

A calibration shorter than one 0.5 s RMS window, or one that yields fewer than two such windows, cannot set thresholds either. The calibration is the reference section, so a longer recording still raises when that section is shorter than one window. clean_asr raises ASRCalibrationError and names the sample or window counts. clean_artifacts and pop_clean_rawdata warn, leave the burst stage unchanged, and still run window removal when it was requested. On a 44-sample recording at 128 Hz the burst stage needs 64 samples, so with the other stages off the samples stay identical, including NaNs. EEGLAB prints the calibration error and returns before window removal.

Two or three 0.5 s RMS windows still calibrate. EEGLAB’s fit indexes a rounded width of 0 and clean_artifacts returns the recording unchanged. EEGPrep skips those non-positive widths and still removes bursts. A 21-channel, 89-sample recording at 128 Hz has two such windows.

Riemannian ASR Notes#

clean_asr(EEG, useriemannian=True) and clean_artifacts(EEG, Distance="Riemannian") run Riemannian ASR. Calibration takes the geometric median of block covariances and sets component thresholds from the nonlinear eigenspace of the mixing matrix. Processing uses one sample covariance per chunk. A later chunk averages that covariance with the previous chunk along the affine-invariant geodesic.

clean_asr(EEG, useriemannian="calib") keeps the smaller variant: a Riemannian covariance during calibration, then the Euclidean ASR processor. Passing an unknown useriemannian value raises ValueError.

Calibration does not change non-finite samples in the recording it receives. clean_asr reflects the end of that original recording to build lookahead. Processing prepends its carry, then replaces non-finite samples, including those reflected samples, with 0.

A Riemannian calibration shorter than one 0.1 s RMS window, or one with fewer than two such windows, raises ASRCalibrationError. The message names asr_calibrate_r, the sample counts, and the choice to record more data or set BurstCriterion off. pop_clean_rawdata warns and leaves the burst stage unchanged. EEGLAB does not catch that call.

Save and Reapply the ASR State#

clean_asr can hand back the calibration it used, so the same cleaning can be repeated later or recorded beside a result:

import json

from eegprep import clean_asr
from eegprep.plugins.clean_rawdata.asr_state import state_from_json, state_to_json

cleaned, state = clean_asr(EEG, return_state=True)
with open("asr_state.json", "w") as handle:
    json.dump(state_to_json(state), handle)

with open("asr_state.json") as handle:
    state = state_from_json(json.load(handle))
again = clean_asr(EEG, state=state)  # again["data"] equals cleaned["data"]

The state is the calibration as asr_calibrate (or asr_calibrate_r for full Riemannian ASR) returned it, plus the sampling rate, the channel labels in data order, and the processing arguments (cutoff, window_len, step_size, max_dims, useriemannian, maxmem). The JSON text holds every array as float64 and every number in its shortest exact form, so reading it back loses nothing. A 32-channel state is about 48 kB.

Reapplying skips reference selection and calibration and processes with the recorded arguments. A cutoff, window_len, step_size, max_dims, ref_*, useriemannian, or maxmem passed beside state must be that argument’s default or the value the state records (useriemannian=False and "off" both mean Euclidean); any other value raises ValueError. A state is checked as a stored document is, whether it comes from return_state or from a dictionary you built, and a malformed one raises ValueError naming the key. A whole-number step_size such as 32.0 is recorded as an integer, and a fractional one raises when a state is requested.

A state whose sampling rate or channel labels differ from the EEG’s raises ValueError, because a state from another montage would mix the wrong channels. A state recorded from an EEG without chanlocs has no labels. It fits only an EEG without labels, and a labeled state is refused for an EEG without them.

maxmem fixes how many blocks processing uses, in every variant, and the block count changes the cleaned data. Processing reserves channels * channels * lookahead_samples * 24 bytes before it sizes its blocks. When maxmem is None or smaller than that, asr_process replaces it with half of the free memory, and the output then differs between runs and machines. The default of 64 MB is too small for 128 channels at 1 kHz and for 256 channels at 512 Hz. clean_asr emits a UserWarning that names the overhead and the smallest sufficient maxmem when a state is requested or supplied with such a budget; it never raises.

Identical output after reapplying holds on one platform and numerical library stack. Another BLAS or LAPACK build can differ in the last bits of the float arithmetic.

Review Rejected Segments#

vis_artifacts(clean_eeg, original_eeg) opens an EEG browser with rejected sample intervals highlighted from clean_sample_mask. Use show=False when scripting diagnostics:

from eegprep import vis_artifacts

diagnostics = vis_artifacts(clean_eeg, EEG, show=False)
print(diagnostics["rejected_fraction"])

Resample#

Resample after early cleaning/filtering when a lower sampling rate is appropriate:

from eegprep import pop_resample

EEG, com = pop_resample(EEG, 64, return_com=True)

GUI path: Tools > Change sampling rate. Continuous data with boundary events is resampled by segment. Event latencies and the time vector are updated. A missing sample stays missing where the anti-aliasing filter reaches it, including the exact-zero delay pads in MATLAB’s resampler. Outputs no tap reaches stay finite.

Re-Reference and Interpolate#

Average reference:

from eegprep import pop_reref

EEG, com = pop_reref(EEG, [], return_com=True)

Interpolate known bad channels after channel removal or rejection:

from eegprep import pop_interp

EEG, com = pop_interp(EEG, [1, 5, 10], return_com=True)

Use Tools > Re-reference the data and Tools > Interpolate electrodes from the GUI.

bad_elec integers are 1-based: channel 1 is the first channel. A t_range of [start, end] seconds is inclusive on EEG.times. An empty dialog time field interpolates the whole recording. A scripted range is honoured. Pinned EEGLAB pop_interp.m line 61 discards that fourth argument.

Run ICA and ICLabel#

Run ICA:

from eegprep import pop_runica

EEG, com = pop_runica(EEG, icatype="picard", gui=False, return_com=True)

Label and review components:

from eegprep import eeg_icalabelstat, pop_iclabel, pop_viewprops

EEG, com = pop_iclabel(EEG, "default", return_com=True)
stats = eeg_icalabelstat(EEG, threshold=0.9, verbose=False)
figures = pop_viewprops(EEG, typecomp=0, chanorcomp=[1, 2, 3])

See ICA, ICLabel, Rejection, and Visual Diagnostics before removing components.

Time-Frequency and ERP Images#

After epoching or component review, use EEGPrep’s standalone time-frequency wrappers for the common EEGLAB plotting workflows:

from eegprep import pop_newcrossf, pop_newtimef, pop_spectopo

ersp, com_timef = pop_newtimef(EEG, typeproc=1, num=1, return_com=True)
cross, com_crossf = pop_newcrossf(EEG, typeproc=1, num1=1, num2=2, return_com=True)
spectra, com_spectopo = pop_spectopo(EEG, 1, timerange=[], return_com=True)

pop_newtimef plots one channel or component at a time. Set typeproc=1 for a channel and typeproc=0 for an ICA component; num is the 1-based channel or component index. tlimits is the epoch window in milliseconds. The sample window uses EEGLAB rounding and the decomposition uses those requested limits. pop_newtimef includes the end sample. pop_newcrossf includes it too; pinned pop_newcrossf.m line 189 drops that sample. pop_newcrossf without a frequency range stops at 50 Hz. cycles selects the decomposition: [0] uses a short-time FFT, while [n] (optionally [n factor]) uses an n-cycle Morlet wavelet that narrows toward higher frequencies. Called with no index, pop_newtimef opens a dialog to collect these values; the Plot menu exposes the same dialog under Channel time-frequency and Component time-frequency.

Reading the figure#

The figure stacks two images that share a time axis:

  • ERSP (top) – event-related spectral perturbation, the trial-averaged power change from the baseline window, in dB.

  • ITC (bottom) – inter-trial coherence, from 0 (phase varies across trials) to 1 (perfect phase locking).

Each image carries marginal panels: a rotated plot to its left (the baseline power spectrum beside ERSP; the frequency-averaged coherence beside ITC) and a horizontal plot below it (the ERSP minimum/maximum envelope; the ERP trace). A dashed line marks time zero, colorbars sit on the right, and – when channel locations are available – a scalp-map inset in the upper left shows the channel’s head position or the component’s interpolated map, captioned with the channel label or IC n.

Baseline and significance#

Pass a baseline window in milliseconds to normalize power, and an alpha to test each time-frequency point against a bootstrap null drawn from the baseline:

result = pop_newtimef(EEG, 1, 1, [-1000, 1000], [3, 0.5],
                      baseline=[-1000, 0], alpha=0.05)

Non-significant points are drawn at the colormap’s green midpoint so the surviving effects stand out; add mcorrect="fdr" to control the false discovery rate across the image. Because this is a permutation test, roughly alpha of the effect-free baseline points survive by chance – expected scatter, not a real effect.

Display options#

  • pcontour="on" keeps every point at its measured value and outlines the significant regions with a contour instead of masking them.

  • The ITC image is colored by the sign of the coherence phase by default; pass plotphase="off" for magnitude only, or plotphaseonly="on" to show the phase angle in degrees.

  • erspmax sets a symmetric ERSP color scale ([-erspmax, erspmax]) and itcmax the ITC image maximum; leave them unset to auto-scale.

Two-condition comparisons (contrasting two datasets in a single figure) are not yet part of the standalone wrapper.

Legacy pop_timef and pop_crossf calls route through the standalone pop_newtimef and pop_newcrossf implementations. ERP-image workflows use pop_erpimage for channel or component images of epoched data (see Epoch and Baseline below), with EEGLAB’s arguments:

from eegprep import pop_erpimage

# Channel Cz of epoched data, sorted by reaction-time latency, smoothed over 10 trials.
result, com = pop_erpimage(EEG, 1, "Cz", None, "", 10, 1, ["rt"], None, "latency", return_com=True)

pop_erpimage draws the ERP trace, a colorbar and a scalp map by default, and accepts every erpimage() option, such as phase sorting or inter-trial coherence. See GUI Tutorials for the dialog.

GUI paths live under the Plot menu when a dataset is loaded. EEGPrep returns replayable Python history commands for these wrappers.

Epoch and Baseline#

Use pop_epoch and pop_rmbase for ERP-style workflows:

from eegprep import pop_epoch, pop_rmbase

EEG, com_epoch = pop_epoch(EEG, ["square"], [-1, 2], return_com=True)
EEG, com_base = pop_rmbase(EEG, [-200, 0], return_com=True)

GUI paths: Tools > Extract epochs and Tools > Remove epoch baseline.

Save#

Save reviewed datasets with pop_saveset:

from pathlib import Path
from eegprep import pop_saveset

pop_saveset(EEG, Path("sample_data") / "eeglab_data_preprocessed.set")

For large datasets, review Large-Dataset Storage before deciding whether to keep data in memory, memory-map sidecar files, or retrieve offloaded datasets.

Quality Control Checklist#

  • Confirm EEG["data"].shape after each major transform.

  • Inspect events after filtering/resampling and before epoching.

  • Review rejected channels/windows visually before final removal.

  • Inspect ICLabel probabilities, scalp maps, spectra, and activity before removing components.

  • Keep LASTCOM and ALLCOM from GUI/console runs so the workflow can be replayed in scripts.