forked from OHBA-analysis/osl-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosl_headmodel.m
176 lines (146 loc) · 6.09 KB
/
osl_headmodel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function D = osl_headmodel(S)
% OSL_HEADMODEL runs MEG coregistration and forward model in SPM8 or
% SPM12. These two tasks are separately performed by nosl_datareg.m and
% osl_forward_model.m which are wrapped together to ensure functionality
% of local spheres forward model when montaging has been applied
%
% D = osl_headmodel(S)
%
% REQUIRED INPUTS:
%
% S.D - SPM MEG object filename or MEEG
%
% S.mri - structural MRI nii file name (set S.mri=[] or '' to
% use template structural)
%
% S.useheadshape - set to 0 or 1 to indicated if the headshape points
% should be used in the registration
%
%
% OPTIONAL INPUTS:
%
% S.use_rhino - use RHINO coregistration instead of SPM
%
% S.forward_meg - 'Single Shell' or 'MEG Local Spheres' (default)
%
% S.forward_eeg - 'EEG BEM' (default)
%
% S.fid - Fiducial definition: [] for manual placement, or
% define coordinates using the following fields:
%
% .label - Fiducial labels with fields:
% .nasion (Neuromag default 'Nasion')
% .lpa (Neuromag default 'LPA')
% .rpa (Neuromag default 'RPA')
%
% .coords - Specify fiducual coordinates with fields:
% .nasion - [1 x 3]
% .lpa - [1 x 3]
% .rpa - [1 x 3]
% (leave empty to use SPM defaults)
%
% .coordsys - Specify fiducial coordinate system as:
% 'Native' or 'MNI' (default 'MNI')
%
% Romesh Abeysuriya 2017
% Adam Baker 2014
%%%%%%%%%%%%%%%%%%%%%%% P A R S E I N P U T S %%%%%%%%%%%%%%%%%%%%%%%
% Check SPM File Specification:
if isa(S.D,'meeg')
D = S.D;
S.D = D.fullfile;
else
D = spm_eeg_load(S.D);
end
% Check Headmodel Specification:
try
S = ft_checkopt(S,'forward_meg','char',{'Single Shell','MEG Local Spheres'});
catch
warning('MEG Forward model specification not recognised or incorrect, assigning default: "Single Shell"')
S = ft_setopt(S,'forward_meg','Single Shell');
end
% Check Headmodel Specification:
try
S = ft_checkopt(S,'forward_eeg','char',{'EEG BEM'});
catch
if isfield(S,'forward_eeg')
error('EEG Forward model specification not recognised or incorrect')
end
end
% Check RHINO Specification:
try
S = ft_checkopt(S,'use_rhino',{'double','logical'});
catch
S = ft_setopt(S,'use_rhino',0);
end
% Check Structural Specification:
S.mri = char(S.mri);
if ~isempty(S.mri)
assert(logical(exist(S.mri,'file')),'Structural MRI %s not found',S.mri);
[~,~,ext] = fileparts(S.mri);
if strcmp(ext,'.gz') && S.use_rhino == 0
% Try unzipping
fnames = gunzip(S.mri);
assert(length(fnames)==1,'Specified .gz file contained more than one file');
S.mri = fnames{1};
[~,~,ext] = fileparts(S.mri);
end
if ~strcmp(ext,'.nii') && S.use_rhino == 0
error('S.mri must be .nii (not .nii.gz) when using SPM coregistration')
end
end
% Check Headshape Specification:
try
S = ft_checkopt(S,'useheadshape',{'single','double','logical'},{0,1});
catch
warning('Headshape specification not recognised or incorrect, assigning default: "1"')
S = ft_setopt(S, 'useheadshape', 1);
end
if S.use_rhino
%%%%% R U N C O R E G I S T R A T I O N U S I N G R H I N O %%%%%
S_coreg = S;
S_coreg.modality = {};
if isfield(S, 'forward_meg'),
S_coreg = rmfield(S_coreg, 'forward_meg');
S_coreg.modality(end+1) = {'MEG'};
end
if isfield(S, 'forward_eeg'),
S_coreg = rmfield(S_coreg, 'forward_eeg');
S_coreg.modality(end+1) = {'EEG'};
end
S_coreg.do_plots = 0;
D = rhino(S_coreg);
close all
%%%%%%%%%%%%%%%%%% R U N F O R W A R D M O D E L %%%%%%%%%%%%%%%%%%
D = osl_forward_model(D,S);
D.save()
else % ~S.use_rhino
%%%%%%% R U N C O R E G I S T R A T I O N U S I N G S P M %%%%%%%
matlabbatch{1}.spm.meeg.source.headmodel.D = {S.D};
matlabbatch{1}.spm.meeg.source.headmodel.val = 1;
matlabbatch{1}.spm.meeg.source.headmodel.comment = '';
if isempty(S.mri)
matlabbatch{1}.spm.meeg.source.headmodel.meshing.meshes.mri = {''};
else
matlabbatch{1}.spm.meeg.source.headmodel.meshing.meshes.mri = {[S.mri ',1']};
end;
matlabbatch{1}.spm.meeg.source.headmodel.meshing.meshres = 2;
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(1).fidname = S.fid.label.nasion;
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(2).fidname = S.fid.label.lpa;
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(3).fidname = S.fid.label.rpa;
if(isfield(S,'fid_mnicoords'))
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(1).specification.type = S.fid.mnicoords.nasion;
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(2).specification.type = S.fid.mnicoords.lpa;
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(3).specification.type = S.fid.mnicoords.rpa;
else
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(1).specification.select = 'nas';
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(2).specification.select = 'lpa';
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.fiducial(3).specification.select = 'rpa';
end
matlabbatch{1}.spm.meeg.source.headmodel.coregistration.coregspecify.useheadshape = S.useheadshape;
matlabbatch{1}.spm.meeg.source.headmodel.forward.eeg = 'EEG BEM';
matlabbatch{1}.spm.meeg.source.headmodel.forward.meg = S.forward_meg;
spm_jobman('run', matlabbatch);
end % if S.use_rhino
D = spm_eeg_load(S.D); % Reload the file
end%osl_headmodel