source: trunk/src/Utils/Statistics.hh @ 208

Last change on this file since 208 was 205, checked in by Matthew Whiting, 18 years ago

Changes here mostly aimed at reducing/removing memory leaks. These were one of:

  • Forgetting to delete something allocated with "new". The Cube destructor has improved with the use of bool variables to keep track of when recon & baseline ahave been allocated.
  • Incorrectly using the wcs->flag parameter (eg. wcsIO had it set to -1 *after* calling wcsini)
  • Not closing a fits file once finished with (dataIO)
  • Allocating the wcsprm structs with calloc before calling wcsini, so that wcsvfree can work (it calls "free", so the memory needs to be allocated with calloc or malloc).

The only other change was the following:

  • A new way of doing the Cube::setCubeStats -- rather than calling the functions in getStats.cc, we explicitly do the calculations. This means we can sort the tempArray that has the BLANKS etc removed. This saves a great deal of memory usage on large FITS files (such as Enno's 2Gb one)
  • The old setCubeStats function is still there but called setCubeStatsOld.
File size: 3.5 KB
Line 
1#ifndef STATS_H
2#define STATS_H
3
4#include <iostream>
5#include <math.h>
6#ifndef UTILS_H
7#include <Utils/utils.hh>
8#endif
9
10namespace Statistics
11{
12
13  // Divide by the following correction factor to convert from
14  //   MADFM to sigma estimator.
15  const float correctionFactor = 0.6744888;
16  // Multiply by the following correction factor to convert from
17  //   trimmedSigma to sigma estimator.
18  const double trimToNormal = 1.17036753077;
19
20  template <class T> float madfmToSigma(T madfm){
21    return float(madfm)/correctionFactor;
22  };
23  template float madfmToSigma<int>(int madfm);
24  template float madfmToSigma<long>(long madfm);
25  template float madfmToSigma<float>(float madfm);
26  template float madfmToSigma<double>(double madfm);
27
28  template <class Type>
29  class StatsContainer
30  {
31  public:
32    StatsContainer(){useRobust=true; defined=false; useFDR=false;};
33    virtual ~StatsContainer(){};
34    StatsContainer(const StatsContainer<Type>& s);
35    StatsContainer<Type>& operator= (const StatsContainer<Type>& s);
36    template <class T> friend std::ostream& operator<< ( std::ostream& theStream, StatsContainer<T> &s);
37
38    float getMean(){return mean;};
39    void  setMean(float f){mean=f;};
40    float getStddev(){return stddev;};
41    void  setStddev(float f){stddev=f;};
42    Type  getMedian(){return median;};
43    void  setMedian(Type f){median=f;};
44    Type  getMadfm(){return madfm;};
45    void  setMadfm(Type f){madfm=f;};
46    float getThreshold(){return threshold;};
47    void  setThreshold(float f){threshold=f;};
48    float getThresholdSNR(){
49      return (threshold - this->getMiddle())/this->getSpread();};
50    void  setThresholdSNR(float snr){
51      threshold=this->getMiddle() + snr*this->getSpread();};
52    float getPThreshold(){return pThreshold;};
53    void  setPThreshold(float f){pThreshold=f;};
54    bool  getRobust(){return useRobust;};
55    void  setRobust(bool b){useRobust=b;};
56    bool  setUseFDR(){return useFDR;};
57    void  setUseFDR(bool b){useFDR=b;};
58
59    float getMiddle(){if(useRobust) return float(median); else return mean;};
60    float getSpread(){
61      if(useRobust) return madfmToSigma(madfm);
62      else return stddev;
63    };
64
65    float getPValue(float value){
66      float zStat = (value - this->getMiddle()) / this->getSpread();
67      return 0.5 * erfc( zStat / M_SQRT2 );
68    };
69
70    bool isDetection(float value){
71      if(useFDR) return (this->getPValue(value) < this->pThreshold);
72      else       return (value > this->threshold);
73    };
74
75    // Functions to calculate the stats for a given array.
76    // The idea here is that there are two options to do the calculations:
77    //   *The first just uses all the points in the array. If you need to
78    //     remove BLANK points (or something similar), do this beforehand.
79    //   *Alternatively, construct a bool array of the same size, showing which
80    //     points are good, and use the second option.
81    void calculate(Type *array, long size);
82    void calculate(Type *array, long size, bool *isGood);
83
84  private:
85    bool   defined;      // a flag indicating whether the stats are defined.
86
87    // basic statistics
88    float  mean;   
89    float  stddev;
90    Type   median;
91    Type   madfm;       
92
93    float  threshold;    // a threshold for simple sigma-clipping
94    float  pThreshold;   // a threshold for the FDR case -- the upper limit
95                         //   of P values that detected pixels can have.
96    bool   useRobust;    // whether we use the two robust stats or not
97    bool   useFDR;       // whether the FDR method is used for determining a
98                         //  detection
99
100  };
101
102}
103
104#endif /*STATS_H*/
Note: See TracBrowser for help on using the repository browser.