38 #ifndef __PCAModelBuilder_TXX
39 #define __PCAModelBuilder_TXX
41 #include "PCAModelBuilder.h"
47 #include "CommonTypes.h"
48 #include "Exceptions.h"
53 PCAModelBuilder<T>::PCAModelBuilder()
59 typename PCAModelBuilder<T>::StatisticalModelType*
62 unsigned n = sampleDataList.size();
67 unsigned p = sampleDataList.front()->GetSampleVector().rows();
68 const Representer<T>* representer = sampleDataList.front()->GetRepresenter();
74 for (
typename DataItemListType::const_iterator it = sampleDataList.begin();
75 it != sampleDataList.end();
77 assert ((*it)->GetSampleVector().rows() == p);
78 assert ((*it)->GetRepresenter() == representer);
79 X.row(i++) = (*it)->GetSampleVector();
87 scores = this->ComputeScores(X, model);
91 typename BuilderInfo::ParameterInfoList bi;
92 bi.push_back(BuilderInfo::KeyValuePair(
"NoiseVariance ", Utils::toString(noiseVariance)));
94 typename BuilderInfo::DataInfoList dataInfo;
96 for (
typename DataItemListType::const_iterator it = sampleDataList.begin();
97 it != sampleDataList.end();
99 std::ostringstream os;
101 dataInfo.push_back(BuilderInfo::KeyValuePair(os.str().c_str(),(*it)->GetDatasetURI()));
106 BuilderInfo builderInfo(
"PCAModelBuilder", dataInfo, bi);
108 ModelInfo::BuilderInfoList biList;
109 biList.push_back(builderInfo);
118 template <
typename T>
122 typedef Eigen::JacobiSVD<MatrixType> SVDType;
123 typedef Eigen::JacobiSVD<MatrixTypeDoublePrecision> SVDDoublePrecisionType;
125 unsigned n = X.rows();
126 unsigned p = X.cols();
128 RowVectorType mu = X.colwise().mean();
129 MatrixType X0 = X.rowwise() - mu;
141 MatrixType Cov = X0 * X0.transpose() * 1.0/(n-1);
142 SVDDoublePrecisionType SVD(Cov.cast<
double>(), Eigen::ComputeThinV);
143 VectorType singularValues = SVD.singularValues().cast<
ScalarType>();
144 MatrixType V = SVD.matrixV().cast<
ScalarType>();
146 unsigned numComponentsAboveTolerance = ((singularValues.array() - noiseVariance - Superclass::TOLERANCE) > 0).count();
149 unsigned numComponentsToKeep = std::min(numComponentsAboveTolerance, n - 1);
152 VectorType singSqrt = singularValues.array().sqrt();
153 VectorType singSqrtInv = VectorType::Zero(singSqrt.rows());
154 for (
unsigned i = 0; i < numComponentsToKeep; i++) {
155 assert(singSqrt(i) > Superclass::TOLERANCE);
156 singSqrtInv(i) = 1.0 / singSqrt(i);
163 MatrixType pcaBasis = (X0.transpose() * V * singSqrtInv.asDiagonal() / sqrt(n-1.0)).topLeftCorner(p, numComponentsToKeep);;
165 if (numComponentsToKeep == 0) {
166 throw StatisticalModelException(
"All the eigenvalues are below the given tolerance. Model cannot be built.");
169 VectorType sampleVarianceVector = singularValues.topRows(numComponentsToKeep);
170 VectorType pcaVariance = (sampleVarianceVector - VectorType::Ones(numComponentsToKeep) * noiseVariance);
172 StatisticalModelType* model = StatisticalModelType::Create(representer, mu, pcaBasis, pcaVariance, noiseVariance);
177 SVDType SVD(X0.transpose() * X0 * 1.0/(n-1), Eigen::ComputeThinU);
178 VectorType singularValues = SVD.singularValues();
179 unsigned numComponentsToKeep = ((singularValues.array() - noiseVariance - Superclass::TOLERANCE) > 0).count();
180 MatrixType pcaBasis = SVD.matrixU().topLeftCorner(p, numComponentsToKeep);
182 if (numComponentsToKeep == 0) {
183 throw StatisticalModelException(
"All the eigenvalues are below the given tolerance. Model cannot be built.");
186 VectorType sampleVarianceVector = singularValues.topRows(numComponentsToKeep);
187 VectorType pcaVariance = (sampleVarianceVector - VectorType::Ones(numComponentsToKeep) * noiseVariance);
188 StatisticalModelType* model = StatisticalModelType::Create(representer, mu, pcaBasis, pcaVariance, noiseVariance);
Generic Exception class for the statismo Library.
Definition: Exceptions.h:68
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
stores meta information about the model, such as e.g. the name (uri) of the datasets used to build th...
Definition: ModelInfo.h:61
StatisticalModelType * BuildNewModel(const DataItemListType &samples, double noiseVariance, bool computeScores=true) const
Definition: PCAModelBuilder.hxx:60
float ScalarType
the type that is used for all vector and matrices throughout the library.
Definition: CommonTypes.h:60
Creates StatisticalModel using Principal Component Analysis.
Definition: PCAModelBuilder.h:60
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