LSST Applications g0603fd7c41+501e3db9f9,g0aad566f14+23d8574c86,g0dd44d6229+a1a4c8b791,g2079a07aa2+86d27d4dc4,g2305ad1205+a62672bbc1,g2bbee38e9b+047b288a59,g337abbeb29+047b288a59,g33d1c0ed96+047b288a59,g3a166c0a6a+047b288a59,g3d1719c13e+23d8574c86,g487adcacf7+cb7fd919b2,g4be5004598+23d8574c86,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+4a9e435310,g63cd9335cc+585e252eca,g858d7b2824+23d8574c86,g88963caddf+0cb8e002cc,g99cad8db69+43388bcaec,g9ddcbc5298+9a081db1e4,ga1e77700b3+a912195c07,gae0086650b+585e252eca,gb0e22166c9+60f28cb32d,gb2522980b2+793639e996,gb3a676b8dc+b4feba26a1,gb4b16eec92+63f8520565,gba4ed39666+c2a2e4ac27,gbb8dafda3b+a5d255a82e,gc120e1dc64+d820f8acdb,gc28159a63d+047b288a59,gc3e9b769f7+f4f1cc6b50,gcf0d15dbbd+a1a4c8b791,gdaeeff99f8+f9a426f77a,gdb0af172c8+b6d5496702,ge79ae78c31+047b288a59,w.2024.19
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)