-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbf_output_sourcedata_robust.m
153 lines (117 loc) · 4.76 KB
/
bf_output_sourcedata_robust.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
function sourcedata_robust = bf_output_sourcedata_robust(BF, S)
% Extracts source data, handling bad data segments
% Copyright (C) 2013 Wellcome Trust Centre for Neuroimaging
% Vladimir Litvak
% $Id: bf_output_sourcedata_robust.m 89 2013-11-14 12:34:26Z [email protected] $
%--------------------------------------------------------------------------
if nargin == 0
method = cfg_menu;
method.tag = 'method';
method.name = 'Summary method';
method.labels = {'max', 'svd', 'keep'};
method.val = {'max'};
method.values = {'max', 'svd', 'keep'};
method.help = {'How to summarise sources in the ROI'};
sourcedata_robust = cfg_branch;
sourcedata_robust.tag = 'sourcedata_robust';
sourcedata_robust.name = 'Source data (robust)';
sourcedata_robust.val = {method};
return
elseif nargin < 2
error('Two input arguments are required');
end
D = BF.data.D;
modalities = intersect(fieldnames(BF.features), {'EEG', 'MEG', 'MEGPLANAR'});
samples = 1:D.nsamples;
for m = 1:numel(modalities)
U = BF.features.(modalities{m}).U;
if any(any(U-eye(size(U, 1))))
error('Projection to modes is not supported by this plug-in');
end
chanind = D.indchannel(BF.inverse.(modalities{m}).channels);
bad = badsamples(D, chanind, samples, 1);
chngpnt = [1 find(any(diff(bad, [], 2)))];
nsegments = length(chngpnt);
id = zeros(1, length(samples));
L = {};
ev = [];
for i = 1:nsegments
goodind = ~bad(:, chngpnt(i));
if i<nsegments
id(chngpnt(i):(chngpnt(i+1)-1)) = spm_data_id(chanind(goodind));
else
id(chngpnt(i):end) = spm_data_id(chanind(goodind));
end
ev(i).type = 'artefact_filterchange';
ev(i).value = id(chngpnt(i));
ev(i).time = D.time(samples(chngpnt(i))) - D.time(1) + D.trialonset(1);
end
uid = unique(id);
for i = 1:length(uid)
id(id == uid(i)) = i;
uid(i) = i;
end
ftdata = [];
lbl = {};
if isfield(BF.sources, 'voi')
ftdata.label = BF.sources.voi.label(:);
for v = 1:numel(ftdata.label)
ind = find(BF.sources.voi.pos2voi == v);
W = cat(1, BF.inverse.(modalities{m}).W{ind});
L{v} = cat(2, BF.inverse.(modalities{m}).L{ind});
switch S.method
case 'max'
Wc = W* BF.features.(modalities{m}).C*W'; % bf estimated source covariance matrix
[dum, mi] = max(diag(Wc));
L{v} = L{v}(:, mi);
case 'keep'
for i = 1:size(L{v}, 2)
lbl{end+1, 1} = [ftdata.label{v} '_' num2str(i)];
end
end
end
else
mnipos = spm_eeg_inv_transform_points(BF.data.transforms.toMNI, BF.sources.pos);
for i = 1:size(mnipos, 1)
w = BF.inverse.(modalities{m}).W{i};
if ~isnan(w)
ftdata.label{i} = sprintf('%.2f_%.2f_%.2f', mnipos(i, :));
L{i} = BF.inverse.(modalities{m}).L{i};
end
end
ftdata.label = ftdata.label(:);
end
if isempty(lbl)
lbl = ftdata.label;
end
data = nan(length(lbl), length(samples));
C = BF.features.(modalities{m}).C;
spm('Pointer', 'Watch');drawnow;
spm_progress_bar('Init', length(uid), ['Projecting ' modalities{m} ' data']); drawnow;
if length(uid) > 100, Ibar = floor(linspace(1, length(uid),100));
else Ibar = 1:length(uid); end
for i = 1:length(uid)
goodind = ~bad(:, find(id==uid(i), 1, 'first'));
Cy = C(goodind, goodind);
invCy = pinv_plus(Cy);
n = 1;
for j = 1:length(ftdata.label)
w = [];
for k = 1:size(L{j}, 2)
lf = L{j}(goodind, k);
w = [w; lf'*invCy/(lf' * invCy * lf)];
end
data(n:(n+size(w, 1)-1), id==uid(i)) = w*D(chanind(goodind), find(id==uid(i)));
n = n+size(w, 1);
end
if ismember(i, Ibar)
spm_progress_bar('Set', i); drawnow;
end
end
spm_progress_bar('Clear');
ftdata.trial{1} = data;
ftdata.time{1} = D.time(samples);
ftdata.label = lbl;
sourcedata_robust.(modalities{m}).ftdata = ftdata;
sourcedata_robust.(modalities{m}).events = ev;
end