casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LatticeFractile.h
Go to the documentation of this file.
00001 //# LatticeFractile.cc: Static functions to get median and fractiles
00002 //# Copyright (C) 1999,2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: LatticeFractile.h 20229 2008-01-29 15:19:06Z gervandiepen $
00027 
00028 #ifndef LATTICES_LATTICEFRACTILE_H
00029 #define LATTICES_LATTICEFRACTILE_H
00030 
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 //# Forward Declarations
00038 template<class T> class Lattice;
00039 template<class T> class MaskedLattice;
00040 template<class T> class Vector;
00041 template<class T> class Block;
00042 
00043 
00044 // <summary>
00045 // Static functions to get median and fractiles of a lattice
00046 // </summary>
00047 
00048 // <use visibility=local>
00049 
00050 // <reviewed reviewer="" date="yyyy/mm/dd" tests="tLatticeFractile.cc tLELMedian.cc" demos="">
00051 // </reviewed>
00052 
00053 // <prerequisite>
00054 //   <li> <linkto class="Lattice"> Lattice</linkto>
00055 // </prerequisite>
00056 
00057 // <synopsis>
00058 // This class contains a few static functions to find 1 or 2 fractiles
00059 // in a lattice. They are primarily used by the LEL classes, but can
00060 // also be used standalone.
00061 // <br>
00062 // A fractile is the same as a percentile be it that it is given as a
00063 // fraction instead of a percentage. A fraction of 0.5 yields the median.
00064 // <br>
00065 // When the lattice has a mask, only the masked-on elements are taken into
00066 // account. If all elements are masked_off, an empty Vector is returned
00067 // indicating that no fractiles were found.
00068 // <p>
00069 // The algorithm used depends on the size of the lattice.
00070 // Smallish lattices (i.e. not exceeding the argument smallSize)
00071 // are handled in one pass im memory.
00072 // For bigger lattices a multi-pass algorithm is used. First the
00073 // lattices is binned. Thereafter the algorithm continues with the
00074 // elements of the bins containing the fractiles. This continues
00075 // until the number of elements left is less than <src>smallSize</src>.
00076 // Typically only 2 passes are needed for a big image.
00077 // <br>
00078 // The algorithm is robust and takes possible rounding errors into account.
00079 // It also takes into account that the lattice can contain many equal values.
00080 // </synopsis> 
00081 
00082 // <motivation>
00083 // Separated from file LELFunction.h to make it more commonly usable
00084 // and to make the source files more readable.
00085 // </motivation>
00086 
00087 //# <todo asof="2001/02/10">
00088 //# </todo>
00089 
00090 
00091 template<class T> class LatticeFractile
00092 {
00093 public: 
00094   // Determine the fractile of the given lattice. It returns the value
00095   // of the lattice at the given fraction. A fraction of 0.5 returns
00096   // the median. If the lattice has an even number of elements and if
00097   // the lattice is small enough (< 100 elements), the median is the
00098   // mean of the 2 middle elements.
00099   // <br>If the lattice is masked, only masked-on elements are taken
00100   // into account.
00101   // <br>If the lattice is large, successive histograms are made until
00102   // <src>smallSize</src> elements are left. Thereafter an in-memory
00103   // algorithm will be used to finish.
00104   // The number of passes made over the data is undetermined, but
00105   // a typical number is 2 passes.
00106   // <br>Normally a vector with 1 element is returned.
00107   // If the lattice has no masked-on elements, an empty vector is returned.
00108   // <group>
00109   static Vector<T> unmaskedFractile (const Lattice<T>& lattice,
00110                                      Float fraction,
00111                                      uInt smallSize = 4096*4096);
00112   static Vector<T> maskedFractile (const MaskedLattice<T>& lattice,
00113                                    Float fraction,
00114                                    uInt smallSize = 4096*4096);
00115   // </group>
00116 
00117   // Determine the values of the 2 elements at the given fractiles.
00118   // Thus <src>left=0.25; right=0.75</src> gives the quartiles of the lattice.
00119   // <br>If the lattice is masked, onlu masked-on elements are taken
00120   // into account.
00121   // <br>If the lattice is large, successive histograms are made until
00122   // <src>smallSize</src> elements are left. Thereafter an in-memory
00123   // algorithm will be used to finish.
00124   // The number of passes made over the data is undetermined, but
00125   // a typical number is 2 passes.
00126   // <br>Normally a vector with 2 elements is returned.
00127   // If the lattice has no masked-on elements, an empty vector is returned.
00128   // <group>
00129   static Vector<T> unmaskedFractiles (const Lattice<T>& lattice,
00130                                       Float left, Float right,
00131                                       uInt smallSize = 4096*4096);
00132   static Vector<T> maskedFractiles (const MaskedLattice<T>& lattice,
00133                                     Float left, Float right,
00134                                     uInt smallSize = 4096*4096);
00135   // </group>
00136 
00137 private:
00138   // Determine the fractile for a small masked lattice.
00139   static Vector<T> smallMaskedFractile (const MaskedLattice<T>& lattice,
00140                                         Float fraction);
00141 
00142   // Determine the fractiles for a small masked lattice.
00143   static Vector<T> smallMaskedFractiles (const MaskedLattice<T>& lattice,
00144                                          Float left, Float right);
00145 
00146   // Calculate the first histogram (with 10000 bins).
00147   // Also calculate the minimum and maximum. It returns the number
00148   // of masked-on values. Masked-off values are ignored.
00149   // <group>
00150   static uInt maskedHistogram (T& stv, T& endv, T& minv, T& maxv,
00151                                Block<uInt>& hist,
00152                                Block<T>& boundaries,
00153                                const MaskedLattice<T>& lattice);
00154   static void unmaskedHistogram (T& stv, T& endv, T& minv, T& maxv,
00155                                  Block<uInt>& hist,
00156                                  Block<T>& boundaries,
00157                                  const Lattice<T>& lattice);
00158   // </group>
00159 
00160   // Helper function which determines which bin in the histogram 
00161   // contains the passed index. 
00162   // On input fractileInx gives the index of the fractile in the entire
00163   // histogram.
00164   // On output stv and endv are set to the boundaries of the bin containing
00165   // the index and fractileInx is set to the index in that bin.
00166   // The nr of values in that bin is returned as the function value.
00167   // minv and maxv are used as the outer limits, thus the first bin extends
00168   // to minv and the last bin to maxv.
00169   // If the bins are getting too small (i.e. if stv is nearly endv), 0 is
00170   // returned. In that case endv contains the fractile.
00171   static uInt findBin (uInt& fractileInx,
00172                        T& stv, T& endv,
00173                        T minv, T maxv,
00174                        const Block<uInt>& hist,
00175                        const Block<T>& boundaries);
00176 };
00177 
00178 
00179 
00180 } //# NAMESPACE CASA - END
00181 
00182 #ifndef CASACORE_NO_AUTO_TEMPLATES
00183 #include <lattices/Lattices/LatticeFractile.tcc>
00184 #endif //# CASACORE_NO_AUTO_TEMPLATES
00185 #endif