LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
InputUtilities.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014 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 <cmath>
25 
26 #include "lsst/afw/table/Source.h"
30 
31 namespace lsst { namespace meas { namespace base {
32 
35  std::string const & name,
36  bool isCentroider
37 ) :
38  _name(name),
39  _isCentroider(isCentroider)
40 {
41  // Instead of aliasing e.g. MyAlgorithm_flag_badCentroid->slot_Centroid_flag, we actually
42  // look up the target of slot_Centroid_flag, and alias that to MyAlgorithm_flag_badCentroid.
43  // That way, if someone changes the slots later, after we've already done the measurement,
44  // this alias still points to the right thing.
45  std::string aliasedFlagName = schema.join("slot", "Centroid", "flag");
46  std::string slotFlagName = schema.getAliasMap()->apply(aliasedFlagName);
47  if (_isCentroider) {
48  if (slotFlagName != schema.join(name, "flag")) {
49  // only setup the alias if this isn't the slot algorithm itself (otherwise it'd be circular)
50  schema.getAliasMap()->set(schema.join(name, "flag", "badInitialCentroid"), slotFlagName);
51  }
52  } else {
53  if (aliasedFlagName == slotFlagName) {
54  throw LSST_EXCEPT(
55  pex::exceptions::LogicError,
56  (boost::format("Alias for '%s' must be defined before initializing '%s' plugin.")
57  % aliasedFlagName % name).str()
58  );
59  }
60  schema.getAliasMap()->set(schema.join(name, "flag", "badCentroid"), slotFlagName);
61  }
62 }
63 
64 namespace {
65 
66 afw::geom::Point2D extractPeak(afw::table::SourceRecord const & record, std::string const & name) {
67  afw::geom::Point2D result;
68  PTR(afw::detection::Footprint) footprint = record.getFootprint();
69  if (!footprint) {
70  throw LSST_EXCEPT(
71  pex::exceptions::RuntimeError,
72  (boost::format("%s: Centroid slot value is NaN, but no Footprint attached to record")
73  % name).str()
74  );
75  }
76  if (footprint->getPeaks().empty()) {
77  throw LSST_EXCEPT(
78  pex::exceptions::RuntimeError,
79  (boost::format("%s: Centroid slot value is NaN, but Footprint has no Peaks")
80  % name).str()
81  );
82  }
83  result.setX(footprint->getPeaks().front().getFx());
84  result.setY(footprint->getPeaks().front().getFy());
85  return result;
86 }
87 
88 } // anonymous
89 
91  afw::table::SourceRecord & record,
92  FlagHandler const & flags
93 ) const {
94  if (!record.getTable()->getCentroidKey().isValid()) {
95  if (_isCentroider) {
96  return extractPeak(record, _name);
97  } else {
98  throw LSST_EXCEPT(
99  FatalAlgorithmError,
100  (boost::format("%s requires a centroid, but the centroid slot is not defined") % _name).str()
101  );
102  }
103  }
104  afw::geom::Point2D result = record.getCentroid();
105  if (std::isnan(result.getX()) || std::isnan(result.getY())) {
106  if (!record.getTable()->getCentroidFlagKey().isValid()) {
107  if (_isCentroider) {
108  return extractPeak(record, _name);
109  } else {
110  throw LSST_EXCEPT(
111  pex::exceptions::RuntimeError,
112  (boost::format("%s: Centroid slot value is NaN, but there is no Centroid slot flag "
113  "(is the executionOrder for %s lower than that of the slot Centroid?)")
114  % _name % _name).str()
115  );
116  }
117  }
118  if (!record.getCentroidFlag() && !_isCentroider) {
119  throw LSST_EXCEPT(
120  pex::exceptions::RuntimeError,
121  (boost::format("%s: Centroid slot value is NaN, but the Centroid slot flag is not set "
122  "(is the executionOrder for %s lower than that of the slot Centroid?)")
123  % _name % _name).str()
124  );
125  }
126  result = extractPeak(record, _name);
127  if (!_isCentroider) {
128  // set the general flag, because using the Peak might affect the current measurement
129  flags.setValue(record, FlagHandler::FAILURE, true);
130  }
131  } else if (!_isCentroider && record.getTable()->getCentroidFlagKey().isValid()
132  && record.getCentroidFlag()) {
133  // we got a usable value, but the centroid flag is still be set, and that might affect
134  // the current measurement
135  flags.setValue(record, FlagHandler::FAILURE, true);
136  }
137  return result;
138 }
139 
141  _name(name)
142 {
143  // Instead of aliasing e.g. MyAlgorithm_flag_badShape->slot_Shape_flag, we actually
144  // look up the target of slot_Shape_flag, and alias that to MyAlgorithm_flag_badCentroid.
145  // That way, if someone changes the slots later, after we've already done the measurement,
146  // this alias still points to the right thing.
147  std::string aliasedFlagName = schema.join("slot", "Shape", "flag");
148  std::string slotFlagName = schema.getAliasMap()->apply(aliasedFlagName);
149  if (aliasedFlagName == slotFlagName) {
150  throw LSST_EXCEPT(
151  pex::exceptions::LogicError,
152  (boost::format("Alias for '%s' must be defined before initializing '%s' plugin.")
153  % aliasedFlagName % name).str()
154  );
155  }
156  schema.getAliasMap()->set(schema.join(name, "flag", "badShape"), slotFlagName);
157 }
158 
160  afw::table::SourceRecord & record,
161  FlagHandler const & flags
162 ) const {
163  if (!record.getTable()->getShapeKey().isValid()) {
164  throw LSST_EXCEPT(
165  FatalAlgorithmError,
166  (boost::format("%s requires a shape, but the shape slot is not defined") % _name).str()
167  );
168  }
169  afw::geom::ellipses::Quadrupole result = record.getShape();
170  if (std::isnan(result.getIxx()) || std::isnan(result.getIyy()) || std::isnan(result.getIxy())
171  || result.getIxx()*result.getIyy() <
172  (1.0 + 1.0e-6)*result.getIxy()*result.getIxy()
173  // We are checking that Ixx*Iyy > (1 + epsilon)*Ixy*Ixy where epsilon is suitably small. The
174  // value of epsilon used here is a magic number. DM-5801 is supposed to figure out if we are
175  // to keep this value.
176  ) {
177  if (!record.getTable()->getShapeFlagKey().isValid()) {
178  throw LSST_EXCEPT(
179  pex::exceptions::RuntimeError,
180  (boost::format("%s: Shape slot value is NaN, but there is no Shape slot flag "
181  "(is the executionOrder for %s lower than that of the slot Shape?)")
182  % _name % _name).str()
183  );
184  }
185  if (!record.getShapeFlag()) {
186  throw LSST_EXCEPT(
187  pex::exceptions::RuntimeError,
188  (boost::format("%s: Shape slot value is NaN, but the Shape slot flag is not set "
189  "(is the executionOrder for %s lower than that of the slot Shape?)")
190  % _name % _name).str()
191  );
192  }
193  throw LSST_EXCEPT(
195  (boost::format("%s: Shape needed, and Shape slot measurement failed.") % _name).str(),
197  );
198  } else if (record.getTable()->getShapeFlagKey().isValid() && record.getShapeFlag()) {
199  // we got a usable value, but the shape flag might still be set, and that might affect
200  // the current measurement
201  flags.setValue(record, FlagHandler::FAILURE, true);
202  }
203  return result;
204 }
205 
206 }}} // lsst::meas::base
An ellipse core with quadrupole moments as parameters.
Definition: Quadrupole.h:45
Defines the fields and offsets for a table.
Definition: Schema.h:44
Represent a set of pixels of an arbitrary shape and size.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
SafeShapeExtractor(afw::table::Schema &schema, std::string const &name)
boost::shared_ptr< AliasMap > getAliasMap() const
Definition: Schema.h:254
std::string const & _name
Definition: Mask.cc:678
afw::table::Schema schema
Definition: GaussianPsf.cc:41
double const getIxx() const
Definition: Quadrupole.h:56
ShapeSlotDefinition::MeasValue getShape() const
Get the value of the Shape slot measurement.
Definition: Source.h:914
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
double const getIyy() const
Definition: Quadrupole.h:59
A coordinate class intended to represent absolute positions.
Definition: PSF.h:39
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition: Source.h:902
double const getIxy() const
Definition: Quadrupole.h:62
if(width!=gim.getWidth()||height!=gim.getHeight()||x0!=gim.getX0()||y0!=gim.getY0())
Definition: saturated.cc:47
SafeCentroidExtractor(afw::table::Schema &schema, std::string const &name, bool isCentroider=false)
A set of pixels in an Image.
Definition: Footprint.h:62
afw::geom::ellipses::Quadrupole operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
boost::shared_ptr< SourceTable const > getTable() const
Definition: Source.h:92
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
#define PTR(...)
Definition: base.h:41
bool getCentroidFlag() const
Return true if the measurement in the Centroid slot failed.
Definition: Source.h:910
void setValue(afw::table::BaseRecord &record, std::size_t i, bool value) const
Definition: FlagHandler.h:188
Record class that contains measurements made on a single exposure.
Definition: Source.h:80
afw::geom::Point2D operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
bool getShapeFlag() const
Return true if the measurement in the Shape slot failed.
Definition: Source.h:922