lsst.meas.extensions.trailedSources  master-gf91b6b1793+0cae287e40
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
double x
int y
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
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)