# Concatenate epochs based on epoch number

**URL:** <https://mne.discourse.group/t/concatenate-epochs-based-on-epoch-number/3863>\
**Category:** Support & Discussions\
**Tags:** epochs, autoreject\
**Created:** [October 26, 2021, 10:54pm UTC](https://mne.discourse.group/t/concatenate-epochs-based-on-epoch-number/3863 "2021-10-26T22:54:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibra332](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@ibra332](https://mne.discourse.group/u/ibra332)\
**Post date:** [October 26, 2021, 10:54pm UTC](https://mne.discourse.group/t/concatenate-epochs-based-on-epoch-number/3863/1 "2021-10-26T22:54:22Z")

</div>

**If you have a question or issue with MNE-Python, please include the following info:**

- MNE-Python version: 0.23
- operating system: Win 10

Greetings,

Is it possible to concatenate epochs based on the epoch number?

I have extracted a subset of epochs based on event id to perform some preprocessing tasks and now I want to put them back in the same order. When I used `mne.concatenate_epochs()`, I couldn’t sort them based on the epoch number.

Regards,

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [October 27, 2021, 9:40am UTC](https://mne.discourse.group/t/concatenate-epochs-based-on-epoch-number/3863/2 "2021-10-27T09:40:37Z")

</div>

Hello,

> [@ibra332](#):
>
> I have extracted a subset of epochs based on event id to perform some preprocessing tasks and now I want to put them back in the same order.

this is not trivial. When you subset epochs e.g. via `epochs_subset = epochs['foo']`, all epochs in `epochs_subset` that don’t match the specified criterion will be marked as `IGNORED` in `epochs_subset.drop_log`. You can use this information to find out the original indices:

```python
# %%
from pathlib import Path
import mne

sample_dir = Path(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}

epochs = mne.Epochs(raw, events=events, event_id=event_id,
                    tmin=-0.2, tmax=0.5, baseline=(None, 0),
                    preload=True)

# %%
e1 = epochs['auditory']
e2 = epochs['visual']

# %%
orig_idx_e1 = []
for idx, drop_reasons in enumerate(e1.drop_log):
    if 'IGNORED' not in drop_reasons:
        orig_idx_e1.append(idx)

orig_idx_e2 = []
for idx, drop_reasons in enumerate(e2.drop_log):
    if 'IGNORED' not in drop_reasons:
        orig_idx_e2.append(idx)

print(orig_idx_e1)
print(orig_idx_e2)

```

You can then probably use this info to fiddle the epochs back together in the desired order somehow. But this entire procedure is no fun…

---

<div class="post-metadata">

**Author:** ![ibra332](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@ibra332](https://mne.discourse.group/u/ibra332)\
**Post date:** [October 28, 2021, 3:44am UTC](https://mne.discourse.group/t/concatenate-epochs-based-on-epoch-number/3863/3 "2021-10-28T03:44:22Z")

</div>

Hello @richard,

Thank you for the help.

The goal of this step is to implement `Autoreject()` on multiple subset epochs based on events. I’m doing this to see if it would improve the results of a multi-class classification task.

I truly appreciate the time and effort you and the other developers put into helping all the users.

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [October 29, 2021, 7:45am UTC](https://mne.discourse.group/t/concatenate-epochs-based-on-epoch-number/3863/4 "2021-10-29T07:45:37Z")

</div>

Tagging @mainakjas, creator of autoreject

---

<div class="post-metadata">

**Author:** ![mainakjas](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mainakjas/32/34_2.png) [@mainakjas](https://mne.discourse.group/u/mainakjas)\
**Post date:** [October 29, 2021, 10:59am UTC](https://mne.discourse.group/t/concatenate-epochs-based-on-epoch-number/3863/5 "2021-10-29T10:59:14Z")

</div>

Conversation happened on autoreject github 🙂 Thanks for tagging!

Mainak
