MRI coordinate to closest volume source point

External Email - Use Caution

Hello MNE community,

I've made a volume source space for the MNE-Somato dataset like so:

import mne
import mne_bids

bids_root = mne.datasets.somato.data_path()
subjects_dir = f'{bids_root}/derivatives/freesurfer/subjects'
trans =
mne_bids.get_head_mri_trans(f'{bids_root}/sub-01/meg/sub-01_task-somato_meg.fif',
bids_root)
bem = mne.make_bem_model('01', ico=4, subjects_dir=subjects_dir)
bem_sol = mne.make_bem_solution(bem)
src = mne.setup_volume_source_space(subject='01', bem=bem_sol,
subjects_dir=subjects_dir)

How would I translate an MRI coordinate, lets say (x=33, y=8, z=57) to the
nearest source point (="vertex") in the source space? Could someone explain a
bit about the coordinate systems and transforms in play?

best,
Marijn.

External Email - Use Caution

This documentation might help:

https://mne.tools/dev/overview/implementation.html#the-forward-solution

MNE uses "Freesurfer surface RAS" coordinates for the source space, so make
sure your points are in that coordinate frame in meters. So if they x/y/z
you gave were in surface RAS mm, divide by 1000. If they were voxel
numbers, you could use img.header.vox2ras_tkr after loading your img with
nibabel.load, then divide by 1000 (since Nibabel uses mm units).

Once your points are in meters in Freesurfer surface RAS coordinate system,
you can just brute force the nearest calculation using some suitable
nearest-point algorithm (scipy's KDTree, sklearn's BallTree, or just
scipy.spatial.distance.cdist plus argmin) to the `src[0]['rr']` points.

Eric