LSSTApplications  18.1.0
LSSTDataManagementBasePackage
arrays.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 "ndarray/pybind11.h"
27 
28 #include "lsst/utils/python.h"
29 #include "lsst/afw/table/Key.h"
32 #include "lsst/afw/table/arrays.h"
33 
34 namespace py = pybind11;
35 using namespace pybind11::literals;
36 
37 namespace lsst {
38 namespace afw {
39 namespace table {
40 namespace {
41 
42 // We don't expose base classes (e.g. FunctorKey) to Python, since they're just used to
43 // define a CRTP interface in C++ and in Python that's just duck-typing.
44 
45 template <typename T>
46 using PyArrayKey = py::class_<ArrayKey<T>, std::shared_ptr<ArrayKey<T>>>;
47 
48 template <typename T>
49 void declareArrayKey(py::module &mod, std::string const &suffix) {
50  PyArrayKey<T> cls(mod, ("Array" + suffix + "Key").c_str());
51 
52  cls.def(py::init<>());
53  cls.def(py::init<Key<Array<T>> const &>());
54  cls.def(py::init<SubSchema const &>());
55  cls.def(py::init<std::vector<Key<T>> const &>());
56 
57  cls.def_static("addFields", (ArrayKey<T>(*)(Schema &, std::string const &, std::string const &,
58  std::string const &, std::vector<T> const &)) &
59  ArrayKey<T>::addFields,
60  "schema"_a, "name"_a, "doc"_a, "unit"_a, "docData"_a);
61  cls.def_static("addFields", (ArrayKey<T>(*)(Schema &, std::string const &, std::string const &,
62  std::string const &, int size)) &
63  ArrayKey<T>::addFields,
64  "schema"_a, "name"_a, "doc"_a, "unit"_a, "size"_a);
65  cls.def("get", &ArrayKey<T>::get);
66  cls.def("set", &ArrayKey<T>::set);
67  cls.def("isValid", &ArrayKey<T>::isValid);
68  cls.def("__eq__", &ArrayKey<T>::operator==, py::is_operator());
69  cls.def("__ne__", &ArrayKey<T>::operator!=, py::is_operator());
70  cls.def("__getitem__",
71  // Special implementation of __getitem__ to support ints and slices
72  [](ArrayKey<T> const &self, py::object const &index) -> py::object {
73  if (py::isinstance<py::slice>(index)) {
74  py::slice slice(index);
75  py::size_t start = 0, stop = 0, step = 0, length = 0;
76  bool valid = slice.compute(self.getSize(), &start, &stop, &step, &length);
77  if (!valid) throw py::error_already_set();
78  if (step != 1) {
79  throw py::index_error("Step for ArrayKey must be 1.");
80  }
81  return py::cast(self.slice(start, stop));
82  } else {
83  std::size_t n = utils::python::cppIndex(self.getSize(), py::cast<std::ptrdiff_t>(index));
84  return py::cast(self[n]);
85  }
86  });
87  cls.def("getSize", &ArrayKey<T>::getSize);
88  cls.def("slice", &ArrayKey<T>::slice);
89 };
90 
91 PYBIND11_MODULE(arrays, mod) {
92  py::module::import("lsst.afw.table.base");
93 
94  declareArrayKey<float>(mod, "F");
95  declareArrayKey<double>(mod, "D");
96 }
97 }
98 }
99 }
100 } // namespace lsst::afw::table::<anonymous>
std::size_t cppIndex(std::ptrdiff_t size, std::ptrdiff_t i)
Compute a C++ index from a Python index (negative values count from the end) and range-check.
Definition: python.h:124
def init()
Definition: tests.py:75
PYBIND11_MODULE(camera, mod)
Definition: camera.cc:34
daf::base::PropertySet * set
Definition: fits.cc:884
STL class.
A base class for image defects.
int const step
bool isValid
Definition: fits.cc:380
STL class.
ArrayKeyVector arrays