openModeller  Version 1.4.0
MeanVarianceNormalizer.cpp
Go to the documentation of this file.
00001 
00027 #include <openmodeller/MeanVarianceNormalizer.hh>
00028 #include <openmodeller/Sampler.hh>
00029 #include <openmodeller/Occurrences.hh>
00030 #include <openmodeller/Log.hh>
00031 
00032 /*******************/
00033 /*** constructor ***/
00034 MeanVarianceNormalizer::MeanVarianceNormalizer() :
00035   _mean(),
00036   _stddev()
00037 {
00038 }
00039 
00040 /*******************/
00041 /*** destructor ***/
00042 MeanVarianceNormalizer::~MeanVarianceNormalizer() {}
00043 
00044 /****************/
00045 /*** get Copy ***/
00046 Normalizer * MeanVarianceNormalizer::getCopy() {
00047 
00048   return new MeanVarianceNormalizer( *this );
00049 }
00050 
00051 /*****************************/
00052 /*** compute Normalization ***/
00053 void MeanVarianceNormalizer::computeNormalization( const ReferenceCountedPointer<const SamplerImpl>& samplerPtr ) {
00054 
00055   int dim = samplerPtr->numIndependent();
00056 
00057   _mean.resize(dim);
00058   _stddev.resize(dim);
00059 
00060   int numPoints = samplerPtr->numPresence() + samplerPtr->numAbsence();
00061 
00062   // Join all occurrences
00063   OccurrencesPtr presences = samplerPtr->getPresences();
00064   OccurrencesPtr absences = samplerPtr->getAbsences();
00065   
00066   OccurrencesPtr allOccs( new OccurrencesImpl( presences->label(), presences->coordSystem() ) );
00067 
00068   allOccs->appendFrom( presences );
00069   allOccs->appendFrom( absences );
00070   
00071   // Compute mean  
00072   OccurrencesImpl::const_iterator p_iterator = allOccs->begin();
00073   OccurrencesImpl::const_iterator p_end = allOccs->end();
00074 
00075   while ( p_iterator != p_end ) {
00076 
00077     Sample point = (*p_iterator)->environment();
00078 
00079     _mean += point;
00080 
00081     ++p_iterator;
00082   }
00083 
00084   _mean /= Scalar( numPoints );
00085 
00086   // Compute standard deviation
00087   p_iterator = allOccs->begin();
00088 
00089   while ( p_iterator != p_end ) {
00090 
00091     Sample point = (*p_iterator)->environment();
00092 
00093     point -= _mean;
00094     point *= point;
00095 
00096     _stddev += point;
00097     
00098     ++p_iterator;
00099   }
00100  
00101   _stddev /= Scalar( numPoints - 1 );
00102   _stddev.sqrt();
00103 }
00104 
00105 /*****************/
00106 /*** normalize ***/
00107 void MeanVarianceNormalizer::normalize( Sample * samplePtr ) {
00108 
00109   if ( samplePtr->size() != 0 ) {
00110 
00111     *samplePtr -= _mean;
00112     *samplePtr /= _stddev;
00113   }
00114 }
00115 
00116 /*************************/
00117 /*** get configuration ***/
00118 ConfigurationPtr MeanVarianceNormalizer::getConfiguration() const {
00119 
00120   ConfigurationPtr config( new ConfigurationImpl("Normalization") );
00121 
00122   config->addNameValue( "Class", "MeanVarianceNormalizer" );
00123 
00124   config->addNameValue( "Mean", _mean );
00125   config->addNameValue( "StdDev", _stddev );
00126 
00127   return config;
00128 }
00129 
00130 /*************************/
00131 /*** set configuration ***/
00132 void MeanVarianceNormalizer::setConfiguration( const ConstConfigurationPtr &config ) {
00133 
00134   _mean   = config->getAttributeAsSample( "Mean" );
00135   _stddev = config->getAttributeAsSample( "StdDev" );
00136 }