-
Notifications
You must be signed in to change notification settings - Fork 7
/
bf_output_PLI.m
337 lines (287 loc) · 12.3 KB
/
bf_output_PLI.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
function mont = bf_output_PLI(BF, S)
% Generates a montage for source extraction, projects the sources one by
% one and computes phase lag index on the fly, which is written out with no
% need to call bf_write. Only one VOI allowed, which can be a sphere or a
% mask image.
% Copyright (C) 2015-2017 Wellcome Trust Centre for Neuroimaging & University of
% Zurich
% Dominik R Bach
% PLI computation using code published by Gerald Cooray (2010) Karolinska
% Institutet: EFFECT OF DIABETES MELLITUS ON HUMAN BRAIN FUNCTION
% (https://openarchive.ki.se/xmlui/bitstream/handle/10616/40241/ram_ber%C3%A4ttelse.pdf?sequence=1)
% with a method based on Stam CJ, Nolte G, Daffertshofer A (Hum Brain Mapp 2007)
%
% $Id: $
%--------------------------------------------------------------------------
if nargin == 0
label = cfg_entry;
label.tag = 'label';
label.name = 'Label';
label.strtype = 's';
label.help = {'Label for the reference VOI'};
pos = cfg_entry;
pos.tag = 'pos';
pos.name = 'MNI coordinates';
pos.strtype = 'r';
pos.num = [1 3];
pos.help = {'Locations for the reference VOI in MNI coordinates'};
pos.val = {};
radius = cfg_entry;
radius.tag = 'radius';
radius.name = 'Radius';
radius.strtype = 'r';
radius.num = [1 1];
radius.val = {0};
radius.help = {'Radius (in mm) for the reference VOIs (leave 0 for single point)'};
voidef = cfg_branch;
voidef.tag = 'voidef';
voidef.name = 'VOI';
voidef.val = {label, pos, radius};
mask = cfg_files;
mask.tag = 'mask';
mask.name = 'MNI mask';
mask.filter = 'image';
mask.ufilter = '.*';
mask.num = [1 1];
mask.help = {'Select a reference mask image'};
maskdef = cfg_branch;
maskdef.tag = 'maskdef';
maskdef.name = 'Mask VOI';
maskdef.val = {label, mask};
sourcefile = cfg_files;
sourcefile.tag = 'sourcefile';
sourcefile.name = 'Source file';
sourcefile.filter = 'mat';
sourcefile.num = [1 1];
sourcefile.help = {'Select the M/EEG data file'};
sourcefiledef = cfg_branch;
sourcefiledef.tag = 'sourcefiledef';
sourcefiledef.name = 'Source file';
sourcefiledef.val = {label, sourcefile};
sourcefiledef.help = {'Select an M/EEG file with channels corresponding to reference sources. Otherwise this file must exactly correspond to the file on which the BF was computed'};
vois = cfg_repeat;
vois.tag = 'vois';
vois.name = 'Redefine reference VOIs';
vois.num = [1 1];
vois.values = {voidef, maskdef, sourcefiledef};
vois.val = {};
vois.help = {'Select the source VOI from which you would like to compute the PLI'};
highpass = cfg_entry;
highpass.tag = 'highpass';
highpass.strtype = 'r';
highpass.name = 'Highpass cutoff';
highpass.num = [];
highpass.val = {0};
highpass.help = {'Highpass filter cutoff (4th order butterworth)'};
lowpass = cfg_entry;
lowpass.tag = 'lowpass';
lowpass.name = 'Lowpass cutoff';
lowpass.strtype = 'r';
lowpass.num = [];
lowpass.val = {300};
lowpass.help = {'Lowpass filter cutoff (4th order butterworth)'};
filtermethod = cfg_branch;
filtermethod.tag = 'filtermethod';
filtermethod.name = 'Filter reconstructed sources';
filtermethod.val = {highpass, lowpass};
method = cfg_menu;
method.tag = 'method';
method.name = 'Summary method';
method.labels = {'max', 'svd', 'keep'};
method.val = {'svd'};
method.values = {'max', 'svd', 'keep'};
method.help = {'How to summarise sources in the reference ROI'};
mont = cfg_branch;
mont.tag = 'PLI';
mont.name = 'Phase Lag Index';
mont.val = {method, filtermethod, vois};
return
elseif nargin < 2
error('Two input arguments are required');
end
D = BF.data.D;
modalities = intersect(fieldnames(BF.features), {'EEG', 'MEG', 'MEGPLANAR'});
for m = 1:numel(modalities)
U = BF.features.(modalities{m}).U;
montage = [];
montage.labelorg = BF.inverse.(modalities{m}).channels;
montage.labelorg = montage.labelorg(:);
montage.tra = [];
chantypenew = 'LFP';
v = 1; % only one VOI allowed
if isfield(BF.inverse.(modalities{m}), 'label')
error('Don''t know what to do. This function is supposed to work with source grids/meshes.');
elseif isfield(BF.sources, 'voi') || (isfield(S, 'vois') && numel(S.vois)>0)
if isfield(BF.sources, 'voi')
montage.labelnew = BF.sources.voi.label;
elseif isfield(S, 'vois')
% collect labels from voi and maks definitions
if isfield(S.vois{1}, 'voidef')
montage.labelnew{1} = S.vois{v}.voidef.label;
elseif isfield(S.vois{1}, 'maskdef')
montage.labelnew{1} = S.vois{v}.maskdef.label;
elseif isfield(S.vois{1}, 'sourcefiledef')
montage.labelnew{1} = S.vois{1}.sourcefiledef.label;
E = spm_eeg_load(S.vois{1}.sourcefiledef.sourcefile{1});
if numel(E(1, :, :)) ~= numel(D(1, :, :))
error('Reference source and BF source files have different dimensions');
else
C1 = conditions(D); C2 = conditions(E);
for trl = 1:ntrials(D)
if ~strcmpi(C1{trl}, C2{trl})
error('Trial labels don''t match between reference source and BF source');
end;
end;
end;
end;
mnipos = spm_eeg_inv_transform_points(BF.data.transforms.toMNI, BF.sources.pos);
else
error('Don''t know what to do.');
end
lbl = {};
if isfield(BF.sources, 'voi')
ind = find(BF.sources.voi.pos2voi == v);
elseif isfield(S.vois{v}, 'maskdef') % adapted from bf_sources_voi, v115
% find sources within mask
V = spm_vol(char(S.vois{v}.maskdef.mask));
vox = spm_eeg_inv_transform_points(inv(V.mat), mnipos);
Y = spm_sample_vol(V, vox(:, 1), vox(:, 2), vox(:, 3), 0);
ind = find(~isnan(Y) & abs(Y)>0);
% if no sources within mask, use sources close to mask
if isempty(ind)
maxdist = 5;
fprintf('No sources within mask. Using sources up to %1.1f cm away from mask.\n', maxdist/10);
xY.def = 'mask';
xY.spec = V;
[xY, XYZmm, j] = spm_ROI(xY, V);
for k =1:size(XYZmm, 2)
dist = sqrt(sum((mnipos-repmat(XYZmm(:,k)', size(mnipos, 1), 1)).^2, 2));
[minval, vxind(k)] = min(dist);
if minval>maxdist,vxind(k)=NaN;end;
end;
vxind(isnan(vxind))=[];
ind = unique(vxind);
end;
elseif isfield(S.vois{v}, 'voidef')
dist = sqrt(sum((mnipos-repmat(S.vois{v}.voidef.pos, size(mnipos, 1), 1)).^2, 2));
if S.vois{v}.voidef.radius>0
ind = find(dist<S.vois{v}.voidef.radius);
else
[minval ind] = min(dist);
if minval>20 % if there is nothing within 2cm something must be wrong
ind = [];
end
end
elseif isfield(S.vois{v}, 'sourcefiledef')
S.method = [];
if nchannels(E) > 1
for chnl = 1:nchannels(E)
lbl{chnl} = [montage.labelnew{1}, '_', num2str(chnl)];
end;
else
lbl = montage.labelnew;
end;
else
error('Don''t know what to do.');
end
if isfield(S.vois{v}, 'voidef') && isempty(ind)
error(['No sources were found close enough for VOI ' S.vois{v}.voidef.label]);
elseif isfield(S.vois{v}, 'maskdef') && isempty(ind)
error(['No sources were found close enough for VOI ' S.vois{v}.maskdef.label]);
end
if ~isfield(S.vois{v}, 'sourcefiledef')
W = cat(1, BF.inverse.(modalities{m}).W{ind});
switch S.method
case 'max'
Wc = W* BF.features.(modalities{m}).C*W'; % bf estimated source covariance matrix
[dum, mi] = max(diag(Wc));
montage.tra = [montage.tra; W(mi, :)*U'];
case 'svd'
%% just take top pca component for now
Wc = W* BF.features.(modalities{m}).C*W'; % bf estimated source covariance matrix
[V,dum,dum]=svd(Wc);
montage.tra=[montage.tra;(V(:,1)'/sqrt(size(Wc, 1)))*W*U'];
case 'keep'
montage.tra = [montage.tra; W*U'];
for i = 1:size(W, 1)
lbl{end+1, 1} = [montage.labelnew{v} '_' num2str(i)];
end
end;
else
montage.tra = NaN(numel(lbl), numel(cat(1, BF.inverse.(modalities{m}).W{1})*U'));
end;
else
error('Reference source definition is required');
end;
if ~isempty(lbl)
montage.labelnew = lbl;
end
% add all other sources
spm_progress_bar('Init', size(mnipos, 1), 'Generating source projectors');
nsources = size(mnipos, 1);
nRef = numel(montage.labelnew);
montage.tra = [montage.tra; NaN(nsources, size(montage.tra, 2))];
for i = 1:nsources
w = BF.inverse.(modalities{m}).W{i};
if ~isnan(w)
montage.tra(nRef + i, :) = w*U';
end
spm_progress_bar('Set', i);
end
spm_progress_bar('Clear');
montage.chantypenew = repmat({chantypenew}, length(montage.labelnew), 1);
montage.chanunitnew = repmat({'nA*m'}, length(montage.labelnew), 1);
% doing the actual job (this is new code, up to here was modified from
% bf_output_montage). This works trial-by-trial, source-by-source
% ---------------------------------------------------------------------
spm_progress_bar('Init', ntrials(D), 'Computing PLI');
PLI = NaN(nsources, nRef, ntrials(D));
% set up filter (4th order bandpass Butterworth)
[b, a] = butter(2, [S.filtermethod.highpass, S.filtermethod.lowpass]./(fsample(D)/2));
for iTrl = 1:ntrials(D)
% project and keep reference sources
for iRef = 1:nRef
if isfield(S.vois{1}, 'sourcefiledef')
Dnew = filtfilt(b, a, E(iRef, :, iTrl));
else
Dnew = filtfilt(b, a, montage.tra(iRef, :) * D(:, :, iTrl));
end;
complex_ref(iRef, :) = hilbert(Dnew);
end;
% project, compute and discard all other sources
for iOther = 1:nsources
Dtemp = filtfilt(b, a, montage.tra(iOther + nRef, :) * D(:, :, iTrl));
complex_temp = hilbert(Dtemp);
for iRef = 1:nRef
PLI(iOther, iRef, iTrl) = abs(mean(sign(angle(complex_temp./complex_ref(iRef, :)))));
end;
end;
spm_progress_bar('Set', iTrl);
end;
spm_progress_bar('Clear');
% average over trials
spm_progress_bar('Init', nconditions(D), 'Averaging PLI across trials');
cl = condlist(D);
aPLI = NaN(size(PLI, 1), nRef, nconditions(D));
for iCond = 1:nconditions(D)
indx = indtrial(D, cl{iCond}, 'GOOD');
if ~isempty(indx)
aPLI(:, :, iCond) = mean(PLI(:, :, indx), 3);
end;
spm_progress_bar('Set', iCond);
end;
spm_progress_bar('Clear');
% write into NIFTI-files
cImage = 1;
for iRef = 1:nRef
for iCond = 1:nconditions(D)
BF.output.image(cImage).val = aPLI(:, iRef, iCond);
BF.output.image(cImage).label = ['BF_', montage.labelnew{iRef}, '_', cl{iCond}];
cImage = cImage + 1;
end;
end;
S.normalise = 'none';
S.space = 'mni';
bf_write_nifti(BF, S);
end
mont = [];