Loading [MathJax]/extensions/tex2jax.js
LSST Applications g0fba68d861+aff6b1d179,g1e78f5e6d3+b2e1eec3e3,g1fd858c14a+eb8d917efb,g35bb328faa+fcb1d3bbc8,g436fd98eb5+de86862952,g4af146b050+2f70285269,g4d2262a081+c17cfe15e3,g4e0f332c67+8616b824a5,g53246c7159+fcb1d3bbc8,g5a012ec0e7+d65fd7031a,g60b5630c4e+de86862952,g67b6fd64d1+c0248a1c13,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g87b7deb4dc+1a71b41694,g8852436030+40f6ec51d1,g89139ef638+c0248a1c13,g9125e01d80+fcb1d3bbc8,g94187f82dc+de86862952,g989de1cb63+c0248a1c13,g9f33ca652e+62adb22cd2,g9f7030ddb1+d892b2cb3e,ga2b97cdc51+de86862952,gabe3b4be73+1e0a283bba,gabf8522325+83c19109ce,gb1101e3267+1371da34ff,gb58c049af0+f03b321e39,gb89ab40317+c0248a1c13,gcf25f946ba+40f6ec51d1,gd6cbbdb0b4+d9e8db455e,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+fc726a16be,gded526ad44+763ef31e97,ge278dab8ac+4ce6343b44,ge410e46f29+c0248a1c13,gf67bdafdda+c0248a1c13,gfe06eef73a+95f9f0e40c,v29.0.0.rc3
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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) {
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) {
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 setting flags.");
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 "Pixel in source outside usable exposure region (masked EDGE or centroid off image).");
111 _anyKeys["NO_DATA"] = schema.addField<afw::table::Flag>(name + "_flag_nodata",
112 "NO_DATA pixel in the source footprint.");
113 _anyKeys["INTRP"] = schema.addField<afw::table::Flag>(name + "_flag_interpolated",
114 "Interpolated pixel in the Source footprint");
115 _anyKeys["SAT"] = schema.addField<afw::table::Flag>(name + "_flag_saturated",
116 "Saturated pixel in the Source footprint");
117 _anyKeys["CR"] =
118 schema.addField<afw::table::Flag>(name + "_flag_cr", "Cosmic ray in the Source footprint");
119 _anyKeys["BAD"] =
120 schema.addField<afw::table::Flag>(name + "_flag_bad", "Bad pixel in the Source footprint");
121 _anyKeys["SUSPECT"] = schema.addField<afw::table::Flag>(name + "_flag_suspect",
122 "Source's footprint includes suspect pixels");
123 // Flags that correspond to mask bits which are set anywhere in the 3x3 central region of the object.
124 _centerKeys["EDGE"] = schema.addField<afw::table::Flag>(
125 name + "_flag_edgeCenter", "EDGE pixel in the 3x3 region around the centroid.");
126 _centerKeys["NO_DATA"] = schema.addField<afw::table::Flag>(
127 name + "_flag_nodataCenter", "NO_DATA pixel in the 3x3 region around the centroid.");
128 _centerKeys["INTRP"] = schema.addField<afw::table::Flag>(
129 name + "_flag_interpolatedCenter", "Interpolated pixel in the 3x3 region around the centroid.");
130 _centerKeys["SAT"] = schema.addField<afw::table::Flag>(
131 name + "_flag_saturatedCenter", "Saturated pixel in the 3x3 region around the centroid.");
132 _centerKeys["CR"] = schema.addField<afw::table::Flag>(
133 name + "_flag_crCenter", "Cosmic ray in the 3x3 region around the centroid.");
134 _centerKeys["BAD"] = schema.addField<afw::table::Flag>(name + "_flag_badCenter",
135 "Bad pixel in the 3x3 region around the centroid");
136 _centerKeys["SUSPECT"] = schema.addField<afw::table::Flag>(
137 name + "_flag_suspectCenter", "Suspect pixel in the 3x3 region around the centroid.");
138
139 // Flags that correspond to mask bits which are set on all of the 3x3 central pixels of the object.
140 _centerAllKeys["EDGE"] = schema.addField<afw::table::Flag>(
141 name + "_flag_edgeCenterAll",
142 "All pixels in the 3x3 region around the centroid are marked EDGE.");
143 _centerAllKeys["NO_DATA"] = schema.addField<afw::table::Flag>(
144 name + "_flag_nodataCenterAll",
145 "All pixels in the 3x3 region around the centroid are marked NO_DATA");
146 _centerAllKeys["INTRP"] = schema.addField<afw::table::Flag>(
147 name + "_flag_interpolatedCenterAll",
148 "All pixels in the 3x3 region around the centroid are interpolated.");
149 _centerAllKeys["SAT"] = schema.addField<afw::table::Flag>(
150 name + "_flag_saturatedCenterAll",
151 "All pixels in the 3x3 region around the centroid are saturated.");
152 _centerAllKeys["CR"] = schema.addField<afw::table::Flag>(
153 name + "_flag_crCenterAll",
154 "All pixels in the 3x3 region around the centroid have the cosmic ray mask bit.");
155 _centerAllKeys["BAD"] = schema.addField<afw::table::Flag>(
156 name + "_flag_badCenterAll", "All pixels in the 3x3 region around the centroid are bad.");
157 _centerAllKeys["SUSPECT"] = schema.addField<afw::table::Flag>(
158 name + "_flag_suspectCenterAll", "All pixels in the 3x3 region around the centroid are suspect.");
159
160 // Read in the flags passed from the configuration, and add them to the schema
161 for (auto const& i : _ctrl.masksFpCenter) {
162 std::string maskName(i);
163 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
164 _centerKeys[i] = schema.addField<afw::table::Flag>(
165 name + "_flag_" + maskName + "Center", "3x3 region around the centroid has " + i + " pixels");
166 }
167 for (auto const& i : _ctrl.masksFpCenter) {
168 std::string maskName(i);
169 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
170 _centerAllKeys[i] = schema.addField<afw::table::Flag>(
171 name + "_flag_" + maskName + "CenterAll",
172 "All pixels in the 3x3 region around the source centroid are " + i + " pixels");
173 }
174
175 for (auto const& i : _ctrl.masksFpAnywhere) {
176 std::string maskName(i);
177 std::transform(maskName.begin(), maskName.end(), maskName.begin(), ::tolower);
178 _anyKeys[i] = schema.addField<afw::table::Flag>(name + "_flag_" + maskName,
179 "Source footprint includes " + i + " pixels");
180 }
181}
182
184 afw::image::Exposure<float> const& exposure) const {
185 MaskedImageF mimage = exposure.getMaskedImage();
186 FootprintBits<MaskedImageF> func;
187
188 // Check if the measRecord has a valid centroid key, i.e. it was centroided
189 geom::Point2D center;
190 if (measRecord.getTable()->getCentroidSlot().getMeasKey().isValid()) {
191 center = measRecord.getCentroid();
192 } else {
193 // Set the general failure flag because using the Peak might affect
194 // the current measurement
195 measRecord.set(_generalFailureKey, true);
196 // Attempting to set pixel flags with no centroider, the center will
197 // be determined though the peak pixel. The first peak in the
198 // footprint (supplied by the measurement framework) should be the
199 // highest peak so that one will be used as a proxy for the central
200 // tendency of the distribution of flux for the record.
202 // If there is no footprint or the footprint contains no peaks, throw
203 // a runtime error.
204 if (!footprint || footprint->getPeaks().empty()) {
205 throw LSST_EXCEPT(pex::exceptions::RuntimeError, "No footprint, or no footprint peaks detected");
206 } else {
207 center.setX(footprint->getPeaks().front().getFx());
208 center.setY(footprint->getPeaks().front().getFy());
209 }
210 }
211
212 // Flag centroids off the image.
213 // Float bbox to properly handle non-finite centroids (casting Point2D->Point2I breaks NaN/inf).
214 geom::Box2D bbox(mimage.getBBox());
215 if (!bbox.contains(center)) {
216 measRecord.set(_offImageKey, true);
217 measRecord.set(_anyKeys.at("EDGE"), true);
218 measRecord.set(_centerKeys.at("EDGE"), true);
219 measRecord.set(_centerAllKeys.at("EDGE"), true);
220 }
221
222 // Check for bits set in the source's Footprint
223 auto footprint = measRecord.getFootprint();
224 if (!footprint) {
226 (boost::format("Source id %d has no footprint.") % measRecord.getId()).str());
227 }
228 auto fullSpans = footprint->getSpans();
229 if (!fullSpans) {
230 throw LSST_EXCEPT(
232 (boost::format("Source id %d has no spans in footprint.") % measRecord.getId()).str());
233 }
234 fullSpans->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
235
236 // update the source record for the any keys
237 updateFlags(_anyKeys, func, measRecord);
238
239 if (!(std::isfinite(center.getX()) && std::isfinite(center.getY()))) {
240 auto msg =
241 (boost::format("Centroid of source id %d passed to PixelFlags is non-finite; "
242 "footprint-based flags have been set, but centroid-based flags will not be.") %
243 measRecord.getId())
244 .str();
246 } else {
247 // Check for bits set in the 3x3 box around the center
248 geom::Point2I llc(afw::image::positionToIndex(center.getX()) - 1,
249 afw::image::positionToIndex(center.getY()) - 1);
250
251 func.reset();
253 afw::detection::Footprint const middle(spans); // central 3x3
254 middle.getSpans()->clippedTo(mimage.getBBox())->applyFunctor(func, *(mimage.getMask()));
255
256 // Update the flags which have to do with the center of the footprint
257 updateFlags(_centerKeys, func, measRecord);
258 updateFlagsAll(_centerAllKeys, func, measRecord);
259 }
260}
261
263 measRecord.set(_generalFailureKey, true);
264}
265
266} // namespace base
267} // namespace meas
268} // namespace lsst
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
This is the algorithm for PixelFlags.
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 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.
PixelFlagsControl Control
A typedef to the Control object for this algorithm, defined above.
Definition PixelFlags.h:68
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.
virtual char const * what(void) const noexcept
Return a character string summarizing this exception.
Definition Exception.cc:99
Reports errors that are due to events beyond the control of the program.
Definition Runtime.h:104
T isfinite(T... args)
T make_shared(T... args)
int positionToIndex(double pos)
Convert image position to nearest integer index.
Definition ImageUtils.h:69
Extent< int, 2 > ExtentI
Definition Extent.h:396
Point< double, 2 > Point2D
Definition Point.h:324
Point< int, 2 > Point2I
Definition Point.h:321
float Pixel
Typedefs to be used for pixel values.
Definition common.h:37
g2d::python::Image< bool > Mask
Definition test_image.cc:16