LCOV - code coverage report
Current view: top level - components/SpectralComponents - SpectralFit2.tcc (source / functions) Hit Total Coverage
Test: casacpp_coverage.info Lines: 76 83 91.6 %
Date: 2024-11-06 17:42:47 Functions: 1 2 50.0 %

          Line data    Source code
       1             : //# SpectralFit2.cc: Least Squares fitting of spectral elements: templated part
       2             : //# Copyright (C) 2001,2002,2004
       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             : //# $Id: SpectralFit2.tcc 19935 2007-02-27 05:07:40Z Malte.Marquarding $
      27             : 
      28             : //# Includes
      29             : #include <components/SpectralComponents/SpectralFit.h>
      30             : 
      31             : #include <components/SpectralComponents/CompiledSpectralElement.h>
      32             : #include <components/SpectralComponents/GaussianSpectralElement.h>
      33             : #include <components/SpectralComponents/LogTransformedPolynomialSpectralElement.h>
      34             : #include <components/SpectralComponents/LorentzianSpectralElement.h>
      35             : #include <components/SpectralComponents/PolynomialSpectralElement.h>
      36             : #include <components/SpectralComponents/PowerLogPolynomialSpectralElement.h>
      37             : #include <casacore/scimath/Fitting/NonLinearFitLM.h>
      38             : #include <casacore/scimath/Functionals/CompiledFunction.h>
      39             : #include <casacore/scimath/Functionals/CompoundFunction.h>
      40             : #include <casacore/scimath/Functionals/CompoundParam.h>
      41             : #include <casacore/scimath/Functionals/Gaussian1D.h>
      42             : #include <casacore/scimath/Functionals/Lorentzian1D.h>
      43             : #include <casacore/scimath/Functionals/Polynomial.h>
      44             : #include <casacore/scimath/Functionals/PowerLogarithmicPolynomial.h>
      45             : 
      46             : namespace casa { //# NAMESPACE CASA - BEGIN
      47             : 
      48             : //# Templated member functions
      49             : 
      50             : template <class MT>
      51           0 : bool SpectralFit::fit(const casacore::Vector<MT> &y,
      52             :                       const casacore::Vector<MT> &x,
      53             :                       const casacore::Vector<casacore::Bool> *mask) {
      54           0 :   casacore::Vector<MT> sigma(x.nelements());
      55           0 :   sigma = 1.0;
      56           0 :   return fit(sigma, y, x, mask);
      57           0 : }
      58             : 
      59             : template <class MT>
      60      545637 : bool SpectralFit::fit(
      61             :         const casacore::Vector<MT> &sigma, const casacore::Vector<MT> &y,
      62             :         const casacore::Vector<MT> &x, const casacore::Vector<casacore::Bool> *mask
      63             : ) {
      64      545637 :         casacore::NonLinearFitLM<MT> fitter;
      65      545637 :         iter_p = 0;
      66             :         // The functions to fit
      67      545637 :         casacore::CompoundFunction<casacore::AutoDiff<MT> > func;
      68      545637 :         casacore::uInt ncomps = slist_p.nelements();
      69      545637 :         std::unique_ptr<casacore::Function<casacore::AutoDiff<MT> > > autodiff;
      70     1107561 :         for (casacore::uInt i=0; i<ncomps; i++) {
      71      561924 :                 SpectralElement *elem = slist_p[i];
      72      561924 :                 casacore::uInt nparms = elem->getOrder();
      73      561924 :                 SpectralElement::Types type = slist_p[i]->getType();
      74      561924 :                 switch(type) {
      75       32428 :                 case SpectralElement::GAUSSIAN: {
      76       32428 :                         autodiff.reset(new casacore::Gaussian1D<casacore::AutoDiff<MT> >());
      77             :                 }
      78       32428 :                 break;
      79      529222 :                 case SpectralElement::POLYNOMIAL: {
      80      529222 :                         PolynomialSpectralElement *x = dynamic_cast<PolynomialSpectralElement *>(elem);
      81      529222 :                         autodiff.reset(new casacore::Polynomial<casacore::AutoDiff<MT> >(x->getDegree()));
      82             :                 }
      83      529222 :                 break;
      84          26 :                 case SpectralElement::COMPILED:
      85             :                         // Allow fall through; these use the same code
      86             :                 case SpectralElement::GMULTIPLET: {
      87          26 :                         CompiledSpectralElement *x = dynamic_cast<CompiledSpectralElement *>(elem);
      88          26 :                         autodiff.reset(new casacore::CompiledFunction<casacore::AutoDiff<MT> >());
      89          26 :                         dynamic_cast<casacore::CompiledFunction<casacore::AutoDiff<MT> > *>(
      90          26 :                                 autodiff.get()
      91          26 :                         )->setFunction(x->getFunction());
      92             :                 }
      93          26 :                 break;
      94          50 :                 case SpectralElement::LORENTZIAN: {
      95          50 :                         autodiff.reset(new casacore::Lorentzian1D<casacore::AutoDiff<MT> >());
      96             :                 }
      97          50 :                 break;
      98         155 :                 case SpectralElement::POWERLOGPOLY: {
      99         155 :                         casacore::Vector<casacore::Double> parms = elem->get();
     100         155 :                         autodiff.reset(new casacore::PowerLogarithmicPolynomial<casacore::AutoDiff<MT> > (nparms));
     101         155 :                 }
     102         155 :                 break;
     103          43 :                 case SpectralElement::LOGTRANSPOLY: {
     104          43 :                         LogTransformedPolynomialSpectralElement *x = dynamic_cast<
     105             :                                 LogTransformedPolynomialSpectralElement*
     106          43 :                         >(elem);
     107             :                         // treated as a polynomial for fitting purposes. The caller is responsible for passing the ln's of
     108             :                         // the ordinate and obscissa values to the fitter.
     109          43 :                         autodiff.reset(new casacore::Polynomial<casacore::AutoDiff<MT> > (x->getDegree()));
     110             :                 }
     111          43 :                 break;
     112           0 :                 default:
     113           0 :                         throw casacore::AipsError("SpectralFit::fit(): Logic Error: Unhandled SpectralElement type");
     114             :                 }
     115      561924 :                 casacore::Vector<casacore::Double> parms = elem->get();
     116      561924 :                 casacore::Vector<casacore::Bool> fixed = elem->fixed();
     117     1452105 :                 for (casacore::uInt j=0; j<nparms; j++) {
     118      890181 :                         (*autodiff)[j] = casacore::AutoDiff<MT>(parms[j], nparms, j);
     119      890181 :                         if (j == PCFSpectralElement::WIDTH && type == SpectralElement::GAUSSIAN) {
     120       32428 :                                 (*autodiff)[j] *= GaussianSpectralElement::SigmaToFWHM;
     121             :                         }
     122      890181 :                         autodiff->mask(j) = ! fixed[j];
     123             :                 }
     124      561924 :                 func.addFunction(*autodiff);
     125             :         }
     126      545637 :         fitter.setFunction(func);
     127             :         // Max. number of iterations
     128      545637 :         fitter.setMaxIter(50+ ncomps*10);
     129             :         // Convergence criterium
     130      545637 :         fitter.setCriteria(0.001);
     131             :         // Fit
     132      545637 :         casacore::Vector<MT> sol;
     133      545637 :         casacore::Vector<MT> err;
     134      545637 :         sol = fitter.fit(x, y, sigma, mask);
     135      545553 :         err = fitter.errors();
     136             :         // Number of iterations
     137      545553 :         iter_p = fitter.currentIteration();
     138      545553 :         chiSq_p = fitter.getChi2();
     139      545553 :         casacore::uInt j = 0;
     140      545553 :         casacore::Vector<casacore::Double> tmp, terr;
     141     1107305 :         for (casacore::uInt i=0; i<ncomps; i++) {
     142      561755 :                 SpectralElement *element = slist_p[i];
     143      561755 :                 casacore::uInt nparms = element->getOrder();
     144      561755 :                 tmp.resize(nparms);
     145      561755 :                 terr.resize(nparms);
     146      561755 :                 SpectralElement::Types type = element->getType();
     147     1451429 :                 for (casacore::uInt k=0; k<nparms; k++) {
     148      889674 :                         casacore::Bool convertGaussWidth = k==PCFSpectralElement::WIDTH && type == SpectralElement::GAUSSIAN;
     149      889674 :                         tmp(k) = convertGaussWidth
     150      889674 :                                 ? sol(j) / GaussianSpectralElement::SigmaToFWHM
     151      857415 :                                 : sol(j);
     152      889674 :                         terr(k) = convertGaussWidth
     153      889674 :                                 ? err(j) / GaussianSpectralElement::SigmaToFWHM
     154      857415 :                                 : err(j);
     155      889674 :                         j++;
     156             :                 };
     157      561755 :                 element->set(tmp);
     158      561752 :                 element->setError(terr);
     159             :         }
     160     1091100 :         return fitter.converged();
     161      545991 : }
     162             : 
     163             : 
     164             : } //# NAMESPACE CASA - END
     165             : 

Generated by: LCOV version 1.16