33namespace trailedSources {
41) : _sigma(
data.getPsf()->computeShape().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];
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];
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;
96 double xc = params[0];
97 double yc = params[1];
98 double flux = params[2];
100 double theta = params[4];
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);
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);
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);
130 double flux2L = flux/(2.0*
length);
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);
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);
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;
159 double dfdFlux = _computeModel(x,y,xc,yc,1.0,length,theta);
161 double dfdLength = flux2L/(
length*sqrt2Pi*_sigma)*eypSq*(length/(sqrt2Pi*_sigma)*
162 (exp(-lengthMinus*lengthMinus/sigmaSq8)+expPlus) - erfMinus - erfPlus);
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.
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.
double operator()(std::vector< double > const ¶ms) const
Compute chi-squared of the model given the data.