LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
_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/*
25Unlike most pybind11 wrapper classes, which have one .cc file per header file,
26this module wraps both BaseRecord.h and BaseTable.h (as well as CatalogT<BaseRecord> from Catalog.h).
27
28This allows us to define BaseCatalog.Table = clsBaseTable, which is needed to support `cast` in Python,
29and 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
46namespace py = pybind11;
47using namespace pybind11::literals;
48
49namespace lsst {
50namespace afw {
51namespace table {
52
53using utils::python::WrapperCollection;
54
55namespace {
56
57using PyBaseRecord = py::class_<BaseRecord, std::shared_ptr<BaseRecord>>;
58using PyBaseTable = py::class_<BaseTable, std::shared_ptr<BaseTable>>;
59
60template <typename T>
61void declareBaseRecordOverloads(PyBaseRecord &cls, std::string const &suffix) {
62 using Getter = typename Field<T>::Value (BaseRecord::*)(const Key<T> &) const;
63 using Setter = void (BaseRecord::*)(const Key<T> &, const typename Field<T>::Value &);
64 cls.def(("get" + suffix).c_str(), (Getter)&BaseRecord::get);
65 cls.def(("set" + suffix).c_str(), (Setter)&BaseRecord::set);
66}
67
68template <typename T>
69void 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
97PyBaseRecord 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
151PyBaseTable 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_property("metadata", &BaseTable::getMetadata, &BaseTable::setMetadata);
159 cls.def("makeRecord", &BaseTable::makeRecord);
160 cls.def("copyRecord",
161 (std::shared_ptr<BaseRecord>(BaseTable::*)(BaseRecord const &)) & BaseTable::copyRecord);
162 cls.def("copyRecord",
163 (std::shared_ptr<BaseRecord>(BaseTable::*)(BaseRecord const &, SchemaMapper const &)) &
164 BaseTable::copyRecord);
165 cls.def("getSchema", &BaseTable::getSchema);
166 cls.def_property_readonly("schema", &BaseTable::getSchema);
167 cls.def("getBufferSize", &BaseTable::getBufferSize);
168 cls.def("clone", &BaseTable::clone);
169 cls.def("preallocate", &BaseTable::preallocate);
170 });
171}
172
173} // namespace
174
175void wrapBase(WrapperCollection &wrappers) {
176 wrappers.addSignatureDependency("lsst.daf.base");
177
178 auto clsBaseTable = declareBaseTable(wrappers);
179 auto clsBaseRecord = declareBaseRecord(wrappers);
180 auto clsBaseCatalog = table::python::declareCatalog<BaseRecord>(wrappers, "Base");
181 auto clsBaseColumnView = table::python::declareColumnView<BaseRecord>(wrappers, "Base");
182
183 clsBaseRecord.attr("Table") = clsBaseTable;
184 clsBaseRecord.attr("ColumnView") = clsBaseColumnView;
185 clsBaseRecord.attr("Catalog") = clsBaseCatalog;
186 clsBaseTable.attr("Record") = clsBaseRecord;
187 clsBaseTable.attr("ColumnView") = clsBaseColumnView;
188 clsBaseTable.attr("Catalog") = clsBaseCatalog;
189 clsBaseCatalog.attr("Record") = clsBaseRecord;
190 clsBaseCatalog.attr("Table") = clsBaseTable;
191 clsBaseCatalog.attr("ColumnView") = clsBaseColumnView;
192}
193
194} // namespace table
195} // namespace afw
196} // namespace lsst
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
Tag types used to declare specialized field types.
Definition misc.h:31
Field< T >::Value get(Key< T > const &key) const
Return the value of a field for the given key.
Definition BaseRecord.h:151
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Definition BaseRecord.h:164
void wrapBase(WrapperCollection &wrappers)
Definition _base.cc:175
T ref(T... args)
T Value
the type returned by BaseRecord::get
Definition FieldBase.h:42