LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
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 
30 namespace lsst {
31 namespace meas {
32 namespace extensions {
33 namespace 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 
46 double 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 
95 double VeresModel::_computeModel(double x, double y, double xc, double yc,
96  double flux, double length, double theta) const noexcept {
97  double xp = (x-xc)*cos(theta) + (y-yc)*sin(theta);
98  double yp = (x-xc)*sin(theta) - (y-yc)*cos(theta);
99  double A = exp(-0.5 * yp*yp / (_sigma*_sigma));
100  double B = erf((xp+length/2) / (sqrt(2.0) * _sigma));
101  double C = erf((xp-length/2) / (sqrt(2.0) * _sigma));
102  return flux * A * (B - C) / (length * 2 * sqrt(2.0 * geom::PI) * _sigma);
103 }
104 
105 std::array<double, 5> VeresModel::_computeGradient(double x, double y, double xc, double yc,
106  double flux, double length, double theta) const noexcept {
107  double xp = (x-xc)*cos(theta) + (y-yc)*sin(theta);
108  double yp = (x-xc)*sin(theta) - (y-yc)*cos(theta);
109 
110  // Duplicated quantities
111  double flux2L = flux/(2.0*length);
112  double ypSq = yp*yp;
113  double sqrt2 = sqrt(2.0);
114  double sqrt2Pi = sqrt(2.0*geom::PI);
115  double sigmaSq = _sigma*_sigma;
116  double sigmaSq8 = sigmaSq * 8.0;
117  double eypSq = exp(-ypSq/(2.0*sigmaSq));
118  double lengthPlus = length+2.0*xp;
119  double lengthMinus= length-2.0*xp;
120  double erfPlus = erf(lengthPlus/(2.0*sqrt2*_sigma));
121  double erfMinus = erf(lengthMinus/(2.0*sqrt2*_sigma));
122  double expPlus = exp(-lengthPlus*lengthPlus/sigmaSq8);
123 
124  // Compute partials wrt the transformed coordinates
125  double dfdxp = flux2L/(geom::PI*sigmaSq)*exp(-4.0*ypSq/sigmaSq8)*expPlus*
126  (1.0 - exp(length*xp/sigmaSq));
127  double dfdyp = -flux2L*yp/(sqrt2Pi*_sigma*sigmaSq)*eypSq*(erfMinus+erfPlus);
128 
129  // Use the chain rule to get partials wrt the centroid and rotation angle
130  double dxpdxc = -cos(theta);
131  double dxpdyc = -sin(theta);
132  double dxpdTheta = -yp;
133  double dypdxc = -sin(theta);
134  double dypdyc = cos(theta);
135  double dypdTheta = xp;
136  double dfdxc = dfdxp*dxpdxc + dfdyp*dypdxc;
137  double dfdyc = dfdxp*dxpdyc + dfdyp*dypdyc;
138  double dfdTheta = dfdxp*dxpdTheta + dfdyp*dypdTheta;
139 
140  double dfdFlux = _computeModel(x,y,xc,yc,1.0,length,theta); // dfdFlux = f / flux
141 
142  double dfdLength = flux2L/(length*sqrt2Pi*_sigma)*eypSq*(length/(sqrt2Pi*_sigma)*
143  (exp(-lengthMinus*lengthMinus/sigmaSq8)+expPlus) - erfMinus - erfPlus);
144 
145  std::array<double, 5> gradModel = {dfdxc, dfdyc, dfdFlux, dfdLength, dfdTheta};
146  return gradModel;
147 }
148 
149 }}}} // lsst::meas::extensions::trailedSources
char * data
Definition: BaseRecord.cc:61
double x
int y
Definition: SpanSet.cc:48
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:51
int getBeginX() const noexcept
Definition: Box.h:172
int getHeight() const noexcept
Definition: Box.h:188
int getWidth() const noexcept
Definition: Box.h:187
int getBeginY() const noexcept
Definition: Box.h:173
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
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)
constexpr double PI
The ratio of a circle's circumference to diameter.
Definition: Angle.h:39
double sin(Angle const &a)
Definition: Angle.h:102
double cos(Angle const &a)
Definition: Angle.h:103
A base class for image defects.
T sin(T... args)
T sqrt(T... args)