-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdateMuNormalML.m
347 lines (324 loc) · 11 KB
/
updateMuNormalML.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
338
339
340
341
342
343
344
345
346
347
function mu = updateMuNormalML(varargin)
%__________________________________________________________________________
%
% Closed form M-step update of the template for the Gaussian noise model.
% (Maximum likelihood update: no prior on Mu)
%
% -------------------------------------------------------------------------
%
% FORMAT mu = updateMuNormalML('f', ..., 'c', ...,
% ('s', ...), ('bb', ...), ...)
%
% REQUIRED KEYWORD (LIST)
% -----------------------
% f - Images pushed in template space
% c - Pushed voxel counts
%
% OPTIONAL KEYWORD (LIST)
% -----------------------
% s - Individual noise variances (sigma2)
% bb - Bounding boxes (if different from template)
%
% KEYWORD ARGUMENTS
% -----------------
% lat - Template lattice [temporarily REQUIRED]
% fwhm - Smoothing kernel used as pseudo prior [do not use]
% loop - How to split processing: 'component', 'slice', 'none' or '' [auto]
% par - Distribute compute [auto]
%
% OUTPUT
% ------
% mu - Updated template
%__________________________________________________________________________
i = 1;
if ischar(varargin{1})
i = i + 1;
N = 0;
while i <= numel(varargin) && ~ischar(varargin{i})
i = i + 1;
N = N + 1;
end
end
f = {};
if ischar(varargin{1}) && strcmpi(varargin{1}, 'f')
f = varargin(2:N+1);
varargin = varargin(N+2:end);
end
c = {};
if ischar(varargin{1}) && strcmpi(varargin{1}, 'c')
c = varargin(2:N+1);
varargin = varargin(N+2:end);
end
s = {};
if ischar(varargin{1}) && strcmpi(varargin{1}, 's')
s = varargin(2:N+1);
varargin = varargin(N+2:end);
end
bb = {};
if ischar(varargin{1}) && strcmpi(varargin{1}, 'bb')
bb = varargin(2:N+1);
varargin = varargin(N+2:end);
end
if numel(c) ~= N
error('There should be as many count as intensity images')
end
if numel(s) > 0 && numel(s) ~= N
error('There should be as many sigma2 as intensity images')
end
if numel(bb) > 0 && numel(bb) ~= N
error('There should be as many bounding boxes as intensity images')
end
% --- Parse inputs
p = inputParser;
p.FunctionName = 'updateMuNormalML';
p.addParameter('lat', [], @isnumeric);
p.addParameter('fwhm', 0, @isnumeric);
p.addParameter('loop', '', @(X) ischar(X) && any(strcmpi(X, {'slice', 'component', 'subject', 'none', ''})));
p.addParameter('par', false, @isscalar);
p.addParameter('debug', false, @isscalar);
p.addParameter('output', false);
p.parse(varargin{:});
lat = p.Results.lat;
fwhm = p.Results.fwhm;
loop = p.Results.loop;
par = p.Results.par;
debug = p.Results.debug;
output = p.Results.output;
if debug, fprintf('* updateMuNormalML\n'); end
if isempty(lat)
error('For now, the lattice size MUST be provided')
end
[par, loop] = autoParLoop(par, loop, isa(f{1}, 'file_array'), ...
size(f{1}, 3), size(f{1}, 4));
if fwhm > 0 && strcmpi(loop, 'slice')
loop = 'component';
end
switch lower(loop)
case 'none'
if debug, fprintf(' - No loop\n'); end
mu = loopNone(f, c, s, bb, lat, output, fwhm);
case 'component'
if debug
if par > 0
fprintf(' - Parallelise on components\n');
else
fprintf(' - Serialise on components\n');
end
end
mu = loopComponent(f, c, s, bb, lat, par, output, fwhm);
case 'slice'
if debug
if par > 0
fprintf(' - Parallelise on slices\n');
else
fprintf(' - Serialise on slices\n');
end
end
mu = loopSlice(f, c, s, bb, lat, par, output);
otherwise
error('Unknown loop type ''%s''', loop)
end
end
function mu = loopComponent(f, c, s, bb, lat, par, output, fwhm)
nc = size(f{1}, 4);
mu = prepareOnDisk(output, [lat nc], 'type', 'float32');
tmp = struct('f', f, 'c', c);
if ~isempty(s)
[tmp.s] = deal(s{:});
else
[tmp.s] = deal(ones(nc, 1));
end
if isempty(bb)
bb = struct('x', 1:lat(1), 'y', 1:lat(2), 'z', 1:lat(3));
[tmp.bb] = deal(bb);
else
[tmp.bb] = deal(bb{:});
end
if ~par
for k=1:nc
tmpf = zeros(lat, 'single');
tmpc = zeros(lat, 'single');
for n=1:numel(tmp)
s1 = tmp(n).s(k);
c1 = single(numeric(tmp(n).c)/s1);
bx = tmp(n).bb.x;
by = tmp(n).bb.y;
bz = tmp(n).bb.z;
tmpc(bx,by,bz) = tmpc(bx,by,bz) + c1;
tmpf(bx,by,bz) = tmpf(bx,by,bz) + c1 .* single(f{n}(:,:,:,k));
end
tmpf = smooth_gaussian(tmpf, fwhm);
tmpc = smooth_gaussian(tmpc, fwhm);
tmpf = tmpf ./ tmpc;
mu(tmp(n).bb.x,tmp(n).bb.y,tmp(n).bb.z,k) = tmpf;
end
elseif isa(tmp(1).f, 'file_array')
parfor (k=1:nc, par)
tmpf = zeros(lat, 'single');
tmpc = zeros(lat, 'single');
for n=1:numel(tmp)
s1 = tmp(n).s(k);
c1 = single(numeric(tmp(n).c)/s1);
bx = tmp(n).bb.x;
by = tmp(n).bb.y;
bz = tmp(n).bb.z;
tmpc(bx,by,bz) = tmpc(bx,by,bz) + c1;
tmpf(bx,by,bz) = tmpf(bx,by,bz) + c1 .* single(slicevol(tmp(n).f, k, 4));
end
tmpf = smooth_gaussian(tmpf, fwhm);
tmpc = smooth_gaussian(tmpc, fwhm);
tmpf = tmpf ./ tmpc;
mu(:,:,:,k) = tmpf;
end
else
parfor (k=1:nc, par)
tmpf = zeros(lat, 'single');
tmpc = zeros(lat, 'single');
for n=1:numel(tmp)
s1 = tmp(n).s(k);
c1 = single(numeric(tmp(n).c)/s1);
bx = tmp(n).bb.x;
by = tmp(n).bb.y;
bz = tmp(n).bb.z;
tmpc(bx,by,bz) = tmpc(bx,by,bz) + c1;
tmpf(bx,by,bz) = tmpf(bx,by,bz) + c1 .* single(tmp(n).f(:,:,:,k));
end
tmpf = smooth_gaussian(tmpf, fwhm);
tmpc = smooth_gaussian(tmpc, fwhm);
tmpf = tmpf ./ tmpc;
mu(:,:,:,k) = tmpf;
end
end
if ~isempty(output)
mu = saveOnDisk(output, mu, 'name', 'mu');
end
end
function mu = loopSlice(f, c, s, bb, lat, par, output)
nc = size(f{1}, 4);
mu = prepareOnDisk(output, [lat nc], 'type', 'float32');
% --- Compute mu
tmp = struct('f', f, 'c', c);
if ~isempty(s)
[tmp.s] = deal(s{:});
else
[tmp.s] = deal(ones(nc, 1));
end
if isempty(bb)
bb = struct('x', 1:lat(1), 'y', 1:lat(2), 'z', 1:lat(3));
[tmp.bb] = deal(bb);
else
[tmp.bb] = deal(bb{:});
end
if ~par
for z=1:lat(3)
tmpf = zeros([lat(1:2) 1 nc], 'single');
if isempty(s)
tmpc = zeros([lat(1:2) 1],'single');
else
tmpc = zeros([lat(1:2) 1 nc],'single');
end
for n=1:numel(tmp)
bx = tmp(n).bb.x;
by = tmp(n).bb.y;
bz = tmp(n).bb.z;
fz = z-bz(1)+1;
if fz >= 1 && fz <= size(tmp(1).f, 3)
s1 = reshape(tmp(n).s, [1 1 1 size(tmp(n).f, 4)]);
c1 = bsxfun(@rdivide, ...
single(numeric(tmp(n).c(:,:,fz))), s1);
f1 = bsxfun(@times, single(tmp(n).f(:,:,fz,:)), s1);
tmpc(bx,by,z) = tmpc(bx,by,z) + c1;
tmpf(bx,by,z) = tmpf(bx,by,z) + f1;
end
end
mu(:,:,z,:) = bsxfun(@rdivide, tmpf, tmpc);
end
elseif isa(tmp(1).f, 'file_array')
parfor (z=1:lat(3), par)
tmpf = zeros([lat(1:2) 1 nc], 'single');
if isempty(s)
tmpc = zeros([lat(1:2) 1],'single');
else
tmpc = zeros([lat(1:2) 1 nc],'single');
end
for n=1:numel(tmp)
bx = tmp(n).bb.x;
by = tmp(n).bb.y;
bz = tmp(n).bb.z;
fz = z-bz(1)+1;
if fz >= 1 && fz <= size(tmp(n).f, 3)
s1 = reshape(tmp(n).s, [1 1 1 size(tmp(n).f, 4)]);
c1 = bsxfun(@rdivide, ...
single(numeric(slicevol(tmp(n).c, fz, 3))), s1);
f1 = bsxfun(@times, single(slicevol(tmp(n).f, fz, 3)), s1);
tmpc(bx,by,z) = tmpc(bx,by,z) + c1;
tmpf(bx,by,z) = tmpf(bx,by,z) + f1;
end
end
mu(:,:,z,:) = bsxfun(@rdivide, tmpf, tmpc);
end
else
parfor (z=1:lat(3), par)
tmpf = zeros([lat(1:2) 1 nc], 'single');
if isempty(s)
tmpc = zeros([lat(1:2) 1],'single');
else
tmpc = zeros([lat(1:2) 1 nc],'single');
end
for n=1:numel(tmp)
bx = tmp(n).bb.x;
by = tmp(n).bb.y;
bz = tmp(n).bb.z;
fz = z-bz(1)+1;
if fz >= 1 && fz <= size(tmp(n).f, 3)
s1 = reshape(tmp(n).s, [1 1 1 size(tmp(n).f, 4)]);
c1 = bsxfun(@rdivide, ...
single(numeric(tmp(n).c(:,:,fz))), s1);
f1 = bsxfun(@times, single(tmp(n).f(:,:,fz,:)), s1);
tmpc(bx,by,z) = tmpc(bx,by,z) + c1;
tmpf(bx,by,z) = tmpf(bx,by,z) + f1;
end
end
mu(:,:,z,:) = bsxfun(@rdivide, tmpf, tmpc);
end
end
if ~isempty(output)
mu = saveOnDisk(output, mu, 'name', 'mu');
end
end
function mu = loopNone(f, c, s, bb, lat, output, fwhm)
nc = size(f{1}, 4);
mu = zeros([lat nc], 'single');
if isempty(s)
tmpc = zeros(lat, 'single');
else
tmpc = zeros([lat nc], 'single');
end
for n=1:numel(f)
if ~isempty(bb)
bx = bb{n}.x;
by = bb{n}.y;
bz = bb{n}.z;
else
bx = 1:lat(1);
by = 1:lat(2);
bz = 1:lat(3);
end
if ~isempty(s)
s1 = reshape(s{n}, [1 1 1 nc]);
c1 = bsxfun(@rdivide, single(numeric(c{n})), s1);
f1 = bsxfun(@times, single(numeric(f{n})), s1);
else
c1 = single(numeric(c{n}));
f1 = single(numeric(f{n}));
end
tmpc(bx,by,bz,:) = tmpc(bx,by,bz,:) + c1;
mu(bx,by,bz,:) = mu(bx,by,bz,:) + f1;
end
mu = smooth_gaussian(mu, fwhm);
tmpc = smooth_gaussian(tmpc, fwhm);
mu = bsxfun(@rdivide, mu, tmpc);
if ~isempty(output)
mu = saveOnDisk(output, mu, 'name', 'mu');
end
end