-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinference_vb.h
More file actions
196 lines (158 loc) · 5.57 KB
/
inference_vb.h
File metadata and controls
196 lines (158 loc) · 5.57 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
/* inference_spatialvb.h - implementation of VB with spatial priors
Adrian Groves & Michael Chappell, FMRIB Image Analysis Group & IBME QuBIc Group
Copyright (C) 2007-2015 University of Oxford */
/* CCOPYRIGHT */
#include "convergence.h"
#include "inference.h"
#include "run_context.h"
#include <string>
#include <vector>
class Vb : public InferenceTechnique
{
public:
static InferenceTechnique *NewInstance();
Vb()
: m_nvoxels(0)
, m_noise_params(0)
, m_needF(false)
, m_printF(false)
, m_saveF(false)
, m_origdata(NULL)
, m_coords(NULL)
, m_suppdata(NULL)
, m_num_mcsteps(0)
, m_spatial_dims(-1)
, m_locked_linear(false)
{
}
virtual void GetOptions(std::vector<OptionSpec> &opts) const;
virtual std::string GetDescription() const;
virtual std::string GetVersion() const;
virtual void Initialize(FwdModel *fwd_model, FabberRunData &args);
virtual void DoCalculations(FabberRunData &data);
virtual void SaveResults(FabberRunData &rundata) const;
protected:
/**
* Initialize noise prior or posterior distribution from a file stored in the
* rundata under the given parameter key
*/
void InitializeNoiseFromParam(FabberRunData &args, NoiseParams *dist, std::string param_key);
/**
* Pass the model the data, coords and suppdata for a voxel.
*
* FIXME this is not very nice and should not be necessary. Need to
* audit what models are using this info and find alternatives, e.g.
* reading suppdata in Initialize instead
*/
void PassModelData(int voxel);
/**
* Determine whether we need spatial VB mode
*
* It is required either because it has been asked for (--method=spatialvb) or
* if any spatial priors have been specified (types mMpP)
*/
bool IsSpatial(FabberRunData &rundata) const;
/**
* Do calculations loop in voxelwise mode (i.e. all iterations for
* one voxel, then all iterations for the next voxel, etc)
*/
virtual void DoCalculationsVoxelwise(FabberRunData &data);
/**
* Do calculations loop in spatial mode (i.e. one iteration of all
* voxels, then next iteration of all voxels, etc)
*/
virtual void DoCalculationsSpatial(FabberRunData &data);
/**
* Calculate free energy if required, and display if required
*/
double CalculateF(int v, std::string label, double Fprior);
/**
* Output detailed debugging information for a voxel
*/
void DebugVoxel(int v, const std::string &where);
/**
* Setup per-voxel data for Spatial VB
*
* Spatial VB needs each voxel's prior/posterior and other
* data stored as it affects neighbouring voxels. This sets
* up the vectors which store these things which are just
* created on the fly for normal VB and throw away after each
* voxel is done.
*/
void SetupPerVoxelDists(FabberRunData &allData);
/**
* Check voxels are listed in order
*
* Order must be increasing in z value, or if same
* increasing in y value, and if y and z are same
* increasing in x value.
*
* This is basically column-major (Fortran) ordering - used as default by NEWIMAGE.
*/
void CheckCoordMatrixCorrectlyOrdered(const NEWMAT::Matrix &voxelCoords);
/**
* Calculate first and second nearest neighbours of each voxel
*/
void CalcNeighbours(const NEWMAT::Matrix &voxelCoords);
/**
* Ignore this voxel in future updates.
*
* No calculation of priors or posteriors will occur for this voxel
* and it will be removed from the lists of neighbours for other voxels.
* The effect should be as if it were masked
*/
void IgnoreVoxel(int v);
/** Number of voxels in data */
int m_nvoxels;
/**
* Noise model in use. This is created by the inference
* method deleted on destruction
*/
std::auto_ptr<NoiseModel> m_noise;
/**
* Number of noise parameters.
*/
int m_noise_params;
/** True if convergence detector requires the free energy */
bool m_needF;
/** True if we need to print the free energy at each iteration */
bool m_printF;
/** True if we need to to save the final free energy */
bool m_saveF;
/** True if we need to to save the free energy history */
bool m_saveFsHistory;
/** Free energy for each voxel */
std::vector<double> resultFs;
/** Free energy history for each voxel / iteration number */
std::vector<std::vector<double> > resultFsHistory;
/** Voxelwise input data */
const NEWMAT::Matrix *m_origdata;
/** Voxelwise co-ordinates */
const NEWMAT::Matrix *m_coords;
/** Voxelwise supplementary data */
const NEWMAT::Matrix *m_suppdata;
/** Number of motion correction steps to run */
int m_num_mcsteps;
/** Stores current run state (parameters, MVNs, linearization centres etc */
RunContext *m_ctx;
/** Linearized wrapper around the forward model */
std::vector<LinearizedFwdModel> m_lin_model;
/** Convergence detector for each voxel */
std::vector<ConvergenceDetector *> m_conv;
/**
* Number of spatial dimensions
*
* 0 = no spatial smoothing
* 1 = Probably not sensible!
* 2 = Smoothing in slices only
* 3 = Smoothing by volume
*/
int m_spatial_dims;
/**
* Fix the linearization centres of the linearized forward model.
*
* This reduces the inference to a purely linear problem. The fixed
* centres are generally loaded from an MVN file
*/
bool m_locked_linear;
};