LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
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  typedef std::vector<Key<T>> ErrKeyArray;
168  typedef std::vector<Key<T>> CovarianceKeyArray;
169  typedef std::vector<std::string> NameArray;
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 
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
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition: python.h:242
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition: python.h:448
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
Definition: python.h:391
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.