epoching mismatch between python and matlab

External Email - Use Caution

Dear MNE people,

I got a problem when epoching data in MNE Python using mne.Epochs.

I am running some analysis with a Matlab code and I would like to transfer
the same analysis to MNE Python.

My problem is that if I compare one epoch from Matlab with the output of
mne.Epochs in Python they do not correspond.

Here below the code and a figure explaining the problem

My question is: why

  spike1[295, :] # epoch from matlab data
  data[295, (np.arange(index-100,index+100))] # epoch from data imported
in python and manually extracted with a sample reference 'index'

do not correspond with

  epochs9[0,295,:] # which is the output of mne.Epochs for the very same
epoch?

They both should refer to the sample 75650, with a pre- and post-trigger of
100 samples.

I am not applying any SSP projection or baseline. What am I missing?

Thanks in advance for your help,

Best,
Tommaso

%%%%%%%%%%%%%%%%% code

import numpy as np
import mne
import xlrd
import scipy.io as sio
import pandas as pd
from mne.viz import plot_evoked_topo
import matplotlib.pyplot as plt

spike = sio.loadmat(
        'E:/../spike75650.mat'
        )# data imported from Matlab as dict spike.
         #The field 'spike is an array float64 (404, 201) channels, samples

raw = mne.io.read_raw_fif('E:/.../B1C2_ii_run1_raw_tsss_mc_art_corr.fif')
#data after tsss
excel = xlrd.open_workbook('E:/.../list raw detections B1C2.xlsx') #trigger
info from excel file

# create an array for mne.Epochs
sheet = excel.sheet_by_index(2)
sp=list()
for i in range(1,61):
    sp.append(sheet.cell_value(i,2))
spevents = np.array(sp, ndmin=2, dtype=int)
zeros = np.zeros((1,60), dtype=int)
event_id_int = np.ones((1,60), dtype=int)
event_id_int[:,:] = np.arange(0,60)
event_id_str = list(map(str,np.arange(0,60)))
events = np.hstack((spevents.T, zeros.T, event_id_int.T)) # array int32
(60,3) Nevents,3

               # one row is 75650 0 9

# Create epochs including different events
event_id = dict(zip(event_id_str, np.arange(0,60)))
epochs = mne.Epochs(raw, events, event_id, tmin = -0.1, tmax = 0.1, proj
= False)

# Generate list of evoked objects from conditions names
evokeds = [epochs[name].average() for name in np.arange(0,60)]

##################### Figure
plt.close('all')
plt.figure()

ep_id = 9
index = spevents[0,ep_id] # trigger sample for continuous data, value 75650

# spike from matlab
spike1 = spike['spike']
plt.plot(spike1[295, :],
                       label = ['Matlab: spike from raw data - MEG2613'])

