-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbf_output_montage.m
203 lines (176 loc) · 7.82 KB
/
bf_output_montage.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
function mont = bf_output_montage(BF, S)
% Generates a montage for source extraction
% Copyright (C) 2013 Wellcome Trust Centre for Neuroimaging
% Vladimir Litvak
% $Id: bf_output_montage.m 130 2015-07-02 16:25:57Z dominik.bach@googlemail.com $
%--------------------------------------------------------------------------
if nargin == 0
label = cfg_entry;
label.tag = 'label';
label.name = 'Label';
label.strtype = 's';
label.help = {'Label for the VOI'};
pos = cfg_entry;
pos.tag = 'pos';
pos.name = 'MNI coordinates';
pos.strtype = 'r';
pos.num = [1 3];
pos.help = {'Locations for the 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 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 mask image'};
maskdef = cfg_branch;
maskdef.tag = 'maskdef';
maskdef.name = 'Mask VOI';
maskdef.val = {label, mask};
vois = cfg_repeat;
vois.tag = 'vois';
vois.name = 'Redefine VOIs';
vois.num = [0 Inf];
vois.values = {voidef, maskdef};
vois.val = {};
vois.help = {'This makes it possible to define new VOIs when the original source space was mesh or grid.',...
'Only the sources present in the original source space can be used at this stage'};
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'};
mont = cfg_branch;
mont.tag = 'montage';
mont.name = 'Source montage';
mont.val = {method, vois};
return
elseif nargin < 2
error('Two input arguments are required');
end
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';
if isfield(BF.inverse.(modalities{m}), 'label')
montage.labelnew = BF.inverse.(modalities{m}).label(:);
montage.tra = cat(1, BF.inverse.(modalities{m}).W{:})*U';
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
for v = 1:numel(S.vois)
if isfield(S.vois{v}, 'voidef')
montage.labelnew{v} = S.vois{v}.voidef.label;
elseif isfield(S.vois{v}, 'maskdef')
montage.labelnew{v} = S.vois{v}.maskdef.label;
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 = {};
for v = 1:numel(montage.labelnew) % v: VOI number
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
else
error('Don''t know what to do.');
end
if isempty(ind) && isfield(S.vois{v}, 'voidef')
error(['No sources were found close enough for VOI ' S.vois{v}.voidef.label]);
elseif isempty(ind) && isfield(S.vois{v}, 'maskdef')
error(['No sources were found close enough for VOI ' S.vois{v}.maskdef.label]);
end
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'];
if isfield(BF.sources, 'voi')
maxpos = BF.sources.voi.pos(ind(mi), :);
else
maxpos = mnipos(ind, :);
end
disp(['Selected position for ' montage.labelnew{v} ' is [' num2str(maxpos) ']']);
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;
end;
if ~isempty(lbl)
montage.labelnew = lbl;
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)
montage.labelnew{i} = sprintf('%.2f_%.2f_%.2f', mnipos(i, :));
montage.tra = [montage.tra; w*U'];
end
end
chantypenew = 'SRC';
end
montage.chantypenew = repmat({chantypenew}, length(montage.labelnew), 1);
montage.chanunitnew = repmat({'nA*m'}, length(montage.labelnew), 1);
mont.(modalities{m}) = montage;
end