LSST Applications g063fba187b+fee0456c91,g0f08755f38+ea96e5a5a3,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+90257ff92a,g20f6ffc8e0+ea96e5a5a3,g217e2c1bcf+937a289c59,g28da252d5a+daa7da44eb,g2bbee38e9b+253935c60e,g2bc492864f+253935c60e,g3156d2b45e+6e55a43351,g32e5bea42b+31359a2a7a,g347aa1857d+253935c60e,g35bb328faa+a8ce1bb630,g3a166c0a6a+253935c60e,g3b1af351f3+a8ce1bb630,g3e281a1b8c+c5dd892a6c,g414038480c+416496e02f,g41af890bb2+afe91b1188,g599934f4f4+0db33f7991,g7af13505b9+e36de7bce6,g80478fca09+da231ba887,g82479be7b0+a4516e59e3,g858d7b2824+ea96e5a5a3,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+bc6ab8dfbd,gb58c049af0+d64f4d3760,gc28159a63d+253935c60e,gcab2d0539d+3f2b72788c,gcf0d15dbbd+4ea9c45075,gda6a2b7d83+4ea9c45075,gdaeeff99f8+1711a396fd,ge79ae78c31+253935c60e,gef2f8181fd+3031e3cf99,gf0baf85859+c1f95f4921,gfa517265be+ea96e5a5a3,gfa999e8aa5+17cd334064,w.2024.50
LSST Data Management Base Package
Loading...
Searching...
No Matches
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.
 
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.
 
template<typename ImageT >
SdssShapeResult computeAdaptiveMoments (ImageT const &image, geom::Point2D const &center, bool negative, Control const &control)
 
void measureForced (afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure, afw::table::SourceRecord const &refRecord, afw::geom::SkyWcs const &refWcs) const override
 Called to measure a single child source in an image.
 
void measureNForced (afw::table::SourceCatalog const &measCat, afw::image::Exposure< float > const &exposure, afw::table::SourceCatalog const &refRecord, afw::geom::SkyWcs const &refWcs) const override
 Called to simultaneously measure all children in a deblend family, in a single image.
 
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.
 
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.
 
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.
 

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

761 : _ctrl(ctrl),
762 _resultKey(ResultKey::addFields(schema, name, ctrl.doMeasurePsf)),
763 _centroidExtractor(schema, name) {}
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:641

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

767 {
768 double xcen = center.getX(); // object's column position
769 double ycen = center.getY(); // object's row position
770
771 xcen -= image.getX0(); // work in image Pixel coordinates
772 ycen -= image.getY0();
773
774 float shiftmax = control.maxShift; // Max allowed centroid shift
775 if (shiftmax < 2) {
776 shiftmax = 2;
777 } else if (shiftmax > 10) {
778 shiftmax = 10;
779 }
780
781 SdssShapeResult result;
782 try {
783 result.flags[FAILURE.number] =
784 !getAdaptiveMoments(image, control.background, xcen, ycen, shiftmax, &result, control.maxIter,
785 control.tol1, control.tol2, negative);
786 } catch (pex::exceptions::Exception &err) {
787 result.flags[FAILURE.number] = true;
788 }
789 if (result.flags[UNWEIGHTED.number] || result.flags[SHIFT.number]) {
790 // These are also considered fatal errors in terms of the quality of the results,
791 // even though they do produce some results.
792 result.flags[FAILURE.number] = true;
793 }
794 double IxxIyy = result.getQuadrupole().getIxx() * result.getQuadrupole().getIyy();
795 double Ixy_sq = result.getQuadrupole().getIxy();
796 Ixy_sq *= Ixy_sq;
797 double epsilon = 1.0e-6;
798 if (IxxIyy < (1.0 + epsilon) * Ixy_sq)
799 // We are checking that Ixx*Iyy > (1 + epsilon)*Ixy*Ixy where epsilon is suitably small. The
800 // value of epsilon used here is a magic number subject to future review (DM-5801 was marked won't fix).
801 {
802 if (!result.flags[FAILURE.number]) {
803 throw LSST_EXCEPT(pex::exceptions::LogicError,
804 (boost::format("computeAdaptiveMoments IxxIxy %d < (1 + eps=%d)*(Ixy^2=%d);"
805 " implying singular moments without any flag set")
806 % IxxIyy % epsilon % Ixy_sq).str());
807 }
808 }
809
810 // getAdaptiveMoments() just computes the zeroth moment in result.instFlux (and its error in
811 // result.instFluxErr, result.instFlux_xx_Cov, etc.) That's related to the instFlux by some geometric
812 // factors, which we apply here.
813 // This scale factor is just the inverse of the normalization constant in a bivariate normal distribution:
814 // 2*pi*sigma_x*sigma_y*sqrt(1-rho^2), where rho is the correlation.
815 // This happens to be twice the ellipse area pi*sigma_maj*sigma_min, which is identically equal to:
816 // pi*sqrt(I_xx*I_yy - I_xy^2); i.e. pi*sqrt(determinant(I))
817 double instFluxScale = geom::TWOPI * std::sqrt(IxxIyy - Ixy_sq);
818
819 result.instFlux *= instFluxScale;
820 result.instFluxErr *= instFluxScale;
821 result.x += image.getX0();
822 result.y += image.getY0();
823
824 if (ImageAdaptor<ImageT>::hasVariance) {
825 result.instFlux_xx_Cov *= instFluxScale;
826 result.instFlux_yy_Cov *= instFluxScale;
827 result.instFlux_xy_Cov *= instFluxScale;
828 }
829
830 return result;
831}
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
double constexpr TWOPI
Definition Angle.h:41
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 834 of file SdssShape.cc.

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

◆ 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}
void handleFailure(afw::table::BaseRecord &record, MeasurementError const *error=nullptr) const
Handle an expected or unexpected Exception thrown by a measurement algorithm.
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 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 {
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}
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:739
Provides consistent interface for LSST exceptions.
Definition Exception.h:107
Key< int > psf
Definition Exposure.cc:65

◆ measureForced()

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
inlineoverridevirtualinherited

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()

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
inlineoverridevirtualinherited

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: