lsst.gauss2d.fit g199a45376c+5e234f8357
 
Loading...
Searching...
No Matches
lsst::gauss2d::fit::Model< T, Image, Indices, Mask > Class Template Reference

A collection of Sources comprising a ParametricModel of Data. More...

#include <model.h>

Inheritance diagram for lsst::gauss2d::fit::Model< T, Image, Indices, Mask >:
lsst::gauss2d::fit::ParametricModel lsst::gauss2d::fit::Parametric

Public Types

typedef GaussianEvaluator< T, Image, Indices > Evaluator
 
typedef std::map< std::reference_wrapper< const Channel >, std::vector< std::unique_ptr< const Gaussians > > > GaussiansMap
 
typedef Data< T, Image, Mask > ModelData
 
typedef ModelData::Observation Observation
 

Public Member Functions

 Model (std::shared_ptr< const ModelData > data, PsfModels &psfmodels, Sources &sources, Priors &priors)
 
void add_extra_param_map (const Channel &channel, ExtraParamMap &map_extra, const GradParamMap &map_grad, ParameterMap &offsets) const override
 
void add_extra_param_factors (const Channel &channel, ExtraParamFactors &factors) const override
 
void add_grad_param_map (const Channel &channel, GradParamMap &map, ParameterMap &offsets) const override
 
void add_grad_param_factors (const Channel &channel, GradParamFactors &factors) const override
 
std::vector< double > compute_loglike_grad (bool include_prior=true, bool print=false, bool verify=false, double findiff_frac=1e-5, double findiff_add=1e-5, double rtol=1e-3, double atol=1e-8)
 
std::unique_ptr< Image > compute_hessian (bool transformed=false, bool include_prior=true, std::optional< HessianOptions > options=std::nullopt, bool print=false)
 
std::vector< double > evaluate (bool print=false, bool normalize_loglike=false)
 
double evaluate_observation (size_t idx)
 
std::shared_ptr< const ModelDataget_data () const
 Return _data.
 
EvaluatorMode get_mode () const
 
std::unique_ptr< const lsst::gauss2d::Gaussians > get_gaussians (const Channel &channel) const override
 
std::vector< double > get_loglike_const_terms ()
 
size_t get_n_gaussians (const Channel &channel) const override
 Return the number of Gaussian sub-components controlled by this model.
 
std::vector< std::shared_ptr< Image > > get_outputs () const
 Return _outputs (output Image instances for each Observation in _data)
 
std::vector< std::pair< ParamBaseCRef, size_t > > get_offsets_parameters () const
 
ParamRefs & get_parameters (ParamRefs &params, ParamFilter *filter=nullptr) const override
 
ParamCRefs & get_parameters_const (ParamCRefs &params, ParamFilter *filter=nullptr) const override
 Same as get_parameters(), but for const refs.
 
ParamRefs & get_parameters_observation (ParamRefs &params, size_t idx, ParamFilter *filter=nullptr) const
 Same as get_parameters(), but for a single Observation with index idx in _data.
 
ParamCRefs & get_parameters_observation_const (ParamCRefs &params, size_t idx, ParamFilter *filter=nullptr) const
 Same as get_parameters_const(), but for a single Observation with index idx in _data.
 
Priors get_priors () const
 Return _priors, the list of Prior instances.
 
PsfModels get_psfmodels () const
 Return _psfmodels, the list of PsfModel instances for each Observation in _data.
 
Sources get_sources () const
 Return _sources, the list of Source instances for each Observation in _data.
 
void set_extra_param_factors (const Channel &channel, ExtraParamFactors &factors, size_t index) const override
 
void set_grad_param_factors (const Channel &channel, GradParamFactors &factors, size_t index) const override
 
void setup_evaluators (EvaluatorMode mode=EvaluatorMode::image, std::vector< std::vector< std::shared_ptr< Image > > > outputs={}, std::vector< std::shared_ptr< Image > > residuals={}, std::vector< std::shared_ptr< Image > > outputs_prior={}, std::shared_ptr< Image > residuals_prior=nullptr, bool force=false, bool print=false)
 
size_t size () const
 Get the size of this->_data.
 
