LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 "lsst/utils/ieee.h"
25 #include "lsst/afw/table/Source.h"
29 
30 namespace lsst { namespace meas { namespace base {
31 
34  std::string const & name,
35  bool isCentroider
36 ) :
37  _name(name),
38  _isCentroider(isCentroider)
39 {
40  // Instead of aliasing e.g. MyAlgorithm_flag_badCentroid->slot_Centroid_flag, we actually
41  // look up the target of slot_Centroid_flag, and alias that to MyAlgorithm_flag_badCentroid.
42  // That way, if someone changes the slots later, after we've already done the measurement,
43  // this alias still points to the right thing.
44  std::string aliasedFlagName = schema.join("slot", "Centroid", "flag");
45  std::string slotFlagName = schema.getAliasMap()->apply(aliasedFlagName);
46  if (_isCentroider) {
47  if (slotFlagName != schema.join(name, "flag")) {
48  // only setup the alias if this isn't the slot algorithm itself (otherwise it'd be circular)
49  schema.getAliasMap()->set(schema.join(name, "flag", "badInitialCentroid"), slotFlagName);
50  }
51  } else {
52  if (aliasedFlagName == slotFlagName) {
53  throw LSST_EXCEPT(
54  pex::exceptions::LogicError,
55  (boost::format("Alias for '%s' must be defined before initializing '%s' plugin.")
56  % aliasedFlagName % name).str()
57  );
58  }
59  schema.getAliasMap()->set(schema.join(name, "flag", "badCentroid"), slotFlagName);
60  }
61 }
62 
63 namespace {
64 
65 afw::geom::Point2D extractPeak(afw::table::SourceRecord const & record, std::string const & name) {
66  afw::geom::Point2D result;
67  PTR(afw::detection::Footprint) footprint = record.getFootprint();
68  if (!footprint) {
69  throw LSST_EXCEPT(
70  pex::exceptions::RuntimeError,
71  (boost::format("%s: Centroid slot value is NaN, but no Footprint attached to record")
72  % name).str()
73  );
74  }
75  if (footprint->getPeaks().empty()) {
76  throw LSST_EXCEPT(
77  pex::exceptions::RuntimeError,
78  (boost::format("%s: Centroid slot value is NaN, but Footprint has no Peaks")
79  % name).str()
80  );
81  }
82  result.setX(footprint->getPeaks().front().getFx());
83  result.setY(footprint->getPeaks().front().getFy());
84  return result;
85 }
86 
87 } // anonymous
88 
90  afw::table::SourceRecord & record,
91  FlagHandler const & flags
92 ) const {
93  if (!record.getTable()->getCentroidKey().isValid()) {
94  if (_isCentroider) {
95  return extractPeak(record, _name);
96  } else {
97  throw LSST_EXCEPT(
98  FatalAlgorithmError,
99  (boost::format("%s requires a centroid, but the centroid slot is not defined") % _name).str()
100  );
101  }
102  }
103  afw::geom::Point2D result = record.getCentroid();
104  if (utils::isnan(result.getX()) || utils::isnan(result.getY())) {
105  if (!record.getTable()->getCentroidFlagKey().isValid()) {
106  if (_isCentroider) {
107  return extractPeak(record, _name);
108  } else {
109  throw LSST_EXCEPT(
110  pex::exceptions::RuntimeError,
111  (boost::format("%s: Centroid slot value is NaN, but there is no Centroid slot flag "
112  "(is the executionOrder for %s lower than that of the slot Centroid?)")
113  % _name % _name).str()
114  );
115  }
116  }
117  if (!record.getCentroidFlag() && !_isCentroider) {
118  throw LSST_EXCEPT(
119  pex::exceptions::RuntimeError,
120  (boost::format("%s: Centroid slot value is NaN, but the Centroid slot flag is not set "
121  "(is the executionOrder for %s lower than that of the slot Centroid?)")
122  % _name % _name).str()
123  );
124  }
125  result = extractPeak(record, _name);
126  if (!_isCentroider) {
127  // set the general flag, because using the Peak might affect the current measurement
128  flags.setValue(record, FlagHandler::FAILURE, true);
129  }
130  } else if (!_isCentroider && record.getTable()->getCentroidFlagKey().isValid()
131  && record.getCentroidFlag()) {
132  // we got a usable value, but the centroid flag is still be set, and that might affect
133  // the current measurement
134  flags.setValue(record, FlagHandler::FAILURE, true);
135  }
136  return result;
137 }
138 
140  _name(name)
141 {
142  // Instead of aliasing e.g. MyAlgorithm_flag_badShape->slot_Shape_flag, we actually
143  // look up the target of slot_Shape_flag, and alias that to MyAlgorithm_flag_badCentroid.
144  // That way, if someone changes the slots later, after we've already done the measurement,
145  // this alias still points to the right thing.
146  std::string aliasedFlagName = schema.join("slot", "Shape", "flag");
147  std::string slotFlagName = schema.getAliasMap()->apply(aliasedFlagName);
148  if (aliasedFlagName == slotFlagName) {
149  throw LSST_EXCEPT(
150  pex::exceptions::LogicError,
151  (boost::format("Alias for '%s' must be defined before initializing '%s' plugin.")
152  % aliasedFlagName % name).str()
153  );
154  }
155  schema.getAliasMap()->set(schema.join(name, "flag", "badShape"), slotFlagName);
156 }
157 
159  afw::table::SourceRecord & record,
160  FlagHandler const & flags
161 ) const {
162  if (!record.getTable()->getShapeKey().isValid()) {
163  throw LSST_EXCEPT(
164  FatalAlgorithmError,
165  (boost::format("%s requires a shape, but the shape slot is not defined") % _name).str()
166  );
167  }
168  afw::geom::ellipses::Quadrupole result = record.getShape();
169  if (utils::isnan(result.getIxx()) || utils::isnan(result.getIyy()) || utils::isnan(result.getIxy())
170  || result.getDeterminant() < 0
171  ) {
172  if (!record.getTable()->getShapeFlagKey().isValid()) {
173  throw LSST_EXCEPT(
174  pex::exceptions::RuntimeError,
175  (boost::format("%s: Shape slot value is NaN, but there is no Shape slot flag "
176  "(is the executionOrder for %s lower than that of the slot Shape?)")
177  % _name % _name).str()
178  );
179  }
180  if (!record.getShapeFlag()) {
181  throw LSST_EXCEPT(
182  pex::exceptions::RuntimeError,
183  (boost::format("%s: Shape slot value is NaN, but the Shape slot flag is not set "
184  "(is the executionOrder for %s lower than that of the slot Shape?)")
185  % _name % _name).str()
186  );
187  }
188  throw LSST_EXCEPT(
190  (boost::format("%s: Shape needed, and Shape slot measurement failed.") % _name).str(),
192  );
193  } else if (record.getTable()->getShapeFlagKey().isValid() && record.getShapeFlag()) {
194  // we got a usable value, but the shape flag might still be set, and that might affect
195  // the current measurement
196  flags.setValue(record, FlagHandler::FAILURE, true);
197  }
198  return result;
199 }
200 
201 }}} // 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:46
bool getShapeFlag() const
Return true if the measurement in the Shape slot failed.
Definition: Source.h:923
afw::geom::ellipses::Quadrupole operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
CentroidSlotDefinition::MeasValue getCentroid() const
Get the value of the Centroid slot measurement.
Definition: Source.h:903
#define PTR(...)
Definition: base.h:41
Represent a set of pixels of an arbitrary shape and size.
boost::shared_ptr< SourceTable const > getTable() const
Definition: Source.h:93
table::Key< std::string > name
Definition: ApCorrMap.cc:71
std::string const & _name
Definition: Mask.cc:678
boost::shared_ptr< AliasMap > getAliasMap() const
Definition: Schema.h:258
double const getIyy() const
Definition: Quadrupole.h:59
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
int isnan(T t)
Definition: ieee.h:110
bool getCentroidFlag() const
Return true if the measurement in the Centroid slot failed.
Definition: Source.h:911
SafeShapeExtractor(afw::table::Schema &schema, std::string const &name)
if(width!=gim.getWidth()||height!=gim.getHeight()||x0!=gim.getX0()||y0!=gim.getY0())
Definition: saturated.cc:47
void setValue(afw::table::BaseRecord &record, int i, bool value) const
Definition: FlagHandler.h:72
double const getIxx() const
Definition: Quadrupole.h:56
ShapeSlotDefinition::MeasValue getShape() const
Get the value of the Shape slot measurement.
Definition: Source.h:915
tbl::Schema schema
A set of pixels in an Image.
Definition: Footprint.h:62
SafeCentroidExtractor(afw::table::Schema &schema, std::string const &name, bool isCentroider=false)
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::string join(std::string const &a, std::string const &b) const
Join strings using the field delimiter appropriate for this Schema.
afw::geom::Point2D operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
double getDeterminant() const
Return the determinant of the matrix representation.
Definition: Quadrupole.h:85
double const getIxy() const
Definition: Quadrupole.h:62
Record class that contains measurements made on a single exposure.
Definition: Source.h:81