casacore
Loading...
Searching...
No Matches
LatticeFractile.h
Go to the documentation of this file.
1// # LatticeFractile.cc: Static functions to get median and fractiles
2// # Copyright (C) 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 LATTICES_LATTICEFRACTILE_H
27#define LATTICES_LATTICEFRACTILE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/ArrayFwd.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// # Forward Declarations
36template <class T>
37class Lattice;
38template <class T>
39class MaskedLattice;
40template <class T>
41class Block;
42
43// <summary>
44// Static functions to get median and fractiles of a lattice
45// </summary>
46
47// <use visibility=local>
48
49// <reviewed reviewer="" date="yyyy/mm/dd" tests="tLatticeFractile.cc tLELMedian.cc" demos="">
50// </reviewed>
51
52// <prerequisite>
53// <li> <linkto class="Lattice"> Lattice</linkto>
54// </prerequisite>
55
56// <synopsis>
57// This class contains a few static functions to find 1 or 2 fractiles
58// in a lattice. They are primarily used by the LEL classes, but can
59// also be used standalone.
60// <br>
61// A fractile is the same as a percentile be it that it is given as a
62// fraction instead of a percentage. A fraction of 0.5 yields the median.
63// <br>
64// When the lattice has a mask, only the masked-on elements are taken into
65// account. If all elements are masked_off, an empty Vector is returned
66// indicating that no fractiles were found.
67// <p>
68// The algorithm used depends on the size of the lattice.
69// Smallish lattices (i.e. not exceeding the argument smallSize)
70// are handled in one pass im memory.
71// For bigger lattices a multi-pass algorithm is used. First the
72// lattices is binned. Thereafter the algorithm continues with the
73// elements of the bins containing the fractiles. This continues
74// until the number of elements left is less than <src>smallSize</src>.
75// Typically only 2 passes are needed for a big image.
76// <br>
77// The algorithm is robust and takes possible rounding errors into account.
78// It also takes into account that the lattice can contain many equal values.
79// </synopsis>
80
81// <motivation>
82// Separated from file LELFunction.h to make it more commonly usable
83// and to make the source files more readable.
84// </motivation>
85
86// # <todo asof="2001/02/10">
87// # </todo>
88
89template <class T>
91 public:
92 // Determine the fractile of the given lattice. It returns the value
93 // of the lattice at the given fraction. A fraction of 0.5 returns
94 // the median. If the lattice has an even number of elements and if
95 // the lattice is small enough (< 100 elements), the median is the
96 // mean of the 2 middle elements.
97 // <br>If the lattice is masked, only masked-on elements are taken
98 // into account.
99 // <br>If the lattice is large, successive histograms are made until
100 // <src>smallSize</src> elements are left. Thereafter an in-memory
101 // algorithm will be used to finish.
102 // The number of passes made over the data is undetermined, but
103 // a typical number is 2 passes.
104 // <br>Normally a vector with 1 element is returned.
105 // If the lattice has no masked-on elements, an empty vector is returned.
106 // <group>
107 static Vector<T> unmaskedFractile(const Lattice<T>& lattice, Float fraction,
108 uInt smallSize = 4096 * 4096);
109 static Vector<T> maskedFractile(const MaskedLattice<T>& lattice, Float fraction,
110 uInt smallSize = 4096 * 4096);
111 // </group>
112
113 // Determine the values of the 2 elements at the given fractiles.
114 // Thus <src>left=0.25; right=0.75</src> gives the quartiles of the lattice.
115 // <br>If the lattice is masked, onlu masked-on elements are taken
116 // into account.
117 // <br>If the lattice is large, successive histograms are made until
118 // <src>smallSize</src> elements are left. Thereafter an in-memory
119 // algorithm will be used to finish.
120 // The number of passes made over the data is undetermined, but
121 // a typical number is 2 passes.
122 // <br>Normally a vector with 2 elements is returned.
123 // If the lattice has no masked-on elements, an empty vector is returned.
124 // <group>
125 static Vector<T> unmaskedFractiles(const Lattice<T>& lattice, Float left, Float right,
126 uInt smallSize = 4096 * 4096);
127 static Vector<T> maskedFractiles(const MaskedLattice<T>& lattice, Float left, Float right,
128 uInt smallSize = 4096 * 4096);
129 // </group>
130
131 private:
132 // Determine the fractile for a small masked lattice.
133 static Vector<T> smallMaskedFractile(const MaskedLattice<T>& lattice, Float fraction);
134
135 // Determine the fractiles for a small masked lattice.
136 static Vector<T> smallMaskedFractiles(const MaskedLattice<T>& lattice, Float left, Float right);
137
138 // Calculate the first histogram (with 10000 bins).
139 // Also calculate the minimum and maximum. It returns the number
140 // of masked-on values. Masked-off values are ignored.
141 // <group>
142 static uInt maskedHistogram(T& stv, T& endv, T& minv, T& maxv, Block<uInt>& hist,
143 Block<T>& boundaries, const MaskedLattice<T>& lattice);
144 static void unmaskedHistogram(T& stv, T& endv, T& minv, T& maxv, Block<uInt>& hist,
145 Block<T>& boundaries, const Lattice<T>& lattice);
146 // </group>
147
148 // Helper function which determines which bin in the histogram
149 // contains the passed index.
150 // On input fractileInx gives the index of the fractile in the entire
151 // histogram.
152 // On output stv and endv are set to the boundaries of the bin containing
153 // the index and fractileInx is set to the index in that bin.
154 // The nr of values in that bin is returned as the function value.
155 // minv and maxv are used as the outer limits, thus the first bin extends
156 // to minv and the last bin to maxv.
157 // If the bins are getting too small (i.e. if stv is nearly endv), 0 is
158 // returned. In that case endv contains the fractile.
159 static uInt findBin(uInt& fractileInx, T& stv, T& endv, T minv, T maxv, const Block<uInt>& hist,
160 const Block<T>& boundaries);
161};
162
163} // namespace casacore
164
165#ifndef CASACORE_NO_AUTO_TEMPLATES
166#include <casacore/lattices/LatticeMath/LatticeFractile.tcc>
167#endif // # CASACORE_NO_AUTO_TEMPLATES
168#endif
static Vector< T > unmaskedFractile(const Lattice< T > &lattice, Float fraction, uInt smallSize=4096 *4096)
Determine the fractile of the given lattice.
static uInt maskedHistogram(T &stv, T &endv, T &minv, T &maxv, Block< uInt > &hist, Block< T > &boundaries, const MaskedLattice< T > &lattice)
Calculate the first histogram (with 10000 bins).
static Vector< T > smallMaskedFractile(const MaskedLattice< T > &lattice, Float fraction)
Determine the fractile for a small masked lattice.
static Vector< T > smallMaskedFractiles(const MaskedLattice< T > &lattice, Float left, Float right)
Determine the fractiles for a small masked lattice.
static Vector< T > maskedFractiles(const MaskedLattice< T > &lattice, Float left, Float right, uInt smallSize=4096 *4096)
static uInt findBin(uInt &fractileInx, T &stv, T &endv, T minv, T maxv, const Block< uInt > &hist, const Block< T > &boundaries)
Helper function which determines which bin in the histogram contains the passed index.
static void unmaskedHistogram(T &stv, T &endv, T &minv, T &maxv, Block< uInt > &hist, Block< T > &boundaries, const Lattice< T > &lattice)
static Vector< T > unmaskedFractiles(const Lattice< T > &lattice, Float left, Float right, uInt smallSize=4096 *4096)
Determine the values of the 2 elements at the given fractiles.
static Vector< T > maskedFractile(const MaskedLattice< T > &lattice, Float fraction, uInt smallSize=4096 *4096)
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
float Float
Definition aipstype.h:52