Subsetting Epochs by selecting events that don't satisfy a condition

Hi all,

I would like to subset my Epochs variable by selecting epochs which are present in one condition but not present in another. For example, to use the sample dataset ‘sample_audvis_filt-0-40_raw.fif’, I would like to know how to select the epochs around ‘auditory’ events but not ones which are also ‘left’. Is this a way that I can do this that is similar to new_epochs = epochs[‘auditory/left’]?
I know in this example that I could just select the ‘right’ events instead but in my own data I have several levels to one variable and would like to select all but one of these levels.

Thanks!

Hello @johnnytwohats and welcome to the forum!

I would use epochs metadata for that:

# %%
import mne

sample_dir = mne.datasets.sample.data_path()
sample_fname = sample_dir / 'MEG' / 'sample' / 'sample_audvis_raw.fif'

raw = mne.io.read_raw_fif(sample_fname)
raw.crop(tmax=60)

events = mne.find_events(raw, stim_channel='STI 014')
event_id = {'auditory/left': 1, 'auditory/right': 2, 'visual/left': 3,
            'visual/right': 4, 'face': 5, 'buttonpress': 32}

metadata, _, _ = mne.epochs.make_metadata(
    events=events,
    event_id=event_id,
    tmin=0, tmax=0,  # we really only want one event per row
    sfreq=raw.info['sfreq'],
)
# Add a column for stimulus modality
for modality_name in ['auditory', 'visual']:
    metadata.loc[
        metadata['event_name'].str.startswith(modality_name), 'modality'
    ] = modality_name

# Add a column for stimulation side
for side_name in ['left', 'right']:
    metadata.loc[
        metadata['event_name'].str.endswith(side_name), 'side'
    ] = side_name


epochs = mne.Epochs(
    raw, events=events, event_id=event_id, metadata=metadata,
    preload=True
)

# %%
epochs['modality == "auditory" and side != "left"']

Screenshot 2022-11-02 at 20.37.52

See our tutorial on how to work with metadata:
https://mne.tools/stable/auto_tutorials/epochs/30_epochs_metadata.html

Best wishes,
Richard