LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
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)