LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
_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"
35#include "lsst/geom/Box.h"
36#include "lsst/afw/table/Key.h"
41
42namespace py = pybind11;
43using namespace pybind11::literals;
44
45namespace lsst {
46namespace afw {
47namespace table {
48
49using utils::python::WrapperCollection;
50
51namespace {
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
56template <typename T>
57using PyPointKey = py::class_<PointKey<T>, std::shared_ptr<PointKey<T>>>;
58
59template <typename T>
60using PyPoint3Key = py::class_<Point3Key<T>, std::shared_ptr<Point3Key<T>>>;
61
62template <typename Box>
63using PyBoxKey = py::class_<BoxKey<Box>, std::shared_ptr<BoxKey<Box>>>;
64
65using PyCoordKey = py::class_<CoordKey, std::shared_ptr<CoordKey>>;
66
67using PyQuadrupoleKey = py::class_<QuadrupoleKey, std::shared_ptr<QuadrupoleKey>>;
68
69using PyEllipseKey = py::class_<EllipseKey, std::shared_ptr<EllipseKey>>;
70
71template <typename T, int N>
72using PyCovarianceMatrixKey =
73 py::class_<CovarianceMatrixKey<T, N>, std::shared_ptr<CovarianceMatrixKey<T, N>>>;
74
75template <typename T>
76static void declarePointKey(WrapperCollection &wrappers, std::string const &suffix) {
77 wrappers.wrapType(
78 PyPointKey<T>(wrappers.module, ("Point" + suffix + "Key").c_str()), [](auto &mod, auto &cls) {
79 cls.def(py::init<>());
80 cls.def(py::init<Key<T> const &, Key<T> const &>(), "x"_a, "y"_a);
81 cls.def(py::init<SubSchema const &>());
82 cls.def("__eq__", &PointKey<T>::operator==, py::is_operator());
83 cls.def("__ne__", &PointKey<T>::operator!=, py::is_operator());
84 cls.def("getX", &PointKey<T>::getX);
85 cls.def("getY", &PointKey<T>::getY);
86 cls.def("isValid", &PointKey<T>::isValid);
87 cls.def_static("addFields", &PointKey<T>::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
88 cls.def("set", [](PointKey<T> &self, BaseRecord &record,
89 lsst::geom::Point<T, 2> const &value) { return self.set(record, value); });
90 cls.def("get", &PointKey<T>::get);
91 });
92};
93
94template <typename T>
95static void declarePoint3Key(WrapperCollection &wrappers, std::string const &suffix) {
96 wrappers.wrapType(
97 PyPoint3Key<T>(wrappers.module, ("Point3" + suffix + "Key").c_str()), [](auto &mod, auto &cls) {
98 cls.def(py::init<>());
99 cls.def(py::init<Key<T> const &, Key<T> const &, Key<T> const &>(), "x"_a, "y"_a, "z"_a);
100 cls.def(py::init<SubSchema const &>());
101 cls.def("__eq__", &Point3Key<T>::operator==, py::is_operator());
102 cls.def("__ne__", &Point3Key<T>::operator!=, py::is_operator());
103 cls.def("getX", &Point3Key<T>::getX);
104 cls.def("getY", &Point3Key<T>::getY);
105 cls.def("isValid", &Point3Key<T>::isValid);
106 cls.def_static("addFields", &Point3Key<T>::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
107 cls.def("set", [](Point3Key<T> &self, BaseRecord &record,
108 lsst::geom::Point<T, 3> const &value) { return self.set(record, value); });
109 cls.def("get", &Point3Key<T>::get);
110 });
111};
112
113template <typename Box>
114static void declareBoxKey(WrapperCollection &wrappers, std::string const &suffix) {
115 wrappers.wrapType(
116 PyBoxKey<Box>(wrappers.module, ("Box" + suffix + "Key").c_str()), [](auto &mod, auto &cls) {
117 using Element = typename Box::Element;
118 cls.def(py::init<>());
119 cls.def(py::init<PointKey<Element> const &, PointKey<Element> const &>(), "min"_a, "max"_a);
120 cls.def(py::init<SubSchema const &>());
121 cls.def("__eq__", &BoxKey<Box>::operator==, py::is_operator());
122 cls.def("__ne__", &BoxKey<Box>::operator!=, py::is_operator());
123 cls.def("getMin", &BoxKey<Box>::getMin);
124 cls.def("getMax", &BoxKey<Box>::getMax);
125 cls.def("isValid", &BoxKey<Box>::isValid);
126 cls.def_static("addFields", &BoxKey<Box>::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
127 cls.def("set", &BoxKey<Box>::set);
128 cls.def("get", &BoxKey<Box>::get);
129
130 });
131};
132
133static void declareCoordKey(WrapperCollection &wrappers) {
134 wrappers.wrapType(PyCoordKey(wrappers.module, "CoordKey"), [](auto &mod, auto &cls) {
135 cls.def(py::init<>());
136 cls.def(py::init<Key<lsst::geom::Angle>, Key<lsst::geom::Angle>>(), "ra"_a, "dec"_a);
137 cls.def(py::init<SubSchema const &>());
138 cls.def("__eq__", &CoordKey::operator==, py::is_operator());
139 cls.def("__ne__", &CoordKey::operator!=, py::is_operator());
140 cls.def_static("addFields", &CoordKey::addFields, "schema"_a, "name"_a, "doc"_a);
141 cls.def_static("addErrorFields", &CoordKey::addErrorFields, "schema"_a);
142 cls.def_static("getErrorKey", &CoordKey::getErrorKey, "schema"_a);
143 cls.def("getRa", &CoordKey::getRa);
144 cls.def("getDec", &CoordKey::getDec);
145 cls.def("isValid", &CoordKey::isValid);
146 cls.def("get", [](CoordKey &self, BaseRecord const &record) { return self.get(record); });
147 cls.def("set", &CoordKey::set);
148 });
149}
150
151static void declareQuadrupoleKey(WrapperCollection &wrappers) {
152 wrappers.wrapType(PyQuadrupoleKey(wrappers.module, "QuadrupoleKey"), [](auto &mod, auto &cls) {
153 cls.def(py::init<>());
154 cls.def(py::init<Key<double> const &, Key<double> const &, Key<double> const &>(), "ixx"_a, "iyy"_a,
155 "ixy"_a);
156 cls.def(py::init<SubSchema const &>());
157 cls.def("__eq__", &QuadrupoleKey::operator==, py::is_operator());
158 cls.def("__nq__", &QuadrupoleKey::operator!=, py::is_operator());
159 cls.def_static("addFields", &QuadrupoleKey::addFields, "schema"_a, "name"_a, "doc"_a,
160 "coordType"_a = CoordinateType::PIXEL);
161 cls.def("getIxx", &QuadrupoleKey::getIxx);
162 cls.def("getIyy", &QuadrupoleKey::getIyy);
163 cls.def("getIxy", &QuadrupoleKey::getIxy);
164 cls.def("isValid", &QuadrupoleKey::isValid);
165 cls.def("set", &QuadrupoleKey::set);
166 cls.def("get", &QuadrupoleKey::get);
167 });
168}
169
170static void declareEllipseKey(WrapperCollection &wrappers) {
171 wrappers.wrapType(PyEllipseKey(wrappers.module, "EllipseKey"), [](auto &mod, auto &cls) {
172 cls.def(py::init<>());
173 cls.def(py::init<QuadrupoleKey const &, PointKey<double> const &>(), "qKey"_a, "pKey"_a);
174 cls.def(py::init<SubSchema const &>());
175 cls.def("__eq__", &EllipseKey::operator==, py::is_operator());
176 cls.def("__nq__", &EllipseKey::operator!=, py::is_operator());
177 cls.def_static("addFields", &EllipseKey::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
178 cls.def("get", &EllipseKey::get);
179 cls.def("set", &EllipseKey::set);
180 cls.def("isValid", &EllipseKey::isValid);
181 cls.def("getCore", &EllipseKey::getCore);
182 cls.def("getCenter", &EllipseKey::getCenter);
183 });
184}
185
186template <typename T, int N>
187static void declareCovarianceMatrixKey(WrapperCollection &wrappers, const ::std::string &suffix) {
188 wrappers.wrapType(
189 PyCovarianceMatrixKey<T, N>(wrappers.module, ("CovarianceMatrix" + suffix + "Key").c_str()),
190 [](auto &mod, auto &cls) {
191 using ErrKeyArray = std::vector<Key<T>>;
192 using CovarianceKeyArray = std::vector<Key<T>>;
193 using NameArray = std::vector<std::string>;
194
195 cls.def(py::init<>());
196 // Ordering of the next two ctor declaration matters, as a workaround for DM-8580.
197 cls.def(py::init<SubSchema const &, NameArray const &>());
198 cls.def(py::init<ErrKeyArray const &, CovarianceKeyArray const &>(), "err"_a,
199 "cov"_a = CovarianceKeyArray());
200 cls.def("__eq__", &CovarianceMatrixKey<T, N>::operator==, py::is_operator());
201 cls.def("__ne__", &CovarianceMatrixKey<T, N>::operator!=, py::is_operator());
202 cls.def_static("addFields",
203 (CovarianceMatrixKey<T, N>(*)(Schema &, std::string const &, NameArray const &,
204 std::string const &, bool)) &
205 CovarianceMatrixKey<T, N>::addFields,
206 "schema"_a, "prefix"_a, "names"_a, "unit"_a, "diagonalOnly"_a = false);
207 cls.def_static("addFields",
208 (CovarianceMatrixKey<T, N>(*)(Schema &, std::string const &, NameArray const &,
209 NameArray const &, bool)) &
210 CovarianceMatrixKey<T, N>::addFields,
211 "schema"_a, "prefix"_a, "names"_a, "units"_a, "diagonalOnly"_a = false);
212 cls.def("set", [](CovarianceMatrixKey<T, N> &cov, BaseRecord &record,
213 Eigen::Matrix<T, N, N> const &value) { return cov.set(record, value); });
214 cls.def("get", [](CovarianceMatrixKey<T, N> &cov, BaseRecord const &record) {
215 return cov.get(record);
216 });
217 cls.def("isValid", &CovarianceMatrixKey<T, N>::isValid);
218 cls.def("setElement", &CovarianceMatrixKey<T, N>::setElement);
219 cls.def("getElement", &CovarianceMatrixKey<T, N>::getElement);
220 });
221}
222
223} // namespace
224
225void wrapAggregates(WrapperCollection &wrappers) {
226 // TODO: uncomment once afw.geom uses WrapperCollection
227 // wrappers.addSignatureDependency("lsst.afw.geom.ellipses");
228
229 wrappers.wrapType(py::enum_<CoordinateType>(wrappers.module, "CoordinateType"), [](auto &mod, auto &enm) {
230 enm.value("PIXEL", CoordinateType::PIXEL);
231 enm.value("CELESTIAL", CoordinateType::CELESTIAL);
232 enm.export_values();
233 });
234
235 declarePointKey<double>(wrappers, "2D");
236 declarePointKey<int>(wrappers, "2I");
237 declarePoint3Key<double>(wrappers, "D");
238 declarePoint3Key<int>(wrappers, "I");
239
240 declareBoxKey<lsst::geom::Box2D>(wrappers, "2D");
241 declareBoxKey<lsst::geom::Box2I>(wrappers, "2I");
242
243 declareCoordKey(wrappers);
244 declareQuadrupoleKey(wrappers);
245 declareEllipseKey(wrappers);
246
255}
256
257} // namespace table
258} // namespace afw
259} // namespace lsst
Tag types used to declare specialized field types.
Definition misc.h:31
lsst::geom::Point< T, 3 > get(BaseRecord const &record) const override
Get a Point from the given record.
Definition aggregates.cc:69
lsst::geom::Point< T, 2 > get(BaseRecord const &record) const override
Get a Point from the given record.
Definition aggregates.cc:44
void wrapAggregates(WrapperCollection &wrappers)