Loading...
Searching...
No Matches
casacore::FitGaussian< T > Class Template Reference
scimath package (libcasa_scimath) » Fitting_module

More...

`#include <FitGaussian.h>`

## Public Member Functions

| FitGaussian ()
| Create the fitter.
| FitGaussian (uInt dimension)
| FitGaussian (uInt dimension, uInt numgaussians)
void | setDimensions (uInt dimensions)
| Adjust the number of dimensions.
void | setNumGaussians (uInt numgaussians)
| Adjust the number of gaussians to fit.
void | setFirstEstimate (const Matrix< T > &estimate)
| Set the initial estimate (the starting point of the first fit.).
void | setMaxRetries (uInt nretries)
| Set the maximum number of retries.
void | setMaxTime (Double maxtime)
| Set the maximum amount of time to spend (in seconds).
void | setRetryFactors ()
| Set the retry factors, the values that are added/multiplied with the first estimate on subsequent attempts if the first attempt fails.
void | setRetryFactors (const Matrix< T > &retryfactors)
uInt | nRetryFactors ()
| Return the number of retry options available.
Bool & | mask (uInt gaussian, uInt parameter)
| Mask out some parameters so that they are not modified during fitting.
const Bool & | mask (uInt gaussian, uInt parameter) const
Matrix< T > | fit (const Matrix< T > &pos, const Vector< T > &f, T maximumRMS=1.0, uInt maxiter=1024, T convcriteria=0.0001)
| Run the fit, using the data provided in the arguments pos and f.
Matrix< T > | fit (const Matrix< T > &pos, const Vector< T > &f, const Vector< T > &sigma, T maximumRMS=1.0, uInt maxiter=1024, T convcriteria=0.0001)
const Matrix< T > & | solution ()
| Allow access to the fit parameters from this class.
const Matrix< T > & | errors ()
void | correctParameters (Matrix< T > &parameters)
| Internal function for ensuring that parameters stay within their stated domains (see Gaussian2D and Gaussian3D.).
T | chisquared ()
| Return the chi squared of the fit.
T | RMS ()
| Return the RMS of the fit.
Bool | converged ()
| Returns True if the fit (eventually) converged to a value.

## Private Member Functions

Matrix< T > | defaultRetryMatrix ()
| Sets the retry matrix to a default value.
void | expandRetryMatrix (uInt rowstoadd)
| Add one or more rows to the retry matrix.
uInt | countFreeParameters ()
| Find the number of unmasked parameters to be fit.

## Private Attributes

uInt | itsDimension
uInt | itsNGaussians
uInt | itsMaxRetries
Double | itsMaxTime
T | itsChisquare
T | itsRMS
Bool | itsSuccess
LogIO | os
Matrix< T > | itsFirstEstimate
Matrix< T > | itsRetryFctr
Matrix< Bool > | itsMask
Matrix< T > | itsSolutionParameters
| The solutions to the fit.
Matrix< T > | itsSolutionErrors
| The errors on the solution parameters.

## Detailed Description

template<class T>
class casacore::FitGaussian< T >

Multidimensional fitter class for Gaussians.

### Review Status

Test programs:
  tFitGaussian

### Prerequisite

- Gaussian1D class
- Gaussian2D class
- Gaussian3D class
- NonLinearFitLM class

### Etymology

Fits Gaussians to data.

### Synopsis

FitGaussian is specially designed for fitting procedures in code that must be generalized for general dimensionality and number of components, and for complicated fits where the failure rate of the standard nonlinear fitter is unacceptibly high.

FitGaussian essentially provides a Gaussian-adapted interface for NonLinearFitLM. The user specifies the dimension, number of gaussians, initial estimate, retry factors, and the data, and the fitting proceeds automatically. Upon failure of the fitter it will retry the fit according to the retry factors until a fit is completed successfully. The user can optionally require as a criterion for success that the RMS of the fit residuals not exceed some maximum value.

The retry factors are applied in different ways: the height and widths are multiplied by the retry factors while the center and angles are increased by their factors. As of 2002/07/12 these are applied randomly (instead of sequentially) to different components and combinations of components. The factors can be specified by the user, but a default set is available. This random method is better than the sequential method for a limited number of retries, but true optimization of the retry system would demand the use of a more sophisticated method.

### Example

