lsst.afw  13.0-98-g5a654715e+2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
aggregates.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 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 <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #include "pybind11/pybind11.h"
24 #include "pybind11/stl.h"
25 
26 #include "numpy/arrayobject.h"
27 #include "ndarray/pybind11.h"
28 
30 
31 #include "lsst/afw/coord/Coord.h"
32 #include "lsst/afw/geom/Angle.h"
33 #include "lsst/afw/table/Key.h"
34 #include "lsst/afw/table/Schema.h"
38 
39 namespace py = pybind11;
40 using namespace pybind11::literals;
41 
42 namespace lsst {
43 namespace afw {
44 namespace table {
45 namespace {
46 
47 // We don't expose base classes (e.g. FunctorKey) to Python, since they're just used to
48 // define a CRTP interface in C++ and in Python that's just duck-typing.
49 
50 template <typename T>
51 using PyPointKey = py::class_<PointKey<T>, std::shared_ptr<PointKey<T>>>;
52 
53 using PyCoordKey = py::class_<CoordKey, std::shared_ptr<CoordKey>>;
54 
55 using PyQuadrupoleKey = py::class_<QuadrupoleKey, std::shared_ptr<QuadrupoleKey>>;
56 
57 using PyEllipseKey = py::class_<EllipseKey, std::shared_ptr<EllipseKey>>;
58 
59 template <typename T, int N>
60 using PyCovarianceMatrixKey =
61  py::class_<CovarianceMatrixKey<T, N>, std::shared_ptr<CovarianceMatrixKey<T, N>>>;
62 
63 template <typename T>
64 static void declarePointKey(py::module &mod, std::string const &suffix) {
65  PyPointKey<T> cls(mod, ("Point" + suffix + "Key").c_str());
66  cls.def(py::init<>());
67  cls.def(py::init<Key<T> const &, Key<T> const &>(), "x"_a, "y"_a);
68  cls.def(py::init<SubSchema const &>());
69  cls.def("__eq__", &PointKey<T>::operator==, py::is_operator());
70  cls.def("__ne__", &PointKey<T>::operator!=, py::is_operator());
71  cls.def("getX", &PointKey<T>::getX);
72  cls.def("getY", &PointKey<T>::getY);
73  cls.def("isValid", &PointKey<T>::isValid);
74  cls.def_static("addFields", &PointKey<T>::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
75  cls.def("set", [](PointKey<T> &self, BaseRecord &record, geom::Point<T, 2> const &value) {
76  return self.set(record, value);
77  });
78  cls.def("get", &PointKey<T>::get);
79 };
80 
81 static void declareCoordKey(py::module &mod) {
82  PyCoordKey cls(mod, "CoordKey");
83  cls.def(py::init<>());
84  cls.def(py::init<Key<lsst::afw::geom::Angle>, Key<lsst::afw::geom::Angle>>(), "ra"_a, "dec"_a);
85  cls.def(py::init<SubSchema const &>());
86  cls.def("__eq__", &CoordKey::operator==, py::is_operator());
87  cls.def("__ne__", &CoordKey::operator!=, py::is_operator());
88  cls.def_static("addFields", &CoordKey::addFields, "schema"_a, "name"_a, "doc"_a);
89  cls.def("getRa", &CoordKey::getRa);
90  cls.def("getDec", &CoordKey::getDec);
91  cls.def("isValid", &CoordKey::isValid);
92  cls.def("get", [](CoordKey &self, BaseRecord const &record) { return self.get(record); });
93  cls.def("set",
94  (void (CoordKey::*)(BaseRecord & record, coord::IcrsCoord const &value) const) & CoordKey::set);
95  cls.def("set",
96  (void (CoordKey::*)(BaseRecord & record, coord::Coord const &value) const) & CoordKey::set);
97 }
98 
99 static void declareQuadrupoleKey(py::module &mod) {
100  PyQuadrupoleKey cls(mod, "QuadrupoleKey");
101  cls.def(py::init<>());
102  cls.def(py::init<Key<double> const &, Key<double> const &, Key<double> const &>(), "ixx"_a, "iyy"_a,
103  "ixy"_a);
104  cls.def(py::init<SubSchema const &>());
105  cls.def("__eq__", &QuadrupoleKey::operator==, py::is_operator());
106  cls.def("__nq__", &QuadrupoleKey::operator!=, py::is_operator());
107  cls.def_static("addFields", &QuadrupoleKey::addFields, "schema"_a, "name"_a, "doc"_a,
108  "coordType"_a = CoordinateType::PIXEL);
109  cls.def("getIxx", &QuadrupoleKey::getIxx);
110  cls.def("getIyy", &QuadrupoleKey::getIyy);
111  cls.def("getIxy", &QuadrupoleKey::getIxy);
112  cls.def("isValid", &QuadrupoleKey::isValid);
113  cls.def("set", &QuadrupoleKey::set);
114  cls.def("get", &QuadrupoleKey::get);
115 }
116 
117 static void declareEllipseKey(py::module &mod) {
118  PyEllipseKey cls(mod, "EllipseKey");
119  cls.def(py::init<>());
120  cls.def(py::init<QuadrupoleKey const &, PointKey<double> const &>(), "qKey"_a, "pKey"_a);
121  cls.def(py::init<SubSchema const &>());
122  cls.def("__eq__", &EllipseKey::operator==, py::is_operator());
123  cls.def("__nq__", &EllipseKey::operator!=, py::is_operator());
124  cls.def_static("addFields", &EllipseKey::addFields, "schema"_a, "name"_a, "doc"_a, "unit"_a);
125  cls.def("get", &EllipseKey::get);
126  cls.def("set", &EllipseKey::set);
127  cls.def("isValid", &EllipseKey::isValid);
128  cls.def("getCore", &EllipseKey::getCore);
129  cls.def("getCenter", &EllipseKey::getCenter);
130 }
131 
132 template <typename T, int N>
133 static void declareCovarianceMatrixKey(py::module &mod, const ::std::string &suffix) {
134  typedef std::vector<Key<T>> SigmaKeyArray;
135  typedef std::vector<Key<T>> CovarianceKeyArray;
136  typedef std::vector<std::string> NameArray;
137 
138  PyCovarianceMatrixKey<T, N> cls(mod, ("CovarianceMatrix" + suffix + "Key").c_str());
139 
140  cls.def(py::init<>());
141  // Ordering of the next two ctor declaration matters, as a workaround for DM-8580.
142  cls.def(py::init<SubSchema const &, NameArray const &>());
143  cls.def(py::init<SigmaKeyArray const &, CovarianceKeyArray const &>(), "sigma"_a,
144  "cov"_a = CovarianceKeyArray());
145  cls.def("__eq__", &CovarianceMatrixKey<T, N>::operator==, py::is_operator());
146  cls.def("__ne__", &CovarianceMatrixKey<T, N>::operator!=, py::is_operator());
147  cls.def_static("addFields", (CovarianceMatrixKey<T, N>(*)(Schema &, std::string const &,
148  NameArray const &, std::string const &, bool)) &
149  CovarianceMatrixKey<T, N>::addFields,
150  "schema"_a, "prefix"_a, "names"_a, "unit"_a, "diagonalOnly"_a = false);
151  cls.def_static("addFields", (CovarianceMatrixKey<T, N>(*)(Schema &, std::string const &,
152  NameArray const &, NameArray const &, bool)) &
153  CovarianceMatrixKey<T, N>::addFields,
154  "schema"_a, "prefix"_a, "names"_a, "units"_a, "diagonalOnly"_a = false);
155  cls.def("set", [](CovarianceMatrixKey<T, N> &cov, BaseRecord &record,
156  Eigen::Matrix<T, N, N> const &value) { return cov.set(record, value); });
157  cls.def("get", [](CovarianceMatrixKey<T, N> &cov, BaseRecord const &record) { return cov.get(record); });
158  cls.def("isValid", &CovarianceMatrixKey<T, N>::isValid);
159  cls.def("setElement", &CovarianceMatrixKey<T, N>::setElement);
160  cls.def("getElement", &CovarianceMatrixKey<T, N>::getElement);
161 };
162 
163 } // namespace lsst::afw::table::<anonymous>
164 
165 PYBIND11_PLUGIN(aggregates) {
166  py::module::import("lsst.afw.geom.ellipses");
167  py::module::import("lsst.afw.table.base");
168  py::module mod("aggregates");
169 
170  if (_import_array() < 0) {
171  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
172  return nullptr;
173  };
174 
175  py::enum_<CoordinateType>(mod, "CoordinateType")
176  .value("PIXEL", CoordinateType::PIXEL)
177  .value("CELESTIAL", CoordinateType::CELESTIAL)
178  .export_values();
179 
180  declarePointKey<double>(mod, "2D");
181  declarePointKey<int>(mod, "2I");
182 
183  declareCoordKey(mod);
184  declareQuadrupoleKey(mod);
185  declareEllipseKey(mod);
186 
187  declareCovarianceMatrixKey<float, 2>(mod, "2f");
188  declareCovarianceMatrixKey<float, 3>(mod, "3f");
189  declareCovarianceMatrixKey<float, 4>(mod, "4f");
190  declareCovarianceMatrixKey<float, Eigen::Dynamic>(mod, "Xf");
191  declareCovarianceMatrixKey<double, 2>(mod, "2d");
192  declareCovarianceMatrixKey<double, 3>(mod, "3d");
193  declareCovarianceMatrixKey<double, 4>(mod, "4d");
194  declareCovarianceMatrixKey<double, Eigen::Dynamic>(mod, "Xd");
195 
196  return mod.ptr();
197 }
198 }
199 }
200 } // namespace lsst::afw::table
lsst::afw::coord::Coord Coord
Definition: misc.h:33
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
lsst::afw::coord::IcrsCoord IcrsCoord
Definition: misc.h:35
double const _a
Definition: RandomImage.cc:77