lsst.meas.extensions.trailedSources g812f256287+a21825b06e
VeresModel.cc
Go to the documentation of this file.
1// -*- LSST-C++ -*-
2/*
3 * This file is part of meas_extensions_trailedSources.
4 *
5 * Developed for the LSST Data Management System.
6 * This product includes software developed by the LSST Project
7 * (https://www.lsst.org).
8 * See the COPYRIGHT file at the top-level directory of this distribution
9 * for details of code ownership.
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25#include "lsst/geom.h"
26#include "lsst/afw/image.h"
27#include "lsst/afw/detection.h"
29
30namespace lsst {
31namespace meas {
32namespace extensions {
33namespace trailedSources {
34
38
40 ExposureF const& data
41) : _sigma(data.getPsf()->computeShape().getTraceRadius()),
42 _bbox(data.getBBox()),
43 _data(data.getMaskedImage().getImage()->getArray()),
44 _variance(data.getMaskedImage().getVariance()->getArray()) {}
45
46double VeresModel::operator()(std::vector<double> const& params) const {
47
48 double xc = params[0]; // Centroid x
49 double yc = params[1]; // Centroid y
50 double flux = params[2]; // Flux
51 double length = params[3]; // Trail length
52 double theta = params[4]; // Angle from +x-axis
53
54 // Compute model image and chi-squared
55 double chiSq = 0.0;
56 // Loop is adapted from lsst::afw::detection::Psf::computeKernelImage()
57 for (int yIndex = 0, yp = _bbox.getBeginY(); yIndex < _bbox.getHeight(); ++yIndex, ++yp) {
58 ImageF::Array::Reference dataRow = _data[yIndex];
59 ImageF::Array::Reference varRow = _variance[yIndex];
60 for (int xIndex = 0, xp = _bbox.getBeginX(); xIndex < _bbox.getWidth(); ++xIndex, ++xp) {
61 double model = _computeModel(xp,yp,xc,yc,flux,length,theta);
62 double diff = dataRow[xIndex] - model;
63 chiSq += diff*diff/varRow[xIndex];
64 }
65 }
66
67 return chiSq;
68}
69
71
72 double xc = params[0]; // Centroid x
73 double yc = params[1]; // Centroid y
74 double flux = params[2]; // Flux
75 double length = params[3]; // Trail length
76 double theta = params[4]; // Angle from +x-axis
77
78 // Compute gradients of the model and of chi-squared
79 std::vector<double> gradChiSq = {0.0,0.0,0.0,0.0,0.0};
80 for (int yIndex = 0, yp = _bbox.getBeginY(); yIndex < _bbox.getHeight(); ++yIndex, ++yp) {
81 ImageF::Array::Reference dataRow = _data[yIndex];
82 ImageF::Array::Reference varRow = _variance[yIndex];
83 for (int xIndex = 0, xp = _bbox.getBeginX(); xIndex < _bbox.getWidth(); ++xIndex, ++xp) {
84 double model = _computeModel(xp,yp,xc,yc,flux,length,theta);
85 double gradDiff = -2.0 * (dataRow[xIndex] - model) / varRow[xIndex];
86 std::array<double, 5> gradModel = _computeGradient(xp,yp,xc,yc,flux,length,theta);
87 for (int k=0; k<5; ++k) {
88 gradChiSq[k] += gradModel[k] * gradDiff;
89 }
90 }
91 }
92 return gradChiSq;
93}
94
96 double xc = params[0]; // Centroid x
97 double yc = params[1]; // Centroid y
98 double flux = params[2]; // Flux
99 double length = params[3]; // Trail length
100 double theta = params[4]; // Angle from +x-axis
101
102 // Loop is adapted from lsst::afw::detection::GaussianPsf::doComputeKernelImage()
104 ImageF::Array array = image->getArray();
105 for (int yIndex = 0, yp = _bbox.getBeginY(); yIndex < _bbox.getHeight(); ++yIndex, ++yp) {
106 ImageF::Array::Reference row = array[yIndex];
107 for (int xIndex = 0, xp = _bbox.getBeginX(); xIndex < _bbox.getWidth(); ++xIndex, ++xp) {
108 row[xIndex] = _computeModel(xp,yp,xc,yc,flux,length,theta);
109 }
110 }
111 return image;
112}
113
114double VeresModel::_computeModel(double x, double y, double xc, double yc,
115 double flux, double length, double theta) const noexcept {
116 double xp = (x-xc)*cos(theta) + (y-yc)*sin(theta);
117 double yp = (x-xc)*sin(theta) - (y-yc)*cos(theta);
118 double A = exp(-0.5 * yp*yp / (_sigma*_sigma));
119 double B = erf((xp+length/2) / (sqrt(2.0) * _sigma));
120 double C = erf((xp-length/2) / (sqrt(2.0) * _sigma));
121 return flux * A * (B - C) / (length * 2 * sqrt(2.0 * geom::PI) * _sigma);
122}
123
124std::array<double, 5> VeresModel::_computeGradient(double x, double y, double xc, double yc,
125 double flux, double length, double theta) const noexcept {
126 double xp = (x-xc)*cos(theta) + (y-yc)*sin(theta);
127 double yp = (x-xc)*sin(theta) - (y-yc)*cos(theta);
128
129 // Duplicated quantities
130 double flux2L = flux/(2.0*length);
131 double ypSq = yp*yp;
132 double sqrt2 = sqrt(2.0);
133 double sqrt2Pi = sqrt(2.0*geom::PI);
134 double sigmaSq = _sigma*_sigma;
135 double sigmaSq8 = sigmaSq * 8.0;
136 double eypSq = exp(-ypSq/(2.0*sigmaSq));
137 double lengthPlus = length+2.0*xp;
138 double lengthMinus= length-2.0*xp;
139 double erfPlus = erf(lengthPlus/(2.0*sqrt2*_sigma));
140 double erfMinus = erf(lengthMinus/(2.0*sqrt2*_sigma));
141 double expPlus = exp(-lengthPlus*lengthPlus/sigmaSq8);
142
143 // Compute partials wrt the transformed coordinates
144 double dfdxp = flux2L/(geom::PI*sigmaSq)*exp(-4.0*ypSq/sigmaSq8)*expPlus*
145 (1.0 - exp(length*xp/sigmaSq));
146 double dfdyp = -flux2L*yp/(sqrt2Pi*_sigma*sigmaSq)*eypSq*(erfMinus+erfPlus);
147
148 // Use the chain rule to get partials wrt the centroid and rotation angle
149 double dxpdxc = -cos(theta);
150 double dxpdyc = -sin(theta);
151 double dxpdTheta = -yp;
152 double dypdxc = -sin(theta);
153 double dypdyc = cos(theta);
154 double dypdTheta = xp;
155 double dfdxc = dfdxp*dxpdxc + dfdyp*dypdxc;
156 double dfdyc = dfdxp*dxpdyc + dfdyp*dypdyc;
157 double dfdTheta = dfdxp*dxpdTheta + dfdyp*dypdTheta;
158
159 double dfdFlux = _computeModel(x,y,xc,yc,1.0,length,theta); // dfdFlux = f / flux
160
161 double dfdLength = flux2L/(length*sqrt2Pi*_sigma)*eypSq*(length/(sqrt2Pi*_sigma)*
162 (exp(-lengthMinus*lengthMinus/sigmaSq8)+expPlus) - erfMinus - erfPlus);
163
164 std::array<double, 5> gradModel = {dfdxc, dfdyc, dfdFlux, dfdLength, dfdTheta};
165 return gradModel;
166}
167
168}}}} // lsst::meas::extensions::trailedSources
char * data
double x
afw::table::Key< afw::table::Array< ImagePixelT > > image
int y
typename ndarray::Array< PixelT, 2, 1 > Array
int getBeginX() const noexcept
int getHeight() const noexcept
int getWidth() const noexcept
int getBeginY() const noexcept
VeresModel(ExposureF const &data)
Constructor for VeresModel.
Definition: VeresModel.cc:39
std::vector< double > gradient(std::vector< double > const &params) const
Compute the gradient of chi-squared of the model given the data.
Definition: VeresModel.cc:70
std::shared_ptr< ImageF > computeModelImage(std::vector< double > const &params) const
Compute an image for a trail generated from the Veres model.
Definition: VeresModel.cc:95
double operator()(std::vector< double > const &params) const
Compute chi-squared of the model given the data.
Definition: VeresModel.cc:46
T cos(T... args)
T erf(T... args)
T exp(T... args)
def length(self)
constexpr double PI
T sin(T... args)
T sqrt(T... args)