Hello,
I’m not following your code completely, but if you want to create epochs from a continuous data (raw) recording and automatically drop epochs where the peak-to-peak amplitude exceeds a threshold, you have 2 options:
- Provide the argument
reject
when creating the epochs
- Annotate the continuous recording to mark segments with large amplitude as
bads
You can find below an example with both:
import numpy as np
from mne import Epochs, create_info, find_events
from mne.io import RawArray
from mne.preprocessing import annotate_amplitude
info = create_info(["EEG 01", "EEG 02", "EEG 03"], sfreq=512, ch_types="eeg")
data = np.random.randn(3, 30720) # 60 seconds of continuous data
data *= 1e-5 # scaling to get at least the correct order of magnitude
raw = RawArray(data, info)
# add an event channel with fake events every 1 seconds
info = create_info(["STI"], sfreq=raw.info["sfreq"], ch_types="stim")
data_stim = np.zeros(shape=(1, len(raw.times)))
data_stim[0, 0::512] = 1
stim = RawArray(data_stim, info)
raw.add_channels([stim], force_update_info=True)
#%% Option 1: PTP rejection threshold
# create epochs with a peak-to-peak amplitude rejection criterium
threshold = 60 * 1e-6 # 60 uV peak-to-peak amplitude within an epoch
events = find_events(raw, stim_channel="STI")
epochs = Epochs(
raw,
events,
event_id=dict(test=1),
tmin=0,
tmax=0.5,
picks="eeg",
reject=dict(eeg=threshold),
reject_by_annotation=False, # there are no annotations anyway
baseline=None,
preload=True,
)
#%% Option 2: Annotation of bad segments based on PTP amplitude
# create annotations that will mark the segments of continuous data exceeding
# a threshold
threshold = 30 * 1e-6 # 30 uV maximum amplitude
annotations, bads = annotate_amplitude(
raw,
peak=dict(eeg=threshold),
picks="eeg",
)
raw.set_annotations(annotations)
# N.B: If you already had annotations in your recording and want to keep them
# alongside the added BAD_peak annotations, you need to pass both:
# raw.set_annotations(raw.annotations + annotations)
# then create epochs and reject by annotations
epochs = Epochs(
raw,
events,
event_id=dict(test=1),
tmin=0,
tmax=0.5,
picks="eeg",
reject=None,
reject_by_annotation=True,
baseline=None,
preload=True,
)
Note that they do not do exactly the same thing. After adding the annotations, you can use raw.plot()
to display the data and the annotations, it should make it clear which part of the signal has been rejected.
Mathieu