1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkNthElementTest1.cxx,v $
Language: C++
Date: $Date: 2008-04-30 06:06:31 $
Version: $Revision: 1.2 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkListSample.h"
#include "itkStatisticsAlgorithm.h"
#include "itkMersenneTwisterRandomVariateGenerator.h"
#include <fstream>
#include <vector>
#include <algorithm>
int itkNthElementTest1(int argc, char * argv [] )
{
std::cout << "Statistics Algorithm Test \n \n";
bool pass = true;
typedef float MeasurementType;
const unsigned int Dimension = 1;
const unsigned int testDimension = 0;
typedef itk::FixedArray< MeasurementType, Dimension > MeasurementVectorType;
typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
typedef itk::Statistics::Subsample< SampleType > SubsampleType;
SampleType::Pointer sample = SampleType::New();
sample->SetMeasurementVectorSize( Dimension );
typedef std::vector< MeasurementType > VerificationVectorType;
VerificationVectorType verificationVector;
if( argc > 1 )
{
//
// Read the values from a file
//
std::cout << "Reading input file " << argv[1] << std::endl;
std::ifstream valuesFile;
valuesFile.open( argv[1] );
MeasurementVectorType vector;
valuesFile >> vector[testDimension];
while( !valuesFile.eof() )
{
sample->PushBack( vector );
MeasurementType value;
valuesFile >> value;
vector[testDimension] = value;
verificationVector.push_back( value );
}
valuesFile.close();
}
else
{
//
// Generate values using a Random number generator
//
typedef itk::Statistics::MersenneTwisterRandomVariateGenerator NumberGeneratorType;
NumberGeneratorType::Pointer randomNumberGenerator = NumberGeneratorType::New();
randomNumberGenerator->Initialize();
unsigned int numberOfValues = 100;
MeasurementVectorType vector;
for(unsigned int i=0; i<numberOfValues; i++)
{
MeasurementType value = randomNumberGenerator->GetNormalVariate( 0.0, 1.0 );
vector[testDimension] = value;
sample->PushBack( vector );
verificationVector.push_back( value );
}
}
SubsampleType::Pointer subsample1 = SubsampleType::New();
subsample1->SetSample( sample );
subsample1->InitializeWithAllInstances();
// Sort all the values in subsample1
itk::Statistics::HeapSort< SubsampleType >( subsample1, testDimension, 0, subsample1->Size());
try
{
//
// Test the NthElement Algorithm by asking k-th elements in subsample2 and
// comparing them to the entries in the sorted subsample1.
//
for( unsigned int kth = 0; kth < subsample1->Size(); kth++)
{
SubsampleType::Pointer subsample2 = SubsampleType::New();
subsample2->SetSample( sample );
subsample2->InitializeWithAllInstances();
MeasurementType kthValue1 = itk::Statistics::NthElement< SubsampleType >(
subsample2, testDimension, 0, subsample2->Size(), kth );
MeasurementType kthValue2 =
subsample1->GetMeasurementVectorByIndex( kth )[testDimension];
for(unsigned int i=0; i<sample->Size(); i++)
{
verificationVector[i] = sample->GetMeasurementVector(i)[testDimension];
}
std::nth_element( verificationVector.begin(),
verificationVector.begin() + kth,
verificationVector.end() );
MeasurementType kthValue3 = verificationVector[kth];
if( vnl_math_abs( kthValue1 - kthValue2 ) > vnl_math::eps )
{
std::cerr << "Comparison failed for component kth= " << kth << std::endl;
pass = false;
}
if( vnl_math_abs( kthValue1 - kthValue3 ) > vnl_math::eps )
{
std::cerr << "Comparison with std::nth_element failed for component kth= " << kth << std::endl;
for(unsigned int k=0; k<verificationVector.size(); k++)
{
std::cerr << "STL: " << verificationVector[k] << " : " << subsample2->GetMeasurementVectorByIndex(k)[testDimension] << std::endl;
}
pass = false;
}
}
}
catch( itk::ExceptionObject & excp )
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
if( !pass )
{
std::cerr << "Test FAILED " << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test passed." << std::endl;
return EXIT_SUCCESS;
}
|