1- import vedo
2- import numpy as np
3- import nibabel as nib
4- from scipy .spatial import KDTree
5-
6- from .._fiff .constants import FIFF
7- from ..surface import _CheckInside
8- from ..transforms import (
9- apply_trans ,
10- invert_transform ,
11- read_trans ,
12- )
13-
14-
151def fit_spheres_to_mri (subjects_dir , subject , bem , trans , n_spheres ):
162 """Fits two spheres to MRI using BEM, such that spheres fit while brain but
173 do not encroach on sensors. For use with Milti-SSS Maxwell Filtering
@@ -22,8 +8,8 @@ def fit_spheres_to_mri(subjects_dir, subject, bem, trans, n_spheres):
228 director to Freesurfer subjects
239 subject: str
2410 Subject ID
25- bem: bem.ConductorModel
26- istance of bem.ConductorModel , must be three shell conductivity profiles
11+ bem: list
12+ output of mne.make_bem_model() , must be three shell conductivity profiles
2713 trans: str
2814 path to trans file, mri_dev_t information
2915 n_spheres: int
@@ -32,15 +18,35 @@ def fit_spheres_to_mri(subjects_dir, subject, bem, trans, n_spheres):
3218 Returns
3319 -------
3420 centers: np.ndarray
35- 2D array containing the two centers in cartesian coordinates
21+ 2x3 array containing the two centers in HEAD coordinate space
22+ can be directly fed into:
23+ raw_msss = mne.preprocessing.maxwell_filter(raw, origin=origins, ...)
24+ for multi-SSS preprocessing
25+
3626
3727 Notes
3828 -----
39-
4029 * Must have vedo and nibabel installed
4130 * Must have run mne watershed BEM using freesurfer segmentation
4231 """
4332
33+ ## --- required imports
34+ import vedo
35+ import os
36+ import numpy as np
37+ import nibabel as nib
38+ from scipy .spatial import KDTree
39+
40+ from .._fiff .constants import FIFF
41+ from ..surface import _CheckInside
42+ from ..transforms import (
43+ apply_trans ,
44+ invert_transform ,
45+ read_trans ,
46+ )
47+
48+
49+ ## --- begin
4450 mindist = 2e-3
4551 assert bem [0 ]["id" ] == FIFF .FIFFV_BEM_SURF_ID_HEAD
4652 assert bem [2 ]["id" ] == FIFF .FIFFV_BEM_SURF_ID_BRAIN
@@ -55,7 +61,7 @@ def fit_spheres_to_mri(subjects_dir, subject, bem, trans, n_spheres):
5561 s_tree = KDTree (scalp ["rr" ])
5662 brain_volume = b .volume ()
5763 print (f"Brain vedo: { brain_volume * m3_to_cc :8.2f} cc" )
58- brain_vol = nib .load (subjects_dir / subject / " mri" / " brainmask.mgz" )
64+ brain_vol = nib .load (os . path . join ( subjects_dir , subject , ' mri' , ' brainmask.mgz' ) )
5965 brain_rr = np .array (np .where (brain_vol .get_fdata ())).T
6066 brain_rr = (
6167 apply_trans (brain_vol .header .get_vox2ras_tkr (), brain_rr ) / 1000.0
@@ -121,8 +127,14 @@ def _cons(c):
121127 else :
122128 raise ValueError ("Implementation is for 3 or less origins" )
123129
124- # 4. transform centers for return using "trans" matrix
125- mri_head_t = invert_transform (read_trans (trans ))
126- assert mri_head_t ["from" ] == FIFF .FIFFV_COORD_MRI , mri_head_t ["from" ]
130+ # 4. transform centers for return using "mri_head_t" matrix
131+ # Output 2-sphere case
132+ mri_head_t = invert_transform (read_trans (trans )) #trans is the raw_fif_trans file
133+ if mri_head_t ["from" ] == FIFF .FIFFV_COORD_HEAD :
134+ mri_head_t = invert_transform (mri_head_t )
135+ assert mri_head_t ['from' ] == FIFF .FIFFV_COORD_MRI , mri_head_t ['from' ]
127136 centers = apply_trans (mri_head_t , c_opt .reshape (- 1 , 3 ))
128137 return centers
138+
139+
140+
0 commit comments