LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
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
schema
table::Schema schema
Definition: Amplifier.cc:115
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:307
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
PTR
#define PTR(...)
Definition: base.h:41
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
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...)
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:273
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
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:271
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