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