LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
catalog.h
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 #ifndef AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
24 #define AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
25 
26 #include "pybind11/pybind11.h"
27 
28 #include "lsst/utils/python.h"
30 #include "lsst/afw/table/Catalog.h"
31 
32 namespace lsst {
33 namespace afw {
34 namespace table {
35 namespace python {
36 
37 template <typename Record>
38 using PyCatalog = pybind11::class_<CatalogT<Record>, std::shared_ptr<CatalogT<Record>>>;
39 
41 template <typename T, typename Record>
42 ndarray::Array<typename Field<T>::Value const, 1, 1> _getArrayFromCatalog(
43  CatalogT<Record> const &catalog,
44  Key<T> const &key
45 ) {
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);
51  }
52  return out;
53 }
54 
55 // Specialization of the above for lsst::geom::Angle: have to return a double array (in
56 // radians), since NumPy arrays can't hold lsst::geom::Angles.
57 template <typename Record>
58 ndarray::Array<double const, 1, 1> _getArrayFromCatalog(
59  CatalogT<Record> const &catalog,
61 ) {
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();
67  }
68  return out;
69 }
70 
71 template <typename Record>
73  CatalogT<Record> & catalog,
74  Key<Flag> const & key,
75  ndarray::Array<bool const, 1> const & array
76 ) {
77  if (array.size() != catalog.size()) {
78  throw LSST_EXCEPT(
80  (boost::format("Catalog has %d rows, while array has %d elements.")
81  % catalog.size() % array.size()).str()
82  );
83  }
84  auto catIter = catalog.begin();
85  auto arrayIter = array.begin();
86  for (; catIter != catalog.end(); ++catIter, ++arrayIter) {
87  catIter->set(key, *arrayIter);
88  }
89 }
90 
91 template <typename Record>
93  CatalogT<Record> & catalog,
94  Key<Flag> const & key,
95  bool value
96 ) {
97  for (auto catIter = catalog.begin(); catIter != catalog.end(); ++catIter) {
98  catIter->set(key, value);
99  }
100 }
101 
110 template <typename T, typename Record>
112  namespace py = pybind11;
113  using namespace pybind11::literals;
114 
115  typedef CatalogT<Record> Catalog;
116  typedef typename Field<T>::Value Value;
117 
118  cls.def("isSorted", (bool (Catalog::*)(Key<T> const &) const) & Catalog::isSorted);
119  cls.def("sort", (void (Catalog::*)(Key<T> const &)) & Catalog::sort);
120  cls.def("find", [](Catalog &self, Value const &value, Key<T> const &key) -> std::shared_ptr<Record> {
121  auto iter = self.find(value, key);
122  if (iter == self.end()) {
123  return nullptr;
124  };
125  return iter;
126  });
127  cls.def("upper_bound", [](Catalog &self, Value const &value, Key<T> const &key) -> std::ptrdiff_t {
128  return self.upper_bound(value, key) - self.begin();
129  });
130  cls.def("lower_bound", [](Catalog &self, Value const &value, Key<T> const &key) -> std::ptrdiff_t {
131  return self.lower_bound(value, key) - self.begin();
132  });
133  cls.def("equal_range", [](Catalog &self, Value const &value, Key<T> const &key) {
134  auto p = self.equal_range(value, key);
135  return py::slice(p.first - self.begin(), p.second - self.begin(), 1);
136  });
137  cls.def("between", [](Catalog &self, Value const &lower, Value const &upper, Key<T> const &key) {
138  std::ptrdiff_t a = self.lower_bound(lower, key) - self.begin();
139  std::ptrdiff_t b = self.upper_bound(upper, key) - self.begin();
140  return py::slice(a, b, 1);
141  });
142 
143  cls.def("_getitem_",
144  [](Catalog const &self, Key<T> const &key) { return _getArrayFromCatalog(self, key); });
145  cls.def(
146  "_set_flag",
147  [](Catalog &self, Key<Flag> const & key, ndarray::Array<bool const, 1> const & array) {
148  _setFlagColumnToArray(self, key, array);
149  }
150  );
151  cls.def(
152  "_set_flag",
153  [](Catalog &self, Key<Flag> const & key, bool value) {
154  _setFlagColumnToScalar(self, key, value);
155  }
156  );
157 }
158 
172 template <typename Record>
174  bool isBase = false) {
175  namespace py = pybind11;
176  using namespace pybind11::literals;
177 
178  using Catalog = CatalogT<Record>;
179  using Table = typename Record::Table;
180 
181  std::string fullName;
182  if (isBase) {
183  fullName = "_" + name + "CatalogBase";
184  } else {
185  fullName = name + "Catalog";
186  }
187 
188  // We need py::dynamic_attr() in the class definition to support our Python-side caching
189  // of the associated ColumnView.
190  return wrappers.wrapType(
191  PyCatalog<Record>(wrappers.module, fullName.c_str(), py::dynamic_attr()),
192  [](auto &mod, auto &cls) {
193  /* Constructors */
194  cls.def(py::init<Schema const &>(), "schema"_a);
195  cls.def(py::init<std::shared_ptr<Table> const &>(), "table"_a);
196  cls.def(py::init<Catalog const &>(), "other"_a);
197 
198  /* Static Methods */
199  cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits,
200  "filename"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
201  cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
202  "manager"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
203  // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
204 
205  /* Methods */
206  cls.def("getTable", &Catalog::getTable);
207  cls.def_property_readonly("table", &Catalog::getTable);
208  cls.def("getSchema", &Catalog::getSchema);
209  cls.def_property_readonly("schema", &Catalog::getSchema);
210  cls.def("capacity", &Catalog::capacity);
211  cls.def("__len__", &Catalog::size);
212  cls.def("resize", &Catalog::resize);
213 
214  // Use private names for the following so the public Python method
215  // can manage the _column cache
216  cls.def("_getColumnView", &Catalog::getColumnView);
217  cls.def("_addNew", &Catalog::addNew);
218  cls.def("_extend", [](Catalog &self, Catalog const &other, bool deep) {
219  self.insert(self.end(), other.begin(), other.end(), deep);
220  });
221  cls.def("_extend", [](Catalog &self, Catalog const &other, SchemaMapper const &mapper) {
222  self.insert(mapper, self.end(), other.begin(), other.end());
223  });
224  cls.def("_append",
225  [](Catalog &self, std::shared_ptr<Record> const &rec) { self.push_back(rec); });
226  cls.def("_delitem_", [](Catalog &self, std::ptrdiff_t i) {
227  self.erase(self.begin() + utils::python::cppIndex(self.size(), i));
228  });
229  cls.def("_delslice_", [](Catalog &self, py::slice const &s) {
230  Py_ssize_t start = 0, stop = 0, step = 0, length = 0;
231  if (PySlice_GetIndicesEx(s.ptr(), self.size(), &start, &stop, &step, &length) != 0) {
232  throw py::error_already_set();
233  }
234  if (step != 1) {
235  throw py::index_error("Slice step must not exactly 1");
236  }
237  self.erase(self.begin() + start, self.begin() + stop);
238  });
239  cls.def("_clear", &Catalog::clear);
240 
241  cls.def("set", &Catalog::set);
242  cls.def("_getitem_", [](Catalog &self, int i) {
243  return self.get(utils::python::cppIndex(self.size(), i));
244  });
245  cls.def("isContiguous", &Catalog::isContiguous);
246  cls.def("writeFits",
247  (void (Catalog::*)(std::string const &, std::string const &, int) const) &
248  Catalog::writeFits,
249  "filename"_a, "mode"_a = "w", "flags"_a = 0);
250  cls.def("writeFits",
251  (void (Catalog::*)(fits::MemFileManager &, std::string const &, int) const) &
252  Catalog::writeFits,
253  "manager"_a, "mode"_a = "w", "flags"_a = 0);
254  cls.def("reserve", &Catalog::reserve);
255  cls.def("subset",
256  (Catalog(Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset);
257  cls.def("subset",
259  Catalog::subset);
260 
261  declareCatalogOverloads<std::int32_t>(cls);
262  declareCatalogOverloads<std::int64_t>(cls);
263  declareCatalogOverloads<float>(cls);
264  declareCatalogOverloads<double>(cls);
265  declareCatalogOverloads<lsst::geom::Angle>(cls);
266 
267  cls.def("_getitem_",
268  [](Catalog const &self, Key<Flag> const &key) -> ndarray::Array<bool const, 1, 0> {
269  return _getArrayFromCatalog(self, key);
270  });
271 
272  });
273 }
274 
275 } // namespace python
276 } // namespace table
277 } // namespace afw
278 } // namespace lsst
279 
280 #endif // !LSST_AFW_TABLE_PYTHON_CATALOG_H_INCLUDED
lsst::afw::table::CatalogT::end
iterator end()
Definition: Catalog.h:397
std::string
STL class.
std::shared_ptr
STL class.
python.h
lsst::afw::table::python::PyCatalog
pybind11::class_< CatalogT< Record >, std::shared_ptr< CatalogT< Record > >> PyCatalog
Definition: catalog.h:38
lsst::afw
Definition: imageAlgorithm.dox:1
BaseColumnView.h
lsst::afw::table._base.Catalog
Definition: _base.py:88
lsst::utils::python::cppIndex
std::size_t cppIndex(std::ptrdiff_t size, std::ptrdiff_t i)
Compute a C++ index from a Python index (negative values count from the end) and range-check.
Definition: python.h:124
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw::fits::MemFileManager
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
lsst::afw::table::python::_getArrayFromCatalog
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:42
lsst::afw::table::python::_setFlagColumnToScalar
void _setFlagColumnToScalar(CatalogT< Record > &catalog, Key< Flag > const &key, bool value)
Definition: catalog.h:92
end
int end
Definition: BoundedField.cc:105
lsst::afw::geom.transform.transformContinued.cls
cls
Definition: transformContinued.py:33
lsst::afw::table::python::declareCatalog
PyCatalog< Record > declareCatalog(utils::python::WrapperCollection &wrappers, std::string const &name, bool isBase=false)
Wrap an instantiation of lsst::afw::table::CatalogT<Record>.
Definition: catalog.h:173
lsst::afw::table::python::_setFlagColumnToArray
void _setFlagColumnToArray(CatalogT< Record > &catalog, Key< Flag > const &key, ndarray::Array< bool const, 1 > const &array)
Definition: catalog.h:72
lsst::afw::table::CatalogT::size
size_type size() const
Return the number of elements in the catalog.
Definition: Catalog.h:408
lsst::afw::table._base.Catalog.clear
def clear(self)
Definition: _base.py:166
lsst::utils::python::WrapperCollection::wrapType
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
Definition: python.h:391
lsst.pex::exceptions::LengthError
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
std::string::c_str
T c_str(T... args)
other
ItemVariant const * other
Definition: Schema.cc:56
step
int const step
Definition: BoundedField.cc:102
lsst::afw::table::SchemaMapper
A mapping between the keys of two Schemas, used to copy data between them.
Definition: SchemaMapper.h:21
lsst::afw::table::Key
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
lsst::utils::python::WrapperCollection
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition: python.h:242
b
table::Key< int > b
Definition: TransmissionCurve.cc:467
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
a
table::Key< int > a
Definition: TransmissionCurve.cc:466
key
Key< U > key
Definition: Schema.cc:281
std::ptrdiff_t
mapper
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
lsst::afw::table::FieldBase::Value
T Value
the type returned by BaseRecord::get
Definition: FieldBase.h:44
pybind11
Definition: _GenericMap.cc:40
lsst::utils::python::WrapperCollection::module
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition: python.h:448
Catalog.h
lsst::afw::table::CatalogT::begin
iterator begin()
Iterator access.
Definition: Catalog.h:396
astshim.fitsChanContinued.length
def length(self)
Definition: fitsChanContinued.py:81
lsst::afw::table::CatalogT
A custom container class for records, based on std::vector.
Definition: Catalog.h:97
set
daf::base::PropertySet * set
Definition: fits.cc:912
astshim.fitsChanContinued.iter
def iter(self)
Definition: fitsChanContinued.py:88
lsst::afw::table::python::declareCatalogOverloads
void declareCatalogOverloads(PyCatalog< Record > &cls)
Declare field-type-specific overloaded catalog member functions for one field type.
Definition: catalog.h:111
lsst::afw::table::Key< Flag >
Key specialization for Flag.
Definition: Flag.h:94