mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
mx::math::fit::fitGaussian2D< fitterT > Class Template Reference

template<typename fitterT>
class mx::math::fit::fitGaussian2D< fitterT >

Class to manage fitting a 2D Gaussian to data via the levmarInterface.

Can fit either the symmetric Gaussian, or the rotated asymmetric Gaussian. Individual parameters can be fixed, except in the asymmetric case \(\sigma_x, \sigma_y, \theta\) must currently be fixed together.

In addition to the requirements on fitterT specified by levmarInterface this class also requires the following in fitterT

static const int nparams = 7; //where the number 7 is replaced by the number of parameters that fitterT expects to fit.
void paramNormalizer( array2FitGaussian2D * arr,
realT *,
int); //A function which converts from input parameters to fitting parameters. May do nothing.
array2FitGaussian2D< realT > arr
Data array to pass to the levmar library. Contains the actual data plus the parameters if fixed.
Template Parameters
fitterTa type meeting the above requirements.
Test:
Scenario: Verify direction and accuracy of various image shifts [test doc]

Definition at line 322 of file fitGaussian.hpp.

#include <math/fit/fitGaussian.hpp>

+ Inheritance diagram for mx::math::fit::fitGaussian2D< fitterT >:

Public Member Functions

void setFixed (bool G0, bool G, bool x0, bool y0, bool sigma_x, bool sigma_y, bool theta)
 Set whether each parameter is fixed. More...
 
void setFixed (bool G0, bool G, bool x0, bool y0, bool sigma)
 Set whether each parameter is fixed. More...
 
void setGuess (realT G0, realT G, realT x0, realT y0, realT sigma)
 Set the initial guess for a symmetric Gaussian. More...
 
void setGuess (realT G0, realT G, realT x0, realT y0, realT sigma_x, realT sigma_y, realT theta)
 Set the initial guess for the general Gaussian. More...
 
void setArray (realT *data, int nx, int ny)
 Set the data aray. More...
 
void setArray (realT *data, int nx, int ny, realT *mask)
 Set the data aray, with a mask. More...
 
int fit ()
 Do the fit. More...
 
realT G0 ()
 Get the current value of G0, the constant. More...
 
realT G ()
 Get the peak scaling. More...
 
realT x0 ()
 Get the center x-coordinate. More...
 
realT y0 ()
 Get the center y-coordinate. More...
 
realT sigma ()
 Return the width parameter. More...
 
realT fwhm ()
 Return the full-width at half maximum. More...
 
realT sigma_x ()
 Return the width parameter on the long axis. More...
 
realT sigma_y ()
 Return the width parameter on the short axis. More...
 
realT theta ()
 Return the orientation of the long axis. More...
 
- Public Member Functions inherited from mx::math::fit::levmarInterface< fitterT >
 levmarInterface ()
 Default constructor. More...
 
 levmarInterface (realT *i_p, realT *i_x, int i_m, int i_n, void *i_adata)
 Setup constructor. More...
 
 ~levmarInterface ()
 Destructor. More...
 
void nParams (int i_m)
 Set number of parameters, but don't allocate. More...
 
int nParams ()
 Get the current number of parameters. More...
 
void allocate_params ()
 Allocate parameters array based on previous call to nParams. More...
 
void allocate_params (int i_m)
 Set number of parameters and allocate. More...
 
void point_params (realT *i_p)
 Point the parameter pointer at an externally allocated array. More...
 
void point_params (realT *i_p, int i_m)
 Point the parameter pointer at an externally allocated array. More...
 
void set_params (realT *i_p)
 Copy parameters to the parameter array. More...
 
realT * get_params ()
 Get current pointer array address. More...
 
void set_itmax (int i_itmax)
 Set the maximum number of iterations. More...
 
int get_itmax ()
 Get the maximum number of iterations. More...
 
void allocate_work ()
 Allocate the work and covar matrices. More...
 
void set_opts (int n, realT val)
 Set one of the minimization options to val. More...
 
void set_opts_default (int n=-1)
 Set one or all of the minimization options to the default. More...
 
realT get_opts (int n)
 Get the current value of an option. More...
 
int fit ()
 Perform the fit. More...
 
realT get_initial_norm ()
 Returns the L2-norm before minimization occurs. More...
 
realT get_final_norm ()
 Returns the L2-norm at the end of the minimization. More...
 
int get_iterations ()
 Get the number of iterations taken during the minimization. More...
 
int get_reason_code ()
 Get a code specifying the reason minimization terminated. More...
 
std::string get_reason_string ()
 Get the descriptive string describing the reason minimization terminated. More...
 
