LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Private Attributes | List of all members
lsst::ip::diffim::PsfDipoleFlux Class Reference

#include <DipoleAlgorithms.h>

Inheritance diagram for lsst::ip::diffim::PsfDipoleFlux:
lsst::ip::diffim::DipoleFluxAlgorithm lsst::meas::base::SimpleAlgorithm lsst::meas::base::SingleFrameAlgorithm lsst::meas::base::ForcedAlgorithm lsst::meas::base::BaseAlgorithm lsst::meas::base::BaseAlgorithm

Public Types

typedef PsfDipoleFluxControl Control
 
- Public Types inherited from lsst::ip::diffim::DipoleFluxAlgorithm
enum  { FAILURE =meas::base::FlagHandler::FAILURE, POS_FAILURE, NEG_FAILURE, N_FLAGS }
 
typedef DipoleFluxControl Control
 
typedef meas::base::FluxResultKey ResultKey
 

Public Member Functions

 PsfDipoleFlux (PsfDipoleFluxControl const &ctrl, std::string const &name, afw::table::Schema &schema)
 
std::pair< double, int > chi2 (afw::table::SourceRecord &source, afw::image::Exposure< float > const &exposure, double negCenterX, double negCenterY, double negFlux, double posCenterX, double poCenterY, double posFlux) const
 
