casacore
Loading...
Searching...
No Matches
FitGaussian.h
Go to the documentation of this file.
1// # FitGaussian.h: Multidimensional fitter class for Gaussians
2// # Copyright (C) 2001,2002
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#ifndef SCIMATH_FITGAUSSIAN_H
26#define SCIMATH_FITGAUSSIAN_H
27
28#include <casacore/casa/aips.h>
29#include <casacore/casa/Arrays/Matrix.h>
30#include <casacore/casa/Logging/LogIO.h>
31
32namespace casacore { // # NAMESPACE CASACORE - BEGIN
33
34// <summary>Multidimensional fitter class for Gaussians.</summary>
35
36// <reviewed reviewer="" date="" tests="tFitGaussian">
37// </reviewed>
38
39// <prerequisite>
40// <li> <linkto class="Gaussian1D">Gaussian1D</linkto> class
41// <li> <linkto class="Gaussian2D">Gaussian2D</linkto> class
42// <li> <linkto class="Gaussian3D">Gaussian3D</linkto> class
43// <li> <linkto class="NonLinearFitLM">NonLinearFitLM</linkto> class
44// </prerequisite>
45
46// <etymology>
47// Fits Gaussians to data.
48// </etymology>
49
50// <synopsis>
51
52// <src>FitGaussian</src> is specially designed for fitting procedures in
53// code that must be generalized for general dimensionality and
54// number of components, and for complicated fits where the failure rate of
55// the standard nonlinear fitter is unacceptibly high.
56
57// <src>FitGaussian</src> essentially provides a Gaussian-adapted
58// interface for NonLinearFitLM. The user specifies the dimension,
59// number of gaussians, initial estimate, retry factors, and the data,
60// and the fitting proceeds automatically. Upon failure of the fitter it will
61// retry the fit according to the retry factors until a fit is completed
62// successfully. The user can optionally require as a criterion for success
63// that the RMS of the fit residuals not exceed some maximum value.
64
65// The retry factors are applied in different ways: the height and widths
66// are multiplied by the retry factors while the center and angles are
67// increased by their factors. As of 2002/07/12 these are applied randomly
68// (instead of sequentially) to different components and combinations of
69// components. The factors can be specified by the user, but a default
70// set is available. This random method is better than the sequential method
71// for a limited number of retries, but true optimization of the retry system
72// would demand the use of a more sophisticated method.
73// </synopsis>
74
75// <example>
76// <srcblock>
77// FitGaussian<Double> fitgauss(1,1);
78// 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;
79// Vector<Double> y(5); y(0) = 0; y(1) = 1; y(2) = 4; y(3) = 1; y(4) = 1;
80// Matrix<Double> estimate(1,3);
81// estimate(0,0) = 1; estimate(0,1) = 1; estimate(0,2) = 1;
82// fitgauss.setFirstEstimate(estimate);
83// Matrix<Double> solution;
84// solution = fitgauss.fit(x,y);
85// cout << solution;
86// </srcblock>
87// </example>
88
89// <motivation>
90// Fitting multiple Gaussians is required for many different applications,
91// but requires a substantial amount of coding - especially if the
92// dimensionality of the image is not known to the programmer. Furthermore,
93// fitting multiple Gaussians has a very high failure rate. So, a specialized
94// Gaussian fitting class that retries from different initial estimates
95// until an acceptible fit was found was needed.
96// </motivation>
97
98// <templating arg=T>
99// <li> T must be a real data type compatible with NonLinearFitLM - Float or
100// Double.
101// </templating>
102
103// <thrown>
104// <li> AipsError if dimension is not 1, 2, or 3
105// <li> AipsError if incorrect parameter number specified.
106// <li> AipsError if estimate/retry/data arrays are of wrong dimension
107// </thrown>
108
109// <todo asof="2002/07/22">
110// <li> Optimize the default retry matrix
111// <li> Send fitting messages to logger instead of console
112// <li> Consider using a more sophisticated retry ststem (above).
113// <li> Check the estimates for reasonability, especially on failure of fit.
114// <li> Consider adding other models (polynomial, etc) to make this a Fit3D
115// class.
116// </todo>
117
118template <class T>
120 public:
121 // Create the fitter. The dimension and the number of gaussians to fit
122 // can be modified later if necessary.
123 // <group>
125 FitGaussian(uInt dimension);
126 FitGaussian(uInt dimension, uInt numgaussians);
127 // </group>
128
129 // Adjust the number of dimensions
130 void setDimensions(uInt dimensions);
131
132 // Adjust the number of gaussians to fit
133 void setNumGaussians(uInt numgaussians);
134
135 // Set the initial estimate (the starting point of the first fit.)
136 void setFirstEstimate(const Matrix<T>& estimate);
137
138 // Set the maximum number of retries.
139 void setMaxRetries(uInt nretries) { itsMaxRetries = nretries; };
140
141 // Set the maximum amount of time to spend (in seconds). If time runs out
142 // during a fit the process will still complete that fit.
143 void setMaxTime(Double maxtime) { itsMaxTime = maxtime; };
144
145 // Set the retry factors, the values that are added/multiplied with the
146 // first estimate on subsequent attempts if the first attempt fails.
147 // Using the function with no argument sets the retry factors to the default.
148 // <group>
150 void setRetryFactors(const Matrix<T>& retryfactors);
151 // </group>
152
153 // Return the number of retry options available
154 uInt nRetryFactors() { return itsRetryFctr.nrow(); };
155
156 // Mask out some parameters so that they are not modified during fitting
157 Bool& mask(uInt gaussian, uInt parameter);
158 const Bool& mask(uInt gaussian, uInt parameter) const;
159
160 // Run the fit, using the data provided in the arguments pos and f.
161 // The fit will retry from different initial estimates until it converges
162 // to a value with an RMS error less than maximumRMS. If this cannot be
163 // accomplished it will simply take the result that generated the best RMS.
164 Matrix<T> fit(const Matrix<T>& pos, const Vector<T>& f, T maximumRMS = 1.0, uInt maxiter = 1024,
165 T convcriteria = 0.0001);
166 Matrix<T> fit(const Matrix<T>& pos, const Vector<T>& f, const Vector<T>& sigma,
167 T maximumRMS = 1.0, uInt maxiter = 1024, T convcriteria = 0.0001);
168
169 // Allow access to the fit parameters from this class
171 const Matrix<T>& errors() { return itsSolutionErrors; };
172
173 // Internal function for ensuring that parameters stay within their stated
174 // domains (see <src>Gaussian2D</src> and <src>Gaussian3D</src>.)
175 void correctParameters(Matrix<T>& parameters);
176
177 // Return the chi squared of the fit
179
180 // Return the RMS of the fit
181 T RMS();
182
183 // Returns True if the fit (eventually) converged to a value.
185
186 private:
187 uInt itsDimension; // how many dimensions (1, 2, or 3)
188 uInt itsNGaussians; // number of gaussians to fit
189 uInt itsMaxRetries; // maximum number of retries to attempt
190 Double itsMaxTime; // maximum time to spend fitting in secs
191 T itsChisquare; // chisquare of fit
192 T itsRMS; // RMS of fit (sqrt[chisquare / N])
193 Bool itsSuccess; // flags success or failure
195
196 Matrix<T> itsFirstEstimate; // user's estimate.
197 Matrix<T> itsRetryFctr; // source of retry information
198 Matrix<Bool> itsMask; // masks parameters not to change in fitting
199
200 // Sets the retry matrix to a default value. This is done automatically if
201 // the retry matrix is not set directly.
203
204 // Add one or more rows to the retry matrix.
205 void expandRetryMatrix(uInt rowstoadd);
206
207 // Find the number of unmasked parameters to be fit
209
210 // The solutions to the fit
212
213 // The errors on the solution parameters
215};
216
217} // namespace casacore
218
219#ifndef CASACORE_NO_AUTO_TEMPLATES
220#include <casacore/scimath/Fitting/FitGaussian.tcc>
221#endif // # CASACORE_NO_AUTO_TEMPLATES
222#endif
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.
Bool converged()
Returns True if the fit (eventually) converged to a value.
uInt nRetryFactors()
Return the number of retry options available.
const Matrix< T > & solution()
Allow access to the fit parameters from this class.
T chisquared()
Return the chi squared of the fit.
Bool & mask(uInt gaussian, uInt parameter)
Mask out some parameters so that they are not modified during fitting.
Matrix< T > itsSolutionErrors
The errors on the solution parameters.
const Matrix< T > & errors()
void setRetryFactors(const Matrix< T > &retryfactors)
Matrix< Bool > itsMask
void expandRetryMatrix(uInt rowstoadd)
Add one or more rows to the retry matrix.
uInt countFreeParameters()
Find the number of unmasked parameters to be fit.
FitGaussian(uInt dimension, uInt numgaussians)
T RMS()
Return the RMS of the fit.
Matrix< T > itsFirstEstimate
Matrix< T > defaultRetryMatrix()
Sets the retry matrix to a default value.
Matrix< T > itsRetryFctr
void setFirstEstimate(const Matrix< T > &estimate)
Set the initial estimate (the starting point of the first fit.).
FitGaussian(uInt dimension)
void setMaxTime(Double maxtime)
Set the maximum amount of time to spend (in seconds).
void setMaxRetries(uInt nretries)
Set the maximum number of retries.
const Bool & mask(uInt gaussian, uInt parameter) const
void correctParameters(Matrix< T > &parameters)
Internal function for ensuring that parameters stay within their stated domains (see Gaussian2D and G...
void setNumGaussians(uInt numgaussians)
Adjust the number of gaussians to fit.
FitGaussian()
Create the fitter.
Matrix< T > itsSolutionParameters
The solutions to the fit.
void setDimensions(uInt dimensions)
Adjust the number of dimensions.
void setRetryFactors()
Set the retry factors, the values that are added/multiplied with the first estimate on subsequent att...
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)
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53