Loading [MathJax]/extensions/tex2jax.js
LSST Applications g04a91732dc+cc8870d3f5,g07dc498a13+5aa0b8792f,g0fba68d861+80045be308,g1409bbee79+5aa0b8792f,g1a7e361dbc+5aa0b8792f,g1fd858c14a+f64bc332a9,g208c678f98+1ae86710ed,g35bb328faa+fcb1d3bbc8,g4d2262a081+47ad8a29a8,g4d39ba7253+9633a327c1,g4e0f332c67+5d362be553,g53246c7159+fcb1d3bbc8,g60b5630c4e+9633a327c1,g668ecb457e+25d63fd678,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g8852436030+8b64ca622a,g89139ef638+5aa0b8792f,g89e1512fd8+04325574d3,g8d6b6b353c+9633a327c1,g9125e01d80+fcb1d3bbc8,g989de1cb63+5aa0b8792f,g9f33ca652e+b196626af7,ga9baa6287d+9633a327c1,gaaedd4e678+5aa0b8792f,gabe3b4be73+1e0a283bba,gb1101e3267+71e32094df,gb58c049af0+f03b321e39,gb90eeb9370+2807b1ad02,gcf25f946ba+8b64ca622a,gd315a588df+a39986a76f,gd6cbbdb0b4+c8606af20c,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+4e42d81ab7,ge278dab8ac+932305ba37,ge82c20c137+76d20ab76d,gfe73954cf8+a1301e4c20,w.2025.11
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
lsst::gauss2d::fit::ShapePrior Class Reference

A two-part prior on the shape of a parametric ellipse. More...

#include <shapeprior.h>

Inheritance diagram for lsst::gauss2d::fit::ShapePrior:
lsst::gauss2d::fit::Prior lsst::gauss2d::Object

Public Member Functions

 ShapePrior (std::shared_ptr< const ParametricEllipse > ellipse, std::shared_ptr< ParametricGaussian1D > prior_size=nullptr, std::shared_ptr< ParametricGaussian1D > prior_axrat=nullptr, std::shared_ptr< ShapePriorOptions > options=nullptr)
 Construct a ShapePrior from a Parameter and mean_size/std.
 
 ~ShapePrior ()
 
PriorEvaluation evaluate (bool calc_jacobians=false, bool normalize_loglike=false) const override
 Evaluate the log likelihood and residual-dependent terms.
 
std::shared_ptr< ParametricGaussian1Dget_prior_size () const
 
std::shared_ptr< ParametricGaussian1Dget_prior_axrat () const
 
std::vector< double > get_loglike_const_terms () const override
 Return the constant terms of the log likelihood (dependent on stddevs only)
 
void set_prior_size (std::shared_ptr< ParametricGaussian1D > prior_size)
 
void set_prior_axrat (std::shared_ptr< ParametricGaussian1D > prior_axrat)
 
size_t size () const override
 
