Newer
Older
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
/* AutoCorrEstimator.cc
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* CCOPYRIGHT */
#include <iostream>
#include <strstream>
#include "AutoCorrEstimator.h"
#include "miscmaths.h"
#include "Log.h"
#include "Volume.h"
#include "histogram.h"
using namespace NEWMAT;
using namespace UTILS;
namespace TACO {
void AutoCorrEstimator::setDesignMatrix(const Matrix& dm) {
Tracer tr("AutoCorrEstimator::setDesignMatrix");
int sizeTS = xdata.getNumVolumes();
int numPars = dm.Ncols();
dminFFTReal.ReSize(zeropad, numPars);
dminFFTImag.ReSize(zeropad, numPars);
ColumnVector dmrow;
dmrow.ReSize(zeropad);
ColumnVector dm_fft_real, dm_fft_imag;
ColumnVector dummy(zeropad);
ColumnVector realifft(zeropad);
// FFT design matrix
for(int k = 1; k <= numPars; k++)
{
dummy = 0;
dmrow = 0;
mn(k) = MISCMATHS::mean(dm.Column(k));
dmrow.Rows(1,sizeTS) = dm.Column(k) - mn(k);
FFT(dmrow, dummy, dm_fft_real, dm_fft_imag);
dminFFTImag.Column(k) = dm_fft_imag;
dminFFTReal.Column(k) = dm_fft_real;
}
}
void AutoCorrEstimator::preWhiten(ColumnVector& in, ColumnVector& ret, int i, Matrix& dmret) {
Tracer tr("AutoCorrEstimator::preWhiten");
int sizeTS = xdata.getNumVolumes();
int numPars = dminFFTReal.getNumSeries();
ret.ReSize(sizeTS);
dmret.ReSize(sizeTS, numPars);
// FFT auto corr estimate
dummy = 0;
vrow = 0;
vrow.Rows(1,sizeTS/2) = acEst.getSeries(i).Rows(1,sizeTS/2);
vrow.Rows(zeropad - sizeTS/2 + 2, zeropad) = acEst.getSeries(i).Rows(2, sizeTS/2).Reverse();
FFT(vrow, dummy, ac_fft_real, ac_fft_im);
// FFT x data
dummy = 0;
xrow = 0;
xrow.Rows(1,sizeTS) = in;
FFT(xrow, dummy, x_fft_real, x_fft_im);
// inverse auto corr to give prewhitening filter:
// no DC component so set first value to 0;
ac_fft_real(1) = 0.0;
for(int j = 2; j <= zeropad; j++)
{
ac_fft_real(j) = 1.0/ac_fft_real(j);
}
// filter design matrix
for(int k = 1; k <= numPars; k++)
{
dm_fft_real = dminFFTReal.getSeries(k);
dm_fft_imag = dminFFTImag.getSeries(k);
FFTI(SP(ac_fft_real, dm_fft_real), SP(ac_fft_real, dm_fft_imag), realifft, dummy);
// place result into ret:
dmret.Column(k) = realifft.Rows(1,sizeTS);
float std = pow(MISCMATHS::var(dmret.Column(k)),0.5);
dmret.Column(k) = (dmret.Column(k)/std) + mn(k);
}
// Do filtering and inverse FFT:
FFTI(SP(ac_fft_real, x_fft_real), SP(ac_fft_real, x_fft_im), realifft, dummy);
// place result into ret:
ret = realifft.Rows(1,sizeTS);
}
void AutoCorrEstimator::preWhiten(VolumeSeries& in, VolumeSeries& ret)
{
Tracer tr("AutoCorrEstimator::preWhiten");
cerr << "Prewhitening... ";
int sizeTS = xdata.getNumVolumes();
int numTS = xdata.getNumSeries();
ret.ReSize(sizeTS, numTS);
// make sure p_vrow is cyclic (even function)
ColumnVector vrow, xrow;
vrow.ReSize(zeropad);
xrow.ReSize(zeropad);
ColumnVector x_fft_real, ac_fft_real;
ColumnVector x_fft_im, ac_fft_im;
ColumnVector dummy(zeropad);
ColumnVector realifft(zeropad);
int co = 1;
for(int i = 1; i <= numTS; i++)
{
// FFT auto corr estimate
dummy = 0;
vrow = 0;
vrow.Rows(1,sizeTS/2) = acEst.getSeries(i).Rows(1,sizeTS/2);
vrow.Rows(zeropad - sizeTS/2 + 2, zeropad) = acEst.getSeries(i).Rows(2, sizeTS/2).Reverse();
FFT(vrow, dummy, ac_fft_real, ac_fft_im);
// FFT x data
dummy = 0;
xrow = 0;
xrow.Rows(1,sizeTS/2) = in.getSeries(i).Rows(1,sizeTS/2);
xrow.Rows(zeropad - sizeTS/2 + 2, zeropad) = in.getSeries(i).Rows(2, sizeTS/2).Reverse();
FFT(xrow, dummy, x_fft_real, x_fft_im);
// inverse auto corr to give prewhitening filter:
// no DC component so set first value to 0;
ac_fft_real(1) = 0.0;
for(int j = 2; j <= zeropad; j++)
{
ac_fft_real(j) = 1.0/ac_fft_real(j);
}
// Do filtering and inverse FFT:
FFTI(SP(ac_fft_real, x_fft_real), SP(ac_fft_real, x_fft_im), realifft, dummy);
// place result into ret:
ret.Column(i) = realifft.Rows(1,sizeTS);
if(co > 100)
{
co = 1;
cerr << (float)i/(float)numTS << ",";
}
else
co++;
}
cerr << " Completed" << endl;
}
void AutoCorrEstimator::fitAutoRegressiveModel()
{
Tracer trace("AutoCorrEstimator::fitAutoRegressiveModel");
cerr << "Fitting autoregressive model..." << endl;
const int maxorder = 10;
int sizeTS = xdata.getNumVolumes();
int numTS = xdata.getNumSeries();
// setup temp variables
ColumnVector x(sizeTS);
ColumnVector order(numTS);
ColumnVector betas(maxorder);
acEst.ReSize(sizeTS, numTS);
acEst = 0;
int co = 1;
for(int i = 1; i <= numTS; i++)
{
x = xdata.getSeries(i).AsColumn();
order(i) = SIGPROC::Pacf(x, minorder, maxorder, betas);
if(order(i) != -1)
{
// Calculate auto corr:
ColumnVector Krow(sizeTS);
Krow = 0;
Krow(sizeTS) = 1;
if(order(i) > 0)
Krow.Rows(sizeTS-order(i), sizeTS-1) = -betas.Rows(1,order(i)).Reverse();
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
Matrix Kinv(sizeTS, sizeTS);
Kinv = 0;
for(int j = 1; j <= sizeTS; j++)
{
Kinv.SubMatrix(j,j,1,j) = Krow.Rows(sizeTS-j+1,sizeTS).t();
}
// Kinv now becomes V:
Kinv = (Kinv.t()*Kinv).i();
acEst.SubMatrix(1,sizeTS/2+1,i,i) = (Kinv.SubMatrix(sizeTS/2, sizeTS/2, sizeTS/2, sizeTS)/Kinv.MaximumAbsoluteValue()).AsColumn();
if(co > 200)
{
co = 1;
cerr << (float)i/(float)numTS << ",";
}
else
co++;
}
}
Log::getInstance().out("order", order);
cerr << " Completed" << endl;
}
int AutoCorrEstimator::establishUsanThresh(const Volume& epivol)
{
int usanthresh = 100;
int num = epivol.getVolumeSize();
Histogram hist(epivol, num/200);
hist.generate();
float mode = hist.mode();
cerr << "mode = " << mode << endl;
float sum = 0.0;
int count = 0;
// Work out standard deviation from mode for values greater than mode:
for(int i = 1; i <= num; i++) {
if(epivol(i) > mode) {
sum = sum + (epivol(i) - mode)*(epivol(i) - mode);
count++;
}
}
int sig = (int)pow(sum/num, 0.5);
cerr << "sig = " << sig << endl;
usanthresh = sig/3;
return usanthresh;
}
void AutoCorrEstimator::spatiallySmooth(const string& usanfname, const Volume& epivol, int masksize, const string& epifname, const string& susanpath) {
Tracer trace("AutoCorrEstimator::spatiallySmooth");
Log& logger = Log::getInstance();
// Establish epi thresh to use:
int usanthresh = establishUsanThresh(epivol);
// Setup external call to susan program:
char callsusanstr[1000];
ostrstream osc3(callsusanstr,1000);
string preSmoothVol = "preSmoothVol";
string postSmoothVol = "postSmoothVol";
osc3 << susanpath << " "
<< logger.getDir() << "/" << preSmoothVol << " 1 "
<< logger.getDir() << "/" << postSmoothVol << " "
<< masksize << " 3D 0 1 " << usanfname << " " << usanthresh << " "
<< logger.getDir() << "/" << "usanSize" << '\0';
// Loop through first third of volumes
// assume the rest are zero
int factor = 10000;
// Setup volume for reading and writing volumes:
Volume vol(acEst.getNumSeries(), xdata.getDims(), xdata.getPreThresholdPositions());
int i = 2;
cerr << "Spatially smoothing auto corr estimates" << endl;
cerr << callsusanstr << endl;
for(; i < 20; i++)
{
// output unsmoothed estimates:
vol = acEst.getVolume(i).AsColumn()*factor;
vol.unthreshold();
vol.writeAsInt(logger.getDir() + "/" + preSmoothVol);
// call susan:
system(callsusanstr);
// read in smoothed volume:
vol.read(logger.getDir() + "/" + postSmoothVol);
vol.threshold();
acEst.getVolume(i) = vol.AsRow()/factor;
cerr << ".";
}
cerr << endl;
// Clear unwanted written files
char rmfilesstr[1000];
ostrstream osc(rmfilesstr,1000);
osc << "rm -rf "
<< logger.getDir() + "/" + postSmoothVol + "* "
<< logger.getDir() + "/" + preSmoothVol + "* "
<< logger.getDir() + "/" + epifname + "* "
<< logger.getDir() + "/usanSize*" << '\0';
cerr << rmfilesstr << endl;
system(rmfilesstr);
cerr << "Completed" << endl;
}
void AutoCorrEstimator::calcRaw() {
cerr << "Calculating raw AutoCorrs...";
AutoCorr(xdata, acEst, zeropad);
cerr << " Completed" << endl;
}
void AutoCorrEstimator::filter(const ColumnVector& filterFFT) {
Tracer tr("AutoCorrEstimator::filter");
cerr << "Combining temporal filtering effects with AutoCorr estimates... ";
// This function adjusts the autocorrelations as if the
// xdata has been filtered by the passed in filterFFT
// DOES NOT filter the xdata itself
ColumnVector vrow;
// make sure p_vrow is cyclic (even function)
vrow.ReSize(zeropad);
ColumnVector fft_real;
ColumnVector fft_im;
ColumnVector dummy(zeropad);
ColumnVector realifft(zeropad);
int sizeTS = xdata.getNumVolumes();
for(int i = 1; i <= xdata.getNumSeries(); i++)
{
dummy = 0;
vrow = 0;
vrow.Rows(1,sizeTS/2) = acEst.getSeries(i).Rows(1,sizeTS/2);
vrow.Rows(zeropad - sizeTS/2 + 2, zeropad) = acEst.getSeries(i).Rows(2, sizeTS/2).Reverse();
FFT(vrow, dummy, fft_real, fft_im);
FFTI(SP(fft_real, filterFFT), dummy, realifft, dummy);
// place result into acEst:
acEst.Column(i) = realifft.Rows(1,sizeTS)/realifft(1);
}
cerr << " Completed" << endl;
}
void AutoCorrEstimator::pava() {
Tracer tr("AutoCorrEstimator::pava");
cerr << "Using New PAVA on AutoCorr estimates... ";
for(int i = 1; i <= xdata.getNumSeries(); i++) {
int sizeTS = xdata.getNumVolumes();
int stopat = (int)sizeTS/2;
// 5% point of distribution of autocorr about zero
const float th = (-1/sizeTS)+(2/sqrt(sizeTS));
ColumnVector values = acEst.Column(i);
ColumnVector zero(1);
zero = 0;
values = values.Rows(1,stopat) & zero;
ColumnVector gm(stopat + 1);
for(int j = 1; j <= stopat + 1; gm(j) = j++);
ColumnVector weights(stopat+1);
weights = 1;
bool anyviolators = true;
while(anyviolators) {
anyviolators = false;
for(int k = 2; k <= values.Nrows(); k++) {
if(values(k) > values(k-1)) {
anyviolators = true;
values(k-1) = (values(k-1)*weights(k-1) + values(k)*weights(k))/(weights(k-1) + weights(k));
values = values.Rows(1,k-1) & values.Rows(k+1,values.Nrows());
weights(k-1) = weights(k) + weights(k-1);
weights = weights.Rows(1,k-1) & weights.Rows(k+1,weights.Nrows());
for(int j = 1; j <= stopat + 1; j++) {
if(gm(j) >= k)
gm(j) = gm(j)-1;
}
break;
}
}
}
acEst.Column(i) = 0.0;
int j=1;
for(; j <= stopat; j++) {
acEst(j,i) = values(gm(j));
if(acEst(j,i) <= 0.0)
{
acEst(j,i) = 0.0;
break;
}
}
if(acEst(2,i) < th/2)
{
acEst.SubMatrix(2,stopat,i,i) = 0;
}
else if(j > 2)
//if(j > 2)
{
int endst = j;
int stst = j-(int)(1+(j/8.0));
const int expwidth = MISCMATHS::Max((endst - stst)/2,1);
const int exppow = 2;
for(j = stst; j <= endst; j++)
{
acEst(j,i) = acEst(j,i)*exp(-pow((j-stst)/expwidth,exppow));
}
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
}
cerr << " Completed" << endl;
}
void AutoCorrEstimator::applyConstraints() {
Tracer tr("AutoCorrEstimator::applyConstraints");
cerr << "Applying constraints to AutoCorr estimates... ";
for(int i = 1; i <= xdata.getNumSeries(); i++)
{
int sizeTS = xdata.getNumVolumes();
int j = 3;
int stopat = (int)sizeTS/4;
// found1 is last valid value above threshold
int found1 = stopat;
// 5% point of distribution of autocorr about zero
const float thresh = (-1/sizeTS)+(2/sqrt(sizeTS));
acEst(2,i) = (acEst(2,i)+ acEst(3,i))/2;
if(acEst(2,i) < 0)
{
acEst(2,i) = 0;
}
else
{
float grad = 0.0;
while(j <= stopat && j < found1 + 2)
{
grad = ((acEst(j,i) + acEst(j+1,i))/2 - acEst(j-1,i))/1.5;
if(grad < 0)
acEst(j,i) = grad + acEst(j-1,i);
else
acEst(j,i) = acEst(j-1,i);
// look for threshold
if(acEst(j,i) < thresh/3.0 && found1 == stopat)
{
found1 = j;
}
if(acEst(j,i) < 0)
{
acEst(j,i) = 0;
}
j++;
}
}
// set rest to zero:
for(; j <= sizeTS; j++)
{
acEst(j,i) = 0;
}
}
cerr << "Completed" << endl;
}
void AutoCorrEstimator::getMeanEstimate(ColumnVector& ret)
{
Tracer tr("AutoCorrEstimator::getMeanEstimate");
ret.ReSize(acEst.getNumVolumes());
// Calc global Vrow:
for(int i = 1; i <= acEst.getNumVolumes(); i++)
{
ret(i) = MISCMATHS::mean(acEst.getVolume(i).AsColumn());
}
}
}