void measure (afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
 
void fail (afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
 
- Public Member Functions inherited from lsst::ip::diffim::DipoleFluxAlgorithm
 DipoleFluxAlgorithm (Control const &ctrl, std::string const &name, afw::table::Schema &schema, std::string const &doc)
 
ResultKey const & getPositiveKeys () const
 Return the standard flux keys registered by this algorithm. More...
 
ResultKey const & getNegativeKeys () const
 
- Public Member Functions inherited from lsst::meas::base::SimpleAlgorithm
virtual void measureForced (afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure, afw::table::SourceRecord const &refRecord, afw::image::Wcs const &refWcs) const
 
virtual void measureNForced (afw::table::SourceCatalog const &measCat, afw::image::Exposure< float > const &exposure, afw::table::SourceCatalog const &refRecord, afw::image::Wcs const &refWcs) const
 
- Public Member Functions inherited from lsst::meas::base::SingleFrameAlgorithm
virtual void measureN (afw::table::SourceCatalog const &measCat, afw::image::Exposure< float > const &exposure) const
 
- Public Member Functions inherited from lsst::meas::base::BaseAlgorithm
virtual ~BaseAlgorithm ()
 

Private Attributes

Control _ctrl
 
afw::table::Key< float > _chi2dofKey
 
meas::base::CentroidResultKey _avgCentroid
 
meas::base::CentroidResultKey _negCentroid
 
meas::base::CentroidResultKey _posCentroid
 
afw::table::Key< afw::table::Flag > _flagMaxPixelsKey
 

Additional Inherited Members

- Protected Member Functions inherited from lsst::ip::diffim::DipoleFluxAlgorithm
 DipoleFluxAlgorithm (Control const &ctrl, std::string const &name, afw::table::Schema &schema, std::string const &doc, ResultKey const &positiveKeys, ResultKey const &negativeKeys)
 Initialize with a manually-constructed result key. More...
 
- Protected Attributes inherited from lsst::ip::diffim::DipoleFluxAlgorithm
Control _ctrl
 
meas::base::FluxResultKey _fluxResultKey
 
meas::base::FlagHandler _flagHandler
 
meas::base::SafeCentroidExtractor _centroidExtractor
 
ResultKey _positiveKeys
 
ResultKey _negativeKeys
 

Detailed Description

Implementation of Psf dipole flux

Definition at line 314 of file DipoleAlgorithms.h.

Member Typedef Documentation

Definition at line 317 of file DipoleAlgorithms.h.

Constructor & Destructor Documentation

lsst::ip::diffim::PsfDipoleFlux::PsfDipoleFlux ( PsfDipoleFluxControl const &  ctrl,
std::string const &  name,
afw::table::Schema schema 
)
inline

Definition at line 319 of file DipoleAlgorithms.h.

319  :
320  DipoleFluxAlgorithm(ctrl, name, schema, "jointly fitted psf flux counts"),
321  _ctrl(ctrl),
322  _chi2dofKey(schema.addField<float>(name+"_chi2dof",
323  "chi2 per degree of freedom of fit")),
324  _flagMaxPixelsKey(schema.addField<afw::table::Flag>(name+"_flags_maxpix",
325  "set if too large a footprint was sent to the algorithm"))
326  {
327  meas::base::CentroidResultKey::addFields(schema, name+"_pos_centroid", "psf fitted center of positive lobe", meas::base::SIGMA_ONLY);
328  meas::base::CentroidResultKey::addFields(schema, name+"_neg_centroid", "psf fitted center of negative lobe", meas::base::SIGMA_ONLY);
329  meas::base::CentroidResultKey::addFields(schema, name+"_centroid", "average of negative and positive lobe positions", meas::base::SIGMA_ONLY);
330  _posCentroid = meas::base::CentroidResultKey(schema[name+"_pos_centroid"]);
331  _negCentroid = meas::base::CentroidResultKey(schema[name+"_neg_centroid"]);
332  _avgCentroid = meas::base::CentroidResultKey(schema[name+"_centroid"]);
333  }
afw::table::Key< float > _chi2dofKey
meas::base::CentroidResultKey _avgCentroid
table::Key< std::string > name
Definition: ApCorrMap.cc:71
afw::table::Schema schema
Definition: GaussianPsf.cc:41
Only the diagonal elements of the covariance matrix are provided.
Definition: constants.h:43
meas::base::CentroidResultKey _negCentroid
meas::base::CentroidResultKey _posCentroid
static CentroidResultKey addFields(afw::table::Schema &schema, std::string const &name, std::string const &doc, UncertaintyEnum uncertainty)
Add the appropriate fields to a Schema, and return a CentroidResultKey that manages them...
DipoleFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema, std::string const &doc)
afw::table::Key< afw::table::Flag > _flagMaxPixelsKey

Member Function Documentation

std::pair< double, int > lsst::ip::diffim::PsfDipoleFlux::chi2 ( afw::table::SourceRecord source,
afw::image::Exposure< float > const &  exposure,
double  negCenterX,
double  negCenterY,
double  negFlux,
double  posCenterX,
double  poCenterY,
double  posFlux 
) const

Definition at line 295 of file DipoleAlgorithms.cc.

300  {
301 
302  afw::geom::Point2D negCenter(negCenterX, negCenterY);
303  afw::geom::Point2D posCenter(posCenterX, posCenterY);
304 
305  CONST_PTR(afw::detection::Footprint) footprint = source.getFootprint();
306 
307  /*
308  * Fit for the superposition of Psfs at the two centroids.
309  */
310  CONST_PTR(afwDet::Psf) psf = exposure.getPsf();
311  PTR(afwImage::Image<afwMath::Kernel::Pixel>) negPsf = psf->computeImage(negCenter);
312  PTR(afwImage::Image<afwMath::Kernel::Pixel>) posPsf = psf->computeImage(posCenter);
313 
314  afwImage::Image<double> negModel(footprint->getBBox());
315  afwImage::Image<double> posModel(footprint->getBBox());
316  afwImage::Image<float> data(*(exposure.getMaskedImage().getImage()),footprint->getBBox());
317  afwImage::Image<afwImage::VariancePixel> var(*(exposure.getMaskedImage().getVariance()),
318  footprint->getBBox());
319 
320  afwGeom::Box2I negPsfBBox = negPsf->getBBox();
321  afwGeom::Box2I posPsfBBox = posPsf->getBBox();
322  afwGeom::Box2I negModelBBox = negModel.getBBox();
323  afwGeom::Box2I posModelBBox = posModel.getBBox();
324 
325  // Portion of the negative Psf that overlaps the model
326  int negXmin = std::max(negPsfBBox.getMinX(), negModelBBox.getMinX());
327  int negYmin = std::max(negPsfBBox.getMinY(), negModelBBox.getMinY());
328  int negXmax = std::min(negPsfBBox.getMaxX(), negModelBBox.getMaxX());
329  int negYmax = std::min(negPsfBBox.getMaxY(), negModelBBox.getMaxY());
330  afwGeom::Box2I negBBox = afwGeom::Box2I(afwGeom::Point2I(negXmin, negYmin),
331  afwGeom::Point2I(negXmax, negYmax));
332  afwImage::Image<afwMath::Kernel::Pixel> negSubim(*negPsf, negBBox);
333  afwImage::Image<double> negModelSubim(negModel, negBBox);
334  negModelSubim += negSubim;
335 
336  // Portion of the positive Psf that overlaps the model
337  int posXmin = std::max(posPsfBBox.getMinX(), posModelBBox.getMinX());
338  int posYmin = std::max(posPsfBBox.getMinY(), posModelBBox.getMinY());
339  int posXmax = std::min(posPsfBBox.getMaxX(), posModelBBox.getMaxX());
340  int posYmax = std::min(posPsfBBox.getMaxY(), posModelBBox.getMaxY());
341  afwGeom::Box2I posBBox = afwGeom::Box2I(afwGeom::Point2I(posXmin, posYmin),
342  afwGeom::Point2I(posXmax, posYmax));
343  afwImage::Image<afwMath::Kernel::Pixel> posSubim(*posPsf, posBBox);
344  afwImage::Image<double> posModelSubim(posModel, posBBox);
345  posModelSubim += posSubim;
346 
347  negModel *= negFlux; // scale negative model to image
348  posModel *= posFlux; // scale positive model to image
349  afwImage::Image<double> residuals(negModel, true); // full model contains negative lobe...
350  residuals += posModel; // plus positive lobe...
351  residuals -= data; // minus the data...
352  residuals *= residuals; // squared...
353  residuals /= var; // divided by the variance : [(model-data)/sigma]**2
354  afwMath::Statistics stats = afwMath::makeStatistics(residuals, afwMath::SUM | afwMath::NPOINT);
355  double chi2 = stats.getValue(afwMath::SUM);
356  int nPix = stats.getValue(afwMath::NPOINT);
357  return std::pair<double,int>(chi2, nPix);
358 }
#define PTR(...)
Definition: base.h:41
Point< double, 2 > Point2D
Definition: Point.h:286
Point< int, 2 > Point2I
Definition: Point.h:283
number of sample points
Definition: Statistics.h:66
lsst::afw::detection::Footprint Footprint
Definition: Source.h:61
std::pair< double, int > chi2(afw::table::SourceRecord &source, afw::image::Exposure< float > const &exposure, double negCenterX, double negCenterY, double negFlux, double posCenterX, double poCenterY, double posFlux) const
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
Definition: Statistics.cc:1082
#define CONST_PTR(...)
Definition: base.h:47
find sum of pixels in the image
Definition: Statistics.h:78
float VariancePixel
! default type for Masks and MaskedImage Masks
void lsst::ip::diffim::PsfDipoleFlux::fail ( afw::table::SourceRecord measRecord,
meas::base::MeasurementError error = NULL 
) const
virtual

Handle an exception thrown by the current algorithm by setting flags in the given record.

fail() is called by the measurement framework when an exception is allowed to propagate out of one the algorithm's measure() methods. It should generally set both a general failure flag for the algorithm as well as a specific flag indicating the error condition, if possible. To aid in this, if the exception was an instance of MeasurementError, it will be passed in, carrying information about what flag to set.

An algorithm can also to chose to set flags within its own measure() methods, and then just return, rather than throw an exception. However, fail() should be implemented even when all known failure modes do not throw exceptions, to ensure that unexpected exceptions thrown in lower-level code are properly handled.

Implements lsst::meas::base::BaseAlgorithm.

Definition at line 463 of file DipoleAlgorithms.cc.

463  {
464  _flagHandler.handleFailure(measRecord, error);
465 }
def error
Definition: log.py:108
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=NULL) const
Definition: FlagHandler.cc:59
void lsst::ip::diffim::PsfDipoleFlux::measure ( afw::table::SourceRecord measRecord,
afw::image::Exposure< float > const &  exposure 
) const
virtual

