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;
95double 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);
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);
111 double flux2L = flux/(2.0*
length);
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);
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);
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;
140 double dfdFlux = _computeModel(x,y,xc,yc,1.0,length,theta);
142 double dfdLength = flux2L/(
length*sqrt2Pi*_sigma)*eypSq*(length/(sqrt2Pi*_sigma)*
143 (exp(-lengthMinus*lengthMinus/sigmaSq8)+expPlus) - erfMinus - erfPlus);
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.
double operator()(std::vector< double > const ¶ms) const
Compute chi-squared of the model given the data.