You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
computing forward model using EEG electrode coords MRI voxel space? #3877
about the head model and forward computation, but i'm having trouble getting my EEG electrodes in a form that MNE can recognize.
right now, i have electrode locations as freesurfer voxel coordinates (in the 256x256x256 freesurfer volume).
i'm wondering if there is a way to bypass all the fiducial/digitization stuff, since i already have the electrode locations in MRI space (the data is from simultaneous EEG-FMRI experiments, so we were able to image the EEG cap in the scanner and consequently the electrodes are already aligned to the T1.
In principle each coordinate is accompanied by a variable indicating which coordinate frame it's in, but I am not sure if all functions would support having non-standard coordinate frames, i.e. adding channels with coordinates in the MRI coordinate frame.
Actually I meant to suggest that you use standard coordinate frames, but you could leave the sensor coordinates unchanged, so that you can use identity matrices as transform matrices between coordinate frames (taking into account unit conversion). I don't think having a non-standard origin/orientation (e.g. Neuromag space but not having the origin between the ears) of the sensor positions will affect much (although it is possible that you might get unexpected results from plots that expect a certain orientation).
I don't know if all functions are built to support objects with coordinates in non-standard coordinate frames, so I would not suggest doing that without checking the functions, but someone else might know more.
Some things will work well with this approach and others will work poorly.
Can you also get the lpa/rpa/nasion voxels? Then you can get everything
properly defined.
Okay on a real computer so I can answer more fully.
i'm wondering if there is a way to bypass all the fiducial/digitization stuff, since i already have the electrode locations in MRI space
Yes you can but some things will not work so well. The difference between what is required to do it properly vs. incompletely is mostly just getting the MRI voxels for LPA/RPA/Nasion.
The best procedure would be something like:
Get eeg channel locations in MRI voxels.
Get LPA/Nasion/RPA in MRI voxels.
Use nibabel's get_affine or maybe get_vox2ras_tkr (I think the latter) to convert your voxels to mm or m.
Ensure these points are in meters.
Construct a DigMontage with nasion, lpa, rpa, and dig_ch_pos arguments and use convert=True.
Use raw.apply_montage, which should set the dig information that the forward code needs.
You probably need to turn that matrix into a proper Transform with trans = mne.Transform('mri', 'head', trans_matrix).
Use plot_trans to see if it all worked correctly.
If this works, we can make it simpler in a few different ways (e.g., avoiding use of get_ras_to_neuromag_trans). But first let's get it working.
If you can't or don't want to get the fiducials, you can skip step 2 and instead pass something like lpa=[-0.1, 0, 0], rpa=[0.1, 0, 0], nasion=[0, 0.1, 0] to DigMontage, which should effectively set the head and MRI coordinate frames equal. Then you should also skip steps 7 and 8 and use an identity head<->MRI transform.
thanks for the help. i got up to step 6, but i can't find an apply_montage function, and when i try to set_montage with my DigMontage, it crashes (i'm using the non-fiducial method btw).
also, what do you mean by use convert=True in 5? i couldn't find any convert option in the doc for that function.
here is the code:
import nibabel as nib
path = '/media/sf_shared/all_subs_freesurfer_segs/sub1_alex/mri/'
img=nib.load(path+'T1.mgz') # T1
vox2ras = img.get_header().get_vox2ras() # transform matrix
coords = nib.load(path+'coords.nii.gz') # brain with color-coded coords
coordvol = coords.get_data()
# mat file with electrode labels corresponding to coords above
elabs = scipy.io.loadmat('/media/sf_shared/subutes/a2_good/elabs.mat')
elabs = elabs['elabs'][0]
# find the voxel indices of each coordinate
allcoords = np.zeros([elabs.shape[0],4])
for i in np.arange(0,elabs.shape[0]):
val = i+1 ; # ignore 0 (background)
ind_i = np.where(coordvol==val)
allcoords[i,0] = ind_i[0][0]
allcoords[i,1] = ind_i[1][0]
allcoords[i,2] = ind_i[2][0]
allcoords[i,3] = 1 ;
print val
# multiply voxel indices by transformation matrix
ras_elecs = np.dot(vox2ras,allcoords.T)/1000 # apply RAS transform
# define fids
lpa=[-0.1, 0, 0]
rpa=[0.1, 0, 0]
nasion=[0, 0.1, 0]
# create electrode label - coordinate dictionary
elec_dic = {}
for i in np.arange(0,allcoords.shape[0]):
elec_dic[str(elabs[i][0])] = ras_elecs[0:2,i]
# create digital montage
dig_mont = mne.channels.DigMontage(hsp=[],hpi=[],elp=[],
point_names=[],nasion=nasion,lpa=lpa,rpa=rpa,dig_ch_pos=elec_dic)
# set montage (no raw.apply_montage was found)
raw.set_montage(dig_mont)
here is the error of the set_montage call (i assume its because the coords are 3d?)
`Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/usr/local/lib/python2.7/dist-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/home/brain/projects/eegpython/montage.py", line 58, in <module>
raw.set_montage(dig_mont)
File "<string>", line 2, in set_montage
File "/usr/local/lib/python2.7/dist-packages/mne/utils.py", line 708, in verbose
return function(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/mne/channels/channels.py", line 425, in set_montage
_set_montage(self.info, montage)
File "/usr/local/lib/python2.7/dist-packages/mne/channels/montage.py", line 701, in _set_montage
dig_ch_pos=montage.dig_ch_pos)
File "/usr/local/lib/python2.7/dist-packages/mne/io/meas_info.py", line 509, in _make_dig_points
if hpi.shape[1] == 3:
IndexError: tuple index out of range`
hey jona, thanks for fixing my post. did you manage to get it working? i'm still confused over the use of digMontage, i guess i'm supposed to load the montage as a fif (after saving my RAS coordinates as a dig montage fif, not sure how to do this though) because the digMontage function has no 'convert' option...
also i'm still not able to find an apply_montage function...were you talking about set_montage, Eric?
thanks
Probably set_montage, yeah.
You shouldn't need to save and load. We'll need to expose the convert
option. I thought it was in the class constructor but it must only be in
the function. I can do this Tuesday or Wednesday.
If there is any sample data you can share privately or even better
publicly, feel free to email me. It will reduce the number of iterations if
I can debug remotely. But even if you don't have sharable data, I think I
can fake it with the sample data now that I think about it.
If this is expected to be a common use case I can try to turn it into a
tutorial.
@russellu this is on hold a little bit until we take care of #3893 (we'll address this then) probably next month. In the meantime you at least have something that works, right?
@russellu just wanted to let you know that @jona-sassenhagen has developed a workflow for this. He will open a PR soon to demo the workflow. Hopefully that will work while we sort out a simpler workflow.
@russellu I hope Eric hasn't promised more than I can keep :) I think what I did only concerns 1-4 and 7-8 of Eric's post.
My situation is that we can identify the fiducials in MRI space, and that is our only coreg info, so we have to get the precise location of the fiducials into MNE. So what I am doing is:
identify the points in question on the T1; I use mne_analyze -> load surface -> view MRI, but you can also use freeview; here, you can find the position of the coreg markers in RAS.
then, I use this function to create a trans file:
def _make_trans(subj, fids_ras, subjects_dir=subjects_dir, write=True):
trans_matrix = mne.transforms.get_ras_to_neuromag_trans(**fids_ras)
trans = mne.Transform('mri', 'head', trans_matrix)
if write:
fname = subjects_dir + "{subj}/{subj}_fid-trans.fif".format(subj=subj)
mne.write_trans(fname, trans)
return trans
(where fids_ras is a dictionary with keys rpa, lpa, nasion and values being an array of the RAS coordinates divided by 1000 - get_ras_to_neuromag_trans expects the coordinates in meters, but freesurfer tells them in mm. E.g.:
From what I can tell, you've set up the vox2ras transfer correctly btw.
)
Then I use this file for my coreg and FWD.
I'm not sure how much that helps you in your case though!
The key insight was that it's pretty easy to map something from MRI to head space: freesurfer's Volume RAS ~ Surface RAS, and that you can feed into mne.transforms.get_ras_to_neuromag_trans for a trans matrix.
I mentioned this on Gitter, but I think this could make for a nice little example (and eventually tutorial). You could try to replicate the process with the sample subject, for example.
Activity
christianbrodbeck commentedon Dec 29, 2016
Since all coordinates have RAS representations (http://martinos.org/mne/stable/manual/source_localization/forward.html#meg-eeg-and-mri-coordinate-systems) I think the straight forward solution would be to create and use an identity transform, which should be pretty quick (http://martinos.org/mne/stable/generated/mne.Transform.html). MNE uses [m], is the FreeSurfer coordinate frame in [m] or in [mm]? That would have to be taken into account.
In principle each coordinate is accompanied by a variable indicating which coordinate frame it's in, but I am not sure if all functions would support having non-standard coordinate frames, i.e. adding channels with coordinates in the MRI coordinate frame.
russellu commentedon Dec 29, 2016
i see...so you're saying i should just manually change the coordinate frame variable in my epochs.info, so that it is set to freesurfer RAS?
christianbrodbeck commentedon Dec 29, 2016
Actually I meant to suggest that you use standard coordinate frames, but you could leave the sensor coordinates unchanged, so that you can use identity matrices as transform matrices between coordinate frames (taking into account unit conversion). I don't think having a non-standard origin/orientation (e.g. Neuromag space but not having the origin between the ears) of the sensor positions will affect much (although it is possible that you might get unexpected results from plots that expect a certain orientation).
I don't know if all functions are built to support objects with coordinates in non-standard coordinate frames, so I would not suggest doing that without checking the functions, but someone else might know more.
larsoner commentedon Dec 29, 2016
larsoner commentedon Dec 29, 2016
Okay on a real computer so I can answer more fully.
Yes you can but some things will not work so well. The difference between what is required to do it properly vs. incompletely is mostly just getting the MRI voxels for LPA/RPA/Nasion.
The best procedure would be something like:
get_affineor maybeget_vox2ras_tkr(I think the latter) to convert your voxels to mm or m.convert=True.raw.apply_montage, which should set the dig information that the forward code needs.trans = mne.Transform('mri', 'head', trans_matrix).plot_transto see if it all worked correctly.If this works, we can make it simpler in a few different ways (e.g., avoiding use of get_ras_to_neuromag_trans). But first let's get it working.
If you can't or don't want to get the fiducials, you can skip step 2 and instead pass something like
lpa=[-0.1, 0, 0], rpa=[0.1, 0, 0], nasion=[0, 0.1, 0]to DigMontage, which should effectively set the head and MRI coordinate frames equal. Then you should also skip steps 7 and 8 and use an identity head<->MRI transform.russellu commentedon Dec 30, 2016
thanks for the help. i got up to step 6, but i can't find an apply_montage function, and when i try to set_montage with my DigMontage, it crashes (i'm using the non-fiducial method btw).
also, what do you mean by use convert=True in 5? i couldn't find any convert option in the doc for that function.
here is the code:
jona-sassenhagen commentedon Dec 30, 2016
I think this is also relevant to me. I will see if it works for me.
@russellu I have updated your comment to make the code look like code :)
russellu commentedon Dec 31, 2016
hey jona, thanks for fixing my post. did you manage to get it working? i'm still confused over the use of digMontage, i guess i'm supposed to load the montage as a fif (after saving my RAS coordinates as a dig montage fif, not sure how to do this though) because the digMontage function has no 'convert' option...
also i'm still not able to find an apply_montage function...were you talking about set_montage, Eric?
thanks
larsoner commentedon Dec 31, 2016
larsoner commentedon Jan 17, 2017
@russellu this is on hold a little bit until we take care of #3893 (we'll address this then) probably next month. In the meantime you at least have something that works, right?
russellu commentedon Jan 17, 2017
Hey Eric, yeah i'm using brainstorm for the moment but i will switch back when you get this handled, thanks!
larsoner commentedon May 4, 2017
@russellu just wanted to let you know that @jona-sassenhagen has developed a workflow for this. He will open a PR soon to demo the workflow. Hopefully that will work while we sort out a simpler workflow.
russellu commentedon May 4, 2017
@Eric89GXL @jona-sassenhagen awesome, thanks! btw are any of you gonna be at ohbm this summer?
agramfort commentedon May 5, 2017
jona-sassenhagen commentedon May 6, 2017
@russellu I hope Eric hasn't promised more than I can keep :) I think what I did only concerns 1-4 and 7-8 of Eric's post.
My situation is that we can identify the fiducials in MRI space, and that is our only coreg info, so we have to get the precise location of the fiducials into MNE. So what I am doing is:
(where fids_ras is a dictionary with keys rpa, lpa, nasion and values being an array of the RAS coordinates divided by 1000 - get_ras_to_neuromag_trans expects the coordinates in meters, but freesurfer tells them in mm. E.g.:
From what I can tell, you've set up the vox2ras transfer correctly btw.
)
Then I use this file for my coreg and FWD.
I'm not sure how much that helps you in your case though!
The key insight was that it's pretty easy to map something from MRI to head space: freesurfer's Volume RAS ~ Surface RAS, and that you can feed into
mne.transforms.get_ras_to_neuromag_transfor a trans matrix.larsoner commentedon May 8, 2017
I mentioned this on Gitter, but I think this could make for a nice little example (and eventually tutorial). You could try to replicate the process with the
samplesubject, for example.jona-sassenhagen commentedon May 8, 2017
Yup, I think will try. I'm interested in the average error of this coreg procedure.