TePDIStatistic_test.cpp

Shows how to use this class.

00001 #define TEAGN_ENABLE_STDOUT_LOG
00002 
00003 #include <TePDIExamplesBase.hpp>
00004 
00005 #include <TePDIStatistic.hpp>
00006 #include <TePDIParameters.hpp>
00007 #include <TeAgnostic.h>
00008 
00009 #include <TeInitRasterDecoders.h>
00010 #include <TeRaster.h>
00011 #include <TeProgress.h>
00012 #include <TeStdIOProgress.h>
00013 #include <TeGeometry.h>
00014 #include <TeBox.h>
00015 
00016 void numeric_test_with_polygonset()
00017 {
00018   TePDITypes::TePDIRasterPtrType inRaster1( new TeRaster(
00019     std::string( TEPDIEXAMPLESRESPATH "cbers2b_rgb342_crop.tif" ), 'r' ) );
00020 
00021   TEAGN_TRUE_OR_THROW( inRaster1->init(), "Unable to init inRaster1" );
00022 
00023   TePDITypes::TePDIRasterPtrType inRaster2( new TeRaster(
00024     std::string( TEPDIEXAMPLESRESPATH "cbers2b_rgb342_crop.tif" ), 'r' ) );
00025 
00026   TEAGN_TRUE_OR_THROW( inRaster2->init(), "Unable to init inRaster2" );
00027   
00028   TePDITypes::TePDIRasterPtrType inRaster3( new TeRaster(
00029     std::string( TEPDIEXAMPLESRESPATH "cbers2b_rgb342_crop.tif" ), 'r' ) );
00030 
00031   TEAGN_TRUE_OR_THROW( inRaster3->init(), "Unable to init inRaster2" );  
00032 
00033   TePDIParameters pars;
00034   
00035   TePDITypes::TePDIRasterVectorType rasters;
00036   rasters.push_back( inRaster1 );
00037   rasters.push_back( inRaster2 );
00038   rasters.push_back( inRaster3 );
00039   
00040   pars.SetParameter( "rasters", rasters );
00041   
00042   std::vector< int > bands;
00043   bands.push_back( 0 );
00044   bands.push_back( 1 );
00045   bands.push_back( 2 );
00046   
00047   pars.SetParameter( "bands", bands );
00048   
00049   TeBox box = inRaster1->params().boundingBox();
00050   TePolygon pol = polygonFromBox( box );
00051   TePDITypes::TePDIPolygonSetPtrType polset( new TePolygonSet );
00052   polset->add( pol );
00053   pars.SetParameter( "polygonset", polset );
00054 
00055   TePDIStatistic stat;
00056 
00057   TEAGN_TRUE_OR_THROW( stat.Reset( pars ), "Reset error" );
00058   
00059   TEAGN_CHECK_EPS( stat.getMin( 0 ), 15, 0.0001, "" );
00060   TEAGN_CHECK_EPS( stat.getMin( 1 ), 61, 0.0001, "" );
00061   TEAGN_CHECK_EPS( stat.getMin( 2 ), 10, 0.0001, "" );
00062 
00063   TEAGN_CHECK_EPS( stat.getMax( 0 ), 255, 0.0001, "" );
00064   TEAGN_CHECK_EPS( stat.getMax( 1 ), 255, 0.0001, "" );
00065   TEAGN_CHECK_EPS( stat.getMax( 2 ), 255, 0.0001, "" );
00066   
00067   TEAGN_CHECK_EPS( stat.getMean( 0 ), 134.405948, 0.0001, "" );
00068   TEAGN_CHECK_EPS( stat.getMean( 1 ), 209.620141, 0.0001, "" );
00069   TEAGN_CHECK_EPS( stat.getMean( 2 ), 148.682201, 0.0001, "" );
00070   
00071   TEAGN_CHECK_EPS( stat.getStdDev( 0 ), 46.058313, 0.0001, "" );
00072   TEAGN_CHECK_EPS( stat.getStdDev( 1 ), 29.600182, 0.0001, "" );
00073   TEAGN_CHECK_EPS( stat.getStdDev( 2 ), 36.111183, 0.0001, "" );    
00074 
00075   TEAGN_CHECK_EPS( stat.getVariance( 0 ), 2121.368232, 0.0001, "" );
00076   TEAGN_CHECK_EPS( stat.getVariance( 1 ), 876.170773, 0.0001, "" );
00077   TEAGN_CHECK_EPS( stat.getVariance( 2 ), 1304.017573, 0.0001, "" );
00078   
00079   TEAGN_CHECK_EPS( stat.getCovariance( 0, 0, 0 ), 2121.368232, 0.0001, "" );
00080   TEAGN_CHECK_EPS( stat.getCovariance( 0, 1, 0 ), -135.178860, 0.0001, "" );
00081   TEAGN_CHECK_EPS( stat.getCovariance( 0, 2, 0 ), 1307.875240, 0.0001, "" );
00082   TEAGN_CHECK_EPS( stat.getCovariance( 1, 0, 0 ), -135.178860, 0.0001, "" );
00083   TEAGN_CHECK_EPS( stat.getCovariance( 1, 1, 0 ), 876.170773, 0.0001, "" );
00084   TEAGN_CHECK_EPS( stat.getCovariance( 1, 2, 0 ), 482.378791, 0.0001, "" );
00085   TEAGN_CHECK_EPS( stat.getCovariance( 2, 0, 0 ), 1307.875240, 0.0001, "" );
00086   TEAGN_CHECK_EPS( stat.getCovariance( 2, 1, 0 ), 482.378791, 0.0001, "" );
00087   TEAGN_CHECK_EPS( stat.getCovariance( 2, 2, 0 ), 1304.017573, 0.0001, "" );
00088       
00089   TEAGN_CHECK_EPS( stat.getCorrelation( 0, 0, 0 ), 1, 0.000001, "" );
00090   TEAGN_CHECK_EPS( stat.getCorrelation( 0, 1, 0 ), -0.099153, 0.000001, "" );
00091   TEAGN_CHECK_EPS( stat.getCorrelation( 0, 2, 0 ), 0.786351, 0.000001, "" );
00092   TEAGN_CHECK_EPS( stat.getCorrelation( 1, 0, 0 ), -0.099153, 0.000001, "" );
00093   TEAGN_CHECK_EPS( stat.getCorrelation( 1, 1, 0 ), 1, 0.000001, "" );
00094   TEAGN_CHECK_EPS( stat.getCorrelation( 1, 2, 0 ), 0.451286, 0.000001, "" );
00095   TEAGN_CHECK_EPS( stat.getCorrelation( 2, 0, 0 ), 0.786351, 0.000001, "" );
00096   TEAGN_CHECK_EPS( stat.getCorrelation( 2, 1, 0 ), 0.451286, 0.000001, "" );      
00097   TEAGN_CHECK_EPS( stat.getCorrelation( 2, 2, 0 ), 1, 0.000001, "" );            
00098   
00099   TEAGN_CHECK_EPS( stat.getSum( 0 ), 17408124.000000, 0.0001, "" );
00100   TEAGN_CHECK_EPS( stat.getSum( 1 ), 27149791.000000, 0.0001, "" );
00101   TEAGN_CHECK_EPS( stat.getSum( 2 ), 19257170.000000, 0.0001, "" );
00102   
00103   TEAGN_CHECK_EPS( stat.getSum3( 0 ), 428939435700.000000, 0.0001, "" );
00104   TEAGN_CHECK_EPS( stat.getSum3( 1 ), 1262662900207.000000, 0.0001, "" );
00105   TEAGN_CHECK_EPS( stat.getSum3( 2 ), 499433085194.000000, 0.0001, "" );
00106 
00107   TEAGN_CHECK_EPS( stat.getSum4( 0 ), 75997706109496.000000, 0.0001, "" );
00108   TEAGN_CHECK_EPS( stat.getSum4( 1 ), 278887661881795.000000, 0.0001, "" );
00109   TEAGN_CHECK_EPS( stat.getSum4( 2 ), 85455958573178.000000, 0.0001, "" );    
00110   
00111   TEAGN_CHECK_EPS( stat.getEntropy( 0 ), 5.110813, 0.0001, "" );
00112   TEAGN_CHECK_EPS( stat.getEntropy( 1 ), 5.304196, 0.0001, "" );
00113   TEAGN_CHECK_EPS( stat.getEntropy( 2 ), 3.823902, 0.0001, "" );    
00114   
00115   TEAGN_CHECK_EPS( stat.getMode( 0 ), 133.000000, 0.0001, "" );
00116   TEAGN_CHECK_EPS( stat.getMode( 1 ), 255.000000, 0.0001, "" );
00117   TEAGN_CHECK_EPS( stat.getMode( 2 ), 163.000000, 0.0001, "" );
00118   
00119   TEAGN_CHECK_EPS( stat.getPercentile( 0., 0, 0 ), 15.000000, 0, 
00120     "Invalid Percentile" );
00121   TEAGN_CHECK_EPS( stat.getPercentile( 50., 0, 0 ), 133.000000, 0, 
00122     "Invalid Percentile" );
00123   TEAGN_CHECK_EPS( stat.getPercentile( 75., 0, 0 ), 158.000000, 0, 
00124     "Invalid Percentile" );
00125   TEAGN_CHECK_EPS( stat.getPercentile( 100., 0, 0 ), 255, 0, 
00126     "Invalid Percentile" );
00127      
00128   TeMatrix VarMatrix = stat.getVarMatrix();
00129 
00130   TeMatrix StdDevMatrix = stat.getStdDevMatrix();
00131   
00132   TeMatrix CMMatrix = stat.getCMMatrix();
00133   TEAGN_CHECK_EPS( CMMatrix( 0, 0 ), 2121.368232, 0.0001, "" );
00134   TEAGN_CHECK_EPS( CMMatrix( 0, 1 ), 876.170773, 0.000001, "" );
00135   TEAGN_CHECK_EPS( CMMatrix( 1, 0 ), 28376.279826, 0.00001, "" );
00136   TEAGN_CHECK_EPS( CMMatrix( 1, 1 ), -12962.252229, 0.000001, "" );
00137   TEAGN_CHECK_EPS( CMMatrix( 2, 0 ), 15235700.947282, 0.00001, "" );
00138   TEAGN_CHECK_EPS( CMMatrix( 2, 1 ), 2351893.787613, 0.00001, "" );
00139 
00140   TeMatrix CovMatrix = stat.getCovMatrix();
00141 
00142   TeMatrix CorMatrix = stat.getCorMatrix();
00143     
00144   TeMatrix AssMatrix = stat.getAssimetryMatrix();
00145   TEAGN_CHECK_EPS( AssMatrix( 0, 0 ), 0.290423, 0.000001, "" );
00146   TEAGN_CHECK_EPS( AssMatrix( 0, 1 ), -0.499801, 0.000001, "" );
00147 
00148   TeMatrix KurMatrix = stat.getKurtosisMatrix();
00149   TEAGN_CHECK_EPS( KurMatrix( 0, 0 ), 0.385558, 0.000001, "" );
00150   TEAGN_CHECK_EPS( KurMatrix( 0, 1 ), 0.063657, 0.000001, "" );
00151 
00152   TeMatrix VarCoefMatrix = stat.getVarCoefMatrix();
00153   TEAGN_CHECK_EPS( VarCoefMatrix( 0, 0 ), 0.342681, 0.000001, "" );
00154   TEAGN_CHECK_EPS( VarCoefMatrix( 0, 1 ), 0.141209, 0.000001, "" );
00155 }
00156 
00157 
00158 void numeric_with_histograms_test()
00159 {
00160   TePDITypes::TePDIRasterPtrType inRaster1( new TeRaster(
00161     std::string( TEPDIEXAMPLESRESPATH "cbers_b2_crop.tif" ), 'r' ) );
00162 
00163   TEAGN_TRUE_OR_THROW( inRaster1->init(), "Unable to init inRaster1" );
00164 
00165   TePDITypes::TePDIRasterPtrType inRaster2( new TeRaster(
00166     std::string( TEPDIEXAMPLESRESPATH "cbers_b2_crop_contraste.tif" ), 'r' ) );
00167 
00168   TEAGN_TRUE_OR_THROW( inRaster2->init(), "Unable to init inRaster2" );
00169   
00170   /* Building histograms */
00171   
00172   std::vector< TePDIHistogram::pointer > histograms;
00173   
00174   TePDIHistogram::pointer user_hist1_ptr( new TePDIHistogram );
00175   TEAGN_TRUE_OR_THROW( user_hist1_ptr->reset( inRaster1, 0, 0, 
00176     TeBoxPixelIn ),
00177     "Histogram generation error" );
00178   histograms.push_back( user_hist1_ptr );
00179     
00180   TePDIHistogram::pointer user_hist2_ptr( new TePDIHistogram );
00181   TEAGN_TRUE_OR_THROW( user_hist2_ptr->reset( inRaster2, 0, 0, 
00182     TeBoxPixelIn ),
00183     "Histogram generation error" );
00184   histograms.push_back( user_hist2_ptr );
00185   
00186   /* Building statistic parameters */
00187 
00188   TePDIParameters pars;
00189   
00190   pars.SetParameter( "histograms", histograms );
00191   
00192   TePDITypes::TePDIRasterVectorType rasters;
00193   rasters.push_back( inRaster1 );
00194   rasters.push_back( inRaster2 );
00195   
00196   pars.SetParameter( "rasters", rasters );
00197   
00198   std::vector< int > bands;
00199   bands.push_back( 0 );
00200   bands.push_back( 0 );
00201   
00202   pars.SetParameter( "bands", bands );
00203   
00204   TePDIStatistic stat;
00205 
00206   TEAGN_TRUE_OR_THROW( stat.Reset( pars ), "Reset error" );
00207   
00208   TePDIHistogram hist1 = stat.getHistogram( 0 );
00209   TEAGN_TRUE_OR_THROW( ( hist1.size() == user_hist1_ptr->size() ), 
00210     "Invalid histogram1 size" );
00211 
00212   TePDIHistogram hist2 = stat.getHistogram( 1 );
00213   TEAGN_TRUE_OR_THROW( ( hist2.size() == user_hist2_ptr->size() ), 
00214     "Invalid histogram2 size" );
00215       
00216   TEAGN_CHECK_EPS( stat.getSum( 0 ), 55438975.000000, 0.0001, "" );
00217   TEAGN_CHECK_EPS( stat.getSum( 1 ), 87782597.000000, 0.0001, "" );
00218 
00219   TEAGN_CHECK_EPS( stat.getSum3( 0 ), 340982998465.000000, 0.0001, "" );
00220   TEAGN_CHECK_EPS( stat.getSum3( 1 ), 1689469326995.000000, 0.0001, "" );
00221 
00222   TEAGN_CHECK_EPS( stat.getSum4( 0 ), 27381030612693.000000, 0.0001, "" );
00223   TEAGN_CHECK_EPS( stat.getSum4( 1 ), 252981440505241.000000, 0.0001, "" );  
00224     
00225   TEAGN_CHECK_EPS( stat.getMean( 0 ), 76.928379, 0.0001, "" );
00226   TEAGN_CHECK_EPS( stat.getMean( 1 ), 121.809123, 0.0001, "" );
00227 
00228   TEAGN_CHECK_EPS( stat.getVariance( 0 ), 75.432528, 0.0001, "" );
00229   TEAGN_CHECK_EPS( stat.getVariance( 1 ), 1561.214764, 0.0001, "" );
00230   
00231   TEAGN_CHECK_EPS( stat.getStdDev( 0 ), 8.685190, 0.0001, "" );
00232   TEAGN_CHECK_EPS( stat.getStdDev( 1 ), 39.512210, 0.0001, "" );
00233   
00234   TEAGN_CHECK_EPS( stat.getEntropy( 0 ), 5.004244, 0.0001, "" );
00235   TEAGN_CHECK_EPS( stat.getEntropy( 1 ), 4.998273, 0.0001, "" );  
00236   
00237   TEAGN_CHECK_EPS( stat.getMin( 0 ), 47, 0.0001, "" );
00238   TEAGN_CHECK_EPS( stat.getMin( 1 ), 0, 0.0001, "" );
00239 
00240   TEAGN_CHECK_EPS( stat.getMax( 0 ), 255, 0.0001, "" );
00241   TEAGN_CHECK_EPS( stat.getMax( 1 ), 255, 0.0001, "" );
00242 
00243   TEAGN_CHECK_EPS( stat.getMode( 0 ), 81, 0.0001, "" );
00244   TEAGN_CHECK_EPS( stat.getMode( 1 ), 141, 0.0001, "" );
00245   
00246   TEAGN_CHECK_EPS( stat.getPercentile( 0., 0, 0 ), 47, 0, 
00247     "Invalid Percentile" );
00248   TEAGN_CHECK_EPS( stat.getPercentile( 50., 0, 0 ), 78, 0, 
00249     "Invalid Percentile" );
00250   TEAGN_CHECK_EPS( stat.getPercentile( 51., 0, 0 ), 78, 0, 
00251     "Invalid Percentile" );
00252   TEAGN_CHECK_EPS( stat.getPercentile( 100., 0, 0 ), 255, 0, 
00253     "Invalid Percentile" );  
00254           
00255   TEAGN_CHECK_EPS( stat.getCovariance( 0, 0 ),
00256     75.432528, 0.001, "" );
00257     
00258   TEAGN_CHECK_EPS( stat.getCovariance( 0, 1 ),
00259     335.166, 0.001, "" );
00260 
00261   TEAGN_CHECK_EPS( stat.getCovariance( 1, 0 ),
00262     335.166, 0.001, "" );
00263         
00264   TEAGN_CHECK_EPS( stat.getCovariance( 1, 1 ),
00265     1561.214764, 0.001, "" );
00266     
00267   TEAGN_CHECK_EPS( stat.getCorrelation( 0, 0 ),
00268      1.0, 0.000001, "" );
00269 
00270   TEAGN_CHECK_EPS( stat.getCorrelation( 0, 1 ),
00271      0.976674, 0.000001, "" );
00272 
00273   TEAGN_CHECK_EPS( stat.getCorrelation( 1, 0 ),
00274      0.976674, 0.000001, "" );
00275      
00276   TeMatrix VarMatrix = stat.getVarMatrix();
00277   TEAGN_CHECK_EPS( VarMatrix( 0, 0 ), 75.432528, 0.000001, "" );
00278   TEAGN_CHECK_EPS( VarMatrix( 1, 0 ), 1561.214764, 0.000001, "" );  
00279 
00280   TeMatrix StdDevMatrix = stat.getStdDevMatrix();
00281   TEAGN_CHECK_EPS( StdDevMatrix( 0, 0 ), 8.685190, 0.000001, "" );
00282   TEAGN_CHECK_EPS( StdDevMatrix( 1, 0 ), 39.512210, 0.000001, "" );  
00283   
00284   TeMatrix CMMatrix = stat.getCMMatrix();
00285   TEAGN_CHECK_EPS( CMMatrix( 0, 0 ), 75.432528, 0.0001, "" );
00286   TEAGN_CHECK_EPS( CMMatrix( 0, 1 ), 1561.214764, 0.000001, "" );
00287   TEAGN_CHECK_EPS( CMMatrix( 1, 0 ), 486.770730, 0.00001, "" );
00288   TEAGN_CHECK_EPS( CMMatrix( 1, 1 ), -33502.920720, 0.000001, "" );
00289   TEAGN_CHECK_EPS( CMMatrix( 2, 0 ), 143871.693264, 0.00001, "" );
00290   TEAGN_CHECK_EPS( CMMatrix( 2, 1 ), 8229548.372509, 0.000001, "" );
00291 
00292   TeMatrix CovMatrix = stat.getCovMatrix();
00293   TEAGN_CHECK_EPS( CovMatrix( 0, 0 ), 75.432528, 0.000001, "" );  
00294   TEAGN_CHECK_EPS( CovMatrix( 0, 1 ), 335.166257, 0.000001, "" );  
00295   TEAGN_CHECK_EPS( CovMatrix( 1, 0 ), 335.166257, 0.000001, "" );  
00296   TEAGN_CHECK_EPS( CovMatrix( 1, 1 ), 1561.214764, 0.000001, "" );  
00297 
00298   TeMatrix CorMatrix = stat.getCorMatrix();
00299   TEAGN_CHECK_EPS( CorMatrix( 0, 0 ), 1.0, 0.000001, "" );  
00300   TEAGN_CHECK_EPS( CorMatrix( 0, 1 ), 0.976674, 0.000001, "" );  
00301   TEAGN_CHECK_EPS( CorMatrix( 1, 0 ), 0.976674, 0.000001, "" );  
00302   TEAGN_CHECK_EPS( CorMatrix( 1, 1 ), 1.0, 0.000001, "" );  
00303     
00304   TeMatrix AssMatrix = stat.getAssimetryMatrix();
00305   TEAGN_CHECK_EPS( AssMatrix( 0, 0 ), 0.742996, 0.000001, "" );
00306   TEAGN_CHECK_EPS( AssMatrix( 0, 1 ), -0.543111, 0.000001, "" );
00307 
00308   TeMatrix KurMatrix = stat.getKurtosisMatrix();
00309   TEAGN_CHECK_EPS( KurMatrix( 0, 0 ), 22.284713, 0.000001, "" );
00310   TEAGN_CHECK_EPS( KurMatrix( 0, 1 ), 0.376375, 0.000001, "" );
00311 
00312   TeMatrix VarCoefMatrix = stat.getVarCoefMatrix();
00313   TEAGN_CHECK_EPS( VarCoefMatrix( 0, 0 ), 0.112900, 0.000001, "" );
00314   TEAGN_CHECK_EPS( VarCoefMatrix( 0, 1 ), 0.324378, 0.000001, "" );
00315 }
00316 
00317 
00318 int main()
00319 {
00320   TEAGN_LOGMSG( "Test started." );
00321 
00322   try{
00323     TeStdIOProgress pi;
00324     TeProgress::setProgressInterf( dynamic_cast< TeProgressBase* >( &pi ) );
00325       
00326     TeInitRasterDecoders();
00327 
00328     numeric_test_with_polygonset();
00329     numeric_with_histograms_test();
00330   }
00331   catch( const TeException& e ){
00332     TEAGN_LOGERR( "Test Failed - " + e.message() );
00333     return EXIT_FAILURE;
00334   }
00335 
00336   TEAGN_LOGMSG( "Test OK." );
00337   return EXIT_SUCCESS;
00338 }

Generated on Sun Jul 29 04:01:03 2012 for TerraLib - Development Source by  doxygen 1.5.3