-
Notifications
You must be signed in to change notification settings - Fork 14
/
osl_inverse_model.m
371 lines (317 loc) · 19.1 KB
/
osl_inverse_model.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
function D = osl_inverse_model(D,mni_coords,varargin)
% OSL_INVERSE_MODEL runs MEG forward model in SPM12
%
% This function passes a limited set of parameters to SPM batch and returns
% an SPM object containing the inverse solution in an online montage.
%
% D = osl_inverse_model(D,mni_coords,S)
%
% REQUIRED INPUTS:
%
% D - SPM MEG object filename (or SPM MEG object)
%
% mni_coords - [N x 3] list of MNI coordinates to beamform to. A dummy
% variable is required if we are doing a cortical SR
%
%
% OPTIONAL INPUTS:
%
% Passed in as a struct:
%
% S.modalities - Sensor modalities to use (e.g. MEG,MEGPLANAR, or both)
% (default MEGPLANAR for Neuromag, MEG for CTF)
%
%
% S.type - Beamformer type to use {'Scalar' or 'Vector'}
% (default 'Scalar')
%
% S.timespan - Time range to use in seconds [start end]
% (default [0 Inf])
%
% S.pca_order - PCA dimensionality to use for covariance matrix inversion
% (default to full rank)
%
% S.use_class_channel
% - flag indicating whether or not to use the class channel
% in S.D to determine the time samples to be used for
% (default is 0)
%
% S.inverse_method - inverse method to use
% (default is to use 'beamform')
%
% S.conditions - conditions to use
% (default is to use all)
%
% S.dirname - dir to output results to
% (default is a temporary directory created within D.path)
%
% S.mode - reconstruction mode. Can be 'volumetric' (what we
% normally do at OHBA or 'cortical' to constrain
% solutions to the cortex (if for example you've run
% a recon-all in FreeSurfer). Defaults to volumetric
% if not set
%
% S.prefix - write new SPM file by prepending this prefix
% (default is '')
%
% AB 2014, MWW 2014, RT 2020
arg = inputParser;
arg.addParameter('modalities',[],@(x) ischar(x) || iscell(x)); % Sensor modalities to use
arg.addParameter('fuse','no',@(x) any(strcmp(x,{'no','all','meg'}))); % fuse modalities listed in 'modalities' - e.g. set modalities to {'MEGMAG','MEGPLANAR'} to fuse them (does not work with MEGANY)
arg.addParameter('type','Scalar',@(x) any(strcmp(x,{'Scalar','Vector'})));
arg.addParameter('timespan',[0 Inf]); % Time range to use in seconds [start end]
arg.addParameter('pca_order',[],@(x) isnumeric(x) && isscalar(x) && x>0 && ~mod(x,1)); % PCA dimensionality to use for covariance matrix inversion (defaults to full rank)
arg.addParameter('use_class_channel',false); % flag indicating whether or not to use the class channel to determine the time samples to be used for
arg.addParameter('inverse_method','beamform',@(x) any(strcmp(x,{'beamform','beamform_bilateral'}))); %any(strcmp(x,{'beamform','beamform_bilateral','mne_eye','mne_diag_datacov','mne_adaptive'}))); % inverse method to use
arg.addParameter('conditions','all',@(x) ischar(x) || iscell(x)); % Should be a cell array of conditions
arg.addParameter('dirname','',@ischar); % dir to output results to - default/empty makes a temporary folder alongside the D object
arg.addParameter('prefix',''); % write new SPM file by prepending this prefix
arg.addParameter('mode',''); %volumetric or cortical mode
arg.parse(varargin{:});
S = arg.Results;
%%%%%%%%%%%%%%%%%%%%%%% P A R S E I N P U T S %%%%%%%%%%%%%%%%%%%%%%%
old_dir = pwd; % Back up the original working directory
if nargin < 3 || isempty(S)
S = struct;
end
if isa(D,'meeg')
% If the fullfile is relative (e.g. if the user ran `D =
% D.copy('./temp'))` then the paths fail later on. The solution is to
% reload the file e.g. D = spm_eeg_load(D.fullfile) However, this will
% only work properly if the user is the same directory as when they
% created the MEEG object. We prompt the user to do this themselves,
% rather than trying to automatically reload the file. If the user did
% change directory e.g.
%
% D = D.copy('./temp'))
% cd ..
% D = spm_eeg_load(D.fullfile)
%
% likely scenario is that the file won't exist and cannot be loaded. Worst
% case scenario is that the new folder contains an MEEG with the same
% filename, in which case an incorrect file would be loaded. It should
% definitely not be automatically reloaded
if ~strcmp(D.fullfile,getfullpath(D.fullfile))
error('MEEG fullfile (''%s'') is relative - reload the file (e.g. ''D = spm_eeg_load(D.fullfile)'')to make it absolute',D.fullfile);
end
else
if ischar(D) || isstring(D)
[pathstr,filestr] = fileparts(D);
D = fullfile(pathstr,[filestr '.mat']); % force .mat suffix
D = spm_eeg_load(D);
else
error('Unrecognized input time - must be a file name or an MEEG');
end
end
D.check;
D.save(); % Save the object to disk to ensure that the current online montage is used
% Check Modality Specification:
if isempty(S.modalities)
if any(strcmp(unique(D.chantype),'MEGPLANAR'))
default_modality = {'MEGPLANAR'};
else
default_modality = {'MEG'};
end
S.modalities = default_modality;
elseif ischar(S.modalities)
S.modalities = {S.modalities};
end
for j = 1:length(S.modalities)
assert(~isempty(D.indchantype(S.modalities{j},'GOOD')),'No good channels found for modality %s',S.modalities{j});
end
% Check PCA order
if isempty(S.pca_order)
S.pca_order = D.nchannels;
fprintf('Setting PCA order to %d (full rank)\n',D.nchannels);
end
% Check conditions Specification:
if ischar(S.conditions)
S.conditions = {S.conditions};
end
assert(any(strcmp(S.conditions,'all')) || isempty(setdiff(S.conditions,D.condlist)),'Not all requested conditions are present in the MEEG object');
% Check dirname Specification:
if isempty(S.dirname)
[~,tn] = fileparts(tempname(D.path));
S.dirname = fullfile(D.path,sprintf('osl_bf_temp_%s',tn(3:3+7)));
else
S.dirname = getfullpath(S.dirname);
end
if ~exist(S.dirname,'dir')
mkdir(S.dirname);
end
if isempty(S.mode);
S.mode='volumetric'
elseif any(ismember(S.mode,['cortical','Cortical','volumetric','Volumetric']))~=1;
error('Volumetric/cortical mode not recognised. Please check and try again');
end
fprintf(1,'BF working directory: %s\n',S.dirname);
%%%%%%%%%%%%%%%%%% R U N I N V E R S E M O D E L %%%%%%%%%%%%%%%%%%
clear matlabbatch
if (strcmp(S.mode,'cortical') || strcmp(S.mode,'Cortical'))~=1; % Stick with the boring volumetric method
fprintf('\nRUNNING A VOLUMETRIC SR\n'); % tell user
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{1}.spm.tools.beamforming.data.dir = {S.dirname};
matlabbatch{1}.spm.tools.beamforming.data.D = {fullfile(D.path,D.fname)};
matlabbatch{1}.spm.tools.beamforming.data.val = 1;
matlabbatch{1}.spm.tools.beamforming.data.gradsource = 'inv';
matlabbatch{1}.spm.tools.beamforming.data.space = 'MNI-aligned';
matlabbatch{1}.spm.tools.beamforming.data.overwrite = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{2}.spm.tools.beamforming.sources.BF(1) = cfg_dep('Prepare data: BF.mat file', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
matlabbatch{2}.spm.tools.beamforming.sources.reduce_rank = [2 3];
matlabbatch{2}.spm.tools.beamforming.sources.keep3d = 1;
matlabbatch{2}.spm.tools.beamforming.sources.visualise = 1;
matlabbatch{2}.spm.tools.beamforming.sources.plugin.mni_coords.pos = double(mni_coords);
% Note that the "visualise" function won't work if the D object contain's
% an overlapping spheres head model due to the "vol.label" field being
% empty. This will crash on the call to
% ft_plot_vol(vol, 'edgecolor', [0 0 0], 'facealpha', 0);
% on line 137 of bf_sources.m
if strcmp(D.inv{1}.forward.voltype,'MEG Local Spheres') ==1 && matlabbatch{2}.spm.tools.beamforming.sources.visualise ==1;
warning('Sorry - we cannot visualise this head model with the the DAiSS toolbox. Please seek other visualisation method.');
matlabbatch{2}.spm.tools.beamforming.sources.visualise = 0;
end
% MESH STUFF!
%matlabbatch{2}.spm.tools.beamforming.sources.BF(1) = cfg_dep('Prepare data: BF.mat file', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
%matlabbatch{2}.spm.tools.beamforming.sources.reduce_rank = [2 3];
%matlabbatch{2}.spm.tools.beamforming.sources.keep3d = 1;
%matlabbatch{2}.spm.tools.beamforming.sources.visualise = 0;
%matlabbatch{2}.spm.tools.beamforming.sources.plugin.mesh.orient = 'Original';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{3}.spm.tools.beamforming.features.BF(1) = cfg_dep('Define sources: BF.mat file', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
if strcmp(S.conditions{1},'all')
matlabbatch{3}.spm.tools.beamforming.features.whatconditions.all = 1;
else
matlabbatch{3}.spm.tools.beamforming.features.whatconditions.condlabel = S.conditions;
end
if ~S.use_class_channel
if D.ntrials == 1
matlabbatch{3}.spm.tools.beamforming.features.plugin.contcov = struct([]);
else
matlabbatch{3}.spm.tools.beamforming.features.plugin.cov = struct([]);
end
else
matlabbatch{3}.spm.tools.beamforming.features.plugin.cov_bysamples = struct([]);
end
matlabbatch{3}.spm.tools.beamforming.features.woi = S.timespan*1000; % needs to be in msecs for bf_features
for jj=1:length(S.modalities),
matlabbatch{3}.spm.tools.beamforming.features.modality{jj} = S.modalities{jj};
end
matlabbatch{3}.spm.tools.beamforming.features.fuse = S.fuse;
matlabbatch{3}.spm.tools.beamforming.features.regularisation.manual.lambda = 0;
matlabbatch{3}.spm.tools.beamforming.features.bootstrap = false;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{4}.spm.tools.beamforming.inverse.BF(1) = cfg_dep('Define sources: BF.mat file', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
switch S.inverse_method,
case 'beamform'
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.pca_order = S.pca_order;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.type = S.type;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.bilateral = 0;
case 'beamform_bilateral'
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.pca_order = S.pca_order;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.type = S.type;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.bilateral = 1;
case 'mne_diag_datacov'
mne_lambda=1;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.noise_cov_type = 'diag_datacov';
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.lambda = mne_lambda;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.type = S.type;
case 'mne_eye'
mne_lambda=1;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.noise_cov_type = 'eye';
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.lambda = mne_lambda;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.type = S.type;
case 'mne_adaptive'
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.mne_adaptive.Noise = S.MNE.Noise;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.mne_adaptive.Options = S.MNE.Options;
otherwise
disp('Inversion method unknown!');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{5}.spm.tools.beamforming.output.BF(1) = cfg_dep('Inverse solution: BF.mat file', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
matlabbatch{5}.spm.tools.beamforming.output.plugin.montage_osl.normalise = 'both';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{6}.spm.tools.beamforming.write.BF(1) = cfg_dep('Output: BF.mat file', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
matlabbatch{6}.spm.tools.beamforming.write.plugin.spmeeg_osl.prefix = S.prefix;
obj = onCleanup(@() cd(old_dir)); % Restore the working directory
spm_jobman('run',matlabbatch)
else % Do a cortical SR
fprintf('\nRUNNING A CORTICAL SR\n');
warning('We have assumed that you have already downsampled your brain surface');
matlabbatch{1}.spm.tools.beamforming.data.dir = {S.dirname};
matlabbatch{1}.spm.tools.beamforming.data.D = {fullfile(D.path,D.fname)};
matlabbatch{1}.spm.tools.beamforming.data.val = 1;
matlabbatch{1}.spm.tools.beamforming.data.gradsource = 'inv';
matlabbatch{1}.spm.tools.beamforming.data.space = 'MNI-aligned';
matlabbatch{1}.spm.tools.beamforming.data.overwrite = 1;
matlabbatch{2}.spm.tools.beamforming.sources.BF(1) = cfg_dep('Prepare data: BF.mat file', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
matlabbatch{2}.spm.tools.beamforming.sources.reduce_rank = [2 3];
matlabbatch{2}.spm.tools.beamforming.sources.keep3d = 1;
matlabbatch{2}.spm.tools.beamforming.sources.plugin.mesh.orient = 'Original';
matlabbatch{2}.spm.tools.beamforming.sources.plugin.mesh.fdownsample = 1;
matlabbatch{2}.spm.tools.beamforming.sources.plugin.mesh.flip = false;
matlabbatch{2}.spm.tools.beamforming.sources.visualise = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{3}.spm.tools.beamforming.features.BF(1) = cfg_dep('Define sources: BF.mat file', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
if strcmp(S.conditions{1},'all')
matlabbatch{3}.spm.tools.beamforming.features.whatconditions.all = 1;
else
matlabbatch{3}.spm.tools.beamforming.features.whatconditions.condlabel = S.conditions;
end
if ~S.use_class_channel
if D.ntrials == 1
matlabbatch{3}.spm.tools.beamforming.features.plugin.contcov = struct([]);
else
matlabbatch{3}.spm.tools.beamforming.features.plugin.cov = struct([]);
end
else
matlabbatch{3}.spm.tools.beamforming.features.plugin.cov_bysamples = struct([]);
end
matlabbatch{3}.spm.tools.beamforming.features.woi = S.timespan*1000; % needs to be in msecs for bf_features
for jj=1:length(S.modalities),
matlabbatch{3}.spm.tools.beamforming.features.modality{jj} = S.modalities{jj};
end
matlabbatch{3}.spm.tools.beamforming.features.fuse = S.fuse;
matlabbatch{3}.spm.tools.beamforming.features.regularisation.manual.lambda = 0;
matlabbatch{3}.spm.tools.beamforming.features.bootstrap = false;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{4}.spm.tools.beamforming.inverse.BF(1) = cfg_dep('Define sources: BF.mat file', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
switch S.inverse_method,
case 'beamform'
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.pca_order = S.pca_order;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.type = S.type;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.bilateral = 0;
case 'beamform_bilateral'
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.pca_order = S.pca_order;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.type = S.type;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.lcmv_multicov.bilateral = 1;
case 'mne_diag_datacov'
mne_lambda=1;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.noise_cov_type = 'diag_datacov';
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.lambda = mne_lambda;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.type = S.type;
case 'mne_eye'
mne_lambda=1;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.noise_cov_type = 'eye';
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.lambda = mne_lambda;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.minimumnorm.type = S.type;
case 'mne_adaptive'
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.mne_adaptive.Noise = S.MNE.Noise;
matlabbatch{4}.spm.tools.beamforming.inverse.plugin.mne_adaptive.Options = S.MNE.Options;
otherwise
disp('Inversion method unknown!');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{5}.spm.tools.beamforming.output.BF(1) = cfg_dep('Inverse solution: BF.mat file', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
matlabbatch{5}.spm.tools.beamforming.output.plugin.montage_osl.normalise = 'both';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
matlabbatch{6}.spm.tools.beamforming.write.BF(1) = cfg_dep('Output: BF.mat file', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','BF'));
matlabbatch{6}.spm.tools.beamforming.write.plugin.spmeeg_osl.prefix = S.prefix;
obj = onCleanup(@() cd(old_dir)); % Restore the working directory
spm_jobman('run',matlabbatch)
end
if ~isempty(S.prefix)
D = spm_eeg_load(fullfile(D.path,[S.prefix D.fname]))
else
D = spm_eeg_load(D.fullfile); % Load the file back from disk with the new online montages
end