int get_fevals ()
 Get the number of function evaluations during the minimization. More...
 
int get_jevals ()
 Get the number of jacobian evaluations during the minimization. More...
 
int get_nlinsys ()
 Get the number of linear system solutions during the minimization. More...
 
double get_deltaT ()
 Get the elapsed time of the fit. More...
 
template<typename iosT , char comment = '#'>
iosT & dumpParameters (iosT &ios)
 Output current parameters to a stream. More...
 
template<char comment = '#'>
std::ostream & dumpParameters ()
 Dump the parameter vector to stdout. More...
 
template<typename iosT , char comment = '#'>
iosT & dumpReport (iosT &ios, bool dumpParams=true)
 Output current parameters to a stream. More...
 
template<char comment = '#'>
std::ostream & dumpReport (bool dumpParams=true)
 Dump a status report to stdout. More...
 

Public Attributes

array2FitGaussian2D< realT > arr
 Data array to pass to the levmar library. Contains the actual data plus the parameters if fixed. More...
 
- Public Attributes inherited from mx::math::fit::levmarInterface< fitterT >
realT * x
 I: measurement vector. NULL implies a zero vector. More...
 
int n
 I: measurement vector dimension. More...
 
realT opts [LM_OPTS_SZ]
 Options passed to the minimization routines. See set_opts for details. More...
 
realT info [LM_INFO_SZ]
 Information regarding the minimization. More...
 
double deltaT
 Elapsed time of the fitting procedure. More...
 
realT * work
 Working memory passed to the levmar routines. More...
 
int work_sz
 The current size of the work array. More...
 
realT * covar
 Covariance matrix corresponding to LS solution; mxm. More...
 
int covar_sz
 The current size of the covar array. More...
 
bool getCovar
 Controls whether the covar array is allocated. More...
 
void * adata
 Pointer to possibly additional data, passed uninterpreted to func & jacf. More...
 

Additional Inherited Members

- Protected Attributes inherited from mx::math::fit::levmarInterface< fitterT >
realT * p
 Parameter array. On input is the initial estimates. On output has the estimated solution. More...
 
realT * init_p
 Parameter array on input, saved for comparison. More...
 
int m
 Parameter vector dimension (i.e. number of unknowns) More...
 
bool own_p
 Flag indicating whether the p array is owned by this object (for de-allocation). More...
 
int itmax
 Maximum number of iterations, default is 100. More...
 

Member Function Documentation

◆ fit()

template<typename fitterT >
int mx::math::fit::fitGaussian2D< fitterT >::fit ( )
inline

Do the fit.

Definition at line 464 of file fitGaussian.hpp.

References mx::math::fit::levmarInterface< fitterT >::fit().

Referenced by SCENARIO().

◆ fwhm()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::fwhm ( )
inline

Return the full-width at half maximum.

This is a simple scaling of the sigma() result

Definition at line 517 of file fitGaussian.hpp.

References mx::math::func::sigma2fwhm().

◆ G()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::G ( )
inline

Get the peak scaling.

Definition at line 487 of file fitGaussian.hpp.

◆ G0()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::G0 ( )
inline

Get the current value of G0, the constant.

Returns
the current value of G0.

Definition at line 481 of file fitGaussian.hpp.

◆ setArray() [1/2]

template<typename fitterT >
void mx::math::fit::fitGaussian2D< fitterT >::setArray ( realT *  data,
int  nx,
int  ny 
)
inline

Set the data aray.

Parameters
[in]dataThe 2D array of data to fit.
[in]nxthe x size of the data
[in]nythe y size of the data

Definition at line 424 of file fitGaussian.hpp.

References mx::math::fit::array2FitGaussian2D< realT >::data, mx::math::fit::array2FitGaussian2D< realT >::mask, mx::math::fit::array2FitGaussian2D< realT >::nx, and mx::math::fit::array2FitGaussian2D< realT >::ny.

Referenced by SCENARIO().

◆ setArray() [2/2]

template<typename fitterT >
void mx::math::fit::fitGaussian2D< fitterT >::setArray ( realT *  data,
int  nx,
int  ny,
realT *  mask 
)
inline

Set the data aray, with a mask.

Parameters
[in]dataThe 2D array of data to fit.
[in]nxthe x size of the data
[in]nythe y size of the data
[in]maskArray of same size as data. Any 0 pixels in this array will be excluded from the fit.

Definition at line 438 of file fitGaussian.hpp.

References mx::math::fit::array2FitGaussian2D< realT >::data, mx::math::fit::array2FitGaussian2D< realT >::mask, mx::math::fit::array2FitGaussian2D< realT >::nx, and mx::math::fit::array2FitGaussian2D< realT >::ny.

