| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/kernel/TeStatistics.h
Go to the documentation of this file.

    00001 /************************************************************************************
    00002 TerraLib - a library for developing GIS applications.
    00003 Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio.
    00004
    00005 This code is part of the TerraLib library.
    00006 This library is free software; you can redistribute it and/or
    00007 modify it under the terms of the GNU Lesser General Public
    00008 License as published by the Free Software Foundation; either
    00009 version 2.1 of the License, or (at your option) any later version.
    00010
    00011 You should have received a copy of the GNU Lesser General Public
    00012 License along with this library.
    00013
    00014 The authors reassure the license terms regarding the warranties.
    00015 They specifically disclaim any warranties, including, but not limited to,
    00016 the implied warranties of merchantability and fitness for a particular purpose.
    00017 The library provided hereunder is on an "as is" basis, and the authors have no
    00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
    00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
    00020 indirect, special, incidental, or consequential damages arising out of the use
    00021 of this library and its documentation.
    00022 *************************************************************************************/
    00027 #ifndef TeStatistics_H
    00028 #define TeStatistics_H
    00029
    00030 #include "TeDataTypes.h"
    00031
    00032 #include <string>
    00033 #include <map>
    00034
    00035 using namespace std;
    00036
    00040
    00041 typedef vector<double>           stats;
    00042
    00044 struct TL_DLL TeStatisticsDimension
    00045 {
    00046            int                                                    dimension_;
    00047            TeStatisticValMap     stat_;
    00048
    00049            TeStatisticsDimension(int d, TeStatisticValMap& st):
    00050            dimension_(d),
    00051            stat_(st)
    00052            {}
    00053 };
    00054
    00056 typedef vector<TeStatisticsDimension>       TeStatisticsDimensionVect;
    00057
    00059 typedef map<double, int> TeHistogram;
    00060
    00061
    00067 template<typename T>
    00068 bool TeCalculateHistogram(const T& itBegin, const T& itEnd, TeHistogram& histOut)
    00069 {
    00070            T it = itBegin;
    00071
    00072            //initialization
    00073            while(it!=itEnd)
    00074            {
    00075                       double val = (*it);
    00076                       histOut[val] = 0;
    00077                       it++;
    00078            }
    00079
    00080            it= itBegin;
    00081
    00082            //calcule histogram
    00083            while(it!=itEnd)
    00084            {
    00085                       double val = (*it);
    00086                       histOut[val]++;
    00087                       it++;
    00088            }
    00089
    00090            return true;
    00091 }
    00092
    00099 template<typename It>
    00100 bool TeCalculateStatistics(const It& itBegin, const It& itEnd, TeStatisticValMap& stat, int dim)
    00101 {
    00102            double     sum, mean, minValue, maxValue, variance, assim,
    00103                                  curtose, stDev, coefVar, amplitude, moda, median;
    00104            sum=mean=variance=assim=curtose=stDev=coefVar=amplitude=0.0;
    00105
    00106            minValue = TeMAXFLOAT;
    00107            maxValue = -minValue ;
    00108
    00109            vector<double> values;
    00110            int count=0;
    00111            int countTotal = 0;
    00112            It itt = itBegin;
    00113            while(itt != itEnd)
    00114     {
    00115                       double val;
    00116                       val = (*itt)[dim];
    00117
    00118                       if(val!=TeMAXFLOAT)  // MAXFLOAT is considered invalid value
    00119                       {
    00120                                  values.push_back (val);
    00121                                  sum += val;
    00122
    00123                                  if(minValue>val)
    00124                                             minValue = val;
    00125                                  if(maxValue<val)
    00126                                             maxValue = val;
    00127
    00128                                  ++count;
    00129                                  mean = sum/count;
    00130                       }
    00131
    00132                       ++itt;
    00133                       ++countTotal;
    00134            }
    00135
    00136            for(int i=0; i<count; i++)
    00137            {
    00138                       double v= values[i];
    00139                       variance += pow((v-mean),2);
    00140                       assim += pow((v-mean),3);
    00141                       curtose += pow((v-mean),4);
    00142            }
    00143
    00144            if(!count)
    00145                       return false;
    00146
    00147            variance /= count;
    00148            stDev = pow(variance,0.5);
    00149            assim /= count;
    00150            assim /= pow(stDev,3);
    00151            curtose /= count;
    00152            curtose /= pow(stDev,4);
    00153
    00154            coefVar = (100*stDev)/mean;
    00155            amplitude = maxValue-minValue;
    00156
    00157            sort(values.begin(), values.end());
    00158
    00159            //calculate median
    00160            if((count%2)==0)
    00161                       median = (values[(count/2)]+values[(count/2-1)])/2;
    00162            else
    00163                       median = values[(count-1)/2];
    00164
    00165            //calculate the mode
    00166            TeHistogram histog;
    00167            TeCalculateHistogram(values.begin(),values.end(), histog);
    00168
    00169            TeHistogram::iterator itHist = histog.begin();
    00170            int nCount=0;
    00171            while(itHist!=histog.end())
    00172            {
    00173                       int hCount = itHist->second;
    00174                       if (hCount > nCount)
    00175                       {
    00176                                  nCount = hCount;
    00177                                  moda = itHist->first;
    00178                       }
    00179                       itHist++;
    00180            }
    00181
    00182            //fill results
    00183            stat[TeCOUNT] = countTotal;
    00184            stat[TeVALIDCOUNT] = count;
    00185            stat[TeMINVALUE] = minValue;
    00186            stat[TeMAXVALUE] = maxValue;
    00187            stat[TeSUM] = sum;
    00188            stat[TeMEAN] = mean;
    00189            stat[TeSTANDARDDEVIATION] = stDev;
    00190            stat[TeVARIANCE] = variance;
    00191            stat[TeSKEWNESS] = assim;
    00192            stat[TeKURTOSIS] = curtose;
    00193            stat[TeAMPLITUDE] = amplitude;
    00194            stat[TeMEDIAN] = median;
    00195            stat[TeVARCOEFF] = coefVar;
    00196            stat[TeMODE] = moda;
    00197
    00198            return true;
    00199 }
    00200
    00201
    00207 template<typename It>
    00208 bool TeCalculateStatistics(const It& itBegin, const It& itEnd, TeStatisticStringValMap& stat, const double &dummy=255, const bool &useDummy=true)
    00209 {
    00210            string min, max, val;
    00211            int        count = 0;
    00212
    00213            It it = itBegin;
    00214            int totalCount = 0;
    00215
    00216            if(it != itEnd)
    00217                       min = max = (*it);
    00218            while(it != itEnd)
    00219            {
    00220                       val = (*it);
    00221                       ++it;
    00222                       if((useDummy == false) && (!val.empty()))
    00223                       {
    00224                                  if(val.compare(Te2String(dummy))==0)
    00225                                             continue;
    00226                       }
    00227                       if(val.empty() == false)
    00228                       {
    00229                                  count++;
    00230                                  min = MIN(min, val);
    00231                                  max = MAX(max, val);
    00232                       }
    00233                       ++totalCount;
    00234            }
    00235
    00236            stat[TeMINVALUE] = min;
    00237            stat[TeMAXVALUE] = max;
    00238            stat[TeCOUNT] = Te2String(totalCount);
    00239            stat[TeVALIDCOUNT] = Te2String(count);
    00240            return true;
    00241 }
    00242
    00243
    00249 template<typename It>
    00250 bool TeCalculateStatistics(const It& itBegin, const It& itEnd, TeStatisticValMap& stat, const double &dummy=255, const bool &useDummy=true)
    00251 {
    00252            double     sum, mean, minValue, maxValue, variance, assim,
    00253                                  curtose, stDev, coefVar, amplitude, moda, median;
    00254            sum=mean=variance=assim=curtose=stDev=coefVar=amplitude=moda=0.0;
    00255
    00256            minValue = TeMAXFLOAT;
    00257            maxValue = -minValue;
    00258
    00259            vector<double>        values;
    00260
    00261            int        totalCount = 0;
    00262            int count=0;
    00263            It itt = itBegin;
    00264            while(itt != itEnd)
    00265     {
    00266                       double val = (*itt);
    00267                       ++itt;
    00268                       if((useDummy == false) && (val == dummy))
    00269                       {
    00270                                  continue;
    00271                       }
    00272                       if(val != TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00273                       {
    00274                                  values.push_back (val);
    00275                                  sum += val;
    00276                                  minValue = MIN(minValue, val);
    00277                                  maxValue = MAX(maxValue, val);
    00278                       }
    00279                       ++totalCount;
    00280            }
    00281
    00282            count = values.size();
    00283            mean = sum/count;
    00284
    00285            for(int i=0; i<count; i++)
    00286            {
    00287                       double v= values[i];
    00288                       variance += pow((v-mean),2);
    00289                       assim += pow((v-mean),3);
    00290                       curtose += pow((v-mean),4);
    00291            }
    00292
    00293            if(!count)
    00294                       return false;
    00295
    00296            variance /= count;
    00297            stDev = pow(variance,0.5);
    00298            assim /= count;
    00299            assim /= pow(stDev,3);
    00300            curtose /= count;
    00301            curtose /= pow(stDev,4);
    00302
    00303            coefVar = (100*stDev)/mean;
    00304            amplitude = maxValue-minValue;
    00305
    00306            sort(values.begin(), values.end());
    00307
    00308            //calculate median
    00309            if((count%2)==0)
    00310                       median = (values[(count/2)]+values[(count/2-1)])/2;
    00311            else
    00312                       median = values[(count-1)/2];
    00313
    00314            //calculate the mode
    00315            TeHistogram histog;
    00316            TeCalculateHistogram(values.begin(),values.end(), histog);
    00317
    00318            TeHistogram::iterator itHist = histog.begin();
    00319            int nCount=0;
    00320            while(itHist!=histog.end())
    00321            {
    00322                       int hCount = itHist->second;
    00323                       if (hCount > nCount)
    00324                       {
    00325                                  nCount = hCount;
    00326                                  moda = itHist->first;
    00327                       }
    00328                       itHist++;
    00329            }
    00330
    00331            //fill results
    00332            stat[TeVALIDCOUNT] = count;
    00333            stat[TeCOUNT] = totalCount;
    00334            stat[TeMINVALUE] = minValue;
    00335            stat[TeMAXVALUE] = maxValue;
    00336            stat[TeSUM] = sum;
    00337            stat[TeMEAN] = mean;
    00338            stat[TeSTANDARDDEVIATION] = stDev;
    00339            stat[TeVARIANCE] = variance;
    00340            stat[TeSKEWNESS] = assim;
    00341            stat[TeKURTOSIS] = curtose;
    00342            stat[TeAMPLITUDE] = amplitude;
    00343            stat[TeMEDIAN] = median;
    00344            stat[TeVARCOEFF] = coefVar;
    00345            stat[TeMODE] = moda;
    00346
    00347            return true;
    00348 }
    00349
    00350 template<typename It>
    00351 bool TeCalculateStatistics(It& itBegin, It& itEnd, TeStatisticsDimensionVect& stat)
    00352 {
    00353            std::vector<double> dummy;
    00354            return TeCalculateStatistics(itBegin, itEnd,  stat, dummy);
    00355 }
    00356
    00357
    00363 template<typename It>
    00364 bool TeCalculateStatistics(It& itBegin, It& itEnd, TeStatisticsDimensionVect& stat, std::vector<double> dummy,const bool &useDummy=true)
    00365 {
    00366
    00367            vector<double>        sum, mean, minValue, maxValue, variance, assim, curtose,
    00368                                                        stDev, coefVar, amplitude, moda, median;
    00369
    00370            vector<double>        values;
    00371            bool                             noDummy;
    00372
    00373            int nb = itBegin.nBands();
    00374
    00375            //initialization of the vetors
    00376            for (int i=0; i<nb; i++)
    00377            {
    00378                       sum.push_back(0.0);
    00379                       mean.push_back(0.0);
    00380                       variance.push_back(0.0);
    00381                       assim.push_back(0.0);
    00382                       curtose.push_back(0.0);
    00383                       stDev.push_back(0.0);
    00384                       coefVar.push_back(0.0);
    00385                       amplitude.push_back(0.0);
    00386                       median.push_back (0.0);
    00387                       moda.push_back (0.0);
    00388                       minValue.push_back(TeMAXFLOAT);
    00389                       maxValue.push_back(TeMINFLOAT);
    00390            }
    00391
    00392            map<int,stats> bandValues;
    00393
    00394            int count=0;
    00395            int validCount=0;
    00396            It itt = itBegin;
    00397
    00398            while(itt != itEnd)
    00399     {
    00400                       noDummy=false;
    00401                       values.clear();
    00402                       for(int j=0; j<nb; j++)
    00403                       {
    00404                                  double val = itt.operator* (j);
    00405                                  values.push_back(val);
    00406                                  if(val!=dummy[j]) noDummy=true;
    00407                       }
    00408                       ++itt;
    00409                       if((useDummy==false) && (noDummy==false))
    00410                       {
    00411                                  continue;
    00412                       }
    00413
    00414                       for (int j=0; j<nb; j++)
    00415                       {
    00416                                  double val = values[j];
    00417                                  if(val!=TeMAXFLOAT)  // MAXFLOAT is considered invalid value
    00418                                  {
    00419                                             bandValues[j].push_back(val);
    00420                                             sum[j] += val;
    00421
    00422                                             if(minValue[j]>val)
    00423                                                        minValue[j] = val;
    00424                                             if(maxValue[j]<val)
    00425                                                        maxValue[j] = val;
    00426
    00427                                             int size = bandValues[j].size();
    00428                                             mean[j] = sum[j]/size;
    00429                                             validCount++;
    00430                                  }
    00431                       }
    00432                       ++count;
    00433            }
    00434
    00435            if(!count)
    00436                       return false;
    00437
    00438            for (int jj=0; jj<nb; jj++)
    00439            {
    00440                       for(int i=0; i<count; i++)
    00441                       {
    00442                                  double v = bandValues[jj][i];
    00443                                  variance[jj] += pow((v-mean[jj]),2);
    00444                                  assim[jj] += pow((v-mean[jj]),3);
    00445                                  curtose[jj] += pow((v-mean[jj]),4);
    00446                       }
    00447
    00448                       variance[jj] /= count;
    00449                       stDev[jj] = pow(variance[jj],0.5);
    00450                       assim[jj] /= count;
    00451                       assim[jj] /= pow(stDev[jj],3);
    00452                       curtose[jj] /= count;
    00453                       curtose[jj] /= pow(stDev[jj],4);
    00454
    00455                       coefVar[jj] = (100*stDev[jj])/mean[jj];
    00456                       amplitude[jj] = maxValue[jj]-minValue[jj];
    00457
    00458                       sort(bandValues[jj].begin(), bandValues[jj].end());
    00459
    00460                       //calculate median
    00461                       if((count%2)==0)
    00462                                  median[jj] = ((bandValues[jj][(count/2)])+(bandValues[jj][(count/2-1)]))/2;
    00463                       else
    00464                                  median[jj] = bandValues[jj][(count-1)/2];
    00465
    00466                       //calculate the mode
    00467                       TeHistogram histog;
    00468                       TeCalculateHistogram(bandValues[jj].begin(),bandValues[jj].end(), histog);
    00469
    00470                       TeHistogram::iterator itHist = histog.begin();
    00471                       int nCount=0;
    00472                       while(itHist!=histog.end())
    00473                       {
    00474                                  int hCount = itHist->second;
    00475                                  if (hCount > nCount)
    00476                                  {
    00477                                             nCount = hCount;
    00478                                             moda[jj] = itHist->first;
    00479                                  }
    00480                                  itHist++;
    00481                       }
    00482
    00483                       //fill results
    00484                       TeStatisticValMap statVal;
    00485
    00486                       statVal[TeCOUNT] = count;
    00487                       statVal[TeVALIDCOUNT]=validCount;
    00488                       statVal[TeMINVALUE] = minValue[jj];
    00489                       statVal[TeMAXVALUE] = maxValue[jj];
    00490                       statVal[TeSUM] = sum[jj];
    00491                       statVal[TeMEAN] = mean[jj];
    00492                       statVal[TeSTANDARDDEVIATION] = stDev[jj];
    00493                       statVal[TeVARIANCE] = variance[jj];
    00494                       statVal[TeSKEWNESS] = assim[jj];
    00495                       statVal[TeKURTOSIS] = curtose[jj];
    00496                       statVal[TeAMPLITUDE] = amplitude[jj];
    00497                       statVal[TeMEDIAN] = median[jj];
    00498                       statVal[TeVARCOEFF] = coefVar[jj];
    00499                       statVal[TeMODE] = moda[jj];
    00500
    00501                       TeStatisticsDimension statBand(jj, statVal);
    00502                       stat.push_back(statBand);
    00503            }
    00504
    00505            return true;
    00506 }
    00507
    00508
    00510 template <typename It>
    00511 double TeSum (It begin, It end, int dim)
    00512 {
    00513            double sum = 0.;
    00514            while ( begin != end)
    00515            {
    00516                       if(((*begin)[dim])!= TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00517                                  sum += (*begin)[dim];
    00518
    00519                       ++begin;
    00520            }
    00521            return sum;
    00522 }
    00523
    00524
    00526 template <typename It>
    00527 double TeSum (It begin, It end)
    00528 {
    00529            double sum = 0.;
    00530            while ( begin != end)
    00531            {
    00532                       if((*begin)!= TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00533                                  sum += (*begin);
    00534
    00535                       ++begin;
    00536            }
    00537            return sum;
    00538 }
    00539
    00540
    00542 template <typename It>
    00543 double TeFirstMoment (It begin, It end, int dim)
    00544 {
    00545            int number = 0;
    00546            double mean = 0.;
    00547            while (begin != end)
    00548            {
    00549                       if(((*begin)[dim])!= TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00550                       {
    00551                                  mean += (*begin)[dim];
    00552                                  number++;
    00553                       }
    00554                       ++begin;
    00555            }
    00556            return mean /= number;
    00557 }
    00558
    00559
    00561 template <typename It>
    00562 double TeFirstMoment (It begin, It end)
    00563 {
    00564            int number = 0;
    00565            double mean = 0.;
    00566            while (begin != end)
    00567            {
    00568                       if((*begin)!= TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00569                       {
    00570                                  mean += (*begin);
    00571                                  number++;
    00572                       }
    00573                       begin++;
    00574            }
    00575            return mean /= number;
    00576 }
    00577
    00578
    00580 template <typename It>
    00581 double TeSecondMoment (It begin, It end, double mean)
    00582 {
    00583            int number = 0;
    00584            double SSD = 0;  //sum of squares of desviation
    00585
    00586            while (begin != end)
    00587            {
    00588                       if((*begin)!= TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00589                       {
    00590                                  SSD += pow(( (*begin) - mean), 2);
    00591                                  number++;
    00592                       }
    00593                       begin++;
    00594            }
    00595            return SSD /= number;
    00596 }
    00597
    00598
    00600 template <typename It>
    00601 double TeSecondMoment (It begin, It end, double mean, int dim)
    00602 {
    00603            int number = 0;
    00604            double SSD = 0;  //sum of squares of desviation
    00605
    00606            while (begin != end)
    00607            {
    00608                       if(((*begin)[dim])!= TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00609                       {
    00610                                  SSD += pow(((*begin)[dim] - mean), 2);
    00611                                  number++;
    00612                       }
    00613                       ++begin;
    00614            }
    00615            return SSD /= number;
    00616 }
    00617
    00618
    00619
    00621 template <typename It, typename T>
    00622 bool TeMeanVector ( It begin, It end, T& result)
    00623 {
    00624            int i = 0;
    00625            int dim = result.size();
    00626            T soma;
    00627
    00628            for (int m = 0; m < dim ; m++)
    00629                       soma.push_back(0.);
    00630
    00631            while (begin != end)
    00632            {
    00633                       for( int j = 0; j < dim; j++)
    00634                       {
    00635                                  if(((*begin)[j]) != TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00636                                  {
    00637                                             soma[j]+= (*begin)[j];
    00638                                             i++;
    00639                                  }
    00640                                  else
    00641                                             soma[j]= TeMAXFLOAT;
    00642                       }
    00643                       begin++;
    00644            }
    00645
    00646            for (int k = 0; k < dim; k++)
    00647            {
    00648                       if(soma[k] != TeMAXFLOAT)
    00649                                  result[k] = soma[k]/i;
    00650                       else
    00651                                  result[k] = TeMAXFLOAT;
    00652            }
    00653
    00654            return true;
    00655 }
    00656
    00658 template <typename It>
    00659 bool TeDeviation ( It begin, It end, It result, double mean)
    00660 {
    00661            while (begin != end)
    00662            {
    00663                       if((*begin) != TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00664                                  *result = double(*begin) - mean;
    00665                       else
    00666                                  *result = TeMAXFLOAT;
    00667
    00668                       begin++; result++;
    00669            }
    00670            return true;
    00671 }
    00672
    00673
    00675 template <typename iteratorSet>
    00676 bool TeDeviation ( iteratorSet begin, iteratorSet end, double mean, int index=0)
    00677 {
    00678            //Adds a new attribute in the attribute list of the set
    00679            TeAttribute attrRep;
    00680            attrRep.rep_.name_ = "Z";
    00681            attrRep.rep_.type_ = TeREAL;
    00682            begin->addProperty(attrRep);
    00683
    00684            while (begin != end)
    00685            {
    00686                       double result;
    00687
    00688                       if(((*begin)[index]) != TeMAXFLOAT) // MAXFLOAT is considered invalid value
    00689                                  result = ((*begin)[index]) - mean;
    00690                       else
    00691                                  result = TeMAXFLOAT;
    00692
    00693                       (*begin).addPropertyValue(Te2String(result, 9));
    00694                       ++begin;
    00695            }
    00696            return true;
    00697 }
    00699 #endif
    00700

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines