lsst.meas.base  14.0-22-gcfb0d17
ShapeUtilities.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2015 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
25 
26 namespace lsst { namespace meas { namespace base {
27 
29  xx(std::numeric_limits<ShapeElement>::quiet_NaN()),
30  yy(std::numeric_limits<ShapeElement>::quiet_NaN()),
31  xy(std::numeric_limits<ShapeElement>::quiet_NaN()),
32  xxSigma(std::numeric_limits<ErrElement>::quiet_NaN()),
33  yySigma(std::numeric_limits<ErrElement>::quiet_NaN()),
34  xySigma(std::numeric_limits<ErrElement>::quiet_NaN()),
35  xx_yy_Cov(std::numeric_limits<ErrElement>::quiet_NaN()),
36  xx_xy_Cov(std::numeric_limits<ErrElement>::quiet_NaN()),
37  yy_xy_Cov(std::numeric_limits<ErrElement>::quiet_NaN())
38 {}
39 
40 Shape const ShapeResult::getShape() const { return Shape(xx, yy, xy); }
41 
42 void ShapeResult::setShape(Shape const & shape) {
43  xx = shape.getIxx();
44  yy = shape.getIyy();
45  xy = shape.getIxy();
46 }
47 
49  ShapeCov m;
50  m <<
54  return m;
55 }
56 
57 void ShapeResult::setShapeErr(ShapeCov const & matrix) {
58  xxSigma = std::sqrt(matrix(0, 0));
59  yySigma = std::sqrt(matrix(1, 1));
60  xySigma = std::sqrt(matrix(2, 2));
61  xx_yy_Cov = matrix(0, 1);
62  xx_xy_Cov = matrix(0, 2);
63  yy_xy_Cov = matrix(1, 2);
64 }
65 
66 void ShapeResult::setShapeErr(ErrElement _xxSigma, ErrElement _yySigma, ErrElement _xySigma) {
67  xxSigma = _xxSigma;
68  yySigma = _yySigma;
69  xySigma = _xySigma;
70  xx_yy_Cov = 0.0;
71  xx_xy_Cov = 0.0;
72  yy_xy_Cov = 0.0;
73 }
74 
76  afw::table::Schema & schema,
77  std::string const & name,
78  std::string const & doc,
79  UncertaintyEnum uncertainty,
81 ) {
84  schema,
85  name,
86  doc,
87  coordType
88  );
89  if (uncertainty != NO_UNCERTAINTY) {
92  sigma[0] = schema.addField<ErrElement>(
93  schema.join(name, "xxSigma"), "1-sigma uncertainty on xx moment",
94  coordType == afw::table::CoordinateType::PIXEL ? "pixel^2" : "rad^2"
95  );
96  sigma[1] = schema.addField<ErrElement>(
97  schema.join(name, "yySigma"), "1-sigma uncertainty on yy moment",
98  coordType == afw::table::CoordinateType::PIXEL ? "pixel^2" : "rad^2"
99  );
100  sigma[2] = schema.addField<ErrElement>(
101  schema.join(name, "xySigma"), "1-sigma uncertainty on xy moment",
102  coordType == afw::table::CoordinateType::PIXEL ? "pixel^2" : "rad^2"
103  );
104  if (uncertainty == FULL_COVARIANCE) {
105  cov.push_back(
106  schema.addField<ErrElement>(
107  schema.join(name, "xx_yy_Cov"), "uncertainty covariance in xx and yy",
108  coordType == afw::table::CoordinateType::PIXEL ? "pixel^4" : "rad^4"
109  )
110  );
111  cov.push_back(
112  schema.addField<ErrElement>(
113  schema.join(name, "xx_xy_Cov"), "uncertainty covariance in xx and xy",
114  coordType == afw::table::CoordinateType::PIXEL ? "pixel^4" : "rad^4"
115  )
116  );
117  cov.push_back(
118  schema.addField<ErrElement>(
119  schema.join(name, "yy_xy_Cov"), "uncertainty covariance in yy and xy",
120  coordType == afw::table::CoordinateType::PIXEL ? "pixel^4" : "rad^4"
121  )
122  );
123  }
124  r._shapeErr = afw::table::CovarianceMatrixKey<ErrElement,3>(sigma, cov);
125  }
126  return r;
127 }
128 
129 namespace {
130 
131 std::vector<std::string> getNameVector() {
133  v.push_back("xx");
134  v.push_back("yy");
135  v.push_back("xy");
136  return v;
137 }
138 
139 } // anonymous
140 
142  _shape(s)
143 {
144  static std::vector<std::string> names = getNameVector(); // C++11 TODO: just use initializer list
145  try {
146  _shapeErr = afw::table::CovarianceMatrixKey<ErrElement,3>(s, names);
147  } catch (pex::exceptions::NotFoundError &) {}
148 }
149 
151  ShapeResult r;
152  r.setShape(record.get(_shape));
153  if (_shapeErr.isValid()) {
154  r.setShapeErr(record.get(_shapeErr));
155  }
156  return r;
157 }
158 
159 void ShapeResultKey::set(afw::table::BaseRecord & record, ShapeResult const & value) const {
160  record.set(_shape, value.getShape());
161  if (_shapeErr.isValid()) {
162  record.set(_shapeErr, value.getShapeErr());
163  }
164 }
165 
167  typedef afw::geom::LinearTransform LT;
168  Eigen::Matrix<ShapeElement,3,3,Eigen::DontAlign> m;
169  m << xform[LT::XX]*xform[LT::XX], xform[LT::XY]*xform[LT::XY], 2*xform[LT::XX]*xform[LT::XY],
170  xform[LT::YX]*xform[LT::YX], xform[LT::YY]*xform[LT::YY], 2*xform[LT::YX]*xform[LT::YY],
171  xform[LT::XX]*xform[LT::YX], xform[LT::XY]*xform[LT::YY],
172  xform[LT::XX]*xform[LT::YY] + xform[LT::XY]*xform[LT::YX];
173  return m;
174 }
175 
176 }}} // lsst::meas::base
virtual void set(afw::table::BaseRecord &record, ShapeResult const &value) const
Set a ShapeResult in the given record.
ShapeTrMatrix makeShapeTransformMatrix(afw::geom::LinearTransform const &xform)
Construct a matrix suitable for transforming second moments.
ErrElement yy_xy_Cov
yy,xy term in the uncertainty convariance matrix
virtual ShapeResult get(afw::table::BaseRecord const &record) const
Get a ShapeResult from the given record.
Eigen::Matrix< ErrElement, 3, 3, Eigen::DontAlign > ShapeCov
Definition: constants.h:59
UncertaintyEnum
An enum used to specify how much uncertainty information measurement algorithms provide.
Definition: constants.h:41
std::string join(std::string const &a, std::string const &b) const
Shape const getShape() const
Return an afw::geom::ellipses object corresponding to xx, yy, xy.
ShapeResultKey()
Default constructor; instance will not be usuable unless subsequently assigned to.
The full covariance matrix is provided.
Definition: constants.h:44
STL namespace.
Key< T > addField(Field< T > const &field, bool doReplace=false)
ErrElement xySigma
1-Sigma uncertainty on xy (sqrt of variance)
Field< T >::Value get(Key< T > const &key) const
float ErrElement
Definition: constants.h:53
afw::table::Key< double > sigma
A FunctorKey for ShapeResult.
STL class.
ErrElement xx_xy_Cov
xx,xy term in the uncertainty convariance matrix
void setShapeErr(ShapeCov const &matrix)
Set the struct uncertainty elements from the given matrix, with rows and columns ordered (xx...
ErrElement xx_yy_Cov
xx,yy term in the uncertainty convariance matrix
afw::geom::ellipses::Quadrupole Shape
Definition: constants.h:58
T push_back(T... args)
A reusable struct for moments-based shape measurements.
static QuadrupoleKey addFields(Schema &schema, std::string const &name, std::string const &doc, CoordinateType coordType=CoordinateType::PIXEL)
ShapeResult()
Constructor; initializes everything to NaN.
STL class.
Algorithm provides no uncertainy information at all.
Definition: constants.h:42
ErrElement yySigma
1-Sigma uncertainty on yy (sqrt of variance)
void set(Key< T > const &key, U const &value)
double ShapeElement
Definition: constants.h:55
ErrElement xxSigma
1-Sigma uncertainty on xx (sqrt of variance)
T sqrt(T... args)
m
void setShape(Shape const &shape)
Set struct elements from the given Quadrupole object.
Eigen::Matrix< ShapeElement, 3, 3, Eigen::DontAlign > ShapeTrMatrix
Definition: constants.h:60
ShapeCov const getShapeErr() const
Return the 3x3 symmetric covariance matrix, with rows and columns ordered (xx, yy, xy)
static ShapeResultKey addFields(afw::table::Schema &schema, std::string const &name, std::string const &doc, UncertaintyEnum uncertainty, afw::table::CoordinateType coordType=afw::table::CoordinateType::PIXEL)
Add the appropriate fields to a Schema, and return a ShapeResultKey that manages them.