std::string repr (bool name_keywords=false, std::string_view namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
 Return a full, callable string representation of this.
 
std::string str () const override
 Return a brief, human-readable string representation of this.
 

Static Public Member Functions

static std::string_view null_str (const std::string_view &namespace_separator)
 

Static Public Attributes

static constexpr std::string_view CC_NAMESPACE_SEPARATOR = "::"
 The C++ namespace separator.
 
static constexpr std::string_view NULL_STR_GENERAL = "None"
 
static constexpr std::string_view PY_NAMESPACE_SEPARATOR = "."
 

Detailed Description

A two-part prior on the shape of a parametric ellipse.

Note
The size and axis ratio priors are separate and optional.
The size prior applies to the major-axis size.

Definition at line 63 of file shapeprior.h.

Constructor & Destructor Documentation

◆ ShapePrior()

lsst::gauss2d::fit::ShapePrior::ShapePrior ( std::shared_ptr< const ParametricEllipse > ellipse,
std::shared_ptr< ParametricGaussian1D > prior_size = nullptr,
std::shared_ptr< ParametricGaussian1D > prior_axrat = nullptr,
std::shared_ptr< ShapePriorOptions > options = nullptr )
explicit

Construct a ShapePrior from a Parameter and mean_size/std.

deviation.

Parameters
ellipseThe ParametricEllipse to compute a prior for.
mean_sizeThe mean value of the size prior.
stddev_sizeThe standard deviation of the size prior.

Definition at line 109 of file shapeprior.cc.

113 : _ellipse(std::move(ellipse)),
114 _prior_size(std::move(prior_size)),
115 _prior_axrat(std::move(prior_axrat)),
116 _options(std::move((options == nullptr) ? std::make_shared<ShapePriorOptions>() : options)) {
117 if (_ellipse == nullptr) throw std::invalid_argument("ellipse param must not be null");
118 double loglike = this->evaluate().loglike;
119 if (!std::isfinite(loglike)) {
120 throw std::invalid_argument(this->str() + " has non-finite loglike=" + std::to_string(loglike)
121 + " on init");
122 }
123}
PriorEvaluation evaluate(bool calc_jacobians=false, bool normalize_loglike=false) const override
Evaluate the log likelihood and residual-dependent terms.
std::string str() const override
Return a brief, human-readable string representation of this.
T isfinite(T... args)
T make_shared(T... args)
T move(T... args)
@ loglike
Compute the log likelihood only.
Definition model.h:34
T to_string(T... args)

◆ ~ShapePrior()

lsst::gauss2d::fit::ShapePrior::~ShapePrior ( )

Definition at line 125 of file shapeprior.cc.

125{}

Member Function Documentation

◆ evaluate()

PriorEvaluation lsst::gauss2d::fit::ShapePrior::evaluate ( bool calc_jacobians = false,
bool normalize_loglike = false ) const
overridevirtual

Evaluate the log likelihood and residual-dependent terms.

Parameters
calc_jacobiansWhether to compute the Jacobian and residual terms.
normalize_loglikeWhether to add the constant (sigma-dependent) factors to the log likehood.
Returns
The result of the evaluation.

Implements lsst::gauss2d::fit::Prior.

Definition at line 127 of file shapeprior.cc.

127 {
128 const auto ellipse = lsst::gauss2d::Ellipse(this->_ellipse->get_size_x(), this->_ellipse->get_size_y(),
129 this->_ellipse->get_rho());
130 const auto ellipse_major = lsst::gauss2d::EllipseMajor(ellipse);
131
132 double size_maj = ellipse_major.get_r_major();
133 double axrat = size_maj == 0 ? 0 : ellipse_major.get_axrat();
134
135 if (!(axrat >= 0)) {
136 throw std::runtime_error(this->str() + " got invalid axrat=" + std::to_string(axrat));
137 }
138
139 auto result = PriorEvaluation(0, {}, {}, false);
140
141 if (this->_prior_size != nullptr) {
142 double size_maj_floor = this->_options->get_size_maj_floor();
143 size_maj = sqrt(size_maj * size_maj * axrat + size_maj_floor * size_maj_floor);
144 size_maj = _prior_size->get_mean_parameter().get_transform().forward(size_maj);
145 double sigma = _prior_size->get_stddev();
146 double residual = (size_maj - _prior_size->get_mean_parameter().get_value_transformed()) / sigma;
147 if (!std::isfinite(residual)) {
148 throw std::runtime_error(this->str() + ".evaluate() got non-finite size residual="
149 + std::to_string(residual) + " from size_maj=" + std::to_string(size_maj)
150 + ", _prior_size=" + _prior_size->str());
151 }
152 result.residuals.emplace_back(residual);
153 result.loglike += normalize ? logpdf_norm(residual, 1.0) : -residual * residual / 2.;
154 }
155
156 if (this->_prior_axrat != nullptr) {
157 double axrat_floor = this->_options->get_axrat_floor();
158 axrat = sqrt(axrat_floor * axrat_floor + axrat * axrat);
159 if (axrat > 1) {
160 axrat = 1;
161 } else if (!(axrat >= 0)) {
162 throw std::runtime_error(this->str() + " got invalid axrat=" + std::to_string(axrat)
163 + " from axrat_floor=" + std::to_string(axrat_floor));
164 }
165 const auto& transform_param = _prior_axrat->get_mean_parameter().get_transform();
166 axrat = transform_param.forward(axrat);
167 double sigma = _prior_axrat->get_stddev();
168 double residual = (axrat - _prior_axrat->get_mean_parameter().get_value_transformed()) / sigma;
169 if (!std::isfinite(residual)) {
170 auto str = this->str();
171 throw std::runtime_error(str + ".evaluate() got non-finite axrat residual="
172 + std::to_string(residual) + " from axrat=" + std::to_string(axrat)
173 + ", _prior_axrat=" + _prior_axrat->str());
174 }
175 // RuntimeError('Infinite axis ratio prior residual from q={axrat} and mean, std, f
176 // 'logit stretch divisor = {self.axrat_params}')
177 result.residuals.emplace_back(residual);
178 result.loglike += normalize ? logpdf_norm(residual, 1.0) : -residual * residual / 2.;
179 }
180
181 if (calc_jacobians) {
182 ParamRefs params = nonconsecutive_unique(_ellipse->get_parameters_new());
183 for (auto& paramref : params) {
184 auto& param = paramref.get();
185 double value_init = param.get_value();
186 double value_trans = param.get_value_transformed();
187 double delta = finite_difference_param(param, _options->get_delta_jacobian());
188 double value_new = param.get_value_transformed();
189
190 std::vector<double> residuals_old;
191 std::vector<double> residuals_new;
192 try {
193 param.set_value_transformed(value_trans + delta / 2.);
194 residuals_new = this->evaluate(false, normalize).residuals;
195 param.set_value_transformed(value_trans - delta / 2.);
196 residuals_old = this->evaluate(false, normalize).residuals;
197 } catch (std::exception& e) {
198 param.set_value_transformed(value_new);
199 residuals_new = this->evaluate(false, normalize).residuals;
200 residuals_old = result.residuals;
201 }
202 // Return to the old value
203 param.set_value(value_init);
204 value_new = param.get_value();
205 if (value_new != value_init) {
206 throw std::runtime_error(this->str() + " could not return " + param.str()
207 + " to original value=" + to_string_float(value_init) + " (stuck at "
208 + to_string_float(value_new) + "); check limits");
209 }
210 std::vector<double> jacobians(result.residuals.size());
211 for (size_t idx = 0; idx < result.residuals.size(); ++idx) {
212 jacobians[idx] = (residuals_new[idx] - residuals_old[idx]) / delta;
213 }
214 result.jacobians[paramref] = jacobians;
215 }
216 }
217
218 return result;
219}
std::vector< double > residuals
Definition prior.h:21
std::vector< ParamBaseRef > ParamRefs
Definition param_defs.h:13
double finite_difference_param(ParamBase &param, double delta)
Definition param_defs.cc:6
double logpdf_norm(T residual, T sigma)
Definition math.h:18
std::vector< T > nonconsecutive_unique(const std::vector< T > &vec)
Definition util.h:92
std::string to_string_float(const T value, const int precision=6, const bool scientific=true)
Definition to_string.h:15
T sqrt(T... args)

◆ get_loglike_const_terms()

std::vector< double > lsst::gauss2d::fit::ShapePrior::get_loglike_const_terms ( ) const
overridevirtual

Return the constant terms of the log likelihood (dependent on stddevs only)

Implements lsst::gauss2d::fit::Prior.

Definition at line 225 of file shapeprior.cc.

225 {
226 return {this->_prior_size == nullptr ? 0 : (LOG_1 - log(this->_prior_size->get_stddev() * SQRT_2_PI)),
227 this->_prior_axrat == nullptr ? 0 : (LOG_1 - log(this->_prior_axrat->get_stddev() * SQRT_2_PI))};
228}
const double LOG_1
Definition math.h:8
const double SQRT_2_PI
Definition math.h:9

◆ get_prior_axrat()

std::shared_ptr< ParametricGaussian1D > lsst::gauss2d::fit::ShapePrior::get_prior_axrat ( ) const

Definition at line 223 of file shapeprior.cc.

223{ return _prior_axrat; }

◆ get_prior_size()

std::shared_ptr< ParametricGaussian1D > lsst::gauss2d::fit::ShapePrior::get_prior_size ( ) const

Definition at line 221 of file shapeprior.cc.

221{ return _prior_size; }

◆ null_str()

static std::string_view lsst::gauss2d::Object::null_str ( const std::string_view & namespace_separator)
inlinestaticinherited

Definition at line 49 of file object.h.

49 {
50 return namespace_separator == CC_NAMESPACE_SEPARATOR ? "nullptr" : NULL_STR_GENERAL;
51 }
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:45
static constexpr std::string_view NULL_STR_GENERAL
Definition object.h:46

◆ repr()

std::string lsst::gauss2d::fit::ShapePrior::repr ( bool name_keywords = false,
std::string_view namespace_separator = Object::CC_NAMESPACE_SEPARATOR ) const
overridevirtual

Return a full, callable string representation of this.

Parameters
name_keywordsWhether to prefix arguments with "{name}=", where name is the arg name in the header (as with keyword arguments in Python).
namespace_separatorThe string to use to delimit namespaces, i.e. :: in C++ and . in Python.
Returns
A callable string representation of this, which should return an an identical object to this.
Note
The representation with name_keywords=false must be callable in C++. The representation with name_keywords=true should be callable in Python, if there are any bindings.

Implements lsst::gauss2d::Object.

Definition at line 240 of file shapeprior.cc.

240 {
241 return type_name_str<ShapePrior>(false, namespace_separator) + "(" + (name_keywords ? "ellipse=" : "")
242 + _ellipse->repr(name_keywords, namespace_separator) + ", " + (name_keywords ? "prior_size=" : "")
243 + repr_ptr(_prior_size.get(), name_keywords, namespace_separator) + ", "
244 + (name_keywords ? "prior_axrat=" : "")
245 + repr_ptr(_prior_axrat.get(), name_keywords, namespace_separator) + ", "
246 + (name_keywords ? "options=" : "") + _options->repr(name_keywords, namespace_separator) + ")";
247}
std::string type_name_str(bool strip_namespace=false, std::string_view namespace_str=detail::NAMESPACE_SEPARATOR)
Get a string representation of an arbitrary C++ type, potentially modifying its namespace prefix.
Definition type_name.h:104
std::string repr_ptr(T ptr, bool name_keywords, std::string_view namespace_separator)
Definition object.h:82

◆ set_prior_axrat()

void lsst::gauss2d::fit::ShapePrior::set_prior_axrat ( std::shared_ptr< ParametricGaussian1D > prior_axrat)

Definition at line 234 of file shapeprior.cc.

234 {
235 _prior_axrat = prior_axrat;
236}

◆ set_prior_size()

void lsst::gauss2d::fit::ShapePrior::set_prior_size ( std::shared_ptr< ParametricGaussian1D > prior_size)

Definition at line 230 of file shapeprior.cc.

230 {
231 _prior_size = prior_size;
232}

◆ size()

size_t lsst::gauss2d::fit::ShapePrior::size ( ) const
overridevirtual

Implements lsst::gauss2d::fit::Prior.

Definition at line 238 of file shapeprior.cc.

238{ return (this->_prior_size != nullptr) + (this->_prior_axrat != nullptr); };

◆ str()

std::string lsst::gauss2d::fit::ShapePrior::str ( ) const
overridevirtual

Return a brief, human-readable string representation of this.

Implements lsst::gauss2d::Object.

Definition at line 249 of file shapeprior.cc.

249 {
250 return type_name_str<ShapePrior>(true) + "(ellipse=" + _ellipse->str()
251 + ", prior_size=" + str_ptr(_prior_size.get()) + ", prior_axrat=" + str_ptr(_prior_axrat.get())
252 + ", options=" + _options->str() + ")";
253}
std::string str_ptr(T ptr)
Definition object.h:132

Member Data Documentation

◆ CC_NAMESPACE_SEPARATOR

std::string_view lsst::gauss2d::Object::CC_NAMESPACE_SEPARATOR = "::"
staticconstexprinherited

The C++ namespace separator.

Definition at line 45 of file object.h.

◆ NULL_STR_GENERAL

std::string_view lsst::gauss2d::Object::NULL_STR_GENERAL = "None"
staticconstexprinherited

Definition at line 46 of file object.h.

◆ PY_NAMESPACE_SEPARATOR

std::string_view lsst::gauss2d::Object::PY_NAMESPACE_SEPARATOR = "."
staticconstexprinherited

Definition at line 47 of file object.h.


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