LSSTApplications  21.0.0+75b29a8a7f,21.0.0+e70536a077,21.0.0-1-ga51b5d4+62c747d40b,21.0.0-11-ga6ea59e8e+47cba9fc36,21.0.0-2-g103fe59+914993bf7c,21.0.0-2-g1367e85+e2614ded12,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g4bc9b9f+7b2b5f8678,21.0.0-2-g5242d73+e2614ded12,21.0.0-2-g54e2caa+6403186824,21.0.0-2-g7f82c8f+3ac4acbffc,21.0.0-2-g8dde007+04a6aea1af,21.0.0-2-g8f08a60+9402881886,21.0.0-2-ga326454+3ac4acbffc,21.0.0-2-ga63a54e+81dd751046,21.0.0-2-gc738bc1+5f65c6e7a9,21.0.0-2-gde069b7+26c92b3210,21.0.0-2-gecfae73+0993ddc9bd,21.0.0-2-gfc62afb+e2614ded12,21.0.0-21-gba890a8+5a4f502a26,21.0.0-23-g9966ff26+03098d1af8,21.0.0-3-g357aad2+8ad216c477,21.0.0-3-g4be5c26+e2614ded12,21.0.0-3-g6d51c4a+4d2fe0280d,21.0.0-3-g7d9da8d+75b29a8a7f,21.0.0-3-gaa929c8+522e0f12c2,21.0.0-3-ge02ed75+4d2fe0280d,21.0.0-4-g3300ddd+e70536a077,21.0.0-4-gc004bbf+eac6615e82,21.0.0-4-gccdca77+f94adcd104,21.0.0-4-gd1c1571+18b81799f9,21.0.0-5-g7b47fff+4d2fe0280d,21.0.0-5-gb155db7+d2632f662b,21.0.0-5-gdf36809+637e4641ee,21.0.0-6-g722ad07+28c848f42a,21.0.0-7-g959bb79+522e0f12c2,21.0.0-7-gfd72ab2+cf01990774,21.0.0-9-g87fb7b8d+e2ab11cdd6,w.2021.04
LSSTDataManagementBasePackage
PsfFlux.cc
Go to the documentation of this file.
1 
2 // -*- lsst-c++ -*-
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2015 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #include <array>
26 #include <cmath>
27 
28 #include "ndarray/eigen.h"
29 
30 #include "lsst/afw/table/Source.h"
31 #include "lsst/afw/detection/Psf.h"
32 #include "lsst/log/Log.h"
33 #include "lsst/afw/geom/SpanSet.h"
34 #include "lsst/meas/base/PsfFlux.h"
35 
36 namespace lsst {
37 namespace meas {
38 namespace base {
39 namespace {
40 FlagDefinitionList flagDefinitions;
41 } // namespace
42 
43 FlagDefinition const PsfFluxAlgorithm::FAILURE = flagDefinitions.addFailureFlag();
44 FlagDefinition const PsfFluxAlgorithm::NO_GOOD_PIXELS =
45  flagDefinitions.add("flag_noGoodPixels", "not enough non-rejected pixels in data to attempt the fit");
46 FlagDefinition const PsfFluxAlgorithm::EDGE = flagDefinitions.add(
47  "flag_edge", "object was too close to the edge of the image to use the full PSF model");
48 
49 FlagDefinitionList const& PsfFluxAlgorithm::getFlagDefinitions() { return flagDefinitions; }
50 
51 namespace {} // namespace
52 
54  std::string const& logName)
55  : _ctrl(ctrl),
56  _instFluxResultKey(FluxResultKey::addFields(
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  _centroidExtractor(schema, name) {
60  _logName = logName.size() ? logName : name;
62 }
63 
65  afw::image::Exposure<float> const& exposure) const {
66  PTR(afw::detection::Psf const) psf = exposure.getPsf();
67  if (!psf) {
68  LOGL_ERROR(getLogName(), "PsfFlux: no psf attached to exposure");
69  throw LSST_EXCEPT(FatalAlgorithmError, "PsfFlux algorithm requires a Psf with every exposure");
70  }
71  geom::Point2D position = _centroidExtractor(measRecord, _flagHandler);
72  PTR(afw::detection::Psf::Image) psfImage = psf->computeImage(position);
73  geom::Box2I fitBBox = psfImage->getBBox();
74  fitBBox.clip(exposure.getBBox());
75  if (fitBBox != psfImage->getBBox()) {
76  _flagHandler.setValue(measRecord, FAILURE.number,
77  true); // if we had a suspect flag, we'd set that instead
78  _flagHandler.setValue(measRecord, EDGE.number, true);
79  }
80  auto fitRegionSpans = std::make_shared<afw::geom::SpanSet>(fitBBox);
81  afw::detection::Footprint fitRegion(fitRegionSpans);
82  if (!_ctrl.badMaskPlanes.empty()) {
83  afw::image::MaskPixel badBits = 0x0;
85  i != _ctrl.badMaskPlanes.end(); ++i) {
86  badBits |= exposure.getMaskedImage().getMask()->getPlaneBitMask(*i);
87  }
88  fitRegion.setSpans(fitRegion.getSpans()
89  ->intersectNot(*exposure.getMaskedImage().getMask(), badBits)
90  ->clippedTo(exposure.getMaskedImage().getMask()->getBBox()));
91  }
92  if (fitRegion.getArea() == 0) {
94  }
95  typedef afw::detection::Psf::Pixel PsfPixel;
96  // SpanSet::flatten returns a new ndarray::Array, which must stay in scope
97  // while we use an Eigen::Map view of it
98  auto modelNdArray = fitRegion.getSpans()->flatten(psfImage->getArray(), psfImage->getXY0());
99  auto dataNdArray = fitRegion.getSpans()->flatten(exposure.getMaskedImage().getImage()->getArray(),
100  exposure.getXY0());
101  auto varianceNdArray = fitRegion.getSpans()->flatten(exposure.getMaskedImage().getVariance()->getArray(),
102  exposure.getXY0());
103  auto model = ndarray::asEigenMatrix(modelNdArray);
104  auto data = ndarray::asEigenMatrix(dataNdArray);
105  auto variance = ndarray::asEigenMatrix(varianceNdArray);
106  PsfPixel alpha = model.squaredNorm();
108  result.instFlux = model.dot(data.cast<PsfPixel>()) / alpha;
109  // If we're not using per-pixel weights to compute the instFlux, we'll still want to compute the
110  // variance as if we had, so we'll apply the weights to the model now, and update alpha.
111  result.instFluxErr = std::sqrt(model.array().square().matrix().dot(variance.cast<PsfPixel>())) / alpha;
112  measRecord.set(_areaKey, model.sum() / alpha);
113  if (!std::isfinite(result.instFlux) || !std::isfinite(result.instFluxErr)) {
114  throw LSST_EXCEPT(PixelValueError, "Invalid pixel value detected in image.");
115  }
116  measRecord.set(_instFluxResultKey, result);
117 }
118 
120  _flagHandler.handleFailure(measRecord, error);
121 }
122 
126  for (std::size_t i = 0; i < PsfFluxAlgorithm::getFlagDefinitions().size(); i++) {
128  if (flag == PsfFluxAlgorithm::FAILURE) continue;
129  if (mapper.getInputSchema().getNames().count(mapper.getInputSchema().join(name, flag.name)) == 0)
130  continue;
132  mapper.getInputSchema().find<afw::table::Flag>(name + "_" + flag.name).key;
133  mapper.addMapping(key);
134  }
135 }
136 
137 } // namespace base
138 } // namespace meas
139 } // namespace lsst
lsst::afw::detection::Psf::Pixel
math::Kernel::Pixel Pixel
Pixel type of Image returned by computeImage.
Definition: Psf.h:82
lsst::afw::image::Exposure::getPsf
std::shared_ptr< lsst::afw::detection::Psf const > getPsf() const
Return the Exposure's Psf object.
Definition: Exposure.h:322
lsst::meas::base::PsfFluxControl::badMaskPlanes
std::vector< std::string > badMaskPlanes
"Mask planes that indicate pixels that should be excluded from the fit" ;
Definition: PsfFlux.h:51
lsst::meas::base::FlagDefinition::number
std::size_t number
Definition: FlagHandler.h:54
std::string
STL class.
Psf.h
lsst::meas::base::FluxResult
A reusable result struct for instFlux measurements.
Definition: FluxUtilities.h:41
lsst::log.log.logContinued.error
def error(fmt, *args)
Definition: logContinued.py:213
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
lsst::afw::table::SourceRecord
Record class that contains measurements made on a single exposure.
Definition: Source.h:80
lsst::afw::image::Exposure< float >
lsst::meas::base::PsfFluxAlgorithm::PsfFluxAlgorithm
PsfFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema, std::string const &logName="")
Definition: PsfFlux.cc:53
std::vector
STL class.
std::string::size
T size(T... args)
psf
Key< int > psf
Definition: Exposure.cc:65
lsst::meas::base::FlagDefinition::doc
std::string doc
Definition: FlagHandler.h:53
lsst::meas::base::PsfFluxControl
A C++ control class to handle PsfFluxAlgorithm's configuration.
Definition: PsfFlux.h:48
lsst::meas::base::PsfFluxAlgorithm::getFlagDefinitions
static FlagDefinitionList const & getFlagDefinitions()
Definition: PsfFlux.cc:49
lsst::afw::detection::Footprint::getSpans
std::shared_ptr< geom::SpanSet > getSpans() const
Return a shared pointer to the SpanSet.
Definition: Footprint.h:115
lsst::meas::base::PsfFluxTransform::PsfFluxTransform
PsfFluxTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
Definition: PsfFlux.cc:123
lsst::meas::base::MeasurementError
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
lsst::meas::base::FatalAlgorithmError
Exception to be thrown when a measurement algorithm experiences a fatal error.
Definition: exceptions.h:76
lsst::afw::table::Schema
Defines the fields and offsets for a table.
Definition: Schema.h:50
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::meas::base::FlagDefinition::name
std::string name
Definition: FlagHandler.h:52
lsst::meas::base::FluxResultKey
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:59
lsst::afw::image::Exposure::getMaskedImage
MaskedImageT getMaskedImage()
Return the MaskedImage.
Definition: Exposure.h:228
std::sqrt
T sqrt(T... args)
lsst::meas::base::FlagDefinitionList
vector-type utility class to build a collection of FlagDefinitions
Definition: FlagHandler.h:60
lsst::meas::base::PsfFluxAlgorithm::FAILURE
static FlagDefinition const FAILURE
Definition: PsfFlux.h:73
schema
table::Schema schema
Definition: python.h:134
lsst::meas::base::FlagHandler::setValue
void setValue(afw::table::BaseRecord &record, std::size_t i, bool value) const
Set the flag field corresponding to the given flag index.
Definition: FlagHandler.h:262
data
char * data
Definition: BaseRecord.cc:62
lsst::afw::image::Mask::getPlaneBitMask
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR'd together.
Definition: Mask.cc:379
std::isfinite
T isfinite(T... args)
LOGL_ERROR
#define LOGL_ERROR(logger, message...)
Log a error-level message using a varargs/printf style interface.
Definition: Log.h:552
lsst::meas::base::PsfFluxAlgorithm::NO_GOOD_PIXELS
static FlagDefinition const NO_GOOD_PIXELS
Definition: PsfFlux.h:74
lsst::afw::detection::Footprint::setSpans
void setSpans(std::shared_ptr< geom::SpanSet > otherSpanSet)
Sets the shared pointer to the SpanSet in the Footprint.
Definition: Footprint.cc:45
lsst::afw::table::SchemaMapper
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
lsst::afw::image::Exposure::getBBox
lsst::geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
Definition: Exposure.h:277
lsst::afw::table::Key< afw::table::Flag >
lsst::meas::base::PsfFluxAlgorithm::fail
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.
Definition: PsfFlux.cc:119
lsst::afw::image::ImageBase::getArray
Array getArray()
Definition: ImageBase.h:474
lsst::meas::base::FluxTransform
Base for instFlux measurement transformations.
Definition: FluxUtilities.h:187
PTR
#define PTR(...)
Definition: base.h:41
std::int32_t
result
py::object result
Definition: _schema.cc:429
Source.h
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::image::MaskedImage::getVariance
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
Definition: MaskedImage.h:1079
SpanSet.h
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::meas::base::BaseAlgorithm::getLogName
std::string getLogName() const
Definition: Algorithm.h:66
lsst::afw::detection::Footprint::getArea
std::size_t getArea() const
Return the number of pixels in this Footprint.
Definition: Footprint.h:173
lsst::meas::base::FlagDefinitionList::size
std::size_t size() const
return the current size (number of defined elements) of the collection
Definition: FlagHandler.h:125
lsst::meas::base::FlagDefinition
Simple class used to define and document flags The name and doc constitute the identity of the FlagDe...
Definition: FlagHandler.h:40
variance
afw::table::Key< afw::table::Array< VariancePixelT > > variance
Definition: HeavyFootprint.cc:218
lsst::geom::Box2I::clip
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:189
lsst::meas::base::BaseAlgorithm::_logName
std::string _logName
Definition: Algorithm.h:69
std::vector::begin
T begin(T... args)
key
Key< U > key
Definition: Schema.cc:281
PsfFlux.h
lsst::geom::Point< double, 2 >
lsst::meas::base::PixelValueError
Exception to be thrown when a measurement algorithm encounters a NaN or infinite pixel.
Definition: exceptions.h:83
lsst::afw::image::Exposure::getXY0
lsst::geom::Point2I getXY0() const
Return the Exposure's origin.
Definition: Exposure.h:275
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
mapper
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
lsst::meas::base::PsfFluxAlgorithm::EDGE
static FlagDefinition const EDGE
Definition: PsfFlux.h:75
std::vector::empty
T empty(T... args)
lsst::afw::image::MaskedImage::getImage
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
Definition: MaskedImage.h:1046
std::size_t
std::vector::end
T end(T... args)
lsst::afw::detection::Psf
A polymorphic base class for representing an image's Point Spread Function.
Definition: Psf.h:76
lsst::afw::table::BaseRecord::set
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:164
lsst::afw::image::ImageBase::getBBox
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:443
lsst::afw::detection::Footprint
Class to describe the properties of a detected object from an image.
Definition: Footprint.h:63
lsst::afw::image::Image< Pixel >
lsst::afw::image::MaskedImage::getMask
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
Definition: MaskedImage.h:1058
Log.h
LSST DM logging module built on log4cxx.
lsst::meas::base::FlagHandler::addFields
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.
Definition: FlagHandler.cc:37
lsst::meas::base::PsfFluxAlgorithm::measure
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
Definition: PsfFlux.cc:64