25#ifndef LSST_GAUSS2D_EVALUATE_H
26#define LSST_GAUSS2D_EVALUATE_H
42static const ConvolvedGaussians GAUSSIANS_NULL{std::nullopt};
84 const unsigned int dim_x,
const double xx_weight,
85 const double xy_weight) {
86 const double x_init = x_min - cen_x + bin_x / 2.;
87 vecdptr x = std::make_unique<vecd>(dim_x);
88 vecdptr xx = std::make_unique<vecd>(dim_x);
89 vecdptr x_norm = std::make_unique<vecd>(dim_x);
90 for (
unsigned int i = 0; i < dim_x; i++) {
91 double dist = x_init + i * bin_x;
93 (*xx)[i] = dist * dist * xx_weight;
94 (*x_norm)[i] = dist * xy_weight;
207 double xmc_weighted_i = 0,
vecdptr ymc_weighted_i =
nullptr,
double weight_xx_i = 0,
208 double xmc_sq_norm_i = 0,
vecdptr yy_weighted_i =
nullptr)
218 void set(
double weight_,
double weight_kernel_,
double xmc_,
double xx,
vecdptr ymc_weighted_,
220 this->weight = weight_;
221 this->weight_kernel = weight_kernel_;
223 this->weight_xx = xx;
224 this->ymc_weighted =
std::move(ymc_weighted_);
225 this->yy_weighted =
std::move(yy_weighted_);
249 double yy_weight_i = 0,
double rho_factor_i = 0,
double sig_x_inv_i = 0,
250 double sig_y_inv_i = 0,
double rho_xy_factor_i = 0,
251 double sig_x_src_div_conv_i = 0,
double sig_y_src_div_conv_i = 0,
252 double drho_c_dsig_x_src_i = 0,
double drho_c_dsig_y_src_i = 0,
253 double drho_c_drho_s_i = 0,
double xmc_t_xy_weight_i = 0)
276 const double sig_xy = sig_x * sig_y;
277 this->xx_weight = terms.
xx;
278 this->xy_weight = terms.
xy;
279 this->yy_weight = terms.
yy;
280 this->rho_factor = rho / (1. - rho * rho);
281 this->sig_x_inv = 1 / sig_x;
282 this->sig_y_inv = 1 / sig_y;
283 this->rho_xy_factor = 1. / (1. - rho * rho) / sig_xy;
301 double rho_src = ell_src.
get_rho();
304 double rho_psf = ell_psf.
get_rho();
306 this->sig_x_src_div_conv = sig_x_src / sig_x;
307 this->sig_y_src_div_conv = sig_y_src / sig_y;
309 double covar_psf_dsig_xy = rho_psf * sig_x_psf * sig_y_psf / sig_xy;
312 double sig_p_ratio = sig_x_psf / sig_x;
313 drho = rho_src * this->sig_y_src_div_conv * sig_p_ratio * sig_p_ratio / sig_x
314 - covar_psf_dsig_xy * this->sig_x_src_div_conv / sig_x;
316 this->drho_c_dsig_x_src = drho;
318 double sig_p_ratio = sig_y_psf / sig_y;
319 drho = rho_src * this->sig_x_src_div_conv * sig_p_ratio * sig_p_ratio / sig_y
320 - covar_psf_dsig_xy * this->sig_y_src_div_conv / sig_y;
322 this->drho_c_dsig_y_src = drho;
323 this->drho_c_drho_s = sig_x_src * sig_y_src / sig_xy;
329template <
typename t,
class Data,
class Indices>
334 : _param_map(param_map_in), _param_factor(param_factor_in), _output(output) {
335 if(output ==
nullptr) {
338 const auto n_extra_map_rows = param_map_in.
get_n_rows();
339 const auto n_extra_fac_rows = param_factor_in.
get_n_rows();
340 const auto n_extra_map_cols = param_map_in.
get_n_cols();
341 const auto n_extra_fac_cols = param_factor_in.
get_n_cols();
343 if (n_extra_map_rows != n_gauss) {
344 errmsg +=
"extra_param_map n_rows=" +
std::to_string(n_extra_map_rows)
347 if (n_extra_fac_rows != n_gauss) {
348 errmsg +=
"extra_param_factor n_rows=" +
std::to_string(n_extra_fac_rows)
352 errmsg +=
"extra_param_map n_cols=" +
std::to_string(n_extra_map_cols) +
" != 2. ";
355 errmsg +=
"extra_param_factor n_cols=" +
std::to_string(n_extra_fac_cols) +
" != 3. ";
364 inline void add(
size_t g,
size_t dim1,
size_t dim2,
const ValuesGauss& gradients) {
366 _g_check *= (g != 0);
367 const bool to_add = g == _param_map.
get_value(_g_check, 0);
368 const auto idx = _param_map.
get_value(g, 1);
369 double value = gradients.
L * _param_factor.
get_value(g, 0)
372 (*_output)[idx].add_value_unchecked(dim1, dim2, value);
378 bool is_kw = name_keywords;
380 type_name_str<GradientsExtra<t, Data, Indices>>(
false, namespace_separator) +
"("
381 + (is_kw ?
"param_map=" :
"") + _param_map.
repr(is_kw, namespace_separator) +
", "
382 + (is_kw ?
"param_factor=" :
"") + _param_factor.
repr(is_kw, namespace_separator) +
", "
383 + (is_kw ?
"output=" :
"") + _output->repr(is_kw, namespace_separator) +
", "
390 type_name_str<GradientsExtra<t, Data, Indices>>(
true) +
"("
391 +
"param_map=" + _param_map.
str() +
", "
392 +
"param_factor=" + _param_factor.
str() +
", "
393 +
"output=" + _output->str() +
", "
408 const double xx_weight,
const double yy_weight,
const double xy_weight) {
409 const unsigned int dim_x =
image.get_n_cols();
410 const unsigned int dim_y =
image.get_n_rows();
411 const auto coordsys =
image.get_coordsys();
412 const double x_min = coordsys.x_min;
413 const double y_min = coordsys.y_min;
414 const double bin_x = coordsys.get_dx1() /
image.get_n_cols();
415 const double bin_y = coordsys.get_dy2() /
image.get_n_rows();
417 const double weight_pix =
weight * bin_x * bin_y;
419 const auto moment_terms_y =
gaussian_pixel_x_xx(cen_y, y_min, bin_y, dim_y, yy_weight, xy_weight);
420 const vecd& y_weighted = *(moment_terms_y.x_norm);
421 const vecd& yy_weighted = *(moment_terms_y.xx);
423 double x = x_min - cen_x + bin_x / 2.;
425 for (
unsigned int i = 0; i < dim_x; i++) {
426 const double xx_weighted =
x *
x * xx_weight;
427 for (
unsigned int j = 0; j < dim_y; j++) {
428 image.set_value_unchecked(j, i,
429 weight_pix * exp(-(xx_weighted + yy_weighted[j] -
x * y_weighted[j])));
496 const double xmc_norm,
const double ymc_weighted,
const double xmc,
497 const double ymc,
const double norms_yy,
const double xmc_t_xy_factor,
498 const double sig_x_inv,
const double sig_y_inv,
const double xy_norm,
499 const double xx_norm,
const double yy_weighted,
500 const double rho_div_one_m_rhosq,
const double norms_xy_div_rho,
501 const double dsig_x_conv_src = 1,
const double dsig_y_conv_src = 1,
502 const double drho_c_dsig_x_src = 0,
503 const double drho_c_dsig_y_src = 0,
const double drho_c_drho_s = 1) {
504 out.
cen_x =
m * (2 * xmc_norm - ymc_weighted);
505 out.
cen_y =
m * (2 * ymc * norms_yy - xmc_t_xy_factor);
507 const double one_p_xy = 1. + xy_norm;
509 const double dfdrho_c =
m
510 * (rho_div_one_m_rhosq * (1 - 2 * (xx_norm + yy_weighted - xy_norm))
511 + xmc * ymc * norms_xy_div_rho);
512 out.
sigma_x = dfdrho_c * drho_c_dsig_x_src + dsig_x_conv_src * (
m * sig_x_inv * (2 * xx_norm - one_p_xy));
514 = dfdrho_c * drho_c_dsig_y_src + dsig_y_conv_src * (
m * sig_y_inv * (2 * yy_weighted - one_p_xy));
516 out.
rho = dfdrho_c * drho_c_drho_s;
522 double m_unweighted,
double xy_norm) {
534 const ValuesGauss& values,
const t weight_cen_x = 1,
535 const t weight_cen_y = 1,
const t weight_L = 1,
536 const t weight_sig_x = 1,
const t weight_sig_y = 1,
537 const t weight_rho = 1) {
538 cen_x += weight_cen_x * values.
cen_x;
539 cen_y += weight_cen_y * values.
cen_y;
540 L += weight_L * values.
L;
541 sig_x += weight_sig_x * values.
sigma_x;
542 sig_y += weight_sig_y * values.
sigma_y;
543 rho += weight_rho * values.
rho;
547template <
typename t,
class Data,
class Indices,
bool do_extra>
552 double data,
double sigma_inv,
unsigned int dim1,
unsigned int dim2,
const TermsPixelVec& terms_pixel,
556 double diff =
data - model;
557 chi_pix = sigma_inv * diff;
558 double diffvar = sigma_inv * chi_pix;
559 loglike -= diff * diffvar / 2.;
560 for (
size_t g = 0; g < N_GAUSS; ++g) {
561 const Weights& weights = gaussweights[g];
571 weights[0] * diffvar, weights[1] * diffvar, weights[2]);
572 gaussian_pixel_add_values<t>(
582 if constexpr (do_extra) {
583 double value = gradients.
L * extra_param_factor->get_value_unchecked(g, 0)
584 + gradients.
sigma_x * extra_param_factor->get_value_unchecked(g, 1)
585 + gradients.
sigma_y * extra_param_factor->get_value_unchecked(g, 2);
591template <
typename T,
class Data,
class Indices, GradientType gradient_type,
bool do_extra>
598 const TermsPixel& terms_pixel = terms_pixel_vec[g];
599 const double xy_norm = terms_pixel.
xmc * (*terms_pixel.
ymc_weighted)[j];
600 const double value_unweight
602 const double value =
weight * value_unweight;
605 Weights& output = gradweights[g];
607 output[1] = value_unweight;
611 sigma_inv * value, sigma_inv * value_unweight, xy_norm);
612 gaussian_pixel_add_values<T>(
623 if constexpr (do_extra) grad_extra->
add(g, j, i, gradients);
628template <
typename Indices>
631 size_t increment = 1) {
632 auto param_map = std::make_shared<Indices>(n_gaussians, n_params);
634 for (
size_t g = 0; g < n_gaussians; ++g) {
635 for (
size_t p = 0; p < n_params; ++p) {
636 param_map->set_value(g, p, index);
640 return std::const_pointer_cast<const Indices>(param_map);
643template <
typename Data>
647 auto param_factor = std::make_shared<Data>(n_gaussians, n_params);
648 for (
size_t g = 0; g < n_gaussians; ++g) {
649 for (
size_t p = 0; p < n_params; ++p) {
650 param_factor->set_value(g, p, value);
653 return std::const_pointer_cast<const Data>(param_factor);
667template <
typename T,
class Data,
class Indices>
710 : _gaussians(gaussians == nullptr ? GAUSSIANS_NULL : *gaussians),
711 _gaussians_ptr(gaussians == nullptr ? nullptr : gaussians),
712 _n_gaussians(_gaussians.size()),
713 _do_output(output != nullptr),
714 _is_sigma_image((sigma_inv != nullptr) && (sigma_inv->size() > 1)),
715 _do_residual(residual != nullptr),
716 _has_background(background != nullptr),
717 _gradienttype((grads != nullptr && grads->size() > 0)
718 ? (((grads->size() == 1) && ((*grads)[0].
get_n_rows() == 1))
723 extra_param_map != nullptr && extra_param_factor != nullptr
728 _get_likelihood((
data != nullptr) && (sigma_inv != nullptr)),
730 _sigma_inv(sigma_inv),
736 : ((grad_param_map != nullptr)
738 : detail::_param_map_default<Indices>(_gaussians.size()))),
742 : ((grad_param_factor != nullptr)
744 : detail::_param_factor_default<Data>(_gaussians.size()))),
747 : ((extra_param_map != nullptr)
749 : detail::_param_map_default<Indices>(_gaussians.size(),
753 : ((extra_param_factor != nullptr)
755 : detail::_param_factor_default<Data>(
757 _grad_extra(_do_extra ?
std::make_unique<detail::GradientsExtra<T, Data, Indices>>(
758 *extra_param_map, *extra_param_factor, _grads, _n_gaussians)
760 _grad_param_idx(_grad_param_map == nullptr ?
std::vector<size_t>{} : _get_grad_param_idx()),
771 _size(_n_cols * _n_rows),
778 if (_has_background && background->size() > 1)
780 if (!(_n_cols > 0 && _n_rows > 0))
783 if (!_do_extra && ((extra_param_map !=
nullptr) || (extra_param_factor !=
nullptr))) {
785 "Must pass all of extra_param_map, extra_param_factor and grads"
786 " to compute extra gradients");
788 if (_get_likelihood) {
790 if (_is_sigma_image) {
791 if (_n_cols != _sigma_inv->get_n_cols() || _n_rows != _sigma_inv->get_n_rows()) {
794 +
"] don't match inverse variance dimensions ["
798 if (_sigma_inv->get_coordsys() != _data->get_coordsys()) {
803 }
else if ((
data !=
nullptr) || (sigma_inv !=
nullptr)) {
808 const size_t n_gpm = _grad_param_map->get_n_rows();
809 const size_t n_gpf = _grad_param_factor->get_n_rows();
811 if ((n_gpm != _n_gaussians) || (n_gpf != _n_gaussians)) {
824 : (_grads->at(0).get_n_cols());
825 size_t idx_g_max = 0;
826 size_t idx_e_gauss_max = 0;
827 size_t idx_e_param_max = 0;
828 for (
size_t g = 0;
g < _n_gaussians; ++
g) {
830 auto value = _grad_param_map->get_value_unchecked(g, p);
831 if (value > idx_g_max) idx_g_max =
value;
833 auto value = _extra_param_map->get_value_unchecked(g, 0);
834 if (value > idx_e_gauss_max) idx_e_gauss_max =
value;
835 value = _extra_param_map->get_value_unchecked(g, 1);
836 if (value > idx_e_param_max) idx_e_param_max =
value;
838 if (!((idx_e_gauss_max < _n_gaussians) && (idx_e_param_max < n_grad))) {
844 if (!(idx_g_max < n_grad)) {
851 if (!_get_likelihood) {
853 "Can't compute likelihood gradient without computing likelihood;"
854 " did you pass data and sigma_inv arrays?");
860 for (
const auto& jac_img : *_grads) {
861 if (!(jac_img->get_n_rows() == _n_rows && jac_img->get_n_cols() == _n_cols)) {
864 +
"] don't match Jacobian matrix (grads["
901 ? loglike_gaussians_pixel_output<OutputType::overwrite>()
903 ? loglike_gaussians_pixel_output<OutputType::add>()
904 : loglike_gaussians_pixel_output<OutputType::none>());
911 auto is_kw = name_keywords;
912 auto name_sep = namespace_separator;
915 type_name_str<GaussianEvaluator>(
false, name_sep) +
"("
916 + (is_kw ?
"gaussians=" :
"") +
repr_ptr(_gaussians_ptr, is_kw, name_sep) +
", "
917 + (is_kw ?
"data=" :
"") +
repr_ptr(_data, is_kw, name_sep) +
", "
918 + (is_kw ?
"sigma_inv=" :
"") +
repr_ptr(_sigma_inv, is_kw, name_sep) +
", "
919 + (is_kw ?
"output=" :
"") +
repr_ptr(_output, is_kw, name_sep) +
", "
920 + (is_kw ?
"residual=" :
"") +
repr_ptr(_residual, is_kw, name_sep) +
", "
921 + (is_kw ?
"grads=" :
"") +
repr_ptr(_grads, is_kw, name_sep) +
", "
922 + (is_kw ?
"grad_param_map=" :
"") +
repr_ptr(_grad_param_map, is_kw, name_sep) +
", "
923 + (is_kw ?
"grad_param_factor=" :
"") +
repr_ptr(_grad_param_factor, is_kw, name_sep) + c
924 + (is_kw ?
"extra_param_map=" :
"") +
repr_ptr(_extra_param_map, is_kw, name_sep) +
", "
925 + (is_kw ?
"extra_param_factor=" :
"") +
repr_ptr(_extra_param_factor, is_kw, name_sep) + c
926 + (is_kw ?
"background=" :
"") +
repr_ptr(_background, is_kw, name_sep) +
")");
933 type_name_str<GaussianEvaluator>(
true) +
"("
934 +
"gaussians=" +
str_ptr(_gaussians_ptr) + c
940 +
"backgroundtype=" +
std::to_string(
static_cast<int>(_backgroundtype)) + c
943 +
"sigma_inv=" +
str_ptr(_sigma_inv) + c
944 +
"output=" +
str_ptr(_output) + c
945 +
"residual=" +
str_ptr(_residual) + c
946 +
"grads=" +
str_ptr(_grads) + c
947 +
"grad_param_map=" +
str_ptr(_grad_param_map) + c
948 +
"grad_param_factor=" +
str_ptr(_grad_param_factor) + c
949 +
"extra_param_map=" +
str_ptr(_extra_param_map) + c
950 +
"extra_param_factor=" +
str_ptr(_extra_param_factor) + c
951 +
"grad_extra=" +
str_ptr(_grad_extra ? _grad_extra.get() :
nullptr) + c
955 +
"coordsys=" + _coordsys.
str()
968 const size_t _n_gaussians;
970 const bool _do_output;
971 const bool _is_sigma_image;
972 const bool _do_residual;
973 const bool _has_background;
975 const bool _do_extra;
977 const bool _get_likelihood;
991 const size_t _n_cols;
992 const size_t _n_rows;
998 const Data _data_null_const{0, 0};
1000 const Indices _indices_null_const{0, 0};
1005 for (
size_t g = 0;
g < _n_gaussians; ++
g) {
1007 grad_param_idx_uniq.
insert(_grad_param_map->get_value(g, p));
1009 if (_do_extra) _grad_extra->add_index_to_set(g, grad_param_idx_uniq);
1012 return grad_param_idx;
1021 double gaussians_pixel_template() {
1026 double background_flat = 0;
1028 background_flat += _background->get_value_unchecked(0, 0);
1030 const size_t n_gaussians = _gaussians.
size();
1031 auto data_null = Data{0, 0};
1035 DataT * output_grad = is_loglike ? &(_grads->at(0)) : nullptr;
1037 size_t grad_param_idx_size = _grad_param_idx.
size();
1039 DataT& outputref = writeoutput ? (*_output) : data_null;
1040 DataT& residual_ref = do_residual ? (*_residual) : data_null;
1045 if (_n_cols != _output->get_n_cols() || _n_rows != _output->get_n_rows()) {
1048 +
"] don't match _output matrix dimensions [" +
std::to_string(_output->get_n_cols())
1054 const double x_min = coordsys.get_x_min();
1055 const double y_min = coordsys.get_y_min();
1056 const double bin_x = coordsys.get_dx1();
1057 const double bin_y = coordsys.get_dy2();
1058 const double bin_x_half = bin_x / 2.;
1062 const size_t ngaussgrad = n_gaussians * (do_gradient);
1068 auto* grad_extra_ptr = do_extra ? _grad_extra.
get() :
nullptr;
1070 for (
size_t g = 0;
g < n_gaussians; ++
g) {
1071 const auto&
src = _gaussians[
g].get_source();
1072 const auto& kernel = _gaussians[
g].get_kernel();
1073 const auto weight_src =
src.get_integral_value();
1074 const auto weight_kernel = kernel.get_integral_value();
1076 weights_conv[
g] = weight_src * weight_kernel;
1077 const double cen_x =
src.get_centroid_const().get_x();
1078 const double cen_y =
src.get_centroid_const().get_y();
1079 const auto ell_psf = kernel.get_ellipse_const();
1080 const auto ell_src =
src.get_ellipse_const();
1081 const Covariance cov_psf = Covariance(ell_psf);
1082 const Covariance cov_src = Covariance(ell_src);
1084 const auto cov_conv = cov_src.make_convolution(cov_psf);
1085 const auto ell_conv = Ellipse(*cov_conv);
1091 terms_pixel[
g].set(terms.weight, weight_kernel, x_min - cen_x + bin_x_half, terms.xx,
1094 terms_grad[
g].set(terms, ell_src, ell_psf, ell_conv,
std::move(yvals.x));
1098 + cov_psf.str() +
" due to: " + err.
what());
1105 double data_pix = 0;
1106 double sigma_inv_pix = 0;
1108 detail::ValuesGauss gradients = {0, 0, 0, 0, 0, 0};
1110 for (
unsigned int i = 0; i < _n_cols; i++) {
1111 for (
size_t g = 0;
g < n_gaussians; ++
g) {
1112 terms_pixel[
g].xmc_weighted = terms_pixel[
g].xmc * terms_pixel[
g].weight_xx;
1113 terms_pixel[
g].xmc_sq_norm = terms_pixel[
g].xmc_weighted * terms_pixel[
g].xmc;
1115 terms_grad[
g].xmc_t_xy_weight = terms_pixel[
g].xmc * terms_grad[
g].xy_weight;
1118 for (
unsigned int j = 0; j < _n_rows; j++) {
1119 model = background_flat;
1121 for (
size_t k = 0; k < grad_param_idx_size; ++k) {
1122 output_jac_ref[_grad_param_idx[k]].set_value_unchecked(j, i, 0);
1126 data_pix = data_ref.get_value_unchecked(j, i);
1128 = sigma_inv_ref.get_value_unchecked(j * _is_sigma_image, i * _is_sigma_image);
1130 for (
size_t g = 0;
g < n_gaussians; ++
g) {
1131 model += detail::gaussian_pixel_add_all<T, Data, Indices, gradient_type, do_extra>(
1132 g, j, i, weights_conv[g], sigma_inv_pix, terms_pixel, output_jac_ref,
1133 grad_param_map_ref, grad_param_factor_ref, weights_grad, terms_grad, gradients,
1137 outputref.set_value_unchecked(j, i, model);
1139 outputref.add_value_unchecked(j, i, model);
1141 if constexpr (getlikelihood) {
1142 static_assert(getlikelihood);
1145 chi_pix = (data_pix -
model) * sigma_inv_pix;
1146 loglike -= chi_pix * chi_pix / 2.;
1150 detail::gaussians_pixel_add_like_grad<T, Data, Indices, do_extra>(
1151 output_grad, grad_param_map_ref, grad_param_factor_ref, n_gaussians,
1152 weights_grad, chi_pix,
loglike, model, data_pix, sigma_inv_pix, j, i,
1153 terms_pixel, terms_grad, gradients, _extra_param_map, _extra_param_factor);
1156 if constexpr (do_residual) residual_ref.set_value_unchecked(j, i, chi_pix);
1158 for (
size_t g = 0;
g < n_gaussians; ++
g) terms_pixel[g].xmc += bin_x;
1166 double loglike_gaussians_pixel_getlike() {
1173 ? gaussians_pixel_template<output_type, get_likelihood, background_type, do_residual,
1176 ? gaussians_pixel_template<output_type, get_likelihood, background_type,
1178 : gaussians_pixel_template<output_type, get_likelihood, background_type,
1183 template <OutputType output_type,
bool get_likelihood, BackgroundType background_type>
1184 double loglike_gaussians_pixel_extra() {
1189 return _do_residual ? (_do_extra ? loglike_gaussians_pixel_getlike<output_type, get_likelihood,
1190 background_type,
true,
true>()
1191 : loglike_gaussians_pixel_getlike<output_type, get_likelihood,
1192 background_type, true, false>())
1193 : (_do_extra ? loglike_gaussians_pixel_getlike<output_type, get_likelihood,
1194 background_type, false, true>()
1195 : loglike_gaussians_pixel_getlike<output_type, get_likelihood,
1196 background_type, false, false>());
1200 template <OutputType output_type>
1201 double loglike_gaussians_pixel_output() {
1206 return _get_likelihood
1208 ? loglike_gaussians_pixel_extra<output_type,
true,
1211 : (_has_background ? loglike_gaussians_pixel_extra<output_type, false,
1213 : loglike_gaussians_pixel_extra<output_type, false,
1232template <
typename T,
class Data,
class Indices>
1235 const unsigned int n_rows = 0,
const unsigned int n_cols = 0,
1237 bool to_add =
false) {
1238 if (output ==
nullptr) {
1242 output = std::make_shared<Data>(n_rows, n_cols,
nullptr, coordsys);
1245 = std::make_unique<GaussianEvaluator<T, Data, Indices>>(gaussians,
nullptr,
nullptr, output);
1246 evaluator->loglike_pixel(to_add);
std::shared_ptr< RecordT > src
T back_inserter(T... args)
A collection of ConvolvedGaussian objects.
A coordinate system specifying image scale and orientation.
std::string str() const override
Return a brief, human-readable string representation of this.
An Ellipse with sigma_x, sigma_y, and rho values.
double get_rho() const override
Get rho.
double get_sigma_x() const override
Get sigma_x.
double get_sigma_y() const override
Get sigma_y.
A class that evaluates 2D Gaussians and renders them in images.
~GaussianEvaluator() override=default
Image< idx_type, Indices > IndicesT
ImageArray< T, Data > const & IMAGEARRAY_NULL_CONST() const
Indices const & INDICES_NULL_CONST() const
Data const & IMAGE_NULL_CONST() const
size_t get_n_rows() const
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.
GaussianEvaluator(int x=0, const std::shared_ptr< const ConvolvedGaussians > gaussians=nullptr)
size_t get_n_cols() const
ImageArray< T, Data > ImageArrayT
const CoordinateSystem & get_coordsys() const
double loglike_pixel(bool to_add=false)
Compute the model and/or log-likelihood and/or gradient (d(log-likelihood)/dx) and/or Jacobian (dmode...
GaussianEvaluator(const std::shared_ptr< const ConvolvedGaussians > gaussians, const std::shared_ptr< const Image< T, Data > > data=nullptr, const std::shared_ptr< const Image< T, Data > > sigma_inv=nullptr, const std::shared_ptr< Image< T, Data > > output=nullptr, const std::shared_ptr< Image< T, Data > > residual=nullptr, const std::shared_ptr< ImageArrayT > grads=nullptr, const std::shared_ptr< const Image< idx_type, Indices > > grad_param_map=nullptr, const std::shared_ptr< const Image< T, Data > > grad_param_factor=nullptr, const std::shared_ptr< const Image< idx_type, Indices > > extra_param_map=nullptr, const std::shared_ptr< const Image< T, Data > > extra_param_factor=nullptr, const std::shared_ptr< const Image< T, Data > > background=nullptr)
Construct a GaussianEvaluator, inferring outputs from inputs.
An array of compatible Images.
A 2D image with scalar numeric values, using CRTP.
std::string str() const override
Return a brief, human-readable string representation of this.
T get_value_unchecked(size_t row, size_t col) const
T get_value(size_t row, size_t col) const
size_t get_n_rows() const
size_t get_n_cols() const
std::string repr(bool name_keywords, std::string_view namespace_separator) const override
Return a full, callable string representation of this.
T & _get_value_unchecked(size_t row, size_t col)
A generic object from the gauss2d library.
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
void set(const Terms &terms, const Ellipse &ell_src, const Ellipse &ell_psf, const Ellipse &ell, vecdptr ymc_)
TermsGradient(vecdptr ymc_i=nullptr, double xx_weight_i=0, double xy_weight_i=0, double yy_weight_i=0, double rho_factor_i=0, double sig_x_inv_i=0, double sig_y_inv_i=0, double rho_xy_factor_i=0, double sig_x_src_div_conv_i=0, double sig_y_src_div_conv_i=0, double drho_c_dsig_x_src_i=0, double drho_c_dsig_y_src_i=0, double drho_c_drho_s_i=0, double xmc_t_xy_weight_i=0)
double sig_x_src_div_conv
double sig_y_src_div_conv
Storage for terms common to Gaussians for a single pixel.
TermsPixel(double weight_i=0, double weight_kernel=0, double xmc_i=0, double xmc_weighted_i=0, vecdptr ymc_weighted_i=nullptr, double weight_xx_i=0, double xmc_sq_norm_i=0, vecdptr yy_weighted_i=nullptr)
void set(double weight_, double weight_kernel_, double xmc_, double xx, vecdptr ymc_weighted_, vecdptr yy_weighted_)
void gaussians_pixel_add_like_grad(Image< t, Data > *output, const Image< idx_type, Indices > &grad_param_map, const Image< t, Data > &grad_param_factor, const size_t N_GAUSS, const std::vector< Weights > &gaussweights, double &chi_pix, double &loglike, const double model, double data, double sigma_inv, unsigned int dim1, unsigned int dim2, const TermsPixelVec &terms_pixel, const TermsGradientVec &terms_grad, ValuesGauss &gradients, const std::shared_ptr< const Image< idx_type, Indices > > extra_param_map, const std::shared_ptr< const Image< t, Data > > extra_param_factor)
std::array< double, N_PARAMS_GAUSS2D > Weights
std::vector< double > vecd
const std::shared_ptr< const Data > _param_factor_default(size_t n_gaussians, size_t n_params=N_PARAMS_GAUSS2D, double value=1.)
std::vector< TermsPixel > TermsPixelVec
void gaussian_pixel_get_jacobian_from_terms(ValuesGauss &out, const size_t dim1, const TermsPixel &terms_pixel, const TermsGradient &terms_grad, double m, double m_unweighted, double xy_norm)
Terms terms_from_covar(double weight, const Ellipse &ell)
const std::shared_ptr< const Indices > _param_map_default(size_t n_gaussians, size_t n_params=N_PARAMS_GAUSS2D, size_t increment=1)
std::unique_ptr< vecd > vecdptr
T gaussian_pixel_add_all(size_t g, size_t j, size_t i, double weight, double sigma_inv, const TermsPixelVec &terms_pixel_vec, ImageArray< T, Data > &output_jac_ref, const Image< idx_type, Indices > &grad_param_map, const Image< T, Data > &grad_param_factor, std::vector< Weights > &gradweights, const TermsGradientVec &terms_grad_vec, ValuesGauss &gradients, GradientsExtra< T, Data, Indices > *grad_extra)
void gaussian_pixel(Data &image, const double weight, const double cen_x, const double cen_y, const double xx_weight, const double yy_weight, const double xy_weight)
TermsMoments gaussian_pixel_x_xx(const double cen_x, const double x_min, const double bin_x, const unsigned int dim_x, const double xx_weight, const double xy_weight)
std::vector< TermsGradient > TermsGradientVec
void gaussian_pixel_get_jacobian(ValuesGauss &out, const double m, const double m_unweight, const double xmc_norm, const double ymc_weighted, const double xmc, const double ymc, const double norms_yy, const double xmc_t_xy_factor, const double sig_x_inv, const double sig_y_inv, const double xy_norm, const double xx_norm, const double yy_weighted, const double rho_div_one_m_rhosq, const double norms_xy_div_rho, const double dsig_x_conv_src=1, const double dsig_y_conv_src=1, const double drho_c_dsig_x_src=0, const double drho_c_dsig_y_src=0, const double drho_c_drho_s=1)
void gaussian_pixel_add_values(t &cen_x, t &cen_y, t &L, t &sig_x, t &sig_y, t &rho, const ValuesGauss &values, const t weight_cen_x=1, const t weight_cen_y=1, const t weight_L=1, const t weight_sig_x=1, const t weight_sig_y=1, const t weight_rho=1)
std::string str_ptr(T ptr)
std::string repr_ptr(T ptr, bool name_keywords, std::string_view namespace_separator)
std::shared_ptr< Data > make_gaussians_pixel(const std::shared_ptr< const ConvolvedGaussians > gaussians, std::shared_ptr< Data > output=nullptr, const unsigned int n_rows=0, const unsigned int n_cols=0, const std::shared_ptr< const CoordinateSystem > coordsys=nullptr, bool to_add=false)
Add gaussians to an image, creating one if needed.
const size_t N_EXTRA_FACTOR
const size_t N_PARAMS_GAUSS2D
std::string to_string_iter(const Container< Value > &container)
model(data, psfmodels, sources)
afw::table::Key< double > weight