LSST Applications g070148d5b3+33e5256705,g0d53e28543+25c8b88941,g0da5cf3356+2dd1178308,g1081da9e2a+62d12e78cb,g17e5ecfddb+7e422d6136,g1c76d35bf8+ede3a706f7,g295839609d+225697d880,g2e2c1a68ba+cc1f6f037e,g2ffcdf413f+853cd4dcde,g38293774b4+62d12e78cb,g3b44f30a73+d953f1ac34,g48ccf36440+885b902d19,g4b2f1765b6+7dedbde6d2,g5320a0a9f6+0c5d6105b6,g56b687f8c9+ede3a706f7,g5c4744a4d9+ef6ac23297,g5ffd174ac0+0c5d6105b6,g6075d09f38+66af417445,g667d525e37+2ced63db88,g670421136f+2ced63db88,g71f27ac40c+2ced63db88,g774830318a+463cbe8d1f,g7876bc68e5+1d137996f1,g7985c39107+62d12e78cb,g7fdac2220c+0fd8241c05,g96f01af41f+368e6903a7,g9ca82378b8+2ced63db88,g9d27549199+ef6ac23297,gabe93b2c52+e3573e3735,gb065e2a02a+3dfbe639da,gbc3249ced9+0c5d6105b6,gbec6a3398f+0c5d6105b6,gc9534b9d65+35b9f25267,gd01420fc67+0c5d6105b6,geee7ff78d7+a14128c129,gf63283c776+ede3a706f7,gfed783d017+0c5d6105b6,w.2022.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
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
32#include "lsst/log/Log.h"
35
36namespace lsst {
37namespace meas {
38namespace base {
39namespace {
40FlagDefinitionList flagDefinitions;
41} // namespace
42
43FlagDefinition const PsfFluxAlgorithm::FAILURE = flagDefinitions.addFailureFlag();
44FlagDefinition const PsfFluxAlgorithm::NO_GOOD_PIXELS =
45 flagDefinitions.add("flag_noGoodPixels", "not enough non-rejected pixels in data to attempt the fit");
46FlagDefinition 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
49FlagDefinitionList const& PsfFluxAlgorithm::getFlagDefinitions() { return flagDefinitions; }
50
51namespace {} // 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 _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) {
63 _logName = logName.size() ? logName : name;
65}
66
68 afw::image::Exposure<float> const& exposure) const {
70 if (!psf) {
71 LOGL_ERROR(getLogName(), "PsfFlux: no psf attached to exposure");
72 throw LSST_EXCEPT(FatalAlgorithmError, "PsfFlux algorithm requires a Psf with every exposure");
73 }
74 geom::Point2D position = _centroidExtractor(measRecord, _flagHandler);
75 std::shared_ptr<afw::detection::Psf::Image> psfImage = psf->computeImage(position);
76 geom::Box2I fitBBox = psfImage->getBBox();
77 fitBBox.clip(exposure.getBBox());
78 if (fitBBox != psfImage->getBBox()) {
79 _flagHandler.setValue(measRecord, FAILURE.number,
80 true); // if we had a suspect flag, we'd set that instead
81 _flagHandler.setValue(measRecord, EDGE.number, true);
82 }
83 auto fitRegionSpans = std::make_shared<afw::geom::SpanSet>(fitBBox);
84 afw::detection::Footprint fitRegion(fitRegionSpans);
85 if (!_ctrl.badMaskPlanes.empty()) {
86 afw::image::MaskPixel badBits = 0x0;
88 i != _ctrl.badMaskPlanes.end(); ++i) {
89 badBits |= exposure.getMaskedImage().getMask()->getPlaneBitMask(*i);
90 }
91 fitRegion.setSpans(fitRegion.getSpans()
92 ->intersectNot(*exposure.getMaskedImage().getMask(), badBits)
93 ->clippedTo(exposure.getMaskedImage().getMask()->getBBox()));
94 }
95 if (fitRegion.getArea() == 0) {
96 _flagHandler.setValue(measRecord, NO_GOOD_PIXELS.number, true);
97 _flagHandler.setValue(measRecord, FAILURE.number, true);
98 return;
99 }
100 typedef afw::detection::Psf::Pixel PsfPixel;
101 // SpanSet::flatten returns a new ndarray::Array, which must stay in scope
102 // while we use an Eigen::Map view of it
103 auto modelNdArray = fitRegion.getSpans()->flatten(psfImage->getArray(), psfImage->getXY0());
104 auto dataNdArray = fitRegion.getSpans()->flatten(exposure.getMaskedImage().getImage()->getArray(),
105 exposure.getXY0());
106 auto varianceNdArray = fitRegion.getSpans()->flatten(exposure.getMaskedImage().getVariance()->getArray(),
107 exposure.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;
114 // If we're not using per-pixel weights to compute the instFlux, we'll still want to compute the
115 // variance as if we had, so we'll apply the weights to the model now, and update alpha.
116 result.instFluxErr = std::sqrt(model.array().square().matrix().dot(variance.cast<PsfPixel>())) / alpha;
117 measRecord.set(_areaKey, model.sum() / alpha);
118 measRecord.set(_npixelsKey, fitRegion.getSpans()->getArea());
119 if (!std::isfinite(result.instFlux) || !std::isfinite(result.instFluxErr)) {
120 throw LSST_EXCEPT(PixelValueError, "Invalid pixel value detected in image.");
121 }
122 measRecord.set(_instFluxResultKey, result);
123 auto chi2 = ((data.cast<PsfPixel>() - result.instFlux * model).array().square() /
124 variance.cast<PsfPixel>().array())
125 .sum();
126 measRecord.set(_chi2Key, chi2);
127}
128
130 _flagHandler.handleFailure(measRecord, error);
131}
132
136 for (std::size_t i = 0; i < PsfFluxAlgorithm::getFlagDefinitions().size(); i++) {
138 if (flag == PsfFluxAlgorithm::FAILURE) continue;
139 if (mapper.getInputSchema().getNames().count(mapper.getInputSchema().join(name, flag.name)) == 0)
140 continue;
142 mapper.getInputSchema().find<afw::table::Flag>(name + "_" + flag.name).key;
143 mapper.addMapping(key);
144 }
145}
146
147} // namespace base
148} // namespace meas
149} // namespace lsst
py::object result
Definition: _schema.cc:429
table::Key< std::string > name
Definition: Amplifier.cc:116
char * data
Definition: BaseRecord.cc:61
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
afw::table::Key< afw::table::Array< VariancePixelT > > variance
LSST DM logging module built on log4cxx.
#define LOGL_ERROR(logger, message...)
Log a error-level message using a varargs/printf style interface.
Definition: Log.h:563
SchemaMapper * mapper
Definition: SchemaMapper.cc:71
table::Schema schema
Definition: python.h:134
T begin(T... args)
Class to describe the properties of a detected object from an image.
Definition: Footprint.h:63
std::shared_ptr< geom::SpanSet > getSpans() const
Return a shared pointer to the SpanSet.
Definition: Footprint.h:115
std::size_t getArea() const
Return the number of pixels in this Footprint.
Definition: Footprint.h:173
void setSpans(std::shared_ptr< geom::SpanSet > otherSpanSet)
Sets the shared pointer to the SpanSet in the Footprint.
Definition: Footprint.cc:45
math::Kernel::Pixel Pixel
Pixel type of Image returned by computeImage.
Definition: Psf.h:82
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Definition: Exposure.h:72
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition: BaseRecord.h:164
A class used as a handle to a particular field in a table.
Definition: Key.h:53
Defines the fields and offsets for a table.
Definition: Schema.h:51
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
Record class that contains measurements made on a single exposure.
Definition: Source.h:78
An integer coordinate rectangle.
Definition: Box.h:55
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:189
std::string getLogName() const
Definition: Algorithm.h:66
Exception to be thrown when a measurement algorithm experiences a fatal error.
Definition: exceptions.h:76
vector-type utility class to build a collection of FlagDefinitions
Definition: FlagHandler.h:60
std::size_t size() const
return the current size (number of defined elements) of the collection
Definition: FlagHandler.h:125
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
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
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
A FunctorKey for FluxResult.
Definition: FluxUtilities.h:59
Base for instFlux measurement transformations.
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
Exception to be thrown when a measurement algorithm encounters a NaN or infinite pixel.
Definition: exceptions.h:83
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:67
static FlagDefinition const FAILURE
Definition: PsfFlux.h:73
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:129
static FlagDefinition const NO_GOOD_PIXELS
Definition: PsfFlux.h:74
static FlagDefinition const EDGE
Definition: PsfFlux.h:75
static FlagDefinitionList const & getFlagDefinitions()
Definition: PsfFlux.cc:49
PsfFluxAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema, std::string const &logName="")
Definition: PsfFlux.cc:53
A C++ control class to handle PsfFluxAlgorithm's configuration.
Definition: PsfFlux.h:48
std::vector< std::string > badMaskPlanes
"Mask planes that indicate pixels that should be excluded from the fit" ;
Definition: PsfFlux.h:51
PsfFluxTransform(Control const &ctrl, std::string const &name, afw::table::SchemaMapper &mapper)
Definition: PsfFlux.cc:133
T empty(T... args)
T end(T... args)
T isfinite(T... args)
T size(T... args)
T sqrt(T... args)
Simple class used to define and document flags The name and doc constitute the identity of the FlagDe...
Definition: FlagHandler.h:40
A reusable result struct for instFlux measurements.
Definition: FluxUtilities.h:41
Key< int > psf
Definition: Exposure.cc:65