lsst.meas.base  14.0-9-g876143c
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 
34  afw::table::Schema & schema,
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(
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(
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(
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(
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(
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(
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, flags.getFailureFlagNumber(), 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, flags.getFailureFlagNumber(), 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(
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(
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(
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(
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(),
196  flags.getFailureFlagNumber()
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, flags.getFailureFlagNumber(), true);
202  }
203  return result;
204 }
205 
206 }}} // lsst::meas::base
ShapeSlotDefinition::MeasValue getShape() const
std::string const & _name
std::string join(std::string const &a, std::string const &b) const
std::shared_ptr< Footprint > getFootprint() const
#define PTR(...)
void setValue(afw::table::BaseRecord &record, std::size_t i, bool value) const
Set the flag field corresponding to the given flag index.
Definition: FlagHandler.h:276
Exception to be thrown when a measurement algorithm experiences a known failure mode.
Definition: exceptions.h:48
string name
Exception to be thrown when a measurement algorithm experiences a fatal error.
Definition: exceptions.h:83
std::size_t getFailureFlagNumber() const
Get the index of the General Failure flag, if one is defined.
Definition: FlagHandler.h:300
STL class.
Utility class for handling flag fields that indicate the failure modes of an algorithm.
Definition: FlagHandler.h:156
afw::geom::ellipses::Quadrupole operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
Extract a shape from the given record.
std::shared_ptr< SourceTable const > getTable() const
SafeShapeExtractor(afw::table::Schema &schema, std::string const &name)
Construct the extractor, creating a flag alias that indicates failure in the input centroid by linkin...
SafeCentroidExtractor(afw::table::Schema &schema, std::string const &name, bool isCentroider=false)
Construct the extractor, creating a flag alias that indicates failure in the input centroid by linkin...
#define LSST_EXCEPT(type,...)
std::shared_ptr< AliasMap > getAliasMap() const
T isnan(T... args)
afw::geom::Point2D operator()(afw::table::SourceRecord &record, FlagHandler const &flags) const
Extract a position from the given record.
CentroidSlotDefinition::MeasValue getCentroid() const