# Morph dipole to fsaverage brain

**URL:** <https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [August 26, 2020, 11:41am UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297 "2020-08-26T11:41:06Z")\
**Posts on this page:** 8\
**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:** [August 26, 2020, 11:41am UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297/1 "2020-08-26T11:41:06Z")

</div>

External Email - Use Caution

Dear MNE-Python community,

I have a bunch of dipoles that were fitted in the brains of several subjects.  
Now I want to visualize the locations of all these dipoles in a shared brain.  
Is there a way to morph the positions of the dipoles from the individual  
subjects to a template brain, for example fsaverage?

I see that there is a head\_to\_mni function that will convert the position to MNI  
space. That is close, but does not match fsaverage space. Or is there a way to  
visualize the MNI brain in a 3d sort of way (not MRI image slices)?

best,  
Marijn.

---

<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:** [August 26, 2020, 11:46am UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297/2 "2020-08-26T11:46:56Z")

</div>

External Email - Use Caution

Hi Marijn,

Is morph = mne.compute\_source\_morph() and morph.apply() what you are  
looking for?

Best,

Christian

---

<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:** [August 26, 2020, 11:51am UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297/3 "2020-08-26T11:51:35Z")

</div>

External Email - Use Caution

hi,

how do you want to visualize this? you can go from MNI to fsaverage  
surface RAS but the dipole will not be positioned on the surface.

Alex

---

<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:** [August 26, 2020, 12:45pm UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297/4 "2020-08-26T12:45:40Z")

</div>

External Email - Use Caution

fsaverage is in MNI space, so you should be able to use that affine if you  
want. If they don't land in the right place it suggests some problem with  
how you're applying the transformation or a problem return the  
transformation itself, which you could manually adjust using tkregister  
(then probably rerun the subsequent recon steps) or something similar.

If you want to go beyond affine, you could use the SDR volumetric  
transformation code in compute\_source\_morph. You could try using a full 1mm  
MRI resolution, but this might run into memory problems. You could code it  
yourself if you want to try it from reading the MNE code, but you have to  
be careful about the coordinate frames.

We could extend compute\_source\_morph to work with discrete source spaces  
using this full-resolution MRI intermediate approach. Then you could set up  
each subjects one dipole (or as many ECDs add you have) as a discrete  
source space with one element, and morph it/them.

Eric

---

<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:** [August 26, 2020, 1:42pm UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297/5 "2020-08-26T13:42:54Z")

</div>

External Email - Use Caution

Hi Eric,

if I morph the dipoles to MNI space (mne.head\_to\_mni(dip.pos)) and throw them on  
top of an fsaverage brain with brain.add\_foci(morphed\_dip\_pos), I don't get  
reasonable results, points are floating way outside of the brain. They are sort  
of aligned on the brain, so origin point may be reasonable, but the scaling I  
wouldn't exactly call close enough.

Making compute\_source\_morph work for discrete source spaces would be a great  
solution. But that sounds hard 🙂

best,  
Marijn.

---

<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:** [August 26, 2020, 2:04pm UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297/6 "2020-08-26T14:04:22Z")

</div>

External Email - Use Caution

Hi Marijn,

Have you tried to pick the closest subject surface to your dipole location  
and use the morph\_surface to get the alignment? Not know how the morph  
works, ny hack would something like this:

elec\_xyz = np.random.randn(3)\*50  
surf\_xyz, \_ = nibabel.freesurfer.read\_geometry('sub-0001/surf/lh.pial')

distances = np.sum((surf\_xyz - elec\_xyz[None,:])\*\*2, 1)  
closest = np.argmin(distances)  
data = np.zeros(len(surf\_xyz))  
data[closest] = 1.

stc = mne.SourceEstimate(data, vertices=range(len(surf\_xyz)), tmin=0.,  
tstep=1., subject='sub-0001')  
morph = mne.compute\_source\_morph(stc, subject\_to='fsaverage',  
subjects\_dir='./')  
stc\_morph = morph.apply(stc)  
vert\_idx = stc\_morph.vertices[np.argmax(stc\_morph.data)] # fixme deal with  
left and right hemi

surf\_xyz, \_ = nibabel.freesurfer.read\_geometry('fsaverage/surf/lh.pial')

plt.plot(\*surf\_xyz[vert\_idx])

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:** [August 26, 2020, 2:12pm UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297/7 "2020-08-26T14:12:58Z")

</div>

External Email - Use Caution

Marijn,

If you can replicate the mni problem with sample or privately share the MRI  
with me I'm happy to look into the MNI problem.

Also please open an issue about morphing discrete source spaces. We should  
have a way to do it, and I actually don't think it will require many  
changes.

Eric

---

<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:** [August 28, 2020, 10:23am UTC](https://mne.discourse.group/t/morph-dipole-to-fsaverage-brain/2297/8 "2020-08-28T10:23:22Z")

</div>

External Email - Use Caution

Hi Eric,

I've narrowed it down to a problem with the talairach.xfm files in the  
FreeSurfer folder. Thanks for your suggestion!

Here is a script testing dipole plotting in various ways on the  
MNE-Sample data, where all is well:

[https://gist.github.com/wmvanvliet/c8e3aa6f312da5a82d75ba44b38badcc](https://gist.github.com/wmvanvliet/c8e3aa6f312da5a82d75ba44b38badcc)

best,  
Marijn.
