# epoching mismatch between python and matlab

**URL:** <https://mne.discourse.group/t/epoching-mismatch-between-python-and-matlab/2013>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [November 24, 2019, 9:01pm UTC](https://mne.discourse.group/t/epoching-mismatch-between-python-and-matlab/2013 "2019-11-24T21:01:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [November 24, 2019, 9:01pm UTC](https://mne.discourse.group/t/epoching-mismatch-between-python-and-matlab/2013/1 "2019-11-24T21:01:16Z")

</div>

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

&nbsp;&nbsp;spike1[295, :] # epoch from matlab data  
&nbsp;&nbsp;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

&nbsp;&nbsp;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](http://scipy.io) as sio  
import pandas as pd  
from mne.viz import plot\_evoked\_topo  
import matplotlib.pyplot as plt

spike = sio.loadmat(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'E:/../spike75650.mat'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)# data imported from Matlab as dict spike.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#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):  
&nbsp;&nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 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, :],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label = ['Matlab: spike from raw data - MEG2613'])

# spike from continuous data  
plt.plot(data[295, (np.arange(index-100,index+100))]-5\*10\*\*-11,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](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](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20191124/144beac2/attachment-0001.png)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [November 27, 2019, 2:41pm UTC](https://mne.discourse.group/t/epoching-mismatch-between-python-and-matlab/2013/2 "2019-11-27T14:41:10Z")

</div>

External Email - Use Caution

hi Tommaso,

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

Best,  
Alex

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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
> 
> &nbsp;&nbsp;spike1[295, :] # epoch from matlab data  
> &nbsp;&nbsp;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
> 
> &nbsp;&nbsp;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(  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'E:/../spike75650.mat'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)# data imported from Matlab as dict spike.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#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):  
> &nbsp;&nbsp;&nbsp;&nbsp;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
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 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, :],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label = ['Matlab: spike from raw data - MEG2613'])
> 
> # spike from continuous data  
> plt.plot(data[295, (np.arange(index-100,index+100))]-5\*10\*\*-11,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis)

-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20191127/8d63bb8c/attachment-0001.html](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](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20191127/8d63bb8c/attachment-0001.png)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [November 27, 2019, 4:45pm UTC](https://mne.discourse.group/t/epoching-mismatch-between-python-and-matlab/2013/3 "2019-11-27T16:45:23Z")

</div>

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](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
