lsst.afw  14.0-25-g0e8fc950e+6
catalog.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 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 #ifndef AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
23 #define AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
24 
25 #include "pybind11/pybind11.h"
26 
27 #include "lsst/utils/python.h"
29 #include "lsst/afw/table/Catalog.h"
30 
31 namespace lsst {
32 namespace afw {
33 namespace table {
34 namespace python {
35 
36 template <typename Record>
37 using PyCatalog = pybind11::class_<CatalogT<Record>, std::shared_ptr<CatalogT<Record>>>;
38 
39 
41 template <typename T, typename Record>
42 ndarray::Array<typename Field<T>::Value const, 1, 0> _getArrayFromCatalog(
43  CatalogT<Record> const& catalog,
44  Key<T> const& key
45 ) {
46  ndarray::Array<typename Field<T>::Value, 1, 0> 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);
51  }
52  return out;
53 }
54 
55 
65 template <typename T, typename Record>
67  namespace py = pybind11;
68  using namespace pybind11::literals;
69 
70  typedef CatalogT<Record> Catalog;
71  typedef typename Field<T>::Value Value;
72 
73  cls.def("isSorted", (bool (Catalog::*)(Key<T> const &) const) & Catalog::isSorted);
74  cls.def("sort", (void (Catalog::*)(Key<T> const &)) & Catalog::sort);
75  cls.def("find", [](Catalog &self, Value const &value, Key<T> const &key) -> std::shared_ptr<Record> {
76  auto iter = self.find(value, key);
77  if (iter == self.end()) {
78  return nullptr;
79  };
80  return iter;
81  });
82  cls.def("upper_bound", [](Catalog &self, Value const &value, Key<T> const &key) -> std::ptrdiff_t {
83  return self.upper_bound(value, key) - self.begin();
84  });
85  cls.def("lower_bound", [](Catalog &self, Value const &value, Key<T> const &key) -> std::ptrdiff_t {
86  return self.lower_bound(value, key) - self.begin();
87  });
88  cls.def("equal_range", [](Catalog &self, Value const &value, Key<T> const &key) {
89  auto p = self.equal_range(value, key);
90  return py::slice(p.first - self.begin(), p.second - self.begin(), 1);
91  });
92  cls.def("between", [](Catalog &self, Value const &lower, Value const &upper, Key<T> const &key) {
93  std::ptrdiff_t a = self.lower_bound(lower, key) - self.begin();
94  std::ptrdiff_t b = self.upper_bound(upper, key) - self.begin();
95  return py::slice(a, b, 1);
96  });
97 
98  // Not extracting afw::geom::Angle columns because the units would be ambiguous
100  cls.def("_getitem_", [](Catalog const& self, Key<T> const& key) -> ndarray::Array<T const, 1, 0> {
101  return _getArrayFromCatalog(self, key);
102  });
103  }
104 }
105 
118 template <typename Record>
119 PyCatalog<Record> declareCatalog(pybind11::module &mod, std::string const &name, bool isBase = false) {
120  namespace py = pybind11;
121  using namespace pybind11::literals;
122 
123  using Catalog = CatalogT<Record>;
124  using Table = typename Record::Table;
125 
126  std::string fullName;
127  if (isBase) {
128  fullName = "_" + name + "CatalogBase";
129  } else {
130  fullName = name + "Catalog";
131  }
132 
133  // We need py::dynamic_attr() below to support our Python-side caching of the associated ColumnView.
134  PyCatalog<Record> cls(mod, fullName.c_str(), py::dynamic_attr());
135 
136  /* Constructors */
137  cls.def(py::init<Schema const &>(), "schema"_a);
138  cls.def(py::init<std::shared_ptr<Table> const &>(), "table"_a);
139  cls.def(py::init<Catalog const &>(), "other"_a);
140 
141  /* Static Methods */
142  cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits, "filename"_a,
143  "hdu"_a = INT_MIN, "flags"_a = 0);
144  cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
145  "manager"_a, "hdu"_a = INT_MIN, "flags"_a = 0);
146  // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
147 
148  /* Methods */
149  cls.def("getTable", &Catalog::getTable);
150  cls.def_property_readonly("table", &Catalog::getTable);
151  cls.def("getSchema", &Catalog::getSchema);
152  cls.def_property_readonly("schema", &Catalog::getSchema);
153  cls.def("capacity", &Catalog::capacity);
154  cls.def("__len__", &Catalog::size);
155 
156  // Use private names for the following so the public Python method
157  // can manage the _column cache
158  cls.def("_getColumnView", &Catalog::getColumnView);
159  cls.def("_addNew", &Catalog::addNew);
160  cls.def("_extend", [](Catalog &self, Catalog const &other, bool deep) {
161  self.insert(self.end(), other.begin(), other.end(), deep);
162  });
163  cls.def("_extend", [](Catalog &self, Catalog const &other, SchemaMapper const &mapper) {
164  self.insert(mapper, self.end(), other.begin(), other.end());
165  });
166  cls.def("_append", [](Catalog &self, std::shared_ptr<Record> const &rec) { self.push_back(rec); });
167  cls.def("_delitem_", [](Catalog &self, std::ptrdiff_t i) {
168  self.erase(self.begin() + utils::python::cppIndex(self.size(), i));
169  });
170  cls.def("_delslice_", [](Catalog &self, py::slice const &s) {
171  Py_ssize_t start = 0, stop = 0, step = 0, length = 0;
172  if (PySlice_GetIndicesEx(
173 // The interface to this function changed in Python 3.2
174 #if PY_MAJOR_VERSION < 3 || (PY_MINOR_VERSION == 3 && PY_MINOR_VERSION < 2)
175  (PySliceObject *)
176 #endif
177  s.ptr(),
178  self.size(), &start, &stop, &step, &length) != 0) {
179  throw py::error_already_set();
180  }
181  if (step != 1) {
182  throw py::index_error("Slice step must not exactly 1");
183  }
184  self.erase(self.begin() + start, self.begin() + stop);
185  });
186  cls.def("_clear", &Catalog::clear);
187 
188  cls.def("set", &Catalog::set);
189  cls.def("_getitem_",
190  [](Catalog &self, int i) { return self.get(utils::python::cppIndex(self.size(), i)); });
191  cls.def("isContiguous", &Catalog::isContiguous);
192  cls.def("writeFits",
193  (void (Catalog::*)(std::string const &, std::string const &, int) const) & Catalog::writeFits,
194  "filename"_a, "mode"_a = "w", "flags"_a = 0);
195  cls.def("writeFits",
196  (void (Catalog::*)(fits::MemFileManager &, std::string const &, int) const) & Catalog::writeFits,
197  "manager"_a, "mode"_a = "w", "flags"_a = 0);
198  cls.def("reserve", &Catalog::reserve);
199  cls.def("subset", (Catalog (Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset);
200  cls.def("subset",
201  (Catalog (Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t) const) & Catalog::subset);
202 
203  declareCatalogOverloads<std::int32_t>(cls);
204  declareCatalogOverloads<std::int64_t>(cls);
205  declareCatalogOverloads<float>(cls);
206  declareCatalogOverloads<double>(cls);
207  declareCatalogOverloads<lsst::afw::geom::Angle>(cls);
208 
209  cls.def("_getitem_", [](Catalog const& self, Key<Flag> const& key) -> ndarray::Array<bool const, 1, 0> {
210  return _getArrayFromCatalog(self, key);
211  });
212 
213  return cls;
214 };
215 }
216 }
217 }
218 } // lsst::afw::table::python
219 
220 #endif // !LSST_AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
table::Key< std::string > name
Definition: ApCorrMap.cc:71
A custom container class for records, based on std::vector.
Definition: Catalog.h:96
std::size_t cppIndex(std::ptrdiff_t size, std::ptrdiff_t i)
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
pybind11::class_< CatalogT< Record >, std::shared_ptr< CatalogT< Record > >> PyCatalog
Definition: catalog.h:37
ndarray::Array< typename Field< T >::Value const, 1, 0 > _getArrayFromCatalog(CatalogT< Record > const &catalog, Key< T > const &key)
Extract a column from a potentially non-contiguous Catalog.
Definition: catalog.h:42
int const step
Definition: BoundedField.cc:97
STL class.
A base class for image defects.
int end
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:101
iterator end()
Iterator access.
Definition: Catalog.h:388
void declareCatalogOverloads(PyCatalog< Record > &cls)
Declare field-type-specific overloaded catalog member functions for one field type.
Definition: catalog.h:66
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
T Value
the type returned by BaseRecord::get
Definition: FieldBase.h:45
PyCatalog< Record > declareCatalog(pybind11::module &mod, std::string const &name, bool isBase=false)
Wrap an instantiation of lsst::afw::table::CatalogT<Record>.
Definition: catalog.h:119
Key specialization for Flag.
Definition: Flag.h:79
T c_str(T... args)
size_type size() const
Return the number of elements in the catalog.
Definition: Catalog.h:399
iterator begin()
Iterator access.
Definition: Catalog.h:387