40 #include "CommonTypes.h"
41 #include "HDF5Utils.h"
42 #include "StatismoUtils.h"
47 template <
typename Representer>
53 typedef typename Representer::DatasetConstPointerType DatasetConstPointerType;
54 typedef typename Representer::DatasetPointerType DatasetPointerType;
55 typedef typename Representer::PointType PointType;
56 typedef typename Representer::ValueType ValueType;
58 typedef typename Representer::DomainType DomainType;
65 GenericRepresenterTest(
const Representer* representer, DatasetConstPointerType testDataset, std::pair<PointType, ValueType> pointValuePair)
66 : m_representer(representer),
67 m_testDataset(testDataset),
68 m_testPoint(pointValuePair.first),
69 m_testValue(pointValuePair.second) {
73 bool testSamplePointEvaluation()
const {
74 DatasetConstPointerType sample = m_testDataset;
75 unsigned id = m_representer->GetPointIdForPoint(m_testPoint);
76 ValueType val = m_representer->PointSampleFromSample(sample,
id);
77 statismo::VectorType valVec = m_representer->PointSampleToPointSampleVector(val);
80 statismo::VectorType sampleVector = m_representer->SampleToSampleVector(sample);
81 for (
unsigned i = 0; i < m_representer->GetDimensions(); ++i) {
82 unsigned idx = m_representer->MapPointIdToInternalIdx(
id, i);
83 if (sampleVector(i) != valVec(i)) {
90 bool testDomainValid()
const {
91 std::cout <<
"testDomainValid" << std::endl;
93 const DomainType domain = m_representer->GetDomain();
94 typename DomainType::DomainPointsListType domPoints = domain.
GetDomainPoints();
96 if (domPoints.size() == 0) {
97 std::cout <<
"representer defined empty domain" << std::endl;
101 std::cout <<
"domPoints.size() != domain.GetNumberOfPoints() (" << domPoints.size() <<
" != " << domain.
GetNumberOfPoints() << std::endl;
106 DatasetConstPointerType sample = m_testDataset;
107 statismo::VectorType sampleVector = m_representer->SampleToSampleVector(sample);
108 if (sampleVector.rows() != m_representer->GetDimensions() * domain.
GetNumberOfPoints()) {
109 std::cout <<
"the dimension of the sampleVector does not agree with the number of points in the domain (#points * dimensionality)" << std::endl;
115 for (
typename DomainType::DomainPointsListType::const_iterator it = domPoints.begin();
116 it != domPoints.end();
123 std::cout <<
"a point in the domain did not evaluate to a valid point it" << std::endl;
138 std::cout <<
"testSampleToVectorToSample" << std::endl;
140 statismo::VectorType sampleVec = getSampleVectorFromTestDataset();
142 DatasetConstPointerType reconstructedSample = m_representer->SampleVectorToSample(sampleVec);
145 statismo::VectorType reconstructedSampleAsVec = m_representer->SampleToSampleVector(reconstructedSample);
146 bool isOkay = assertSampleVectorsEqual(sampleVec, reconstructedSampleAsVec);
147 if (isOkay ==
false) {
148 std::cout <<
"Error: the sample has changed by converting between the representations " << std::endl;
155 std::cout <<
"testPointSampleDimension" << std::endl;
157 statismo::VectorType valVec = m_representer->PointSampleToPointSampleVector(m_testValue);
159 if (valVec.rows() != m_representer->GetDimensions()) {
160 std::cout <<
"Error: The dimensionality of the pointSampleVector is not the same as the Dimensionality of the representer" << std::endl;
170 std::cout <<
"testPointSampleToPointSampleVectorAndBack" << std::endl;
172 statismo::VectorType valVec = m_representer->PointSampleToPointSampleVector(m_testValue);
173 ValueType recVal = m_representer->PointSampleVectorToPointSample(valVec);
176 statismo::VectorType recValVec = m_representer->PointSampleToPointSampleVector(recVal);
177 bool ok = assertSampleVectorsEqual(valVec, recValVec);
179 std::cout <<
"Error: the point sample has changed by converting between the representations" << std::endl;
187 std::cout <<
"testSampleVectorHasCorrectValueAtPoint" << std::endl;
189 unsigned ptId = m_representer->GetPointIdForPoint(m_testPoint);
190 if (ptId < 0 || ptId >= m_representer->GetNumberOfPoints()) {
191 std::cout <<
"Error: invalid point id for test point " << ptId << std::endl;
196 statismo::VectorType sampleVec = getSampleVectorFromTestDataset();
197 statismo::VectorType pointSampleVec = m_representer->PointSampleToPointSampleVector(m_testValue);
199 for (
unsigned d = 0; d < m_representer->GetDimensions(); ++d) {
200 unsigned idx = m_representer->MapPointIdToInternalIdx(ptId, d);
201 if (sampleVec[idx] != pointSampleVec[d]) {
202 std::cout <<
"Error: the sample vector does not contain the correct value of the pointSample " << std::endl;
213 std::cout <<
"testSaveLoad" << std::endl;
217 std::string filename = statismo::Utils::CreateTmpName(
".rep");
220 file = H5File( filename, H5F_ACC_TRUNC );
221 }
catch (Exception& e) {
222 std::string msg(std::string(
"Error: Could not open HDF5 file for writing \n") + e.getCDetailMsg());
223 std::cout << msg << std::endl;
226 H5::Group representerGroup = file.createGroup(
"/representer");
228 m_representer->Save(representerGroup);
234 std::string dataTypeStr = Representer::TypeToString(m_representer->GetType());
239 file = H5File(filename.c_str(), H5F_ACC_RDONLY);
240 }
catch (Exception& e) {
241 std::string msg(std::string(
"Error: could not open HDF5 file \n") + e.getCDetailMsg());
242 std::cout << msg << std::endl;
246 representerGroup.close();
247 representerGroup = file.openGroup(
"/representer");
249 Representer* newRep = Representer::Create();
250 newRep->Load(representerGroup);
252 bool isOkay = assertRepresenterEqual(newRep, m_representer);
261 std::cout <<
"testClone" << std::endl;
264 Representer* rep = m_representer->Clone();
265 if (assertRepresenterEqual(rep, m_representer) ==
false) {
266 std::cout <<
"Error: the clone of the representer is not the same as the representer " << std::endl;
275 std::cout <<
"testSampleVectorDimensions()" << std::endl;
276 statismo::VectorType testSampleVec = getSampleVectorFromTestDataset();
278 bool isOk = m_representer->GetDimensions() * m_representer->GetNumberOfPoints() == testSampleVec.rows();
280 std::cout <<
"Error: Dimensionality of the sample vector does not agree with the representer parameters "
281 <<
"dimension and numberOfPoints" << std::endl;
282 std::cout << testSampleVec.rows() <<
" != " << m_representer->GetDimensions() <<
" * " << m_representer->GetNumberOfPoints() << std::endl;
289 std::cout <<
"testGetName" << std::endl;
291 if (m_representer->GetName() ==
"") {
292 std::cout <<
"Error: representer name has to be non empty" << std::endl;
300 std::cout <<
"testDimensions " << std::endl;
302 if (m_representer->GetDimensions() <= 0) {
303 std::cout <<
"Error: Dimensionality of representer has to be > 0" << std::endl;
313 ok = testSamplePointEvaluation() && ok;
314 ok = testDomainValid() && ok;
330 bool assertRepresenterEqual(
const Representer* representer1,
const Representer* representer2)
const {
331 if (representer1->GetNumberOfPoints() != representer2->GetNumberOfPoints()) {
332 std::cout <<
"the representers do not have the same nubmer of points " <<std::endl;
335 statismo::VectorType sampleRep1 = getSampleVectorFromTestDataset(representer1);
336 statismo::VectorType sampleRep2 = getSampleVectorFromTestDataset(representer2);
337 if (assertSampleVectorsEqual(sampleRep1, sampleRep2) ==
false) {
338 std::cout <<
"the representers produce different sample vectors for the same sample" << std::endl;
346 bool assertSampleVectorsEqual(
const statismo::VectorType& v1,
const statismo::VectorType& v2)
const {
347 if (v1.rows() != v2.rows()) {
348 std::cout <<
"dimensionality of SampleVectors do not agree" << std::endl;
352 for (
unsigned i = 0; i < v1.rows(); ++i) {
353 if (v1[i] != v2[i]) {
354 std::cout <<
"the sample vectors are not the same" << std::endl;
363 statismo::VectorType getSampleVectorFromTestDataset()
const {
364 return getSampleVectorFromTestDataset(m_representer);
367 statismo::VectorType getSampleVectorFromTestDataset(
const Representer* representer)
const {
368 DatasetConstPointerType sample = m_testDataset;
369 statismo::VectorType sampleVec = representer->SampleToSampleVector(sample);
374 const Representer* m_representer;
375 DatasetConstPointerType m_testDataset;
376 PointType m_testPoint;
377 ValueType m_testValue;
bool runAllTests()
run all the tests
Definition: genericRepresenterTest.hxx:310
const unsigned GetNumberOfPoints() const
Definition: Domain.h:72
bool testSampleVectorDimensions() const
test if the sample vector dimensions are correct
Definition: genericRepresenterTest.hxx:274
bool testGetName() const
test whether the name is defined
Definition: genericRepresenterTest.hxx:288
GenericRepresenterTest(const Representer *representer, DatasetConstPointerType testDataset, std::pair< PointType, ValueType > pointValuePair)
Definition: genericRepresenterTest.hxx:65
bool testSaveLoad() const
test whether the representer is correctly restored
Definition: genericRepresenterTest.hxx:212
Definition: genericRepresenterTest.hxx:48
bool testDimensions() const
test if the dimensionality is nonnegative
Definition: genericRepresenterTest.hxx:299
bool testPointSampleDimension() const
test if the pointSamples have the correct dimensionality
Definition: genericRepresenterTest.hxx:154
static void writeStringAttribute(const H5::H5Object &group, const char *name, const std::string &s)
Definition: HDF5Utils.hxx:387
bool testClone() const
test whether cloning a representer results in a representer with the same behaviour ...
Definition: genericRepresenterTest.hxx:260
bool testPointSampleToPointSampleVectorAndBack() const
Definition: genericRepresenterTest.hxx:169
const DomainPointsListType & GetDomainPoints() const
Definition: Domain.h:67
bool testSampleVectorHasCorrectValueAtPoint() const
Definition: genericRepresenterTest.hxx:186
bool testSampleToVectorAndBack() const
test whether converting a sample to a vector and back to a sample yields the original sample ...
Definition: genericRepresenterTest.hxx:137