◆ setFixed() [1/2]

template<typename fitterT >
void mx::math::fit::fitGaussian2D< fitterT >::setFixed ( bool  G0,
bool  G,
bool  x0,
bool  y0,
bool  sigma 
)
inline

Set whether each parameter is fixed.

Sets the parameter indices appropriately.

Parameters
[in]G0if true, then G0 will be not be part of the fit
[in]Gif true, then G will be not be part of the fit
[in]x0if true, then x0 will be not be part of the fit
[in]y0if true, then y0 will be not be part of the fit
[in]sigmaif true, then sigma will be not be part of the fit

Definition at line 373 of file fitGaussian.hpp.

References mx::astro::constants::G(), mx::math::fit::array2FitGaussian2D< realT >::setFixed(), and mx::astro::constants::sigma().

◆ setFixed() [2/2]

template<typename fitterT >
void mx::math::fit::fitGaussian2D< fitterT >::setFixed ( bool  G0,
bool  G,
bool  x0,
bool  y0,
bool  sigma_x,
bool  sigma_y,
bool  theta 
)
inline

Set whether each parameter is fixed.

Sets the parameter indices appropriately.

Parameters
[in]G0if true, then G0 will be not be part of the fit
[in]Gif true, then G will be not be part of the fit
[in]x0if true, then x0 will be not be part of the fit
[in]y0if true, then y0 will be not be part of the fit
[in]sigma_xif true, then sigma_x will be not be part of the fit
[in]sigma_yif true, then sigma_y will be not be part of the fit
[in]thetaif true, then theta will be not be part of the fit

Definition at line 357 of file fitGaussian.hpp.

References mx::astro::constants::G(), and mx::math::fit::array2FitGaussian2D< realT >::setFixed().

◆ setGuess() [1/2]

template<typename fitterT >
void mx::math::fit::fitGaussian2D< fitterT >::setGuess ( realT  G0,
realT  G,
realT  x0,
realT  y0,
realT  sigma 
)
inline

Set the initial guess for a symmetric Gaussian.

Also works for the general case, setting the same width in both directions.

Parameters
[in]G0the constant background level
[in]Gthe peak scaling
[in]x0the center x-coordinate
[in]y0the center y-coordinate
[in]sigmathe width parameter

Definition at line 387 of file fitGaussian.hpp.

References mx::astro::constants::G(), and mx::astro::constants::sigma().

Referenced by SCENARIO().

◆ setGuess() [2/2]

template<typename fitterT >
void mx::math::fit::fitGaussian2D< fitterT >::setGuess ( realT  G0,
realT  G,
realT  x0,
realT  y0,
realT  sigma_x,
realT  sigma_y,
realT  theta 
)
inline

Set the initial guess for the general Gaussian.

Parameters
[in]G0the constant background level
[in]Gthe peak scaling
[in]x0the center x-coordinate
[in]y0the center y-coordinate
[in]sigma_xthe width parameter in the rotated x-direction (the long axis)
[in]sigma_ythe width parameter in the rotated y-direction
[in]thetathe angle of the long axis (always sigma_x)

Definition at line 404 of file fitGaussian.hpp.

References mx::astro::constants::G().

◆ sigma()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::sigma ( )
inline

Return the width parameter.

As described for the symmetric Gaussian.

For the general Gaussian, this returns \( \sigma = \sqrt{ \sigma_x^2 + \sigma_y^2} \).

Definition at line 509 of file fitGaussian.hpp.

◆ sigma_x()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::sigma_x ( )
inline

Return the width parameter on the long axis.

Definition at line 523 of file fitGaussian.hpp.

◆ sigma_y()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::sigma_y ( )
inline

Return the width parameter on the short axis.

Definition at line 529 of file fitGaussian.hpp.

◆ theta()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::theta ( )
inline

Return the orientation of the long axis.

Definition at line 535 of file fitGaussian.hpp.

◆ x0()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::x0 ( )
inline

Get the center x-coordinate.

Definition at line 493 of file fitGaussian.hpp.

Referenced by SCENARIO().

◆ y0()

template<typename fitterT >
realT mx::math::fit::fitGaussian2D< fitterT >::y0 ( )
inline

Get the center y-coordinate.

Definition at line 499 of file fitGaussian.hpp.

Referenced by SCENARIO().

Member Data Documentation

◆ arr

template<typename fitterT >
array2FitGaussian2D<realT> mx::math::fit::fitGaussian2D< fitterT >::arr

Data array to pass to the levmar library. Contains the actual data plus the parameters if fixed.

Definition at line 329 of file fitGaussian.hpp.


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