FitGaussian<Double> fitgauss(1,1);
Matrix<Double> x(5,1); x(0,0) = 0; x(1,0) = 1; x(2,0) = 2; x(3,0) = 3; x(4,0) = 4;
Vector<Double> y(5); y(0) = 0; y(1) = 1; y(2) = 4; y(3) = 1; y(4) = 1;
Matrix<Double> estimate(1,3);
estimate(0,0) = 1; estimate(0,1) = 1; estimate(0,2) = 1;
fitgauss.setFirstEstimate(estimate);
Matrix<Double> solution;
solution = fitgauss.fit(x,y);
cout << solution;
casacore::FitGaussian::solution
const Matrix< T > & solution()
Allow access to the fit parameters from this class.
Definition FitGaussian.h:178
casacore::FitGaussian::FitGaussian
FitGaussian()
Create the fitter.
casacore::Matrix
Definition Matrix.h:87
casacore::Vector
Definition Vector.h:82

### Motivation

Fitting multiple Gaussians is required for many different applications, but requires a substantial amount of coding - especially if the dimensionality of the image is not known to the programmer. Furthermore, fitting multiple Gaussians has a very high failure rate. So, a specialized Gaussian fitting class that retries from different initial estimates until an acceptible fit was found was needed.

### Template Type Argument Requirements (T)

- T must be a real data type compatible with NonLinearFitLM - Float or Double.

### Thrown Exceptions

- AipsError if dimension is not 1, 2, or 3
- AipsError if incorrect parameter number specified.
- AipsError if estimate/retry/data arrays are of wrong dimension

### To Do

- Optimize the default retry matrix
- Send fitting messages to logger instead of console
- Consider using a more sophisticated retry ststem (above).
- Check the estimates for reasonability, especially on failure of fit.
- Consider adding other models (polynomial, etc) to make this a Fit3D class.

Definition at line 122 of file FitGaussian.h.

## Constructor & Destructor Documentation

## FitGaussian() [1/3]

template<class T>

casacore::FitGaussian< T >::FitGaussian | ( |  | ) |

Create the fitter.

The dimension and the number of gaussians to fit can be modified later if necessary.

## FitGaussian() [2/3]

template<class T>

casacore::FitGaussian< T >::FitGaussian | ( | uInt | dimension | ) |

## FitGaussian() [3/3]

template<class T>

casacore::FitGaussian< T >::FitGaussian | ( | uInt | dimension,
|  | uInt | numgaussians )

## Member Function Documentation

## chisquared()

template<class T>

T casacore::FitGaussian< T >::chisquared | ( |  | ) |

Return the chi squared of the fit.

## converged()

template<class T>

Bool casacore::FitGaussian< T >::converged | ( |  | ) |

Returns True if the fit (eventually) converged to a value.

## correctParameters()

template<class T>

void casacore::FitGaussian< T >::correctParameters | ( | Matrix< T > & | parameters | ) |

Internal function for ensuring that parameters stay within their stated domains (see Gaussian2D and Gaussian3D.).

## countFreeParameters()

template<class T>

uInt casacore::FitGaussian< T >::countFreeParameters | ( |  | ) |

private

Find the number of unmasked parameters to be fit.

## defaultRetryMatrix()

template<class T>

Matrix< T > casacore::FitGaussian< T >::defaultRetryMatrix | ( |  | ) |

private

Sets the retry matrix to a default value.

This is done automatically if the retry matrix is not set directly.

## errors()

template<class T>

const Matrix< T > & casacore::FitGaussian< T >::errors | ( |  | ) |

inline

Definition at line 179 of file FitGaussian.h.

References itsSolutionErrors.

## expandRetryMatrix()

template<class T>

void casacore::FitGaussian< T >::expandRetryMatrix | ( | uInt | rowstoadd | ) |

private

Add one or more rows to the retry matrix.

## fit() [1/2]

template<class T>

Matrix< T > casacore::FitGaussian< T >::fit | ( | const Matrix< T > & | pos,
|  | const Vector< T > & | f,
|  | const Vector< T > & | sigma,
|  | T | maximumRMS = 1.0,
|  | uInt | maxiter = 1024,
|  | T | convcriteria = 0.0001 )

## fit() [2/2]

template<class T>

Matrix< T > casacore::FitGaussian< T >::fit | ( | const Matrix< T > & | pos,
|  | const Vector< T > & | f,
|  | T | maximumRMS = 1.0,
|  | uInt | maxiter = 1024,
|  | T | convcriteria = 0.0001 )

Run the fit, using the data provided in the arguments pos and f.

The fit will retry from different initial estimates until it converges to a value with an RMS error less than maximumRMS. If this cannot be accomplished it will simply take the result that generated the best RMS.

## mask() [1/2]

template<class T>

Bool & casacore::FitGaussian< T >::mask | ( | uInt | gaussian,
|  | uInt | parameter )

Mask out some parameters so that they are not modified during fitting.

## mask() [2/2]

template<class T>

