Commit d3d2af24 authored by Jeannie Backer's avatar Jeannie Backer
Browse files

PROG: Improved Statistics class coding standards. Added QDataStream, XML, HDF...

PROG: Improved Statistics class coding standards. Added QDataStream, XML, HDF serialization capabilities. These modifications were merged from the IPCE branch.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/trunk@6486 41f8697f-d340-4b68-9986-7bafba869bb8
parent 87ccb359
Loading
Loading
Loading
Loading
+560 −94

File changed.

Preview size limit exceeded, changes collapsed.

+106 −101
Original line number Diff line number Diff line
@@ -20,11 +20,23 @@
 *  http://www.usgs.gov/privacy.html.
 */

#include <QObject>

#include <H5Cpp.h>
#include <hdf5_hl.h>
#include <hdf5.h>

#include "SpecialPixel.h"
#include "Constants.h"
#include "SpecialPixel.h"
#include "XmlStackedHandler.h"

class QDataStream;
class QUuid;
class QXmlStreamWriter;

namespace Isis {
  class Project;// ??? does xml stuff need project???
  class XmlStackedHandlerReader;
  /**
   * @brief This class is used to accumulate statistics on double arrays.
   *
@@ -50,104 +62,72 @@ namespace Isis {
   * Histogram object (inherits from Statistics) and the stats application,
   * stats.cpp (uses the Statistics child class Histogram).
   *
   * @ingroup Math
   * @ingroup Statistics
   *
   * @author 2002-05-06 Jeff Anderson
   *
   * @internal
  * @history 2002-05-08 Jeff Anderson - Added Chebyshev and Best
  * minimum/maximum methods.
  * @history 2004-05-11 Jeff Anderson - Moved Reset, AddData and RemoveData
  * methods into public space.
   *   @history 2002-05-08 Jeff Anderson - Added Chebyshev and Best minimum/maximum methods.
   *   @history 2004-05-11 Jeff Anderson - Moved Reset, AddData and RemoveData methods into public
   *                           space.
   *   @history 2004-06-28 Jeff Anderson - Added Sum and SumSquare methods.
  * @history 2005-02-17 Deborah Lee Soltesz - Modified file to support Doxygen
  * documentation.
   *   @history 2005-02-17 Deborah Lee Soltesz - Modified file to support Doxygen documentation.
   *   @history 2005-05-23 Jeff Anderson - Changed to support 2GB+ files
   *   @history 2006-02-15 Jacob Danton - Added Valid Range options/methods
   *   @history 2006-03-10 Jacob Danton - Added Z-score method
  * @history 2007-01-18 Robert Sucharski - Added AddData method
  *                       for a single double value
   *   @history 2007-01-18 Robert Sucharski - Added AddData method for a single double value
   *   @history 2008-05-06 Steven Lambright - Added AboveRange, BelowRange methods
  * @history 2010-03-18 Sharmila Prasad  - Error message more meaningful for SetValidRange function
  * @history 2011-06-13 Ken Edmundson - Added Rms method
   *   @history 2010-03-18 Sharmila Prasad  - Error message more meaningful for SetValidRange
   *                           function
   *   @history 2011-06-13 Ken Edmundson - Added Rms method.
  *    @history 2015-09-01 Tyler Wilson - Made SetValidRange and the destructor virtual.  
  *                                       See Ref #2188.
  * @todo 2005-02-07 Deborah Lee Soltesz - add example using cube data to the
  * class documentation
   *   @history 2011-06-23 Jeannie Backer - Added QDataStream read(), write() methods and added
   *                           QDataStream >> and << operators. Replaced std strings with QStrings.
   *   @history 2014-09-05 Jeannie Backer - Added xml read/write capabilities.  Moved method
   *                           implementation to cpp file. Improved coverage of unitTest. Brought
   *                           code closer to standards.
   *   @history 2015-09-03 Jeannie Backer - Added hdf5 read/write capabilities by adding
   *                           compoundH5DataType() static method.
   *
   *   @todo 2005-02-07 Deborah Lee Soltesz - add example using cube data to the class documentation
   *   @todo 2015-08-13 Jeannie Backer - Clean up header and implementation files once
   *                        serialization is implemented. (Remove xml, data stream, hdf, etc)
   *
   */
  class Statistics {
  class Statistics : public QObject {
    Q_OBJECT
    public:
      Statistics();
      Statistics(QObject *parent = 0);
      Statistics(Project *project, XmlStackedHandlerReader *xmlReader, QObject *parent = 0);   
      // TODO: does xml read/write stuff need Project input???
      Statistics(const Statistics &other);
      virtual ~Statistics();
      Statistics &operator=(const Statistics &other);

