lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "numpy/arrayobject.h"
27 #include "ndarray/pybind11.h"
28 
29 #include "lsst/utils/python.h"
30 #include "lsst/afw/table/Key.h"
33 #include "lsst/afw/table/arrays.h"
34 
35 namespace py = pybind11;
36 using namespace pybind11::literals;
37 
38 namespace lsst {
39 namespace afw {
40 namespace table {
41 namespace {
42 
43 // We don't expose base classes (e.g. FunctorKey) to Python, since they're just used to
44 // define a CRTP interface in C++ and in Python that's just duck-typing.
45 
46 template <typename T>
47 using PyArrayKey = py::class_<ArrayKey<T>, std::shared_ptr<ArrayKey<T>>>;
48 
49 template <typename T>
50 void declareArrayKey(py::module &mod, std::string const &suffix) {
51  PyArrayKey<T> cls(mod, ("Array" + suffix + "Key").c_str());
52 
53  cls.def(py::init<>());
54  cls.def(py::init<Key<Array<T>> const &>());
55  cls.def(py::init<SubSchema const &>());
56  cls.def(py::init<std::vector<Key<T>> const &>());
57 
58  cls.def_static("addFields", (ArrayKey<T>(*)(Schema &, std::string const &, std::string const &,
59  std::string const &, std::vector<T> const &)) &
60  ArrayKey<T>::addFields,
61  "schema"_a, "name"_a, "doc"_a, "unit"_a, "docData"_a);
62  cls.def_static("addFields", (ArrayKey<T>(*)(Schema &, std::string const &, std::string const &,
63  std::string const &, int size)) &
64  ArrayKey<T>::addFields,
65  "schema"_a, "name"_a, "doc"_a, "unit"_a, "size"_a);
66  cls.def("get", &ArrayKey<T>::get);
67  cls.def("set", &ArrayKey<T>::set);
68  cls.def("isValid", &ArrayKey<T>::isValid);
69  cls.def("__eq__", &ArrayKey<T>::operator==, py::is_operator());
70  cls.def("__ne__", &ArrayKey<T>::operator!=, py::is_operator());
71  cls.def("__getitem__",
72  // Special implementation of __getitem__ to support ints and slices
73  [](ArrayKey<T> const &self, py::object const &index) -> py::object {
74  if (py::isinstance<py::slice>(index)) {
75  py::slice slice(index);
76  py::size_t start = 0, stop = 0, step = 0, length = 0;
77  bool valid = slice.compute(self.getSize(), &start, &stop, &step, &length);
78  if (!valid) throw py::error_already_set();
79  if (step != 1) {
80  throw py::index_error("Step for ArrayKey must be 1.");
81  }
82  return py::cast(self.slice(start, stop));
83  } else {
84  std::size_t n = utils::python::cppIndex(self.getSize(), py::cast<std::ptrdiff_t>(index));
85  return py::cast(self[n]);
86  }
87  });
88  cls.def("getSize", &ArrayKey<T>::getSize);
89  cls.def("slice", &ArrayKey<T>::slice);
90 };
91 
92 PYBIND11_PLUGIN(arrays) {
93  py::module mod("arrays");
94  py::module::import("lsst.afw.table.base");
95 
96  if (_import_array() < 0) {
97  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
98  return nullptr;
99  };
100 
101  declareArrayKey<float>(mod, "F");
102  declareArrayKey<double>(mod, "D");
103 
104  return mod.ptr();
105 }
106 }
107 }
108 }
109 } // namespace lsst::afw::table::<anonymous>
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
double const _a
Definition: RandomImage.cc:77