const Bool & casacore::FitGaussian< T >::mask | ( | uInt | gaussian,
|  | uInt | parameter ) const

## nRetryFactors()

template<class T>

uInt casacore::FitGaussian< T >::nRetryFactors | ( |  | ) |

inline

Return the number of retry options available.

Definition at line 159 of file FitGaussian.h.

References itsRetryFctr.

## RMS()

template<class T>

T casacore::FitGaussian< T >::RMS | ( |  | ) |

Return the RMS of the fit.

## setDimensions()

template<class T>

void casacore::FitGaussian< T >::setDimensions | ( | uInt | dimensions | ) |

Adjust the number of dimensions.

## setFirstEstimate()

template<class T>

void casacore::FitGaussian< T >::setFirstEstimate | ( | const Matrix< T > & | estimate | ) |

Set the initial estimate (the starting point of the first fit.).

## setMaxRetries()

template<class T>

void casacore::FitGaussian< T >::setMaxRetries | ( | uInt | nretries | ) |

inline

Set the maximum number of retries.

Definition at line 144 of file FitGaussian.h.

References itsMaxRetries.

## setMaxTime()

template<class T>

void casacore::FitGaussian< T >::setMaxTime | ( | Double | maxtime | ) |

inline

Set the maximum amount of time to spend (in seconds).

If time runs out during a fit the process will still complete that fit.

Definition at line 148 of file FitGaussian.h.

References itsMaxTime.

## setNumGaussians()

template<class T>

void casacore::FitGaussian< T >::setNumGaussians | ( | uInt | numgaussians | ) |

Adjust the number of gaussians to fit.

## setRetryFactors() [1/2]

template<class T>

void casacore::FitGaussian< T >::setRetryFactors | ( |  | ) |

Set the retry factors, the values that are added/multiplied with the first estimate on subsequent attempts if the first attempt fails.

Using the function with no argument sets the retry factors to the default.

## setRetryFactors() [2/2]

template<class T>

void casacore::FitGaussian< T >::setRetryFactors | ( | const Matrix< T > & | retryfactors | ) |

## solution()

template<class T>

const Matrix< T > & casacore::FitGaussian< T >::solution | ( |  | ) |

inline

Allow access to the fit parameters from this class.

Definition at line 178 of file FitGaussian.h.

References itsSolutionParameters.

## Member Data Documentation

## itsChisquare

template<class T>

T casacore::FitGaussian< T >::itsChisquare

private

Definition at line 200 of file FitGaussian.h.

## itsDimension

template<class T>

uInt casacore::FitGaussian< T >::itsDimension

private

Definition at line 196 of file FitGaussian.h.

## itsFirstEstimate

template<class T>

Matrix<T> casacore::FitGaussian< T >::itsFirstEstimate

private

Definition at line 205 of file FitGaussian.h.

## itsMask

template<class T>

Matrix<Bool> casacore::FitGaussian< T >::itsMask

private

Definition at line 207 of file FitGaussian.h.

## itsMaxRetries

template<class T>

uInt casacore::FitGaussian< T >::itsMaxRetries

private

Definition at line 198 of file FitGaussian.h.

Referenced by setMaxRetries().

## itsMaxTime

template<class T>

Double casacore::FitGaussian< T >::itsMaxTime

private

Definition at line 199 of file FitGaussian.h.

Referenced by setMaxTime().

## itsNGaussians

template<class T>

uInt casacore::FitGaussian< T >::itsNGaussians

private

Definition at line 197 of file FitGaussian.h.

## itsRetryFctr

template<class T>

Matrix<T> casacore::FitGaussian< T >::itsRetryFctr

private

Definition at line 206 of file FitGaussian.h.

Referenced by nRetryFactors().

## itsRMS

template<class T>

T casacore::FitGaussian< T >::itsRMS

private

Definition at line 201 of file FitGaussian.h.

## itsSolutionErrors

template<class T>

Matrix<T> casacore::FitGaussian< T >::itsSolutionErrors

private

The errors on the solution parameters.

Definition at line 224 of file FitGaussian.h.

Referenced by errors().

## itsSolutionParameters

template<class T>

Matrix<T> casacore::FitGaussian< T >::itsSolutionParameters

private

The solutions to the fit.

Definition at line 221 of file FitGaussian.h.

Referenced by solution().

## itsSuccess

template<class T>

Bool casacore::FitGaussian< T >::itsSuccess

private

Definition at line 202 of file FitGaussian.h.

## os

template<class T>

LogIO casacore::FitGaussian< T >::os

private

Definition at line 203 of file FitGaussian.h.

The documentation for this class was generated from the following file:

- scimath/Fitting/FitGaussian.h