LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
_aggregates.cc
Go to the documentation of this file.
1 /*
2  * This file is part of afw.
3  *
4  * Developed for the LSST Data Management System.
5  * This product includes software developed by the LSST Project
6  * (https://www.lsst.org).
7  * See the COPYRIGHT file at the top-level directory of this distribution
8  * for details of code ownership.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #include "pybind11/pybind11.h"
25 #include "pybind11/eigen.h"
26 #include "pybind11/stl.h"
27 
28 #include "ndarray/pybind11.h"
29 
31 
32 #include "lsst/utils/python.h"
33 #include "lsst/geom/Angle.h"
34 #include "lsst/geom/SpherePoint.h"
35 #include "lsst/geom/Box.h"
36 #include "lsst/afw/table/Key.h"
37 #include "lsst/afw/table/Schema.h"
41 
42 namespace py = pybind11;
43 using namespace pybind11::literals;
44 
45 namespace lsst {
46 namespace afw {
47 namespace table {
48 
49 using utils::python::WrapperCollection;
50 
51 namespace {
52 
53 // We don't expose base classes (e.g. FunctorKey) to Python, since they're just used to
54 // define a CRTP interface in C++ and in Python that's just duck-typing.
55 
56 template <typename T>
57 using PyPointKey = py::class_<PointKey<T>, std::shared_ptr<PointKey<T>>>;
58 
59 template <typename Box>
60 using PyBoxKey = py::class_<BoxKey<Box>, std::shared_ptr<BoxKey<Box>>>;
61 
62 using PyCoordKey = py::class_<CoordKey, std::shared_ptr<CoordKey>>;
63 
64 using PyQuadrupoleKey = py::class_<QuadrupoleKey, std::shared_ptr<QuadrupoleKey>>;
65 
66 using PyEllipseKey = py::class_<EllipseKey, std::shared_ptr<EllipseKey>>;
67 
68 template <typename T, int N>
69 using PyCovarianceMatrixKey =
70  py::class_<CovarianceMatrixKey<T, N>, std::shared_ptr<CovarianceMatrixKey<T, N>>>;
71 
72 template <typename T>
73 static void declarePointKey(WrapperCollection &wrappers, std::string const &suffix) {
74  wrappers.wrapType(
75  PyPointKey<T>(wrappers.module, ("Point" + suffix + "Key").c_str()), [](auto &mod, auto &cls) {
76  cls.def(py::init<>());
77  cls.def(py::init<Key<T> const &, Key<T> const &>(), "x"_a, "y"_a);
78  cls.def(py::init<SubSchema const &>());
79  cls.def("__eq__", &PointKey<T>::operator==, py::is_operator());
80  cls.def("__ne__", &PointKey<T>::operator!=, py::is_operator());
81  cls.def("getX", &PointKey<T>::getX);
82  cls.def("getY", &PointKey<T>::getY);
83  cls.def("isValid", &PointKey<T>::isValid);
84  cls.def_static("addFields", &PointKey<T>::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
85  cls.def("set", [](PointKey<T> &self, BaseRecord &record,
86  lsst::geom::Point<T, 2> const &value) { return self.set(record, value); });
87  cls.def("get", &PointKey<T>::get);
88  });
89 };
90 
91 template <typename Box>
92 static void declareBoxKey(WrapperCollection &wrappers, std::string const &suffix) {
93  wrappers.wrapType(
94  PyBoxKey<Box>(wrappers.module, ("Box" + suffix + "Key").c_str()), [](auto &mod, auto &cls) {
95  using Element = typename Box::Element;
96  cls.def(py::init<>());
97  cls.def(py::init<PointKey<Element> const &, PointKey<Element> const &>(), "min"_a, "max"_a);
98  cls.def(py::init<SubSchema const &>());
99  cls.def("__eq__", &BoxKey<Box>::operator==, py::is_operator());
100  cls.def("__ne__", &BoxKey<Box>::operator!=, py::is_operator());
101  cls.def("getMin", &BoxKey<Box>::getMin);
102  cls.def("getMax", &BoxKey<Box>::getMax);
103  cls.def("isValid", &BoxKey<Box>::isValid);
104  cls.def_static("addFields", &BoxKey<Box>::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
105  cls.def("set", &BoxKey<Box>::set);
106  cls.def("get", &BoxKey<Box>::get);
107 
108  });
109 };
110 
111 static void declareCoordKey(WrapperCollection &wrappers) {
112  wrappers.wrapType(PyCoordKey(wrappers.module, "CoordKey"), [](auto &mod, auto &cls) {
113  cls.def(py::init<>());
114  cls.def(py::init<Key<lsst::geom::Angle>, Key<lsst::geom::Angle>>(), "ra"_a, "dec"_a);
115  cls.def(py::init<SubSchema const &>());
116  cls.def("__eq__", &CoordKey::operator==, py::is_operator());
117  cls.def("__ne__", &CoordKey::operator!=, py::is_operator());
118  cls.def_static("addFields", &CoordKey::addFields, "schema"_a, "name"_a, "doc"_a);
119  cls.def("getRa", &CoordKey::getRa);
120  cls.def("getDec", &CoordKey::getDec);
121  cls.def("isValid", &CoordKey::isValid);
122  cls.def("get", [](CoordKey &self, BaseRecord const &record) { return self.get(record); });
123  cls.def("set", &CoordKey::set);
124  });
125 }
126 
127 static void declareQuadrupoleKey(WrapperCollection &wrappers) {
128  wrappers.wrapType(PyQuadrupoleKey(wrappers.module, "QuadrupoleKey"), [](auto &mod, auto &cls) {
129  cls.def(py::init<>());
130  cls.def(py::init<Key<double> const &, Key<double> const &, Key<double> const &>(), "ixx"_a, "iyy"_a,
131  "ixy"_a);
132  cls.def(py::init<SubSchema const &>());
133  cls.def("__eq__", &QuadrupoleKey::operator==, py::is_operator());
134  cls.def("__nq__", &QuadrupoleKey::operator!=, py::is_operator());
135  cls.def_static("addFields", &QuadrupoleKey::addFields, "schema"_a, "name"_a, "doc"_a,
136  "coordType"_a = CoordinateType::PIXEL);
137  cls.def("getIxx", &QuadrupoleKey::getIxx);
138  cls.def("getIyy", &QuadrupoleKey::getIyy);
139  cls.def("getIxy", &QuadrupoleKey::getIxy);
140  cls.def("isValid", &QuadrupoleKey::isValid);
141  cls.def("set", &QuadrupoleKey::set);
142  cls.def("get", &QuadrupoleKey::get);
143  });
144 }
145 
146 static void declareEllipseKey(WrapperCollection &wrappers) {
147  wrappers.wrapType(PyEllipseKey(wrappers.module, "EllipseKey"), [](auto &mod, auto &cls) {
148  cls.def(py::init<>());
149  cls.def(py::init<QuadrupoleKey const &, PointKey<double> const &>(), "qKey"_a, "pKey"_a);
150  cls.def(py::init<SubSchema const &>());
151  cls.def("__eq__", &EllipseKey::operator==, py::is_operator());
152  cls.def("__nq__", &EllipseKey::operator!=, py::is_operator());
153  cls.def_static("addFields", &EllipseKey::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
154  cls.def("get", &EllipseKey::get);
155  cls.def("set", &EllipseKey::set);
156  cls.def("isValid", &EllipseKey::isValid);
157  cls.def("getCore", &EllipseKey::getCore);
158  cls.def("getCenter", &EllipseKey::getCenter);
159  });
160 }
161 
162 template <typename T, int N>
163 static void declareCovarianceMatrixKey(WrapperCollection &wrappers, const ::std::string &suffix) {
164  wrappers.wrapType(
165  PyCovarianceMatrixKey<T, N>(wrappers.module, ("CovarianceMatrix" + suffix + "Key").c_str()),
166  [](auto &mod, auto &cls) {
167  using ErrKeyArray = std::vector<Key<T>>;
168  using CovarianceKeyArray = std::vector<Key<T>>;
169  using NameArray = std::vector<std::string>;
170 
171  cls.def(py::init<>());
172  // Ordering of the next two ctor declaration matters, as a workaround for DM-8580.
173  cls.def(py::init<SubSchema const &, NameArray const &>());
174  cls.def(py::init<ErrKeyArray const &, CovarianceKeyArray const &>(), "err"_a,
175  "cov"_a = CovarianceKeyArray());
176  cls.def("__eq__", &CovarianceMatrixKey<T, N>::operator==, py::is_operator());
177  cls.def("__ne__", &CovarianceMatrixKey<T, N>::operator!=, py::is_operator());
178  cls.def_static("addFields",
179  (CovarianceMatrixKey<T, N>(*)(Schema &, std::string const &, NameArray const &,
180  std::string const &, bool)) &
181  CovarianceMatrixKey<T, N>::addFields,
182  "schema"_a, "prefix"_a, "names"_a, "unit"_a, "diagonalOnly"_a = false);
183  cls.def_static("addFields",
184  (CovarianceMatrixKey<T, N>(*)(Schema &, std::string const &, NameArray const &,
185  NameArray const &, bool)) &
186  CovarianceMatrixKey<T, N>::addFields,
187  "schema"_a, "prefix"_a, "names"_a, "units"_a, "diagonalOnly"_a = false);
188  cls.def("set", [](CovarianceMatrixKey<T, N> &cov, BaseRecord &record,
189  Eigen::Matrix<T, N, N> const &value) { return cov.set(record, value); });
190  cls.def("get", [](CovarianceMatrixKey<T, N> &cov, BaseRecord const &record) {
191  return cov.get(record);
192  });
193  cls.def("isValid", &CovarianceMatrixKey<T, N>::isValid);
194  cls.def("setElement", &CovarianceMatrixKey<T, N>::setElement);
195  cls.def("getElement", &CovarianceMatrixKey<T, N>::getElement);
196  });
197 }
198 
199 } // namespace
200 
201 void wrapAggregates(WrapperCollection &wrappers) {
202  // TODO: uncomment once afw.geom uses WrapperCollection
203  // wrappers.addSignatureDependency("lsst.afw.geom.ellipses");
204 
205  wrappers.wrapType(py::enum_<CoordinateType>(wrappers.module, "CoordinateType"), [](auto &mod, auto &enm) {
206  enm.value("PIXEL", CoordinateType::PIXEL);
207  enm.value("CELESTIAL", CoordinateType::CELESTIAL);
208  enm.export_values();
209  });
210 
211  declarePointKey<double>(wrappers, "2D");
212  declarePointKey<int>(wrappers, "2I");
213 
214  declareBoxKey<lsst::geom::Box2D>(wrappers, "2D");
215  declareBoxKey<lsst::geom::Box2I>(wrappers, "2I");
216 
217  declareCoordKey(wrappers);
218  declareQuadrupoleKey(wrappers);
219  declareEllipseKey(wrappers);
220 
221  declareCovarianceMatrixKey<float, 2>(wrappers, "2f");
222  declareCovarianceMatrixKey<float, 3>(wrappers, "3f");
223  declareCovarianceMatrixKey<float, 4>(wrappers, "4f");
224  declareCovarianceMatrixKey<float, Eigen::Dynamic>(wrappers, "Xf");
225  declareCovarianceMatrixKey<double, 2>(wrappers, "2d");
226  declareCovarianceMatrixKey<double, 3>(wrappers, "3d");
227  declareCovarianceMatrixKey<double, 4>(wrappers, "4d");
228  declareCovarianceMatrixKey<double, Eigen::Dynamic>(wrappers, "Xd");
229 }
230 
231 } // namespace table
232 } // namespace afw
233 } // namespace lsst
daf::base::PropertySet * set
Definition: fits.cc:912
bool isValid
Definition: fits.cc:399
void wrapAggregates(WrapperCollection &wrappers)
Definition: _aggregates.cc:201
A base class for image defects.