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/scimath/Mathematics/StatAcc.h>
31#include <casacore/casa/Containers/Block.h>
32#include <casacore/casa/iosfwd.h>
33#include <casacore/casa/Arrays/ArrayFwd.h>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// forward declarations:
38class String;
39
40// <reviewed reviewer="" date="" tests="tHistAcc" demos="">
41
42// <prerequisite>
43// <li> module Arrays
44// <li> <linkto module="Arrays:description">Arrays </linkto> module
45// </prerequisite>
46//
47// <summary>
48// Makes a histogram from input values.
49// </summary>
50//
51// <etymology>
52// HistAcc stands for `Histogram Accumulator'.
53// </etymology>
54//
55// <templating arg=T>
56// <li> The accepted input types are real, i.e. Int, uInt, Float, Double,
57// but not Complex.
58// </templating>
59
60// <synopsis>
61// Makes a histogram from input values. The histogram bin parameters
62// may be defined, or determined from the first n input values.
63// The input values are fed to HistAcc via the member function `put'.
64// They can be fed individually, or in the form of an Array.
65//
66// The histogram `bins' can be defined via the constructor in the
67// form of loop variables: low bin, high bin, bin-width.
68// It is also possible to let the bin parameters be determined
69// automatically from the first n (e.g. n=50) input values.
70// If the actual nr of input values is less than n when the histogram
71// is interrogated in some way, the bin parameters will be determined
72// from what is available.
73// </synopsis>
74//
75// <example>
76// It is usually convenient to let the bins be defined automatically:
77// <srcblock>
78// Matrix<T> vv(30,100); // an array of input values
79// vv = ... // fill the array
80// HistAcc<T> h(25); // use the first 25 values to define bins
81// h.put(vv); // accumulate values into histogram
82// h.printHistogram(cout,"vv"); // print the histogram of vv
83// std::optional<Double> median = h1.getMedian(); // return the median
84// </srcblock>
85//
86// In some cases the bin parameters are pre-defined:
87// <srcblock>
88// Vector<T> vv(100,0); // a vector (array) of values
89// vv = ... // fill the vector
90// HistAcc<T> h(-10,20,3); // bins with width 3, between -10 and 20
91// h.put(vv); // accumulate values into histogram
92// uInt n = h.getSpurious(l,h);// get the number outside the bins
93// </srcblock>
94//
95// The internal statistics accumulator can be interrogated explicitly
96// or implicitly:
97// <srcblock>
98// StatAcc<T> s = h.getStatistics(); // return the internal StatAcc
99// std::optional<Double> mean = s.getMean(); // get the mean of the input values
100// std::optional<Double> mean = h.getStatistics().getMean(); // alternative
101// </srcblock>
102
103// </example>
104//
105// <motivation>
106// </motivation>
107//
108// <todo asof="">
109// </todo>
110
111// ***************************************************************************
112
113template <class T>
114class HistAcc {
115 public:
116 // Constructors and destructor. If the bin-parameters low, high
117 // and width (for lowest and highest bin, and binwidth) are not
118 // specified, they will be determined automatically from the
119 // first nBuff input values (which are stored in a temporary buffer).
120 // <group>
121 HistAcc(const uInt nBuff); // # fully automatic
122 HistAcc(const uInt nBuff, const T width); // # semi-automatic
123 HistAcc(const T low, const T high, const T width); // # fully specified
124 HistAcc(const HistAcc&); // # copy an existing one
125 ~HistAcc() { ; }
126 // </group>
127
128 // Copy operations.
129 // <group>
130 void copy(const HistAcc&); // # idem
132 // </group>
133
134 // Accumulate (put) value(s) into the histogram.
135 // <group>
136 inline void put(const T v); // # single value
137 void put(const Array<T>& vv); // # array
138 void put(const Block<T>& vv); // # block (simple array)
139 // </group>
140
141 // Reset the contents of the bins to zero, but retain the current
142 // bin definition.
143 void reset();
144
145 // Empty all bins whose contents is < nmin (e.g. nmin=2).
146 // This is useful to remove `noise' values from the histogram.
147 void emptyBinsWithLessThan(const uInt nmin);
148
149 // The median is the 50-percentile (getPercentile(50)), i.e. the
150 // value which has 50 percent of the input values below it.
151 // Calculation takes into account the spurious
152 // input values, i.e. values that fell outside the bins.
153 std::optional<T> getPercentile(const Float p);
154 std::optional<T> getMedian();
155
156 // All bins have the same width.
157 std::optional<T> getBinWidth() const;
158
159 // Get the internal Statistics accumulator (see StatAcc,h).
160 // It can be used to obtain statistics of the input values.
162
163 // The return value is the nr of histogram bins, and is invalid
164 // if the number is zero. The given blocks/vectors are resized,
165 // and contain the contents and centre values of the bins.
166 std::optional<uInt> getHistogram(Block<uInt>& bins, Block<T>& values);
167
168 // Get the nr of `spurious' values, i.e. the ones that fell
169 // outside the defined bins.
170 uInt getSpurious(uInt& tooSmall, uInt& tooLarge);
171
172 // Print histogram.
173 // <group>
174 void printHistogram(ostream&, const String& caption);
175 // </group>
176
177 private:
178 Block<uInt> itsBinContents; // # Contents of histogram bins
179 Block<T> itsBinHighLimit; // # High limit of each bin
180 T itsUserDefinedBinWidth; // # if defined
181
182 StatAcc<T> itsStatAcc; // # private Statistics Accumulator
183
184 Bool itsAutoDefineMode; // # If true: automatic mode
185 Block<T> itsBuffer; // # temporary storage of input T-values
186 uInt itsBufferContents; // # nr of T-values in buffer
187
188 // Accumulate a single value into the histogram.
189 void put1(const T);
190
191 // Definition of histogram bins with given parameters.
192 void defineBins(const T low, const T high, const T width);
193
194 // Internal helper functions for the automatic definition of
195 // histogram parameters, using the contents of itsBuffer.
196 // <group>
197 void initBuffer(const uInt size);
198 void putBuffer(const T v); // # add input value to itsBuffer
199 void clearBuffer(); // # transfer from buffer to bins
201 // </group>
202
203 // Other internal helper function(s).
204 // <group>
205 void init();
206 std::optional<T> getBinValue(const uInt index) const; // # bin centre value
207 // </group>
208};
209
210//*************************** inline functions, have to be in HistAcc.h ****
211
212// Accumulate a single value:
213
214template <class T>
215inline void HistAcc<T>::put(const T v) {
216 put1(v);
217}
218
219} // namespace casacore
220
221#ifndef CASACORE_NO_AUTO_TEMPLATES
222#include <casacore/scimath/Mathematics/HistAcc.tcc>
223#endif // # CASACORE_NO_AUTO_TEMPLATES
224#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
HistAcc(const uInt nBuff)
Makes a histogram from input values.
StatAcc()
A statistics accumulator.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
void defineBins(const T low, const T high, const T width)
Definition of histogram bins with given parameters.
void autoDefineBins()
void clearBuffer()
std::optional< T > getBinWidth() const
All bins have the same width.
void put1(const T)
Accumulate a single value into the histogram.
Block< T > itsBinHighLimit
Definition HistAcc.h:179
void putBuffer(const T v)
~HistAcc()
Definition HistAcc.h:125
uInt itsBufferContents
Definition HistAcc.h:186
void printHistogram(ostream &, const String &caption)
Get the nr of `spurious' values, i.e.
unsigned int uInt
Definition aipstype.h:49
void initBuffer(const uInt size)
Internal helper functions for the automatic definition of histogram parameters, using the contents of...
std::optional< 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.
void reset()
Reset the contents of the bins to zero, but retain the current bin definition.
std::optional< T > getPercentile(const Float p)
Empty all bins whose contents is < nmin (e.g.
void copy(double *target, int npixels) const
std::optional< T > getMedian()
float Float
Definition aipstype.h:52
StatAcc< T > itsStatAcc
Definition HistAcc.h:182
std::optional< T > getBinValue(const uInt index) const
void init()
Other internal helper function(s).
Block< uInt > itsBinContents
Definition HistAcc.h:178
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
size_t size() const
Definition Block.h:566
Block< T > itsBuffer
Definition HistAcc.h:185
T itsUserDefinedBinWidth
Definition HistAcc.h:180
void put(const T v)
Accumulate (put) value(s) into the histogram.
Bool itsAutoDefineMode
Definition HistAcc.h:184
const StatAcc< T > & getStatistics()
Get the internal Statistics accumulator (see StatAcc,h).
Block< T > & operator=(const T &val)
Set all values in the block to "val".
Definition Block.h:536