lsst.afw  13.0-91-g2a3e2ee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
baseColumnView.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2017 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 
29 #include "lsst/afw/table/Key.h"
31 
32 namespace ndarray {
33 namespace detail {
34 
35 // Tell ndarray to wrap ndarray::Array<Angle,T,N> the same way it would wrap
36 // ndarray::Array<double,T,N>. That throws away unit information (just returns
37 // radians, since that's how Angle is implemented), but in some places (e.g.
38 // Catalog.asAstropy) we add it back in via astropy Quantities.
39 template <>
40 struct NumpyTraits<lsst::afw::geom::Angle> : public NumpyTraits<double> {};
41 }
42 } // namespace ndarray::detail
43 
44 namespace py = pybind11;
45 using namespace py::literals;
46 
47 namespace lsst {
48 namespace afw {
49 namespace table {
50 namespace {
51 
52 using PyBaseColumnView = py::class_<BaseColumnView, std::shared_ptr<BaseColumnView>>;
53 
54 using PyBitsColumn = py::class_<BitsColumn, std::shared_ptr<BitsColumn>>;
55 
56 template <typename T, typename PyClass>
57 static void declareBaseColumnViewOverloads(PyClass &cls) {
58  cls.def("_basicget", [](BaseColumnView & self, Key<T> const &key) ->
59  typename ndarray::Array<T, 1> const { return self[key]; });
60 };
61 
62 template <typename U, typename PyClass>
63 static void declareBaseColumnViewArrayOverloads(PyClass &cls) {
64  cls.def("_basicget", [](BaseColumnView & self, Key<lsst::afw::table::Array<U>> const &key) ->
65  typename ndarray::Array<U, 2, 1> const { return self[key]; });
66 };
67 
68 template <typename PyClass>
69 static void declareBaseColumnViewFlagOverloads(PyClass &cls) {
70  cls.def("_basicget",
71  [](BaseColumnView &self, Key<Flag> const &key) -> ndarray::Array<bool const, 1, 1> const {
72  return ndarray::copy(self[key]);
73  });
74 };
75 
76 static void declareBaseColumnView(py::module &mod) {
77  // We can't call this "BaseColumnView" because that's the typedef for "ColumnViewT<BaseRecord>".
78  // This is just a mostly-invisible implementation base class, so we use the same naming convention
79  // we use for those.
80  PyBaseColumnView cls(mod, "_BaseColumnViewBase");
81  cls.def("getTable", &BaseColumnView::getTable);
82  cls.def_property_readonly("table", &BaseColumnView::getTable);
83  cls.def("getSchema", &BaseColumnView::getSchema);
84  cls.def_property_readonly("schema", &BaseColumnView::getSchema);
85  // _getBits supports a Python version of getBits that accepts None and field names as keys
86  cls.def("_getBits", &BaseColumnView::getBits);
87  cls.def("getAllBits", &BaseColumnView::getAllBits);
88  declareBaseColumnViewOverloads<std::uint16_t>(cls);
89  declareBaseColumnViewOverloads<std::int32_t>(cls);
90  declareBaseColumnViewOverloads<std::int64_t>(cls);
91  declareBaseColumnViewOverloads<float>(cls);
92  declareBaseColumnViewOverloads<double>(cls);
93  declareBaseColumnViewFlagOverloads(cls);
94  // std::string columns are not supported, because numpy string arrays
95  // do not have the same memory model as ours.
96  declareBaseColumnViewOverloads<lsst::afw::geom::Angle>(cls);
97  declareBaseColumnViewArrayOverloads<std::uint16_t>(cls);
98  declareBaseColumnViewArrayOverloads<int>(cls);
99  declareBaseColumnViewArrayOverloads<float>(cls);
100  declareBaseColumnViewArrayOverloads<double>(cls);
101 }
102 
103 static void declareBitsColumn(py::module &mod) {
104  PyBitsColumn cls(mod, "BitsColumn");
105  cls.def("getArray", &BitsColumn::getArray);
106  cls.def_property_readonly("array", &BitsColumn::getArray);
107  cls.def("getBit", (BitsColumn::IntT (BitsColumn::*)(Key<Flag> const &) const) & BitsColumn::getBit,
108  "key"_a);
109  cls.def("getBit", (BitsColumn::IntT (BitsColumn::*)(std::string const &) const) & BitsColumn::getBit,
110  "name"_a);
111  cls.def("getMask", (BitsColumn::IntT (BitsColumn::*)(Key<Flag> const &) const) & BitsColumn::getMask,
112  "key"_a);
113  cls.def("getMask", (BitsColumn::IntT (BitsColumn::*)(std::string const &) const) & BitsColumn::getMask,
114  "name"_a);
115 }
116 
117 PYBIND11_PLUGIN(baseColumnView) {
118  py::module mod("baseColumnView");
119 
120  py::module::import("lsst.afw.table.schema");
121 
122  if (_import_array() < 0) {
123  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
124  return nullptr;
125  }
126 
127  declareBaseColumnView(mod);
128  declareBitsColumn(mod);
129 
130  return mod.ptr();
131 }
132 }
133 }
134 }
135 } // namespace lsst::afw::table::<anonymous>
lsst::afw::geom::Angle Angle
Definition: misc.h:36
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
Tag types used to declare specialized field types.
Definition: misc.h:32
double const _a
Definition: RandomImage.cc:77