Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
StatisticalModel.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 
39 #ifndef STATISTICALMODEL_H_
40 #define STATISTICALMODEL_H_
41 
42 #include <limits>
43 #include <vector>
44 
45 #include "CommonTypes.h"
46 #include "Config.h"
47 #include "DataManager.h"
48 #include "ModelInfo.h"
49 #include "Representer.h"
50 
51 namespace H5 {
52 class Group;
53 }
54 
55 namespace statismo {
56 
60 /*
61 template <typename Representer>
62 class PointValuePair {
63 public:
64  typedef typename Representer::PointType PointType;
65  typedef typename Representer::ValueType ValueType;
66 
67  PointValuePair(const PointType& pt, const ValueType& val) : point(pt), value(val) {}
68 
69  PointType point;
70  ValueType value;
71 };
72 */
73 
99 template <typename T>
101  public:
102  typedef Representer<T> RepresenterType ;
103  typedef typename RepresenterType::DatasetPointerType DatasetPointerType;
104  typedef typename RepresenterType::DatasetConstPointerType DatasetConstPointerType;
105  typedef typename RepresenterType::ValueType ValueType;
106  typedef typename RepresenterType::PointType PointType;
107 
108 
110 
111  typedef unsigned PointIdType;
112 
113 
114  //typedef PointValuePair<Representer> PointValuePairType;
115  typedef std::pair<PointType, ValueType> PointValuePairType;
116  typedef std::pair<unsigned, ValueType> PointIdValuePairType;
117  typedef std::list<PointValuePairType> PointValueListType;
118  typedef std::list<PointIdValuePairType> PointIdValueListType;
119 
120 
121 
122 
126  virtual ~StatisticalModel();
127 
128 
132 
134  /*
135  * Factory method that creates a new Model.
136  *
137  * \warning The use of this constructor is discouraged. If possible, use a ModelBuilder to create
138  * a new model or call Load to load an existing model
139  *
140  * \param representer the represener
141  * \param m the mean
142  * \param orthonormalPCABasis An orthonormal matrix with the principal Axes.
143  * \param pcaVariance The Variance for each principal Axis
144  * \param noiseVariance The variance of the (N(0,noiseVariance)) noise on each point
145  */
146  static StatisticalModel* Create(const RepresenterType* representer,
147  const VectorType& m,
148  const MatrixType& orthonormalPCABasis,
149  const VectorType& pcaVariance,
150  double noiseVariance) {
151  return new StatisticalModel(representer, m, orthonormalPCABasis, pcaVariance, noiseVariance);
152  }
153 
154 
155 
162  static StatisticalModel* Load(Representer<T>* representer, const std::string& filename, unsigned maxNumberOfPCAComponents = std::numeric_limits<unsigned>::max());
163 
164 
172  static StatisticalModel* Load(Representer<T>* representer, const H5::Group& modelroot, unsigned maxNumberOfPCAComponents = std::numeric_limits<unsigned>::max());
173 
174 
175 
181  void Delete() const {
182  delete this;
183  }
184 
185 
186 
191  void Save(const std::string& filename) const;
192 
198  void Save(const H5::Group& modelRoot) const;
199 
201 
202 
206 
210  unsigned int GetNumberOfPrincipalComponents() const;
211 
215  const ModelInfo& GetModelInfo() const;
217 
224 
226 
237  DatasetPointerType DatasetToSample(DatasetConstPointerType dataset) const;
238 
247  ValueType EvaluateSampleAtPoint(DatasetConstPointerType sample, unsigned ptId) const ;
248 
249 
258  ValueType EvaluateSampleAtPoint(DatasetConstPointerType sample, const PointType& pt) const;
259 
260 
264  DatasetPointerType DrawMean() const;
265 
275  DatasetPointerType DrawSample(const VectorType& coefficients, bool addNoise = false) const ;
276 
285  DatasetPointerType DrawSample(bool addNoise = false) const ;
286 
287 
288 
296  DatasetPointerType DrawPCABasisSample(unsigned componentNumber) const;
297 
298 
302 
310  ValueType DrawMeanAtPoint( const PointType& point) const;
311 
318  ValueType DrawMeanAtPoint( unsigned pointId) const;
319 
329  ValueType DrawSampleAtPoint(const VectorType& coefficients, const PointType& point, bool addNoise = false) const;
330 
340  ValueType DrawSampleAtPoint(const VectorType& coefficients, unsigned pointId, bool addNoise = false) const;
341 
342 
348  MatrixType GetJacobian(const PointType& pt) const ;
349 
355  MatrixType GetCovarianceAtPoint(const PointType& pt1, const PointType& pt2) const;
356 
362  MatrixType GetCovarianceAtPoint(unsigned ptId1, unsigned ptId2) const;
364 
365 
369 
378  MatrixType GetCovarianceMatrix() const;
379 
391  double ComputeProbabilityOfDataset(DatasetConstPointerType dataset) const ;
392 
400  double ComputeLogProbabilityOfDataset(DatasetConstPointerType dataset) const ;
401 
402 
414  double ComputeProbabilityOfCoefficients(const VectorType& coefficients) const ;
415 
423  double ComputeLogProbabilityOfCoefficients(const VectorType& coefficients) const ;
424 
425 
429  double ComputeMahalanobisDistanceForDataset(DatasetConstPointerType dataset) const;
430 
441  VectorType ComputeCoefficientsForDataset(DatasetConstPointerType dataset) const;
442 
443 
451  VectorType ComputeCoefficientsForSample(DatasetConstPointerType sample) const;
452 
453 
471  VectorType ComputeCoefficientsForPointValues(const PointValueListType& pointValues, double pointValueNoiseVariance=0.0) const;
472 
480  //RB: I had to modify the method name, to avoid prototype collisions when the PointType corresponds to unsigned (= type of the point id)
481  VectorType ComputeCoefficientsForPointIDValues(const PointIdValueListType& pointValues, double pointValueNoiseVariance=0.0) const;
482 
495  VectorType RobustlyComputeCoefficientsForDataset(DatasetConstPointerType dataset, unsigned nIterations=100, unsigned nu=6, double sigma2=1) const;
497 
503 
508  float GetNoiseVariance() const;
509 
513  const VectorType& GetPCAVarianceVector() const;
514 
521  const VectorType& GetMeanVector() const;
522 
531  const MatrixType& GetPCABasisMatrix() const;
532 
538  MatrixType GetOrthonormalPCABasisMatrix() const;
539 
544  VectorType DrawSampleVector(const VectorType& coefficients, bool addNoise = false) const ;
546 
547 
549 
552  void SetModelInfo(const ModelInfo& modelInfo);
553 
558  VectorType ComputeCoefficientsForSampleVector(const VectorType& sample) const;
559 
560 
564  const RepresenterType* GetRepresenter() const {
565  return m_representer;
566  }
567 
568 
572  const DomainType& GetDomain() const {
573  return m_representer->GetDomain();
574  }
575 
577 
578  private:
579  // computes the M Matrix for the PPCA Method (see Bishop, PRML, Chapter 12)
580  void CheckAndUpdateCachedParameters() const;
581 
582 
583 
588  StatisticalModel(const RepresenterType* representer, const VectorType& m, const MatrixType& orthonormalPCABasis, const VectorType& pcaVariance, double noiseVariance);
589 
591  StatisticalModel(const RepresenterType* representer);
592 
593  // to prevent use
595  StatisticalModel& operator=(const StatisticalModel& rhs);
596 
597  const RepresenterType* m_representer;
598 
599  VectorType m_mean;
600  MatrixType m_pcaBasisMatrix;
601  VectorType m_pcaVariance;
602  float m_noiseVariance;
603 
604 
605  // caching
606  mutable bool m_cachedValuesValid;
607 
608  //the matrix M^{-1} in Bishops PRML book. This is roughly the Latent Covariance matrix (but not exactly)
609  mutable MatrixType m_MInverseMatrix;
610 
611  ModelInfo m_modelInfo;
612  bool m_modelLoaded;
613 };
614 
615 } // namespace statismo
616 
617 #include "StatisticalModel.hxx"
618 
619 #endif /* STATISTICALMODEL_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
VectorType ComputeCoefficientsForPointIDValues(const PointIdValueListType &pointValues, double pointValueNoiseVariance=0.0) const
Definition: StatisticalModel.hxx:310
double ComputeMahalanobisDistanceForDataset(DatasetConstPointerType dataset) const
Definition: StatisticalModel.hxx:371
const ModelInfo & GetModelInfo() const
Definition: StatisticalModel.hxx:493
A trivial representer, that does no representation at all, but works directly with vectorial data...
Definition: TrivialVectorialRepresenter.h:83
VectorType DrawSampleVector(const VectorType &coefficients, bool addNoise=false) const
Definition: StatisticalModel.hxx:170
MatrixType GetOrthonormalPCABasisMatrix() const
Definition: StatisticalModel.hxx:473
VectorType ComputeCoefficientsForSample(DatasetConstPointerType sample) const
Definition: StatisticalModel.hxx:277
float GetNoiseVariance() const
Definition: StatisticalModel.hxx:447
VectorType ComputeCoefficientsForSampleVector(const VectorType &sample) const
Definition: StatisticalModel.hxx:283
VectorType RobustlyComputeCoefficientsForDataset(DatasetConstPointerType dataset, unsigned nIterations=100, unsigned nu=6, double sigma2=1) const
Definition: StatisticalModel.hxx:379
void Delete() const
Definition: StatisticalModel.h:181
DatasetPointerType DrawPCABasisSample(unsigned componentNumber) const
Definition: StatisticalModel.hxx:157
virtual ~StatisticalModel()
Definition: StatisticalModel.hxx:73
MatrixType GetCovarianceMatrix() const
Definition: StatisticalModel.hxx:258
const VectorType & GetPCAVarianceVector() const
Definition: StatisticalModel.hxx:460
const MatrixType & GetPCABasisMatrix() const
Definition: StatisticalModel.hxx:467
void SetModelInfo(const ModelInfo &modelInfo)
Definition: StatisticalModel.hxx:486
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
ValueType EvaluateSampleAtPoint(DatasetConstPointerType sample, unsigned ptId) const
Definition: StatisticalModel.hxx:105
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
VectorType ComputeCoefficientsForPointValues(const PointValueListType &pointValues, double pointValueNoiseVariance=0.0) const
Definition: StatisticalModel.hxx:297