lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
base.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 /*
24 Unlike most pybind11 wrapper classes, which have one .cc file per header file,
25 this module wraps both BaseRecord.h and BaseTable.h (as well as CatalogT<BaseRecord> from Catalog.h).
26 
27 This allows us to define BaseCatalog.Table = clsBaseTable, which is needed to support `cast` in Python,
28 and makes wrapping Base catalogs more similar to all other types of catalog.
29 */
30 
31 #include "pybind11/pybind11.h"
32 
33 #include "numpy/arrayobject.h"
34 #include "ndarray/pybind11.h"
35 
36 #include "lsst/utils/python.h"
37 #include "lsst/afw/table/Flag.h"
44 
45 namespace py = pybind11;
46 using namespace pybind11::literals;
47 
48 namespace lsst {
49 namespace afw {
50 namespace table {
51 namespace {
52 
53 using PyBaseRecord = py::class_<BaseRecord, std::shared_ptr<BaseRecord>>;
54 using PyBaseTable = py::class_<BaseTable, std::shared_ptr<BaseTable>>;
55 
56 template <typename T>
57 void declareBaseRecordOverloads(PyBaseRecord &cls, std::string const &suffix) {
58  typedef typename Field<T>::Value (BaseRecord::*Getter)(Key<T> const &) const;
59  typedef void (BaseRecord::*Setter)(Key<T> const &, typename Field<T>::Value const &);
60  cls.def(("get" + suffix).c_str(), (Getter)&BaseRecord::get);
61  cls.def(("set" + suffix).c_str(), (Setter)&BaseRecord::set);
62 }
63 
64 template <typename T>
65 void declareBaseRecordArrayOverloads(PyBaseRecord &cls, std::string const &suffix) {
66  auto getter = [](BaseRecord &self, Key<Array<T>> const &key) -> ndarray::Array<T, 1, 1> {
67  return self[key];
68  };
69  auto setter = [](BaseRecord &self, Key<Array<T>> const &key, py::object const &value) {
70  if (key.getSize() == 0) {
71  // Variable-length array field: do a shallow copy, which requires a non-const
72  // contiguous array.
73  self.set(key, py::cast<ndarray::Array<T, 1, 1>>(value));
74  } else {
75  // Fixed-length array field: do a deep copy, which can work with a const
76  // noncontiguous array. But we need to check the size first, since the
77  // penalty for getting that wrong is assert->abort.
78  auto v = py::cast<ndarray::Array<T const, 1, 0>>(value);
79  ndarray::ArrayRef<T, 1, 1> ref = self[key];
80  if (v.size() != ref.size()) {
81  throw LSST_EXCEPT(
82  pex::exceptions::LengthError,
83  (boost::format("Array sizes do not agree: %s != %s") % v.size() % ref.size()).str());
84  }
85  ref = v;
86  }
87  return;
88  };
89  cls.def(("get" + suffix).c_str(), getter);
90  cls.def(("set" + suffix).c_str(), setter);
91 }
92 
93 PyBaseRecord declareBaseRecord(py::module &mod) {
94  PyBaseRecord cls(mod, "BaseRecord");
95  utils::python::addSharedPtrEquality<BaseRecord>(cls);
96  cls.def("assign", (void (BaseRecord::*)(BaseRecord const &)) & BaseRecord::assign);
97  cls.def("assign", (void (BaseRecord::*)(BaseRecord const &, SchemaMapper const &)) & BaseRecord::assign);
98  cls.def("getSchema", &BaseRecord::getSchema);
99  cls.def("getTable", &BaseRecord::getTable);
100  cls.def_property_readonly("schema", &BaseRecord::getSchema);
101  cls.def_property_readonly("table", &BaseRecord::getTable);
102 
103  declareBaseRecordOverloads<double>(cls, "D");
104  declareBaseRecordOverloads<float>(cls, "F");
105  declareBaseRecordOverloads<lsst::afw::table::Flag>(cls, "Flag");
106  declareBaseRecordOverloads<std::uint16_t>(cls, "U");
107  declareBaseRecordOverloads<std::int32_t>(cls, "I");
108  declareBaseRecordOverloads<std::int64_t>(cls, "L");
109  declareBaseRecordOverloads<std::string>(cls, "String");
110  declareBaseRecordOverloads<lsst::afw::geom::Angle>(cls, "Angle");
111  declareBaseRecordArrayOverloads<std::uint16_t>(cls, "ArrayU");
112  declareBaseRecordArrayOverloads<int>(cls, "ArrayI");
113  declareBaseRecordArrayOverloads<float>(cls, "ArrayF");
114  declareBaseRecordArrayOverloads<double>(cls, "ArrayD");
115 
116  // These are master getters and setters that can take either strings, Keys, or
117  // FunctorKeys, and dispatch to key.get.
118  auto getter = [](py::object const &self, py::object key) -> py::object {
119  py::object schema = self.attr("schema");
120  if (py::isinstance<py::str>(key) || py::isinstance<py::bytes>(key)) {
121  key = schema.attr("find")(key).attr("key");
122  }
123  return key.attr("get")(self);
124  };
125  auto setter = [](py::object const &self, py::object key, py::object const &value) -> void {
126  py::object schema = self.attr("schema");
127  if (py::isinstance<py::str>(key) || py::isinstance<py::bytes>(key)) {
128  key = schema.attr("find")(key).attr("key");
129  }
130  key.attr("set")(self, value);
131  };
132 
133  // The distinction between get/set and operator[] is meaningful in C++, because "record[k] = v"
134  // operates by returning an object that can be assigned to.
135  // But there's no meaningful difference between get/set and __getitem__/__setitem__.
136  cls.def("get", getter);
137  cls.def("__getitem__", getter);
138  cls.def("set", setter);
139  cls.def("__setitem__", setter);
140  return cls;
141 }
142 
143 PyBaseTable declareBaseTable(py::module &mod) {
144  PyBaseTable cls(mod, "BaseTable");
145  utils::python::addSharedPtrEquality<BaseTable>(cls);
146  cls.def_static("make", &BaseTable::make);
147  cls.def("getMetadata", &BaseTable::getMetadata);
148  cls.def("setMetadata", &BaseTable::setMetadata, "metadata"_a);
149  cls.def("popMetadata", &BaseTable::popMetadata);
150  cls.def("makeRecord", &BaseTable::makeRecord);
151  cls.def("copyRecord",
152  (std::shared_ptr<BaseRecord> (BaseTable::*)(BaseRecord const &)) & BaseTable::copyRecord);
153  cls.def("copyRecord",
154  (std::shared_ptr<BaseRecord> (BaseTable::*)(BaseRecord const &, SchemaMapper const &)) &
155  BaseTable::copyRecord);
156  cls.def("getSchema", &BaseTable::getSchema);
157  cls.def_property_readonly("schema", &BaseTable::getSchema);
158  cls.def("getBufferSize", &BaseTable::getBufferSize);
159  cls.def("clone", &BaseTable::clone);
160  cls.def("preallocate", &BaseTable::preallocate);
161 
162  return cls;
163 }
164 
165 PYBIND11_PLUGIN(base) {
166  py::module mod("base");
167 
168  py::module::import("lsst.afw.table.schema");
169  py::module::import("lsst.afw.table.baseColumnView");
170 
171  if (_import_array() < 0) {
172  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
173  return nullptr;
174  };
175 
176  auto clsBaseTable = declareBaseTable(mod);
177  auto clsBaseRecord = declareBaseRecord(mod);
178  auto clsBaseCatalog = table::python::declareCatalog<BaseRecord>(mod, "Base");
179  auto clsBaseColumnView = table::python::declareColumnView<BaseRecord>(mod, "Base");
180 
181  clsBaseRecord.attr("Table") = clsBaseTable;
182  clsBaseRecord.attr("ColumnView") = clsBaseColumnView;
183  clsBaseRecord.attr("Catalog") = clsBaseCatalog;
184  clsBaseTable.attr("Record") = clsBaseRecord;
185  clsBaseTable.attr("ColumnView") = clsBaseColumnView;
186  clsBaseTable.attr("Catalog") = clsBaseCatalog;
187  clsBaseCatalog.attr("Record") = clsBaseRecord;
188  clsBaseCatalog.attr("Table") = clsBaseTable;
189  clsBaseCatalog.attr("ColumnView") = clsBaseColumnView;
190 
191  return mod.ptr();
192 }
193 }
194 }
195 }
196 } // namespace lsst::afw::table::<anonymous>
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
double const _a
Definition: RandomImage.cc:77
afw::table::Schema schema
Definition: GaussianPsf.cc:43