casacore
Loading...
Searching...
No Matches
StatAcc.h
Go to the documentation of this file.
1// # StatAcc.h: Statistics 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_STATACC_H
27#define SCIMATH_STATACC_H
28
29#include <optional>
30
31#include <casacore/casa/aips.h>
32#include <casacore/casa/BasicMath/Math.h>
33#include <casacore/casa/iosfwd.h>
34#include <casacore/casa/Arrays/ArrayFwd.h>
35
36namespace casacore { // # NAMESPACE CASACORE - BEGIN
37
38// forward declarations:
39template <class T>
40class Block;
41class String;
42
43// <reviewed reviewer="" date="" tests="tStatAcc" demos="">
44
45// <prerequisite>
46// <li> module Arrays
47// <li> <linkto module="Arrays:description">Arrays </linkto> module
48// </prerequisite>
49//
50// <summary>
51// A statistics accumulator
52// </summary>
53//
54// <etymology>
55// StatAcc stands for `Statistics Accumulator'.
56// </etymology>
57//
58// <templating arg=T>
59// <li> A statistics accumulator accepts (weighted) input values and
60// calculates simple statistice (min, max, weighted mean, rms etc).
61// The accepted input types are real, i.e. Int, uInt, Float, Double,
62// but not Complex. The reason for this is that the < operator of
63// Complex (needed for min/max) works on the norm in any case, and
64// the sqrt function (needed for rms) yields an ambiguous result.
65//
66// Restriction to real types also allows the internal arithmetic type
67// to be Double rather than the input type. The latter would give
68// all kinds of complications with weighting, accuracy and overflow
69// if the input type would be Int or uInt.
70// </templating>
71
72// <synopsis>
73// The (weighted) values are fed to StatAcc via the member function
74// `put'. They can be fed individually, or in the form of an
75// Array. The weights are optional (default = 1) and always have
76// type Float.
77//
78// Asking for a result does not change the internal state. The
79// type of the returned results is always std::optional<Double>.
80// A result is invalid if no input values with non-zero weight
81// have been accumulated yet.
82//
83// The accumulator
84// can be re-initialised with the function `reset'. Accumulators
85// can be added to each other, which is as if their combined values had been
86// accumulated in the same accumulator.
87//
88// Some functions have been provided to display a summary of the
89// statistics results. One may choose between a one-line format
90// (with an optional associated header line), and a list.
91// </synopsis>
92//
93// <example>
94// <srcblock>
95// StatAcc<T> s; // T is Float, Double, Int etc
96// Matrix<T> vv(2,5); // a matrix (array) of input values
97// Matrix<Float> wgt(2,5); // an associated matrix of weights
98// .... fill vv and wgt with values and individual weights ...
99// s.put(vv,wgt); // accumulate the weighted values
100// std::optional<Double> min = s.getMin(); // return the minimum value
101//
102// s.reset(); // re-initialise
103// s.put(vv); // if wgt omitted, default = 1.0
104// if (s.getRms().isValid() { // check validity of rms
105// ... use it ...
106// }
107// </srcblock>
108// </example>
109//
110// <motivation>
111// One often needs simple statistics of a series of values, which
112// may occur by themselves or in arrays at various points in a program.
113// Sincs it is a pain to have to assign accumulation variables, and to
114// write statistics evaluation code (including exceptions),
115// this helper class is provided.
116// </motivation>
117//
118// <todo asof="">
119// </todo>
120
121// ***************************************************************************
122
123template <class T>
124class StatAcc {
125 public:
126 // constructors and destructor.
127 // <group>
130 ~StatAcc() { ; }
131 // </group>
132
133 // Reset or copy the accumulator attributes.
134 // <group>
135 void reset();
136 void copy(const StatAcc&);
137 // </group>
138
139 // Operators for adding and copying accumulators.
140 // <group>
144 // </group>
145
146 // Accumulate input value(s) v with weight w.
147 // If weight is omitted, the default=1.
148 // <group>
149 inline void put(const T v);
150 inline void put(const T v, const Float w);
151 void put(const Array<T>& v);
152 void put(const Array<T>& v, const Array<Float>& w);
153 void put(const Block<T>& v);
154 void put(const Block<T>& v, const Block<Float>& w);
155 // </group>
156
157 // Get statistics results one at a time.
158 // Count is the nr of values accumulated.
159 // Wtot is the sum of the weights.
160 // Rms is defined w.r.t. the mean, and is the square of Variance.
161 // RmsAbs is the root-mean-square of the absolute input values.
162 // <group>
164 uInt getCount() const;
165 std::optional<Double> getMin() const;
166 std::optional<Double> getMax() const;
167 std::optional<Double> getMean() const;
168 std::optional<Double> getRms() const;
169 std::optional<Double> getVariance() const;
170 std::optional<Double> getRmsAbs() const;
171 // </group>
172
173 // Print summary of accumulated statistics.
174 // Line is a one-line summary, including the (short) caption.
175 // LineHeader gives a one-line explanation of the numbers.
176 // List uses a separate line for each result (mean, max etc).
177 // <group>
178 void printSummaryList(std::ostream&, const String& caption) const;
179 void printSummaryLine(std::ostream&, const String& caption) const;
180 void printSummaryLineHeader(std::ostream&, const String& caption) const;
181 // </group>
182
183 private:
184 Double itsWtot; // # Sum of weights
185 Double itsWsum; // # Sum of weighted values
186 Double itsWssum; // # Sum of weighted squares
187 Double itsMin; // # Minimum value
188 Double itsMax; // # Maximum value
189 uInt itsCount; // # Number of samples
190
191 // Accumulate a single weighted value.
192 void put1(const T, const Float);
193};
194
195//*************************** inline functions, have to be in StatAcc.h ****
196
197// Accumulate a single value:
198
199template <class T>
200inline void StatAcc<T>::put(const T v) {
201 put1(v, 1); // default weight = 1
202}
203
204template <class T>
205inline void StatAcc<T>::put(const T v, const Float w) {
206 put1(v, w);
207}
208
209} // namespace casacore
210
211#ifndef CASACORE_NO_AUTO_TEMPLATES
212#include <casacore/scimath/Mathematics/StatAcc.tcc>
213#endif // # CASACORE_NO_AUTO_TEMPLATES
214#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
String operator+(const String &lhs, const String &rhs)
Global concatenation operators.
Definition String.h:816
StatAcc()
A statistics accumulator.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
void put1(const T)
Accumulate a single value into the histogram.
std::optional< Double > getMean() const
void printSummaryList(std::ostream &, const String &caption) const
Print summary of accumulated statistics.
unsigned int uInt
Definition aipstype.h:49
void printSummaryLine(std::ostream &, const String &caption) const
std::optional< Double > getMax() const
std::optional< Double > getRmsAbs() const
void reset()
Reset the contents of the bins to zero, but retain the current bin definition.
uInt itsCount
Definition StatAcc.h:189
void copy(double *target, int npixels) const
std::optional< Double > getRms() const
~StatAcc()
Definition StatAcc.h:130
float Float
Definition aipstype.h:52
std::optional< Double > getMin() const
StatAcc & operator+=(const StatAcc &)
Double itsWsum
Definition StatAcc.h:185
Double itsWtot
Definition StatAcc.h:184
uInt getCount() const
Double itsMax
Definition StatAcc.h:188
Double getWtot() const
Get statistics results one at a time.
void printSummaryLineHeader(std::ostream &, const String &caption) const
void put(const T v)
Accumulate (put) value(s) into the histogram.
double Double
Definition aipstype.h:53
std::optional< Double > getVariance() const
Double itsMin
Definition StatAcc.h:187
Double itsWssum
Definition StatAcc.h:186
Block< T > & operator=(const T &val)
Set all values in the block to "val".
Definition Block.h:536