22 #ifndef AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
23 #define AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
25 #include "pybind11/pybind11.h"
27 #include "lsst/utils/python.h"
36 template <
typename Record>
37 using PyCatalog = pybind11::class_<CatalogT<Record>, std::shared_ptr<CatalogT<Record>>>;
48 template <
typename T,
typename Record>
50 namespace py = pybind11;
51 using namespace pybind11::literals;
56 cls.def(
"isSorted", (
bool (Catalog::*)(
Key<T> const &)
const) & Catalog::isSorted);
57 cls.def(
"sort", (
void (Catalog::*)(
Key<T> const &)) & Catalog::sort);
58 cls.def(
"find", [](Catalog &
self, Value
const &value,
Key<T> const &key) -> std::shared_ptr<Record> {
59 auto iter =
self.find(value, key);
60 if (iter ==
self.
end()) {
65 cls.def(
"upper_bound", [](Catalog &
self, Value
const &value,
Key<T> const &key) -> std::ptrdiff_t {
66 return self.upper_bound(value, key) -
self.begin();
68 cls.def(
"lower_bound", [](Catalog &
self, Value
const &value,
Key<T> const &key) -> std::ptrdiff_t {
69 return self.lower_bound(value, key) -
self.begin();
71 cls.def(
"equal_range", [](Catalog &
self, Value
const &value,
Key<T> const &key) {
72 auto p =
self.equal_range(value, key);
73 return py::slice(p.first -
self.begin(), p.second -
self.begin(), 1);
75 cls.def(
"between", [](Catalog &
self, Value
const &lower, Value
const &upper,
Key<T> const &key) {
76 std::ptrdiff_t a =
self.lower_bound(lower, key) -
self.begin();
77 std::ptrdiff_t b =
self.upper_bound(upper, key) -
self.begin();
78 return py::slice(a, b, 1);
94 template <
typename Record>
96 namespace py = pybind11;
97 using namespace pybind11::literals;
100 using Table =
typename Record::Table;
102 std::string fullName;
104 fullName =
"_" + name +
"CatalogBase";
106 fullName = name +
"Catalog";
113 cls.def(py::init<Schema const &>(),
"schema"_a);
114 cls.def(py::init<std::shared_ptr<Table>
const &>(),
"table"_a);
115 cls.def(py::init<Catalog const &>(),
"other"_a);
118 cls.def_static(
"readFits", (Catalog(*)(std::string
const &,
int,
int)) & Catalog::readFits,
"filename"_a,
119 "hdu"_a = INT_MIN,
"flags"_a = 0);
121 "manager"_a,
"hdu"_a = INT_MIN,
"flags"_a = 0);
125 cls.def(
"getTable", &Catalog::getTable);
126 cls.def_property_readonly(
"table", &Catalog::getTable);
127 cls.def(
"getSchema", &Catalog::getSchema);
128 cls.def_property_readonly(
"schema", &Catalog::getSchema);
129 cls.def(
"capacity", &Catalog::capacity);
130 cls.def(
"__len__", &Catalog::size);
134 cls.def(
"_getColumnView", &Catalog::getColumnView);
135 cls.def(
"_addNew", &Catalog::addNew);
136 cls.def(
"_extend", [](Catalog &
self, Catalog
const &other,
bool deep) {
137 self.insert(
self.
end(), other.begin(), other.end(), deep);
139 cls.def(
"_extend", [](Catalog &
self, Catalog
const &other,
SchemaMapper const &mapper) {
140 self.insert(mapper,
self.
end(), other.begin(), other.end());
142 cls.def(
"_append", [](Catalog &
self, std::shared_ptr<Record>
const &rec) {
self.push_back(rec); });
143 cls.def(
"_delitem_", [](Catalog &
self, std::ptrdiff_t i) {
144 self.erase(
self.begin() + utils::python::cppIndex(
self.size(), i));
146 cls.def(
"_delslice_", [](Catalog &
self, py::slice
const &s) {
147 Py_ssize_t start = 0, stop = 0,
step = 0, length = 0;
148 if (PySlice_GetIndicesEx(
150 #
if PY_MAJOR_VERSION < 3 || (PY_MINOR_VERSION == 3 && PY_MINOR_VERSION < 2)
154 self.size(), &start, &stop, &
step, &length) != 0) {
155 throw py::error_already_set();
158 throw py::index_error(
"Slice step must not exactly 1");
160 self.erase(
self.begin() + start,
self.begin() + stop);
162 cls.def(
"_clear", &Catalog::clear);
164 cls.def(
"set", &Catalog::set);
166 [](Catalog &
self,
int i) {
return self.get(utils::python::cppIndex(
self.size(), i)); });
167 cls.def(
"isContiguous", &Catalog::isContiguous);
169 (
void (Catalog::*)(std::string
const &, std::string
const &,
int)
const) & Catalog::writeFits,
170 "filename"_a,
"mode"_a =
"w",
"flags"_a = 0);
172 (
void (Catalog::*)(
fits::MemFileManager &, std::string
const &,
int)
const) & Catalog::writeFits,
173 "manager"_a,
"mode"_a =
"w",
"flags"_a = 0);
174 cls.def(
"reserve", &Catalog::reserve);
175 cls.def(
"subset", (Catalog (Catalog::*)(ndarray::Array<bool const, 1>
const &)
const) & Catalog::subset);
177 (Catalog (Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t)
const) & Catalog::subset);
179 declareCatalogOverloads<std::int32_t>(
cls);
180 declareCatalogOverloads<std::int64_t>(
cls);
181 declareCatalogOverloads<float>(
cls);
182 declareCatalogOverloads<double>(
cls);
183 declareCatalogOverloads<lsst::afw::geom::Angle>(
cls);
192 #endif // !LSST_AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
A custom container class for records, based on std::vector.
A mapping between the keys of two Schemas, used to copy data between them.
Lifetime-management for memory that goes into FITS memory files.
pybind11::class_< CatalogT< Record >, std::shared_ptr< CatalogT< Record >>> PyCatalog
table::Key< std::string > name
void declareCatalogOverloads(PyCatalog< Record > &cls)
Declare field-type-specific overloaded catalog member functions for one field type.
A class used as a handle to a particular field in a table.
T Value
the type returned by BaseRecord::get
PyCatalog< Record > declareCatalog(pybind11::module &mod, std::string const &name, bool isBase=false)
Wrap an instantiation of lsst::afw::table::CatalogT<Record>.