<commit_before>// --------------------------------------------------------------------------------------
// File:          MahalanobisDistanceModel.hxx
// Date:          Dec 23, 2014
// Author:        <EMAIL> (<NAME>)
// Version:       1.0 beta
// License:       GPLv3 - 29 June 2007
// Short Summary:
// --------------------------------------------------------------------------------------
//
// Copyright (c) 2014, <EMAIL> (<NAME>)
// with Signal Processing Lab 5, EPFL (LTS5-EPFL)
// and Biomedical Image Technology, UPM (BIT-UPM)
// All rights reserved.
//
// This file is part of ACWEReg
//
// ACWEReg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ACWEReg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ACWEReg.  If not, see <http://www.gnu.org/licenses/>.
//
// 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
// OWNER 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 INTERRUPTION) 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.
//

#ifndef _MAHALANOBISDISTANCEMODEL_HXX_
#define _MAHALANOBISDISTANCEMODEL_HXX_

#include "MahalanobisDistanceModel.h"


#include <math.h>
#include <vnl/vnl_math.h>
#include <vnl/vnl_matrix.h>
#include <vnl/vnl_diag_matrix.h>
#include <vnl/algo/vnl_symmetric_eigensystem.h>
#include <vnl/algo/vnl_ldl_cholesky.h>


namespace rstk {
template< typename TInputVectorImage, typename TPriorsPrecisionType >
MahalanobisDistanceModel< TInputVectorImage, TPriorsPrecisionType >
::MahalanobisDistanceModel():
  Superclass(), m_MaxEnergyGap(0.0) {
}

template< typename TInputVectorImage, typename TPriorsPrecisionType >
void
MahalanobisDistanceModel< TInputVectorImage, TPriorsPrecisionType >
::PrintSelf(std::ostream & os, itk::Indent indent) const {
	Superclass::PrintSelf(os, indent);
}

template< typename TInputVectorImage, typename TPriorsPrecisionType >
void
MahalanobisDistanceModel< TInputVectorImage, TPriorsPrecisionType >
::InitializeMemberships() {
	this->m_NumberOfRegions = this->GetPriorsMap()->GetNumberOfComponentsPerPixel();
	this->m_Memberships.resize(this->m_NumberOfRegions);
}

template< typename TInputVectorImage, typename TPriorsPrecisionType >
void
MahalanobisDistanceModel< TInputVectorImage, TPriorsPrecisionType >
::GenerateData() {
	this->InitializeMemberships();

	ReferenceSamplePointer sample = ReferenceSampleType::New();
	sample->SetImage( this->GetInput() );
	size_t npix = sample->Size();
	size_t nregions = this->m_NumberOfRegions - 1;

	std::vector<WeightArrayType> weights;
	const PriorsPrecisionType* priors = this->GetPriorsMap()->GetBufferPointer();

	WeightArrayType totals;
	totals.SetSize(nregions);
	totals.Fill(0.0);

	for( size_t roi = 0; roi < nregions; roi++ ) {
		WeightArrayType warr;
		warr.SetSize(npix);
		warr.Fill(0.0);
		weights.push_back(warr);
	}


	PriorsPrecisionType w;
	for( size_t i = 0; i < npix; i++ ) {
		for( size_t roi = 0; roi < nregions; roi++ ) {
			w = *(priors + nregions * i + roi);
			weights[roi][i] = w;
			totals[roi]+= w;
		}
	}

	this->m_Means.resize(nregions);
	this->m_Covariances.resize(nregions);
	this->m_RegionOffsetContainer.SetSize(nregions);<commit_msg>Bug in model estimator, fixes #185<commit_after>// --------------------------------------------------------------------------------------
// File:          MahalanobisDistanceModel.hxx
// Date:          Dec 23, 2014
// Author:        <EMAIL> (<NAME>)
// Version:       1.0 beta
// License:       GPLv3 - 29 June 2007
// Short Summary:
// --------------------------------------------------------------------------------------
//
// Copyright (c) 2014, <EMAIL> (<NAME>)
// with Signal Processing Lab 5, EPFL (LTS5-EPFL)
// and Biomedical Image Technology, UPM (BIT-UPM)
// All rights reserved.
//
// This file is part of ACWEReg
//
// ACWEReg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ACWEReg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ACWEReg.  If not, see <http://www.gnu.org/licenses/>.
//
// 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
// OWNER 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 INTERRUPTION) 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.
//

#ifndef _MAHALANOBISDISTANCEMODEL_HXX_
#define _MAHALANOBISDISTANCEMODEL_HXX_

#include "MahalanobisDistanceModel.h"


#include <math.h>
#include <vnl/vnl_math.h>
#include <vnl/vnl_matrix.h>
#include <vnl/vnl_diag_matrix.h>
#include <vnl/algo/vnl_symmetric_eigensystem.h>
#include <vnl/algo/vnl_ldl_cholesky.h>


namespace rstk {
template< typename TInputVectorImage, typename TPriorsPrecisionType >
MahalanobisDistanceModel< TInputVectorImage, TPriorsPrecisionType >
::MahalanobisDistanceModel():
  Superclass(), m_MaxEnergyGap(0.0) {
}

template< typename TInputVectorImage, typename TPriorsPrecisionType >
void
MahalanobisDistanceModel< TInputVectorImage, TPriorsPrecisionType >
::PrintSelf(std::ostream & os, itk::Indent indent) const {
	Superclass::PrintSelf(os, indent);
}

template< typename TInputVectorImage, typename TPriorsPrecisionType >
void
MahalanobisDistanceModel< TInputVectorImage, TPriorsPrecisionType >
::InitializeMemberships() {
	this->m_NumberOfRegions = this->GetPriorsMap()->GetNumberOfComponentsPerPixel();
	this->m_Memberships.resize(this->m_NumberOfRegions);
}

template< typename TInputVectorImage, typename TPriorsPrecisionType >
void
MahalanobisDistanceModel< TInputVectorImage, TPriorsPrecisionType >
::GenerateData() {
	this->InitializeMemberships();

	ReferenceSamplePointer sample = ReferenceSampleType::New();
	sample->SetImage( this->GetInput() );
	size_t npix = sample->Size();
	size_t nregions = this->m_NumberOfRegions - 1;

	std::vector<WeightArrayType> weights;
	const PriorsPrecisionType* priors = this->GetPriorsMap()->GetBufferPointer();
	size_t offset = this->GetPriorsMap()->GetNumberOfComponentsPerPixel();

	WeightArrayType totals;
	totals.SetSize(nregions);
	totals.Fill(0.0);

	for( size_t roi = 0; roi < nregions; roi++ ) {
		WeightArrayType warr;
		warr.SetSize(npix);
		warr.Fill(0.0);
		weights.push_back(warr);
	}


	PriorsPrecisionType w;
	for( size_t i = 0; i < npix; i++ ) {
		for( size_t roi = 0; roi < nregions; roi++ ) {
			w = *(priors + offset * i + roi);
			weights[roi][i] = w;
			totals[roi]+= w;
		}
	}

	this->m_Means.resize(nregions);
	this->m_Covariances.resize(nregions);
	this->m_RegionOffsetContainer.SetSize(nregions);
{'fraction_non_alphanumeric': 0.14487926727726894, 'fraction_numerical': 0.007969549185202807, 'mean_word_length': 3.6737076153418564, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4890779', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2525, 'n_words': 791}