# spike from continuous data
plt.plot(data[295, (np.arange(index-100,index+100))]-5*10**-11,
                            label = ['Python: spike from raw data -
MEG2613'])

# spike from MNE epochs
epochs9 = epochs[ep_id].get_data()
plt.plot(epochs9[0,295,:]-5*2*10**-11,
                        label = ['MNE Python: spike from EPOCHED data -
MEG2613'])

# spike from MNE average
evoked9 = epochs[ep_id].average()
plt.plot(evoked9.data[295]-5*3*10**-11,
                        label = ['MNE Python: spike from averaged data -
MEG2613'])

plt.legend()

# check that the channel labels is the same
print(epochs.ch_names[295]), print(raw.ch_names[295])

%%%%%%%%%%%%%%%%%%%%%% FIGURE

[image: image.png]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20191124/144beac2/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 202423 bytes
Desc: not available
Url : http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20191124/144beac2/attachment-0001.png

External Email - Use Caution

hi Tommaso,

unfortunately it's really hard to help without more context and knowledge
about the data.

Best,
Alex

        External Email - Use Caution

Dear MNE people,

I got a problem when epoching data in MNE Python using mne.Epochs.

I am running some analysis with a Matlab code and I would like to transfer
the same analysis to MNE Python.

My problem is that if I compare one epoch from Matlab with the output of
mne.Epochs in Python they do not correspond.

Here below the code and a figure explaining the problem

My question is: why

  spike1[295, :] # epoch from matlab data
  data[295, (np.arange(index-100,index+100))] # epoch from data imported
in python and manually extracted with a sample reference 'index'

do not correspond with

  epochs9[0,295,:] # which is the output of mne.Epochs for the very same
epoch?

They both should refer to the sample 75650, with a pre- and post-trigger
of 100 samples.

I am not applying any SSP projection or baseline. What am I missing?

Thanks in advance for your help,

Best,
Tommaso

%%%%%%%%%%%%%%%%% code

import numpy as np
import mne
import xlrd
import scipy.io as sio
import pandas as pd
from mne.viz import plot_evoked_topo
import matplotlib.pyplot as plt

spike = sio.loadmat(
        'E:/../spike75650.mat'
        )# data imported from Matlab as dict spike.
         #The field 'spike is an array float64 (404, 201) channels, samples

raw = mne.io.read_raw_fif('E:/.../B1C2_ii_run1_raw_tsss_mc_art_corr.fif')
#data after tsss
excel = xlrd.open_workbook('E:/.../list raw detections B1C2.xlsx')
#trigger info from excel file

# create an array for mne.Epochs
sheet = excel.sheet_by_index(2)
sp=list()
for i in range(1,61):
    sp.append(sheet.cell_value(i,2))
spevents = np.array(sp, ndmin=2, dtype=int)
zeros = np.zeros((1,60), dtype=int)
event_id_int = np.ones((1,60), dtype=int)
event_id_int[:,:] = np.arange(0,60)
event_id_str = list(map(str,np.arange(0,60)))
events = np.hstack((spevents.T, zeros.T, event_id_int.T)) # array int32
(60,3) Nevents,3

                 # one row is 75650 0 9

# Create epochs including different events
event_id = dict(zip(event_id_str, np.arange(0,60)))
epochs = mne.Epochs(raw, events, event_id, tmin = -0.1, tmax = 0.1, proj
= False)

# Generate list of evoked objects from conditions names
evokeds = [epochs[name].average() for name in np.arange(0,60)]

##################### Figure
plt.close('all')
plt.figure()

ep_id = 9
index = spevents[0,ep_id] # trigger sample for continuous data, value
75650

# spike from matlab
spike1 = spike['spike']
plt.plot(spike1[295, :],
                       label = ['Matlab: spike from raw data - MEG2613'])

# spike from continuous data
plt.plot(data[295, (np.arange(index-100,index+100))]-5*10**-11,
                            label = ['Python: spike from raw data -
MEG2613'])

# spike from MNE epochs
epochs9 = epochs[ep_id].get_data()
plt.plot(epochs9[0,295,:]-5*2*10**-11,
                        label = ['MNE Python: spike from EPOCHED data -
MEG2613'])

# spike from MNE average
evoked9 = epochs[ep_id].average()
plt.plot(evoked9.data[295]-5*3*10**-11,
                        label = ['MNE Python: spike from averaged data -
MEG2613'])

plt.legend()

# check that the channel labels is the same
print(epochs.ch_names[295]), print(raw.ch_names[295])

%%%%%%%%%%%%%%%%%%%%%% FIGURE

[image: image.png]

_______________________________________________
Mne_analysis mailing list
Mne_analysis at nmr.mgh.harvard.edu
Mne_analysis Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20191127/8d63bb8c/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 202423 bytes
Desc: not available
Url : http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20191127/8d63bb8c/attachment-0001.png

External Email - Use Caution

Dear Alex,

this is actually the answer.
I found out that MNE Python translates the sample info to the real time of
the recording,
which in my case is starting at t = 39 seconds.
So my code works if I build the events array as follows

*events = np.vstack((spevents[0] + raw.first_samp, np.zeros(60),
np.arange(60)+1)).T.astype(np.int <http://np.int>) # array int32 (60,3)
Nevents,3epochs = mne.Epochs(raw, events, tmin = -0.1, tmax = 0.1, proj =
False, baseline=None, detrend=0, preload=False) *

where *raw.first_samp* set the correct offset for MNE Python to understand
where I want to point in my continuous data.

Thanks,

Best,
Tommaso