[FieldTrip] Creating head and source model from Freesurfer T1.mgz
Schoffelen, J.M. (Jan Mathijs)
janmathijs.schoffelen at donders.ru.nl
Mon Aug 25 09:28:18 CEST 2025
Hi Kiran,
Apart from the warning, do you get sensible output? I suspect (but am not sure since no ‘warning stack’ was provided) that the warning about the close-to-singular-matrix is thrown by the SMP routines during the segmentation of the MRI. I have never looked into this, but I think that in this situation it is pretty harmless. If you feel bold to follow up on this, you may want to type ‘dbstop if warning’ on the matlab command line, before executing the call to ft_volumesegment. This will lead you to the debugger-state once the warning is thrown, and you may inspect the culprit matrix (and what it does etc).
Regarding your other questions:
1. well, you shouldn’t do anything if you don’t want it, but coregistration is tricky business, which becomes exponentially more tricky if there’s more than one coordinate system involved (e.g.: the coordsys in which electrodes/MEG sensors are expressed, the coordsys used by Freesurfer, the coordsys according to the MNI convention etc). Having a solid, ‘central’ MRI volume for which the voxel-to-(any semi-arbitrary)world coordinate system is well-defined will probably diminish headaches downstream, because this volume will allow you to define any world-to-world transformation matrix, by cleverly combining the relevant transformation matrices.
2. The way forward would be to use the T1.mgz (which is an intermediate product of recon-all) as input to ft_volumerealign, click around to obtain a transformation matrix that maps from voxels to the neuromag coordsys, and then combine this transformation matrix with the original one, i.e. the mri.transform that you get when reading the MRI from disk: mri=ft_read_mri(’T1.mgz’);
Then, using the two matrices, you can create a 3d matrix transform_tone2neuromag = mri_neuromag.transform/mri_t1.transform; This matrix can be used (e.g. with ft_transform_geometry) to map the freesurfer based cortical model to neuromag coordinates. I am sure there are snippets of code on fieldtriptoolbox.org which will provide additional guidance (look for the tutorials that mention the creation of headmodels and / or sourcemodels).
Best wishes,
Jan-Mathijs
On 22 Aug 2025, at 16:24, Kirandeep Kaur via fieldtrip <fieldtrip at science.ru.nl> wrote:
Dear Fieldtrip community!
I aim trying to generate source model for meg data files recorded in Elekta Neuromag system. However, I have generated the Freesurfer files using each subject's T1w MRI, without re-aligning the volume before running Freesurfer as suggested in this tutorial (sorry I was too used to working in Brainstorm!).
Now, I am trying to generate a headmodel from the Freesurfer generated T1.mgz using the following code, but I keep getting a warning (also shown below).
----------------------
mri_file=strcat(<path to freesurfer dir>, '/mri/T1.mgz');
mri=ft_read_mri(mri_file);
% re-align the volume
cfg=[];
cfg.method='interactive';
cfg.coordsys='neuromag';
mri=ft_volumerealign(cfg,mri);
% create seg file
cfg=[];
cfg.output='brain';
cfg.brainthreshold=0.5;
seg=ft_volumesegment(cfg,mri);
The above code runs, but I am getting the following warning:
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND=1.045258e-22.
My question is:
1.
Should I instead use the original T1w MRI for creating the headmodel, and use the Freesurfer T1.mgz of the same subject for creating the same model?
2.
If yes, what is the best way to co-register and create source model from a Freesurfer generated T1.mgz from a T1.nii which was not aligned to the neuromag coordsys before recon-all.
For context my ultimate aim is to generate virtual channels as per the Schaefer's atlas for the entire .fif file; i.e. simply go from the sensor space to roi parcellated source space after cleaning the data (using ICA).
Any help/suggestions here would be very useful.
Kind regards,
Kiran
_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmailman.science.ru.nl%2Fmailman%2Flistinfo%2Ffieldtrip&data=05%7C02%7Cfieldtrip%40science.ru.nl%7C08a39c4daa3143c5781608dde3a8f6bd%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638917037019232639%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=zPPteKcARu8L4PvEPk7Z6lr8SNIWs5TBQPtW4FXcHks%3D&reserved=0>
https://doi.org/10.1371/journal.pcbi.1002202<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdoi.org%2F10.1371%2Fjournal.pcbi.1002202&data=05%7C02%7Cfieldtrip%40science.ru.nl%7C08a39c4daa3143c5781608dde3a8f6bd%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638917037019253116%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=2sY71T6%2Bx7QZV6s2ze7WLdVUk0RvaHNRWZbY%2BXfyBlc%3D&reserved=0>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20250825/1f81c0f1/attachment.htm>
More information about the fieldtrip
mailing list