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
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
/* film_gls.cc
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* Part of FSL - FMRIB's Software Library
WWW: http://www.fmrib.ox.ac.uk/fsl
Email: fsl@fmrib.ox.ac.uk
Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance
Imaging of the Brain), Department of Clinical Neurology, Oxford
University, Oxford, UK
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at
your option) any later version.
This program is distributed in the hope that it will be useful, but
in
order that the University as a charitable foundation protects its
assets for the benefit of its educational and research purposes, the
University makes clear that no condition is made or to be implied,
nor
is any warranty given or to be implied, as to the accuracy of FSL,
or
that it will be suitable for any particular purpose or for use under
any specific conditions. Furthermore, the University disclaims all
responsibility for the use which is made of FSL. See the GNU
General
Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA */
#include <iostream>
#include <fstream>
#include <strstream>
#define WANT_STREAM
#define WANT_MATH
#include "newmatap.h"
#include "newmatio.h"
#include "VolumeSeries.h"
#include "Volume.h"
#include "glim.h"
#include "sigproc.h"
#include "miscmaths.h"
#include "gaussComparer.h"
#include "Log.h"
#include "AutoCorrEstimator.h"
#include "paradigm.h"
#include "FilmGlsOptions.h"
#include "glimGls.h"
#include <string>
#ifndef NO_NAMESPACE
using namespace NEWMAT;
using namespace SIGPROC;
using namespace TACO;
using namespace UTILS;
#endif
int main(int argc, char *argv[])
{
try{
rand();
// parse command line to find out directory name for logging:
ofstream out2;
FilmGlsOptions& globalopts = FilmGlsOptions::getInstance();
globalopts.parse_command_line(argc, argv, out2);
// Setup logging:
Log& logger = Log::getInstance();
logger.setLogFile("glslogfile");
logger.establishDir(globalopts.datadir);
// parse command line again to output arguments to logfile
globalopts.parse_command_line(argc, argv, logger.str());
// load non-temporally filtered data
VolumeSeries x;
x.read(globalopts.inputfname);
// if needed output the 12th volume for use later
Volume epivol;
if(globalopts.smoothACEst)
{
epivol = x.getVolume(12).AsColumn();
epivol.setDims(x.getDims());
epivol.writeAsInt(logger.getDir() + "/" + globalopts.epifname);
}
// This also removes the mean from each of the time series:
x.thresholdSeries(globalopts.thresh, true);
// if needed later also threshold the epi volume
if(globalopts.smoothACEst)
{
epivol.setPreThresholdPositions(x.getPreThresholdPositions());
epivol.threshold();
}
int sizeTS = x.getNumVolumes();
int numTS = x.getNumSeries();
// Load paradigm:
Paradigm parad;
parad.load(globalopts.paradigmfname, "", false, sizeTS);
// Sort out detrending:
if(globalopts.detrend)
{
SIGPROC::Detrend(x, false);
}
if(globalopts.verbose)
{
logger.out("Gc", parad.getDesignMatrix());
}
// Set up OLS GLM for non-whitened data
Glim glim(x, parad.getDesignMatrix());
cerr << "Computing residuals for non-whitened data... ";
const VolumeSeries& rnotw = glim.ComputeResids();
cerr << "Completed" << endl;
if(globalopts.verbose)
{
logger.out("rnotw", rnotw);
}
// Estimate Autocorrelations:
AutoCorrEstimator acEst(rnotw);
if(globalopts.fitAutoRegressiveModel)
{
acEst.fitAutoRegressiveModel();
if(globalopts.verbose)
{
AutoCorrEstimator acEstForLogging(rnotw);
acEstForLogging.calcRaw();
logger.out("rawac", acEstForLogging.getEstimates());
logger.out("autoregac", acEst.getEstimates());
}
}
else
{
acEst.calcRaw();
if(globalopts.verbose)
{
logger.out("rawac", acEst.getEstimates());
}
// Smooth raw estimates:
if(globalopts.smoothACEst)
{
acEst.spatiallySmooth(logger.getDir() + "/" + globalopts.epifname, epivol, globalopts.ms, globalopts.epifname, globalopts.susanpath);
}
// Apply constraints to estimate autocorr:
acEst.pava();
if(globalopts.verbose)
{
logger.out("threshac", acEst.getEstimates());
}
}
ColumnVector I(sizeTS);
I = 0;
I(1) = 1;
// Setup OLS GLM for temporally filtered data:
int numParams = parad.getDesignMatrix().Ncols();
GlimGls glimGls(numTS, sizeTS, numParams);
ColumnVector xw(sizeTS);
ColumnVector xprew(sizeTS);
acEst.setDesignMatrix(parad.getDesignMatrix());
int co = 1;
// Loop through voxels:
cerr << "Calculating params..." << endl;
for(int i = 1; i <= numTS; i++)
{
// Use autocorr estimate to prewhiten data:
xprew = x.getSeries(i);
Matrix designmattw;
acEst.preWhiten(xprew, xw, i, designmattw);
if(co > 1000 || i == 5618 || i == 5582)
{
co = 1;
cerr << (float)i/(float)numTS << ",";
}
else
co++;
glimGls.setData(xw, designmattw, i);
}
cerr << "Completed" << endl;
// Write out necessary data:
cerr << "Saving results... ";
glimGls.Save(x.getDims(), x.getPreThresholdPositions());
cerr << "Completed" << endl;
}
catch(Exception p_excp)
{
cerr << p_excp.what() << endl;
}
catch(...)
{
cerr << "Image error" << endl;
}
return 0;
}