# Source space to brain space plotting

**URL:** <https://mne.discourse.group/t/source-space-to-brain-space-plotting/2824>\
**Category:** Support & Discussions\
**Tags:** ﻿source-localization, visualization\
**Created:** [March 11, 2021, 10:52pm UTC](https://mne.discourse.group/t/source-space-to-brain-space-plotting/2824 "2021-03-11T22:52:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexrockhill](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/alexrockhill/32/31_2.png) [@alexrockhill](https://mne.discourse.group/u/alexrockhill)\
**Post date:** [March 11, 2021, 10:52pm UTC](https://mne.discourse.group/t/source-space-to-brain-space-plotting/2824/1 "2021-03-11T22:52:55Z")

</div>

It looks like the source space (`src`) is in head coordinates but when I tried to follow this to transform to surface RAS ([Source alignment and coordinate frames — MNE 0.22.0 documentation](https://mne.tools/stable/auto_tutorials/source-modeling/plot_source_alignment.html#sphx-glr-auto-tutorials-source-modeling-plot-source-alignment-py)) it didn’t work. How do you transform coordinates from source space to plot on the brain object (stc.plot), the same coordinates in `brain.geo['lh'].coords[vertex]`?

```python
import os
import os.path as op
import numpy as np
import mne
from mne.datasets import sample
import nibabel as nib

data_path = sample.data_path()
subjects_dir = op.join(data_path, 'subjects')
subject = 'sample'
sample_dir = op.join(data_path, 'MEG', subject)

fname_raw = op.join(sample_dir, 'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(fname_raw, preload=True)
events = mne.find_events(raw)
# choose event 3, left visual field stimulus
epochs = mne.Epochs(raw, events, event_id=3, tmin=-0.5, tmax=1, baseline=None)
evoked = epochs.average()

# Read inverse solution
fname_inv = op.join(sample_dir, 'sample_audvis-meg-oct-6-meg-inv.fif')
inv = mne.minimum_norm.read_inverse_operator(fname_inv)

# Apply inverse solution, set to obtain an :class:`mne.SourceEstimate` object
snr = 3.0
lambda2 = 1.0 / snr ** 2
stc = mne.minimum_norm.apply_inverse(evoked, inv, lambda2, 'dSPM', pick_ori='normal')

fwd = mne.read_forward_solution(
    op.join(sample_dir, 'sample_audvis-meg-eeg-oct-6-fwd.fif'))
mne.convert_forward_solution(fwd, surf_ori=True, copy=False)

lh, rh = fwd['src']
vert = lh['rr'][stc.lh_vertno[99]]

os.environ['SUBJECTS_DIR'] = subjects_dir
os.environ['SUBJECT'] = subject

brain = stc.plot(smoothing_steps=0, hemi='both',
                     colorbar=False, time_viewer=False)

trans_fname = op.join(sample_dir, 'sample_audvis_raw-trans.fif')
trans = mne.read_trans(trans_fname)

t1w = nib.load(op.join(data_path, 'subjects', 'sample', 'mri', 'T1.mgz'))
t1w = nib.Nifti1Image(t1w.dataobj, t1w.affine)
t1w.header['xyzt_units'] = np.array(10, dtype='uint8')
t1_mgh = nib.MGHImage(t1w.dataobj, t1w.affine)

vox_to_mri = t1_mgh.header.get_vox2ras_tkr()
mri_to_vox = np.linalg.inv(vox_to_mri)

vert_mri = mne.transforms.apply_trans(trans, vert, move=True)
# Then transform to voxel space, after converting from meters to millimeters
vert_vox = mne.transforms.apply_trans(
    mri_to_vox, vert_mri * 1e3, move=True)

print(vert_vox)
print(brain.geo['lh'].coords[99]) # idea is that they match

```

---

<div class="post-metadata">

**Author:** ![alexrockhill](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/alexrockhill/32/31_2.png) [@alexrockhill](https://mne.discourse.group/u/alexrockhill)\
**Post date:** [March 11, 2021, 10:54pm UTC](https://mne.discourse.group/t/source-space-to-brain-space-plotting/2824/2 "2021-03-11T22:54:35Z")

</div>

Also `lh['coord_frame']` is 4 or head space where `lh` is the left src.

---

<div class="post-metadata">

**Author:** ![wmvanvliet](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/wmvanvliet/32/100_2.png) [@wmvanvliet](https://mne.discourse.group/u/wmvanvliet)\
**Post date:** [March 12, 2021, 10:42am UTC](https://mne.discourse.group/t/source-space-to-brain-space-plotting/2824/3 "2021-03-12T10:42:08Z")

</div>

The MRI Surface RAS \<-\> Head transform is produced during your co-registration step. The famous “trans” file that you need to construct the forward model and such.

For the sample data, it’s located in: MEG/sample/sample\_audvis\_raw-trans.fif

---

<div class="post-metadata">

**Author:** ![wmvanvliet](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/wmvanvliet/32/100_2.png) [@wmvanvliet](https://mne.discourse.group/u/wmvanvliet)\
**Post date:** [March 12, 2021, 10:43am UTC](https://mne.discourse.group/t/source-space-to-brain-space-plotting/2824/4 "2021-03-12T10:43:15Z")

</div>

But you already got that, ok 🙂

---

<div class="post-metadata">

**Author:** ![wmvanvliet](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/wmvanvliet/32/100_2.png) [@wmvanvliet](https://mne.discourse.group/u/wmvanvliet)\
**Post date:** [March 12, 2021, 10:54am UTC](https://mne.discourse.group/t/source-space-to-brain-space-plotting/2824/5 "2021-03-12T10:54:12Z")

</div>

Can you tell a bit more about what you’re trying to accomplish? Because looking at your code, I can see some things that are clearly wrong, but I can’t correct them as I’m not certain what your end goal is.

In the simplest case, if you want to plot a vertex that is part of the source space on top of the brain, you do `brain.add_foci(stc.lh_vertno[99], coords_as_verts = True)`. But perhaps you want something more advanced.

---

<div class="post-metadata">

**Author:** ![alexrockhill](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/alexrockhill/32/31_2.png) [@alexrockhill](https://mne.discourse.group/u/alexrockhill)\
**Post date:** [March 12, 2021, 3:38pm UTC](https://mne.discourse.group/t/source-space-to-brain-space-plotting/2824/6 "2021-03-12T15:38:43Z")

</div>

I want to find a normal vector to a point on the plane (using the two nearest points) and then project all points within 30 cm to that plane in order to determine their angle in the plane for use in simulating a traveling wave. So it would be nice to plot two vectors from a point to the two nearest neighbors, their cross product and then also the projection onto the plane of a few choice points in the cortical patch. I don’t think built-in functions are going to quite cover that, unfortunately.
