Commit 4494aab9 authored by Jeannie Backer's avatar Jeannie Backer
Browse files

Modified Statistics class. Added xml read/write serialization. Brought code...

Modified Statistics class. Added xml read/write serialization. Brought code closer to ISIS standards.

git-svn-id: http://subversion.wr.usgs.gov/repos/prog/isis3/branches/ipce@6072 41f8697f-d340-4b68-9986-7bafba869bb8
parent 5ce92b9e
Loading
Loading
Loading
Loading
+99 −35
Original line number Diff line number Diff line
@@ -40,14 +40,15 @@ namespace Isis {
    m_id = NULL;
    m_id = new QUuid(QUuid::createUuid());
    SetValidRange();
    Reset();
    Reset(); // initialize
  }



  Statistics::Statistics(Project *project, XmlStackedHandlerReader *xmlReader, QObject *parent) {   // TODO: does xml stuff need project???
    m_id = NULL;
    // ??? initializations ???
    SetValidRange();
    Reset(); // initialize
    xmlReader->pushContentHandler(new XmlHandler(this, project));   // TODO: does xml stuff need project???
  }

@@ -89,7 +90,7 @@ namespace Isis {
    if (&other != this) {
      delete m_id;
      m_id = NULL;
      m_id = new QUuid(m_id->toString());
      m_id = new QUuid(other.m_id->toString());

      m_sum = other.m_sum;
      m_sumsum = other.m_sumsum;
@@ -147,7 +148,6 @@ namespace Isis {
  void Statistics::AddData(const double *data, const unsigned int count) {
    for(unsigned int i = 0; i < count; i++) {
      double value = data[i];
      //Calls the inline AddData method  --see .h file.
      AddData(value);
    }
  }
@@ -165,14 +165,8 @@ namespace Isis {
   */
  void Statistics::AddData(const double data) {
    m_totalPixels++;
    if(Isis::IsValidPixel(data) && InRange(data)) {
      m_sum += data;
      m_sumsum += data * data;
      if(data < m_minimum) m_minimum = data;
      if(data > m_maximum) m_maximum = data;
      m_validPixels++;
    }
    else if(Isis::IsNullPixel(data)) {

    if (Isis::IsNullPixel(data)) {
      m_nullPixels++;
    }
    else if (Isis::IsHisPixel(data)) {
@@ -190,9 +184,17 @@ namespace Isis {
    else if (AboveRange(data)) {
      m_overRangePixels++;
    }
    else {
    else if (BelowRange(data)) {
      m_underRangePixels++;
    }
    else { // if (Isis::IsValidPixel(data) && InRange(data)) {
      m_sum += data;
      m_sumsum += data * data;
      if (data < m_minimum) m_minimum = data;
      if (data > m_maximum) m_maximum = data;
      m_validPixels++;
    }

  }


@@ -214,34 +216,40 @@ namespace Isis {
    m_removedData = true;

    for(unsigned int i = 0; i < count; i++) {
      m_totalPixels--;
      double value = data[i];
      RemoveData(value);
    }

      if(IsValidPixel(data[i]) && InRange(data[i])) {
        m_sum -= data[i];
        m_sumsum -= data[i] * data[i];
        m_validPixels--;
  }
      else if(Isis::IsNullPixel(data[i])) {

  void Statistics::RemoveData(const double data) {
    m_totalPixels--;

    if (Isis::IsNullPixel(data)) {
      m_nullPixels--;
    }
      else if(Isis::IsHisPixel(data[i])) {
    else if (Isis::IsHisPixel(data)) {
      m_hisPixels--;
    }
      else if(Isis::IsHrsPixel(data[i])) {
    else if (Isis::IsHrsPixel(data)) {
      m_hrsPixels--;
    }
      else if(Isis::IsLisPixel(data[i])) {
    else if (Isis::IsLisPixel(data)) {
      m_lisPixels--;
    }
      else if(Isis::IsLrsPixel(data[i])) {
    else if (Isis::IsLrsPixel(data)) {
      m_lrsPixels--;
    }
      else if(AboveRange(data[i])) {
    else if (AboveRange(data)) {
      m_overRangePixels--;
    }
      else {
    else if (BelowRange(data)) {
      m_underRangePixels--;
    }
    else { // if (IsValidPixel(data) && InRange(data)) {
      m_sum -= data;
      m_sumsum -= data * data;
      m_validPixels--;
    }

    if (m_totalPixels < 0) {
@@ -250,10 +258,6 @@ namespace Isis {
    }
  }

  void Statistics::RemoveData(const double data) {
    RemoveData(&data, 1);
  }

  void Statistics::SetValidRange(const double minimum, const double maximum) {
    m_validMinimum = minimum;
    m_validMaximum = maximum;
@@ -265,6 +269,39 @@ namespace Isis {
      throw IException(IException::Programmer, msg, _FILEINFO_);
    }
  }



  double Statistics::ValidMinimum() const {
    return m_validMinimum;
  }



  double Statistics::ValidMaximum() const {
    return m_validMaximum;
  }



  bool Statistics::InRange(const double value) {
    return (!BelowRange(value) && !AboveRange(value));
  }



  bool Statistics::AboveRange(const double value) {
    return (value > m_validMaximum);
  }



  bool Statistics::BelowRange(const double value) {
    return (value < m_validMinimum);
  }



  /**
   * Computes and returns the average.
   * If there are no valid pixels, then NULL8 is returned.
@@ -304,6 +341,30 @@ namespace Isis {
    return temp / ((m_validPixels - 1.0) * m_validPixels);
  }



  /**
   * Returns the sum of all the data
   *
   * @return The sum of the data
   */
  double Statistics::Sum() const {
    return m_sum;
  }



  /**
   * Returns the sum of all the squared data
   *
   * @return The sum of the squared data
   */
  double Statistics::SumSquare() const {
    return m_sumsum;
  }



  /**
   * Computes and returns the rms.
   * If there are no valid pixels, then NULL8 is returned.
@@ -590,13 +651,14 @@ namespace Isis {
    stream.writeTextElement("id", m_id->toString());
 
    stream.writeTextElement("sum", toString(m_sum));
    stream.writeTextElement("sumSquares", toString(m_sumsum));

    stream.writeStartElement("range");
    stream.writeTextElement("minimum", toString(m_minimum));
    stream.writeTextElement("maximum", toString(m_maximum));
    stream.writeTextElement("validMinimum", toString(m_validMinimum));
    stream.writeTextElement("validMaximum", toString(m_validMaximum));
    stream.writeEndElement();
    stream.writeEndElement(); // end range
    
    stream.writeStartElement("pixelCounts");
    stream.writeTextElement("totalPixels", toString(m_totalPixels));
@@ -608,10 +670,10 @@ namespace Isis {
    stream.writeTextElement("hrsPixels", toString(m_hrsPixels));
    stream.writeTextElement("underRangePixels", toString(m_underRangePixels));
    stream.writeTextElement("overRangePixels", toString(m_overRangePixels));
    stream.writeEndElement();
    stream.writeEndElement(); // end pixelCounts
    
    stream.writeTextElement("removedData", toString(m_removedData));
    stream.writeEndElement();
    stream.writeEndElement(); // end statistics

  }

@@ -662,16 +724,18 @@ namespace Isis {
      }
      if (localName == "sum") {
        m_xmlHandlerStatistics->m_sum = toDouble(m_xmlHandlerCharacters);
        m_xmlHandlerStatistics->m_sumsum = m_xmlHandlerStatistics->m_sum * m_xmlHandlerStatistics->m_sum;
      }
      if (localName == "sumSquares") {
        m_xmlHandlerStatistics->m_sumsum = toDouble(m_xmlHandlerCharacters);
      }
      if (localName == "minimum") {
        m_xmlHandlerStatistics->m_minimum = toDouble(m_xmlHandlerCharacters);
      }
      if (localName == "maximum") {
        m_xmlHandlerStatistics->m_maximum = toInt(m_xmlHandlerCharacters);
        m_xmlHandlerStatistics->m_maximum = toDouble(m_xmlHandlerCharacters);
      }
      if (localName == "validMinimum") {
        m_xmlHandlerStatistics->m_validMinimum = toInt(m_xmlHandlerCharacters);
        m_xmlHandlerStatistics->m_validMinimum = toDouble(m_xmlHandlerCharacters);
      }
      if (localName == "validMaximum") {
        m_xmlHandlerStatistics->m_validMaximum = toDouble(m_xmlHandlerCharacters);
+23 −41
Original line number Diff line number Diff line
@@ -58,9 +58,10 @@ 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 Andersonzz
  * @author 2002-05-06 Jeff Anderson
  *
  * @internal
  *   @history 2002-05-08 Jeff Anderson - Added Chebyshev and Best minimum/maximum methods.
@@ -77,6 +78,9 @@ namespace Isis {
  *   @history 2011-06-13 Ken Edmundson - Added Rms method.
  *   @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.
  *
  *   @todo 2005-02-07 Deborah Lee Soltesz - add example using cube data to the class documentation
  *
@@ -85,7 +89,8 @@ namespace Isis {
    Q_OBJECT
    public:
      Statistics(QObject *parent = 0);
      Statistics(Project *project, XmlStackedHandlerReader *xmlReader, QObject *parent = 0);   // TODO: does xml stuff need project???
      Statistics(Project *project, XmlStackedHandlerReader *xmlReader, QObject *parent = 0);   
      // TODO: does xml read/write stuff need Project input???
      Statistics(const Statistics &other);
      ~Statistics();
      Statistics &operator=(const Statistics &other);
@@ -98,27 +103,20 @@ namespace Isis {
      void RemoveData(const double *data, const unsigned int count);
      void RemoveData(const double data);

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

      double ValidMinimum() const {
        return m_validMinimum;
      }
      double ValidMaximum() const {
        return m_validMaximum;
      }
      bool InRange(const double value) {
        return (value >= m_validMinimum && value <= m_validMaximum);
      }
      bool AboveRange(const double value) {
        return (value > m_validMaximum);
      }
      bool BelowRange(const double value) {
        return (value < m_validMinimum);
      }
      void SetValidRange(const double minimum = Isis::ValidMinimum, 
                         const double maximum = Isis::ValidMaximum);

      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;
@@ -141,26 +139,8 @@ namespace Isis {
      BigInt OutOfRangePixels() const;
      bool RemovedData() const;


      /**
       * Returns the sum of all the data
       *
       * @return The sum of the data
       */
      double Sum() const {
        return m_sum;
      };

      /**
       * Returns the sum of all the squared data
       *
       * @return The sum of the squared data
       */
      double SumSquare() const {
        return m_sumsum;
      };

      void save(QXmlStreamWriter &stream, const Project *project) const;   // TODO: does xml stuff need project???
      void save(QXmlStreamWriter &stream, const Project *project) const;
      // TODO: does xml stuff need project???
    
      QDataStream &write(QDataStream &stream) const;
      QDataStream &read(QDataStream &stream);
@@ -174,7 +154,8 @@ namespace Isis {
       */
      class XmlHandler : public XmlStackedHandler {
        public:
          XmlHandler(Statistics *statistics, Project *project);   // TODO: does xml stuff need project???
          XmlHandler(Statistics *statistics, Project *project);
          // TODO: does xml stuff need project???
          ~XmlHandler();
   
          virtual bool startElement(const QString &namespaceURI, const QString &localName,
@@ -187,7 +168,8 @@ namespace Isis {
          Q_DISABLE_COPY(XmlHandler);
   
          Statistics *m_xmlHandlerStatistics;
          Project *m_xmlHandlerProject;   // TODO: does xml stuff need project???
          Project *m_xmlHandlerProject;
          // TODO: does xml stuff need project???
          QString m_xmlHandlerCharacters;
      };

+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>