[190] | 1 | #ifndef STATS_H |
---|
| 2 | #define STATS_H |
---|
| 3 | |
---|
| 4 | #include <math.h> |
---|
| 5 | #ifndef UTILS_H |
---|
| 6 | #include <Utils/utils.hh> |
---|
| 7 | #endif |
---|
| 8 | |
---|
| 9 | namespace Statistics |
---|
| 10 | { |
---|
| 11 | |
---|
| 12 | // Divide by the following correction factor to convert from |
---|
| 13 | // MADFM to sigma estimator. |
---|
| 14 | const float correctionFactor = 0.6744888; |
---|
| 15 | // Multiply by the following correction factor to convert from |
---|
| 16 | // trimmedSigma to sigma estimator. |
---|
| 17 | const double trimToNormal = 1.17036753077; |
---|
| 18 | |
---|
| 19 | template <class T> float madfmToSigma(T madfm){ |
---|
| 20 | return float(madfm)/correctionFactor; |
---|
| 21 | }; |
---|
| 22 | |
---|
| 23 | template <class Type> |
---|
| 24 | class StatsContainer |
---|
| 25 | { |
---|
| 26 | public: |
---|
| 27 | StatsContainer(){useRobust=true;defined=false;useFDR=false;}; |
---|
| 28 | virtual ~StatsContainer(){}; |
---|
[192] | 29 | StatsContainer(const StatsContainer<Type>& s); |
---|
| 30 | StatsContainer<Type>& operator= (const StatsContainer<Type>& s); |
---|
[190] | 31 | |
---|
| 32 | float getMean(){return mean;}; |
---|
| 33 | void setMean(float f){mean=f;}; |
---|
| 34 | float getStddev(){return stddev;}; |
---|
| 35 | void setStddev(float f){stddev=f;}; |
---|
| 36 | Type getMedian(){return median;}; |
---|
| 37 | void setMedian(Type f){median=f;}; |
---|
| 38 | Type getMadfm(){return madfm;}; |
---|
| 39 | void setMadfm(Type f){madfm=f;}; |
---|
| 40 | float getThreshold(){return threshold;}; |
---|
| 41 | void setThreshold(float f){threshold=f;}; |
---|
| 42 | float getThresholdSNR(){ |
---|
| 43 | return (threshold - this->getMiddle())/this->getSpread();}; |
---|
| 44 | void setThresholdSNR(float snr){ |
---|
| 45 | threshold=this->getMiddle() + snr*this->getSpread();}; |
---|
| 46 | float getPThreshold(){return pThreshold;}; |
---|
| 47 | void setPThreshold(float f){pThreshold=f;}; |
---|
| 48 | bool getRobust(){return useRobust;}; |
---|
| 49 | void setRobust(bool b){useRobust=b;}; |
---|
| 50 | bool setUseFDR(){return useFDR;}; |
---|
| 51 | void setUseFDR(bool b){useFDR=b;}; |
---|
| 52 | |
---|
| 53 | float getMiddle(){if(useRobust) return float(median); else return mean;}; |
---|
| 54 | float getSpread(){ |
---|
| 55 | if(useRobust) return madfmToSigma(madfm); |
---|
| 56 | else return stddev; |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | float getPValue(float value){ |
---|
| 60 | float zStat = (value - this->getMiddle()) / this->getSpread(); |
---|
| 61 | return 0.5 * erfc( zStat / M_SQRT2 ); |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | bool isDetection(float value){ |
---|
| 65 | if(useFDR) return (this->getPValue(value) < this->pThreshold); |
---|
| 66 | else return (value > this->threshold); |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | // Functions to calculate the stats for a given array. |
---|
| 70 | // The idea here is that there are two options to do the calculations: |
---|
| 71 | // *The first just uses all the points in the array. If you need to |
---|
| 72 | // remove BLANK points (or something similar), do this beforehand. |
---|
| 73 | // *Alternatively, construct a bool array of the same size, showing which |
---|
| 74 | // points are good, and use the second option. |
---|
| 75 | void calculate(Type *array, long size); |
---|
| 76 | void calculate(Type *array, long size, bool *isGood); |
---|
| 77 | |
---|
| 78 | private: |
---|
| 79 | bool defined; // a flag indicating whether the stats are defined. |
---|
| 80 | |
---|
| 81 | // basic statistics |
---|
| 82 | float mean; |
---|
| 83 | float stddev; |
---|
| 84 | Type median; |
---|
| 85 | Type madfm; |
---|
| 86 | |
---|
| 87 | float threshold; // a threshold for simple sigma-clipping |
---|
| 88 | float pThreshold; // a threshold for the FDR case -- the upper limit |
---|
| 89 | // of P values that detected pixels can have. |
---|
| 90 | bool useRobust; // whether we use the two robust stats or not |
---|
| 91 | bool useFDR; // whether the FDR method is used for determining a |
---|
| 92 | // detection |
---|
| 93 | |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | #endif /*STATS_H*/ |
---|