casacore
Loading...
Searching...
No Matches
HistAcc.h
Go to the documentation of this file.
1//# HistAcc.h: Histogram Accumulator
2//# Copyright (C) 1996,1999,2000,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_HISTACC_H
27#define SCIMATH_HISTACC_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Utilities/Fallible.h>
31#include <casacore/scimath/Mathematics/StatAcc.h>
32#include <casacore/casa/Containers/Block.h>
33#include <casacore/casa/iosfwd.h>
34#include <casacore/casa/Arrays/ArrayFwd.h>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// forward declarations:
39class String;
40
41// <reviewed reviewer="" date="" tests="tHistAcc" demos="">
42
43// <prerequisite>
44// <li> module Arrays
45// <li> <linkto module="Arrays:description">Arrays </linkto> module
46// </prerequisite>
47//
48// <summary>
49// Makes a histogram from input values.
50// </summary>
51//
52// <etymology>
53// HistAcc stands for `Histogram Accumulator'.
54// </etymology>
55//
56// <templating arg=T>
57// <li> The accepted input types are real, i.e. Int, uInt, Float, Double,
58// but not Complex.
59// </templating>
60
61// <synopsis>
62// Makes a histogram from input values. The histogram bin parameters
63// may be defined, or determined from the first n input values.
64// The input values are fed to HistAcc via the member function `put'.
65// They can be fed individually, or in the form of an Array.
66//
67// The histogram `bins' can be defined via the constructor in the
68// form of loop variables: low bin, high bin, bin-width.
69// It is also possible to let the bin parameters be determined
70// automatically from the first n (e.g. n=50) input values.
71// If the actual nr of input values is less than n when the histogram
72// is interrogated in some way, the bin parameters will be determined
73// from what is available.
74// </synopsis>
75//
76// <example>
77// It is usually convenient to let the bins be defined automatically:
78// <srcblock>
79// Matrix<T> vv(30,100); // an array of input values
80// vv = ... // fill the array
81// HistAcc<T> h(25); // use the first 25 values to define bins
82// h.put(vv); // accumulate values into histogram
83// h.printHistogram(cout,"vv"); // print the histogram of vv
84// Fallible<Double> median = h1.getMedian(); // return the median
85// </srcblock>
86//
87// In some cases the bin parameters are pre-defined:
88// <srcblock>
89// Vector<T> vv(100,0); // a vector (array) of values
90// vv = ... // fill the vector
91// HistAcc<T> h(-10,20,3); // bins with width 3, between -10 and 20
92// h.put(vv); // accumulate values into histogram
93// uInt n = h.getSpurious(l,h);// get the number outside the bins
94// </srcblock>
95//
96// The internal statistics accumulator can be interrogated explicitly
97// or implicitly:
98// <srcblock>
99// StatAcc<T> s = h.getStatistics(); // return the internal StatAcc
100// Fallible<Double> mean = s.getMean(); // get the mean of the input values
101// Fallible<Double> mean = h.getStatistics().getMean(); // alternative
102// </srcblock>
103
104// </example>
105//
106// <motivation>
107// </motivation>
108//
109// <todo asof="">
110// </todo>
111
112
113// ***************************************************************************
114
115template<class T> class HistAcc {
116public:
117 // Constructors and destructor. If the bin-parameters low, high
118 // and width (for lowest and highest bin, and binwidth) are not
119 // specified, they will be determined automatically from the
120 // first nBuff input values (which are stored in a temporary buffer).
121 // <group>
122 HistAcc(const uInt nBuff); //# fully automatic
123 HistAcc(const uInt nBuff, const T width); //# semi-automatic
124 HistAcc(const T low, const T high, const T width); //# fully specified
125 HistAcc(const HistAcc&); //# copy an existing one
127 // </group>
128
129 // Copy operations.
130 // <group>
131 void copy(const HistAcc&); //# idem
133 // </group>
134
135 // Accumulate (put) value(s) into the histogram.
136 // <group>
137 inline void put(const T v); //# single value
138 void put(const Array<T>& vv); //# array
139 void put(const Block<T>& vv); //# block (simple array)
140 // </group>
141
142 // Reset the contents of the bins to zero, but retain the current
143 // bin definition.
144 void reset();
145
146 // Empty all bins whose contents is < nmin (e.g. nmin=2).
147 // This is useful to remove `noise' values from the histogram.
148 void emptyBinsWithLessThan(const uInt nmin);
149
150 // The median is the 50-percentile (getPercentile(50)), i.e. the
151 // value which has 50 percent of the input values below it.
152 // Calculation takes into account the spurious
153 // input values, i.e. values that fell outside the bins.
156
157 // All bins have the same width.
159
160 // Get the internal Statistics accumulator (see StatAcc,h).
161 // It can be used to obtain statistics of the input values.
163
164 // The return value is the nr of histogram bins, and is invalid
165 // if the number is zero. The given blocks/vectors are resized,
166 // and contain the contents and centre values of the bins.
168
169 // Get the nr of `spurious' values, i.e. the ones that fell
170 // outside the defined bins.
171 uInt getSpurious(uInt& tooSmall, uInt& tooLarge);
172
173 // Print histogram.
174 // <group>
175 void printHistogram(ostream&, const String& caption);
176 // </group>
177
178private:
179 Block<uInt> itsBinContents; //# Contents of histogram bins
180 Block<T> itsBinHighLimit; //# High limit of each bin
181 T itsUserDefinedBinWidth; //# if defined
182
183 StatAcc<T> itsStatAcc; //# private Statistics Accumulator
184
185 Bool itsAutoDefineMode; //# If true: automatic mode
186 Block<T> itsBuffer; //# temporary storage of input T-values
187 uInt itsBufferContents; //# nr of T-values in buffer
188
189 // Accumulate a single value into the histogram.
190 void put1(const T);
191
192 // Definition of histogram bins with given parameters.
193 void defineBins(const T low, const T high, const T width);
194
195 // Internal helper functions for the automatic definition of
196 // histogram parameters, using the contents of itsBuffer.
197 // <group>
198 void initBuffer(const uInt size);
199 void putBuffer(const T v); //# add input value to itsBuffer
200 void clearBuffer(); //# transfer from buffer to bins
202 // </group>
203
204 // Other internal helper function(s).
205 // <group>
206 void init();
207 Fallible<T> getBinValue(const uInt index) const; //# bin centre value
208 // </group>
209
210};
211
212
213
214//*************************** inline functions, have to be in HistAcc.h ****
215
216
217// Accumulate a single value:
218
219template<class T>
220inline void HistAcc<T>::put(const T v) {
221 put1(v);
222}
223
224
225} //# NAMESPACE CASACORE - END
226
227#ifndef CASACORE_NO_AUTO_TEMPLATES
228#include <casacore/scimath/Mathematics/HistAcc.tcc>
229#endif //# CASACORE_NO_AUTO_TEMPLATES
230#endif
231
232
233
234
235
236
237
238
239
240
241
242
simple 1-D array
Definition Block.h:198
Mark a value as valid or invalid.
Definition Fallible.h:121
Makes a histogram from input values.
Definition HistAcc.h:115
void put(const T v)
Accumulate (put) value(s) into the histogram.
Definition HistAcc.h:220
const StatAcc< T > & getStatistics()
Get the internal Statistics accumulator (see StatAcc,h).
void put1(const T)
Accumulate a single value into the histogram.
HistAcc(const uInt nBuff)
Constructors and destructor.
void emptyBinsWithLessThan(const uInt nmin)
Empty all bins whose contents is < nmin (e.g.
Block< T > itsBinHighLimit
Definition HistAcc.h:180
Fallible< T > getMedian()
void copy(const HistAcc &)
Copy operations.
void defineBins(const T low, const T high, const T width)
Definition of histogram bins with given parameters.
StatAcc< T > itsStatAcc
Definition HistAcc.h:183
Bool itsAutoDefineMode
Definition HistAcc.h:185
Fallible< T > getPercentile(const Float p)
The median is the 50-percentile (getPercentile(50)), i.e.
Block< T > itsBuffer
Definition HistAcc.h:186
void put(const Array< T > &vv)
void printHistogram(ostream &, const String &caption)
Print histogram.
void putBuffer(const T v)
uInt itsBufferContents
Definition HistAcc.h:187
Block< uInt > itsBinContents
Definition HistAcc.h:179
HistAcc(const uInt nBuff, const T width)
void reset()
Reset the contents of the bins to zero, but retain the current bin definition.
uInt getSpurious(uInt &tooSmall, uInt &tooLarge)
Get the nr of ‘spurious’ values, i.e.
void put(const Block< T > &vv)
HistAcc(const T low, const T high, const T width)
void initBuffer(const uInt size)
Internal helper functions for the automatic definition of histogram parameters, using the contents of...
Fallible< uInt > getHistogram(Block< uInt > &bins, Block< T > &values)
The return value is the nr of histogram bins, and is invalid if the number is zero.
HistAcc(const HistAcc &)
void init()
Other internal helper function(s).
Fallible< T > getBinValue(const uInt index) const
Fallible< T > getBinWidth() const
All bins have the same width.
HistAcc & operator=(const HistAcc &)
A statistics accumulator
Definition StatAcc.h:122
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
float Float
Definition aipstype.h:52
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40