Called to measure a single child source in an image.

Before this method is called, all neighbors will be replaced with noise, using the outputs of the deblender. Outputs should be saved in the given SourceRecord, which can also be used to obtain centroid (see SafeCentroidExtractor) and shape (see SafeShapeExtractor) information.

Implements lsst::meas::base::SingleFrameAlgorithm.

Definition at line 360 of file DipoleAlgorithms.cc.

363  {
364  source.set(_flagMaxPixelsKey, true);
365 
366  typedef afw::image::Exposure<float>::MaskedImageT MaskedImageT;
367 
368  CONST_PTR(afw::detection::Footprint) footprint = source.getFootprint();
369  if (!footprint) {
370  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
371  (boost::format("No footprint for source %d") % source.getId()).str());
372  }
373 
374  if (footprint->getArea() > _ctrl.maxPixels) {
375  // Too big
376  return;
377  }
378  source.set(_flagMaxPixelsKey, false);
379 
380  afw::detection::PeakCatalog peakCatalog = afw::detection::PeakCatalog(footprint->getPeaks());
381 
382  if (peakCatalog.size() == 0) {
383  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
384  (boost::format("No peak for source %d") % source.getId()).str());
385  }
386  else if (peakCatalog.size() == 1) {
387  // No deblending to do
388  return;
389  }
390 
391  // For N>=2, just measure the brightest-positive and brightest-negative
392  // peaks. peakCatalog is automatically ordered by peak flux, with the most
393  // positive one (brightest) being first
394  afw::detection::PeakRecord const& positivePeak = peakCatalog.front();
395  afw::detection::PeakRecord const& negativePeak = peakCatalog.back();
396 
397  // Set up fit parameters and param names
398  ROOT::Minuit2::MnUserParameters fitPar;
399 
400  fitPar.Add((boost::format("P%d")%NEGCENTXPAR).str(), negativePeak.getFx(), _ctrl.stepSizeCoord);
401  fitPar.Add((boost::format("P%d")%NEGCENTYPAR).str(), negativePeak.getFy(), _ctrl.stepSizeCoord);
402  fitPar.Add((boost::format("P%d")%NEGFLUXPAR).str(), negativePeak.getPeakValue(), _ctrl.stepSizeFlux);
403  fitPar.Add((boost::format("P%d")%POSCENTXPAR).str(), positivePeak.getFx(), _ctrl.stepSizeCoord);
404  fitPar.Add((boost::format("P%d")%POSCENTYPAR).str(), positivePeak.getFy(), _ctrl.stepSizeCoord);
405  fitPar.Add((boost::format("P%d")%POSFLUXPAR).str(), positivePeak.getPeakValue(), _ctrl.stepSizeFlux);
406 
407  // Create the minuit object that knows how to minimise our functor
408  //
409  MinimizeDipoleChi2 minimizerFunc(*this, source, exposure);
410  minimizerFunc.setErrorDef(_ctrl.errorDef);
411 
412  //
413  // tell minuit about it
414  //
415  ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
416 
417  //
418  // And let it loose
419  //
420  ROOT::Minuit2::FunctionMinimum min = migrad(_ctrl.maxFnCalls);
421 
422  float minChi2 = min.Fval();
423  bool const isValid = min.IsValid() && std::isfinite(minChi2);
424 
425  if (true || isValid) { // calculate coeffs even in minuit is unhappy
426 
427  /* I need to call chi2 one more time to grab nPix to calculate chi2/dof.
428  Turns out that the Minuit operator method has to be const, and the
429  measurement _apply method has to be const, so I can't store nPix as a
430  private member variable anywhere. Consted into a corner.
431  */
432  std::pair<double,int> fit = chi2(source, exposure,
433  min.UserState().Value(NEGCENTXPAR),
434  min.UserState().Value(NEGCENTYPAR),
435  min.UserState().Value(NEGFLUXPAR),
436  min.UserState().Value(POSCENTXPAR),
437  min.UserState().Value(POSCENTYPAR),
438  min.UserState().Value(POSFLUXPAR));
439  double evalChi2 = fit.first;
440  int nPix = fit.second;
441 
442  PTR(afw::geom::Point2D) minNegCentroid(new afw::geom::Point2D(min.UserState().Value(NEGCENTXPAR),
443  min.UserState().Value(NEGCENTYPAR)));
444  source.set(getNegativeKeys().getFlux(), min.UserState().Value(NEGFLUXPAR));
445  source.set(getNegativeKeys().getFluxSigma(), min.UserState().Error(NEGFLUXPAR));
446 
447  PTR(afw::geom::Point2D) minPosCentroid(new afw::geom::Point2D(min.UserState().Value(POSCENTXPAR),
448  min.UserState().Value(POSCENTYPAR)));
449  source.set(getPositiveKeys().getFlux(), min.UserState().Value(POSFLUXPAR));
450  source.set(getPositiveKeys().getFluxSigma(), min.UserState().Error(POSFLUXPAR));
451 
452  source.set(_chi2dofKey, evalChi2 / (nPix - minimizerFunc.getNpar()));
453  source.set(_negCentroid.getX(), minNegCentroid->getX());
454  source.set(_negCentroid.getY(), minNegCentroid->getY());
455  source.set(_posCentroid.getX(), minPosCentroid->getX());
456  source.set(_posCentroid.getY(), minPosCentroid->getY());
457  source.set(_avgCentroid.getX(), 0.5*(minNegCentroid->getX() + minPosCentroid->getX()));
458  source.set(_avgCentroid.getY(), 0.5*(minNegCentroid->getY() + minPosCentroid->getY()));
459 
460  }
461 }
afw::table::Key< float > _chi2dofKey
float stepSizeFlux
&quot;Default initial step size for flux in non-linear fitter&quot; ;
meas::base::CentroidResultKey _avgCentroid
int const NEGFLUXPAR(2)
int const NEGCENTXPAR(0)
#define PTR(...)
Definition: base.h:41
Point< double, 2 > Point2D
Definition: Point.h:286
meas::base::CentroidResultKey _negCentroid
MaskedImage< float, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel > MaskedImageT
Definition: Exposure.h:51
ResultKey const & getPositiveKeys() const
Return the standard flux keys registered by this algorithm.
int const POSCENTXPAR(3)
int maxPixels
&quot;Maximum number of pixels to apply the measurement to&quot; ;
meas::base::CentroidResultKey _posCentroid
afw::table::CatalogT< PeakRecord > PeakCatalog
Definition: Peak.h:225
ResultKey const & getNegativeKeys() const
if(width!=gim.getWidth()||height!=gim.getHeight()||x0!=gim.getX0()||y0!=gim.getY0())
Definition: saturated.cc:47
int const POSCENTYPAR(4)
double errorDef
&quot;How many sigma the error bars of the non-linear fitter represent&quot; ;
int maxFnCalls
&quot;Maximum function calls for non-linear fitter; 0 = unlimited&quot; ;
int isfinite(T t)
Definition: ieee.h:100
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
int const POSFLUXPAR(5)
int const NEGCENTYPAR(1)
float stepSizeCoord
&quot;Default initial step size for coordinates in non-linear fitter&quot; ;
lsst::afw::detection::Footprint Footprint
Definition: Source.h:61
std::pair< double, int > chi2(afw::table::SourceRecord &source, afw::image::Exposure< float > const &exposure, double negCenterX, double negCenterY, double negFlux, double posCenterX, double poCenterY, double posFlux) const
#define CONST_PTR(...)
Definition: base.h:47
afw::table::Key< afw::table::Flag > _flagMaxPixelsKey

Member Data Documentation

meas::base::CentroidResultKey lsst::ip::diffim::PsfDipoleFlux::_avgCentroid
private

Definition at line 354 of file DipoleAlgorithms.h.

afw::table::Key<float> lsst::ip::diffim::PsfDipoleFlux::_chi2dofKey
private

Definition at line 353 of file DipoleAlgorithms.h.

Control lsst::ip::diffim::PsfDipoleFlux::_ctrl
private

Definition at line 352 of file DipoleAlgorithms.h.

afw::table::Key< afw::table::Flag > lsst::ip::diffim::PsfDipoleFlux::_flagMaxPixelsKey
private

Definition at line 358 of file DipoleAlgorithms.h.

meas::base::CentroidResultKey lsst::ip::diffim::PsfDipoleFlux::_negCentroid
private

Definition at line 355 of file DipoleAlgorithms.h.

meas::base::CentroidResultKey lsst::ip::diffim::PsfDipoleFlux::_posCentroid
private

Definition at line 356 of file DipoleAlgorithms.h.


The documentation for this class was generated from the following files: