Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
ReducedVarianceModelBuilder.hxx
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 __ReducedVarianceModelBuilder_hxx
39 #define __ReducedVarianceModelBuilder_hxx
40 
41 #include "ReducedVarianceModelBuilder.h"
42 
43 #include <iostream>
44 
45 #include <Eigen/SVD>
46 
47 #include "CommonTypes.h"
48 #include "Exceptions.h"
49 
50 namespace statismo {
51 
52 template <typename T>
53 ReducedVarianceModelBuilder<T>::ReducedVarianceModelBuilder()
54  : Superclass() {
55 }
56 
57 template <typename T>
58 typename ReducedVarianceModelBuilder<T>::StatisticalModelType*
60  const StatisticalModelType* inputModel,
61  unsigned numberOfPrincipalComponents) const
62 
63 {
64  StatisticalModelType* reducedModel = StatisticalModelType::Create(
65  inputModel->GetRepresenter(),
66  inputModel->GetMeanVector(),
67  inputModel->GetOrthonormalPCABasisMatrix().leftCols(numberOfPrincipalComponents),
68  inputModel->GetPCAVarianceVector().topRows(numberOfPrincipalComponents),
69  inputModel->GetNoiseVariance());
70 
71  // Write the parameters used to build the models into the builderInfo
72  typename ModelInfo::BuilderInfoList builderInfoList = inputModel->GetModelInfo().GetBuilderInfoList();
73 
74  BuilderInfo::ParameterInfoList bi;
75  bi.push_back(BuilderInfo::KeyValuePair("NumberOfPincipalComponents ", Utils::toString(numberOfPrincipalComponents)));
76 
77  BuilderInfo::DataInfoList di;
78 
79  BuilderInfo builderInfo("ReducedVarianceModelBuilder", di, bi);
80  builderInfoList.push_back(builderInfo);
81 
82  // If the scores matrix is not set, or if we have for some reasons not as many score entries as the number of principal components,
83  // we simply work with what is there.
84  unsigned numComponentsForScores = std::min(static_cast<unsigned>(inputModel->GetModelInfo().GetScoresMatrix().rows()), numberOfPrincipalComponents);
85 
86  ModelInfo info(inputModel->GetModelInfo().GetScoresMatrix().topRows(numComponentsForScores), builderInfoList);
87  reducedModel->SetModelInfo(info);
88 
89  return reducedModel;
90 
91 }
92 
93 
94 
95 template <typename T>
98  const StatisticalModelType* inputModel,
99  double totalVariance) const {
100 
101  VectorType pcaVariance = inputModel->GetPCAVarianceVector();
102  double modelVariance = pcaVariance.sum();
103 
104  //count the number of modes required for the model
105  double cumulatedVariance = 0;
106  unsigned numComponentsToReachPrescribedVariance = 0;
107  for (unsigned i = 0; i < pcaVariance.size(); i++) {
108  cumulatedVariance += pcaVariance(i);
109  numComponentsToReachPrescribedVariance++;
110  if (cumulatedVariance / modelVariance >= totalVariance)
111  break;
112  }
113  return BuildNewModelWithLeadingComponents(inputModel, numComponentsToReachPrescribedVariance);
114 }
115 
116 template <typename T>
119  const StatisticalModelType* inputModel,
120  double totalVariance) const {
121 
122  return BuildNewModelWithVariance(inputModel, totalVariance);
123 }
124 
125 } // namespace statismo
126 
127 #endif
const ModelInfo & GetModelInfo() const
Definition: StatisticalModel.hxx:493
MatrixType GetOrthonormalPCABasisMatrix() const
Definition: StatisticalModel.hxx:473
float GetNoiseVariance() const
Definition: StatisticalModel.hxx:447
StatisticalModelType * BuildNewModelWithVariance(const StatisticalModelType *model, double totalVariance) const
Definition: ReducedVarianceModelBuilder.hxx:97
const MatrixType & GetScoresMatrix() const
Definition: ModelInfo.cxx:74
Builds a new model which retains only the specified total variance.
Definition: ReducedVarianceModelBuilder.h:60
const VectorType & GetPCAVarianceVector() const
Definition: StatisticalModel.hxx:460
Holds information about the data and the parameters used by a specific modelbuilder.
Definition: ModelInfo.h:125
void SetModelInfo(const ModelInfo &modelInfo)
Definition: StatisticalModel.hxx:486
BuilderInfoList GetBuilderInfoList() const
Definition: ModelInfo.cxx:70
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
const VectorType & GetMeanVector() const
Definition: StatisticalModel.hxx:454
StatisticalModelType * BuildNewModelWithLeadingComponents(const StatisticalModelType *model, unsigned numberOfPrincipalComponents) const
Definition: ReducedVarianceModelBuilder.hxx:59
A Point/Value pair that is used to specify a value at a given point.
Definition: StatisticalModel.h:100
ITK Wrapper for the statismo::StatisticalModel class.
Definition: itkStatisticalModel.h:62