lsst.afw  16.0-79-g81b2eb930+1
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 
40 template <typename T, typename Record>
41 ndarray::Array<typename Field<T>::Value const, 1, 1> _getArrayFromCatalog(
42  CatalogT<Record> const &catalog,
43  Key<T> const &key
44 ) {
45  ndarray::Array<typename Field<T>::Value, 1, 1> out = ndarray::allocate(catalog.size());
46  auto outIter = out.begin();
47  auto inIter = catalog.begin();
48  for (; inIter != catalog.end(); ++inIter, ++outIter) {
49  *outIter = inIter->get(key);
50  }
51  return out;
52 }
53 
54 // Specialization of the above for lsst::geom::Angle: have to return a double array (in
55 // radians), since NumPy arrays can't hold lsst::geom::Angles.
56 template <typename Record>
57 ndarray::Array<double const, 1, 1> _getArrayFromCatalog(
58  CatalogT<Record> const &catalog,
60 ) {
61  ndarray::Array<double, 1, 1> out = ndarray::allocate(catalog.size());
62  auto outIter = out.begin();
63  auto inIter = catalog.begin();
64  for (; inIter != catalog.end(); ++inIter, ++outIter) {
65  *outIter = inIter->get(key).asRadians();
66  }
67  return out;
68 }
69 
79 template <typename T, typename Record>
81  namespace py = pybind11;
82  using namespace pybind11::literals;
83 
84  typedef CatalogT<Record> Catalog;
85  typedef typename Field<T>::Value Value;
86 
87  cls.def("isSorted", (bool (Catalog::*)(Key<T> const &) const) & Catalog::isSorted);
88  cls.def("sort", (void (Catalog::*)(Key<T> const &)) & Catalog::sort);
89  cls.def("find", [](Catalog &self, Value const &value, Key<T> const &key) -> std::shared_ptr<Record> {
90  auto iter = self.find(value, key);
91  if (iter == self.end()) {
92  return nullptr;
93  };
94  return iter;
95  });
96  cls.def("upper_bound", [](Catalog &self, Value const &value, Key<T> const &key) -> std::ptrdiff_t {
97  return self.upper_bound(value, key) - self.begin();
98  });
99  cls.def("lower_bound", [](Catalog &self, Value const &value, Key<T> const &key) -> std::ptrdiff_t {
100  return self.lower_bound(value, key) - self.begin();
101  });
102  cls.def("equal_range", [](Catalog &self, Value const &value, Key<T> const &key) {
103  auto p = self.equal_range(value, key);
104  return py::slice(p.first - self.begin(), p.second - self.begin(), 1);
105  });
106  cls.def("between", [](Catalog &self, Value const &lower, Value const &upper, Key<T> const &key) {
107  std::ptrdiff_t a = self.lower_bound(lower, key) - self.begin();
108  std::ptrdiff_t b = self.upper_bound(upper, key) - self.begin();
109  return py::slice(a, b, 1);
110  });
111 
112  cls.def("_getitem_",
113  [](Catalog const &self, Key<T> const &key) { return _getArrayFromCatalog(self, key); });
114 }
115 
128 template <typename Record>
129 PyCatalog<Record> declareCatalog(pybind11::module &mod, std::string const &name, bool isBase = false) {
130  namespace py = pybind11;
131  using namespace pybind11::literals;
132 
133  using Catalog = CatalogT<Record>;
134  using Table = typename Record::Table;
135 
136  std::string fullName;
137  if (isBase) {
138  fullName = "_" + name + "CatalogBase";
139  } else {
140  fullName = name + "Catalog";
141  }
142 
143  // We need py::dynamic_attr() below to support our Python-side caching of the associated ColumnView.
144  PyCatalog<Record> cls(mod, fullName.c_str(), py::dynamic_attr());
145 
146  /* Constructors */
147  cls.def(py::init<Schema const &>(), "schema"_a);
148  cls.def(py::init<std::shared_ptr<Table> const &>(), "table"_a);
149  cls.def(py::init<Catalog const &>(), "other"_a);
150 
151  /* Static Methods */
152  cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits, "filename"_a,
153  "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
154  cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
155  "manager"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
156  // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
157 
158  /* Methods */
159  cls.def("getTable", &Catalog::getTable);
160  cls.def_property_readonly("table", &Catalog::getTable);
161  cls.def("getSchema", &Catalog::getSchema);
162  cls.def_property_readonly("schema", &Catalog::getSchema);
163  cls.def("capacity", &Catalog::capacity);
164  cls.def("__len__", &Catalog::size);
165 
166  // Use private names for the following so the public Python method
167  // can manage the _column cache
168  cls.def("_getColumnView", &Catalog::getColumnView);
169  cls.def("_addNew", &Catalog::addNew);
170  cls.def("_extend", [](Catalog &self, Catalog const &other, bool deep) {
171  self.insert(self.end(), other.begin(), other.end(), deep);
172  });
173  cls.def("_extend", [](Catalog &self, Catalog const &other, SchemaMapper const &mapper) {
174  self.insert(mapper, self.end(), other.begin(), other.end());
175  });
176  cls.def("_append", [](Catalog &self, std::shared_ptr<Record> const &rec) { self.push_back(rec); });
177  cls.def("_delitem_", [](Catalog &self, std::ptrdiff_t i) {
178  self.erase(self.begin() + utils::python::cppIndex(self.size(), i));
179  });
180  cls.def("_delslice_", [](Catalog &self, py::slice const &s) {
181  Py_ssize_t start = 0, stop = 0, step = 0, length = 0;
182  if (PySlice_GetIndicesEx(
183 // The interface to this function changed in Python 3.2
184 #if PY_MAJOR_VERSION < 3 || (PY_MINOR_VERSION == 3 && PY_MINOR_VERSION < 2)
185  (PySliceObject *)
186 #endif
187  s.ptr(),
188  self.size(), &start, &stop, &step, &length) != 0) {
189  throw py::error_already_set();
190  }
191  if (step != 1) {
192  throw py::index_error("Slice step must not exactly 1");
193  }
194  self.erase(self.begin() + start, self.begin() + stop);
195  });
196  cls.def("_clear", &Catalog::clear);
197 
198  cls.def("set", &Catalog::set);
199  cls.def("_getitem_",
200  [](Catalog &self, int i) { return self.get(utils::python::cppIndex(self.size(), i)); });
201  cls.def("isContiguous", &Catalog::isContiguous);
202  cls.def("writeFits",
203  (void (Catalog::*)(std::string const &, std::string const &, int) const) & Catalog::writeFits,
204  "filename"_a, "mode"_a = "w", "flags"_a = 0);
205  cls.def("writeFits",
206  (void (Catalog::*)(fits::MemFileManager &, std::string const &, int) const) & Catalog::writeFits,
207  "manager"_a, "mode"_a = "w", "flags"_a = 0);
208  cls.def("reserve", &Catalog::reserve);
209  cls.def("subset", (Catalog(Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset);
210  cls.def("subset",
211  (Catalog(Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t) const) & Catalog::subset);
212 
213  declareCatalogOverloads<std::int32_t>(cls);
214  declareCatalogOverloads<std::int64_t>(cls);
215  declareCatalogOverloads<float>(cls);
216  declareCatalogOverloads<double>(cls);
217  declareCatalogOverloads<lsst::geom::Angle>(cls);
218 
219  cls.def("_getitem_", [](Catalog const &self, Key<Flag> const &key) -> ndarray::Array<bool const, 1, 0> {
220  return _getArrayFromCatalog(self, key);
221  });
222 
223  return cls;
224 };
225 } // namespace python
226 } // namespace table
227 } // namespace afw
228 } // namespace lsst
229 
230 #endif // !LSST_AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
A custom container class for records, based on std::vector.
Definition: Catalog.h:97
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
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.
Definition: catalog.h:41
pybind11::class_< CatalogT< Record >, std::shared_ptr< CatalogT< Record > >> PyCatalog
Definition: catalog.h:37
ItemVariant const * other
Definition: Schema.cc:56
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
int const step
STL class.
A base class for image defects.
int end
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
iterator end()
Iterator access.
Definition: Catalog.h:397
table::Key< int > b
Key< U > key
Definition: Schema.cc:281
void declareCatalogOverloads(PyCatalog< Record > &cls)
Declare field-type-specific overloaded catalog member functions for one field type.
Definition: catalog.h:80
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:44
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:129
Key specialization for Flag.
Definition: Flag.h:94
T c_str(T... args)
table::Key< int > a
size_type size() const
Return the number of elements in the catalog.
Definition: Catalog.h:408
iterator begin()
Iterator access.
Definition: Catalog.h:396
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
daf::base::PropertySet * set
Definition: fits.cc:832