33namespace trailedSources {
41) : _sigma(
data.getPsf()->computeShape(
data.getPsf()->getAveragePosition()).getTraceRadius()),
42 _bbox(
data.getBBox()),
43 _data(
data.getMaskedImage().getImage()->getArray()),
44 _variance(
data.getMaskedImage().getVariance()->getArray()) {}
48 double xc = params[0];
49 double yc = params[1];
50 double flux = params[2];
51 double length = params[3];
52 double theta = params[4];
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];
72 double xc = params[0];
73 double yc = params[1];
74 double flux = params[2];
75 double length = params[3];
76 double theta = params[4];
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];
87 for (
int k=0; k<5; ++k) {
88 gradChiSq[k] += gradModel[k] * gradDiff;
97 double xc = params[0];
98 double yc = params[1];
100 double length = params[3];
101 double theta = params[4];
108 for (
int yIndex = 0, yp = _bbox.
getBeginY(); yIndex < _bbox.
getHeight(); ++yIndex, ++yp) {
109 ImageF::Array::Reference dataRow = _data[yIndex];
110 for (
int xIndex = 0, xp = _bbox.
getBeginX(); xIndex < _bbox.
getWidth(); ++xIndex, ++xp) {
111 double data = dataRow[xIndex];
112 double model = _computeModel(xp, yp, xc, yc, 1.0, length, theta);
115 md += model*dataRow[xIndex];
116 for (
int k=0; k<5; ++k) {
117 gradmd[k] += gradModel[k] *
data;
118 gradmm[k] += gradModel[k] * model;
122 double flux = md / m2;
124 for (
int k=0; k<5; ++k) {
125 gradFlux[k] = (gradmd[k] - 2.0*flux*gradmm[k]) / m2;
133 double xc = params[0];
134 double yc = params[1];
135 double flux = params[2];
136 double length = params[3];
137 double theta = params[4];
142 for (
int yIndex = 0, yp = _bbox.
getBeginY(); yIndex < _bbox.
getHeight(); ++yIndex, ++yp) {
143 ImageF::Array::Reference
row = array[yIndex];
144 for (
int xIndex = 0, xp = _bbox.
getBeginX(); xIndex < _bbox.
getWidth(); ++xIndex, ++xp) {
145 row[xIndex] = _computeModel(xp,yp,xc,yc,flux,length,theta);
151double VeresModel::_computeModel(
double x,
double y,
double xc,
double yc,
152 double flux,
double length,
double theta)
const noexcept {
153 double xp = (
x-xc)*cos(theta) + (
y-yc)*sin(theta);
154 double yp = (
x-xc)*sin(theta) - (
y-yc)*cos(theta);
155 double A = exp(-0.5 * yp*yp / (_sigma*_sigma));
156 double B = erf((xp+length/2) / (sqrt(2.0) * _sigma));
157 double C = erf((xp-length/2) / (sqrt(2.0) * _sigma));
158 return flux * A * (B - C) / (length * 2 * sqrt(2.0 *
geom::PI) * _sigma);
162 double flux,
double length,
double theta)
const noexcept {
163 double xp = (
x-xc)*cos(theta) + (
y-yc)*sin(theta);
164 double yp = (
x-xc)*sin(theta) - (
y-yc)*cos(theta);
167 double flux2L = flux/(2.0*length);
169 double sqrt2 = sqrt(2.0);
170 double sqrt2Pi = sqrt(2.0*
geom::PI);
171 double sigmaSq = _sigma*_sigma;
172 double sigmaSq8 = sigmaSq * 8.0;
173 double eypSq = exp(-ypSq/(2.0*sigmaSq));
174 double lengthPlus = length+2.0*xp;
175 double lengthMinus= length-2.0*xp;
176 double erfPlus = erf(lengthPlus/(2.0*sqrt2*_sigma));
177 double erfMinus = erf(lengthMinus/(2.0*sqrt2*_sigma));
178 double expPlus = exp(-lengthPlus*lengthPlus/sigmaSq8);
181 double dfdxp = flux2L/(
geom::PI*sigmaSq)*exp(-4.0*ypSq/sigmaSq8)*expPlus*
182 (1.0 - exp(length*xp/sigmaSq));
183 double dfdyp = -flux2L*yp/(sqrt2Pi*_sigma*sigmaSq)*eypSq*(erfMinus+erfPlus);
186 double dxpdxc = -cos(theta);
187 double dxpdyc = -sin(theta);
188 double dxpdTheta = -yp;
189 double dypdxc = -sin(theta);
190 double dypdyc = cos(theta);
191 double dypdTheta = xp;
192 double dfdxc = dfdxp*dxpdxc + dfdyp*dypdxc;
193 double dfdyc = dfdxp*dxpdyc + dfdyp*dypdyc;
194 double dfdTheta = dfdxp*dxpdTheta + dfdyp*dypdTheta;
196 double dfdFlux = _computeModel(
x,
y,xc,yc,1.0,length,theta);
198 double dfdLength = flux2L/(length*sqrt2Pi*_sigma)*eypSq*(length/(sqrt2Pi*_sigma)*
199 (exp(-lengthMinus*lengthMinus/sigmaSq8)+expPlus) - erfMinus - erfPlus);
typename ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
int getBeginX() const noexcept
int getHeight() const noexcept
int getWidth() const noexcept
int getBeginY() const noexcept
VeresModel(ExposureF const &data)
Constructor for VeresModel.
std::vector< double > gradient(std::vector< double > const ¶ms) const
Compute the gradient of chi-squared of the model given the data.
afw::image::Image< float > ImageF
std::shared_ptr< ImageF > computeModelImage(std::vector< double > const ¶ms) const
Compute an image for a trail generated from the Veres model.
std::tuple< double, std::vector< double > > computeFluxWithGradient(std::vector< double > const ¶ms) const
Computes the flux and the gradient with respect to the other model parameters.
double operator()(std::vector< double > const ¶ms) const
Compute chi-squared of the model given the data.
double constexpr PI
The ratio of a circle's circumference to diameter.