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) 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]
?
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