LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
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",
110 "Source is outside usable exposure region (masked EDGE or NO_DATA, or centroid off image).");
111 _anyKeys["NO_DATA"] = _anyKeys.at("EDGE"); // Also set edge flag for NO_DATA.
112 _anyKeys["INTRP"] = schema.addField<afw::table::Flag>(name + "_flag_interpolated",
113 "Interpolated pixel in the Source footprint");
114 _anyKeys["SAT"] = schema.addField<afw::table::Flag>(name + "_flag_saturated",
115 "Saturated pixel in the Source footprint");
116 _anyKeys["CR"] =
117 schema.addField<afw::table::Flag>(name + "_flag_cr", "Cosmic ray in the Source footprint");
118 _anyKeys["BAD"] =
119 schema.addField<afw::table::Flag>(name + "_flag_bad", "Bad pixel in the Source footprint");
120 _anyKeys["SUSPECT"] = schema.addField<afw::table::Flag>(name + "_flag_suspect",
121 "Source's footprint includes suspect pixels");
122 // Flags that correspond to mask bits which are set anywhere in the 3x3 central region of the object.
123 _centerKeys["EDGE"] = schema.addField<afw::table::Flag>(
124 name + "_flag_edgeCenter", "EDGE or NO_DATA Pixel in the 3x3 region around the centroid.");
125 _centerKeys["NO_DATA"] = _centerKeys.at("EDGE"); // Also set edge flag for NO_DATA.
126 _centerKeys["INTRP"] = schema.addField<afw::table::Flag>(
127 name + "_flag_interpolatedCenter", "Interpolated pixel in the 3x3 region around the centroid.");
128 _centerKeys["SAT"] = schema.addField<afw::table::Flag>(
129 name + "_flag_saturatedCenter", "Saturated pixel in the 3x3 region around the centroid.");
130 _centerKeys["CR"] = schema.addField<afw::table::Flag>(
131 name + "_flag_crCenter", "Cosmic ray in the 3x3 region around the centroid.");
132 _centerKeys["BAD"] = schema.addField<afw::table::Flag>(name + "_flag_badCenter",
133 "Bad pixel in the 3x3 region around the centroid");
134 _centerKeys["SUSPECT"] = schema.addField<afw::table::Flag>(
135 name + "_flag_suspectCenter", "Suspect pixel in the 3x3 region around the centroid.");
136
137 // Flags that correspond to mask bits which are set on all of the 3x3 central pixels of the object.
138 _centerAllKeys["EDGE"] = schema.addField<afw::table::Flag>(
139 name + "_flag_edgeCenterAll",
140 "All pixels in the 3x3 region around the centroid are marked EDGE or NO_DATA.");
141 _centerAllKeys["NO_DATA"] = _centerAllKeys.at("EDGE"); // Also set edge flag for NO_DATA.
142 _centerAllKeys["INTRP"] = schema.addField<afw::table::Flag>(
143 name + "_flag_interpolatedCenterAll",
144 "All pixels in the 3x3 region around the centroid are interpolated.");
145 _centerAllKeys["SAT"] = schema.addField<afw::table::Flag>(
146 name + "_flag_saturatedCenterAll",
147 "All pixels in the 3x3 region around the centroid are saturated.");
148 _centerAllKeys["CR"] = schema.addField<afw::table::Flag>(
149 name + "_flag_crCenterAll",
150 "All pixels in the 3x3 region around the centroid have the cosmic ray mask bit.");
151 _centerAllKeys["BAD"] = schema.addField<afw::table::Flag>(
152 name + "_flag_badCenterAll", "All pixels in the 3x3 region around the centroid are bad.");
153 _centerAllKeys["SUSPECT"] = schema.addField<afw::table::Flag>(
154 name + "_flag_suspectCenterAll", "All pixels in the 3x3 region around the centroid are suspect.");
155
156 // Read in the flags passed from the configuration, and add them to the schema
157 for (auto const& i : _ctrl.masksFpCenter) {
158 std::string maskName(i);
159 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
160 _centerKeys[i] = schema.addField<afw::table::Flag>(
161 name + "_flag_" + maskName + "Center", "3x3 region around the centroid has " + i + " pixels");
162 }
163 for (auto const& i : _ctrl.masksFpCenter) {
164 std::string maskName(i);
165 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
166 _centerAllKeys[i] = schema.addField<afw::table::Flag>(
167 name + "_flag_" + maskName + "CenterAll",
168 "All pixels in the 3x3 region around the source centroid are " + i + " pixels");
169 }
170
171 for (auto const& i : _ctrl.masksFpAnywhere) {
172 std::string maskName(i);
173 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
174 _anyKeys[i] = schema.addField<afw::table::Flag>(name + "_flag_" + maskName,
175 "Source footprint includes " + i + " pixels");
176 }
177}
178
180 afw::image::Exposure<float> const& exposure) const {
181 MaskedImageF mimage = exposure.getMaskedImage();
182 FootprintBits<MaskedImageF> func;
183
184 // Check if the measRecord has a valid centroid key, i.e. it was centroided
185 geom::Point2D center;
186 if (measRecord.getTable()->getCentroidSlot().getMeasKey().isValid()) {
187 center = measRecord.getCentroid();
188 } else {
189 // Set the general failure flag because using the Peak might affect
190 // the current measurement
191 measRecord.set(_generalFailureKey, true);
192 // Attempting to set pixel flags with no centroider, the center will
193 // be determined though the peak pixel. The first peak in the
194 // footprint (supplied by the measurement framework) should be the
195 // highest peak so that one will be used as a proxy for the central
196 // tendency of the distribution of flux for the record.
198 // If there is no footprint or the footprint contains no peaks, throw
199 // a runtime error.
200 if (!footprint || footprint->getPeaks().empty()) {
201 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "No footprint, or no footprint peaks detected");
202 } else {
203 center.setX(footprint->getPeaks().front().getFx());
204 center.setY(footprint->getPeaks().front().getFy());
205 }
206 }
207
208 // Flag centroids off the image.
209 // Float bbox to properly handle non-finite centroids (casting Point2D->Point2I breaks NaN/inf).
210 geom::Box2D bbox(mimage.getBBox());
211 if (!bbox.contains(center)) {
212 measRecord.set(_offImageKey, true);
213 measRecord.set(_anyKeys.at("EDGE"), true);
214 measRecord.set(_centerKeys.at("EDGE"), true);
215 measRecord.set(_centerAllKeys.at("EDGE"), true);
216 }
217
218 // Check for bits set in the source's Footprint
219 auto footprint = measRecord.getFootprint();
220 if (!footprint) {
222 (boost::format("Source id %d has no footprint.") % measRecord.getId()).str());
223 }
224 auto fullSpans = footprint->getSpans();
225 if (!fullSpans) {
226 throw LSST_EXCEPT(
228 (boost::format("Source id %d has no spans in footprint.") % measRecord.getId()).str());
229 }
230 fullSpans->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
231
232 // update the source record for the any keys
233 updateFlags(_anyKeys, func, measRecord);
234
235 if (!(std::isfinite(center.getX()) && std::isfinite(center.getY()))) {
236 auto msg =
237 (boost::format("Centroid of source id %d passed to PixelFlags is non-finite; "
238 "footprint-based flags have been set, but centroid-based flags will not be.") %
239 measRecord.getId())
240 .str();
242 } else {
243 // Check for bits set in the 3x3 box around the center
244 geom::Point2I llc(afw::image::positionToIndex(center.getX()) - 1,
245 afw::image::positionToIndex(center.getY()) - 1);
246
247 func.reset();
248 auto spans = std::make_shared<afw::geom::SpanSet>(geom::Box2I(llc, geom::ExtentI(3)));
249 afw::detection::Footprint const middle(spans); // central 3x3
250 middle.getSpans()->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
251
252 // Update the flags which have to do with the center of the footprint
253 updateFlags(_centerKeys, func, measRecord);
254 updateFlagsAll(_centerAllKeys, func, measRecord);
255 }
256}
257
259 measRecord.set(_generalFailureKey, true);
260}
261
262} // namespace base
263} // namespace meas
264} // namespace lsst
AmpInfoBoxKey bbox
Definition Amplifier.cc:117
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
This is the algorithm for PixelFlags.
table::Schema schema
Definition python.h:134
T at(T... args)
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
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
Defines the fields and offsets for a table.
Definition Schema.h:51
RecordId getId() const
Convenience accessors for the keys in the minimal reference schema.
Definition Simple.h:227
Record class that contains measurements made on a single exposure.
Definition Source.h:78
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition Source.h:569
std::shared_ptr< SourceTable const > getTable() const
Definition Source.h:102
std::shared_ptr< Footprint > getFootprint() const
Definition Source.h:98
A floating-point coordinate rectangle geometry.
Definition Box.h:413
An integer coordinate rectangle.
Definition Box.h:55
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
Reports errors that are due to events beyond the control of the program.
Definition Runtime.h:104
T end(T... args)
T isfinite(T... args)
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition ImageUtils.h:69
float Pixel
Typedefs to be used for pixel values.
Definition common.h:37
g2d::python::Image< bool > Mask
Definition test_image.cc:16
T transform(T... args)