USGS

Isis 3.0 Object Programmers' Reference

Home

Statistics.cpp
Go to the documentation of this file.
1 
21 #include <float.h>
22 #include <string>
23 #include "Statistics.h"
24 #include "IException.h"
25 #include "IString.h"
26 
27 using namespace std;
28 namespace Isis {
30  Statistics::Statistics() {
31  SetValidRange();
32  Reset();
33  }
34 
36  void Statistics::Reset() {
37  p_sum = 0.0;
38  p_sumsum = 0.0;
39  p_minimum = DBL_MAX;
40  p_maximum = -DBL_MAX;
41  p_totalPixels = 0;
42  p_validPixels = 00;
43  p_nullPixels = 0;
44  p_lisPixels = 0;
45  p_lrsPixels = 0;
46  p_hrsPixels = 0;
47  p_hisPixels = 0;
48  p_overRangePixels = 0;
49  p_underRangePixels = 0;
50  p_removedData = false;
51  }
52 
54  Statistics::~Statistics() {};
55 
66  void Statistics::AddData(const double *data, const unsigned int count) {
67  for(unsigned int i = 0; i < count; i++) {
68  double value = data[i];
69  //Calls the inline AddData method --see .h file.
70  AddData(value);
71  }
72  }
73 
74 
88  void Statistics::RemoveData(const double *data, const unsigned int count) {
89  p_removedData = true;
90 
91  for(unsigned int i = 0; i < count; i++) {
92  p_totalPixels--;
93 
94  if(Isis::IsValidPixel(data[i]) && InRange(data[i])) {
95  p_sum -= data[i];
96  p_sumsum -= data[i] * data[i];
97  p_validPixels--;
98  }
99  else if(Isis::IsNullPixel(data[i])) {
100  p_nullPixels--;
101  }
102  else if(Isis::IsHisPixel(data[i])) {
103  p_hisPixels--;
104  }
105  else if(Isis::IsHrsPixel(data[i])) {
106  p_hrsPixels--;
107  }
108  else if(Isis::IsLisPixel(data[i])) {
109  p_lisPixels--;
110  }
111  else if(Isis::IsLrsPixel(data[i])) {
112  p_lrsPixels--;
113  }
114  else if(AboveRange(data[i])) {
115  p_overRangePixels--;
116  }
117  else {
118  p_underRangePixels--;
119  }
120  }
121 
122  if(p_totalPixels < 0) {
123  string m = "You are removing non-existant data in [Statistics::RemoveData]";
124  throw IException(IException::Programmer, m, _FILEINFO_);
125  }
126  }
127 
128  void Statistics::RemoveData(const double data) {
129  RemoveData(&data, 1);
130  }
131 
132  void Statistics::SetValidRange(const double minimum, const double maximum) {
133  p_validMinimum = minimum;
134  p_validMaximum = maximum;
135 
136  if(p_validMaximum < p_validMinimum) {
137  // get the min and max DN values in the chosen range
138  IString sMin(minimum);
139  IString sMax(maximum);
140  std::string m = "Invalid Range: Minimum [" + sMin + "] must be less than the Maximum [" + sMax + "]";
141  throw IException(IException::Programmer, m, _FILEINFO_);
142  }
143  }
150  double Statistics::Average() const {
151  if(p_validPixels < 1) return Isis::NULL8;
152  return p_sum / p_validPixels;
153  }
154 
161  double Statistics::StandardDeviation() const {
162  if(p_validPixels <= 1) return Isis::NULL8;
163  return sqrt(Variance());
164  }
165 
176  double Statistics::Variance() const {
177  if(p_validPixels <= 1) return Isis::NULL8;
178  double temp = p_validPixels * p_sumsum - p_sum * p_sum;
179  if(temp < 0.0) temp = 0.0; // This should happen unless roundoff occurs
180  return temp / ((p_validPixels - 1.0) * p_validPixels);
181  }
182 
192  double Statistics::Rms() const {
193  if(p_validPixels < 1) return Isis::NULL8;
194  double temp = p_sumsum / p_validPixels;
195  if(temp < 0.0) temp = 0.0;
196  return sqrt(temp);
197  }
198 
208  double Statistics::Minimum() const {
209  if(p_removedData) {
210  string m = "Minimum is invalid since you removed data";
211  throw IException(IException::Programmer, m, _FILEINFO_);
212  }
213 
214  if(p_validPixels < 1) return Isis::NULL8;
215  return p_minimum;
216  }
217 
228  double Statistics::Maximum() const {
229  if(p_removedData) {
230  string m = "Maximum is invalid since you removed data";
231  throw IException(IException::Programmer, m, _FILEINFO_);
232  }
233 
234  if(p_validPixels < 1) return Isis::NULL8;
235  return p_maximum;
236  }
237 
244  BigInt Statistics::TotalPixels() const {
245  return p_totalPixels;
246  }
247 
256  BigInt Statistics::ValidPixels() const {
257  return p_validPixels;
258  }
259 
266  BigInt Statistics::OverRangePixels() const {
267  return p_overRangePixels;
268  }
269 
276  BigInt Statistics::UnderRangePixels() const {
277  return p_underRangePixels;
278  }
279 
285  BigInt Statistics::NullPixels() const {
286  return p_nullPixels;
287  }
288 
295  BigInt Statistics::LisPixels() const {
296  return p_lisPixels;
297  }
298 
305  BigInt Statistics::LrsPixels() const {
306  return p_lrsPixels;
307  }
308 
315  BigInt Statistics::HisPixels() const {
316  return p_hisPixels;
317  }
318 
325  BigInt Statistics::HrsPixels() const {
326  return p_hrsPixels;
327  }
328 
335  BigInt Statistics::OutOfRangePixels() const {
336  return p_overRangePixels + p_underRangePixels;
337  }
338 
354  double Statistics::ChebyshevMinimum(const double percent) const {
355  if((percent <= 0.0) || (percent >= 100.0)) {
356  string m = "Invalid value for percent";
357  throw IException(IException::Programmer, m, _FILEINFO_);
358  }
359 
360  if(p_validPixels < 1) return Isis::NULL8;
361  double k = sqrt(1.0 / (1.0 - percent / 100.0));
362  return Average() - k * StandardDeviation();
363  }
364 
380  double Statistics::ChebyshevMaximum(const double percent) const {
381  if((percent <= 0.0) || (percent >= 100.0)) {
382  string m = "Invalid value for percent";
383  throw IException(IException::Programmer, m, _FILEINFO_);
384  }
385 
386  if(p_validPixels < 1) return Isis::NULL8;
387  double k = sqrt(1.0 / (1.0 - percent / 100.0));
388  return Average() + k * StandardDeviation();
389  }
390 
405  double Statistics::BestMinimum(const double percent) const {
406  if(p_validPixels < 1) return Isis::NULL8;
407  double min = ChebyshevMinimum(percent);
408  if(Minimum() > min) min = Minimum();
409  return min;
410  }
411 
427  double Statistics::BestMaximum(const double percent) const {
428  if(p_validPixels < 1) return Isis::NULL8;
429  double max = ChebyshevMaximum(percent);
430  if(Maximum() < max) max = Maximum();
431  return max;
432  }
433 
446  double Statistics::ZScore(const double value) const {
447  if(StandardDeviation() == 0) {
448  if(value == Maximum()) return 0;
449  else {
450  string m = "Undefined Z-score. Standard deviation is zero and";
451  m += " the input value[" + Isis::IString(value) + "] is out of range [" + Isis::IString(Maximum()) + "].";
452  throw IException(IException::Programmer, m, _FILEINFO_);
453  }
454  }
455  return (value - Average()) / StandardDeviation();
456  }
457 
458 } // end namespace isis