|
| 1 | +--- |
| 2 | +title: Create a template source model aligned to MNI space |
| 3 | +category: example |
| 4 | +tags: [meg, mri, headmodel, source] |
| 5 | +--- |
| 6 | + |
| 7 | +# Create template source models aligned to MNI space |
| 8 | + |
| 9 | +On the [template sourcemodel](/template/sourcemodel) page we describe that we have a number of 3D grid |
| 10 | +source models defined in MNI space. These models can be used as templates for individual subject data, |
| 11 | +allowing for more accurate and consistent source reconstruction across subjects. The procedure for this |
| 12 | +is described in the [source model tutorial](/tutorial/sourcemodel). |
| 13 | + |
| 14 | +These source models are only available at a few resolutions, and have dipoles throughout the whole brain |
| 15 | +compartment, including CSF, white matter and gray matter. That is convenient for beamformer source |
| 16 | +reconstruction of MEG and EEG data with a BEM or single shell head model, but not so for minimum |
| 17 | +norm source reconstruction or for source estimates using a FEM model. |
| 18 | + |
| 19 | +The following script describes how to create your own template source model in MNI space, at 3mm |
| 20 | +resolution, and only with dipoles in gray matter. |
| 21 | + |
| 22 | +{% include markup/yellow %} |
| 23 | +Note that "gray" is the American English spelling, while "grey" is the British English spelling. |
| 24 | +Both are correct, but **[ft_volumesegment](/reference/ft_volumesegment)** uses the American |
| 25 | +spelling, whereas SPM uses the British spelling. |
| 26 | +{% include markup/end %} |
| 27 | + |
| 28 | + % read the 1 mm resolution canonical MNI template MRI |
| 29 | + [ftver, ftpath] = ft_version |
| 30 | + mri = ft_read_mri(fullfile(ftpath, 'template/anatomy/single_subj_T1_1mm.nii')); |
| 31 | + mri.coordsys = 'mni'; |
| 32 | + |
| 33 | + % make a tissue probability map segmentation of CSF, white and gray matter |
| 34 | + cfg = []; |
| 35 | + cfg.output = 'tpm'; |
| 36 | + cfg.spmmethod = 'old'; |
| 37 | + seg = ft_volumesegment(cfg,mri); |
| 38 | + |
| 39 | + % there are also voxels that are neither CSF, white or gray matter |
| 40 | + seg.otherwise = ones(seg.dim)-seg.gray-seg.white-seg.csf; |
| 41 | + |
| 42 | + % make a matrix with 4 columns, each row is a voxel |
| 43 | + % the probabilistic sum of all tissue types adds up to 1, or 100% |
| 44 | + prob = [seg.gray(:) seg.white(:) seg.csf(:) seg.otherwise(:)]; |
| 45 | + |
| 46 | + % identify which voxels have the highest probability of being gray matter |
| 47 | + % these are considered to be inside the (binary) grey matter compartment |
| 48 | + inside = zeros(seg.dim); |
| 49 | + for voxel = 1:prod(seg.dim) |
| 50 | + [m,tissue] = max(prob(voxel,:)); % tissue = column, m = number |
| 51 | + inside(voxel) = tissue == 1; |
| 52 | + end |
| 53 | + |
| 54 | + % these are not needed any more |
| 55 | + seg = rmfield(seg,'gray'); |
| 56 | + seg = rmfield(seg,'white'); |
| 57 | + seg = rmfield(seg,'csf'); |
| 58 | + seg = rmfield(seg,'otherwise'); |
| 59 | + |
| 60 | + % add the inside-gray-matter mask |
| 61 | + seg.inside = inside; |
| 62 | + |
| 63 | + %% prepare a source model for gray matter |
| 64 | + |
| 65 | + cfg = []; |
| 66 | + cfg.method = 'basedongrid'; |
| 67 | + cfg.xgrid = -90:3:90; |
| 68 | + cfg.ygrid = -120:3:90; |
| 69 | + cfg.zgrid = -90:3:90; |
| 70 | + cfg.unit = 'mm'; |
| 71 | + |
| 72 | + % you want the xgrid/ygrid/zgrid numbers to be such, that they cover the entire brain |
| 73 | + % and that they are nicely symmetric around the origin. In this case they are chosen |
| 74 | + % such that there is also a dipole exactly at [0, 0, 0]. |
| 75 | + |
| 76 | + sourcemodel_template = ft_prepare_sourcemodel(cfg); |
| 77 | + |
| 78 | + % the resulting source model consists of a 3D grid of dipoles that spans the brain, |
| 79 | + % but does not specify which ones are inside the grey matter (or the brain) or outside |
| 80 | + |
| 81 | + figure |
| 82 | + ft_plot_mesh(sourcemodel_template.pos) |
| 83 | + |
| 84 | + % you should rotate the figure, you will see that it is a square block with many dipoles |
| 85 | + |
| 86 | + % we now determine for each dipole what tissue type it is in |
| 87 | + sourcemodel_template.inside = zeros(prod(sourcemodel_template.dim),1); |
| 88 | + |
| 89 | + % the following uses the inverse homogenous transformation to go from head to voxel coordinates |
| 90 | + |
| 91 | + for dipole = 1:prod(sourcemodel_template.dim) % loop over all dipoles |
| 92 | + thispos = sourcemodel_template.pos(dipole,:); % the position of this dipole in head coordinates |
| 93 | + thispos = [thispos 1]'; |
| 94 | + thisvox = round(inv(seg.transform)*thispos); % the indices of the nearest voxel in the segmented MRI |
| 95 | + if thisvox(1) < 1 || thisvox(1) > seg.dim(1) |
| 96 | + % it falls outside the segmented volume |
| 97 | + sourcemodel_template.inside(dipole) = 0; |
| 98 | + elseif thisvox(2) < 1 || thisvox(2) > seg.dim(2) |
| 99 | + % it falls outside the segmented volume |
| 100 | + sourcemodel_template.inside(dipole) = 0; |
| 101 | + elseif thisvox(3) < 1 || thisvox(3) > seg.dim(3) |
| 102 | + % it falls outside the segmented volume |
| 103 | + sourcemodel_template.inside(dipole) = 0; |
| 104 | + else |
| 105 | + % look up in the segmented volume whether the nearest voxel is gray matter |
| 106 | + sourcemodel_template.inside(dipole) = seg.inside(thisvox(1), thisvox(2), thisvox(3)); |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + % convert it into a logical array with true/false values |
| 111 | + sourcemodel_template.inside = logical(sourcemodel_template.inside); |
| 112 | + |
| 113 | + % make a plot of the dipoles that are inside the gray matter |
| 114 | + figure |
| 115 | + ft_plot_mesh(sourcemodel_template.pos(sourcemodel_template.inside,:)) |
| 116 | + |
| 117 | + % you should rotate the figure, you will see that it is a brain-shaped cloud of dipoles, only in gray matter |
| 118 | + |
0 commit comments