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