lsst.meas.base g08a116f7bc+e5b6b89a1e
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
100 afw::table::Schema& schema)
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",
110 "Source is outside usable exposure region (masked EDGE or NO_DATA, or centroid off image)");
111 _anyKeys["INTRP"] = schema.addField<afw::table::Flag>(name + "_flag_interpolated",
112 "Interpolated pixel in the Source footprint");
113 _anyKeys["SAT"] = schema.addField<afw::table::Flag>(name + "_flag_saturated",
114 "Saturated pixel in the Source footprint");
115 _anyKeys["CR"] =
116 schema.addField<afw::table::Flag>(name + "_flag_cr", "Cosmic ray in the Source footprint");
117 _anyKeys["BAD"] =
118 schema.addField<afw::table::Flag>(name + "_flag_bad", "Bad pixel in the Source footprint");
119 _anyKeys["SUSPECT"] = schema.addField<afw::table::Flag>(name + "_flag_suspect",
120 "Source's footprint includes suspect pixels");
121 // Flags that correspond to mask bits which are set anywhere in the 3x3 central region of the object.
122 _centerKeys["INTRP"] = schema.addField<afw::table::Flag>(
123 name + "_flag_interpolatedCenter", "Interpolated pixel in the 3x3 region around the centroid.");
124 _centerKeys["SAT"] = schema.addField<afw::table::Flag>(
125 name + "_flag_saturatedCenter", "Saturated pixel in the 3x3 region around the centroid.");
126 _centerKeys["CR"] = schema.addField<afw::table::Flag>(
127 name + "_flag_crCenter", "Cosmic ray in the 3x3 region around the centroid.");
128 _centerKeys["BAD"] = schema.addField<afw::table::Flag>(name + "_flag_badCenter",
129 "Bad pixel in the 3x3 region around the centroid");
130 _centerKeys["SUSPECT"] = schema.addField<afw::table::Flag>(
131 name + "_flag_suspectCenter", "Suspect pixel in the 3x3 region around the centroid.");
132
133 // Flags that correspond to mask bits which are set on all of the 3x3 central pixels of the object.
134 _centerAllKeys["INTRP"] = schema.addField<afw::table::Flag>(
135 name + "_flag_interpolatedCenterAll",
136 "All pixels in the 3x3 region around the centroid are interpolated.");
137 _centerAllKeys["SAT"] = schema.addField<afw::table::Flag>(
138 name + "_flag_saturatedCenterAll",
139 "All pixels in the 3x3 region around the centroid are saturated.");
140 _centerAllKeys["CR"] = schema.addField<afw::table::Flag>(
141 name + "_flag_crCenterAll",
142 "All pixels in the 3x3 region around the centroid have the cosmic ray mask bit.");
143 _centerAllKeys["BAD"] = schema.addField<afw::table::Flag>(
144 name + "_flag_badCenterAll", "All pixels in the 3x3 region around the centroid are bad.");
145 _centerAllKeys["SUSPECT"] = schema.addField<afw::table::Flag>(
146 name + "_flag_suspectCenterAll", "All pixels in the 3x3 region around the centroid are suspect.");
147
148 // Read in the flags passed from the configuration, and add them to the schema
149 for (auto const& i : _ctrl.masksFpCenter) {
151 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
152 _centerKeys[i] = schema.addField<afw::table::Flag>(
153 name + "_flag_" + maskName + "Center", "3x3 region around the centroid has " + i + " pixels");
154 }
155 for (auto const& i : _ctrl.masksFpCenter) {
157 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
158 _centerAllKeys[i] = schema.addField<afw::table::Flag>(
159 name + "_flag_" + maskName + "CenterAll",
160 "All pixels in the 3x3 region around the source centroid are " + i + " pixels");
161 }
162
163 for (auto const& i : _ctrl.masksFpAnywhere) {
165 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
166 _anyKeys[i] = schema.addField<afw::table::Flag>(name + "_flag_" + maskName,
167 "Source footprint includes " + i + " pixels");
168 }
169}
170
172 afw::image::Exposure<float> const& exposure) const {
173 MaskedImageF mimage = exposure.getMaskedImage();
175
176 // Check if the measRecord has a valid centroid key, i.e. it was centroided
177 geom::Point2D center;
178 if (measRecord.getTable()->getCentroidSlot().getMeasKey().isValid()) {
179 center = measRecord.getCentroid();
180 } else {
181 // Set the general failure flag because using the Peak might affect
182 // the current measurement
183 measRecord.set(_generalFailureKey, true);
184 // Attempting to set pixel flags with no centroider, the center will
185 // be determined though the peak pixel. The first peak in the
186 // footprint (supplied by the measurement framework) should be the
187 // highest peak so that one will be used as a proxy for the central
188 // tendency of the distribution of flux for the record.
190 // If there is no footprint or the footprint contains no peaks, throw
191 // a runtime error.
192 if (!footprint || footprint->getPeaks().empty()) {
193 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "No footprint, or no footprint peaks detected");
194 } else {
195 center.setX(footprint->getPeaks().front().getFx());
196 center.setY(footprint->getPeaks().front().getFy());
197 }
198 }
199
200 // Flag centroids off the image.
201 // Float bbox to properly handle non-finite centroids (casting Point2D->Point2I breaks NaN/inf).
202 geom::Box2D bbox(mimage.getBBox());
203 if (!bbox.contains(center)) {
204 measRecord.set(_offImageKey, true);
205 measRecord.set(_anyKeys.at("EDGE"), true);
206 }
207
208 // Check for bits set in the source's Footprint
209 auto footprint = measRecord.getFootprint();
210 if (!footprint) {
212 (boost::format("Source id %d has no footprint.") % measRecord.getId()).str());
213 }
214 auto fullSpans = footprint->getSpans();
215 if (!fullSpans) {
216 throw LSST_EXCEPT(
218 (boost::format("Source id %d has no spans in footprint.") % measRecord.getId()).str());
219 }
220 fullSpans->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
221
222 // Set the EDGE flag if the bitmask has NO_DATA set
223 try {
224 if (func.getAnyBits() & MaskedImageF::Mask::getPlaneBitMask("NO_DATA")) {
225 measRecord.set(_anyKeys.at("EDGE"), true);
226 }
228 throw LSST_EXCEPT(FatalAlgorithmError, err.what());
229 }
230
231 // update the source record for the any keys
232 updateFlags(_anyKeys, func, measRecord);
233
234 if (!(std::isfinite(center.getX()) && std::isfinite(center.getY()))) {
235 auto msg =
236 (boost::format("Centroid of source id %d passed to PixelFlags is non-finite; "
237 "footprint-based flags have been set, but centroid-based flags will not be.") %
238 measRecord.getId())
239 .str();
241 } else {
242 // Check for bits set in the 3x3 box around the center
244 afw::image::positionToIndex(center.getY()) - 1);
245
246 func.reset();
247 auto spans = std::make_shared<afw::geom::SpanSet>(geom::Box2I(llc, geom::ExtentI(3)));
248 afw::detection::Footprint const middle(spans); // central 3x3
249 middle.getSpans()->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
250
251 // Update the flags which have to do with the center of the footprint
252 updateFlags(_centerKeys, func, measRecord);
253 updateFlagsAll(_centerAllKeys, func, measRecord);
254 }
255}
256
258 measRecord.set(_generalFailureKey, true);
259}
260
261} // namespace base
262} // namespace meas
263} // namespace lsst
AmpInfoBoxKey bbox
#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 isfinite(T... args)
int positionToIndex(double pos)
Point< int, 2 > Point2I
T transform(T... args)