How to concatenate epochs with different lengths and/or time

  • MNE version: 0.24.1
  • operating system: macOS 12.1

Hello,

I wanted to concatenate two epochs with different lengths using the function mne.concatenate_epochs. Unfortunately, that didn’t work and I obtained that error: ValueError: operands could not be broadcast together with shapes (601,) (1751,)

Then, I thought I could get around this limitation by giving the epochs the same lengths. That didn’t work either; I received this error: ValueError: Epochs must have same times

To conclude, maybe I could give the big picture in case another option could give me what I want in the end. I try to exclude a time period inside an epoch. My strategy was to create two epochs using the same time event but with different tmin and tmax. Then, I expected to just concatenate the two epochs.

Thanks for your help !

François

hi François,

MNE enforces epochs to have the same length. If you enforce each epochs to
have the same tmin, tmax and sfreq you should not have a any problem
with concatenate_epochs function.

Alex

Thanks for your answer Alex.

Here’s what I did to solve my problem. Maybe the solution could help other people.

  • Export MNE epochs as numpy array using epochs_trial.get_data
  • Use numpy.concatenate to concatenate them. That function enables concatenation with arrays of different lengths.
  • Create a new MNE epoch with the numpy array using mne.EpochsArray.

François

MNE now has epoch annotations, so you could first epoch and then annotate the segment you would like to be omitted with an annotation with description starting with ‘bad’. The approach with concatenating that you use introduces discontinuities in the data and is not optimal.