LSSTApplications  20.0.0
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
lsst::meas::base::SdssShapeAlgorithm Class Reference

Measure the image moments of source using adaptive Gaussian weights. More...

#include <SdssShape.h>

Inheritance diagram for lsst::meas::base::SdssShapeAlgorithm:
lsst::meas::base::SimpleAlgorithm lsst::meas::base::SingleFrameAlgorithm lsst::meas::base::ForcedAlgorithm lsst::meas::base::BaseAlgorithm lsst::meas::base::BaseAlgorithm

Public Types

typedef SdssShapeControl Control
 
typedef SdssShapeResult Result
 
typedef SdssShapeResultKey ResultKey
 

Public Member Functions

 SdssShapeAlgorithm (Control const &ctrl, std::string const &name, afw::table::Schema &schema)
 
virtual void measure (afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
 Called to measure a single child source in an image. More...
 
virtual void fail (afw::table::SourceRecord &measRecord, MeasurementError *error=nullptr) const
 Handle an exception thrown by the current algorithm by setting flags in the given record. More...
 
template<typename ImageT >
SdssShapeResult computeAdaptiveMoments (ImageT const &image, geom::Point2D const &center, bool negative, Control const &control)
 
virtual void measureForced (afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure, afw::table::SourceRecord const &refRecord, afw::geom::SkyWcs const &refWcs) const
 Called to measure a single child source in an image. More...
 
virtual void measureNForced (afw::table::SourceCatalog const &measCat, afw::image::Exposure< float > const &exposure, afw::table::SourceCatalog const &refRecord, afw::geom::SkyWcs const &refWcs) const
 Called to simultaneously measure all children in a deblend family, in a single image. More...
 
virtual void measureN (afw::table::SourceCatalog const &measCat, afw::image::Exposure< float > const &exposure) const
 Called to simultaneously measure all children in a deblend family, in a single image. More...
 
std::string getLogName () const
 

Static Public Member Functions

static FlagDefinitionList const & getFlagDefinitions ()
 
template<typename ImageT >
static Result computeAdaptiveMoments (ImageT const &image, geom::Point2D const &position, bool negative=false, Control const &ctrl=Control())
 Compute the adaptive Gaussian-weighted moments of an image. More...
 
template<typename ImageT >
static FluxResult computeFixedMomentsFlux (ImageT const &image, afw::geom::ellipses::Quadrupole const &shape, geom::Point2D const &position)
 Compute the instFlux within a fixed Gaussian aperture. More...
 

Static Public Attributes

static unsigned int const N_FLAGS = 6
 
static FlagDefinition const FAILURE = flagDefinitions.addFailureFlag()
 
static FlagDefinition const UNWEIGHTED_BAD
 
static FlagDefinition const UNWEIGHTED
 
static FlagDefinition const SHIFT
 
static FlagDefinition const MAXITER
 
static FlagDefinition const PSF_SHAPE_BAD
 

Protected Attributes

std::string _logName
 

Detailed Description

Measure the image moments of source using adaptive Gaussian weights.

This algorithm measures the weighted second moments of an image using a Gaussian weight function, which is iteratively updated to match the current weights. If this iteration does not converge, it can fall back to using unweighted moments, which can be significantly noisier.

See Bernstein & Jarvis, 2002, for more information on this type of algorithm. Note that the code here makes no attempt to correct for the PSF; for PSF corrected ellipticities using weighted moments please use the shapeHSM package.

Definition at line 150 of file SdssShape.h.

Member Typedef Documentation

◆ Control

Definition at line 162 of file SdssShape.h.

◆ Result

Definition at line 163 of file SdssShape.h.

◆ ResultKey

Definition at line 164 of file SdssShape.h.

Constructor & Destructor Documentation

◆ SdssShapeAlgorithm()

lsst::meas::base::SdssShapeAlgorithm::SdssShapeAlgorithm ( Control const &  ctrl,
std::string const &  name,
afw::table::Schema schema 
)

Definition at line 764 of file SdssShape.cc.

766  : _ctrl(ctrl),
767  _resultKey(ResultKey::addFields(schema, name, ctrl.doMeasurePsf)),
768  _centroidExtractor(schema, name) {}

Member Function Documentation

◆ computeAdaptiveMoments() [1/2]

template<typename ImageT >
SdssShapeResult lsst::meas::base::SdssShapeAlgorithm::computeAdaptiveMoments ( ImageT const &  image,
geom::Point2D const &  center,
bool  negative,
Control const &  control 
)

Definition at line 771 of file SdssShape.cc.

772  {
773  double xcen = center.getX(); // object's column position
774  double ycen = center.getY(); // object's row position
775 
776  xcen -= image.getX0(); // work in image Pixel coordinates
777  ycen -= image.getY0();
778 
779  float shiftmax = control.maxShift; // Max allowed centroid shift
780  if (shiftmax < 2) {
781  shiftmax = 2;
782  } else if (shiftmax > 10) {
783  shiftmax = 10;
784  }
785 
786  SdssShapeResult result;
787  try {
788  result.flags[FAILURE.number] =
789  !getAdaptiveMoments(image, control.background, xcen, ycen, shiftmax, &result, control.maxIter,
790  control.tol1, control.tol2, negative);
791  } catch (pex::exceptions::Exception &err) {
792  result.flags[FAILURE.number] = true;
793  }
794  if (result.flags[UNWEIGHTED.number] || result.flags[SHIFT.number]) {
795  // These are also considered fatal errors in terms of the quality of the results,
796  // even though they do produce some results.
797  result.flags[FAILURE.number] = true;
798  }
799  if (result.getQuadrupole().getIxx() * result.getQuadrupole().getIyy() <
800  (1.0 + 1.0e-6) * result.getQuadrupole().getIxy() * result.getQuadrupole().getIxy())
801  // We are checking that Ixx*Iyy > (1 + epsilon)*Ixy*Ixy where epsilon is suitably small. The
802  // value of epsilon used here is a magic number. DM-5801 is supposed to figure out if we are
803  // to keep this value.
804  {
805  if (!result.flags[FAILURE.number]) {
806  throw LSST_EXCEPT(pex::exceptions::LogicError,
807  "Should not get singular moments unless a flag is set");
808  }
809  }
810 
811  // getAdaptiveMoments() just computes the zeroth moment in result.instFlux (and its error in
812  // result.instFluxErr, result.instFlux_xx_Cov, etc.) That's related to the instFlux by some geometric
813  // factors, which we apply here.
814  double instFluxScale = computeFluxScale(result);
815 
816  result.instFlux *= instFluxScale;
817  result.instFluxErr *= instFluxScale;
818  result.x += image.getX0();
819  result.y += image.getY0();
820 
821  if (ImageAdaptor<ImageT>::hasVariance) {
822  result.instFlux_xx_Cov *= instFluxScale;
823  result.instFlux_yy_Cov *= instFluxScale;
824  result.instFlux_xy_Cov *= instFluxScale;
825  }
826 
827  return result;
828 }

◆ computeAdaptiveMoments() [2/2]

template<typename ImageT >
static Result lsst::meas::base::SdssShapeAlgorithm::computeAdaptiveMoments ( ImageT const &  image,
geom::Point2D const &  position,
bool  negative = false,
Control const &  ctrl = Control() 
)
static

Compute the adaptive Gaussian-weighted moments of an image.

Parameters
[in]imageAn Image or MaskedImage instance with int, float, or double pixels. This need not be a small postage stamp (the pixel region actually used in the fit will be a subset of this image determined automatically).
[in]positionCenter position of the object to be measured, in the image's PARENT coordinates.
[in]negativeBoolean, specify if the source is in negative instFlux space
[in]ctrlControl object specifying the details of how the object is to be measured.

◆ computeFixedMomentsFlux()

template<typename ImageT >
template FluxResult lsst::meas::base::SdssShapeAlgorithm::computeFixedMomentsFlux ( ImageT const &  image,
afw::geom::ellipses::Quadrupole const &  shape,
geom::Point2D const &  position 
)
static

Compute the instFlux within a fixed Gaussian aperture.

Parameters
[in]imageAn Image or MaskedImage instance with int, float, or double pixels. This need not be a small postage stamp (the pixel region actually used in the fit will be a subset of this image determined automatically).
[in]shapeEllipse object specifying the 1-sigma contour of the Gaussian.
[in]positionCenter position of the object to be measured, in the image's PARENT coordinates.

Definition at line 831 of file SdssShape.cc.

833  {
834  // while arguments to computeFixedMomentsFlux are in PARENT coordinates, the implementation is LOCAL.
835  geom::Point2D localCenter = center - geom::Extent2D(image.getXY0());
836 
837  geom::BoxI const bbox = computeAdaptiveMomentsBBox(image.getBBox(afw::image::LOCAL), localCenter,
838  shape.getIxx(), shape.getIxy(), shape.getIyy());
839 
840  std::tuple<std::pair<bool, double>, double, double, double> weights =
841  getWeights(shape.getIxx(), shape.getIxy(), shape.getIyy());
842 
843  FluxResult result;
844 
845  if (!std::get<0>(weights).first) {
846  throw pex::exceptions::InvalidParameterError("Input shape is singular");
847  }
848 
849  double const w11 = std::get<1>(weights);
850  double const w12 = std::get<2>(weights);
851  double const w22 = std::get<3>(weights);
852  bool const interp = shouldInterp(shape.getIxx(), shape.getIyy(), std::get<0>(weights).second);
853 
854  double i0 = 0; // amplitude of Gaussian
855  if (calcmom<true>(ImageAdaptor<ImageT>().getImage(image), localCenter.getX(), localCenter.getY(), bbox,
856  0.0, interp, w11, w12, w22, &i0, NULL, NULL, NULL, NULL, NULL, NULL, NULL) < 0) {
857  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Error from calcmom");
858  }
859 
860  double const wArea = geom::PI * std::sqrt(shape.getDeterminant());
861 
862  result.instFlux = i0 * 2 * wArea;
863 
864  if (ImageAdaptor<ImageT>::hasVariance) {
865  int ix = static_cast<int>(center.getX() - image.getX0());
866  int iy = static_cast<int>(center.getY() - image.getY0());
867  if (!image.getBBox(afw::image::LOCAL).contains(geom::Point2I(ix, iy))) {
868  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
869  (boost::format("Center (%d,%d) not in image (%dx%d)") % ix % iy %
870  image.getWidth() % image.getHeight())
871  .str());
872  }
873  double var = ImageAdaptor<ImageT>().getVariance(image, ix, iy);
874  double i0Err = std::sqrt(var / wArea);
875  result.instFluxErr = i0Err * 2 * wArea;
876  }
877 
878  return result;
879 }

◆ fail()

void lsst::meas::base::SdssShapeAlgorithm::fail ( afw::table::SourceRecord measRecord,
MeasurementError error = nullptr 
) 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 916 of file SdssShape.cc.

916  {
917  _resultKey.getFlagHandler().handleFailure(measRecord, error);
918 }

◆ getFlagDefinitions()

FlagDefinitionList const & lsst::meas::base::SdssShapeAlgorithm::getFlagDefinitions ( )
static

Definition at line 58 of file SdssShape.cc.

58 { return flagDefinitions; }

◆ getLogName()

std::string lsst::meas::base::BaseAlgorithm::getLogName ( ) const
inlineinherited

Definition at line 66 of file Algorithm.h.

66 { return _logName; }

◆ measure()

void lsst::meas::base::SdssShapeAlgorithm::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 881 of file SdssShape.cc.

882  {
883  bool negative = false;
884 
885  try {
886  negative = measRecord.get(measRecord.getSchema().find<afw::table::Flag>("flags_negative").key);
887  } catch (pexExcept::Exception &e) {
888  }
889  SdssShapeResult result = computeAdaptiveMoments(
890  exposure.getMaskedImage(), _centroidExtractor(measRecord, _resultKey.getFlagHandler()), negative,
891  _ctrl);
892 
893  if (_ctrl.doMeasurePsf) {
894  // Compute moments of Psf model. In the interest of implementing this quickly, we're just
895  // calling Psf::computeShape(), which delegates to SdssShapeResult::computeAdaptiveMoments
896  // for all nontrivial Psf classes. But this could in theory save the results of a shape
897  // computed some other way as part of base_SdssShape, which might be confusing. We should
898  // fix this eventually either by making Psf shape measurement not part of base_SdssShape, or
899  // by making the measurements stored with shape.sdss always computed via the
900  // SdssShapeAlgorithm instead of delegating to the Psf class.
901  try {
902  PTR(afw::detection::Psf const) psf = exposure.getPsf();
903  if (!psf) {
904  result.flags[PSF_SHAPE_BAD.number] = true;
905  } else {
906  _resultKey.setPsfShape(measRecord, psf->computeShape(geom::Point2D(result.x, result.y)));
907  }
908  } catch (pex::exceptions::Exception &err) {
909  result.flags[PSF_SHAPE_BAD.number] = true;
910  }
911  }
912 
913  measRecord.set(_resultKey, result);
914 }

◆ measureForced()

virtual void lsst::meas::base::SimpleAlgorithm::measureForced ( afw::table::SourceRecord measRecord,
afw::image::Exposure< float > const &  exposure,
afw::table::SourceRecord const &  refRecord,
afw::geom::SkyWcs const &  refWcs 
) const
inlinevirtualinherited

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::ForcedAlgorithm.

Reimplemented in lsst::meas::extensions::photometryKron::KronFluxAlgorithm.

Definition at line 172 of file Algorithm.h.

175  {
176  measure(measRecord, exposure);
177  }

◆ measureN()

void lsst::meas::base::SingleFrameAlgorithm::measureN ( afw::table::SourceCatalog const &  measCat,
afw::image::Exposure< float > const &  exposure 
) const
virtualinherited

Called to simultaneously measure all children in a deblend family, in a single image.

Outputs should be saved in the given SourceCatalog, which can also be used to obtain centroid (see SafeCentroidExtractor) and shape (see SafeShapeExtractor) information.

The default implementation simply throws an exception, indicating that simultaneous measurement is not supported.

Definition at line 31 of file Algorithm.cc.

32  {
33  throw LSST_EXCEPT(pex::exceptions::LogicError, "measureN not implemented for this algorithm");
34 }

◆ measureNForced()

virtual void lsst::meas::base::SimpleAlgorithm::measureNForced ( afw::table::SourceCatalog const &  measCat,
afw::image::Exposure< float > const &  exposure,
afw::table::SourceCatalog const &  refRecord,
afw::geom::SkyWcs const &  refWcs 
) const
inlinevirtualinherited

Called to simultaneously measure all children in a deblend family, in a single image.

Outputs should be saved in the given SourceCatalog, which can also be used to obtain centroid (see SafeCentroidExtractor) and shape (see SafeShapeExtractor) information.

The default implementation simply throws an exception, indicating that simultaneous measurement is not supported.

Reimplemented from lsst::meas::base::ForcedAlgorithm.

Definition at line 179 of file Algorithm.h.

182  {
183  measureN(measCat, exposure);
184  }

Member Data Documentation

◆ _logName

std::string lsst::meas::base::BaseAlgorithm::_logName
protectedinherited

Definition at line 69 of file Algorithm.h.

◆ FAILURE

FlagDefinition const lsst::meas::base::SdssShapeAlgorithm::FAILURE = flagDefinitions.addFailureFlag()
static

Definition at line 155 of file SdssShape.h.

◆ MAXITER

FlagDefinition const lsst::meas::base::SdssShapeAlgorithm::MAXITER
static
Initial value:
=
flagDefinitions.add("flag_maxIter", "Too many iterations in adaptive moments")

Definition at line 159 of file SdssShape.h.

◆ N_FLAGS

unsigned int const lsst::meas::base::SdssShapeAlgorithm::N_FLAGS = 6
static

Definition at line 154 of file SdssShape.h.

◆ PSF_SHAPE_BAD

FlagDefinition const lsst::meas::base::SdssShapeAlgorithm::PSF_SHAPE_BAD
static
Initial value:
=
flagDefinitions.add("flag_psf", "Failure in measuring PSF model shape")

Definition at line 160 of file SdssShape.h.

◆ SHIFT

FlagDefinition const lsst::meas::base::SdssShapeAlgorithm::SHIFT
static
Initial value:
=
flagDefinitions.add("flag_shift", "centroid shifted by more than the maximum allowed amount")

Definition at line 158 of file SdssShape.h.

◆ UNWEIGHTED

FlagDefinition const lsst::meas::base::SdssShapeAlgorithm::UNWEIGHTED
static
Initial value:
= flagDefinitions.add(
"flag_unweighted", "Weighted moments converged to an invalid value; using unweighted moments")

Definition at line 157 of file SdssShape.h.

◆ UNWEIGHTED_BAD

FlagDefinition const lsst::meas::base::SdssShapeAlgorithm::UNWEIGHTED_BAD
static
Initial value:
=
flagDefinitions.add("flag_unweightedBad", "Both weighted and unweighted moments were invalid")

Definition at line 156 of file SdssShape.h.


The documentation for this class was generated from the following files:
schema
table::Schema schema
Definition: Amplifier.cc:115
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::afw::image::LOCAL
@ LOCAL
Definition: ImageBase.h:94
lsst::geom::PI
constexpr double PI
The ratio of a circle's circumference to diameter.
Definition: Angle.h:39
lsst::meas::base::FlagDefinition::number
std::size_t number
Definition: FlagHandler.h:54
lsst::log.log.logContinued.error
def error(fmt, *args)
Definition: logContinued.py:210
lsst::meas::base::FlagHandler::handleFailure
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=nullptr) const
Handle an expected or unexpected Exception thrown by a measurement algorithm.
Definition: FlagHandler.cc:76
psf
Key< int > psf
Definition: Exposure.cc:65
pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw::table._match.first
first
Definition: _match.py:76
std::tuple
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::meas::base::SdssShapeResultKey::setPsfShape
virtual void setPsfShape(afw::table::BaseRecord &record, afw::geom::ellipses::Quadrupole const &value) const
Set a Quadrupole for the Psf at the position of the given record.
Definition: SdssShape.cc:744
std::sqrt
T sqrt(T... args)
lsst::meas::base::SdssShapeAlgorithm::computeAdaptiveMoments
static Result computeAdaptiveMoments(ImageT const &image, geom::Point2D const &position, bool negative=false, Control const &ctrl=Control())
Compute the adaptive Gaussian-weighted moments of an image.
lsst::meas::base::SdssShapeAlgorithm::SHIFT
static FlagDefinition const SHIFT
Definition: SdssShape.h:158
lsst::meas::base::SingleFrameAlgorithm::measureN
virtual void measureN(afw::table::SourceCatalog const &measCat, afw::image::Exposure< float > const &exposure) const
Called to simultaneously measure all children in a deblend family, in a single image.
Definition: Algorithm.cc:31
lsst::meas::base::SdssShapeControl::doMeasurePsf
bool doMeasurePsf
"Whether to also compute the shape of the PSF model" ;
Definition: SdssShape.h:59
lsst::meas::base::SingleFrameAlgorithm::measure
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const =0
Called to measure a single child source in an image.
lsst::meas::base::SdssShapeAlgorithm::UNWEIGHTED
static FlagDefinition const UNWEIGHTED
Definition: SdssShape.h:157
result
py::object result
Definition: _schema.cc:429
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::meas::base::SdssShapeResultKey::addFields
static SdssShapeResultKey addFields(afw::table::Schema &schema, std::string const &name, bool doMeasurePsf)
Add the appropriate fields to a Schema, and return a SdssShapeResultKey that manages them.
Definition: SdssShape.cc:646
lsst::meas::base::BaseAlgorithm::_logName
std::string _logName
Definition: Algorithm.h:69
lsst::meas::base::SdssShapeAlgorithm::PSF_SHAPE_BAD
static FlagDefinition const PSF_SHAPE_BAD
Definition: SdssShape.h:160
PTR
#define PTR(...)
Definition: base.h:41
lsst::geom::Point< double, 2 >
lsst::meas::base::SdssShapeAlgorithm::FAILURE
static FlagDefinition const FAILURE
Definition: SdssShape.h:155
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::geom::Extent2D
Extent< double, 2 > Extent2D
Definition: Extent.h:400
lsst::meas::base::SdssShapeResultKey::getFlagHandler
FlagHandler const & getFlagHandler() const
Definition: SdssShape.h:125
lsst::pex::exceptions::Exception
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
bbox
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117