forked from physimals/fabber_core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.cc
More file actions
442 lines (395 loc) · 16.3 KB
/
Copy pathinference.cc
File metadata and controls
442 lines (395 loc) · 16.3 KB
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
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
/* inference.cc - General inference technique base class
Adrian Groves & Michael Chappell, FMRIB Image Analysis Group & IBME QuBIc Group
Copyright (C) 2007-20015 University of Oxford */
/* CCOPYRIGHT */
#include "inference.h"
#include "easylog.h"
#include <newmat.h>
#include <fstream>
#include <iomanip>
#include <math.h>
using namespace std;
using namespace NEWMAT;
std::vector<std::string> InferenceTechnique::GetKnown()
{
InferenceTechniqueFactory *factory = InferenceTechniqueFactory::GetInstance();
return factory->GetNames();
}
InferenceTechnique *InferenceTechnique::NewFromName(const string &name)
{
InferenceTechniqueFactory *factory = InferenceTechniqueFactory::GetInstance();
InferenceTechnique *inf = factory->Create(name);
if (inf == NULL)
{
throw InvalidOptionValue("method", name, "Unrecognized inference method");
}
return inf;
}
void InferenceTechnique::UsageFromName(const string &name, std::ostream &stream)
{
stream << "Usage information for method: " << name << endl << endl;
std::auto_ptr<InferenceTechnique> method(NewFromName(name));
stream << method->GetDescription() << endl << endl << "Options: " << endl << endl;
vector<OptionSpec> options;
method->GetOptions(options);
if (options.size() > 0)
{
for (vector<OptionSpec>::iterator iter = options.begin(); iter != options.end(); ++iter)
{
stream << *iter << endl;
}
}
}
InferenceTechnique::InferenceTechnique()
: m_model(NULL)
, m_num_params(0)
, m_halt_bad_voxel(true)
{
}
void InferenceTechnique::Initialize(FwdModel *fwd_model, FabberRunData &rundata)
{
m_log = rundata.GetLogger();
m_debug = rundata.GetBool("debug");
if (m_debug)
LOG << setprecision(17);
m_model = fwd_model;
vector<Parameter> params;
m_model->GetParameters(rundata, params);
m_num_params = params.size();
LOG << "InferenceTechnique::Model has " << m_num_params << " parameters" << endl;
// Read masked time points option and log if any have been specified
m_masked_tpoints = rundata.GetIntList("mt", 1);
if (m_masked_tpoints.size() > 0)
{
LOG << "InferenceTechnique::Masking " << m_masked_tpoints.size() << " time points: ";
for (unsigned int i = 0; i < m_masked_tpoints.size(); i++)
{
LOG << m_masked_tpoints[i] << " ";
}
LOG << endl;
}
// Allow calculation to continue even with bad voxels
// Note that this is a bad idea when spatialDims>0, because the bad voxel
// will drag its neighbours around... but can never recover! Maybe a more
// sensible approach is to reset bad voxels to the prior on each iteration.
// Spatial VB ignores this parameter, presumably for the above reason
m_halt_bad_voxel = !rundata.GetBool("allow-bad-voxels");
if (m_halt_bad_voxel)
{
LOG << "InferenceTechnique::Note: numerical errors in voxels will cause the program to "
"halt.\n"
<< "InferenceTechnique::Use --allow-bad-voxels (with caution!) to keep on "
"calculating.\n";
}
else
{
LOG << "InferenceTechnique::Using --allow-bad-voxels: numerical errors in a voxel will\n"
<< "InferenceTechnique::simply stop the calculation of that voxel.\n"
<< "InferenceTechnique::Check log for 'Going on to the next voxel' messages.\n"
<< "InferenceTechnique::Note that you should get very few (if any) exceptions like "
"this;"
<< "InferenceTechnique::they are probably due to bugs or a numerically unstable model.";
}
}
void InferenceTechnique::SaveResults(FabberRunData &rundata) const
{
LOG << "InferenceTechnique::Preparing to save results..." << endl;
int nVoxels = resultMVNs.size();
// Save the resultMVNs NIFTI file
if (rundata.GetBool("save-mvn"))
{
MVNDist::Save(resultMVNs, "finalMVN", rundata);
}
// Create individual files for each parameter's mean and Z-stat
vector<Parameter> params;
m_model->GetParameters(rundata, params);
if (rundata.GetBool("save-mean") | rundata.GetBool("save-std") | rundata.GetBool("save-zstat") | rundata.GetBool("save-var"))
{
LOG << "InferenceTechnique::Writing means..." << endl;
for (unsigned i = 1; i <= params.size(); i++)
{
Matrix paramMean, paramZstat, paramStd, paramVar;
paramMean.ReSize(1, nVoxels);
paramZstat.ReSize(1, nVoxels);
paramStd.ReSize(1, nVoxels);
paramVar.ReSize(1, nVoxels);
for (int vox = 1; vox <= nVoxels; vox++)
{
MVNDist result = *resultMVNs[vox - 1];
m_model->ToModel(result);
paramMean(1, vox) = result.means(i);
paramVar(1, vox) = result.GetCovariance()(i, i);
double std = sqrt(paramVar(1, vox));
paramZstat(1, vox) = paramMean(1, vox) / std;
paramStd(1, vox) = std;
}
if (rundata.GetBool("save-mean"))
rundata.SaveVoxelData("mean_" + params.at(i - 1).name, paramMean);
if (rundata.GetBool("save-zstat"))
rundata.SaveVoxelData("zstat_" + params.at(i - 1).name, paramZstat);
if (rundata.GetBool("save-std"))
rundata.SaveVoxelData("std_" + params.at(i - 1).name, paramStd);
if (rundata.GetBool("save-var"))
rundata.SaveVoxelData("var_" + params.at(i - 1).name, paramVar);
}
}
// Produce the model fit and residual volume series
bool saveModelFit = rundata.GetBool("save-model-fit");
bool saveResiduals = rundata.GetBool("save-residuals");
vector<string> outputs;
outputs.push_back("");
m_model->GetOutputs(outputs);
if (saveModelFit || saveResiduals || (outputs.size() > 1))
{
LOG << "InferenceTechnique::Writing model time series data (fit, residuals and "
"model-specific output)"
<< endl;
Matrix result, residuals, datamtx, coords, suppdata;
datamtx = rundata.GetMainVoxelData(); // it is just possible that the model needs the data
// in its calculations
coords = rundata.GetVoxelCoords();
suppdata = rundata.GetVoxelSuppData();
result.ReSize(datamtx.Nrows(), nVoxels);
ColumnVector tmp;
for (vector<string>::iterator iter = outputs.begin(); iter != outputs.end(); ++iter)
{
LOG << "InferenceTechnique::Evaluating model for output: " << *iter << endl;
for (int vox = 1; vox <= nVoxels; vox++)
{
// do the evaluation
try
{
// pass in stuff that the model might need
ColumnVector y = datamtx.Column(vox);
ColumnVector vcoords = coords.Column(vox);
if (suppdata.Ncols() > 0)
{
m_model->PassData(vox, y, vcoords, suppdata.Column(vox));
}
else
{
m_model->PassData(vox, y, vcoords);
}
m_model->EvaluateFabber(
resultMVNs.at(vox - 1)->means.Rows(1, m_num_params), tmp, *iter);
if (result.Nrows() != tmp.Nrows())
{
// Only occurs on first voxel if output size is not equal to
// data size
result.ReSize(tmp.Nrows(), nVoxels);
}
result.Column(vox) = tmp;
}
// Ignore exceptions for the default Evaluate key - errors when evaluating the model would already have
// occurred during inference and the relevant warnings output.
catch (NEWMAT::Exception &e)
{
if (*iter != "")
{
LOG << "InferenceTechnique::NEWMAT error generating output " << *iter << " for voxel " << vox << " : " << e.what() << endl;
}
}
catch (std::exception &e)
{
if (*iter != "")
{
LOG << "InferenceTechnique::Error generating output " << *iter << " for voxel " << vox << " : " << e.what() << endl;
}
}
catch (...)
{
if (*iter != "")
{
LOG << "InferenceTechnique::Unexpected error generating output " << *iter << " for voxel " << vox << " : no message available" << endl;
}
}
}
if (*iter == "")
{
if (saveResiduals)
{
LOG << "InferenceTechnique::Saving residuals" << endl;
residuals = datamtx - result;
rundata.SaveVoxelData("residuals", residuals);
}
if (saveModelFit)
{
LOG << "InferenceTechnique::Saving model fit" << endl;
rundata.SaveVoxelData("modelfit", result);
}
}
else if (rundata.GetBool("save-model-extras"))
{
LOG << "InferenceTechnique::Saving extra output (size=" << result.Nrows() << ")" << endl;
rundata.SaveVoxelData(*iter, result);
}
}
}
#if 0
{
LOG << "InferenceTechnique::Writing model variances..." << endl;
Matrix modelStd;
Matrix datamtx = data.GetMainVoxelData();
modelStd.ReSize(datamtx.Nrows(), nVoxels);
for (int vox = 1; vox <= nVoxels; vox++)
{
LinearizedFwdModel lin(model);
lin.ReCentre(resultMVNs.at(vox - 1)->means.Rows(1, m_num_params));
Matrix var = resultMVNs.at(vox - 1)->GetCovariance().SymSubMatrix(1, m_num_params);
Matrix mvar = lin.Jacobian() * var * lin.Jacobian().t();
ColumnVector tmp(datamtx.Nrows());
for (int i = 1; i <= datamtx.Nrows(); i++)
{
tmp(i) = sqrt(mvar(i, i));
}
modelStd.Column(vox) = tmp;
}
data.SaveVoxelData("modelstd", modelStd);
}
#endif
LOG << "InferenceTechnique::Done writing results." << endl;
}
void InferenceTechnique::InitMVNFromFile(FabberRunData &rundata, string paramFilename = "")
{
// Loads in a MVN to set it as inital values for inference
// can cope with the special scenario in which extra parameters have been added to the inference
LOG << "InferenceTechnique::Merging supplied MVN with model intialization." << endl;
if (paramFilename == "")
{
MVNDist::Load(resultMVNs, "continue-from-mvn", rundata, m_log);
}
else
{
// load in parameters FIXME this will not work right now
LOG << "InferenceTechnique::Parameters named in file" << endl;
// Get a list of parameter names which were associated with the previous
// run so we can merge the MVNs with the full parameter set
string currparam;
ifstream paramFile((paramFilename).c_str());
if (!paramFile.good())
{
throw InvalidOptionValue("", paramFilename, "Could not open parameter file");
}
vector<string> paramNames;
LOG << "InferenceTechnique::Parameters from previous run: " << endl;
while (paramFile.good())
{
getline(paramFile, currparam);
paramNames.push_back(currparam);
LOG << currparam << endl;
}
paramNames.pop_back(); // remove final empty line assocaited with eof
// get the parameters in the model
vector<string> ModelparamNames;
m_model->NameParams(ModelparamNames);
LOG << "InferenceTechnique::Parameters named in model" << endl;
for (int p = 0; p < m_num_params; p++)
{
LOG << ModelparamNames[p] << endl;
}
// load in the MVN
vector<MVNDist *> MVNfile;
MVNDist::Load(MVNfile, "continue-from-mvn", rundata, m_log);
// Get defaults from the model. The prior is not used, the posterior is used
// if we don't have a posterior for a parameter in the file
MVNDist tempprior(m_num_params);
MVNDist tempposterior(m_num_params);
m_model->HardcodedInitialDists(tempprior, tempposterior);
// go through the parameters in the model and either:
// 1.) load the MVN from MVNfile if it is included, or
// 2.) use the default value from the model
// first work out where parameters in file MVN go in the model
LOG << "InferenceTechnique::Matching parameters from file with model:" << endl;
// Vector of bools for each parameter in the model. True if found in the file we're loading
vector<bool> usefile(m_num_params, false);
// If true, location of parameter in file (starting at 0)
vector<int> oldloc(m_num_params, 0);
// Vector of bools, one for each parameter in the file. True to flag matched to a file
// parameter
vector<bool> hasmatched(m_num_params, false);
for (int p = 0; p < m_num_params; p++)
{
usefile[p] = false;
for (unsigned q = 0; q < paramNames.size(); q++)
{
if (ModelparamNames[p] == paramNames[q])
{
usefile[p] = true;
oldloc[p] = q;
hasmatched[q] = true;
LOG << ModelparamNames[p] << ": Matched with file" << endl;
}
}
if (!usefile[p])
{
LOG << ModelparamNames[p] << ": Not matched, set from model default" << endl;
}
}
// Make a note of any parameters in the file that were not matched to params in the model
for (unsigned int q = 0; q < paramNames.size(); q++)
{
if (!hasmatched[q])
{
LOG_ERR(paramNames[q] + ": Not matched!");
}
}
// for (int a=0; a<usefile.size(); a++) {
// cout << usefile[a] << " " << oldloc[a] << endl;
// }
ColumnVector modelmeans = tempposterior.means;
ColumnVector newmeans = modelmeans;
SymmetricMatrix modelcov = tempposterior.GetCovariance();
SymmetricMatrix newcov = modelcov;
// Number of forward model and noise params in the FILE we are loading
int n_file_params = paramNames.size();
int n_file_noiseparams = MVNfile[1]->means.Nrows() - n_file_params;
int nvox = MVNfile.size();
// For every voxel we need to add an MVNDist to resultMVNs.
for (int v = 0; v < nvox; v++)
{
// Get the file data
MVNDist fwddist = MVNfile[v]->GetSubmatrix(1, n_file_params);
MVNDist noisedist
= MVNfile[v]->GetSubmatrix(n_file_params + 1, n_file_params + n_file_noiseparams);
for (unsigned int p = 0; p < ModelparamNames.size(); p++)
{
// deal with the means
if (usefile[p])
{
newmeans(p + 1) = fwddist.means(oldloc[p] + 1);
}
}
MVNDist newfwd(m_num_params);
newfwd.means = newmeans;
// deal with the covariances
SymmetricMatrix filecov = fwddist.GetCovariance();
for (unsigned int p = 0; p < ModelparamNames.size(); p++)
{
for (unsigned int q = 0; q <= p; q++)
{
if (usefile[p])
{
if (usefile[q])
{
newcov(p + 1, q + 1) = filecov(oldloc[p] + 1, oldloc[q] + 1);
}
}
}
}
newfwd.SetCovariance(newcov);
// Note that this assumes we have the same number of noise parameters in the
// file as we want in the calculation, not sure at moment if this is always
// true or not.
MVNDist *distout = new MVNDist(newfwd, noisedist);
resultMVNs.push_back(distout);
}
}
}
InferenceTechnique::~InferenceTechnique()
{
while (!resultMVNs.empty())
{
delete resultMVNs.back();
resultMVNs.pop_back();
}
}