LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
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 746 of file SdssShape.cc.

748  : _ctrl(ctrl),
749  _resultKey(ResultKey::addFields(schema, name, ctrl.doMeasurePsf)),
750  _centroidExtractor(schema, name) {}
table::Key< std::string > name
Definition: Amplifier.cc:116
table::Schema schema
Definition: python.h:134
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:628

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 753 of file SdssShape.cc.

754  {
755  double xcen = center.getX(); // object's column position
756  double ycen = center.getY(); // object's row position
757 
758  xcen -= image.getX0(); // work in image Pixel coordinates
759  ycen -= image.getY0();
760 
761  float shiftmax = control.maxShift; // Max allowed centroid shift
762  if (shiftmax < 2) {
763  shiftmax = 2;
764  } else if (shiftmax > 10) {
765  shiftmax = 10;
766  }
767 
768  SdssShapeResult result;
769  try {
770  result.flags[FAILURE.number] =
771  !getAdaptiveMoments(image, control.background, xcen, ycen, shiftmax, &result, control.maxIter,
772  control.tol1, control.tol2, negative);
773  } catch (pex::exceptions::Exception &err) {
774  result.flags[FAILURE.number] = true;
775  }
776  if (result.flags[UNWEIGHTED.number] || result.flags[SHIFT.number]) {
777  // These are also considered fatal errors in terms of the quality of the results,
778  // even though they do produce some results.
779  result.flags[FAILURE.number] = true;
780  }
781  double IxxIyy = result.getQuadrupole().getIxx() * result.getQuadrupole().getIyy();
782  double Ixy_sq = result.getQuadrupole().getIxy();
783  Ixy_sq *= Ixy_sq;
784  double epsilon = 1.0e-6;
785  if (IxxIyy < (1.0 + epsilon) * Ixy_sq)
786  // We are checking that Ixx*Iyy > (1 + epsilon)*Ixy*Ixy where epsilon is suitably small. The
787  // value of epsilon used here is a magic number subject to future review (DM-5801 was marked won't fix).
788  {
789  if (!result.flags[FAILURE.number]) {
790  throw LSST_EXCEPT(pex::exceptions::LogicError,
791  (boost::format("computeAdaptiveMoments IxxIxy %d < (1 + eps=%d)*(Ixy^2=%d);"
792  " implying singular moments without any flag set")
793  % IxxIyy % epsilon % Ixy_sq).str());
794  }
795  }
796 
797  // getAdaptiveMoments() just computes the zeroth moment in result.instFlux (and its error in
798  // result.instFluxErr, result.instFlux_xx_Cov, etc.) That's related to the instFlux by some geometric
799  // factors, which we apply here.
800  // This scale factor is just the inverse of the normalization constant in a bivariate normal distribution:
801  // 2*pi*sigma_x*sigma_y*sqrt(1-rho^2), where rho is the correlation.
802  // This happens to be twice the ellipse area pi*sigma_maj*sigma_min, which is identically equal to:
803  // pi*sqrt(I_xx*I_yy - I_xy^2); i.e. pi*sqrt(determinant(I))
804  double instFluxScale = geom::TWOPI * std::sqrt(IxxIyy - Ixy_sq);
805 
806  result.instFlux *= instFluxScale;
807  result.instFluxErr *= instFluxScale;
808  result.x += image.getX0();
809  result.y += image.getY0();
810 
811  if (ImageAdaptor<ImageT>::hasVariance) {
812  result.instFlux_xx_Cov *= instFluxScale;
813  result.instFlux_yy_Cov *= instFluxScale;
814  result.instFlux_xy_Cov *= instFluxScale;
815  }
816 
817  return result;
818 }
py::object result
Definition: _schema.cc:429
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
static FlagDefinition const SHIFT
Definition: SdssShape.h:158
static FlagDefinition const FAILURE
Definition: SdssShape.h:155
static FlagDefinition const UNWEIGHTED
Definition: SdssShape.h:157
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
constexpr double TWOPI
Definition: Angle.h:40
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
T sqrt(T... args)

◆ 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 821 of file SdssShape.cc.

823  {
824  // while arguments to computeFixedMomentsFlux are in PARENT coordinates, the implementation is LOCAL.
825  geom::Point2D localCenter = center - geom::Extent2D(image.getXY0());
826 
827  geom::BoxI const bbox = computeAdaptiveMomentsBBox(image.getBBox(afw::image::LOCAL), localCenter,
828  shape.getIxx(), shape.getIxy(), shape.getIyy());
829 
830  std::tuple<std::pair<bool, double>, double, double, double> weights =
831  getWeights(shape.getIxx(), shape.getIxy(), shape.getIyy());
832 
833  FluxResult result;
834 
835  if (!std::get<0>(weights).first) {
836  throw pex::exceptions::InvalidParameterError("Input shape is singular");
837  }
838 
839  double const w11 = std::get<1>(weights);
840  double const w12 = std::get<2>(weights);
841  double const w22 = std::get<3>(weights);
842  bool const interp = shouldInterp(shape.getIxx(), shape.getIyy(), std::get<0>(weights).second);
843 
844  double sum0 = 0; // sum of pixel values weighted by a Gaussian
845  if (calcmom<true>(ImageAdaptor<ImageT>().getImage(image), localCenter.getX(), localCenter.getY(), bbox,
846  0.0, interp, w11, w12, w22, NULL, &sum0, NULL, NULL, NULL, NULL, NULL, NULL) < 0) {
847  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Error from calcmom");
848  }
849 
850  result.instFlux = sum0 * 2.0;
851 
852  if (ImageAdaptor<ImageT>::hasVariance) {
853  int ix = static_cast<int>(center.getX() - image.getX0());
854  int iy = static_cast<int>(center.getY() - image.getY0());
855  if (!image.getBBox(afw::image::LOCAL).contains(geom::Point2I(ix, iy))) {
856  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
857  (boost::format("Center (%d,%d) not in image (%dx%d)") % ix % iy %
858  image.getWidth() % image.getHeight())
859  .str());
860  }
861  double var = ImageAdaptor<ImageT>().getVariance(image, ix, iy);
862  // 0th moment (i0) error = sqrt(var / wArea); instFlux (error) = 2 * wArea * i0 (error)
863  double const wArea = geom::PI * std::sqrt(shape.getDeterminant());
864  result.instFluxErr = 2 * std::sqrt(var * wArea);
865  }
866 
867  return result;
868 }
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
An integer coordinate rectangle.
Definition: Box.h:55
constexpr double PI
The ratio of a circle's circumference to diameter.
Definition: Angle.h:39
Extent< double, 2 > Extent2D
Definition: Extent.h:400

◆ 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 905 of file SdssShape.cc.

905  {
906  _resultKey.getFlagHandler().handleFailure(measRecord, error);
907 }
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
FlagHandler const & getFlagHandler() const
Definition: SdssShape.h:125

◆ 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 870 of file SdssShape.cc.

871  {
872  bool negative = false;
873 
874  try {
875  negative = measRecord.get(measRecord.getSchema().find<afw::table::Flag>("flags_negative").key);
876  } catch (pexExcept::Exception &e) {
877  }
878  SdssShapeResult result = computeAdaptiveMoments(
879  exposure.getMaskedImage(), _centroidExtractor(measRecord, _resultKey.getFlagHandler()), negative,
880  _ctrl);
881 
882  if (_ctrl.doMeasurePsf) {
883  // Compute moments of Psf model. In the interest of implementing this quickly, we're just
884  // calling Psf::computeShape(), which delegates to SdssShapeResult::computeAdaptiveMoments
885  // for all nontrivial Psf classes. But this could in theory save the results of a shape
886  // computed some other way as part of base_SdssShape, which might be confusing. We should
887  // fix this eventually either by making Psf shape measurement not part of base_SdssShape, or
888  // by making the measurements stored with shape.sdss always computed via the
889  // SdssShapeAlgorithm instead of delegating to the Psf class.
890  try {
892  if (!psf) {
893  result.flags[PSF_SHAPE_BAD.number] = true;
894  } else {
895  _resultKey.setPsfShape(measRecord, psf->computeShape(geom::Point2D(result.x, result.y)));
896  }
897  } catch (pex::exceptions::Exception &err) {
898  result.flags[PSF_SHAPE_BAD.number] = true;
899  }
900  }
901 
902  measRecord.set(_resultKey, result);
903 }
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.
static FlagDefinition const PSF_SHAPE_BAD
Definition: SdssShape.h:160
bool doMeasurePsf
"Whether to also compute the shape of the PSF model" ;
Definition: SdssShape.h:59
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:726
Provides consistent interface for LSST exceptions.
Definition: Exception.h:107
Key< int > psf
Definition: Exposure.cc:65

◆ 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  }
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.

◆ 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  }
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

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: