LSST Applications g0d97872fb5+4fd969bb9d,g1653933729+34a971ddd9,g28da252d5a+072f89fe25,g2bbee38e9b+a99b0ab4cd,g2bc492864f+a99b0ab4cd,g2ca4be77d2+c0e3b27cd8,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+a99b0ab4cd,g35bb328faa+34a971ddd9,g3a166c0a6a+a99b0ab4cd,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9ed5ed841a,g569e0e2b34+cb4faa46ad,g5a97de2502+520531a62c,g717e5f8c0f+29153700a5,g7ede599f99+367733290c,g80478fca09+17051a22cc,g82479be7b0+f2f1ea0a87,g858d7b2824+29153700a5,g8b782ad322+29153700a5,g8cd86fa7b1+05420e7f7d,g9125e01d80+34a971ddd9,ga5288a1d22+e7f674aaf3,gae0086650b+34a971ddd9,gae74b0b5c6+45ef5cdc51,gb58c049af0+ace264a4f2,gc28159a63d+a99b0ab4cd,gcf0d15dbbd+8051a81198,gda6a2b7d83+8051a81198,gdaeeff99f8+7774323b41,gdf4d240d4a+34a971ddd9,ge2409df99d+cb167bac99,ge33fd446bb+29153700a5,ge79ae78c31+a99b0ab4cd,gf0baf85859+890af219f9,gf5289d68f6+9faa5c5784,w.2024.36
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
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 move(T... args)
@ loglike
Compute the log likelihood only.
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}
py::object result
Definition _schema.cc:429
afw::table::Key< double > sigma
An Ellipse with sigma_x, sigma_y, and rho values.
Definition ellipse.h:283
An Ellipse with r_major, axrat and angle values.
Definition ellipse.h:337
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

◆ 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 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 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

constexpr 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

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

Definition at line 46 of file object.h.

◆ PY_NAMESPACE_SEPARATOR

constexpr 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: