LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
_base.cc
Go to the documentation of this file.
1 /*
2  * This file is part of afw.
3  *
4  * Developed for the LSST Data Management System.
5  * This product includes software developed by the LSST Project
6  * (https://www.lsst.org).
7  * See the COPYRIGHT file at the top-level directory of this distribution
8  * for details of code ownership.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 /*
25 Unlike most pybind11 wrapper classes, which have one .cc file per header file,
26 this module wraps both BaseRecord.h and BaseTable.h (as well as CatalogT<BaseRecord> from Catalog.h).
27 
28 This allows us to define BaseCatalog.Table = clsBaseTable, which is needed to support `cast` in Python,
29 and makes wrapping Base catalogs more similar to all other types of catalog.
30 */
31 
32 #include "pybind11/pybind11.h"
33 
34 #include "ndarray/pybind11.h"
35 
36 #include "lsst/utils/python.h"
37 
38 #include "lsst/afw/table/Flag.h"
45 
46 namespace py = pybind11;
47 using namespace pybind11::literals;
48 
49 namespace lsst {
50 namespace afw {
51 namespace table {
52 
53 using utils::python::WrapperCollection;
54 
55 namespace {
56 
57 using PyBaseRecord = py::class_<BaseRecord, std::shared_ptr<BaseRecord>>;
58 using PyBaseTable = py::class_<BaseTable, std::shared_ptr<BaseTable>>;
59 
60 template <typename T>
61 void declareBaseRecordOverloads(PyBaseRecord &cls, std::string const &suffix) {
62  typedef typename Field<T>::Value (BaseRecord::*Getter)(Key<T> const &) const;
63  typedef void (BaseRecord::*Setter)(Key<T> const &, typename Field<T>::Value const &);
64  cls.def(("get" + suffix).c_str(), (Getter)&BaseRecord::get);
65  cls.def(("set" + suffix).c_str(), (Setter)&BaseRecord::set);
66 }
67 
68 template <typename T>
69 void declareBaseRecordArrayOverloads(PyBaseRecord &cls, std::string const &suffix) {
70  auto getter = [](BaseRecord &self, Key<Array<T>> const &key) -> ndarray::Array<T, 1, 1> {
71  return self[key];
72  };
73  auto setter = [](BaseRecord &self, Key<Array<T>> const &key, py::object const &value) {
74  if (key.getSize() == 0) {
75  // Variable-length array field: do a shallow copy, which requires a non-const
76  // contiguous array.
77  self.set(key, py::cast<ndarray::Array<T, 1, 1>>(value));
78  } else {
79  // Fixed-length array field: do a deep copy, which can work with a const
80  // noncontiguous array. But we need to check the size first, since the
81  // penalty for getting that wrong is assert->abort.
82  auto v = py::cast<ndarray::Array<T const, 1, 0>>(value);
83  ndarray::ArrayRef<T, 1, 1> ref = self[key];
84  if (v.size() != ref.size()) {
85  throw LSST_EXCEPT(
86  pex::exceptions::LengthError,
87  (boost::format("Array sizes do not agree: %s != %s") % v.size() % ref.size()).str());
88  }
89  ref = v;
90  }
91  return;
92  };
93  cls.def(("get" + suffix).c_str(), getter);
94  cls.def(("set" + suffix).c_str(), setter);
95 }
96 
97 PyBaseRecord declareBaseRecord(WrapperCollection &wrappers) {
98  return wrappers.wrapType(PyBaseRecord(wrappers.module, "BaseRecord"), [](auto &mod, auto &cls) {
99  utils::python::addSharedPtrEquality<BaseRecord>(cls);
100  cls.def("assign", (void (BaseRecord::*)(BaseRecord const &)) & BaseRecord::assign);
101  cls.def("assign",
102  (void (BaseRecord::*)(BaseRecord const &, SchemaMapper const &)) & BaseRecord::assign);
103  cls.def("getSchema", &BaseRecord::getSchema);
104  cls.def("getTable", &BaseRecord::getTable);
105  cls.def_property_readonly("schema", &BaseRecord::getSchema);
106  cls.def_property_readonly("table", &BaseRecord::getTable);
107 
108  declareBaseRecordOverloads<double>(cls, "D");
109  declareBaseRecordOverloads<float>(cls, "F");
110  declareBaseRecordOverloads<lsst::afw::table::Flag>(cls, "Flag");
111  declareBaseRecordOverloads<std::uint8_t>(cls, "B");
112  declareBaseRecordOverloads<std::uint16_t>(cls, "U");
113  declareBaseRecordOverloads<std::int32_t>(cls, "I");
114  declareBaseRecordOverloads<std::int64_t>(cls, "L");
115  declareBaseRecordOverloads<std::string>(cls, "String");
116  declareBaseRecordOverloads<lsst::geom::Angle>(cls, "Angle");
117  declareBaseRecordArrayOverloads<std::uint8_t>(cls, "ArrayB");
118  declareBaseRecordArrayOverloads<std::uint16_t>(cls, "ArrayU");
119  declareBaseRecordArrayOverloads<int>(cls, "ArrayI");
120  declareBaseRecordArrayOverloads<float>(cls, "ArrayF");
121  declareBaseRecordArrayOverloads<double>(cls, "ArrayD");
122  utils::python::addOutputOp(cls, "__str__"); // __repr__ is defined in baseContinued.py
123 
124  // These are master getters and setters that can take either strings, Keys, or
125  // FunctorKeys, and dispatch to key.get.
126  auto getter = [](py::object const &self, py::object key) -> py::object {
127  py::object schema = self.attr("schema");
128  if (py::isinstance<py::str>(key) || py::isinstance<py::bytes>(key)) {
129  key = schema.attr("find")(key).attr("key");
130  }
131  return key.attr("get")(self);
132  };
133  auto setter = [](py::object const &self, py::object key, py::object const &value) -> void {
134  py::object schema = self.attr("schema");
135  if (py::isinstance<py::str>(key) || py::isinstance<py::bytes>(key)) {
136  key = schema.attr("find")(key).attr("key");
137  }
138  key.attr("set")(self, value);
139  };
140 
141  // The distinction between get/set and operator[] is meaningful in C++, because "record[k] = v"
142  // operates by returning an object that can be assigned to.
143  // But there's no meaningful difference between get/set and __getitem__/__setitem__.
144  cls.def("get", getter);
145  cls.def("__getitem__", getter);
146  cls.def("set", setter);
147  cls.def("__setitem__", setter);
148  });
149 }
150 
151 PyBaseTable declareBaseTable(WrapperCollection &wrappers) {
152  return wrappers.wrapType(PyBaseTable(wrappers.module, "BaseTable"), [](auto &mod, auto &cls) {
153  utils::python::addSharedPtrEquality<BaseTable>(cls);
154  cls.def_static("make", &BaseTable::make);
155  cls.def("getMetadata", &BaseTable::getMetadata);
156  cls.def("setMetadata", &BaseTable::setMetadata, "metadata"_a);
157  cls.def("popMetadata", &BaseTable::popMetadata);
158  cls.def("makeRecord", &BaseTable::makeRecord);
159  cls.def("copyRecord",
160  (std::shared_ptr<BaseRecord>(BaseTable::*)(BaseRecord const &)) & BaseTable::copyRecord);
161  cls.def("copyRecord",
162  (std::shared_ptr<BaseRecord>(BaseTable::*)(BaseRecord const &, SchemaMapper const &)) &
163  BaseTable::copyRecord);
164  cls.def("getSchema", &BaseTable::getSchema);
165  cls.def_property_readonly("schema", &BaseTable::getSchema);
166  cls.def("getBufferSize", &BaseTable::getBufferSize);
167  cls.def("clone", &BaseTable::clone);
168  cls.def("preallocate", &BaseTable::preallocate);
169  });
170 }
171 
172 } // namespace
173 
174 void wrapBase(WrapperCollection &wrappers) {
175  wrappers.addSignatureDependency("lsst.daf.base");
176 
177  auto clsBaseTable = declareBaseTable(wrappers);
178  auto clsBaseRecord = declareBaseRecord(wrappers);
179  auto clsBaseCatalog = table::python::declareCatalog<BaseRecord>(wrappers, "Base");
180  auto clsBaseColumnView = table::python::declareColumnView<BaseRecord>(wrappers, "Base");
181 
182  clsBaseRecord.attr("Table") = clsBaseTable;
183  clsBaseRecord.attr("ColumnView") = clsBaseColumnView;
184  clsBaseRecord.attr("Catalog") = clsBaseCatalog;
185  clsBaseTable.attr("Record") = clsBaseRecord;
186  clsBaseTable.attr("ColumnView") = clsBaseColumnView;
187  clsBaseTable.attr("Catalog") = clsBaseCatalog;
188  clsBaseCatalog.attr("Record") = clsBaseRecord;
189  clsBaseCatalog.attr("Table") = clsBaseTable;
190  clsBaseCatalog.attr("ColumnView") = clsBaseColumnView;
191 }
192 
193 } // namespace table
194 } // namespace afw
195 } // namespace lsst
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Key< U > key
Definition: Schema.cc:281
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition: python.h:242
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
Definition: python.h:357
daf::base::PropertySet * set
Definition: fits.cc:912
void wrapBase(WrapperCollection &wrappers)
Definition: _base.cc:174
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
T ref(T... args)