lsst.meas.base gfbd57f68c2+d85b565b0c
Loading...
Searching...
No Matches
PixelFlags.cc
Go to the documentation of this file.
1// -*- lsst-c++ -*-
2/*
3 * LSST Data Management System
4 * Copyright 2008-2013 LSST Corporation.
5 *
6 * This product includes software developed by the
7 * LSST Project (http://www.lsst.org/).
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the LSST License Statement and
20 * the GNU General Public License along with this program. If not,
21 * see <http://www.lsstcorp.org/LegalNotices/>.
22 */
23
24#include <cctype> // ::tolower
25#include <algorithm> // std::transform
26#include <cmath>
27
28#include "ndarray/eigen.h"
29
30#include "lsst/geom/Box.h"
31#include "lsst/geom/Point.h"
36
37namespace lsst {
38namespace meas {
39namespace base {
40namespace {
41template <typename MaskedImageT>
42class FootprintBits {
43public:
44 explicit FootprintBits() : _anyBits(0), _allBits(~static_cast<typename MaskedImageT::Mask::Pixel>(0x0)) {}
45
47 void reset() {
48 _anyBits = 0x0;
49 _allBits = ~static_cast<typename MaskedImageT::Mask::Pixel>(0x0);
50 }
51
52 void operator()(geom::Point2I const& point, typename MaskedImageT::Mask::Pixel const& value) {
53 _anyBits |= value;
54 _allBits &= value;
55 }
56
58 typename MaskedImageT::Mask::Pixel getAnyBits() const { return _anyBits; }
60 typename MaskedImageT::Mask::Pixel getAllBits() const { return _allBits; }
61
62private:
63 typename MaskedImageT::Mask::Pixel _anyBits;
64 typename MaskedImageT::Mask::Pixel _allBits;
65};
66
67typedef afw::image::MaskedImage<float> MaskedImageF;
68
69// Set flags when any pixel in func has the mask bit set.
70void updateFlags(PixelFlagsAlgorithm::KeyMap const& maskFlagToPixelFlag,
71 const FootprintBits<MaskedImageF>& func, afw::table::SourceRecord& measRecord) {
72 for (auto const& i : maskFlagToPixelFlag) {
73 try {
74 if (func.getAnyBits() & MaskedImageF::Mask::getPlaneBitMask(i.first)) {
75 measRecord.set(i.second, true);
76 }
77 } catch (pex::exceptions::InvalidParameterError& err) {
78 throw LSST_EXCEPT(FatalAlgorithmError, err.what());
79 }
80 }
81}
82
83// Set flags when all pixels in func have the mask bit set.
84void updateFlagsAll(PixelFlagsAlgorithm::KeyMap const& maskFlagToPixelFlag,
85 const FootprintBits<MaskedImageF>& func, afw::table::SourceRecord& measRecord) {
86 for (auto const& i : maskFlagToPixelFlag) {
87 try {
88 if (func.getAllBits() & MaskedImageF::Mask::getPlaneBitMask(i.first)) {
89 measRecord.set(i.second, true);
90 }
91 } catch (pex::exceptions::InvalidParameterError& err) {
92 throw LSST_EXCEPT(FatalAlgorithmError, err.what());
93 }
94 }
95}
96
97} // end anonymous namespace
98
101 : _ctrl(ctrl) {
102 // Add generic keys first, which don't correspond to specific mask planes
103 _generalFailureKey = schema.addField<afw::table::Flag>(
104 name + "_flag", "General failure flag, set if anything went wrong");
105 _offImageKey =
106 schema.addField<afw::table::Flag>(name + "_flag" + "_offimage", "Source center is off image");
107 // Set all the flags that correspond to mask planes anywhere in the footprint
108 _anyKeys["EDGE"] = schema.addField<afw::table::Flag>(
109 name + "_flag_edge", "Source is outside usable exposure region (masked EDGE or NO_DATA)");
110 _anyKeys["INTRP"] = schema.addField<afw::table::Flag>(name + "_flag_interpolated",
111 "Interpolated pixel in the Source footprint");
112 _anyKeys["SAT"] = schema.addField<afw::table::Flag>(name + "_flag_saturated",
113 "Saturated pixel in the Source footprint");
114 _anyKeys["CR"] =
115 schema.addField<afw::table::Flag>(name + "_flag_cr", "Cosmic ray in the Source footprint");
116 _anyKeys["BAD"] =
117 schema.addField<afw::table::Flag>(name + "_flag_bad", "Bad pixel in the Source footprint");
118 _anyKeys["SUSPECT"] = schema.addField<afw::table::Flag>(name + "_flag_suspect",
119 "Source's footprint includes suspect pixels");
120 // Flags that correspond to mask bits which are set anywhere in the 3x3 central region of the object.
121 _centerKeys["INTRP"] = schema.addField<afw::table::Flag>(
122 name + "_flag_interpolatedCenter", "Interpolated pixel in the 3x3 region around the centroid.");
123 _centerKeys["SAT"] = schema.addField<afw::table::Flag>(
124 name + "_flag_saturatedCenter", "Saturated pixel in the 3x3 region around the centroid.");
125 _centerKeys["CR"] = schema.addField<afw::table::Flag>(
126 name + "_flag_crCenter", "Cosmic ray in the 3x3 region around the centroid.");
127 _centerKeys["BAD"] = schema.addField<afw::table::Flag>(name + "_flag_badCenter",
128 "Bad pixel in the 3x3 region around the centroid");
129 _centerKeys["SUSPECT"] = schema.addField<afw::table::Flag>(
130 name + "_flag_suspectCenter", "Suspect pixel in the 3x3 region around the centroid.");
131
132 // Flags that correspond to mask bits which are set on all of the 3x3 central pixels of the object.
133 _centerAllKeys["INTRP"] = schema.addField<afw::table::Flag>(
134 name + "_flag_interpolatedCenterAll",
135 "All pixels in the 3x3 region around the centroid are interpolated.");
136 _centerAllKeys["SAT"] = schema.addField<afw::table::Flag>(
137 name + "_flag_saturatedCenterAll",
138 "All pixels in the 3x3 region around the centroid are saturated.");
139 _centerAllKeys["CR"] = schema.addField<afw::table::Flag>(
140 name + "_flag_crCenterAll",
141 "All pixels in the 3x3 region around the centroid have the cosmic ray mask bit.");
142 _centerAllKeys["BAD"] = schema.addField<afw::table::Flag>(
143 name + "_flag_badCenterAll", "All pixels in the 3x3 region around the centroid are bad.");
144 _centerAllKeys["SUSPECT"] = schema.addField<afw::table::Flag>(
145 name + "_flag_suspectCenterAll", "All pixels in the 3x3 region around the centroid are suspect.");
146
147 // Read in the flags passed from the configuration, and add them to the schema
148 for (auto const& i : _ctrl.masksFpCenter) {
150 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
151 _centerKeys[i] = schema.addField<afw::table::Flag>(
152 name + "_flag_" + maskName + "Center", "3x3 region around the centroid has " + i + " pixels");
153 }
154 for (auto const& i : _ctrl.masksFpCenter) {
156 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
157 _centerAllKeys[i] = schema.addField<afw::table::Flag>(
158 name + "_flag_" + maskName + "CenterAll",
159 "All pixels in the 3x3 region around the source centroid are " + i + " pixels");
160 }
161
162 for (auto const& i : _ctrl.masksFpAnywhere) {
164 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
165 _anyKeys[i] = schema.addField<afw::table::Flag>(name + "_flag_" + maskName,
166 "Source footprint includes " + i + " pixels");
167 }
168}
169
172 MaskedImageF mimage = exposure.getMaskedImage();
174
175 // Check if the measRecord has a valid centroid key, i.e. it was centroided
176 geom::Point2D center;
177 if (measRecord.getTable()->getCentroidSlot().getMeasKey().isValid()) {
178 center = measRecord.getCentroid();
179 // Catch NAN in centroid estimate
180 if (std::isnan(center.getX()) || std::isnan(center.getY())) {
182 "Center point passed to PixelFlagsAlgorithm is NaN");
183 }
184 } else {
185 // Set the general failure flag because using the Peak might affect
186 // the current measurement
187 measRecord.set(_generalFailureKey, true);
188 // Attempting to set pixel flags with no centroider, the center will
189 // be determined though the peak pixel. The first peak in the
190 // footprint (supplied by the measurement framework) should be the
191 // highest peak so that one will be used as a proxy for the central
192 // tendency of the distribution of flux for the record.
194 // If there is no footprint or the footprint contains no peaks, throw
195 // a runtime error.
196 if (!footprint || footprint->getPeaks().empty()) {
197 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "No footprint, or no footprint peaks detected");
198 } else {
199 center.setX(footprint->getPeaks().front().getFx());
200 center.setY(footprint->getPeaks().front().getFy());
201 }
202 }
203
204 // Catch centroids off the image
205 if (!mimage.getBBox().contains(geom::Point2I(center))) {
206 measRecord.set(_offImageKey, true);
207 measRecord.set(_anyKeys.at("EDGE"), true);
208 }
209
210 // Check for bits set in the source's Footprint
211 auto footprint = measRecord.getFootprint();
212 if (!footprint) {
213 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "No footprint present.");
214 }
215 auto fullSpans = footprint->getSpans();
216 if (!fullSpans) {
217 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "No spans in footprint.");
218 }
219 fullSpans->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
220
221 // Set the EDGE flag if the bitmask has NO_DATA set
222 try {
223 if (func.getAnyBits() & MaskedImageF::Mask::getPlaneBitMask("NO_DATA")) {
224 measRecord.set(_anyKeys.at("EDGE"), true);
225 }
227 throw LSST_EXCEPT(FatalAlgorithmError, err.what());
228 }
229
230 // update the source record for the any keys
231 updateFlags(_anyKeys, func, measRecord);
232
233 // Check for bits set in the 3x3 box around the center
235 afw::image::positionToIndex(center.getY()) - 1);
236
237 func.reset();
238 auto spans = std::make_shared<afw::geom::SpanSet>(geom::Box2I(llc, geom::ExtentI(3)));
239 afw::detection::Footprint const middle(spans); // central 3x3
240 middle.getSpans()->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
241
242 // Update the flags which have to do with the center of the footprint
243 updateFlags(_centerKeys, func, measRecord);
244 updateFlagsAll(_centerAllKeys, func, measRecord);
245}
246
248 measRecord.set(_generalFailureKey, true);
249}
250
251} // namespace base
252} // namespace meas
253} // namespace lsst
table::Key< std::string > name
#define LSST_EXCEPT(type,...)
This is the algorithm for PixelFlags.
T at(T... args)
Exception to be thrown when a measurement algorithm experiences a fatal error.
Definition exceptions.h:76
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition exceptions.h:48
std::map< std::string, afw::table::Key< afw::table::Flag > > KeyMap
Definition PixelFlags.h:77
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.
PixelFlagsAlgorithm(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
Definition PixelFlags.cc:99
virtual void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
Called to measure a single child source in an image.
A C++ control class to handle PixelFlagsAlgorithm's configuration.
Definition PixelFlags.h:44
std::vector< std::string > masksFpAnywhere
"List of mask planes to be searched for which occur anywhere within a footprint. " "If any of the pla...
Definition PixelFlags.h:51
std::vector< std::string > masksFpCenter
"List of mask planes to be searched for which occur in the center of a footprint. " "If any of the pl...
Definition PixelFlags.h:48
T isnan(T... args)
int positionToIndex(double pos)
Point< int, 2 > Point2I
T transform(T... args)