Skip to content

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

Closed

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?

Activity

  1. larsoner commented on Feb 11, 2026

    @larsoner
    Member

    I could see use cases where users might want what we currently do, and others might want them renumbered ascending.

    Instead of a new constructor option (which already has too many options!) how about a epochs.reset_indices() or similar? Maybe this will need to reset stuff like drop_log etc., not sure...

  2. cbrnr commented on Feb 11, 2026

    @cbrnr
    ContributorAuthor

    Instead of a new constructor option (which already has too many options!) how about a epochs.reset_indices() or similar? Maybe this will need to reset stuff like drop_log etc., not sure...

    Yes, this would work of course! I can take a stab and see what else needs to be changed.

  3. cbrnr commented on Feb 18, 2026

    @cbrnr
    ContributorAuthor

    Could it be that epochs.reset_drop_log_selection() already does what I want? When calling it at the end of the example, epochs.selection changes from array([1, 4, 7]) to array([0, 1, 2]).

  4. larsoner commented on Feb 18, 2026

    @larsoner
    Member

    Could be. If so maybe we "just" need improved docs and discoverability

  5. cbrnr commented on Feb 18, 2026

    @cbrnr
    ContributorAuthor

    The main problem I see is the name. I would have found what I needed if the method was called reset_index or similar, but the current name is a misnomer, at least in the context I was looking for (creating epochs has nothing to do, or should have nothing to do with dropping epochs). Therefore, I suggest we rename it, even if this means going through a deprecation cycle. "Just" changing the docs wouldn't help, because I would have never looked in this method.

  6. larsoner commented on Feb 18, 2026

    @larsoner
    Member

    No need for deprecation cycle, keeping the old name around is close to zero maintenance cost so we should just @legacy decorate it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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