std::string repr (bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
 
std::string str () const override
 
std::vector< std::string > verify_jacobian (double findiff_frac=1e-5, double findiff_add=1e-5, double rtol=1e-3, double atol=1e-3, double max_ll_diff=0)
 
- Public Member Functions inherited from lsst::gauss2d::fit::Parametric
ParamRefs get_parameters_new (ParamFilter *filter=nullptr) const
 Same as get_parameters(), but returning a new vector.
 
ParamCRefs get_parameters_const_new (ParamFilter *filter=nullptr) const
 Same as get_parameters_const(), but returning a new vector.
 

Detailed Description

template<typename T, typename Image, typename Indices, typename Mask>
class lsst::gauss2d::fit::Model< T, Image, Indices, Mask >

A collection of Sources comprising a ParametricModel of Data.

Template Parameters
TThe type of the Image (usually float or double).
ImageThe class of image used in Data.
IndicesThe class of index map used in Data.
MaskThe class of mask used in Data.

Constructor & Destructor Documentation

◆ Model()

template<typename T, typename Image, typename Indices, typename Mask>
lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::Model ( std::shared_ptr< const ModelData > data,
PsfModels & psfmodels,
Sources & sources,
Priors & priors )
inlineexplicit

Construct a Model instance from Data, PsfModels, Sources and Priors.

Parameters
dataThe data to model.
psfmodelsA vector of PSF models, ordered to match each Observation in data.
sourcesA vector of Source models.
priorsA vector of Prior likelihoods.

Member Function Documentation

◆ add_extra_param_factors()

template<typename T, typename Image, typename Indices, typename Mask>
void lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::add_extra_param_factors ( const Channel & channel,
ExtraParamFactors & factors ) const
inlineoverridevirtual

Add extra Parameter gradient factors to an existing vector.

Parameters
channelThe Channel to add factors for.
factorsThe ExtraParamFactors to add to.

Implements lsst::gauss2d::fit::ParametricModel.

◆ add_extra_param_map()

template<typename T, typename Image, typename Indices, typename Mask>
void lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::add_extra_param_map ( const Channel & channel,
ExtraParamMap & map_extra,
const GradParamMap & map_grad,
ParameterMap & offsets ) const
inlineoverridevirtual

Add extra Parameter indices to a map.

Parameters
channelThe Channel to add indices for.
map_extraThe ExtraParamMap to add to.
map_gradThe completed GradParamMap.
offsetsA map of index offsets for Parameters that have already been added.

Implements lsst::gauss2d::fit::ParametricModel.

◆ add_grad_param_factors()

template<typename T, typename Image, typename Indices, typename Mask>
void lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::add_grad_param_factors ( const Channel & channel,
GradParamFactors & factors ) const
inlineoverridevirtual

Add Parameter gradient factors to an existing map.

Parameters
channelThe Channel to add factors for.
factorsThe GradParamFactors to add to.

Implements lsst::gauss2d::fit::ParametricModel.

◆ add_grad_param_map()

template<typename T, typename Image, typename Indices, typename Mask>
void lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::add_grad_param_map ( const Channel & channel,
GradParamMap & map,
ParameterMap & offsets ) const
inlineoverridevirtual

Add Parameter gradient indices to an existing map.

Parameters
channelThe Channel to add indices for.
mapThe map to add to.
offsetsA map of index offsets for Parameters that have already been added.

Implements lsst::gauss2d::fit::ParametricModel.

◆ compute_hessian()

template<typename T, typename Image, typename Indices, typename Mask>
std::unique_ptr< Image > lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::compute_hessian ( bool transformed = false,
bool include_prior = true,
std::optional< HessianOptions > options = std::nullopt,
bool print = false )
inline

Compute the Hessian matrix (second order partial derivatives) of the log likehood.

Parameters
transformedWhether the matrix should be computed for transformed parameters or not If not, parameter transforms are temporarily removed.
include_priorWhether to include the prior likelihood(s) in the Hessian.
optionsOptions for computing the Hessian via finite differencing of loglikelihood gradients. If null, the Hessian is estimated as J^T J (where J is the Jacobian).
Returns
The Hessian matrix for all free parameters.

◆ compute_loglike_grad()

template<typename T, typename Image, typename Indices, typename Mask>
std::vector< double > lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::compute_loglike_grad ( bool include_prior = true,
bool print = false,
bool verify = false,
double findiff_frac = 1e-5,
double findiff_add = 1e-5,
double rtol = 1e-3,
double atol = 1e-8 )
inline

Compute the gradient (partial first derivative) of the log-likelihood for each free parameter.

Parameters
include_priorWhether to include the prior likelihood(s) in the gradients.
printWhether to print diagnostic/debugging information.
verifyWhether to verify the values by comparing to finite differences.
findiff_fracThe value of the finite difference increment, as a fraction of the parameter value.
findiff_addThe minimum value of the finite difference increment.
rtolThe allowed relative tolerance in the Jacobian as compared to the finite difference.
atolThe allowed absolute tolerance in the Jacobian as compared to the finite difference.
Returns
The gradient of the log likelihoods for the free parameters of the model, in the order returned by get_parameters.

◆ evaluate()

template<typename T, typename Image, typename Indices, typename Mask>
std::vector< double > lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::evaluate ( bool print = false,
bool normalize_loglike = false )
inline

Evaluate the model for every Observation in _data.

Parameters
printWhether to print diagnostic statements to stdout.
normalize_loglikeWhether to include the normalizing (variance-dependent) term in the log likelihood. If false, the log likelihood for a model with no residuals is 0.
Returns
The log likelihood of each observation, followed by the summed log likelihood of the priors.

◆ evaluate_observation()

template<typename T, typename Image, typename Indices, typename Mask>
double lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::evaluate_observation ( size_t idx)
inline

Evaluate a single observation with the given index in _data.

Parameters
idxThe numeric index of the observation.
Returns
The log likelihood of the model for that observation.

◆ get_gaussians()

template<typename T, typename Image, typename Indices, typename Mask>
std::unique_ptr< const lsst::gauss2d::Gaussians > lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::get_gaussians ( const Channel & channel) const
inlineoverridevirtual

Return the vector of Gaussian sub-components controlled by this model.

Parameters
channelThe Channel to return Gaussians for.
Returns
The Gaussians controlled by this model.

Implements lsst::gauss2d::fit::ParametricModel.

◆ get_loglike_const_terms()

template<typename T, typename Image, typename Indices, typename Mask>
std::vector< double > lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::get_loglike_const_terms ( )
inline

Get the constant (variance-dependent) terms of the log likelihood for each observation.

Returns
A vector with the constant terms for each observation, and a final term for the priors.

◆ get_n_gaussians()

template<typename T, typename Image, typename Indices, typename Mask>
size_t lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::get_n_gaussians ( const Channel & channel) const
inlineoverridevirtual

Return the number of Gaussian sub-components controlled by this model.

Implements lsst::gauss2d::fit::ParametricModel.

◆ get_parameters()

template<typename T, typename Image, typename Indices, typename Mask>
ParamRefs & lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::get_parameters ( ParamRefs & params,
ParamFilter * filter = nullptr ) const
inlineoverridevirtual

Add Parameter refs matching the filter to a vector, in order.

Parameters
paramsThe vector to add to.
filterThe filter to apply to this Object's parameters.
Returns
A ref to params (for method chaining)

Implements lsst::gauss2d::fit::Parametric.

◆ get_parameters_const()

template<typename T, typename Image, typename Indices, typename Mask>
ParamCRefs & lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::get_parameters_const ( ParamCRefs & params,
ParamFilter * filter = nullptr ) const
inlineoverridevirtual

Same as get_parameters(), but for const refs.

Implements lsst::gauss2d::fit::Parametric.

◆ set_extra_param_factors()

template<typename T, typename Image, typename Indices, typename Mask>
void lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::set_extra_param_factors ( const Channel & channel,
ExtraParamFactors & factors,
size_t index ) const
inlineoverridevirtual

Set extra Parameter gradient factors in an existing map.

Parameters
channelThe Channel to set factors for.
factorsThe ExtraParamFactors to set factors for.
indexThe index to begin setting factors at.

Implements lsst::gauss2d::fit::ParametricModel.

◆ set_grad_param_factors()

template<typename T, typename Image, typename Indices, typename Mask>
void lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::set_grad_param_factors ( const Channel & channel,
GradParamFactors & factors,
size_t index ) const
inlineoverridevirtual

Set Parameter gradient factors in an existing map.

Parameters
channelThe Channel to set factors for.
factorsThe GradParamFactors to set factors for.
indexThe index to begin setting factors at.

Implements lsst::gauss2d::fit::ParametricModel.

◆ setup_evaluators()

template<typename T, typename Image, typename Indices, typename Mask>
void lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::setup_evaluators ( EvaluatorMode mode = EvaluatorMode::image,
std::vector< std::vector< std::shared_ptr< Image > > > outputs = {},
std::vector< std::shared_ptr< Image > > residuals = {},
std::vector< std::shared_ptr< Image > > outputs_prior = {},
std::shared_ptr< Image > residuals_prior = nullptr,
bool force = false,
bool print = false )
inline

Setup Evaluator instances for every Observation in _data using the given EvaluatorMode.

Parameters
modeThe EvaluatorMode to use for all Evaluator instances.
outputsA vector of vectors of Image outputs for each Evaluator (created if empty and needed).
residualsA vector of residual Images for each Evaluator (created if empty and needed).
outputs_priorA vector of prior output (Jacobian) Images for each Evaluator (created if empty and needed).
residuals_priorA vector of prior residual Images for each Evaluator (created if empty and needed).
forceWhether to force setting up even if already set up in the same mode
printWhether to print diagnostic statements to stdout.
Note
Different modes require different sized vectors for outputs EvaluatorMode::jacobian requires one Image per free parameter per Observation. EvaluatorMode::loglike_grad requires only one Image(n_rows=1, n_cols=n_params_free)

◆ verify_jacobian()

template<typename T, typename Image, typename Indices, typename Mask>
std::vector< std::string > lsst::gauss2d::fit::Model< T, Image, Indices, Mask >::verify_jacobian ( double findiff_frac = 1e-5,
double findiff_add = 1e-5,
double rtol = 1e-3,
double atol = 1e-3,
double max_ll_diff = 0 )
inline

Verify that the Jacobian is correct by comparing to finite differences.

Parameters
findiff_fracThe value of the finite difference increment, as a fraction of the parameter value.
findiff_addThe minimum value of the finite difference increment.
rtolThe allowed relative tolerance in the Jacobian as compared to the finite difference.
atolThe allowed absolute tolerance in the Jacobian as compared to the finite difference.
max_ll_diffThe maximum allowed difference between equivalent log-likelihood evaluations. Must be >= 0 and should only be a few orders of magnitude larger than machine epsilon.
Returns
A vector of error messages, one for each Parameter that failed verification.
Note
Verification is done using isclose(), which is modelled after Python's numpy.isclose.

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