-
Notifications
You must be signed in to change notification settings - Fork 7
/
bf_features.m
346 lines (283 loc) · 11.2 KB
/
bf_features.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
function out = bf_features
% Prepares data features for filter computation
% Copyright (C) 2012 Wellcome Trust Centre for Neuroimaging
% Vladimir Litvak
% $Id$
% dir Directory
% ---------------------------------------------------------------------
BF = cfg_files;
BF.tag = 'BF';
BF.name = 'BF.mat file';
BF.filter = '^BF.mat$';
BF.num = [1 1];
BF.help = {'Select BF.mat file.'};
all = cfg_const;
all.tag = 'all';
all.name = 'All';
all.val = {1};
condlabel = cfg_entry;
condlabel.tag = 'condlabel';
condlabel.name = 'Condition label';
condlabel.strtype = 's';
condlabel.val = {''};
conditions = cfg_repeat;
conditions.tag = 'conditions';
conditions.name = 'Conditions';
conditions.help = {'Specify the labels of the conditions to be included in the inversion'};
conditions.num = [1 Inf];
conditions.values = {condlabel};
conditions.val = {condlabel};
whatconditions = cfg_choice;
whatconditions.tag = 'whatconditions';
whatconditions.name = 'What conditions to include?';
whatconditions.values = {all, conditions};
whatconditions.val = {all};
woi = cfg_entry;
woi.tag = 'woi';
woi.name = 'Time windows of interest';
woi.strtype = 'r';
woi.num = [Inf 2];
woi.val = {[-Inf Inf]};
woi.help = {'Time windows to average over (ms)'};
modality = cfg_menu;
modality.tag = 'modality';
modality.name = 'Select modalities';
modality.help = {'Select modalities for the inversion (only relevant for multimodal datasets).'};
modality.labels = {'All', 'EEG', 'MEG', 'MEGPLANAR', 'EEG+MEG', 'MEG+MEGPLANAR', 'EEG+MEGPLANAR'};
modality.values = {
{'EEG', 'MEG', 'MEGPLANAR'}
{'EEG'}
{'MEG'}
{'MEGPLANAR'}
{'EEG', 'MEG'}
{'MEG', 'MEGPLANAR'}
{'EEG', 'MEGPLANAR'}
}';
modality.val = {{'MEG'}};
fuse = cfg_menu;
fuse.tag = 'fuse';
fuse.name = 'Fuse modalities';
fuse.help = {'Fuse sensors for different modalities together (requires prior rescaling).'};
fuse.labels = {'Don''t fuse' 'Fuse MEG only', 'Fuse all'};
fuse.values = {'no', 'meg', 'all'};
fuse.val = {'no'};
cross_terms = cfg_menu;
cross_terms.tag = 'cross_terms';
cross_terms.name = 'Zero cross terms';
cross_terms.help = {'When fusing, set cross-terms between modalities to zero'};
cross_terms.labels = {'MEG to EEG only' 'MEG, MEGPLANAR, EEG', 'No'};
cross_terms.values = {'megeeg', 'all', 'no'};
cross_terms.val = {'megeeg'};
%--------------------------------------------------------------------------
% method
%--------------------------------------------------------------------------
plugin = cfg_choice;
plugin.tag = 'plugin';
plugin.name = 'Covariance computation method';
feature_funs = spm_select('List', fileparts(mfilename('fullpath')), '^bf_features_.*\.m$');
feature_funs = cellstr(feature_funs );
for i = 1:numel(feature_funs)
plugin.values{i} = feval(spm_file(feature_funs{i},'basename'));
end
%--------------------------------------------------------------------------
% regularisation/reduction
%--------------------------------------------------------------------------
reg = cfg_choice;
reg.tag = 'regularisation';
reg.name = 'Regularisation method';
reg_funs = spm_select('List', fileparts(mfilename('fullpath')), '^bf_regularise_.*\.m$');
reg_funs = cellstr(reg_funs );
for i = 1:numel(reg_funs)
reg.values{i} = feval(spm_file(reg_funs{i},'basename'));
end
bootstrap = cfg_menu;
bootstrap.tag = 'bootstrap';
bootstrap.name = 'Bootstrap';
bootstrap.labels = {'yes', 'no'};
bootstrap.values = {true, false};
bootstrap.val = {false};
visualise = cfg_menu;
visualise.tag = 'visualise';
visualise.name = 'Visualise eigen-spectrum';
visualise.help = {'Visualise covariance log eigen-spectrum to check for effective data dimensionality.'};
visualise.labels = {'yes', 'no'};
visualise.values = {1, 0};
visualise.val = {1};
out = cfg_exbranch;
out.tag = 'features';
out.name = 'Covariance features';
out.val = {BF, whatconditions, woi, modality, fuse, cross_terms, plugin, reg, bootstrap, visualise};
out.help = {'Define features for covariance computation'};
out.prog = @bf_features_run;
out.vout = @bf_features_vout;
out.modality = {'EEG'};
end
function out = bf_features_run(job)
outdir = spm_file(job.BF{1}, 'fpath');
% cd(outdir);
BF = bf_load(fullfile(outdir, 'BF.mat'));
D = BF.data.D;
plugin_name = cell2mat(fieldnames(job.plugin));
S = job.plugin.(plugin_name);
%%%%%%%%%%%%
% MWW 19/11/2014
classchanind=[];
try
classchanind=find(strcmp(D.chanlabels,'Class')); % MWW 19/11/2014
catch
end;
if isempty(classchanind)
%%%%%%%%%%%%
S(1).samples = {};
for i = 1:size(job.woi, 1)
S.samples{i} = D.indsample(1e-3*job.woi(i, 1)):D.indsample(1e-3*job.woi(i, 2));
if isnan(S.samples{i})
error('Window specified not in dataset');
end;
end
%%%%%%%%%%%%
% MWW 19/11/2014
else
try
classchanind=find(strcmp(D.chanlabels,'Class')); % MWW 19/11/2014
catch
error('There must be a Class channel in D if job.woi is not specfied');
end
end
%%%%%%%%%%%%
if isfield(job.whatconditions, 'all')
S(1).trials = D.indtrial(D.condlist, 'GOOD');
else
S(1).trials = D.indtrial(job.whatconditions.condlabel, 'GOOD');
if isempty(S.trials)
error('No trials matched the selection, check the specified condition labels');
end
end
if job.bootstrap
S.trials = S.trials(ceil(rand(1, length(S.trials)).*length(S.trials)));
end
reg_name = cell2mat(fieldnames(job.regularisation));
S1 = job.regularisation.(reg_name);
switch job.fuse
case 'no'
modalities = job.modality;
case 'meg'
modalities{1} = intersect(job.modality, {'MEG', 'MEGMAG', 'MEGPLANAR'});%% added MEGMAG
modalities = [modalities intersect(job.modality, {'EEG'})];
case 'all'
modalities{1} = job.modality;
end
for m = 1:numel(modalities)
if ~isa(modalities{m}, 'cell')
cmod = modalities(m);
else
cmod = modalities{m};
end
eegind = strmatch('EEG', cmod, 'exact');
megind = [strmatch('MEG', cmod, 'exact') strmatch('MEGMAG', cmod, 'exact')];
planarind = strmatch('MEGPLANAR', cmod, 'exact');
chanind = cell(1, numel(cmod));
chanind_cov = cell(1, numel(cmod));
for n = 1:numel(cmod)
chanind{n} = indchantype(BF.data.D, cmod{n}, 'GOOD');
chanind_cov{n} = 1:length(chanind{n});
if n>1
chanind_cov{n} = chanind_cov{n} + length([chanind{1:(n-1)}]);
end
end
S.channels=[chanind{:}];
if isempty(S.channels)
if ~isa(modalities{m}, 'cell')
error('No good %s channels were found.\n', cmod{:});
end
end
if isequal(char(modalities{m}), 'EEG')
modality_name = 'EEG';
elseif isequal(char(modalities{m}), 'MEGPLANAR')
modality_name = 'MEGPLANAR';
elseif isequal(char(modalities{m}), 'MEGMAG')
modality_name = 'MEGMAG';
else
modality_name = 'MEG';
end
%%%%%%%%%%%%
% MWW 19/11/2014
if isempty(classchanind)
%%%%%%%%%%%%
BF.features.(modality_name) = feval(['bf_features_' plugin_name], BF, S);
if numel(cmod)>1
if isequal(job.cross_terms, 'all') && ~isempty(megind) && ~isempty(planarind)
BF.features.(modality_name).C(chanind_cov{megind}, chanind_cov{planarind}) = 0;
BF.features.(modality_name).C(chanind_cov{planarind}, chanind_cov{megind}) = 0;
end
if isequal(job.cross_terms, 'megeeg') && ~isempty(megind) && ~isempty(eegind)
BF.features.(modality_name).C(chanind_cov{megind}, chanind_cov{eegind}) = 0;
BF.features.(modality_name).C(chanind_cov{eegind}, chanind_cov{megind}) = 0;
end
if isequal(job.cross_terms, 'megeeg') && ~isempty(planarind) && ~isempty(eegind)
BF.features.(modality_name).C(chanind_cov{planarind}, chanind_cov{eegind}) = 0;
BF.features.(modality_name).C(chanind_cov{eegind}, chanind_cov{planarind}) = 0;
end
end
if job.visualise
F = spm_figure('GetWin', ['Log-eigenspectrum for ' modality_name]);clf;
[~, SS, ~] = svd(BF.features.(modality_name).C);
semilogy(diag(SS), '-o');
xlabel('Singular value index');
ylabel('Singular value');
end
S1.modality = modality_name;
S1.chanind = S.channels;
BF.features.(modality_name) = feval(['bf_regularise_' reg_name], BF, S1);
%%%%%%%%%%%%
% MWW 19/11/2014
% added to allow S.samples to be specified via a "Class" channel, which
% specifies which time points correspond to each class. This is so that
% class-specific features can be calculated separately using just the
% timepoints for each class. This can then be used later for doing
% source reconstruction specific to each class.
% For example, the class specific features (covariances) can be used
% by bf_features_cov_bysamples and bf_inverse_lcmv_multicov
else
disp('Ignoring job.woi. Using Class channel in D object to determine the time samples to use');
NK=max(squash(D(classchanind,:,:)));
for ii=1:NK
S.samples = (D(classchanind,:,:)==ii);
BF.data.samples.(modality_name).class{ii}=S.samples;
BF.features.(modality_name).class{ii} = feval(['bf_features_' plugin_name], BF, S);
if numel(cmod)>1
if isequal(job.cross_terms, 'all') && ~isempty(megind) && ~isempty(planarind)
BF.features.(modality_name).class{ii}.C(chanind_cov{megind}, chanind_cov{planarind}) = 0;
BF.features.(modality_name).class{ii}.C(chanind_cov{planarind}, chanind_cov{megind}) = 0;
end
if isequal(job.cross_terms, 'megeeg') && ~isempty(megind) && ~isempty(eegind)
BF.features.(modality_name).class{ii}.C(chanind_cov{megind}, chanind_cov{eegind}) = 0;
BF.features.(modality_name).class{ii}.C(chanind_cov{eegind}, chanind_cov{megind}) = 0;
end
if isequal(job.cross_terms, 'megeeg') && ~isempty(planarind) && ~isempty(eegind)
BF.features.(modality_name).class{ii}.C(chanind_cov{planarind}, chanind_cov{eegind}) = 0;
BF.features.(modality_name).class{ii}.C(chanind_cov{eegind}, chanind_cov{planarind}) = 0;
end
end
S1.chanind = S.channels;
S1.modality=modality_name;
S1.class=ii;
BF.features.(modality_name).class{ii} = feval(['bf_regularise_' reg_name], BF, S1);
end
end
%%%%%%%%%%%%
BF.features.(modality_name).chanind = S.channels;
end
BF.features.trials = S.trials;
bf_save_path(BF,fullfile(outdir, 'BF.mat'));
out.BF{1} = fullfile(outdir, 'BF.mat');
end
function dep = bf_features_vout(job)
% Output is always in field "D", no matter how job is structured
dep = cfg_dep;
dep.sname = 'BF.mat file';
% reference field "B" from output
dep.src_output = substruct('.','BF');
% this can be entered into any evaluated input
dep.tgt_spec = cfg_findspec({{'filter','mat'}});
end