ProfileFit1D.h

Go to the documentation of this file.
00001 //# ProfileFit1D.h: Class to fit profiles to vectors
00002 //# Copyright (C) 2004
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: ProfileFit1D.h 20299 2008-04-03 05:56:44Z gervandiepen $
00027 
00028 #ifndef COMPONENTS_PROFILEFIT1D_H
00029 #define COMPONENTS_PROFILEFIT1D_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/Vector.h>
00034 
00035 #include <components/SpectralComponents/SpectralList.h>
00036 #include <components/SpectralComponents/SpectralFit.h>
00037 
00038 #include <set>
00039 
00040 namespace casa {
00041 
00042 class SpectralElement;
00043 
00044 // <summary>
00045 // Fit spectral components to a Vector of data
00046 // </summary>
00047 
00048 // <use visibility=export>
00049 
00050 // <reviewed reviewer="" date="" tests="tProfileFit1D.cc">
00051 // </reviewed>
00052 
00053 // <prerequisite>
00054 //   <li> <linkto class="SpectralElement">SpectralElement</linkto> 
00055 //   <li> <linkto class="SpectralList">SpectralList</linkto> 
00056 //   <li> <linkto class="SpectralFit">SpectralFit</linkto> 
00057 // </prerequisite>
00058 
00059 // <synopsis> 
00060 // Fit lists (held in class SpectralList) of SpectralElements to a 
00061 // Vector of data.  Each SpectralElement can  be one from a variety
00062 // of types.  The values of the parameters for each SpectralElement 
00063 // provide the initial starting guesses for the fitting process.  
00064 // Also, a SpectralElement object holds a mask indicating whether 
00065 // a parameter should be held fixed or solved for.   After the 
00066 // fitting is done, a new SpectralList holding SpectralElements with 
00067 // the fitted parameters is created.  
00068 //
00069 // For all the functions that return a status Bool, True is good. If
00070 // False is returned, an error message can be recovered with function
00071 // <src>errorMessage</src>,  You should not proceed if False is returned.
00072 // </synopsis> 
00073 
00074 // <example>
00075 // <srcblock>
00076 // const uInt n = 512;
00077 // Vector<Double> x(n);
00078 // Vector<Double> y(n);
00079 // Vector<Bool> m(n);
00080 // 
00081 // // Code to fill data vectors x,y,m
00082 //
00083 // ProfileFit1D<Float> fitter; 
00084 // Bool ok = fitter.setData (x, y, m);
00085 // ok = fitter.setGaussianElements (2);              
00086 // ok = fitter.fit();       
00087 // const SpectralList& fitList = fitter.getList(True);
00088 // </srcblock>
00089 // </example>
00090 
00091 // <todo asof="2004/07/10">
00092 //   <li> Add constraints
00093 // </todo>
00094 //
00095 //
00096 //  <note role=caution>
00097 //  At the moment, because of templating limitations of the underlying
00098 //  fitting classes, you must use template type Double.  This restriction
00099 //  will be lifted in the future.
00100 //  </note>
00101 //
00102 
00103 template <class T> class ProfileFit1D 
00104 {
00105 public:
00106     // Constructor
00107     ProfileFit1D();
00108 
00109     // Destructor
00110     ~ProfileFit1D();
00111 
00112     // Copy constructor.  Uses copy semantics.
00113     ProfileFit1D(const ProfileFit1D& other);
00114 
00115     // Assignment operator. Uses copy semantics.
00116     ProfileFit1D& operator=(const ProfileFit1D& other);
00117 
00118     // Set abcissa, ordinate, mask and weights. A True mask value means the 
00119     // data are good. If you don't specify the weights vector, all weights 
00120     // are assumed to be unity.  If you don't specify a mask it will be
00121     // created as all good.
00122     // Status is returned, if False, error message can be recovered with <src>errorMessage</src>
00123     // <group>
00124     Bool setData (const Vector<Double>& x, const Vector<T>& y, const Vector<Bool>& mask,
00125                   const Vector<Double>& weight);
00126     Bool setData (const Vector<Double>& x, const Vector<T>& y, const Vector<Bool>& mask);
00127     Bool setData (const Vector<Double>& x, const Vector<T>& y);
00128     // </group>
00129 
00130     // Set a SpectralList of SpectralElements to fit for.
00131     // The SpectralElements in the list hold the
00132     // initial estimates and must reflect the abcissa and ordinate units.  
00133     // They also contain the information about whether
00134     // specific parameters are to be held fixed or allowed to vary in
00135     // the fitting process.
00136     // You can recover the list of elements with function getList.
00137     void setElements (const SpectralList& list);
00138 
00139     // Set a SpectralList of Gaussian SpectralElements to fit for.  
00140     // The initial estimates for the Gaussians will be automatically determined.
00141     // All of the parameters created by this function will be solved for
00142     // by default. You can recover the list of elements with function getList.
00143     // Status is returned, if False, error message can be 
00144     // recovered with <src>errorMessage</src>
00145     Bool setGaussianElements (uInt nGauss);
00146 
00147     // Add new SpectralElement(s) to the SpectralList (can be empty)
00148     // of SpectralElements to be fit for.  
00149     //<group>
00150     void addElement (const SpectralElement& el);
00151     void addElements (const SpectralList& list);
00152     // </group>
00153 
00154     // Clear the SpectralList of elements to be fit for
00155     void clearList ();
00156 
00157     // Set abscissa range mask.  You can specify a number of ranges
00158     // via a vector of start indices (or X values) and a vector of end
00159     // indices (or X values).   When argument insideIsGood is True,
00160     // a mask will be created which
00161     // will be True (good) inside the ranges and bad (False)
00162     // outside of those ranges.  When argument insideIsGood is False,
00163     // the mask will be False (bad) inside the ranges and 
00164     // True (good) outside of those ranges. When the data are fit, a total
00165     // mask is formed combining (via a logical AND) the 
00166     // data mask (setData) and this range mask.
00167     // Status is returned, if False, error message can be recovered with <src>errorMessage</src>
00168     // In the single set version, the values in the set indicate the pixels to set the mask for,
00169     // ie no ranges, just specific pixels are to be provided. In this case, specified values
00170     // which are greater than or equal to the number of pixels are tacitly ignored.
00171     // <group>
00172     Bool setXRangeMask (const Vector<uInt>& startIndex,
00173                        const Vector<uInt>& endIndex,
00174                        Bool insideIsGood=True);
00175     Bool setXRangeMask (const Vector<T>& startIndex,
00176                        const Vector<T>& endIndex,
00177                        Bool insideIsGood=True);
00178 
00179     Bool setXMask(const std::set<uInt>& indices, Bool specifiedPixelsAreGood);
00180     // </group>
00181 
00182     // Recover masks.  These are the data mask (setData) the range
00183     // mask (setRangeMask may be length zero) and the total 
00184     // mask combining the two.
00185     // <group>
00186     Vector<Bool> getDataMask() const {return itsDataMask;};
00187     Vector<Bool> getRangeMask() const {return itsRangeMask;}
00188     Vector<Bool> getTotalMask() const {return makeTotalMask();};
00189     // </group>
00190 
00191     // Do the fit and return status.  Returns convergence status.
00192     // Error conditions in the solution process will generate
00193     // an AipsError exception and you should catch these yourself.
00194     Bool fit ();
00195 
00196     // Get Chi Squared of fit
00197     Double getChiSquared () const {return itsFitter.chiSq();}
00198 
00199     // Get number of iterations for last fit
00200     Double getNumberIterations () const {return itsFitter.nIterations();}
00201 
00202     // Recover the list of elements.  You can get the elements
00203     // as initially estimated (fit=False), or after fitting 
00204     // (fit=True).  In the latter case, the SpectralElements
00205     // hold the parameters and errors of the fit.
00206     const SpectralList& getList (Bool fit=True) const;
00207 
00208     // Recover vectors for the estimate, fit and residual.
00209     // If you don't specify which element, all elements are included
00210     // If the Vectors are returned with zero length, it means an error
00211     // condition exists (e.g. asking for fit before you do one). In this
00212     // case an error message can be recovered with function <src>errorMessage</src>.
00213     //<group>
00214     Vector<T> getEstimate (Int which=-1) const;
00215     Vector<T> getFit (Int which=-1) const;
00216     Vector<T> getResidual (Int which=-1, Bool fit=True)  const;
00217     //</group>
00218 
00219     // Recover the error message
00220     String errorMessage () const {return itsError;};
00221 
00222 private:
00223    Vector<Double> itsX;                    // Abcissa (really should not be type T)
00224    Vector<T> itsY;                         // Ordinate 
00225    Vector<Double> itsWeight;               // Weights 
00226    Vector<Bool> itsDataMask;               // Data mask
00227    Vector<Bool> itsRangeMask;              // Mask associated with ranges
00228 //
00229    SpectralList itsList;                   // List of elements to fit for
00230 //
00231    SpectralFit itsFitter;                  // Fitter
00232    mutable String itsError;                // Error message
00233 
00234 // Functions
00235    Vector<Bool> makeTotalMask() const;
00236    SpectralList getSubsetList (const SpectralList& list, Int which)  const;
00237    void checkType() const;
00238    void copy(const ProfileFit1D<T>& other);
00239 };
00240 
00241 } //#End casa namespace
00242 #ifndef CASACORE_NO_AUTO_TEMPLATES
00243 #include <components/SpectralComponents/ProfileFit1D.tcc>
00244 #endif //# CASACORE_NO_AUTO_TEMPLATES
00245 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1