-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspm_bias.m
259 lines (242 loc) · 8.58 KB
/
spm_bias.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
function varargout = spm_bias(X, varargin)
%__________________________________________________________________________
%
% Estimate a spatial bias field by fitting a GMM to the data.
%
% FORMAT [B,X,...] = spm_bias(X,...)
%
% MANDATORY
% ---------
% X - [LAT]xP Multichannel image or volume
%
% OPTIONAL
% ---------
% K - Number of GMM classes [10]
%
% KEYWORD
% -------
% VoxelSize - 1x[D] Vector of voxel sizes [1] (mm/vox)
% FWHM - 1x[D] Vector of FWHM to choose the number of bases [60] (mm)
% NbBases - 1x[D] Number of bases along each dimension [0=use FWHM]
% RegParam - 1x3 Regularisation parameters [0 0 10] (abs/mem/ben)
% IterMax - Maximum number of EM iterations [1000]
% Tolerance - Convergence criterion (lower bound gain) [1e-4]
% BinWidth - 1x[P] Bin width (histogram mode: add bits of variance) [0]
% InputDim - Input space dimension [0=try to guess]
% Verbose - Verbosity level: [0]= quiet
% 1 = write (lower bound)
% 2 = plot (lower bound)
% 3 = plot more (gmm fit)
% GMM - Cell of GMM options:
% GaussPrior - {MU0,b0,V0,n0} [{}=ML]
% PropPrior - a0 [0=ML]
% IterMax - Max number of EM iterations [1024]
% Tolerance - Gain tolerance to stop the EM algorithm [1e-4]
% SubIterMax - Max number of sub-EM iterations [1024]
% SubTolerance - Sub-EM gain tolerance [1e-4]
% Bias - Cell of Bias options:
% IterMax - Max number of GN iterations [1024]
% Tolerance - Gain tolerance to stop the GN algorithm [1e-4]
% LineSearch - Max number of line search iterations [6]
% JointOptim - Optimisation strategy [true=Joint]/false=Iterative
%
% OUTPUT
% ------
% B - [LAT]xP bias field
% X - [LAT]xP corrected input volume
% ... - Same output as spm_gmm, in the same order > help spm_gmm
%__________________________________________________________________________
% Copyright (C) 2018 Wellcome Centre for Human Neuroimaging
% -------------------------------------------------------------------------
% Parse inputs
p = inputParser;
p.FunctionName = 'spm_bias';
p.addRequired('X', @isnumeric);
p.addOptional('K', 10, @(X) isscalar(X) && isnumeric(X));
p.addParameter('VoxelSize', 1, @isnumeric);
p.addParameter('FWHM', 60, @isnumeric);
p.addParameter('NbBases', 0, @isnumeric);
p.addParameter('RegParam', 1E7, @isnumeric);
p.addParameter('GMM', {}, @iscell);
p.addParameter('Bias', {}, @iscell);
p.addParameter('IterMax', 1000, @(X) isscalar(X) && isnumeric(X));
p.addParameter('Tolerance', 1e-4, @(X) isscalar(X) && isnumeric(X));
p.addParameter('BinWidth', 0, @isnumeric);
p.addParameter('InputDim', 0, @(X) isscalar(X) && isnumeric(X));
p.addParameter('Verbose', 0, @(X) isnumeric(X) || islogical(X));
p.parse(X, varargin{:});
K = p.Results.K;
vs = p.Results.VoxelSize;
fwhm = p.Results.FWHM;
nbbases = p.Results.NbBases;
RegParam = p.Results.RegParam;
GMMopt = p.Results.GMM;
Biasopt = p.Results.Bias;
P = p.Results.InputDim;
BinWidth = p.Results.BinWidth;
IterMax = p.Results.IterMax;
Verbose = p.Results.Verbose;
Tolerance = p.Results.Tolerance;
% -------------------------------------------------------------------------
% Prepare input
[P,latX] = dimFromObservations(P, X);
dim = numel(latX);
C = spm_gmm_lib('obs2code', reshape(X, [], P)); % Code image
L = unique(C); % List of codes
% -------------------------------------------------------------------------
% Prepare bases
if sum(nbbases) == 0
nbbases = spm_bias_lib('fwhm2nbcomp', latX, vs, fwhm);
end
[Bx,By,Bz] = spm_bias_lib('dcbasis', latX, nbbases);
switch dim
case 2, bases = {Bx,By};
case 3, bases = {Bx,By,Bz};
end
ICO = spm_bias_lib('regulariser', 'bending', latX, nbbases, vs);
coeff = zeros([nbbases P], 'like', X);
field = spm_bias_lib('reconstruct', bases, coeff, 'mult');
% -------------------------------------------------------------------------
% GMM prior
MU0 = zeros(P,K);
BX = reshape(field.*X, [], P);
minval = min(BX, [], 'omitnan');
maxval = max(BX, [], 'omitnan');
clear BX
for p=1:P
tmp = linspace(minval(p), maxval(p), K+1);
MU0(p,:) = (tmp(1:end-1) + tmp(2:end))/2;
end
A0 = diag(((maxval - minval)./(2*K)).^(-2));
A0 = repmat(A0, [1 1 K]);
n0 = 10 * ones(1,K);
b0 = 10 * ones(1,K);
V0 = bsxfun(@rdivide, A0, reshape(n0, [1 1 K]));
a0 = 0 * ones(1,K);
mean = {MU0, b0};
prec = {V0, n0};
cluster = {mean prec};
a = a0;
PI = repmat(1/K, [1 K]);
Z = [];
% -------------------------------------------------------------------------
% EM loop
lb = struct('sum', NaN, 'X', [], 'XB', [], 'B', [], ...
'Z', [], 'P', [], 'MU', [], 'A', []);
ThisRegParam = RegParam(1);
RegParam = RegParam(2:end);
for em=1:IterMax
% ---------------------------------------------------------------------
% Save previous lower bound value
obj0 = lb.sum(end);
% ---------------------------------------------------------------------
% Optimise GMM
[Z,cluster,prop,lb] = spm_gmm_loop(reshape(field, [], P).*reshape(X, [], P), ...
cluster, {'Prop', PI, 'Dir', a}, ...
'PropPrior', a0, ...
'LowerBound', lb, ...
'Missing', true, ...
'MissingCode', {C,L}, ...
'BinUncertainty', (bsxfun(@times, reshape(field, [], P), reshape(BinWidth, 1, [])).^2)/12, ...
'Verbose', Verbose, ...
GMMopt{:});
MU = cluster.MU;
b = cluster.b;
A = cluster.A;
V = cluster.V;
n = cluster.n;
PI = prop.Prop;
a = prop.Dir;
if sum(b) > 0, mean = {MU b};
else, mean = {MU};
end
if sum(n) > 0, prec = {V n};
else, prec = {A};
end
cluster = {mean prec};
% ---------------------------------------------------------------------
% Plot Bias
if Verbose(1) >= 3
spm_bias_lib('Plot', 'Bias', X, field, latX);
end
% ---------------------------------------------------------------------
% Optimise Bias Field
[field,coeff,lb,ok] = spm_bias_loop(X, Z, cluster, bases, ...
'Coefficients', coeff, ...
'BiasField', field, ...
'LowerBound', lb, ...
'RegPrecision', ICO, ...
'RegParam', ThisRegParam, ...
'MissingCode', {C,L}, ...
'BinWidth', BinWidth, ...
'Verbose', Verbose, ...
Biasopt{:});
if ~ok
break;
end
% ---------------------------------------------------------------------
% Check convergence
obj = lb.sum(end);
den = max(lb.sum(:), [], 'omitnan')-min(lb.sum(:), [], 'omitnan');
gain = check_convergence(obj, obj0, den, em, Verbose(1));
if gain < Tolerance
if ~isempty(RegParam)
PrevRegParam = ThisRegParam;
ThisRegParam = RegParam(1);
RegParam = RegParam(2:end);
lb.B(:,end+1) = lb.B(:,end) * ThisRegParam / PrevRegParam;
else
break
end
end
end
% -------------------------------------------------------------------------
% Prepare output
Z = reshape(Z, [latX K]);
X = reshape(X, [latX P]);
field = reshape(field, [latX P]);
varargout{1} = field;
varargout{2} = field.*X;
varargout{3} = Z;
varargout{4} = MU;
varargout{5} = A;
varargout{6} = PI;
varargout{7} = b;
varargout{8} = V;
varargout{9} = n;
varargout{10} = a;
% =========================================================================
function gain = check_convergence(obj, obj0, den, it, verbose)
% FORMAT gain = check_convergence(obj, obj0, den, it, verbose)
gain = (obj - obj0)/den;
if verbose >= 1
switch sign(gain)
case 1, incr = '(+)';
case -1, incr = '(-)';
case 0, incr = '(=)';
otherwise, incr = '';
end
fprintf('%-5s | %4d | lb = %-10.6g | gain = %-10.4g | %3s\n', 'b+g', it, obj, gain, incr);
end
% =========================================================================
function [P,latX] = dimFromObservations(P, X)
dimX = size(X);
if P == 0
if numel(dimX) == 2
if dimX(1) == 1
% row-vector case
P = dimX(1);
else
% matrix case
P = dimX(2);
end
else
% N-array case
P = dimX(end);
end
end
if P == 1
latX = dimX;
else
latX = dimX(1:end-1);
end