-
Notifications
You must be signed in to change notification settings - Fork 722
/
ft_denoise_pca.m
558 lines (466 loc) · 18 KB
/
ft_denoise_pca.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
function data = ft_denoise_pca(cfg, varargin)
% FT_DENOISE_PCA performs a principal component analysis (PCA) on specified reference
% channels and subtracts the projection of the data of interest onto this orthogonal
% basis from the data of interest. This is the algorithm which is applied by 4D to
% compute noise cancellation weights on a dataset of interest. This function has been
% designed for 4D MEG data, but can also be applied to data from other MEG systems.
%
% Use as
% [dataout] = ft_denoise_pca(cfg, data)
% or as
% [dataout] = ft_denoise_pca(cfg, data, refdata)
% where "data" is a raw data structure that was obtained with FT_PREPROCESSING. If
% you specify the additional input "refdata", the specified reference channels for
% the regression will be taken from this second data structure. This can be useful
% when reference-channel specific preprocessing needs to be done (e.g. low-pass
% filtering).
%
% The output structure dataout contains the denoised data in a format that is
% consistent with the output of FT_PREPROCESSING.
%
% The configuration should contain
% cfg.refchannel = the channels used as reference signal (default = 'MEGREF')
% cfg.channel = the channels to be denoised (default = 'MEG')
% cfg.truncate = optional truncation of the singular value spectrum (default = 'no')
% cfg.zscore = standardise reference data prior to PCA (default = 'no')
% cfg.pertrial = 'no' (default) or 'yes'. Regress out the references on a per trial basis
% cfg.trials = list of trials that are used (default = 'all')
% cfg.updatesens = 'no' or 'yes' (default = 'yes')
%
% if cfg.truncate is integer n > 1, n will be the number of singular values kept.
% if 0 < cfg.truncate < 1, the singular value spectrum will be thresholded at the
% fraction cfg.truncate of the largest singular value.
%
% See also FT_PREPROCESSING, FT_DENOISE_SYNTHETIC, FT_DENOISE_SSP
% Undocumented cfg-option: cfg.pca the output structure of an earlier call
% to the function. Can be used regress out the reference channels from
% another data set.
% Copyright (c) 2008-2009, Jan-Mathijs Schoffelen, CCNi Glasgow
% Copyright (c) 2010-2022, Jan-Mathijs Schoffelen, DCCN Nijmegen
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
% these are used by the ft_preamble/ft_postamble function and scripts
ft_revision = '$Id$';
ft_nargin = nargin;
ft_nargout = nargout;
% do the general setup of the function
ft_defaults
ft_preamble init
ft_preamble debug
ft_preamble provenance varargin
% the ft_abort variable is set to true or false in ft_preamble_init
if ft_abort
return
end
% check if the input data is valid for this function
for i=1:length(varargin)
varargin{i} = ft_checkdata(varargin{i}, 'datatype', 'raw');
end
% check if the input cfg is valid for this function
cfg = ft_checkconfig(cfg, 'forbidden', {'channels', 'trial'}); % prevent accidental typos, see issue 1729
% set the defaults
cfg.refchannel = ft_getopt(cfg, 'refchannel', 'MEGREF');
cfg.channel = ft_getopt(cfg, 'channel', 'MEG');
cfg.truncate = ft_getopt(cfg, 'truncate', 'no');
cfg.zscore = ft_getopt(cfg, 'zscore', 'no');
cfg.trials = ft_getopt(cfg, 'trials', 'all', 1);
cfg.pertrial = ft_getopt(cfg, 'pertrial', 'no');
cfg.feedback = ft_getopt(cfg, 'feedback', 'none');
cfg.updatesens = ft_getopt(cfg, 'updatesens', 'yes');
if istrue(cfg.pertrial)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iterate over trials
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tmpcfg = keepfields(cfg, {'trials', 'showcallinfo', 'trackcallinfo', 'trackusage', 'trackdatainfo', 'trackmeminfo', 'tracktimeinfo', 'checksize'});
% select trials of interest
for i=1:numel(varargin)
varargin{i} = ft_selectdata(tmpcfg, varargin{i});
[dum, varargin{i}] = rollback_provenance(cfg, varargin{i});
if i==1
cfg = dum;
end
end
tmp = cell(numel(varargin{1}.trial),1);
tmpcfg = cfg;
tmpcfg.pertrial = 'no';
for k = 1:numel(varargin{1}.trial)
tmpcfg.trials = k; % select a single trial
tmp{k} = ft_denoise_pca(tmpcfg, varargin{:});
[dum, tmp{k}] = rollback_provenance(tmpcfg, tmp{k});
end
data = ft_appenddata([], tmp{:});
[cfg, data] = rollback_provenance(cfg, data);
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute it for the data concatenated over all trials
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
computeweights = ~isfield(cfg, 'pca');
% select trials of interest
tmpcfg = keepfields(cfg, {'trials', 'showcallinfo', 'trackcallinfo', 'trackusage', 'trackdatainfo', 'trackmeminfo', 'tracktimeinfo', 'checksize'});
if length(varargin)==1
% channel data and reference channel data are in 1 data structure
megchan = ft_channelselection(cfg.channel, varargin{1}.label);
refchan = ft_channelselection(cfg.refchannel, varargin{1}.label);
tmpcfg.channel = refchan;
refdata = ft_selectdata(tmpcfg, varargin{1});
[dum,refdata] = rollback_provenance(cfg, refdata);
tmpcfg.channel = megchan;
data = ft_selectdata(tmpcfg, varargin{1});
[cfg, data] = rollback_provenance(cfg, data);
else
% channel data and reference channel data are in 2 data structures
megchan = ft_channelselection(cfg.channel, varargin{1}.label);
refchan = ft_channelselection(cfg.refchannel, varargin{2}.label);
% throw a warning if some of the specified reference channels are also
% in the first data argument
if ~isempty(ft_channelselection(cfg.refchannel, varargin{1}.label))
ft_warning('some of the specified reference channels are also present in the first data argument, this information will not be used for the cleaning of the data');
end
tmpcfg.channel = refchan;
refdata = ft_selectdata(tmpcfg, varargin{2});
[dum, refdata] = rollback_provenance(cfg, refdata);
tmpcfg.channel = megchan;
data = ft_selectdata(tmpcfg, varargin{1});
[cfg, data] = rollback_provenance(cfg, data);
end
refchan = ft_channelselection(cfg.refchannel, refdata.label, ft_senstype(refdata));
refindx = match_str(refdata.label, refchan);
megchan = ft_channelselection(cfg.channel, data.label, ft_senstype(data));
megindx = match_str(data.label, megchan);
nref = length(refindx);
ntrl = length(data.trial);
if ischar(cfg.truncate) && strcmp(cfg.truncate, 'no')
cfg.truncate = length(refindx);
elseif ischar(cfg.truncate) || (cfg.truncate>1 && cfg.truncate/round(cfg.truncate)~=1) || cfg.truncate>length(refindx)
ft_error('cfg.truncate should be either ''no'', an integer number <= the number of references, or a number between 0 and 1');
% FIXME the default truncation applied by 4D is 1x10^-8
end
% compute and remove mean from data
fprintf('removing the mean from the channel data and reference channel data\n');
m = cellmean(data.trial, 2);
data.trial = cellvecadd(data.trial, -m);
m = cellmean(refdata.trial, 2);
refdata.trial = cellvecadd(refdata.trial, -m);
if computeweights
% zscore
if strcmp(cfg.zscore, 'yes')
fprintf('zscoring the reference channel data\n');
[refdata.trial, sdref] = cellzscore(refdata.trial, 2, 0); %forced demeaned already
else
sdref = ones(nref, 1);
end
% compute covariance of refchannels and do svd
fprintf('performing pca on the reference channel data\n');
crefdat = cellcov(refdata.trial, [], 2, 0);
[u,s,v] = svd(crefdat);
% determine the truncation and rotation
if cfg.truncate<1
% keep all singular vectors with singular values >= cfg.truncate*s(1,1)
s1 = s./max(s(:));
keep = find(diag(s1)>cfg.truncate);
else
keep = 1:cfg.truncate;
end
fprintf('keeping %d out of %d components\n',numel(keep),size(u,2));
rotmat = u(:, keep)';
% rotate the refdata
fprintf('projecting the reference data onto the pca-subspace\n');
refdata.trial = cellfun(@mtimes, repmat({rotmat}, 1, ntrl), refdata.trial, 'UniformOutput', 0);
% project megdata onto the orthogonal basis
fprintf('computing the regression weights\n');
nom = cellcov(data.trial, refdata.trial, 2, 0);
denom = cellcov(refdata.trial, [], 2, 0);
rw = (pinv(denom)*nom')';
% subtract projected data
fprintf('subtracting the reference channel data from the channel data\n');
for k = 1:ntrl
data.trial{k} = data.trial{k} - rw*refdata.trial{k};
end
% rotate back and 'unscale'
pca.w = rw*rotmat*diag(1./sdref);
pca.label = data.label;
pca.reflabel = refdata.label;
pca.rotmat = rotmat;
cfg.pca = pca;
else
fprintf('applying precomputed weights to the data\n');
% check whether the weight table contains the specified references
% ensure the ordering of the meg-data to be consistent with the weights
% ensure the ordering of the ref-data to be consistent with the weights
[i1,i2] = match_str(refchan, cfg.pca.reflabel);
[i3,i4] = match_str(megchan, cfg.pca.label);
if length(i2)~=length(cfg.pca.reflabel)
ft_error('you specified fewer references to use as there are in the precomputed weight table');
end
refindx = refindx(i1);
megindx = megindx(i3);
cfg.pca.w = cfg.pca.w(i4,i2);
cfg.pca.label = cfg.pca.label(i4);
cfg.pca.reflabel= cfg.pca.reflabel(i2);
if isfield(cfg.pca, 'rotmat')
cfg.pca = rmfield(cfg.pca, 'rotmat'); % dont know
end
for k = 1:ntrl
data.trial{k} = data.trial{k} - cfg.pca.w*refdata.trial{k};
end
pca = cfg.pca;
end
% demean FIXME is this needed
m = cellmean(data.trial, 2);
data.trial = cellvecadd(data.trial, -m);
if isfield(data, 'grad')
sensfield = 'grad';
elseif isfield(data, 'elec')
sensfield = 'elec';
elseif isfield(data, 'opto')
sensfield = 'opto';
else
sensfield = [];
end
% apply the linear projection also to the sensor description
if ~isempty(sensfield)
if strcmp(cfg.updatesens, 'yes')
fprintf('also applying the weights to the %s structure\n', sensfield);
montage = [];
labelnew = pca.label;
% add columns of refchannels not yet present in labelnew
% [id, i1] = setdiff(pca.reflabel, labelnew);
% labelold = [labelnew; pca.reflabel(sort(i1))];
labelold = data.grad.label;
nlabelold = length(labelold);
% start with identity
montage.tra = eye(nlabelold);
% subtract weights
[i1, i2] = match_str(labelold, pca.reflabel);
[i3, i4] = match_str(labelold, pca.label);
montage.tra(i3,i1) = montage.tra(i3,i1) - pca.w(i4,i2);
montage.labelold = labelold;
montage.labelnew = labelold;
data.(sensfield) = ft_apply_montage(data.(sensfield), montage, 'keepunused', 'yes', 'balancename', 'pca');
% order the fields
fnames = fieldnames(data.(sensfield).balance);
tmp = false(1,numel(fnames));
for k = 1:numel(fnames)
tmp(k) = isstruct(data.(sensfield).balance.(fnames{k}));
end
[tmp, ix] = sort(tmp, 'descend');
data.grad.balance = orderfields(data.(sensfield).balance, fnames(ix));
else
fprintf('not applying the weights to the %s structure\n', sensfield);
end
end % if sensfield
end % if pertrial
% do the general cleanup and bookkeeping at the end of the function
ft_postamble debug
ft_postamble previous varargin
ft_postamble provenance data
ft_postamble history data
ft_postamble savevar data
%%%%%%%%%%%%%%%%%
% SUBFUNCTIONS
%%%%%%%%%%%%%%%%%
%-----cellcov
function [c] = cellcov(x, y, dim, flag)
% [C] = CELLCOV(X, DIM) computes the covariance, across all cells in x along
% the dimension dim. When there are three inputs, covariance is computed between
% all cells in x and y
%
% X (and Y) should be linear cell-array(s) of matrices for which the size in at
% least one of the dimensions should be the same for all cells
if nargin==2
flag = 1;
dim = y;
y = [];
elseif nargin==3
flag = 1;
end
nx = size(x);
if ~iscell(x) || length(nx)>2 || all(nx>1)
ft_error('incorrect input for cellmean');
end
if nargin==1
scx1 = cellfun('size', x, 1);
scx2 = cellfun('size', x, 2);
if all(scx2==scx2(1)), dim = 2; %let second dimension prevail
elseif all(scx1==scx1(1)), dim = 1;
else ft_error('no dimension to compute covariance for');
end
end
if flag
mx = cellmean(x, 2);
x = cellvecadd(x, -mx);
if ~isempty(y)
my = cellmean(y, 2);
y = cellvecadd(y, -my);
end
end
nx = max(nx);
nsmp = cellfun('size', x, dim);
if isempty(y)
csmp = cellfun(@covc, x, repmat({dim},1,nx), 'UniformOutput', 0);
else
csmp = cellfun(@covc, x, y, repmat({dim},1,nx), 'UniformOutput', 0);
end
nc = size(csmp{1});
c = sum(reshape(cell2mat(csmp), [nc(1) nc(2) nx]), 3)./sum(nsmp);
function [c] = covc(x, y, dim)
if nargin==2
dim = y;
y = x;
end
if dim==1
c = x'*y;
elseif dim==2
c = x*y';
end
%-----cellmean
function [m] = cellmean(x, dim)
% [M] = CELLMEAN(X, DIM) computes the mean, across all cells in x along
% the dimension dim.
%
% X should be an linear cell-array of matrices for which the size in at
% least one of the dimensions should be the same for all cells
nx = size(x);
if ~iscell(x) || length(nx)>2 || all(nx>1)
ft_error('incorrect input for cellmean');
end
if nargin==1
scx1 = cellfun('size', x, 1);
scx2 = cellfun('size', x, 2);
if all(scx2==scx2(1)), dim = 2; %let second dimension prevail
elseif all(scx1==scx1(1)), dim = 1;
else ft_error('no dimension to compute mean for');
end
end
nx = max(nx);
nsmp = cellfun('size', x, dim);
ssmp = cellfun(@sum, x, repmat({dim},1,nx), 'UniformOutput', 0);
m = sum(cell2mat(ssmp), dim)./sum(nsmp);
%-----cellstd
function [sd] = cellstd(x, dim, flag)
% [M] = CELLSTD(X, DIM, FLAG) computes the standard deviation, across all cells in x along
% the dimension dim, normalising by the total number of samples
%
% X should be an linear cell-array of matrices for which the size in at
% least one of the dimensions should be the same for all cells. If flag==1, the mean will
% be subtracted first (default behavior, but to save time on already demeaned data, it
% can be set to 0).
nx = size(x);
if ~iscell(x) || length(nx)>2 || all(nx>1)
ft_error('incorrect input for cellstd');
end
if nargin<2
scx1 = cellfun('size', x, 1);
scx2 = cellfun('size', x, 2);
if all(scx2==scx2(1)), dim = 2; %let second dimension prevail
elseif all(scx1==scx1(1)), dim = 1;
else ft_error('no dimension to compute mean for');
end
elseif nargin==2
flag = 1;
end
if flag
m = cellmean(x, dim);
x = cellvecadd(x, -m);
end
nx = max(nx);
nsmp = cellfun('size', x, dim);
ssmp = cellfun(@sumsq, x, repmat({dim},1,nx), 'UniformOutput', 0);
sd = sqrt(sum(cell2mat(ssmp), dim)./sum(nsmp));
function [s] = sumsq(x, dim)
s = sum(x.^2, dim);
%-----cellvecadd
function [y] = cellvecadd(x, v)
% [Y]= CELLVECADD(X, V) - add vector to all rows or columns of each matrix
% in cell-array X
% check once and for all to save time
persistent bsxfun_exists;
if isempty(bsxfun_exists)
bsxfun_exists=exist('bsxfun', 'builtin');
if ~bsxfun_exists
ft_error('bsxfun not found.');
end
end
nx = size(x);
if ~iscell(x) || length(nx)>2 || all(nx>1)
ft_error('incorrect input for cellmean');
end
if ~iscell(v)
v = repmat({v}, nx);
end
y = cellfun(@bsxfun, repmat({@plus}, nx), x, v, 'UniformOutput', 0);
%-----cellvecmult
function [y] = cellvecmult(x, v)
% [Y]= CELLVECMULT(X, V) - multiply vectors in cell-array V
% to all rows or columns of each matrix in cell-array X
% V can be a vector or a cell-array of vectors
% check once and for all to save time
persistent bsxfun_exists;
if isempty(bsxfun_exists)
bsxfun_exists=exist('bsxfun', 'builtin');
if ~bsxfun_exists
ft_error('bsxfun not found.');
end
end
nx = size(x);
if ~iscell(x) || length(nx)>2 || all(nx>1)
ft_error('incorrect input for cellmean');
end
if ~iscell(v)
v = repmat({v}, nx);
end
sx1 = cellfun('size', x, 1);
sx2 = cellfun('size', x, 2);
sv1 = cellfun('size', v, 1);
sv2 = cellfun('size', v, 2);
if all(sx1==sv1) && all(sv2==1)
elseif all(sx2==sv2) && all(sv1==1)
elseif all(sv1==1) && all(sv2==1)
else ft_error('inconsistent input');
end
y = cellfun(@bsxfun, repmat({@times}, nx), x, v, 'UniformOutput', 0);
%-----cellzscore
function [z, sd, m] = cellzscore(x, dim, flag)
% [Z, SD] = CELLZSCORE(X, DIM, FLAG) computes the zscore, across all cells in x along
% the dimension dim, normalising by the total number of samples
%
% X should be an linear cell-array of matrices for which the size in at
% least one of the dimensions should be the same for all cells. If flag==1, the mean will
% be subtracted first (default behavior, but to save time on already demeaned data, it
% can be set to 0). SD is a vector containing the standard deviations, used for the normalisation.
nx = size(x);
if ~iscell(x) || length(nx)>2 || all(nx>1)
ft_error('incorrect input for cellstd');
end
if nargin<2
scx1 = cellfun('size', x, 1);
scx2 = cellfun('size', x, 2);
if all(scx2==scx2(1)), dim = 2; % let second dimension prevail
elseif all(scx1==scx1(1)), dim = 1;
else ft_error('no dimension to compute mean for');
end
elseif nargin==2
flag = 1;
end
if flag
m = cellmean(x, dim);
x = cellvecadd(x, -m);
end
sd = cellstd(x, dim, 0);
z = cellvecmult(x, 1./sd);