Creating an epoch from a .mat data file

  • MNE-Python version: 3.8.8
  • operating system: Windows

Hello,

I am trying to convert a .mat data file into an mne epoch object.

I have read the .mat file into mne as follows:

s_m12_AB_mat_outdata = read_mat('s005_m12_all-epochs_AB_OutData.mat')

I understand that to convert to an epoch object, I must use mne.EpochsArray, and the first parameter is a three-dimensional data array in the shape of (n_epochs, n_channels, times). The data has 111 epochs, 129 channels, each epoch 0-0.998 seconds long, sampling frequency 500 Hz. So the shape must be 111 x 129 x 500. But the .mat file I read in currently has a shape of 129 x 55500. I am trying to convert it to 3 dimensional, so that it can be converted to an epochs object using mne.EpochsArray.

How may I achieve this?

Thank you for your time!

Best,
Diksha

Hi Diksha,

I’m not very familiar with pymatreader.read_mat, but if the data in the .mat files were epochs, I’m not exactly sure why your data are shape 129 (channels) x 55500 (samples), although 55500 / 500(Hz) = 111, which is what you suggested the first dimension (n_epochs) of your data should be.

Since read_mat should return a dictionary, what are the dictionary keys you are accessing to get this 129x55500 matrix?

If the data in the .mat files are not epochs but are continuous data, you can follow this tutorial to first create a Raw object, after which epoching the data in mne should be easier.

3 Likes

It’s a bit hacky, but if you’re sure the data in the .mat files are epoched, and you want to reshape the 2d numpy array to a 3d array of n_epochs, n_channels, n_times, you could do:

my_3d_arr = np.reshape(my_2d_arr, (129, 111, 500)).swapaxes(0,1)

You could run a quick test to make sure the reshaped data are what you expect, for example:

assert my_2d_arr[0,500] == my_3d_arr[1,0,0] # the 501st sample in the 2d_arr should now be the 1st sample of the second epoch in the 3d_arr

Although I think the better approach is to figure out why the .mat files are being loaded in as 2d arrays :slightly_smiling_face: If the data in the .mat files were epochs, I’d expect them to be a 3d matrix, and I’d expect read_mat to convert them to 3d numpy arrays. Although maybe I am working off faulty assumptions.

3 Likes

Hi Scott - the approach you suggested worked! I tested it using epoch.plot(); I had the original epoched data plotted in MNE. Then I plotted the version from the .mat file converted to epochs using the 2D-3D conversion you suggested, and they both are identical.
As for why the .mat files are being loaded in as 2d arrays; I am working with an artifact correction algorithm in MATLAB. One of the steps involves converting the format of the input epoched data using the ‘cell2mat’ function. I suspect that might be why.
Thank you for helping me figure this out :smiley: