# single-trial mne sources

**URL:** <https://mne.discourse.group/t/single-trial-mne-sources/1556>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [June 22, 2018, 3:08pm UTC](https://mne.discourse.group/t/single-trial-mne-sources/1556 "2018-06-22T15:08:51Z")\
**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:** [June 22, 2018, 3:08pm UTC](https://mne.discourse.group/t/single-trial-mne-sources/1556/1 "2018-06-22T15:08:51Z")

</div>

External Email - Use Caution

Dear MNE users,

We are trying to perform source localization at the single trial. For a reason we don't understand, the results we obtain when applying the inverse operator on each trial and then computing the average across trials do not really make sense when compared to applying the inverse operator to the averaged epochs. In other words, applying the inverse operator on the evoked object doesn't seem to be equivalent to averaging across trials the sources from each trial. We expected the two to be equivalent since the inverse operation is linear, but perhaps due to regularization, or another reason, the two shouldn't be the same.

If any of you had an example script for source localization at the single trial level it would help us to understand what we are doing wrong.

We have also looked at this example on mne website: "Compute MNE-dSPM inverse solution on single epochs", but didn't find the solution there.

Many thanks in advance

Best

S?bastien & Yair

Some details:

mne version: 0.16.dev0

OS: Ubuntu 16.04

Code:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'''

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apply inverse operator and compare conditions

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'''

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import sys

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import os

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from os.path import join

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import mne

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from mne import morph\_data

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from mne.source\_space import (setup\_source\_space, morph\_source\_spaces,

add\_source\_space\_distances)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from mne.minimum\_norm import (make\_inverse\_operator, read\_inverse\_operator, apply\_inverse\_epochs)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from sentcomp\_epoching import get\_condition

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import numpy as np

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import numpy.matlib

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data\_path = '/neurospin/meg/meg\_tmp/sentcomp\_Marti\_2016/Seb/data'

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subjects\_dir = join(data\_path, 'anatomy', 'subjects')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meg\_dir = join(data\_path, 'sss')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;os.environ['SUBJECTS\_DIR'] = subjects\_dir

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;os.environ['MEG\_DIR'] = meg\_dir

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;speed\_oi = 4

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if speed\_oi==1:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;real\_speed = .083

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif speed\_oi==2:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;real\_speed = .116

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif speed\_oi==3:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;real\_speed = .166

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif speed\_oi==4:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;real\_speed = .301

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# read epochs

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epochs = mne.read\_epochs(join(data\_path, 'epochs', subject + '\_speed'

+ str(speed) + '\_V2-epo.fif'))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epochs.apply\_baseline(baseline=(-0.76,-0.3), verbose=None)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# read inverse operator

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inv\_meg = read\_inverse\_operator(join(data\_path, 'sources', subject +

'-meg-oct-6-meg-inv.fif'))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get conditions

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;knames2, ep2 = get\_condition(conditions=c2,epochs=epochs,startTime=-.2,

duration=1.5,real\_speed=real\_speed)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if timelock\_to\_word==True:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev2 = ep2.average()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev2 = epochs[knames2].average()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ep2 = epochs[knames2]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;snr = 3.0

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lambda2 = 1./snr\*\*2

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# apply inverse operator

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stc\_meg2 = apply\_inverse\_epochs(ep2, inv\_meg, lambda2=lambda2, method='MNE', pick\_ori=None, nave=1)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;av\_stc\_meg2 = np.mean(stc\_meg2) # average of inverse

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev\_stc\_meg2 = apply\_inverse(ev2, inv\_meg, lambda2=lambda2, method='MNE', pick\_ori=None) # inverse of average

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;av\_stc\_meg2.plot(subject='am150105', surface='inflated', hemi='lh', smoothing\_steps=12, time\_viewer=True, subjects\_dir=subjects\_dir, figure=None, views='lat', backend='mayavi', time\_unit='ms', spacing='oct6')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev\_stc\_meg2.plot(subject='am150105', surface='inflated', hemi='lh', smoothing\_steps=12, time\_viewer=True, subjects\_dir=subjects\_dir, figure=None, views='lat', backend='mayavi', time\_unit='ms', spacing='oct6')

-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180622/5bc2513d/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180622/5bc2513d/attachment.html)

---

