epoching on epoched data


Is it possible to make fixed length epochs on existing epoched file ? I currently have an EEG file that has epochs of length 30s each. I need to preprocess this file and run autoreject. To do so, I need to make 1s length epochs and then run autoreject. Is there a way I can create more epochs on the existing file ?


1 Like

do you want to use autoreject local or global?


global! I usually identify the global rejection threshold on the epoched EEG data and then run ICA and then compute channel level autorejections.

Hi Apoorva,

You can do something like this:

for epochs_data in epochs:
    raw_epoch = mne.io.RawArray(epochs_data, epochs.info)
    epochs_autoreject = mne.make_fixed_length_epochs(raw_epoch, duration=1, preload=True)

where epochs is your long 30 s epochs and epochs_autoreject are the shorter epochs. Does this work for you?


1 Like

ooh, that’s even better than what I came up with. @apoorva6262 and I discussed this during office hours today and I suggested

data = epochs.get_data()
pseudo_raw_data = np.hstack(data)
pseudo_raw = mne.io.RawArray(pseudo_raw_data, epochs.info)
new_epochs = mne.make_fixed_length_epochs(pseudo_raw, duration=1.)
1 Like

Thanks @drammock and @mainakjas .

For both suggestions, I get this error : numpy.ndarray’ object has no attribute ‘info’

Are you sure you are working with epochs and not epochs.get_data()? The second is a numpy array and no attribute info.


Sorry, yes I did use epochs. Then it gave me an error that it is 3d and data must be 2d. So I tried using epochs.transpose.reshape and then it gave me an error that ‘EpochsFIF’ object has no attribute ‘transpose’

please give us the exact lines of code that you ran as well as the error traceback. Also, some general advice:

  1. different kinds of data objects have different methods. NumPy arrays allow things like my_array.transpose() but MNE-Python Epochs objects do not have a .transpose() method. If you type the name of your variable in an iPython console and push . and then the tab key, you can get a pop-up of all its available methods and attributes. That can help you avoid trying things that won’t work (like my_epochs.transpose()… because “transpose” won’t be one of the available options that pops up). Here’s an example:

  1. in MNE-Python, the actual data (the numbers representing voltage X in channel Y at time Z) are stored in a NumPy array that is part of a larger object that also contains other data (like channel positions, etc). From any MNE-Python object you can always do my_object.get_data() or my_object.data if you need direct access to the NumPy array, but do not confuse the (Raw, Epochs, Evoked) object for the data array that it contains.

Thank you . That was helpful.
This worked for me :

ch_types = ['eeg'] * 22 
info = mne.create_info(ch_names=ch_names, ch_types=ch_types, sfreq=sfreq)
n_epochs, n_channels, n_times = epoch.shape
data=epoch.transpose([1, 0, 2]).reshape((n_channels, n_epochs * n_times))
raw_epoch = mne.io.RawArray(data, info)
new_epochs = mne.make_fixed_length_epochs(raw_epoch, duration=1, preload=True)

glad you got it working! One more tiny suggestion: instead of doing epoch=epochs.get_data(), use a more descriptive variable name like epoch_data or data_array or similar. Having 2 variables called “epoch” and “epochs” implies that they are both the same type of object, differing in how many trials they include. In your code, however, one is a mne.Epochs object and the other is a numpy.ndarray object, and both of them contain all trials… so the variable names are misleading and make it easier to get confused.