HistAcc.h

Go to the documentation of this file.
00001 //# HistAcc.h: Histogram Accumulator
00002 //# Copyright (C) 1996,1999,2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id$
00028 
00029 #ifndef SCIMATH_HISTACC_H
00030 #define SCIMATH_HISTACC_H
00031 
00032 #include <casacore/casa/aips.h>
00033 #include <casacore/casa/Utilities/Fallible.h>
00034 #include <casacore/scimath/Mathematics/StatAcc.h>
00035 #include <casacore/casa/Containers/Block.h>
00036 #include <casacore/casa/iosfwd.h>
00037 
00038 namespace casacore { //# NAMESPACE CASACORE - BEGIN
00039 
00040 // forward declarations:
00041 template <class T> class Array;
00042 class String;
00043 
00044 // <reviewed reviewer="" date="" tests="tHistAcc" demos="">
00045 
00046 // <prerequisite>
00047 //   <li> module Arrays
00048 //   <li> <linkto module="Arrays:description">Arrays </linkto> module
00049 // </prerequisite>
00050 //
00051 // <summary> 
00052 // Makes a histogram from input values.
00053 // </summary>
00054 //
00055 // <etymology>
00056 // HistAcc stands for `Histogram Accumulator'. 
00057 // </etymology>
00058 //
00059 // <templating arg=T>
00060 // <li> The accepted input types are real, i.e. Int, uInt, Float, Double, 
00061 // but not Complex. 
00062 // </templating>
00063 
00064 // <synopsis>
00065 // Makes a histogram from input values. The histogram bin parameters
00066 // may be defined, or determined from the first n input values.
00067 // The input values are fed to HistAcc via the member function `put'. 
00068 // They can be fed individually, or in the form of an Array. 
00069 //
00070 // The histogram `bins' can be defined via the constructor in the
00071 // form of loop variables: low bin, high bin, bin-width.
00072 // It is also possible to let the bin parameters be determined 
00073 // automatically from the first n (e.g. n=50) input values.
00074 // If the actual nr of input values is less than n when the histogram
00075 // is interrogated in some way, the bin parameters will be determined 
00076 // from what is available.
00077 // </synopsis> 
00078 //
00079 // <example>
00080 // It is usually convenient to let the bins be defined automatically: 
00081 // <srcblock>
00082 //   Matrix<T> vv(30,100);        // an array of input values
00083 //   vv = ...                     // fill the array
00084 //   HistAcc<T> h(25);            // use the first 25 values to define bins 
00085 //   h.put(vv);                   // accumulate values into histogram 
00086 //   h.printHistogram(cout,"vv"); // print the histogram of vv
00087 //   Fallible<Double> median = h1.getMedian();  // return the median
00088 // </srcblock>
00089 //  
00090 // In some cases the bin parameters are pre-defined:
00091 // <srcblock>
00092 //   Vector<T> vv(100,0);        // a vector (array) of values
00093 //   vv = ...                    // fill the vector
00094 //   HistAcc<T> h(-10,20,3);     // bins with width 3, between -10 and 20
00095 //   h.put(vv);                  // accumulate values into histogram   
00096 //   uInt n = h.getSpurious(l,h);// get the number outside the bins
00097 // </srcblock>
00098 //
00099 // The internal statistics accumulator can be interrogated explicitly
00100 // or implicitly:
00101 // <srcblock>
00102 //   StatAcc<T> s = h.getStatistics();     // return the internal StatAcc
00103 //   Fallible<Double> mean = s.getMean();  // get the mean of the input values
00104 //   Fallible<Double> mean = h.getStatistics().getMean();  // alternative
00105 // </srcblock>
00106 
00107 // </example>
00108 //
00109 // <motivation>
00110 // </motivation>
00111 //
00112 // <todo asof="">
00113 // </todo>
00114 
00115 
00116 // *************************************************************************** 
00117 
00118 template<class T> class HistAcc  {
00119 public:
00120     // Constructors and destructor. If the bin-parameters low, high 
00121     // and width (for lowest and highest bin, and binwidth) are not
00122     // specified, they will be determined automatically from the
00123     // first nBuff input values (which are stored in a temporary buffer).
00124     // <group>
00125     HistAcc(const uInt nBuff);                 //# fully automatic  
00126     HistAcc(const uInt nBuff, const T width);  //# semi-automatic    
00127     HistAcc(const T low, const T high, const T width);  //# fully specified
00128     HistAcc(const HistAcc&);                   //# copy an existing one
00129     ~HistAcc(){;} 
00130     // </group>
00131 
00132     // Copy operations.
00133     // <group>
00134     void copy(const HistAcc&);           //# idem
00135     HistAcc& operator= (const HistAcc&); 
00136     // </group>
00137 
00138     // Accumulate (put) value(s) into the histogram.
00139     // <group>
00140     inline void put(const T v);           //# single value           
00141     void put(const Array<T>& vv);         //# array 
00142     void put(const Block<T>& vv);         //# block (simple array)
00143     // </group>
00144 
00145     // Reset the contents of the bins to zero, but retain the current 
00146     // bin definition. 
00147     void reset();                    
00148 
00149     // Empty all bins whose contents is < nmin (e.g. nmin=2). 
00150     // This is useful to remove `noise' values from the histogram.
00151     void emptyBinsWithLessThan(const uInt nmin);
00152 
00153     // The median is the 50-percentile (getPercentile(50)), i.e. the 
00154     // value which has 50 percent of the input values below it.
00155     // Calculation takes into account the spurious
00156     // input values, i.e. values that fell outside the bins.
00157     Fallible<T> getPercentile(const Float p); 
00158     Fallible<T> getMedian();                 
00159 
00160     // All bins have the same width.
00161     Fallible<T> getBinWidth() const;           
00162 
00163     // Get the internal Statistics accumulator (see StatAcc,h).
00164     // It can be used to obtain statistics of the input values.
00165     const StatAcc<T>& getStatistics();    
00166 
00167     // The return value is the nr of histogram bins, and is invalid
00168     // if the number is zero. The given blocks/vectors are resized,
00169     // and contain the contents and centre values of the bins. 
00170     Fallible<uInt> getHistogram(Block<uInt>& bins, Block<T>& values);
00171 
00172     // Get the nr of `spurious' values, i.e. the ones that fell
00173     // outside the defined bins. 
00174     uInt getSpurious(uInt& tooSmall, uInt& tooLarge);        
00175 
00176     // Print histogram.
00177     // <group>
00178     void printHistogram(ostream&, const String& caption); 
00179     // </group>
00180         
00181 private:
00182     Block<uInt> itsBinContents;   //# Contents of histogram bins
00183     Block<T> itsBinHighLimit;     //# High limit of each bin
00184     T itsUserDefinedBinWidth;     //# if defined
00185 
00186     StatAcc<T> itsStatAcc;        //# private Statistics Accumulator
00187 
00188     Bool itsAutoDefineMode;       //# If true: automatic mode
00189     Block<T> itsBuffer;           //# temporary storage of input T-values
00190     uInt itsBufferContents;       //# nr of T-values in buffer 
00191 
00192     // Accumulate a single value into the histogram.
00193     void put1(const T);
00194 
00195     // Definition of histogram bins with given parameters.
00196     void defineBins(const T low, const T high, const T width);
00197 
00198     // Internal helper functions for the automatic definition of
00199     // histogram parameters, using the contents of itsBuffer.  
00200     // <group> 
00201     void initBuffer(const uInt size); 
00202     void putBuffer(const T v);     //# add input value to itsBuffer 
00203     void clearBuffer();            //# transfer from buffer to bins
00204     void autoDefineBins(); 
00205     // </group>
00206 
00207     // Other internal helper function(s).
00208     // <group>
00209     void init();   
00210     Fallible<T> getBinValue(const uInt index) const;  //# bin centre value
00211     // </group>
00212 
00213 };
00214 
00215 
00216 
00217 //*************************** inline functions, have to be in HistAcc.h ****
00218 
00219 
00220 // Accumulate a single value:
00221 
00222 template<class T> 
00223 inline void HistAcc<T>::put(const T v) {
00224     put1(v);            
00225 }
00226 
00227 
00228 } //# NAMESPACE CASACORE - END
00229 
00230 #ifndef CASACORE_NO_AUTO_TEMPLATES
00231 #include <casacore/scimath/Mathematics/HistAcc.tcc>
00232 #endif //# CASACORE_NO_AUTO_TEMPLATES
00233 #endif
00234 
00235 
00236 
00237 
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1