38 #ifndef __PosteriorModelBuilder_hxx
39 #define __PosteriorModelBuilder_hxx
41 #include "PosteriorModelBuilder.h"
47 #include "CommonTypes.h"
48 #include "PCAModelBuilder.h"
58 PosteriorModelBuilder<T>::PosteriorModelBuilder()
64 typename PosteriorModelBuilder<T>::StatisticalModelType*
66 const DataItemListType& sampleDataList,
67 const PointValueListType& pointValues,
68 double pointValuesNoiseVariance,
69 double noiseVariance)
const {
70 return BuildNewModel(sampleDataList, TrivialPointValueWithCovarianceListWithUniformNoise(pointValues, pointValuesNoiseVariance), noiseVariance);
78 const PointValueListType& pointValues,
79 double pointValuesNoiseVariance,
80 bool computeScores)
const {
82 return BuildNewModelFromModel(inputModel, TrivialPointValueWithCovarianceListWithUniformNoise(pointValues,pointValuesNoiseVariance), computeScores);
87 typename PosteriorModelBuilder<T>::PointValueWithCovarianceListType
89 const PointValueListType& pointValues,
double pointValueNoiseVariance)
const {
91 const MatrixType pointCovarianceMatrix = pointValueNoiseVariance * MatrixType::Identity(3,3);
92 PointValueWithCovarianceListType pvcList;
95 for (
typename PointValueListType::const_iterator it = pointValues.begin(); it != pointValues.end(); ++it) {
96 pvcList.push_back(PointValueWithCovariancePairType(*it,pointCovarianceMatrix));
104 template <
typename T>
107 const DataItemListType& sampleDataList,
108 const PointValueWithCovarianceListType& pointValuesWithCovariance,
109 double noiseVariance)
const {
111 PCAModelBuilderType* modelBuilder = PCAModelBuilderType::Create();
113 StatisticalModelType* PosteriorModel = BuildNewModelFromModel(model, pointValuesWithCovariance, noiseVariance);
116 return PosteriorModel;
120 template <
typename T>
124 const PointValueWithCovarianceListType& pointValuesWithCovariance,
125 bool computeScores)
const {
127 typedef statismo::Representer<T> RepresenterType;
129 const RepresenterType* representer = inputModel->
GetRepresenter();
141 double rho2 = std::max((
double) inputModel->
GetNoiseVariance(), (double) Superclass::TOLERANCE);
143 unsigned dim = representer->GetDimensions();
149 MatrixType Q_g(pointValuesWithCovariance.size()* dim, numPrincipalComponents);
150 VectorType mu_g(pointValuesWithCovariance.size() * dim);
151 VectorType s_g(pointValuesWithCovariance.size() * dim);
153 MatrixType LQ_g(pointValuesWithCovariance.size()* dim, numPrincipalComponents);
156 for (
typename PointValueWithCovarianceListType::const_iterator it = pointValuesWithCovariance.begin(); it != pointValuesWithCovariance.end(); ++it) {
157 VectorType val = representer->PointSampleToPointSampleVector(it->first.second);
158 unsigned pt_id = representer->GetPointIdForPoint(it->first.first);
161 const MatrixType pointPrecisionMatrix = it->second.inverse();
164 const MatrixType Qrows_for_pt_id = Q.block(pt_id * dim, 0, dim, numPrincipalComponents);
166 Q_g.block(i * dim, 0, dim, numPrincipalComponents) = Qrows_for_pt_id;
167 mu_g.block(i * dim, 0, dim, 1) = mu.block(pt_id * dim, 0, dim, 1);
168 s_g.block(i * dim, 0, dim, 1) = val;
170 LQ_g.block(i * dim, 0, dim, numPrincipalComponents) = pointPrecisionMatrix * Qrows_for_pt_id;
176 const MatrixType& Q_gT = Q_g.transpose();
178 MatrixType M = Q_gT * LQ_g;
179 M.diagonal() += VectorType::Ones(Q_g.cols());
181 MatrixTypeDoublePrecision Minv = M.cast<
double>().inverse();
184 VectorType coeffs = Minv.cast<
ScalarType>() * LQ_g.transpose() * (s_g - mu_g);
190 VectorTypeDoublePrecision pcaSdev = pcaVariance.cast<
double>().array().sqrt();
192 VectorType D2MinusRho = D2 - VectorType::Ones(D2.rows()) * rho2;
194 for (
unsigned i = 0; i < D2MinusRho.rows(); i++) {
195 D2MinusRho(i) = std::max((
ScalarType) 0, D2(i));
197 VectorType D2MinusRhoSqrt = D2MinusRho.array().sqrt();
200 typedef Eigen::JacobiSVD<MatrixTypeDoublePrecision> SVDType;
201 MatrixTypeDoublePrecision innerMatrix = D2MinusRhoSqrt.cast<
double>().asDiagonal() * Minv * D2MinusRhoSqrt.cast<
double>().asDiagonal();
202 SVDType svd(innerMatrix, Eigen::ComputeThinU);
206 VectorType D_c = svd.singularValues().cast<
ScalarType>();
211 StatisticalModelType* PosteriorModel = StatisticalModelType::Create(representer , mu_c, U_c, D_c, rho2);
217 BuilderInfo::ParameterInfoList bi;
218 bi.push_back(BuilderInfo::KeyValuePair(
"NoiseVariance ", Utils::toString(rho2)));
219 bi.push_back(BuilderInfo::KeyValuePair(
"FixedPointsVariance ", Utils::toString(0.2)));
221 BuilderInfo::DataInfoList di;
224 for (
typename PointValueWithCovarianceListType::const_iterator it = pointValuesWithCovariance.begin(); it != pointValuesWithCovariance.end(); ++it) {
225 VectorType val = representer->PointSampleToPointSampleVector(it->first.second);
228 unsigned pt_id = representer->GetPointIdForPoint(it->first.first);
229 std::ostringstream keySStream;
230 keySStream <<
"Point constraint " << pt_no;
231 std::ostringstream valueSStream;
232 valueSStream <<
"(" << pt_id <<
", (";
234 for (
unsigned d = 0; d < dim - 1; d++) {
235 valueSStream << val[d] <<
",";
237 valueSStream << val[dim -1];
238 valueSStream <<
"))";
239 di.push_back(BuilderInfo::KeyValuePair(keySStream.str(), valueSStream.str()));
244 BuilderInfo builderInfo(
"PosteriorModelBuilder", di, bi);
245 builderInfoList.push_back(builderInfo);
248 MatrixType scores = MatrixType::Zero(inputScores.rows(), inputScores.cols());
250 if (computeScores ==
true) {
253 for (
unsigned i = 0; i < inputScores.cols(); i++) {
255 typename RepresenterType::DatasetPointerType ds = inputModel->
DrawSample(inputScores.col(i));
256 scores.col(i) = PosteriorModel->ComputeCoefficientsForDataset(ds);
257 representer->DeleteDataset(ds);
261 PosteriorModel->SetModelInfo(info);
263 return PosteriorModel;
const ModelInfo & GetModelInfo() const
Definition: StatisticalModel.hxx:493
MatrixType GetOrthonormalPCABasisMatrix() const
Definition: StatisticalModel.hxx:473
float GetNoiseVariance() const
Definition: StatisticalModel.hxx:447
const MatrixType & GetScoresMatrix() const
Definition: ModelInfo.cxx:74
StatisticalModelType * BuildNewModel(const DataItemListType &dataItemList, const PointValueListType &pointValues, double pointValueNoiseVariance, double noiseVariance) const
Definition: PosteriorModelBuilder.hxx:65
const VectorType & GetPCAVarianceVector() const
Definition: StatisticalModel.hxx:460
const MatrixType & GetPCABasisMatrix() const
Definition: StatisticalModel.hxx:467
Holds information about the data and the parameters used by a specific modelbuilder.
Definition: ModelInfo.h:125
BuilderInfoList GetBuilderInfoList() const
Definition: ModelInfo.cxx:70
StatisticalModelType * BuildNewModelFromModel(const StatisticalModelType *model, const PointValueListType &pointValues, double pointValueNoiseVariance, bool computeScores=true) const
Definition: PosteriorModelBuilder.hxx:76
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
unsigned int GetNumberOfPrincipalComponents() const
Definition: StatisticalModel.hxx:501
PointValueWithCovarianceListType TrivialPointValueWithCovarianceListWithUniformNoise(const PointValueListType &pointValues, double pointValueNoiseVariance) const
Definition: PosteriorModelBuilder.hxx:88
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
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