-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbf_regularise_roi.m
151 lines (124 loc) · 4.13 KB
/
bf_regularise_roi.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
function res = bf_regularise_roi(BF, S)
% ROI regularisation
% See: Oswal et al. Optimising beamformer regions of interest analysis, Neuroimage, 2014
% Copyright (C) 2014 Wellcome Trust Centre for Neuroimaging
% Ashwini Oswal, Vladimir Litvak
% $Id$
%--------------------------------------------------------------------------
if nargin == 0
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 = {pos, radius};
vois = cfg_repeat;
vois.tag = 'vois';
vois.name = 'ROI(s)';
vois.num = [1 Inf];
vois.values = {voidef};
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'};
manual = cfg_entry;
manual.tag = 'manual';
manual.name = 'User-specified';
manual.strtype = 'n';
manual.num = [1 1];
manual.help = {'User-specified number of dimensions'};
original = cfg_const;
original.tag = 'original';
original.name = 'Keep original';
original.help = {'Keep the original dimensionality of the ROI'};
original.val = {1};
minka = cfg_const;
minka.tag = 'minka';
minka.name = 'Minka truncation';
minka.help = {'Use Bayesian algorithm to determine dimensionality'};
minka.val = {1};
dimred = cfg_choice;
dimred.tag = 'dimred';
dimred.name = 'Dimension choice';
dimred.values = {original, manual, minka};
dimred.val = {original};
res = cfg_branch;
res.tag = 'roi';
res.name = 'ROI regularisation';
res.val = {vois, dimred};
return
elseif nargin < 2
error('Two input arguments are required');
end
mnipos = spm_eeg_inv_transform_points(BF.data.transforms.toMNI, BF.sources.pos);
ind = [];
for v = 1:numel(S.voidef)
dist = sqrt(sum((mnipos-repmat(S.voidef(v).pos, size(mnipos, 1), 1)).^2, 2));
if S.voidef(v).radius>0
ind = [ind find(dist<S.voidef(v).radius)];
else
[minval mind] = min(dist);
if minval>20 % if there is nothing within 2cm something must be wrong
mind = [];
warning(['No sources were found close enough for VOI ' num2str(2)]);
end
ind = [ind mind];
end
end
ind = unique(ind);
if isempty(ind)
error('No sources were found in the defined ROI');
end
BF.features.(S.modality).chanind = S.chanind;
[L, channels] = bf_fuse_lf(BF, S.modality);
LL = cat(2, L{ind});
Clf = LL*LL';
[LU, Sv, LV] = svd(Clf);
C = BF.features.(S.modality).C;
N = BF.features.(S.modality).N;
switch char(fieldnames(S.dimred))
case 'original'
U = LU;
case 'manual'
U = LU(:, 1:S.dimred.manual);
case 'minka'
[M_opt,log_ev,lambda1] = spm_pca_order (LU'*C*LU, N);
fprintf('Estimated covariance matrix order %d\n', M_opt);
[U, dum] = svd(LU'*C*LU);
U = LU*U(:, 1:M_opt);
end
C = U'*C*U;
Cinv = pinv_plus(C);
features = BF.features.(S.modality);
features.C = C;
features.Cinv = Cinv;
features.U = U;
res = features;
%%
%% plot the mean square root error here defined in the Van Veen paper as the sum of eigenvalues of components not taken over the sum of all eigenvalues
Fgraph = spm_figure('GetWin','Graphics'); figure(Fgraph); clf
all_eig = diag(Sv).^2;
den = sum(all_eig);
all_e = [];
for n = 1:numel(all_eig)
v = 1:n;
num = sum(all_eig(v));
e = (den-num)/den;
all_e = [all_e,e];
end
semilogy(all_e,'ro-');title('error as a function of dimensionality reduction');
hold on
plot(size(U, 2)*[1 1], ylim, 'k');
xlabel('components');
ylabel('error defined from Van Veen paper');