      void Reset();

      void AddData(const double *data, const unsigned int count);
      /**
       * Add a double to the accumulators and counters. This method
       * can be invoked multiple times (for example: once for each
       * pixel in a cube) before obtaining statistics.
       *
       * @param data The data to be added to the data set used for statistical
       *    calculations.
       *
       */
      inline void AddData(const double data) {
        p_totalPixels++;
        if(Isis::IsValidPixel(data) && InRange(data)) {
          p_sum += data;
          p_sumsum += data * data;
          if(data < p_minimum) p_minimum = data;
          if(data > p_maximum) p_maximum = data;
          p_validPixels++;
        }
        else if(Isis::IsNullPixel(data)) {
          p_nullPixels++;
        }
        else if(Isis::IsHisPixel(data)) {
          p_hisPixels++;
        }
        else if(Isis::IsHrsPixel(data)) {
          p_hrsPixels++;
        }
        else if(Isis::IsLisPixel(data)) {
          p_lisPixels++;
        }
        else if(Isis::IsLrsPixel(data)) {
          p_lrsPixels++;
        }
        else if(AboveRange(data)) {
          p_overRangePixels++;
        }
        else {
          p_underRangePixels++;
        }
      }
      void AddData(const double data);

      void RemoveData(const double *data, const unsigned int count);
      void RemoveData(const double data);
      virtual void SetValidRange(const double minimum = Isis::ValidMinimum,

      void SetValidRange(const double minimum = Isis::ValidMinimum,
                         const double maximum = Isis::ValidMaximum);

      double ValidMinimum() const {
        return p_validMinimum;
      }
      double ValidMaximum() const {
        return p_validMaximum;
      }
      bool InRange(const double value) {
        return (value >= p_validMinimum && value <= p_validMaximum);
      }
      bool AboveRange(const double value) {
        return (value > p_validMaximum);
      }
      bool BelowRange(const double value) {
        return (value < p_validMinimum);
      }
      double ValidMinimum() const;
      double ValidMaximum() const;
      bool InRange(const double value);
      bool AboveRange(const double value);
      bool BelowRange(const double value);

      double Average() const;
      double StandardDeviation() const;
      double Variance() const;
      double Sum() const;
      double SumSquare() const;
      double Rms() const;

      double Minimum() const;
@@ -168,45 +148,70 @@ namespace Isis {
      BigInt HisPixels() const;
      BigInt HrsPixels() const;
      BigInt OutOfRangePixels() const;
      bool RemovedData() const;

      void save(QXmlStreamWriter &stream, const Project *project) const;
      // TODO: does xml stuff need project???
    
      /**
       * Returns the sum of all the data
       *
       * @return The sum of the data
       */
      double Sum() const {
        return p_sum;
      };
      QDataStream &write(QDataStream &stream) const;
      QDataStream &read(QDataStream &stream);

      static H5::CompType compoundH5DataType();

    private:
      /**
       * Returns the sum of all the squared data
       *
       * @return The sum of the squared data
       * @author 2014-07-28 Jeannie Backer
       *
       * @internal
       */
      double SumSquare() const {
        return p_sumsum;
      };
      class XmlHandler : public XmlStackedHandler {
        public:
          XmlHandler(Statistics *statistics, Project *project);
          // TODO: does xml stuff need project???
          ~XmlHandler();
   
          virtual bool startElement(const QString &namespaceURI, const QString &localName,
                                    const QString &qName, const QXmlAttributes &atts);
          virtual bool characters(const QString &ch);
          virtual bool endElement(const QString &namespaceURI, const QString &localName,
                                    const QString &qName);
   
        private:
      double p_sum;           //!< Sum accumulator.
      double p_sumsum;        //!< Sum-squared accumulator.
      double p_minimum;       //!< Minimum double value encountered.
      double p_maximum;       //!< Maximum double value encountered.
      double p_validMinimum;  //!< Minimum valid pixel value
      double p_validMaximum;  //!< Maximum valid pixel value
      BigInt p_totalPixels;   //!< Count of total pixels processed.
      BigInt p_validPixels;   //!< Count of valid pixels (non-special) processed.
      BigInt p_nullPixels;    //!< Count of null pixels processed.
      BigInt p_lrsPixels;     //!< Count of low instrument saturation pixels processed.
      BigInt p_lisPixels;     //!< Count of low representation saturation pixels processed.
      BigInt p_hrsPixels;     //!< Count of high instrument saturation pixels processed.
      BigInt p_hisPixels;     //!< Count of high instrument representation pixels processed.
      BigInt p_underRangePixels; //!< Count of pixels less than the valid range
      BigInt p_overRangePixels; //!< Count of pixels greater than the valid range
      bool   p_removedData;   /**< Indicates the RemoveData method was called which implies
                                   p_minimum and p_maximum are invalid. */
          Q_DISABLE_COPY(XmlHandler);
   
          Statistics *m_xmlHandlerStatistics;
          Project *m_xmlHandlerProject;
          // TODO: does xml stuff need project???
          QString m_xmlHandlerCharacters;
      };

//      QUuid *m_id; /**< A unique ID for this object (useful for others to reference
//                        this object when saving to disk).*/
      double m_sum;              //!< The sum accumulator, i.e. the sum of added data values.
      double m_sumsum;           /**< The sum-squared accumulator, i.e. the sum of the squares
                                      of the  data values.*/
      double m_minimum;          //!< Minimum double value encountered.
      double m_maximum;          //!< Maximum double value encountered.
      double m_validMinimum;     //!< Minimum valid pixel value
      double m_validMaximum;     //!< Maximum valid pixel value
      BigInt m_totalPixels;      //!< Count of total pixels processed.
      BigInt m_validPixels;      //!< Count of valid pixels (non-special) processed.
      BigInt m_nullPixels;       //!< Count of null pixels processed.
      BigInt m_lrsPixels;        //!< Count of low instrument saturation pixels processed.
      BigInt m_lisPixels;        //!< Count of low representation saturation pixels processed.
      BigInt m_hrsPixels;        //!< Count of high instrument saturation pixels processed.
      BigInt m_hisPixels;        //!< Count of high instrument representation pixels processed.
      BigInt m_underRangePixels; //!< Count of pixels less than the valid range
      BigInt m_overRangePixels;  //!< Count of pixels greater than the valid range
      bool   m_removedData;      /**< Indicates the RemoveData method was called which implies
                                      m_minimum and m_maximum are invalid. */
  };

  // operators to read/write Statistics to/from binary data
  QDataStream &operator<<(QDataStream &stream, const Statistics &statistics);
  QDataStream &operator>>(QDataStream &stream, Statistics &statistics);

} // end namespace isis

#endif
+226 −12
Original line number Diff line number Diff line
Unit test for Statistics... 
For data set:    5, 5 
Average:         5 
ZScore(5.0):     0 

Testing Reset... 
Average:              -1.79769e+308 
Variance:             -1.79769e+308 
Rms:                  -1.79769e+308 
Std Deviation:        -1.79769e+308 
Minimum:              -1.79769e+308 
Maximum:              -1.79769e+308 
ChebyShev Min:        -1.79769e+308 
ChebyShev Max:        -1.79769e+308 
Best Minimum:         -1.79769e+308 
Best Maximum:         -1.79769e+308 
Valid Minimum:        -1.79769e+308 
Valid Maximum:        1.79769e+308 
Total Pixels:         0 
Valid Pixels:         0 
Null Pixels:          0 
Lis Pixels:           0 
Lrs Pixels:           0 
His Pixels:           0 
Hrs Pixels:           0 
Out of Range Pixels:  0 
Over Range Pixels:    0 
Under Range Pixels:   0 
Sum:                  0 
SumSquare:            0 
Removed Data?         false 
Z-Score at 1.0        -1 

SetValidRange(1, 6) and AddData( 1, 2, 3, Null, HRS, LRS, HIS, LIS, 10, -1) 
InRange(0.0)?         false 
InRange(2.0)?         true 
InRange(7.0)?         false 
Average:              2 
Variance:             1 
Rms:                  2.16025 
@@ -9,16 +45,24 @@ ChebyShev Min: -12.1421
ChebyShev Max:        16.1421 
Best Minimum:         1 
Best Maximum:         3 
Total Pixels:   8
Valid Minimum:        1 
Valid Maximum:        6 
Total Pixels:         10 
Valid Pixels:         3 
Null Pixels:          1 
Lis Pixels:           1 
Lrs Pixels:           1 
His Pixels:           1 
Hrs Pixels:           1 
Out of Range Pixels:  2 
Over Range Pixels:    1 
Under Range Pixels:   1 
Sum:                  6 
SumSquare:            14 
Removed Data?         false 
Z-Score at 1.0        -1 

AddData( 4, 5, 6, Null) 
Average:              3.5 
Variance:             3.5 
Rms:                  3.89444 
@@ -29,38 +73,198 @@ ChebyShev Min: -22.9575
ChebyShev Max:        29.9575 
Best Minimum:         1 
Best Maximum:         6 
Total Pixels:   12
Valid Minimum:        1 
Valid Maximum:        6 
Total Pixels:         14 
Valid Pixels:         6 
Null Pixels:          2 
Lis Pixels:           1 
Lrs Pixels:           1 
His Pixels:           1 
Hrs Pixels:           1 
Out of Range Pixels:  2 
Over Range Pixels:    1 
Under Range Pixels:   1 
Sum:                  21 
SumSquare:            91 
Removed Data?         false 
Z-Score at 1.0        -1.33631 

Average:        5
Variance:       1
Rms:            5.06623
Std Deviation:  1
ChebyShev Min:  -9.14214
ChebyShev Max:  19.1421
Total Pixels:   9
Valid Pixels:   3
Testing copy constructor... 
Average:              3.5 
Variance:             3.5 
Rms:                  3.89444 
Std Deviation:        1.87083 
Minimum:              1 
Maximum:              6 
ChebyShev Min:        -22.9575 
ChebyShev Max:        29.9575 
Best Minimum:         1 
Best Maximum:         6 
Valid Minimum:        1 
Valid Maximum:        6 
Total Pixels:         14 
Valid Pixels:         6 
Null Pixels:          2 
Lis Pixels:           1 
Lrs Pixels:           1 
His Pixels:           1 
Hrs Pixels:           1 
Out of Range Pixels:  2 
Over Range Pixels:    1 
Under Range Pixels:   1 
Sum:                  21 
SumSquare:            91 
Removed Data?         false 
Z-Score at 1.0        -1.33631 

Testing assignment operator=... 
Average:              3.5 
Variance:             3.5 
Rms:                  3.89444 
Std Deviation:        1.87083 
Minimum:              1 
Maximum:              6 
ChebyShev Min:        -22.9575 
ChebyShev Max:        29.9575 
Best Minimum:         1 
Best Maximum:         6 
Valid Minimum:        1 
Valid Maximum:        6 
Total Pixels:         14 
Valid Pixels:         6 
Null Pixels:          2 
Lis Pixels:           1 
Lrs Pixels:           1 
His Pixels:           1 
Hrs Pixels:           1 
Out of Range Pixels:  2 
Over Range Pixels:    1 
Under Range Pixels:   1 
Sum:                  21 
SumSquare:            91 
Removed Data?         false 
Z-Score at 1.0        -1.33631 

Average:              3.5 
Variance:             3.5 
Rms:                  3.89444 
Std Deviation:        1.87083 
Minimum:              1 
Maximum:              6 
ChebyShev Min:        -22.9575 
ChebyShev Max:        29.9575 
Best Minimum:         1 
Best Maximum:         6 
Valid Minimum:        1 
Valid Maximum:        6 
Total Pixels:         14 
Valid Pixels:         6 
Null Pixels:          2 
Lis Pixels:           1 
Lrs Pixels:           1 
His Pixels:           1 
Hrs Pixels:           1 
Sum:            15
SumSquare:      77
Out of Range Pixels:  2 
Over Range Pixels:    1 
Under Range Pixels:   1 
Sum:                  21 
SumSquare:            91 
Removed Data?         false 
Z-Score at 1.0        -1.33631 

Testing RemoveData(3, Null, HRS, LRS, HIS, LIS, 10, -1) 
Average:              3.6 
Variance:             4.3 
Rms:                  4.04969 
Std Deviation:        2.07364 
ChebyShev Min:        -25.7258 
ChebyShev Max:        32.9258 
Total Pixels:         6 
Valid Pixels:         5 
Null Pixels:          1 
Lis Pixels:           0 
Lrs Pixels:           0 
His Pixels:           0 
Hrs Pixels:           0 
Out of Range Pixels:  0 
Over Range Pixels:    0 
Under Range Pixels:   0 
Sum:                  18 
SumSquare:            82 
Removed Data?         true 
Z-Score at 1.0        -1.25383 

Testing serialization from copy constructor object... 
Average:              3.5 
Variance:             3.5 
Rms:                  3.89444 
Std Deviation:        1.87083 
Minimum:              1 
Maximum:              6 
ChebyShev Min:        -22.9575 
ChebyShev Max:        29.9575 
Best Minimum:         1 
Best Maximum:         6 
Valid Minimum:        1 
Valid Maximum:        6 
Total Pixels:         14 
Valid Pixels:         6 
Null Pixels:          2 
Lis Pixels:           1 
Lrs Pixels:           1 
His Pixels:           1 
Hrs Pixels:           1 
Out of Range Pixels:  2 
Over Range Pixels:    1 
Under Range Pixels:   1 
Sum:                  21 
SumSquare:            91 
Removed Data?         false 
Z-Score at 1.0        -1.33631 

Testing XML: write XML from Statistics object... 
Testing XML: read XML to Statistics object... 
Average:              3.5 
Variance:             3.5 
Rms:                  3.89444 
Std Deviation:        1.87083 
Minimum:              1 
Maximum:              6 
ChebyShev Min:        -22.9575 
ChebyShev Max:        29.9575 
Best Minimum:         1 
Best Maximum:         6 
Valid Minimum:        1 
Valid Maximum:        6 
Total Pixels:         14 
Valid Pixels:         6 
Null Pixels:          2 
Lis Pixels:           1 
Lrs Pixels:           1 
His Pixels:           1 
Hrs Pixels:           1 
Out of Range Pixels:  2 
Over Range Pixels:    1 
Under Range Pixels:   1 
Sum:                  21 
SumSquare:            91 
Removed Data?         false 
Z-Score at 1.0        -1.33631 

Testing XML: read XML with no attributes or values to Statistics object... 
Average:              -1.79769e+308 
Variance:             -1.79769e+308 
Rms:                  -1.79769e+308 
Std Deviation:        -1.79769e+308 
Minimum:              -1.79769e+308 
Maximum:              -1.79769e+308 
ChebyShev Min:        -1.79769e+308 
ChebyShev Max:        -1.79769e+308 
Best Minimum:         -1.79769e+308 
Best Maximum:         -1.79769e+308 
Valid Minimum:        -1.79769e+308 
Valid Maximum:        1.79769e+308 
Total Pixels:         0 
Valid Pixels:         0 
Null Pixels:          0 
@@ -68,11 +272,21 @@ Lis Pixels: 0
Lrs Pixels:           0 
His Pixels:           0 
Hrs Pixels:           0 
Out of Range Pixels:  0 
Over Range Pixels:    0 
Under Range Pixels:   0 
Sum:                  0 
SumSquare:            0 
Removed Data?         false 
Z-Score at 1.0        -1 

Testing error throws... 
**PROGRAMMER ERROR** You are removing non-existant data in [Statistics::RemoveData].
**PROGRAMMER ERROR** Minimum is invalid since you removed data.
**PROGRAMMER ERROR** Maximum is invalid since you removed data.
**PROGRAMMER ERROR** Invalid value for percent.
**PROGRAMMER ERROR** Invalid value for percent.
**PROGRAMMER ERROR** Invalid value for percent.
**PROGRAMMER ERROR** Invalid value for percent.
**PROGRAMMER ERROR** Invalid Range: Minimum [100.0] must be less than the Maximum [0.0].
**PROGRAMMER ERROR** Undefined Z-score. Standard deviation is zero and the input value[0.0] is out of range [5.0].
+442 −93

File changed.

Preview size limit exceeded, changes collapsed.

+24 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<statistics>
    <id></id>
    <sum></sum>
    <sumSquares></sumSquares>
    <range>
        <minimum></minimum>
        <maximum></maximum>
        <validMinimum></validMinimum>
        <validMaximum></validMaximum>
    </range>
    <pixelCounts>
        <totalPixels></totalPixels>
        <validPixels></validPixels>
        <nullPixels></nullPixels>
        <lisPixels></lisPixels>
        <lrsPixels></lrsPixels>
        <hisPixels></hisPixels>
        <hrsPixels></hrsPixels>
        <underRangePixels></underRangePixels>
        <overRangePixels></overRangePixels>
    </pixelCounts>
    <removedData></removedData>
</statistics>
+1 −1

File changed.

Contains only whitespace changes.