28#include "ndarray/eigen.h"
45 flagDefinitions.add(
"flag_noGoodPixels",
"not enough non-rejected pixels in data to attempt the fit");
47 "flag_edge",
"object was too close to the edge of the image to use the full PSF model");
57 schema, name,
"instFlux derived from linear least-squares fit of PSF model")),
58 _areaKey(schema.addField<float>(name +
"_area",
"effective area of PSF",
"pixel")),
59 _chi2Key(schema.addField<float>(name +
"_chi2",
"chi2 of the fitted PSF")),
60 _npixelsKey(schema.addField<int>(name +
"_npixels",
61 "number of pixels that were included in the PSF fit",
"pixel")),
62 _centroidExtractor(schema, name) {
74 geom::Point2D position = _centroidExtractor(measRecord, _flagHandler);
78 if (fitBBox != psfImage->getBBox()) {
79 _flagHandler.setValue(measRecord,
FAILURE.number,
81 _flagHandler.setValue(measRecord,
EDGE.number,
true);
85 if (!_ctrl.badMaskPlanes.empty()) {
87 for (std::vector<std::string>::const_iterator i = _ctrl.badMaskPlanes.begin();
88 i != _ctrl.badMaskPlanes.end(); ++i) {
97 _flagHandler.setValue(measRecord,
FAILURE.number,
true);
103 auto modelNdArray = fitRegion.
getSpans()->flatten(psfImage->getArray(), psfImage->getXY0());
108 auto model = ndarray::asEigenMatrix(modelNdArray);
109 auto data = ndarray::asEigenMatrix(dataNdArray);
110 auto variance = ndarray::asEigenMatrix(varianceNdArray);
111 PsfPixel alpha = model.squaredNorm();
113 result.
instFlux = model.dot(data.cast<PsfPixel>()) / alpha;
116 result.
instFluxErr =
std::sqrt(model.array().square().matrix().dot(variance.cast<PsfPixel>())) / alpha;
117 measRecord.
set(_areaKey,
std::pow(model.sum(), 2) / alpha);
118 measRecord.
set(_npixelsKey, fitRegion.
getSpans()->getArea());
122 measRecord.
set(_instFluxResultKey, result);
123 auto chi2 = ((data.cast<PsfPixel>() - result.
instFlux * model).array().square() /
124 variance.cast<PsfPixel>().array())
126 measRecord.
set(_chi2Key, chi2);
130 _flagHandler.handleFailure(measRecord, error);
#define LOGL_ERROR(logger, message...)
#define LSST_EXCEPT(type,...)
math::Kernel::Pixel Pixel
MaskedImageT getMaskedImage()
std::shared_ptr< lsst::afw::detection::Psf const > getPsf() const
lsst::geom::Point2I getXY0() const
lsst::geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
VariancePtr getVariance() const
ImagePtr getImage() const
void set(Key< T > const &key, U const &value)
std::string join(std::string const &a, std::string const &b) const
std::set< std::string > getNames(bool topOnly=false) const
SchemaItem< T > find(std::string const &name) const
Key< T > addMapping(Key< T > const &inputKey, bool doReplace=false)
Schema const getInputSchema() const
void clip(Box2I const &other) noexcept
std::string getLogName() const
Exception to be thrown when a measurement algorithm experiences a fatal error.
vector-type utility class to build a collection of FlagDefinitions
std::size_t size() const
return the current size (number of defined elements) of the collection
static FlagHandler addFields(afw::table::Schema &schema, std::string const &prefix, FlagDefinitionList const &flagDefs, FlagDefinitionList const &exclDefs=FlagDefinitionList::getEmptyList())
Add Flag fields to a schema, creating a FlagHandler object to manage them.
A FunctorKey for FluxResult.
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Exception to be thrown when a measurement algorithm encounters a NaN or infinite pixel.
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
PsfFluxControl Control
A typedef to the Control object for this algorithm, defined above.
static FlagDefinition const FAILURE
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.
static FlagDefinition const NO_GOOD_PIXELS
static FlagDefinition const EDGE
static FlagDefinitionList const & getFlagDefinitions()
PsfFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema, std::string const &logName="")
Point< double, 2 > Point2D
Simple class used to define and document flags The name and doc constitute the identity of the FlagDe...
A reusable result struct for instFlux measurements.
meas::base::Flux instFlux
Measured instFlux in DN.
meas::base::FluxErrElement instFluxErr
Standard deviation of instFlux in DN.