<commit_before>/*
 * This file is part of the statismo library.
 *
 * Author: <NAME> (<EMAIL>)
 *
 * Copyright (c) 2011 University of Basel
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of the project's author nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS addINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "statismo/CommonTypes.h"
#include <iostream>


using statismo::VectorType;

/**
 * This class provides generic tests for representer. The tests need to hold for all representers.
 */
template <typename Representer>
class GenericRepresenterTest {


	// we define typedefs for all required typenames, to force a compilation error if one of them
	// is not defined
	typedef typename Representer::DatasetConstPointerType DatasetConstPointerType;
	typedef typename Representer::DatasetPointerType DatasetPointerType;
	typedef typename Representer::PointType PointType;
	typedef typename Representer::ValueType ValueType;

	typedef typename Representer::DatasetInfo DatasetInfo;

	typedef typename Representer::DomainType DomainType;

public:
	/// Create new test with the given representer.
	/// Tests are performed using the given testDataset and the pointValuePair.
	/// It is assumed that the PointValuePair is taken from the testDataset (otherwise some tests will fail).
	GenericRepresenterTest(const Representer* representer, DatasetConstPointerType testDataset, std::pair<PointType, ValueType> pointValuePair)
	: m_representer(representer),
	  m_testDataset(testDataset),
	  m_testPoint(pointValuePair.first),
	  m_testValue(pointValuePair.second)
	{}


	bool testSamplePointEvaluation() const {
		DatasetConstPointerType sample = m_representer->DatasetToSample(m_testDataset, 0);
		unsigned id = m_representer->GetPointIdForPoint(m_testPoint);
		ValueType val = m_representer->PointSampleFromSample(sample, id);
		VectorType valVec = m_representer->PointSampleToPointSampleVector(val);

		// the obtained value should correspond to the value that is obtained by obtaining the sample vector, and evaluating it at the given position
		VectorType sampleVector = m_representer->SampleToSampleVector(sample);
		for (unsigned i = 0; i < Representer::GetDimensions(); ++i) {
			unsigned idx = m_representer->MapPointIdToInternalIdx(id, i);
			if (sampleVector(i) != valVec(i)) {
				return false;
			}
		}
		return true;
	}

	bool testDomainValid() const {
		std::cout << "testDomainValid" << std::endl;

		const DomainType domain = m_representer->GetDomain();
		typename DomainType::DomainPointsListType domPoints = domain.GetDomainPoints();

		if (domPoints.size() == 0) {
			std::cout << "representer defined empty domain" << std::endl;
			return false;
		}

		if (domPoints.size() != domain.GetNumberOfPoints()) {
			std::cout << "domPoints.size() != domain->GetNumberOfPoints" << std::endl;
			return false;
		}

		// if we convert a dataset to a samplevector, the resulting vector needs to have
		// as many entries as there are points * dimensions
		DatasetConstPointerType sample = m_representer->DatasetToSample(m_testDataset, 0);
		VectorType sampleVector = m_representer->SampleToSampleVector(sample);
		if (sampleVector.cols() != Representer::GetDimensions() * domain.GetNumberOfPoints()) {
			std::cout << "the dimension of the sampleVector does not agree with the number of points in the domain (#points * dimensionality)" << std::endl;
		}


		unsigned ptNo = 0;
		for (typename DomainType::DomainPointsListType::const_iterator it = domPoints.begin();
				it != domPoints.end();
				++it)
		{
			// since this can take long, we only do it for every 10th point
			if (ptNo % 10 != 0)
				break;

			if (m_representer->GetPointIdForPoint(*it) >= domain.GetNumberOfPoints()) {
				std::cout << "a point in the domain did not evaluate to a valid point it" << std::endl;
				return false;
			}
			ptNo++;
		}<commit_msg>bugfix: changed test to return false when it fails<commit_after>/*
 * This file is part of the statismo library.
 *
 * Author: <NAME> (<EMAIL>)
 *
 * Copyright (c) 2011 University of Basel
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of the project's author nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS addINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "statismo/CommonTypes.h"
#include <iostream>


using statismo::VectorType;

/**
 * This class provides generic tests for representer. The tests need to hold for all representers.
 */
template <typename Representer>
class GenericRepresenterTest {


	// we define typedefs for all required typenames, to force a compilation error if one of them
	// is not defined
	typedef typename Representer::DatasetConstPointerType DatasetConstPointerType;
	typedef typename Representer::DatasetPointerType DatasetPointerType;
	typedef typename Representer::PointType PointType;
	typedef typename Representer::ValueType ValueType;

	typedef typename Representer::DatasetInfo DatasetInfo;

	typedef typename Representer::DomainType DomainType;

public:
	/// Create new test with the given representer.
	/// Tests are performed using the given testDataset and the pointValuePair.
	/// It is assumed that the PointValuePair is taken from the testDataset (otherwise some tests will fail).
	GenericRepresenterTest(const Representer* representer, DatasetConstPointerType testDataset, std::pair<PointType, ValueType> pointValuePair)
	: m_representer(representer),
	  m_testDataset(testDataset),
	  m_testPoint(pointValuePair.first),
	  m_testValue(pointValuePair.second)
	{}


	bool testSamplePointEvaluation() const {
		DatasetConstPointerType sample = m_representer->DatasetToSample(m_testDataset, 0);
		unsigned id = m_representer->GetPointIdForPoint(m_testPoint);
		ValueType val = m_representer->PointSampleFromSample(sample, id);
		VectorType valVec = m_representer->PointSampleToPointSampleVector(val);

		// the obtained value should correspond to the value that is obtained by obtaining the sample vector, and evaluating it at the given position
		VectorType sampleVector = m_representer->SampleToSampleVector(sample);
		for (unsigned i = 0; i < Representer::GetDimensions(); ++i) {
			unsigned idx = m_representer->MapPointIdToInternalIdx(id, i);
			if (sampleVector(i) != valVec(i)) {
				return false;
			}
		}
		return true;
	}

	bool testDomainValid() const {
		std::cout << "testDomainValid" << std::endl;

		const DomainType domain = m_representer->GetDomain();
		typename DomainType::DomainPointsListType domPoints = domain.GetDomainPoints();

		if (domPoints.size() == 0) {
			std::cout << "representer defined empty domain" << std::endl;
			return false;
		}
		if (domPoints.size() != domain.GetNumberOfPoints()) {
		  std::cout << "domPoints.size() != domain.GetNumberOfPoints() (" << domPoints.size() << " != " << domain.GetNumberOfPoints() << std::endl;
			return false;
		}
		// if we convert a dataset to a samplevector, the resulting vector needs to have
		// as many entries as there are points * dimensions
		DatasetConstPointerType sample = m_representer->DatasetToSample(m_testDataset, 0);
		VectorType sampleVector = m_representer->SampleToSampleVector(sample);
		if (sampleVector.rows() != Representer::GetDimensions() * domain.GetNumberOfPoints()) {
			std::cout << "the dimension of the sampleVector does not agree with the number of points in the domain (#points * dimensionality)" << std::endl;
			return false;
		}


		unsigned ptNo = 0;
		for (typename DomainType::DomainPointsListType::const_iterator it = domPoints.begin();
				it != domPoints.end();
				++it)
		{
			// since this can take long, we only do it for every 10th point
			if (ptNo % 10 != 0)
				break;

			if (m_representer->GetPointIdForPoint(*it) >= domain.GetNumberOfPoints()) {
				std::cout << "a point in the domain did not evaluate to a valid point it" << std::endl;
				return false;
			}
			ptNo++;
		}
{'fraction_non_alphanumeric': 0.08493254797643929, 'fraction_numerical': 0.002660079802394072, 'mean_word_length': 4.7087852494577005, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1454054', 'n_tokens_mistral': 3133, 'n_tokens_neox': 2773, 'n_words': 1106}