Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
itkStatisticalModel.h
1 /*
2  * This file is part of the statismo library.
3  *
4  * Author: Marcel Luethi (marcel.luethi@unibas.ch)
5  *
6  * Copyright (c) 2011 University of Basel
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * Neither the name of the project's author nor the names of its
21  * contributors may be used to endorse or promote products derived from
22  * this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef ITKSTATISTICALMODEL_H_
39 #define ITKSTATISTICALMODEL_H_
40 
41 #include <boost/bind.hpp>
42 #include <boost/utility/result_of.hpp>
43 
44 #include <itkObject.h>
45 #include <itkObjectFactory.h>
46 
47 #include <vnl/vnl_matrix.h>
48 #include <vnl/vnl_vector.h>
49 
50 #include "ModelInfo.h"
51 #include "Representer.h"
52 #include "StatisticalModel.h"
53 #include "statismoITKConfig.h"
54 
55 namespace itk {
56 
61 template <class T>
62 class StatisticalModel : public Object {
63  public:
64 
65  typedef StatisticalModel Self;
66  typedef Object Superclass;
67  typedef SmartPointer<Self> Pointer;
68  typedef SmartPointer<const Self> ConstPointer;
69 
70  itkNewMacro( Self );
71  itkTypeMacro( StatisticalModel, Object );
72 
73  typedef statismo::Representer<T> RepresenterType;
74 
75  // statismo stuff
77 
78  typedef typename statismo::DataManager<T>::DataItemType DataItemType;
79 
80  typedef vnl_matrix<statismo::ScalarType> MatrixType;
81  typedef vnl_vector<statismo::ScalarType> VectorType;
82 
83 
84  template <class F>
85  typename boost::result_of<F()>::type callstatismoImpl(F f) const {
86  try {
87  return f();
89  itkExceptionMacro(<< s.what());
90  }
91  }
92 
93  void SetstatismoImplObj(ImplType* impl) {
94  if (m_impl) {
95  delete m_impl;
96  }
97  m_impl = impl;
98  }
99 
100  ImplType* GetstatismoImplObj() const {
101  return m_impl;
102  }
103 
104  StatisticalModel() : m_impl(0) {}
105 
106  virtual ~StatisticalModel() {
107  if (m_impl) {
108  delete m_impl;
109  }
110  }
111 
112 
113  typedef typename RepresenterType::DatasetPointerType DatasetPointerType;
114  typedef typename RepresenterType::DatasetConstPointerType DatasetConstPointerType;
115 
116  typedef typename RepresenterType::ValueType ValueType;
117  typedef typename RepresenterType::PointType PointType;
118 
119  typedef typename statismo::StatisticalModel<T>::PointValuePairType PointValuePairType;
120  typedef typename statismo::StatisticalModel<T>::PointValueListType PointValueListType;
121 
123 
124 
125  void Load(RepresenterType* representer, const char* filename) {
126  try {
127  SetstatismoImplObj(ImplType::Load(representer, filename));
129  itkExceptionMacro(<< s.what());
130  }
131  }
132 
133 
134  void Load(RepresenterType* representer, const H5::Group& modelRoot) {
135  try {
136  SetstatismoImplObj(ImplType::Load(modelRoot));
138  itkExceptionMacro(<< s.what());
139  }
140  }
141 
142  //TODO: wrap StatisticalModel* BuildReducedVarianceModel( double pcvar );
143 
144  const RepresenterType* GetRepresenter() const {
145  return callstatismoImpl(boost::bind(&ImplType::GetRepresenter, this->m_impl));
146  }
147 
148  const DomainType& GetDomain() const {
149  return callstatismoImpl(boost::bind(&ImplType::GetDomain, this->m_impl));
150  }
151 
152  DatasetPointerType DrawMean() const {
153  return callstatismoImpl(boost::bind(&ImplType::DrawMean, this->m_impl));
154  }
155 
156  ValueType DrawMeanAtPoint(const PointType& pt) const {
157  typedef ValueType (ImplType::*functype)(const PointType&) const;
158  return callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::DrawMeanAtPoint), this->m_impl, pt));
159  }
160 
161  ValueType DrawMeanAtPoint(unsigned ptid) const {
162  typedef ValueType (ImplType::*functype)(unsigned) const;
163  return callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::DrawMeanAtPoint), this->m_impl, ptid));
164  }
165 
166  DatasetPointerType DrawSample(const VectorType& coeffs, bool addNoise = false) const {
167  typedef DatasetPointerType (ImplType::*functype)(const statismo::VectorType&, bool) const;
168  return callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::DrawSample), this->m_impl, fromVnlVector(coeffs), addNoise));
169  }
170 
171  DatasetPointerType DrawSample(bool addNoise = false) const {
172  typedef DatasetPointerType (ImplType::*functype)(bool) const;
173  return callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::DrawSample), this->m_impl, addNoise));
174  }
175 
176  DatasetPointerType DrawPCABasisSample(unsigned componentNumber) const {
177  typedef DatasetPointerType (ImplType::*functype)(unsigned) const;
178  return callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::DrawPCABasisSample), this->m_impl, componentNumber));
179  }
180 
181  ValueType DrawSampleAtPoint(const VectorType& coeffs, const PointType& pt, bool addNoise = false) const {
182  typedef ValueType (ImplType::*functype)(const statismo::VectorType&, const PointType&, bool) const;
183  return callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::DrawSampleAtPoint), this->m_impl, fromVnlVector(coeffs), pt, addNoise));
184  }
185 
186  ValueType DrawSampleAtPoint(const VectorType& coeffs, unsigned ptid, bool addNoise = false) const {
187  typedef ValueType (ImplType::*functype)(const statismo::VectorType&, unsigned, bool) const;
188  return callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::DrawSampleAtPoint), this->m_impl, fromVnlVector(coeffs), ptid, addNoise));
189  }
190 
191 
192  VectorType ComputeCoefficientsForDataset(DatasetConstPointerType ds) const {
193  return toVnlVector(callstatismoImpl(boost::bind(&ImplType::ComputeCoefficientsForDataset, this->m_impl, ds)));
194  }
195 
196  VectorType ComputeCoefficientsForSample(DatasetConstPointerType ds) const {
197  return toVnlVector(callstatismoImpl(boost::bind(&ImplType::ComputeCoefficientsForSample, this->m_impl, ds)));
198  }
199 
200  VectorType ComputeCoefficientsForDataSample(const DataItemType* sample) const {
201  return toVnlVector(callstatismoImpl(boost::bind(&ImplType::ComputeCoefficientsForDataSample, this->m_impl, sample)));
202  }
203 
204  double ComputeLogProbabilityOfDataset(DatasetConstPointerType ds) const {
205  return callstatismoImpl(boost::bind(&ImplType::ComputeLogProbabilityOfDataset, this->m_impl, ds));
206  }
207 
208  double ComputeProbabilityOfDataset(DatasetConstPointerType ds) const {
209  return callstatismoImpl(boost::bind(&ImplType::ComputeProbabilityOfDataset, this->m_impl, ds));
210  }
211 
212  double ComputeLogProbabilityOfCoefficients(const VectorType& coeffs) const {
213  return callstatismoImpl(boost::bind(&ImplType::ComputeLogProbabilityOfCoefficients, this->m_impl, fromVnlVector(coeffs)));
214  }
215 
216  double ComputeProbabilityOfCoefficients(const VectorType& coeffs) const {
217  return callstatismoImpl(boost::bind(&ImplType::ComputeProbabilityOfCoefficients, this->m_impl, fromVnlVector(coeffs)));
218  }
219 
220  double ComputeMahalanobisDistanceForDataset(DatasetConstPointerType ds) const {
221  return callstatismoImpl(boost::bind(&ImplType::ComputeMahalanobisDistanceForDataset, this->m_impl, ds));
222  }
223 
224  VectorType ComputeCoefficientsForPointValues(const PointValueListType& pvlist, double variance) const {
225  typedef statismo::VectorType (ImplType::*functype)(const PointValueListType&, double) const;
226  return toVnlVector(callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::ComputeCoefficientsForPointValues), this->m_impl, pvlist, variance)));
227  }
228 
229 
230  DatasetPointerType DatasetToSample(DatasetConstPointerType ds) const {
231  return callstatismoImpl(boost::bind(&ImplType::DatasetToSample, this->m_impl, ds));
232  }
233 
234  unsigned GetNumberOfPrincipalComponents() const {
235  return callstatismoImpl(boost::bind(&ImplType::GetNumberOfPrincipalComponents, this->m_impl));
236  }
237 
238  void Save(const char* modelname) {
239  typedef void (ImplType::*functype)(const std::string&) const;
240  callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::Save), this->m_impl, modelname));
241  }
242 
243  void Save(const H5::Group& modelRoot) {
244  typedef void (ImplType::*functype)(const H5::Group&) const;
245  callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::Save), this->m_impl, modelRoot));
246  }
247 
248  float GetNoiseVariance() const {
249  return callstatismoImpl(boost::bind(&ImplType::GetNoiseVariance, this->m_impl));
250  }
251 
252  MatrixType GetCovarianceAtPoint(const PointType& pt1, const PointType& pt2) const {
253  typedef statismo::MatrixType (ImplType::*functype)(const PointType&, const PointType&) const;
254  return toVnlMatrix(callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::GetCovarianceAtPoint), this->m_impl, pt1, pt2)));
255  }
256 
257  MatrixType GetCovarianceAtPoint(unsigned ptid1, unsigned ptid2) const {
258  typedef statismo::MatrixType (ImplType::*functype)(unsigned, unsigned ) const;
259  return toVnlMatrix(callstatismoImpl(boost::bind(static_cast<functype>(&ImplType::GetCovarianceAtPoint),this->m_impl, ptid1, ptid2)));
260  }
261 
262  MatrixType GetJacobian(const PointType& pt) const {
263  return toVnlMatrix(callstatismoImpl(boost::bind(&ImplType::GetJacobian, this->m_impl, pt)));
264  }
265 
266  MatrixType GetPCABasisMatrix() const {
267  return toVnlMatrix(callstatismoImpl(boost::bind(&ImplType::GetPCABasisMatrix, this->m_impl)));
268  }
269 
270  MatrixType GetOrthonormalPCABasisMatrix() const {
271  return toVnlMatrix(callstatismoImpl(boost::bind(&ImplType::GetOrthonormalPCABasisMatrix, this->m_impl)));
272  }
273 
274  VectorType GetPCAVarianceVector() const {
275  return toVnlVector(callstatismoImpl(boost::bind(&ImplType::GetPCAVarianceVector, this->m_impl)));
276  }
277 
278  VectorType GetMeanVector() const {
279  return toVnlVector(callstatismoImpl(boost::bind(&ImplType::GetMeanVector, this->m_impl)));
280  }
281 
282  const statismo::ModelInfo& GetModelInfo() const {
283  return callstatismoImpl(boost::bind(&ImplType::GetModelInfo, this->m_impl));
284  }
285 
286  private:
287 
288  static MatrixType toVnlMatrix(const statismo::MatrixType& M) {
289  return MatrixType(M.data(), M.rows(), M.cols());
290 
291  }
292 
293  static VectorType toVnlVector(const statismo::VectorType& v) {
294  return VectorType(v.data(), v.rows());
295 
296  }
297 
298  static statismo::VectorType fromVnlVector(const VectorType& v) {
299  return Eigen::Map<const statismo::VectorType>(v.data_block(), v.size());
300 
301  }
302 
303  StatisticalModel(const StatisticalModel& orig);
304  StatisticalModel& operator=(const StatisticalModel& rhs);
305 
306  ImplType* m_impl;
307 };
308 
309 
310 }
311 
312 #endif /* ITKSTATISTICALMODEL_H_ */
static StatisticalModel * Load(Representer< T > *representer, const std::string &filename, unsigned maxNumberOfPCAComponents=std::numeric_limits< unsigned >::max())
Definition: StatisticalModel.hxx:526
ValueType DrawSampleAtPoint(const VectorType &coefficients, const PointType &point, bool addNoise=false) const
Definition: StatisticalModel.hxx:191
double ComputeMahalanobisDistanceForDataset(DatasetConstPointerType dataset) const
Definition: StatisticalModel.hxx:371
const ModelInfo & GetModelInfo() const
Definition: StatisticalModel.hxx:493
MatrixType GetOrthonormalPCABasisMatrix() const
Definition: StatisticalModel.hxx:473
VectorType ComputeCoefficientsForSample(DatasetConstPointerType sample) const
Definition: StatisticalModel.hxx:277
float GetNoiseVariance() const
Definition: StatisticalModel.hxx:447
DatasetPointerType DrawPCABasisSample(unsigned componentNumber) const
Definition: StatisticalModel.hxx:157
Generic Exception class for the statismo Library.
Definition: Exceptions.h:68
const VectorType & GetPCAVarianceVector() const
Definition: StatisticalModel.hxx:460
const MatrixType & GetPCABasisMatrix() const
Definition: StatisticalModel.hxx:467
ValueType DrawMeanAtPoint(const PointType &point) const
Definition: StatisticalModel.hxx:120
stores meta information about the model, such as e.g. the name (uri) of the datasets used to build th...
Definition: ModelInfo.h:61
const RepresenterType * GetRepresenter() const
Definition: StatisticalModel.h:564
DatasetPointerType DatasetToSample(DatasetConstPointerType dataset) const
Definition: StatisticalModel.hxx:88
DatasetPointerType DrawMean() const
Definition: StatisticalModel.hxx:112
double ComputeProbabilityOfCoefficients(const VectorType &coefficients) const
Definition: StatisticalModel.hxx:364
MatrixType GetJacobian(const PointType &pt) const
Definition: StatisticalModel.hxx:507
const DomainType & GetDomain() const
Definition: StatisticalModel.h:572
double ComputeProbabilityOfDataset(DatasetConstPointerType dataset) const
Definition: StatisticalModel.hxx:350
unsigned int GetNumberOfPrincipalComponents() const
Definition: StatisticalModel.hxx:501
VectorType ComputeCoefficientsForDataset(DatasetConstPointerType dataset) const
Definition: StatisticalModel.hxx:267
double ComputeLogProbabilityOfDataset(DatasetConstPointerType dataset) const
Definition: StatisticalModel.hxx:343
MatrixType GetCovarianceAtPoint(const PointType &pt1, const PointType &pt2) const
Definition: StatisticalModel.hxx:230
const VectorType & GetMeanVector() const
Definition: StatisticalModel.hxx:454
DatasetPointerType DrawSample(const VectorType &coefficients, bool addNoise=false) const
Definition: StatisticalModel.hxx:150
A Point/Value pair that is used to specify a value at a given point.
Definition: StatisticalModel.h:100
void Save(const std::string &filename) const
Definition: StatisticalModel.hxx:620
double ComputeLogProbabilityOfCoefficients(const VectorType &coefficients) const
Definition: StatisticalModel.hxx:358
Definition: Domain.h:51
ITK Wrapper for the statismo::StatisticalModel class.
Definition: itkStatisticalModel.h:62
VectorType ComputeCoefficientsForPointValues(const PointValueListType &pointValues, double pointValueNoiseVariance=0.0) const
Definition: StatisticalModel.hxx:297