ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
rtkProjectionsDecompositionNegativeLogLikelihood.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright RTK Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19#ifndef rtkProjectionsDecompositionNegativeLogLikelihood_h
20#define rtkProjectionsDecompositionNegativeLogLikelihood_h
21
25#include <itkVectorImage.h>
26
27#include "rtkMacro.h"
28#include <algorithm>
29
30namespace rtk
31{
39
40// We have to define the cost function first
42{
43public:
44 ITK_DISALLOW_COPY_AND_MOVE(ProjectionsDecompositionNegativeLogLikelihood);
45
50 itkNewMacro(Self);
51 itkOverrideGetNameOfClassMacro(ProjectionsDecompositionNegativeLogLikelihood);
52
53 // enum { SpaceDimension=m_NumberOfMaterials };
54
58
59 using DetectorResponseType = vnl_matrix<double>;
60 using MaterialAttenuationsType = vnl_matrix<double>;
61 using IncidentSpectrumType = vnl_matrix<float>;
65
66 // Constructor
73
74 // Destructor
76
78 GetValue(const ParametersType & itkNotUsed(parameters)) const override
79 {
80 long double measure = 0;
81 return measure;
82 }
83 void
84 GetDerivative(const ParametersType & itkNotUsed(lineIntegrals),
85 DerivativeType & itkNotUsed(derivatives)) const override
86 {
87 itkExceptionMacro(<< "Not implemented");
88 }
89 virtual void
91 {}
92
95 {
96 // Return the inverses of the diagonal components (i.e. the inverse variances, to be used directly in WLS
97 // reconstruction)
99 diag.SetSize(m_NumberOfMaterials);
100 diag.Fill(0);
101
102 for (unsigned int mat = 0; mat < m_NumberOfMaterials; mat++)
103 diag[mat] = 1. / m_Fischer.GetInverse()[mat][mat];
104 return diag;
105 }
106
109 {
110 // Return the inverses of the diagonal components (i.e. the inverse variances, to be used directly in WLS
111 // reconstruction)
113 diag.SetSize(m_NumberOfMaterials);
114 diag.Fill(0);
115
116 for (unsigned int mat = 0; mat < m_NumberOfMaterials; mat++)
117 diag[mat] = m_Fischer.GetInverse()[mat][mat];
118 return diag;
119 }
120
123 {
124 // Return the whole Fischer information matrix
126 fischer.SetSize(m_NumberOfMaterials * m_NumberOfMaterials);
127 fischer.Fill(0);
128
129 for (unsigned int i = 0; i < m_NumberOfMaterials; i++)
130 for (unsigned int j = 0; j < m_NumberOfMaterials; j++)
131 fischer[i * m_NumberOfMaterials + j] = m_Fischer[i][j];
132 return fischer;
133 }
134
135 virtual void
136 ComputeFischerMatrix(const ParametersType & itkNotUsed(lineIntegrals))
137 {}
138
139 unsigned int
140 GetNumberOfParameters() const override
141 {
142 return m_NumberOfMaterials;
143 }
144
145 virtual vnl_vector<double>
146 ForwardModel(const ParametersType & lineIntegrals) const
147 {
148 vnl_vector<double> attenuationFactors;
149 attenuationFactors.set_size(m_NumberOfEnergies);
150 GetAttenuationFactors(lineIntegrals, attenuationFactors);
151
152 // Apply detector response, getting the lambdas
153 return (m_IncidentSpectrumAndDetectorResponseProduct * attenuationFactors);
154 }
155
156 void
157 GetAttenuationFactors(const ParametersType & lineIntegrals, vnl_vector<double> & attenuationFactors) const
158 {
159 // Apply attenuation at each energy
160 vnl_vector<double> vnlLineIntegrals;
161
162 // Initialize the line integrals vnl vector
163 vnlLineIntegrals.set_size(m_NumberOfMaterials);
164 for (unsigned int m = 0; m < m_NumberOfMaterials; m++)
165 vnlLineIntegrals[m] = lineIntegrals[m];
166
167 // Apply the material attenuations matrix
168 attenuationFactors = this->m_MaterialAttenuations * vnlLineIntegrals;
169
170 // Compute the negative exponential
171 for (unsigned int energy = 0; energy < m_NumberOfEnergies; energy++)
172 {
173 attenuationFactors[energy] = std::exp(-attenuationFactors[energy]);
174 }
175 }
176
179 {
181 initialGuess.SetSize(m_NumberOfMaterials);
182
183 // Compute the mean attenuation in each bin, weighted by the input spectrum
184 // Needs to be done for each pixel, since the input spectrum is variable
185 MeanAttenuationInBinType MeanAttenuationInBin;
186 MeanAttenuationInBin.SetSize(this->m_NumberOfMaterials, this->m_NumberOfSpectralBins);
187 MeanAttenuationInBin.Fill(0);
188
189 for (unsigned int mat = 0; mat < this->m_NumberOfMaterials; mat++)
190 {
191 for (unsigned int bin = 0; bin < m_NumberOfSpectralBins; bin++)
192 {
193 double accumulate = 0;
194 double accumulateWeights = 0;
195 for (int energy = m_Thresholds[bin] - 1;
196 (energy < m_Thresholds[bin + 1]) && (energy < static_cast<int>(this->m_MaterialAttenuations.rows()));
197 energy++)
198 {
199 accumulate += this->m_MaterialAttenuations[energy][mat] * this->m_IncidentSpectrum[0][energy];
200 accumulateWeights += this->m_IncidentSpectrum[0][energy];
201 }
202 MeanAttenuationInBin[mat][bin] = accumulate / accumulateWeights;
203 }
204 }
205
206 for (unsigned int mat = 0; mat < m_NumberOfMaterials; mat++)
207 {
208 // Initialise to a very high value
209 initialGuess[mat] = 1e10;
210 for (unsigned int bin = 0; bin < m_NumberOfSpectralBins; bin++)
211 {
212 // Compute the length of current material required to obtain the attenuation
213 // observed in current bin. Keep only the minimum among all bins
214 double requiredLength = this->BinwiseLogTransform()[bin] / MeanAttenuationInBin[mat][bin];
215 initialGuess[mat] = std::min(initialGuess[mat], requiredLength);
216 }
217 }
218
219 return initialGuess;
220 }
221
224 {
226 logTransforms.SetSize(m_NumberOfSpectralBins);
227
228 vnl_vector<double> ones, nonAttenuated;
229 ones.set_size(m_NumberOfEnergies);
230 ones.fill(1.0);
231
232 // The way m_IncidentSpectrumAndDetectorResponseProduct works is
233 // it is mutliplied by the vector of attenuations factors (here
234 // filled with ones, since we want the non-attenuated signal)
236
237 for (unsigned int i = 0; i < m_MeasuredData.GetSize(); i++)
238 {
239 // Divide by the actually measured photon counts and apply log
240 if (m_MeasuredData[i] > 0)
241 logTransforms[i] = log(nonAttenuated[i] / m_MeasuredData[i]);
242 }
243
244 return logTransforms;
245 }
246
247 virtual vnl_vector<double>
248 GetVariances(const ParametersType & itkNotUsed(lineIntegrals)) const
249 {
250 vnl_vector<double> meaninglessResult;
251 meaninglessResult.set_size(m_NumberOfSpectralBins);
252 meaninglessResult.fill(0.);
253 return (meaninglessResult);
254 }
255
257 itkGetMacro(MeasuredData, MeasuredDataType);
258
260 itkGetMacro(DetectorResponse, DetectorResponseType);
261
262 itkSetMacro(MaterialAttenuations, MaterialAttenuationsType);
263 itkGetMacro(MaterialAttenuations, MaterialAttenuationsType);
264
265 itkSetMacro(NumberOfEnergies, unsigned int);
266 itkGetMacro(NumberOfEnergies, unsigned int);
267
268 itkSetMacro(NumberOfMaterials, unsigned int);
269 itkGetMacro(NumberOfMaterials, unsigned int);
270
272 itkGetMacro(IncidentSpectrum, IncidentSpectrumType);
273
274 itkSetMacro(NumberOfSpectralBins, unsigned int);
275 itkGetMacro(NumberOfSpectralBins, unsigned int);
276
278 itkGetMacro(Thresholds, ThresholdsType);
279
280protected:
287 unsigned int m_NumberOfEnergies;
292};
293
294} // namespace rtk
295
296#endif
This class is a base for the CostFunctions returning a single value.
Array< ParametersValueType > DerivativeType
Superclass::ParametersType ParametersType
Implements transparent reference counting.
A templated class holding a M x N size Matrix.
bool SetSize(unsigned int r, unsigned int c)
void GetDerivative(const ParametersType &lineIntegrals, DerivativeType &derivatives) const override
virtual vnl_vector< double > ForwardModel(const ParametersType &lineIntegrals) const
MeasureType GetValue(const ParametersType &parameters) const override
void GetAttenuationFactors(const ParametersType &lineIntegrals, vnl_vector< double > &attenuationFactors) const
virtual vnl_vector< double > GetVariances(const ParametersType &lineIntegrals) const
#define itkSetMacro(name, type)