23#ifndef AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
24#define AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
26#include "pybind11/pybind11.h"
37template <
typename Record>
41template <
typename T,
typename Record>
46 ndarray::Array<typename Field<T>::Value, 1, 1> out = ndarray::allocate(catalog.size());
47 auto outIter = out.begin();
48 auto inIter = catalog.begin();
49 for (; inIter != catalog.end(); ++inIter, ++outIter) {
50 *outIter = inIter->get(key);
57template <
typename Record>
62 ndarray::Array<double, 1, 1> out = ndarray::allocate(catalog.size());
63 auto outIter = out.begin();
64 auto inIter = catalog.begin();
65 for (; inIter != catalog.end(); ++inIter, ++outIter) {
66 *outIter = inIter->get(key).asRadians();
71template <
typename Record>
75 ndarray::Array<bool const, 1>
const & array
77 if (array.size() != catalog.size()) {
80 (boost::format(
"Catalog has %d rows, while array has %d elements.")
81 % catalog.size() % array.size()).str()
84 auto catIter = catalog.begin();
85 auto arrayIter = array.begin();
86 for (; catIter != catalog.end(); ++catIter, ++arrayIter) {
87 catIter->set(key, *arrayIter);
91template <
typename Record>
97 for (
auto catIter = catalog.begin(); catIter != catalog.end(); ++catIter) {
98 catIter->set(key, value);
104template <
typename Record>
116 if (_index < _catalog->size()) {
117 return _catalog->
get(_index);
120 "Catalog shrunk during iteration, invalidating this iterator."
130 return _catalog == other._catalog && _index == other._index;
134 return !(*
this == other);
150template <
typename T,
typename Record>
153 using namespace pybind11::literals;
158 cls.def(
"isSorted", (
bool (
Catalog::*)(
Key<T> const &)
const) & Catalog::isSorted);
159 cls.def(
"sort", (
void (
Catalog::*)(
Key<T> const &)) & Catalog::sort);
161 auto iter = self.find(value, key);
162 if (iter == self.end()) {
168 return self.upper_bound(value, key) - self.begin();
171 return self.lower_bound(value, key) - self.begin();
173 cls.def(
"equal_range", [](
Catalog &self, Value
const &value,
Key<T> const &key) {
174 auto p = self.equal_range(value, key);
175 return py::slice(p.first - self.begin(), p.second - self.begin(), 1);
177 cls.def(
"between", [](
Catalog &self, Value
const &lower, Value
const &upper,
Key<T> const &key) {
180 return py::slice(
a,
b, 1);
187 [](
Catalog &self,
Key<Flag> const & key, ndarray::Array<bool const, 1>
const & array) {
212template <
typename Record>
214 bool isBase =
false) {
216 using namespace pybind11::literals;
219 using Table =
typename Record::Table;
223 fullName =
"_" +
name +
"CatalogBase";
225 fullName =
name +
"Catalog";
230 return wrappers.wrapType(
232 [](
auto &mod,
auto &cls) {
234 cls.def(py::init<Schema const &>(),
"schema"_a);
235 cls.def(py::init<std::shared_ptr<Table> const &>(),
"table"_a);
236 cls.def(py::init<Catalog const &>(),
"other"_a);
239 cls.def_static(
"readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits,
240 "filename"_a,
"hdu"_a = fits::DEFAULT_HDU,
"flags"_a = 0);
241 cls.def_static(
"readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
242 "manager"_a,
"hdu"_a = fits::DEFAULT_HDU,
"flags"_a = 0);
246 cls.def(
"getTable", &Catalog::getTable);
247 cls.def_property_readonly(
"table", &Catalog::getTable);
248 cls.def(
"getSchema", &Catalog::getSchema);
249 cls.def_property_readonly(
"schema", &Catalog::getSchema);
250 cls.def(
"capacity", &Catalog::capacity);
251 cls.def(
"__len__", &Catalog::size);
252 cls.def(
"resize", &Catalog::resize);
256 cls.def(
"_getColumnView", &Catalog::getColumnView);
257 cls.def(
"_addNew", &Catalog::addNew);
258 cls.def(
"_extend", [](Catalog &self, Catalog const &other, bool deep) {
259 self.insert(self.end(), other.begin(), other.end(), deep);
262 self.
insert(
mapper, self.end(), other.begin(), other.end());
267 self.erase(self.begin() + utils::python::cppIndex(self.size(), i));
269 cls.def(
"_delslice_", [](
Catalog &self, py::slice
const &s) {
270 Py_ssize_t start = 0, stop = 0,
step = 0, length = 0;
271 if (PySlice_GetIndicesEx(s.ptr(), self.size(), &start, &stop, &
step, &length) != 0) {
272 throw py::error_already_set();
275 throw py::index_error(
"Slice step must not exactly 1");
277 self.erase(self.begin() + start, self.begin() + stop);
281 cls.def(
"set", &Catalog::set);
282 cls.def(
"_getitem_", [](
Catalog &self,
int i) {
283 return self.get(utils::python::cppIndex(self.size(), i));
285 cls.def(
"__iter__", [](
Catalog & self) {
312 return py::make_iterator(
316 }, py::keep_alive<0, 1>());
317 cls.def(
"isContiguous", &Catalog::isContiguous);
321 "filename"_a,
"mode"_a =
"w",
"flags"_a = 0);
325 "manager"_a,
"mode"_a =
"w",
"flags"_a = 0);
326 cls.def(
"reserve", &Catalog::reserve);
328 (
Catalog(
Catalog::*)(ndarray::Array<bool const, 1>
const &)
const) & Catalog::subset);
333 declareCatalogOverloads<std::int32_t>(cls);
334 declareCatalogOverloads<std::int64_t>(cls);
335 declareCatalogOverloads<float>(cls);
336 declareCatalogOverloads<double>(cls);
337 declareCatalogOverloads<lsst::geom::Angle>(cls);
340 [](
Catalog const &self,
Key<Flag> const &key) -> ndarray::Array<bool const, 1, 0> {
table::Key< std::string > name
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Lifetime-management for memory that goes into FITS memory files.
def insert(self, key, value)
A custom container class for records, based on std::vector.
std::shared_ptr< RecordT > const get(size_type i) const
Return a pointer to the record at index i.
A class used as a handle to a particular field in a table.
A mapping between the keys of two Schemas, used to copy data between them.
PyCatalogIndexIterator(CatalogT< Record > const *catalog, std::size_t index)
std::shared_ptr< Record > operator*() const
bool operator!=(PyCatalogIndexIterator const &other) const
PyCatalogIndexIterator & operator++()
bool operator==(PyCatalogIndexIterator const &other) const
Reports attempts to exceed implementation-defined length limits for some classes.
void _setFlagColumnToArray(CatalogT< Record > &catalog, Key< Flag > const &key, ndarray::Array< bool const, 1 > const &array)
ndarray::Array< typename Field< T >::Value const, 1, 1 > _getArrayFromCatalog(CatalogT< Record > const &catalog, Key< T > const &key)
Extract a column from a potentially non-contiguous Catalog.
void _setFlagColumnToScalar(CatalogT< Record > &catalog, Key< Flag > const &key, bool value)
pybind11::class_< CatalogT< Record >, std::shared_ptr< CatalogT< Record > > > PyCatalog
PyCatalog< Record > declareCatalog(utils::python::WrapperCollection &wrappers, std::string const &name, bool isBase=false)
Wrap an instantiation of lsst::afw::table::CatalogT<Record>.
void declareCatalogOverloads(PyCatalog< Record > &cls)
Declare field-type-specific overloaded catalog member functions for one field type.
T Value
the type returned by BaseRecord::get