<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:** [June 22, 2018, 3:21pm UTC](https://mne.discourse.group/t/single-trial-mne-sources/1556/2 "2018-06-22T15:21:31Z")

</div>

External Email - Use Caution

Hi Sebastien!  
(How's life?)

The inverse operator is not always linear. For example it can depend  
whether you use fixed or loose orientations.

In addition, single trials have lower SNR than average, so you may want to  
adapt the lambda parameter.

If you haven't already, you can check:  
[http://www.martinos.org/mne/stable/auto\_examples/inverse/plot\_compute\_mne\_inverse\_epochs\_in\_label.html](http://www.martinos.org/mne/stable/auto_examples/inverse/plot_compute_mne_inverse_epochs_in_label.html)

My 2 cc

JR

---

<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:** [June 22, 2018, 3:55pm UTC](https://mne.discourse.group/t/single-trial-mne-sources/1556/3 "2018-06-22T15:55:47Z")

</div>

Dear S?bastien & Yair,

with the default settings the SourceEstimate object only contains the magnitude of the source currents, but not the direction. Try using pick\_ori=?vector?, which keeps the full vector source estimate. See: [https://martinos.org/mne/stable/auto\_tutorials/plot\_dipole\_orientations.html](https://martinos.org/mne/stable/auto_tutorials/plot_dipole_orientations.html) After averaging, you can use the .magnitude() method to drop the directionality if desired to obtain the traditional ?blobs? view.

best,  
Marijn.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;External Email - Use Caution
> 
> Dear MNE users,  
> We are trying to perform source localization at the single trial. For a reason we don't understand, the results we obtain when applying the inverse operator on each trial and then computing the average across trials do not really make sense when compared to applying the inverse operator to the averaged epochs. In other words, applying the inverse operator on the evoked object doesn't seem to be equivalent to averaging across trials the sources from each trial. We expected the two to be equivalent since the inverse operation is linear, but perhaps due to regularization, or another reason, the two shouldn't be the same.
> 
> If any of you had an example script for source localization at the single trial level it would help us to understand what we are doing wrong.  
> We have also looked at this example on mne website: "Compute MNE-dSPM inverse solution on single epochs", but didn't find the solution there.
> 
> Many thanks in advance
> 
> Best
> 
> S?bastien & Yair
> 
> Some details:  
> mne version: 0.16.dev0
> 
> OS: Ubuntu 16.04
> 
> Code:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'''  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apply inverse operator and compare conditions  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'''
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import sys  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import os  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from os.path import join
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import mne  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from mne import morph\_data  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from mne.source\_space import (setup\_source\_space, morph\_source\_spaces,  
> add\_source\_space\_distances)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from mne.minimum\_norm import (make\_inverse\_operator, read\_inverse\_operator, apply\_inverse\_epochs)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from sentcomp\_epoching import get\_condition  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import numpy as np  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;import numpy.matlib
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data\_path = '/neurospin/meg/meg\_tmp/sentcomp\_Marti\_2016/Seb/data'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subjects\_dir = join(data\_path, 'anatomy', 'subjects')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meg\_dir = join(data\_path, 'sss')
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;os.environ['SUBJECTS\_DIR'] = subjects\_dir  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;os.environ['MEG\_DIR'] = meg\_dir
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;speed\_oi = 4  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if speed\_oi==1:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;real\_speed = .083  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif speed\_oi==2:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;real\_speed = .116  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif speed\_oi==3:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;real\_speed = .166  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif speed\_oi==4:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;real\_speed = .301
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# read epochs  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epochs = mne.read\_epochs(join(data\_path, 'epochs', subject + '\_speed'  
> + str(speed) + '\_V2-epo.fif'))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epochs.apply\_baseline(baseline=(-0.76,-0.3), verbose=None)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# read inverse operator  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inv\_meg = read\_inverse\_operator(join(data\_path, 'sources', subject +  
> '-meg-oct-6-meg-inv.fif'))
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# get conditions  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;knames2, ep2 = get\_condition(conditions=c2,epochs=epochs,startTime=-.2,  
> duration=1.5,real\_speed=real\_speed)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if timelock\_to\_word==True:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev2 = ep2.average()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev2 = epochs[knames2].average()  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ep2 = epochs[knames2]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;snr = 3.0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lambda2 = 1./snr\*\*2
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# apply inverse operator  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stc\_meg2 = apply\_inverse\_epochs(ep2, inv\_meg, lambda2=lambda2, method='MNE', pick\_ori=None, nave=1)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;av\_stc\_meg2 = np.mean(stc\_meg2) # average of inverse
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev\_stc\_meg2 = apply\_inverse(ev2, inv\_meg, lambda2=lambda2, method='MNE', pick\_ori=None) # inverse of average
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;av\_stc\_meg2.plot(subject='am150105', surface='inflated', hemi='lh', smoothing\_steps=12, time\_viewer=True, subjects\_dir=subjects\_dir, figure=None, views='lat', backend='mayavi', time\_unit='ms', spacing='oct6')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ev\_stc\_meg2.plot(subject='am150105', surface='inflated', hemi='lh', smoothing\_steps=12, time\_viewer=True, subjects\_dir=subjects\_dir, figure=None, views='lat', backend='mayavi', time\_unit='ms', spacing='oct6')
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> 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)
> 
> The information in this e-mail is intended only for the person to whom it is  
> addressed. If you believe this e-mail was sent to you in error and the e-mail  
> contains patient information, please contact the Partners Compliance HelpLine at  
> [MyComplianceReport.com: Compliance and Ethics Reporting](http://www.partners.org/complianceline) . If the e-mail was sent to you in error  
> but does not contain patient information, please contact the sender and properly  
> dispose of the e-mail.

-------------- next part --------------  
A non-text attachment was scrubbed...  
Name: smime.p7s  
Type: application/pkcs7-signature  
Size: 1386 bytes  
Desc: not available  
Url : [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180622/d429b3e4/attachment.bin](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180622/d429b3e4/attachment.bin)  
-------------- next part --------------  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;External Email - Use Caution  
-------------- next part --------------  
An HTML attachment was scrubbed...  
URL: [http://mail.nmr.mgh.harvard.edu/pipermail/mne\_analysis/attachments/20180622/d429b3e4/attachment.html](http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20180622/d429b3e4/attachment.html)
