Loading...
Searching...
No Matches
Fit2D.h
Go to the documentation of this file.
//# Fit2D.h: Class to fit 2-D objects to Lattices or Arrays
//# Copyright (C) 1997,1998,1999,2000,2001,2002
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: casa-feedback@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
#ifndef LATTICES_FIT2D_H
#define LATTICES_FIT2D_H
//# Includes
#include <casacore/casa/aips.h>
#include <casacore/casa/Arrays/ArrayFwd.h>
#include <casacore/scimath/Functionals/CompoundFunction.h>
#include <casacore/casa/BasicSL/Constants.h>
#include <casacore/scimath/Fitting/NonLinearFitLM.h>
#include <casacore/casa/Logging/LogIO.h>
namespace casacore { //# NAMESPACE CASACORE - BEGIN
template<class T> class Lattice;
template<class T> class MaskedLattice;
// <summary>
// Fit 2-D objects to 2-D Lattices or Arrays
// </summary>
// <use visibility=export>
// <reviewed reviewer="" date="" tests="">
// </reviewed>
// <prerequisite>
// <li> <linkto class=Lattice>Lattice</linkto>
// </prerequisite>
// <synopsis>
// This class allows you to fit different types of 2-D models
// to either Lattices or Arrays. These must be 2 dimensional;
// for Lattices, the appropriate 2-D Lattice can be made with
// the SubLattice class.
//
// You may fit more than one model simultaneously to the data.
// Models are added with the addModel method. With this method,
// you also specify the initial guesses of the parameters of
// the model. Any parameters involving coordinates are
// expected in zero-relative absolute pixel coordinates (e.g. the centre of
// a model). Additionally with the addModel method,
// you may specify which parameters are to be held fixed
// during the fitting process. This is done with the
// parameterMask Vector which is in the same order as the
// parameter Vector. A value of True indicates the parameter
// will be fitted for. Presently, when you say fix the minor axis,
// you really end up fixing the axial ratio (internals). I don't
// have a solution for this presently.
//
// For Gaussians, the parameter Vector (input or output) consists, in order, of
// the peak, x location, y location, FWHM of major axis, FWHM of minor axis,
// and position angle of the major axis (in radians). The
// position angle is positive +x to +y
// in the pixel coordinate system ([0,0] in center of image) and
// in the range -2pi to 2pi. When the solution is recovered, the
// position angle will be in the range 0 to pi.
//
// </synopsis>
// <example>
// <srcblock>
// </srcblock>
// </example>
// <todo asof="1998/12/11">
// <li> template it
// <li> Speed up some Array calculations indexed with IPositions
// <li> Don't handle Lattices simply by getting pixels into Arrays
// <li> make an addModel interface taking functionals
// </todo>
class Fit2D
{
public:
// Enum describing the different models you can fit
enum Types {
GAUSSIAN = 0,
DISK = 1,
LEVEL=2,
PLANE=3,
nTypes
};
// Enum describing output error conditions
enum ErrorTypes {
// ok
OK = 0,
// Did not converge
NOCONVERGE = 1,
// Solution failed
FAILED = 2,
// There were no unmasked points
NOGOOD = 3,
// No models set
NOMODELS = 4,
// Number of conditions
nErrorTypes
};
// Constructor
explicit Fit2D(LogIO& logger);
// Destructor
~Fit2D();
// Copy constructor. Uses copy semantics except for the logger
// for which a reference copy is made
Fit2D(const Fit2D& other);
// Assignment operator. Uses copy semantics except for the logger
// for which a reference copy is made
Fit2D& operator=(const Fit2D& other);
// Add a model to the list to be simultaneously fit and
// return its index. Specify the initial guesses for
// the model and a mask indicating whether the parameter
// is fixed (False) during the fit or not. Returns the
// the model number added (0, 1, 2 etc)
//<group>
uInt addModel (Fit2D::Types type,
const Vector<Double>& parameters,
const Vector<Bool>& parameterMask);
uInt addModel(Fit2D::Types type,
const Vector<Double>& parameters);
//</group>
// Convert mask from a string to a vector. The string gives the parameters
// to keep fixed in the fit (f (flux), x (x position), y (y position),
// a (FWHM major axis), b (FWHM minor axis), p (position angle)
static Vector<Bool> convertMask (const String fixedmask,
Fit2D::Types type);
// Set a pixel selection range. When the fit is done, only
// pixels in the specified range are included/excluded.
// Only the last call of either of these will be active.
//<group>
void setIncludeRange (Double minVal, Double maxVal);
void setExcludeRange (Double minVal, Double maxVal);
void resetRange();
//</group>
// Return number of parameters for this type of model
static uInt nParameters (Fit2D::Types type);
// Recover number of models
uInt nModels() const;
// Determine an initial estimate for the solution of the specified
// model type to the given data - no compound models are allowable
// in this function. If you have specified an include
// or exclude pixel range to the fitter, that will be honoured.
// This function does not interact with the addModel function.
// Returns a zero length vector if it fails to make an estimate.
//<group>
template<class T> Vector<Double> estimate(Fit2D::Types type,
const MaskedLattice<T>& data);
template<class T> Vector<Double> estimate(
Fit2D::Types type, const Lattice<T>& data
);
template<class T> Vector<Double> estimate(
Fit2D::Types type, const Array<T>& data
);
template<class T> Vector<Double> estimate(
Fit2D::Types type, const Array<T>& data, const Array<Bool>& mask
);
//</group>
// Do the fit. Returns an enum value to tell you what happened if the fit failed
// for some reasons. A message can also be found with function errorMessage if
// the fit was not successful. For Array(i,j) i is x and j is y
//<group>
template <class T> Fit2D::ErrorTypes fit(const MaskedLattice<T>& data,
const Lattice<T>& sigma);
template <class T> Fit2D::ErrorTypes fit(const Lattice<T>& data,
const Lattice<T>& sigma);
template <class T> Fit2D::ErrorTypes fit(const Array<T>& data,
const Array<T>& sigma);
template <class T> Fit2D::ErrorTypes fit(const Array<T>& data,
const Array<Bool>& mask,
const Array<T>& sigma);
//</group>
// Find the residuals to the fit. xOffset and yOffset allow one to provide a data
// array that is offset in space from the grid that was fit. In this way, one
// can fill out a larger image than the subimage that was fit, for example. A negative
// value of xOffset means the supplied data array represents a grid that has a y axis left
// of the grid of pixels that was fit. A negative yOffset value means the supplied data
// array represents a grid that has an x axis that is below the x axis of the grid of pixels
// that was fit.
// NOTE these may need to be templated at some point in the future. My
// current need does not require they be templated. - dmehring 29jun2018
//<group>
template <class T> Fit2D::ErrorTypes residual(
Array<T>& resid, Array<T>& model,
const Array<T>& data, Int xOffset=0, int yOffset=0
) const;
Fit2D::ErrorTypes residual(Array<Float>& resid, Array<Float>& model,
const MaskedLattice<Float>& data);
Fit2D::ErrorTypes residual(Array<Float>& resid, Array<Float>& model,
const Lattice<Float>& data);
//</group>
// If function fit failed, you will find a message here
// saying why it failed
String errorMessage () const;
// Recover solution for either all model components or
// a specific one. These functions will return an empty vector
// if there is no valid solution. All available parameters (fixed and
// adjustable) are included in the solution vectors.
//<group>
Vector<Double> availableSolution () const;
Vector<Double> availableSolution (uInt which) const;
//</group>
// The errors. All available parameters (fixed and adjustable) are
// included in the error vectors. Unsolved for parameters will
// have error 0.
//<group>
Vector<Double> availableErrors() const;
Vector<Double> availableErrors(uInt which) const;
//</group>
// The number of iterations that the fitter finished with
uInt numberIterations() const;
// The chi squared of the fit. Returns 0 if fit has been done.
Double chiSquared () const;
// The number of points used for the last fit
uInt numberPoints () const;
// Return type as a string
static String type(Fit2D::Types type);
// Return string type as enum (min match)
static Fit2D::Types type(const String& type);
// Find type of specific model
Fit2D::Types type(uInt which);
// Convert p.a. (radians) from positive +x -> +y
// (Fit2D) to positive +y -> -x (Gaussian2D)
static Double paToGauss2D (Double pa) {return pa - C::pi_2;};
// Convert p.a. (radians) from positive +y -> -x
// (Gaussian2D) to positive +x -> +y (Fit2D)
static Double paFromGauss2D (Double pa) {return pa + C::pi_2;};
private:
mutable LogIO itsLogger;
Bool itsValid, itsValidSolution, itsHasSigma;
Bool itsInclude;
Vector<Double> itsPixelRange;
CompoundFunction<AutoDiff<Double>> itsFunction;
NonLinearFitLM<Double> itsFitter;
Vector<Double> itsSolution;
Vector<Double> itsErrors;
Double itsChiSquared;
String itsErrorMessage;
uInt itsNumberPoints;
Vector<uInt> itsTypeList;
Fit2D::ErrorTypes fitData(const Vector<Double>& values,
const Matrix<Double>& pos,
const Vector<Double>& sigma);
// Returns available (adjustable + fixed) solution for model of
// interest and tells you where it began in the full solution vector
// Does no axial ratio nor position angle conversions from direct
// fit solution vector
// <group>
Vector<Double> availableSolution (uInt& iStart, uInt which) const;
Vector<Double> availableErrors (uInt& iStart, uInt which) const;
// </group>
Vector<Double> getParams(uInt which) const;
void setParams(const Vector<Double>& params, uInt which);
Bool includeIt (Double value, const Vector<Double>& range,
Int includeIt) const;
template <class T> Bool selectData (
Matrix<Double>& pos, Vector<Double>& values,
Vector<Double>& weights, const Array<T>& pixels,
const Array<Bool>& mask, const Array<T>& sigma
);
void piRange (Double& pa) const;
};
inline Bool Fit2D::includeIt (Double value, const Vector<Double>& range,
Int includeIt) const
{
if (includeIt==0) return True;
//
if (includeIt==1) {
if (value >= range(0) && value <= range(1)) return True;
} else if (value < range(0) || value > range(1)) {
return True;
}
//
return False;
}
} //# NAMESPACE CASACORE - END
#ifndef CASACORE_NO_AUTO_TEMPLATES
#include <casacore/lattices/LatticeMath/Fit2D2.tcc>
#endif //# CASACORE_NO_AUTO_TEMPLATES
#endif
casacore::Array
Definition Array.h:155
casacore::CompoundFunction
Definition CompoundFunction.h:112
casacore::Fit2D::estimate
Vector< Double > estimate(Fit2D::Types type, const MaskedLattice< T > &data)
Determine an initial estimate for the solution of the specified model type to the given data - no com...
casacore::Fit2D::fit
Fit2D::ErrorTypes fit(const Array< T > &data, const Array< Bool > &mask, const Array< T > &sigma)
casacore::Fit2D::residual
Fit2D::ErrorTypes residual(Array< Float > &resid, Array< Float > &model, const Lattice< Float > &data)
casacore::Fit2D::availableErrors
Vector< Double > availableErrors(uInt which) const
casacore::Fit2D::itsNumberPoints
uInt itsNumberPoints
Definition Fit2D.h:289
casacore::Fit2D::itsLogger
LogIO itsLogger
Definition Fit2D.h:279
casacore::Fit2D::availableSolution
Vector< Double > availableSolution() const
Recover solution for either all model components or a specific one.
casacore::Fit2D::selectData
Bool selectData(Matrix< Double > &pos, Vector< Double > &values, Vector< Double > &weights, const Array< T > &pixels, const Array< Bool > &mask, const Array< T > &sigma)
casacore::Fit2D::addModel
uInt addModel(Fit2D::Types type, const Vector< Double > &parameters, const Vector< Bool > &parameterMask)
Add a model to the list to be simultaneously fit and return its index.
casacore::Fit2D::errorMessage
String errorMessage() const
If function fit failed, you will find a message here saying why it failed.
casacore::Fit2D::fit
Fit2D::ErrorTypes fit(const Lattice< T > &data, const Lattice< T > &sigma)
casacore::Fit2D::Fit2D
Fit2D(LogIO &logger)
Constructor.
casacore::Fit2D::paToGauss2D
static Double paToGauss2D(Double pa)
Convert p.a.
Definition Fit2D.h:271
casacore::Fit2D::Fit2D
Fit2D(const Fit2D &other)
Copy constructor.
casacore::Fit2D::itsSolution
Vector< Double > itsSolution
Definition Fit2D.h:285
casacore::Fit2D::availableSolution
Vector< Double > availableSolution(uInt &iStart, uInt which) const
Returns available (adjustable + fixed) solution for model of interest and tells you where it began in...
casacore::Fit2D::setIncludeRange
void setIncludeRange(Double minVal, Double maxVal)
Set a pixel selection range.
casacore::Fit2D::itsFitter
NonLinearFitLM< Double > itsFitter
Definition Fit2D.h:284
casacore::Fit2D::nParameters
static uInt nParameters(Fit2D::Types type)
Return number of parameters for this type of model.
casacore::Fit2D::operator=
Fit2D & operator=(const Fit2D &other)
Assignment operator.
casacore::Fit2D::itsValid
Bool itsValid
Definition Fit2D.h:280
casacore::Fit2D::estimate
Vector< Double > estimate(Fit2D::Types type, const Array< T > &data)
casacore::Fit2D::itsTypeList
Vector< uInt > itsTypeList
Definition Fit2D.h:291
casacore::Fit2D::chiSquared
Double chiSquared() const
The chi squared of the fit.
casacore::Fit2D::fit
Fit2D::ErrorTypes fit(const MaskedLattice< T > &data, const Lattice< T > &sigma)
Do the fit.
casacore::Fit2D::estimate
Vector< Double > estimate(Fit2D::Types type, const Lattice< T > &data)
casacore::Fit2D::itsFunction
CompoundFunction< AutoDiff< Double > > itsFunction
Definition Fit2D.h:283
casacore::Fit2D::type
static String type(Fit2D::Types type)
Return type as a string.
casacore::Fit2D::setExcludeRange
void setExcludeRange(Double minVal, Double maxVal)
casacore::Fit2D::availableErrors
Vector< Double > availableErrors(uInt &iStart, uInt which) const
casacore::Fit2D::itsErrorMessage
String itsErrorMessage
Definition Fit2D.h:288
casacore::Fit2D::addModel
uInt addModel(Fit2D::Types type, const Vector< Double > &parameters)
casacore::Fit2D::itsHasSigma
Bool itsHasSigma
Definition Fit2D.h:280
casacore::Fit2D::residual
Fit2D::ErrorTypes residual(Array< Float > &resid, Array< Float > &model, const MaskedLattice< Float > &data)
casacore::Fit2D::numberIterations
uInt numberIterations() const
The number of iterations that the fitter finished with.
casacore::Fit2D::fitData
Fit2D::ErrorTypes fitData(const Vector< Double > &values, const Matrix< Double > &pos, const Vector< Double > &sigma)
casacore::Fit2D::itsChiSquared
Double itsChiSquared
Definition Fit2D.h:287
casacore::Fit2D::Types
Types
Enum describing the different models you can fit.
Definition Fit2D.h:102
casacore::Fit2D::PLANE
@ PLANE
Definition Fit2D.h:106
casacore::Fit2D::GAUSSIAN
@ GAUSSIAN
Definition Fit2D.h:103
casacore::Fit2D::nTypes
@ nTypes
Definition Fit2D.h:107
casacore::Fit2D::DISK
@ DISK
Definition Fit2D.h:104
casacore::Fit2D::LEVEL
@ LEVEL
Definition Fit2D.h:105
casacore::Fit2D::availableErrors
Vector< Double > availableErrors() const
The errors.
casacore::Fit2D::convertMask
static Vector< Bool > convertMask(const String fixedmask, Fit2D::Types type)
Convert mask from a string to a vector.
casacore::Fit2D::piRange
void piRange(Double &pa) const
casacore::Fit2D::type
static Fit2D::Types type(const String &type)
Return string type as enum (min match).
casacore::Fit2D::type
Fit2D::Types type(uInt which)
Find type of specific model.
casacore::Fit2D::nModels
uInt nModels() const
Recover number of models.
casacore::Fit2D::availableSolution
Vector< Double > availableSolution(uInt which) const
casacore::Fit2D::itsErrors
Vector< Double > itsErrors
Definition Fit2D.h:286
casacore::Fit2D::setParams
void setParams(const Vector< Double > &params, uInt which)
casacore::Fit2D::itsInclude
Bool itsInclude
Definition Fit2D.h:281
casacore::Fit2D::fit
Fit2D::ErrorTypes fit(const Array< T > &data, const Array< T > &sigma)
casacore::Fit2D::ErrorTypes
ErrorTypes
Enum describing output error conditions.
Definition Fit2D.h:111
casacore::Fit2D::FAILED
@ FAILED
Solution failed.
Definition Fit2D.h:117
casacore::Fit2D::NOMODELS
@ NOMODELS
No models set.
Definition Fit2D.h:121
casacore::Fit2D::NOGOOD
@ NOGOOD
There were no unmasked points.
Definition Fit2D.h:119
casacore::Fit2D::OK
@ OK
ok
Definition Fit2D.h:113
casacore::Fit2D::NOCONVERGE
@ NOCONVERGE
Did not converge.
Definition Fit2D.h:115
casacore::Fit2D::nErrorTypes
@ nErrorTypes
Number of conditions.
Definition Fit2D.h:123
casacore::Fit2D::itsValidSolution
Bool itsValidSolution
Definition Fit2D.h:280
casacore::Fit2D::estimate
Vector< Double > estimate(Fit2D::Types type, const Array< T > &data, const Array< Bool > &mask)
casacore::Fit2D::itsPixelRange
Vector< Double > itsPixelRange
Definition Fit2D.h:282
casacore::Fit2D::residual
Fit2D::ErrorTypes residual(Array< T > &resid, Array< T > &model, const Array< T > &data, Int xOffset=0, int yOffset=0) const
Find the residuals to the fit.
casacore::Fit2D::resetRange
void resetRange()
casacore::Fit2D::numberPoints
uInt numberPoints() const
The number of points used for the last fit.
casacore::Fit2D::getParams
Vector< Double > getParams(uInt which) const
casacore::Fit2D::includeIt
Bool includeIt(Double value, const Vector< Double > &range, Int includeIt) const
Definition Fit2D.h:322
casacore::Fit2D::~Fit2D
~Fit2D()
Destructor.
casacore::Fit2D::paFromGauss2D
static Double paFromGauss2D(Double pa)
Convert p.a.
Definition Fit2D.h:275
casacore::Lattice
Definition Lattice.h:230
casacore::LogIO
Definition LogIO.h:165
casacore::MaskedLattice
Definition MaskedLattice.h:216
casacore::Matrix
Definition Matrix.h:87
casacore::NonLinearFitLM
Definition NonLinearFitLM.h:85
casacore::String
String: the storage and methods of handling collections of characters.
Definition String.h:223
casacore::Vector
Definition Vector.h:82
casacore::C::pi_2
constexpr double pi_2
pi/2
Definition Constants.h:369
casacore
this file contains all the compiler specific defines
Definition mainpage.dox:28
casacore::pa
LatticeExprNode pa(const LatticeExprNode &left, const LatticeExprNode &right)
This function finds 180/pi*atan2(left,right)/2.
casacore::False
const Bool False
Definition aipstype.h:42
casacore::uInt
unsigned int uInt
Definition aipstype.h:49
casacore::mask
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
casacore::Int
int Int
Definition aipstype.h:48
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
casacore::True
const Bool True
Definition aipstype.h:41
casacore::value
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:368
casacore::Double
double Double
Definition aipstype.h:53