Skip to content

Add option to flatten indexes when creating epochs with event_id #13650

@cbrnr

Description

@cbrnr

When creating epochs from a subset of the given events (using the event_id argument), the epoch indexes correspond to the indexes from the original events array. Here's an example:

import numpy as np
from mne import Epochs, create_info
from mne.io import RawArray
from numpy.random import default_rng

n_channels = 3
duration = 25
sfreq = 250
data = default_rng(42).standard_normal(size=(n_channels, duration * sfreq)) * 5e-6
info = create_info(n_channels, sfreq, "eeg")
raw = RawArray(data, info)

n_events = 9
events = np.column_stack(
    (
        np.arange(1, n_events + 1) * sfreq,
        np.zeros(n_events, dtype=int),
        np.tile([1, 2, 3], n_events // 3),
    )
)

epochs = Epochs(raw, events, event_id=2, tmin=-0.2, tmax=0.5)
epochs.plot()

This results in three epochs with indexes [1, 4, 7], for example as shown in the x-axis labels of the epochs plot:

Image

I think this behavior is not very intuitive, as users will probably expect epoch indexes to range from 0 to N - 1, i.e., [0, 1, 2] in this example.

My current workaround is to not use event_id at all, but pass a subset of the original events array. Although this works, it feels like there should be an easier option to achieve this behavior with event_id. For example, we could add a new parameter flatten_indexes=False (feel free to suggest a better name). WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions