LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
SdssCentroid.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2013 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #include <iostream>
24 #include <cmath>
25 #include <numeric>
26 #include "ndarray/eigen.h"
27 #include "lsst/afw/detection/Psf.h"
28 #include "lsst/pex/exceptions.h"
31 #include "lsst/afw/geom/Angle.h"
32 #include "lsst/afw/table/Source.h"
34 
35 
36 namespace lsst { namespace meas { namespace base {
37 
38 namespace {
39 
40 /************************************************************************************************************/
41 
42 float const AMPAST4 = 1.33; // amplitude of `4th order' corr compared to theory
43 
44 /*
45  * Do the Gaussian quartic interpolation for the position
46  * of the maximum for three equally spaced values vm,v0,vp, assumed to be at
47  * abscissae -1,0,1; the answer is returned as *cen
48  *
49  * Return 0 is all is well, otherwise 1
50  */
51 static int inter4(float vm, float v0, float vp, float *cen, bool negative=false) {
52  float const sp = v0 - vp;
53  float const sm = v0 - vm;
54  float const d2 = sp + sm;
55  float const s = 0.5*(vp - vm);
56 
57  if ((!negative && (d2 <= 0.0f || v0 <= 0.0f)) ||
58  ( negative && (d2 >= 0.0f || v0 >= 0.0f))) {
59  return(1);
60  }
61 
62  *cen = s/d2*(1.0 + AMPAST4*sp*sm/(d2*v0));
63 
64  return fabs(*cen) < 1 ? 0 : 1;
65 }
66 
67 /*****************************************************************************/
68 /*
69  * Calculate error in centroid
70  */
71 float astrom_errors(float skyVar, // variance of pixels at the sky level
72  float sourceVar, // variance in peak due to excess counts over sky
73  float A, // abs(peak value in raw image)
74  float tau2, // Object is N(0,tau2)
75  float As, // abs(peak value in smoothed image)
76  float s, // slope across central pixel
77  float d, // curvature at central pixel
78  float sigma, // width of smoothing filter
79  int quarticBad) { // was quartic estimate bad?
80 
81  float const k = quarticBad ? 0 : AMPAST4; /* quartic correction coeff */
82  float const sigma2 = sigma*sigma; /* == sigma^2 */
83  float sVar, dVar; /* variances of s and d */
84  float xVar; /* variance of centroid, x */
85 
86  if (fabs(As) < std::numeric_limits<float>::min() ||
87  fabs(d) < std::numeric_limits<float>::min()) {
88  return(1e3);
89  }
90 
91  if (sigma <= 0) { /* no smoothing; no covariance */
92  sVar = 0.5*skyVar; /* due to sky */
93  dVar = 6*skyVar;
94 
95  sVar += 0.5*sourceVar*exp(-1/(2*tau2));
96  dVar += sourceVar*(4*exp(-1/(2*tau2)) + 2*exp(-1/(2*tau2)));
97  } else { /* smoothed */
98  sVar = skyVar/(8*lsst::afw::geom::PI*sigma2)*(1 - exp(-1/sigma2));
99  dVar = skyVar/(2*lsst::afw::geom::PI*sigma2)*(3 - 4*exp(-1/(4*sigma2)) + exp(-1/sigma2));
100 
101  sVar += sourceVar/(12*lsst::afw::geom::PI*sigma2)*(exp(-1/(3*sigma2)) - exp(-1/sigma2));
102  dVar += sourceVar/(3*lsst::afw::geom::PI*sigma2)*(2 - 3*exp(-1/(3*sigma2)) + exp(-1/sigma2));
103  }
104 
105  xVar = sVar*pow(1/d + k/(4*As)*(1 - 12*s*s/(d*d)), 2) +
106  dVar*pow(s/(d*d) - k/(4*As)*8*s*s/(d*d*d), 2);
107 
108  return(xVar >= 0 ? sqrt(xVar) : NAN);
109 }
110 
111 /************************************************************************************************************/
112 /*
113  * Estimate the position of an object, assuming we know that it's approximately the size of the PSF
114  */
115 
116 template<typename ImageXy_locatorT, typename VarImageXy_locatorT>
117 void doMeasureCentroidImpl(double *xCenter, // output; x-position of object
118  double *dxc, // output; error in xCenter
119  double *yCenter, // output; y-position of object
120  double *dyc, // output; error in yCenter
121  double *sizeX2, double *sizeY2, // output; object widths^2 in x and y directions
122  ImageXy_locatorT im, // Locator for the pixel values
123  VarImageXy_locatorT vim, // Locator for the image containing the variance
124  double smoothingSigma, // Gaussian sigma of already-applied smoothing filter
125  FlagHandler flagHandler
126  )
127 {
128  /*
129  * find a first quadratic estimate
130  */
131  double const d2x = 2*im(0, 0) - im(-1, 0) - im(1, 0);
132  double const d2y = 2*im(0, 0) - im( 0, -1) - im(0, 1);
133  double const sx = 0.5*(im(1, 0) - im(-1, 0));
134  double const sy = 0.5*(im(0, 1) - im( 0, -1));
135 
136  if (d2x == 0.0 || d2y == 0.0) {
137  throw LSST_EXCEPT(
138  MeasurementError,
139  flagHandler.getDefinition(SdssCentroidAlgorithm::NO_SECOND_DERIVATIVE).doc,
141  );
142  }
143  if (d2x < 0.0 || d2y < 0.0) {
144  throw LSST_EXCEPT(
145  MeasurementError,
146  flagHandler.getDefinition(SdssCentroidAlgorithm::NOT_AT_MAXIMUM).doc +
147  (boost::format(": d2I/dx2, d2I/dy2 = %g %g") % d2x % d2y).str(),
149  );
150  }
151 
152  double const dx0 = sx/d2x;
153  double const dy0 = sy/d2y; // first guess
154 
155  if (fabs(dx0) > 10.0 || fabs(dy0) > 10.0) {
156  throw LSST_EXCEPT(
157  MeasurementError,
158  flagHandler.getDefinition(SdssCentroidAlgorithm::ALMOST_NO_SECOND_DERIVATIVE).doc +
159  (boost::format(": sx, d2x, sy, d2y = %f %f %f %f") % sx % d2x % sy % d2y).str(),
161  );
162  }
163 
164  double vpk = im(0, 0) + 0.5*(sx*dx0 + sy*dy0); // height of peak in image
165  if (vpk < 0) {
166  vpk = -vpk;
167  }
168 /*
169  * now evaluate maxima on stripes
170  */
171  float m0x = 0, m1x = 0, m2x = 0;
172  float m0y = 0, m1y = 0, m2y = 0;
173 
174  int quarticBad = 0;
175  quarticBad += inter4(im(-1, -1), im( 0, -1), im( 1, -1), &m0x);
176  quarticBad += inter4(im(-1, 0), im( 0, 0), im( 1, 0), &m1x);
177  quarticBad += inter4(im(-1, 1), im( 0, 1), im( 1, 1), &m2x);
178 
179  quarticBad += inter4(im(-1, -1), im(-1, 0), im(-1, 1), &m0y);
180  quarticBad += inter4(im( 0, -1), im( 0, 0), im( 0, 1), &m1y);
181  quarticBad += inter4(im( 1, -1), im( 1, 0), im( 1, 1), &m2y);
182 
183  double xc, yc; // position of maximum
184  double sigmaX2, sigmaY2; // widths^2 in x and y of smoothed object
185 
186  if (quarticBad) { // >= 1 quartic interpolator is bad
187  xc = dx0;
188  yc = dy0;
189  sigmaX2 = vpk/d2x; // widths^2 in x
190  sigmaY2 = vpk/d2y; // and y
191  } else {
192  double const smx = 0.5*(m2x - m0x);
193  double const smy = 0.5*(m2y - m0y);
194  double const dm2x = m1x - 0.5*(m0x + m2x);
195  double const dm2y = m1y - 0.5*(m0y + m2y);
196  double const dx = m1x + dy0*(smx - dy0*dm2x); // first quartic approx
197  double const dy = m1y + dx0*(smy - dx0*dm2y);
198  double const dx4 = m1x + dy*(smx - dy*dm2x); // second quartic approx
199  double const dy4 = m1y + dx*(smy - dx*dm2y);
200 
201  xc = dx4;
202  yc = dy4;
203  sigmaX2 = vpk/d2x - (1 + 6*dx0*dx0)/4; // widths^2 in x
204  sigmaY2 = vpk/d2y - (1 + 6*dy0*dy0)/4; // and y
205  }
206  /*
207  * Now for the errors.
208  */
209  float tauX2 = sigmaX2; // width^2 of _un_ smoothed object
210  float tauY2 = sigmaY2;
211  tauX2 -= smoothingSigma*smoothingSigma; // correct for smoothing
212  tauY2 -= smoothingSigma*smoothingSigma;
213 
214  if (tauX2 <= smoothingSigma*smoothingSigma) { // problem; sigmaX2 must be bad
215  tauX2 = smoothingSigma*smoothingSigma;
216  }
217  if (tauY2 <= smoothingSigma*smoothingSigma) { // sigmaY2 must be bad
218  tauY2 = smoothingSigma*smoothingSigma;
219  }
220 
221  float const skyVar = (vim(-1, -1) + vim( 0, -1) + vim( 1, -1) +
222  vim(-1, 0) + vim( 1, 0) +
223  vim(-1, 1) + vim( 0, 1) + vim( 1, 1))/8.0; // Variance in sky
224  float const sourceVar = vim(0, 0); // extra variance of peak due to its photons
225  float const A = vpk*sqrt((sigmaX2/tauX2)*(sigmaY2/tauY2)); // peak of Unsmoothed object
226 
227  *xCenter = xc;
228  *yCenter = yc;
229 
230  *dxc = astrom_errors(skyVar, sourceVar, A, tauX2, vpk, sx, d2x, fabs(smoothingSigma), quarticBad);
231  *dyc = astrom_errors(skyVar, sourceVar, A, tauY2, vpk, sy, d2y, fabs(smoothingSigma), quarticBad);
232 
233  *sizeX2 = tauX2; // return the estimates of the (object size)^2
234  *sizeY2 = tauY2;
235 }
236 
237 template<typename MaskedImageXy_locatorT>
238 void doMeasureCentroidImpl(double *xCenter, // output; x-position of object
239  double *dxc, // output; error in xCenter
240  double *yCenter, // output; y-position of object
241  double *dyc, // output; error in yCenter
242  double *sizeX2, double *sizeY2, // output; object widths^2 in x and y directions
243  double *peakVal, // output; peak of object
244  MaskedImageXy_locatorT mim, // Locator for the pixel values
245  double smoothingSigma, // Gaussian sigma of already-applied smoothing filter
246  bool negative,
247  FlagHandler flagHandler
248  )
249 {
250  /*
251  * find a first quadratic estimate
252  */
253  double const d2x = 2*mim.image(0, 0) - mim.image(-1, 0) - mim.image(1, 0);
254  double const d2y = 2*mim.image(0, 0) - mim.image( 0, -1) - mim.image(0, 1);
255  double const sx = 0.5*(mim.image(1, 0) - mim.image(-1, 0));
256  double const sy = 0.5*(mim.image(0, 1) - mim.image( 0, -1));
257 
258  if (d2x == 0.0 || d2y == 0.0) {
259  throw LSST_EXCEPT(
260  MeasurementError,
261  flagHandler.getDefinition(SdssCentroidAlgorithm::NO_SECOND_DERIVATIVE).doc,
263  );
264  }
265  if ((!negative && (d2x < 0.0 || d2y < 0.0)) ||
266  ( negative && (d2x > 0.0 || d2y > 0.0))) {
267  throw LSST_EXCEPT(
268  MeasurementError,
269  flagHandler.getDefinition(SdssCentroidAlgorithm::NOT_AT_MAXIMUM).doc +
270  (boost::format(": d2I/dx2, d2I/dy2 = %g %g") % d2x % d2y).str(),
272  );
273  }
274 
275  double const dx0 = sx/d2x;
276  double const dy0 = sy/d2y; // first guess
277 
278  if (fabs(dx0) > 10.0 || fabs(dy0) > 10.0) {
279  throw LSST_EXCEPT(
280  MeasurementError,
281  flagHandler.getDefinition(SdssCentroidAlgorithm::NO_SECOND_DERIVATIVE).doc +
282  (boost::format(": sx, d2x, sy, d2y = %f %f %f %f") % sx % d2x % sy % d2y).str(),
284  );
285  }
286 
287  double vpk = mim.image(0, 0) + 0.5*(sx*dx0 + sy*dy0); // height of peak in image
288  //if (vpk < 0) {
289  // vpk = -vpk;
290  //}
291 /*
292  * now evaluate maxima on stripes
293  */
294  float m0x = 0, m1x = 0, m2x = 0;
295  float m0y = 0, m1y = 0, m2y = 0;
296 
297  int quarticBad = 0;
298  quarticBad += inter4(mim.image(-1, -1), mim.image( 0, -1), mim.image( 1, -1), &m0x, negative);
299  quarticBad += inter4(mim.image(-1, 0), mim.image( 0, 0), mim.image( 1, 0), &m1x, negative);
300  quarticBad += inter4(mim.image(-1, 1), mim.image( 0, 1), mim.image( 1, 1), &m2x, negative);
301 
302  quarticBad += inter4(mim.image(-1, -1), mim.image(-1, 0), mim.image(-1, 1), &m0y, negative);
303  quarticBad += inter4(mim.image( 0, -1), mim.image( 0, 0), mim.image( 0, 1), &m1y, negative);
304  quarticBad += inter4(mim.image( 1, -1), mim.image( 1, 0), mim.image( 1, 1), &m2y, negative);
305 
306  double xc, yc; // position of maximum
307  double sigmaX2, sigmaY2; // widths^2 in x and y of smoothed object
308 
309  if (quarticBad) { // >= 1 quartic interpolator is bad
310  xc = dx0;
311  yc = dy0;
312  sigmaX2 = vpk/d2x; // widths^2 in x
313  sigmaY2 = vpk/d2y; // and y
314  } else {
315  double const smx = 0.5*(m2x - m0x);
316  double const smy = 0.5*(m2y - m0y);
317  double const dm2x = m1x - 0.5*(m0x + m2x);
318  double const dm2y = m1y - 0.5*(m0y + m2y);
319  double const dx = m1x + dy0*(smx - dy0*dm2x); // first quartic approx
320  double const dy = m1y + dx0*(smy - dx0*dm2y);
321  double const dx4 = m1x + dy*(smx - dy*dm2x); // second quartic approx
322  double const dy4 = m1y + dx*(smy - dx*dm2y);
323 
324  xc = dx4;
325  yc = dy4;
326  sigmaX2 = vpk/d2x - (1 + 6*dx0*dx0)/4; // widths^2 in x
327  sigmaY2 = vpk/d2y - (1 + 6*dy0*dy0)/4; // and y
328  }
329  /*
330  * Now for the errors.
331  */
332  float tauX2 = sigmaX2; // width^2 of _un_ smoothed object
333  float tauY2 = sigmaY2;
334  tauX2 -= smoothingSigma*smoothingSigma; // correct for smoothing
335  tauY2 -= smoothingSigma*smoothingSigma;
336 
337  if (tauX2 <= smoothingSigma*smoothingSigma) { // problem; sigmaX2 must be bad
338  tauX2 = smoothingSigma*smoothingSigma;
339  }
340  if (tauY2 <= smoothingSigma*smoothingSigma) { // sigmaY2 must be bad
341  tauY2 = smoothingSigma*smoothingSigma;
342  }
343 
344  float const skyVar = (mim.variance(-1, -1) + mim.variance( 0, -1) + mim.variance( 1, -1) +
345  mim.variance(-1, 0) + mim.variance( 1, 0) +
346  mim.variance(-1, 1) + mim.variance( 0, 1) + mim.variance( 1, 1)
347  )/8.0; // Variance in sky
348  float const sourceVar = mim.variance(0, 0); // extra variance of peak due to its photons
349  float const A = vpk*sqrt((sigmaX2/tauX2)*(sigmaY2/tauY2)); // peak of Unsmoothed object
350 
351  *xCenter = xc;
352  *yCenter = yc;
353 
354  *dxc = astrom_errors(skyVar, sourceVar, A, tauX2, vpk, sx, d2x, fabs(smoothingSigma), quarticBad);
355  *dyc = astrom_errors(skyVar, sourceVar, A, tauY2, vpk, sy, d2y, fabs(smoothingSigma), quarticBad);
356 
357  *sizeX2 = tauX2; // return the estimates of the (object size)^2
358  *sizeY2 = tauY2;
359 
360  *peakVal = vpk;
361 }
362 
363 template<typename MaskedImageT>
364 std::pair<MaskedImageT, double>
365 smoothAndBinImage(CONST_PTR(lsst::afw::detection::Psf) psf,
366  int const x, const int y,
367  MaskedImageT const& mimage,
368  int binX, int binY,
369  FlagHandler _flagHandler)
370 {
371  lsst::afw::geom::Point2D const center(x + mimage.getX0(), y + mimage.getY0());
372  lsst::afw::geom::ellipses::Quadrupole const& shape = psf->computeShape(center);
373  double const smoothingSigma = shape.getDeterminantRadius();
374 #if 0
375  double const nEffective = psf->computeEffectiveArea(); // not implemented yet (#2821)
376 #else
377  double const nEffective = 4*M_PI*smoothingSigma*smoothingSigma; // correct for a Gaussian
378 #endif
379 
381  int const kWidth = kernel->getWidth();
382  int const kHeight = kernel->getHeight();
383 
384  lsst::afw::geom::BoxI bbox(lsst::afw::geom::Point2I(x - binX*(2 + kWidth/2), y - binY*(2 + kHeight/2)),
385  lsst::afw::geom::ExtentI(binX*(3 + kWidth + 1), binY*(3 + kHeight + 1)));
386 
387  // image to smooth, a shallow copy
388  PTR(MaskedImageT) subImage;
389  try {
390  subImage.reset(new MaskedImageT(mimage, bbox, lsst::afw::image::LOCAL));
391  } catch (pex::exceptions::LengthError & err) {
392  throw LSST_EXCEPT(
393  MeasurementError,
394  _flagHandler.getDefinition(SdssCentroidAlgorithm::EDGE).doc,
396  );
397  }
398  PTR(MaskedImageT) binnedImage = lsst::afw::math::binImage(*subImage, binX, binY, lsst::afw::math::MEAN);
399  binnedImage->setXY0(subImage->getXY0());
400  // image to smooth into, a deep copy.
401  MaskedImageT smoothedImage = MaskedImageT(*binnedImage, true);
402  assert(smoothedImage.getWidth()/2 == kWidth/2 + 2); // assumed by the code that uses smoothedImage
403  assert(smoothedImage.getHeight()/2 == kHeight/2 + 2);
404 
405  lsst::afw::math::convolve(smoothedImage, *binnedImage, *kernel, lsst::afw::math::ConvolutionControl());
406  *smoothedImage.getVariance() *= binX*binY*nEffective; // We want the per-pixel variance, so undo the
407  // effects of binning and smoothing
408 
409  return std::make_pair(smoothedImage, smoothingSigma);
410 }
411 
412 std::array<FlagDefinition,SdssCentroidAlgorithm::N_FLAGS> const & getFlagDefinitions() {
413  static std::array<FlagDefinition,SdssCentroidAlgorithm::N_FLAGS> const flagDefs = {{
414  {"flag", "general failure flag, set if anything went wrong"},
415  {"flag_edge", "Object too close to edge"},
416  {"flag_noSecondDerivative", "Vanishing second derivative"},
417  {"flag_almostNoSecondDerivative", "Almost vanishing second derivative"},
418  {"flag_notAtMaximum", "Object is not at a maximum"}
419  }};
420  return flagDefs;
421 }
422 
423 } // end anonymous namespace
424 
426  Control const & ctrl,
427  std::string const & name,
429 ) : _ctrl(ctrl),
430  _centroidKey(
431  CentroidResultKey::addFields(schema, name, "centroid from Sdss Centroid algorithm", SIGMA_ONLY)
432  ),
433  _flagHandler(FlagHandler::addFields(schema, name,
434  getFlagDefinitions().begin(), getFlagDefinitions().end())),
435  _centroidExtractor(schema, name, true),
436  _centroidChecker(schema, name, ctrl.doFootprintCheck, ctrl.maxDistToPeak)
437 {
438 }
440  afw::table::SourceRecord & measRecord,
441  afw::image::Exposure<float> const & exposure
442 ) const {
443 
444  // get our current best guess about the centroid: either a centroider measurement or peak.
445  afw::geom::Point2D center = _centroidExtractor(measRecord, _flagHandler);
446  CentroidResult result;
447  result.x = center.getX();
448  result.y = center.getY();
449  measRecord.set(_centroidKey, result); // better than NaN
450 
451  typedef afw::image::Exposure<float>::MaskedImageT MaskedImageT;
452  typedef MaskedImageT::Image ImageT;
453  typedef MaskedImageT::Variance VarianceT;
454  bool negative = false;
455  try {
456  negative = measRecord.get(measRecord.getSchema().find<afw::table::Flag>("flags_negative").key);
457  } catch(pexExcept::Exception &e) {}
458 
459  MaskedImageT const& mimage = exposure.getMaskedImage();
460  ImageT const& image = *mimage.getImage();
461  CONST_PTR(lsst::afw::detection::Psf) psf = exposure.getPsf();
462 
463  int const x = image.positionToIndex(center.getX(), lsst::afw::image::X).first;
464  int const y = image.positionToIndex(center.getY(), lsst::afw::image::Y).first;
465 
466  if (!image.getBBox().contains(lsst::afw::geom::Extent2I(x,y) + image.getXY0())) {
467  throw LSST_EXCEPT(
469  _flagHandler.getDefinition(EDGE).doc,
470  EDGE
471  );
472  }
473 
474  // Algorithm uses a least-squares fit (implemented via a convolution) to a symmetrized PSF model.
475  // If you don't have a Psf, you need to use another centroider, such as GaussianCentroider.
476  if (!psf) {
477  throw LSST_EXCEPT(
478  FatalAlgorithmError,
479  "SdssCentroid algorithm requires a Psf with every exposure"
480  );
481  }
482 
483  int binX = 1;
484  int binY = 1;
485  double xc=0., yc=0., dxc=0., dyc=0.; // estimated centre and error therein
486  for(int binsize = 1; binsize <= _ctrl.binmax; binsize *= 2) {
487  std::pair<MaskedImageT, double> result = smoothAndBinImage(psf, x, y, mimage, binX, binY, _flagHandler);
488  MaskedImageT const smoothedImage = result.first;
489  double const smoothingSigma = result.second;
490 
491  MaskedImageT::xy_locator mim = smoothedImage.xy_at(smoothedImage.getWidth()/2,
492  smoothedImage.getHeight()/2);
493 
494  double sizeX2, sizeY2; // object widths^2 in x and y directions
495  double peakVal; // peak intensity in image
496 
497  doMeasureCentroidImpl(&xc, &dxc, &yc, &dyc, &sizeX2, &sizeY2, &peakVal, mim,
498  smoothingSigma, negative, _flagHandler);
499 
500  if(binsize > 1) {
501  // dilate from the lower left corner of central pixel
502  xc = (xc + 0.5)*binX - 0.5;
503  dxc *= binX;
504  sizeX2 *= binX*binX;
505 
506  yc = (yc + 0.5)*binY - 0.5;
507  dyc *= binY;
508  sizeY2 *= binY*binY;
509  }
510 
511  xc += x; // xc, yc are measured relative to pixel (x, y)
512  yc += y;
513 
514  double const fac = _ctrl.wfac*(1 + smoothingSigma*smoothingSigma);
515  double const facX2 = fac*binX*binX;
516  double const facY2 = fac*binY*binY;
517 
518  if (sizeX2 < facX2 && ::pow(xc - x, 2) < facX2 &&
519  sizeY2 < facY2 && ::pow(yc - y, 2) < facY2) {
520  if (binsize > 1 || _ctrl.peakMin < 0.0 || peakVal > _ctrl.peakMin) {
521  break;
522  }
523  }
524 
525  if (sizeX2 >= facX2 || ::pow(xc - x, 2) >= facX2) {
526  binX *= 2;
527  }
528  if (sizeY2 >= facY2 || ::pow(yc - y, 2) >= facY2) {
529  binY *= 2;
530  }
531  }
532  result.x = lsst::afw::image::indexToPosition(xc + image.getX0());
533  result.y = lsst::afw::image::indexToPosition(yc + image.getY0());
534 
535  result.xSigma = sqrt(dxc*dxc);
536  result.ySigma = sqrt(dyc*dyc);
537  measRecord.set(_centroidKey, result);
538  _centroidChecker(measRecord);
539 }
540 
541 
543  _flagHandler.handleFailure(measRecord, error);
544 }
545 
547  Control const & ctrl,
548  std::string const & name,
549  afw::table::SchemaMapper & mapper
550 ) :
551  CentroidTransform{name, mapper}
552 {
553  for (auto flag = getFlagDefinitions().begin() + 1; flag < getFlagDefinitions().end(); ++flag) {
554  mapper.addMapping(mapper.getInputSchema().find<afw::table::Flag>(
555  mapper.getInputSchema().join(name, flag->name)).key);
556  }
557 }
558 
559 }}} // end namespace lsst::meas::base
560 
int y
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:45
Defines the fields and offsets for a table.
Definition: Schema.h:44
SdssCentroidAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
ErrElement ySigma
1-Sigma uncertainty on y (sqrt of variance)
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:54
table::Key< std::string > name
Definition: ApCorrMap.cc:71
afw::table::Schema schema
Definition: GaussianPsf.cc:41
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:19
Include files required for standard LSST Exception handling.
Only the diagonal elements of the covariance matrix are provided.
Definition: constants.h:43
A reusable struct for centroid measurements.
ErrElement xSigma
1-Sigma uncertainty on x (sqrt of variance)
CentroidElement x
x (column) coordinate of the measured position
A C++ control class to handle SdssCentroidAlgorithm&#39;s configuration.
Definition: SdssCentroid.h:46
std::array< FlagDefinition, BlendednessAlgorithm::N_FLAGS > const & getFlagDefinitions()
Definition: Blendedness.cc:215
int binmax
&quot;maximum allowed binning&quot; ;
Definition: SdssCentroid.h:49
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:145
A coordinate class intended to represent absolute positions.
An integer coordinate rectangle.
Definition: Box.h:53
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:43
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
def error
Definition: log.py:103
boost::shared_ptr< Kernel const > ConstPtr
Definition: Kernel.h:139
Utility class for handling flag fields that indicate the failure modes of an algorithm.
Definition: FlagHandler.h:73
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:153
SafeCentroidExtractor _centroidExtractor
Definition: SdssCentroid.h:101
double x
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition: BaseRecord.h:132
Convolve and convolveAtAPoint functions for Image and Kernel.
double peakMin
&quot;if the peak&#39;s less than this insist on binning at least once&quot; ;
Definition: SdssCentroid.h:50
boost::shared_ptr< lsst::afw::detection::Psf > getPsf()
Return the Exposure&#39;s Psf object.
Definition: Exposure.h:227
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
double wfac
&quot;fiddle factor for adjusting the binning&quot; ;
Definition: SdssCentroid.h:51
void convolve(OutImageT &convolvedImage, InImageT const &inImage, KernelT const &kernel, ConvolutionControl const &convolutionControl=ConvolutionControl())
Convolve an Image or MaskedImage with a Kernel, setting pixels of an existing output image...
#define PTR(...)
Definition: base.h:41
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
Schema getSchema() const
Return the Schema that holds this record&#39;s fields and keys.
Definition: BaseRecord.h:57
estimate sample mean
Definition: Statistics.h:67
double getDeterminantRadius() const
Return the radius defined as the 4th root of the determinant of the quadrupole matrix.
A FunctorKey for CentroidResult.
boost::shared_ptr< math::Kernel const > getLocalKernel(geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Return a FixedKernel corresponding to the Psf image at the given point.
geom::ellipses::Quadrupole computeShape(geom::Point2D position=makeNullPoint(), image::Color color=image::Color()) const
Compute the ellipse corresponding to the second moments of the Psf.
CentroidElement y
y (row) coordinate of the measured position
metadata try
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Record class that contains measurements made on a single exposure.
Definition: Source.h:80
double const PI
The ratio of a circle&#39;s circumference to diameter.
Definition: Angle.h:17
This implements the SdssCentroid algorithm within the meas_base measurement framework.
#define CONST_PTR(...)
A shared pointer to a const object.
Definition: base.h:47
A polymorphic base class for representing an image&#39;s Point Spread Function.
Definition: Psf.h:68
afw::table::Key< double > sigma2
virtual void fail(afw::table::SourceRecord &measRecord, MeasurementError *error=NULL) const
Handle an exception thrown by the current algorithm by setting flags in the given record...
boost::shared_ptr< ImageT > binImage(ImageT const &in, int const binsize, lsst::afw::math::Property const flags)
Definition: binImage.cc:41
Base for centroid measurement transformations.
Key< T > addMapping(Key< T > const &inputKey, bool doReplace=false)
Add a new field to the output Schema that is a copy of a field in the input Schema.
SdssCentroidTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)