casacore
Loading...
Searching...
No Matches
Fit2D.h
Go to the documentation of this file.
1//# Fit2D.h: Class to fit 2-D objects to Lattices or Arrays
2//# Copyright (C) 1997,1998,1999,2000,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
26#ifndef LATTICES_FIT2D_H
27#define LATTICES_FIT2D_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/ArrayFwd.h>
32#include <casacore/scimath/Functionals/CompoundFunction.h>
33#include <casacore/casa/BasicSL/Constants.h>
34#include <casacore/scimath/Fitting/NonLinearFitLM.h>
35#include <casacore/casa/Logging/LogIO.h>
36
37namespace casacore { //# NAMESPACE CASACORE - BEGIN
38
39template<class T> class Lattice;
40template<class T> class MaskedLattice;
41
42
43// <summary>
44// Fit 2-D objects to 2-D Lattices or Arrays
45// </summary>
46
47// <use visibility=export>
48
49// <reviewed reviewer="" date="" tests="">
50// </reviewed>
51
52// <prerequisite>
53// <li> <linkto class=Lattice>Lattice</linkto>
54// </prerequisite>
55
56// <synopsis>
57// This class allows you to fit different types of 2-D models
58// to either Lattices or Arrays. These must be 2 dimensional;
59// for Lattices, the appropriate 2-D Lattice can be made with
60// the SubLattice class.
61//
62// You may fit more than one model simultaneously to the data.
63// Models are added with the addModel method. With this method,
64// you also specify the initial guesses of the parameters of
65// the model. Any parameters involving coordinates are
66// expected in zero-relative absolute pixel coordinates (e.g. the centre of
67// a model). Additionally with the addModel method,
68// you may specify which parameters are to be held fixed
69// during the fitting process. This is done with the
70// parameterMask Vector which is in the same order as the
71// parameter Vector. A value of True indicates the parameter
72// will be fitted for. Presently, when you say fix the minor axis,
73// you really end up fixing the axial ratio (internals). I don't
74// have a solution for this presently.
75//
76// For Gaussians, the parameter Vector (input or output) consists, in order, of
77// the peak, x location, y location, FWHM of major axis, FWHM of minor axis,
78// and position angle of the major axis (in radians). The
79// position angle is positive +x to +y
80// in the pixel coordinate system ([0,0] in center of image) and
81// in the range -2pi to 2pi. When the solution is recovered, the
82// position angle will be in the range 0 to pi.
83//
84// </synopsis>
85// <example>
86// <srcblock>
87// </srcblock>
88// </example>
89
90// <todo asof="1998/12/11">
91// <li> template it
92// <li> Speed up some Array calculations indexed with IPositions
93// <li> Don't handle Lattices simply by getting pixels into Arrays
94// <li> make an addModel interface taking functionals
95// </todo>
96
97class Fit2D
98{
99public:
100
101 // Enum describing the different models you can fit
109
110 // Enum describing output error conditions
112// ok
113 OK = 0,
114// Did not converge
116// Solution failed
118// There were no unmasked points
120// No models set
122// Number of conditions
124 };
125
126 // Constructor
127 explicit Fit2D(LogIO& logger);
128
129 // Destructor
131
132 // Copy constructor. Uses copy semantics except for the logger
133 // for which a reference copy is made
134 Fit2D(const Fit2D& other);
135
136 // Assignment operator. Uses copy semantics except for the logger
137 // for which a reference copy is made
138 Fit2D& operator=(const Fit2D& other);
139
140 // Add a model to the list to be simultaneously fit and
141 // return its index. Specify the initial guesses for
142 // the model and a mask indicating whether the parameter
143 // is fixed (False) during the fit or not. Returns the
144 // the model number added (0, 1, 2 etc)
145 //<group>
147 const Vector<Double>& parameters,
148 const Vector<Bool>& parameterMask);
150 const Vector<Double>& parameters);
151 //</group>
152
153 // Convert mask from a string to a vector. The string gives the parameters
154 // to keep fixed in the fit (f (flux), x (x position), y (y position),
155 // a (FWHM major axis), b (FWHM minor axis), p (position angle)
156 static Vector<Bool> convertMask (const String fixedmask,
158
159
160 // Set a pixel selection range. When the fit is done, only
161 // pixels in the specified range are included/excluded.
162 // Only the last call of either of these will be active.
163 //<group>
164 void setIncludeRange (Double minVal, Double maxVal);
165 void setExcludeRange (Double minVal, Double maxVal);
167 //</group>
168
169 // Return number of parameters for this type of model
171
172 // Recover number of models
173 uInt nModels() const;
174
175 // Determine an initial estimate for the solution of the specified
176 // model type to the given data - no compound models are allowable
177 // in this function. If you have specified an include
178 // or exclude pixel range to the fitter, that will be honoured.
179 // This function does not interact with the addModel function.
180 // Returns a zero length vector if it fails to make an estimate.
181 //<group>
183 const MaskedLattice<T>& data);
184 template<class T> Vector<Double> estimate(
185 Fit2D::Types type, const Lattice<T>& data
186 );
187 template<class T> Vector<Double> estimate(
188 Fit2D::Types type, const Array<T>& data
189 );
190 template<class T> Vector<Double> estimate(
191 Fit2D::Types type, const Array<T>& data, const Array<Bool>& mask
192 );
193 //</group>
194
195 // Do the fit. Returns an enum value to tell you what happened if the fit failed
196 // for some reasons. A message can also be found with function errorMessage if
197 // the fit was not successful. For Array(i,j) i is x and j is y
198 //<group>
199 template <class T> Fit2D::ErrorTypes fit(const MaskedLattice<T>& data,
200 const Lattice<T>& sigma);
201 template <class T> Fit2D::ErrorTypes fit(const Lattice<T>& data,
202 const Lattice<T>& sigma);
203 template <class T> Fit2D::ErrorTypes fit(const Array<T>& data,
204 const Array<T>& sigma);
205 template <class T> Fit2D::ErrorTypes fit(const Array<T>& data,
206 const Array<Bool>& mask,
207 const Array<T>& sigma);
208 //</group>
209
210 // Find the residuals to the fit. xOffset and yOffset allow one to provide a data
211 // array that is offset in space from the grid that was fit. In this way, one
212 // can fill out a larger image than the subimage that was fit, for example. A negative
213 // value of xOffset means the supplied data array represents a grid that has a y axis left
214 // of the grid of pixels that was fit. A negative yOffset value means the supplied data
215 // array represents a grid that has an x axis that is below the x axis of the grid of pixels
216 // that was fit.
217 // NOTE these may need to be templated at some point in the future. My
218 // current need does not require they be templated. - dmehring 29jun2018
219 //<group>
220 template <class T> Fit2D::ErrorTypes residual(
221 Array<T>& resid, Array<T>& model,
222 const Array<T>& data, Int xOffset=0, int yOffset=0
223 ) const;
224
226 const MaskedLattice<Float>& data);
228 const Lattice<Float>& data);
229 //</group>
230 // If function fit failed, you will find a message here
231 // saying why it failed
233
234 // Recover solution for either all model components or
235 // a specific one. These functions will return an empty vector
236 // if there is no valid solution. All available parameters (fixed and
237 // adjustable) are included in the solution vectors.
238 //<group>
241 //</group>
242
243 // The errors. All available parameters (fixed and adjustable) are
244 // included in the error vectors. Unsolved for parameters will
245 // have error 0.
246 //<group>
249 //</group>
250
251 // The number of iterations that the fitter finished with
253
254 // The chi squared of the fit. Returns 0 if fit has been done.
256
257 // The number of points used for the last fit
259
260 // Return type as a string
262
263 // Return string type as enum (min match)
264 static Fit2D::Types type(const String& type);
265
266 // Find type of specific model
268
269 // Convert p.a. (radians) from positive +x -> +y
270 // (Fit2D) to positive +y -> -x (Gaussian2D)
271 static Double paToGauss2D (Double pa) {return pa - C::pi_2;};
272
273 // Convert p.a. (radians) from positive +y -> -x
274 // (Gaussian2D) to positive +x -> +y (Fit2D)
275 static Double paFromGauss2D (Double pa) {return pa + C::pi_2;};
276
277private:
278
290
292
294 const Matrix<Double>& pos,
295 const Vector<Double>& sigma);
296
297// Returns available (adjustable + fixed) solution for model of
298// interest and tells you where it began in the full solution vector
299// Does no axial ratio nor position angle conversions from direct
300// fit solution vector
301// <group>
303 Vector<Double> availableErrors (uInt& iStart, uInt which) const;
304// </group>
305
307 void setParams(const Vector<Double>& params, uInt which);
308
310 Int includeIt) const;
311
312 template <class T> Bool selectData (
313 Matrix<Double>& pos, Vector<Double>& values,
314 Vector<Double>& weights, const Array<T>& pixels,
315 const Array<Bool>& mask, const Array<T>& sigma
316 );
317
318 void piRange (Double& pa) const;
319
320};
321
323 Int includeIt) const
324{
325 if (includeIt==0) return True;
326//
327 if (includeIt==1) {
328 if (value >= range(0) && value <= range(1)) return True;
329 } else if (value < range(0) || value > range(1)) {
330 return True;
331 }
332//
333 return False;
334}
335
336} //# NAMESPACE CASACORE - END
337
338#ifndef CASACORE_NO_AUTO_TEMPLATES
339#include <casacore/lattices/LatticeMath/Fit2D2.tcc>
340#endif //# CASACORE_NO_AUTO_TEMPLATES
341
342#endif
343
344
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...
Fit2D::ErrorTypes fit(const Array< T > &data, const Array< Bool > &mask, const Array< T > &sigma)
Fit2D::ErrorTypes residual(Array< Float > &resid, Array< Float > &model, const Lattice< Float > &data)
Vector< Double > availableErrors(uInt which) const
uInt itsNumberPoints
Definition Fit2D.h:289
LogIO itsLogger
Definition Fit2D.h:279
Vector< Double > availableSolution() const
Recover solution for either all model components or a specific one.
Bool selectData(Matrix< Double > &pos, Vector< Double > &values, Vector< Double > &weights, const Array< T > &pixels, const Array< Bool > &mask, const Array< T > &sigma)
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.
String errorMessage() const
If function fit failed, you will find a message here saying why it failed.
Fit2D::ErrorTypes fit(const Lattice< T > &data, const Lattice< T > &sigma)
Fit2D(LogIO &logger)
Constructor.
static Double paToGauss2D(Double pa)
Convert p.a.
Definition Fit2D.h:271
Fit2D(const Fit2D &other)
Copy constructor.
Vector< Double > itsSolution
Definition Fit2D.h:285
Vector< Double > availableSolution(uInt &iStart, uInt which) const
Returns available (adjustable + fixed) solution for model of interest and tells you where it began in...
void setIncludeRange(Double minVal, Double maxVal)
Set a pixel selection range.
NonLinearFitLM< Double > itsFitter
Definition Fit2D.h:284
static uInt nParameters(Fit2D::Types type)
Return number of parameters for this type of model.
Fit2D & operator=(const Fit2D &other)
Assignment operator.
Bool itsValid
Definition Fit2D.h:280
Vector< Double > estimate(Fit2D::Types type, const Array< T > &data)
Vector< uInt > itsTypeList
Definition Fit2D.h:291
Double chiSquared() const
The chi squared of the fit.
Fit2D::ErrorTypes fit(const MaskedLattice< T > &data, const Lattice< T > &sigma)
Do the fit.
Vector< Double > estimate(Fit2D::Types type, const Lattice< T > &data)
CompoundFunction< AutoDiff< Double > > itsFunction
Definition Fit2D.h:283
static String type(Fit2D::Types type)
Return type as a string.
void setExcludeRange(Double minVal, Double maxVal)
Vector< Double > availableErrors(uInt &iStart, uInt which) const
String itsErrorMessage
Definition Fit2D.h:288
uInt addModel(Fit2D::Types type, const Vector< Double > &parameters)
Bool itsHasSigma
Definition Fit2D.h:280
Fit2D::ErrorTypes residual(Array< Float > &resid, Array< Float > &model, const MaskedLattice< Float > &data)
uInt numberIterations() const
The number of iterations that the fitter finished with.
Fit2D::ErrorTypes fitData(const Vector< Double > &values, const Matrix< Double > &pos, const Vector< Double > &sigma)
Double itsChiSquared
Definition Fit2D.h:287
Types
Enum describing the different models you can fit.
Definition Fit2D.h:102
Vector< Double > availableErrors() const
The errors.
static Vector< Bool > convertMask(const String fixedmask, Fit2D::Types type)
Convert mask from a string to a vector.
void piRange(Double &pa) const
static Fit2D::Types type(const String &type)
Return string type as enum (min match)
Fit2D::Types type(uInt which)
Find type of specific model.
uInt nModels() const
Recover number of models.
Vector< Double > availableSolution(uInt which) const
Vector< Double > itsErrors
Definition Fit2D.h:286
void setParams(const Vector< Double > &params, uInt which)
Bool itsInclude
Definition Fit2D.h:281
Fit2D::ErrorTypes fit(const Array< T > &data, const Array< T > &sigma)
ErrorTypes
Enum describing output error conditions.
Definition Fit2D.h:111
@ FAILED
Solution failed.
Definition Fit2D.h:117
@ NOMODELS
No models set.
Definition Fit2D.h:121
@ NOGOOD
There were no unmasked points.
Definition Fit2D.h:119
@ NOCONVERGE
Did not converge.
Definition Fit2D.h:115
@ nErrorTypes
Number of conditions.
Definition Fit2D.h:123
Bool itsValidSolution
Definition Fit2D.h:280
Vector< Double > estimate(Fit2D::Types type, const Array< T > &data, const Array< Bool > &mask)
Vector< Double > itsPixelRange
Definition Fit2D.h:282
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.
uInt numberPoints() const
The number of points used for the last fit.
Vector< Double > getParams(uInt which) const
Bool includeIt(Double value, const Vector< Double > &range, Int includeIt) const
Definition Fit2D.h:322
~Fit2D()
Destructor.
static Double paFromGauss2D(Double pa)
Convert p.a.
Definition Fit2D.h:275
String: the storage and methods of handling collections of characters.
Definition String.h:223
constexpr double pi_2
pi/2
Definition Constants.h:369
this file contains all the compiler specific defines
Definition mainpage.dox:28
LatticeExprNode pa(const LatticeExprNode &left, const LatticeExprNode &right)
This function finds 180/pi*atan2(left,right)/2.
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53