LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
Typedefs | Functions
lsst::afw::table::python Namespace Reference

Typedefs

template<typename Record >
using PyCatalog = pybind11::class_< CatalogT< Record >, std::shared_ptr< CatalogT< Record > >>
 
template<typename Record >
using PyColumnView = pybind11::class_< ColumnViewT< Record >, std::shared_ptr< ColumnViewT< Record > >, BaseColumnView >
 
template<typename Record >
using PySortedCatalog = pybind11::class_< SortedCatalogT< Record >, std::shared_ptr< SortedCatalogT< Record > >, CatalogT< Record > >
 

Functions

template<typename T , typename Record >
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. More...
 
template<typename Record >
ndarray::Array< double const, 1, 1 > _getArrayFromCatalog (CatalogT< Record > const &catalog, Key< lsst::geom::Angle > const &key)
 
template<typename T , typename Record >
void declareCatalogOverloads (PyCatalog< Record > &cls)
 Declare field-type-specific overloaded catalog member functions for one field type. More...
 
template<typename Record >
PyCatalog< Record > declareCatalog (utils::python::WrapperCollection &wrappers, std::string const &name, bool isBase=false)
 Wrap an instantiation of lsst::afw::table::CatalogT<Record>. More...
 
template<typename Record >
PyColumnView< Record > declareColumnView (utils::python::WrapperCollection &wrappers, std::string const &name, bool isBase=false)
 Declare member and static functions for a given instantiation of lsst::afw::table::ColumnViewT<RecordT>. More...
 
template<typename Record >
PySortedCatalog< Record > declareSortedCatalog (utils::python::WrapperCollection &wrappers, std::string const &name, bool isBase=false)
 Wrap an instantiation of lsst::afw::table::SortedCatalogT<Record>. More...
 

Typedef Documentation

◆ PyCatalog

template<typename Record >
using lsst::afw::table::python::PyCatalog = typedef pybind11::class_<CatalogT<Record>, std::shared_ptr<CatalogT<Record> >>

Definition at line 38 of file catalog.h.

◆ PyColumnView

template<typename Record >
using lsst::afw::table::python::PyColumnView = typedef pybind11::class_<ColumnViewT<Record>, std::shared_ptr<ColumnViewT<Record> >, BaseColumnView>

Definition at line 39 of file columnView.h.

◆ PySortedCatalog

template<typename Record >
using lsst::afw::table::python::PySortedCatalog = typedef pybind11::class_<SortedCatalogT<Record>, std::shared_ptr<SortedCatalogT<Record> >, CatalogT<Record> >

Definition at line 40 of file sortedCatalog.h.

Function Documentation

◆ _getArrayFromCatalog() [1/2]

template<typename T , typename Record >
ndarray::Array<typename Field<T>::Value const, 1, 1> lsst::afw::table::python::_getArrayFromCatalog ( CatalogT< Record > const &  catalog,
Key< T > const &  key 
)

Extract a column from a potentially non-contiguous Catalog.

Parameters
catalogCatalog
keyKey to column to extract

Definition at line 42 of file catalog.h.

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 }
Key< U > key
Definition: Schema.cc:281

◆ _getArrayFromCatalog() [2/2]

template<typename Record >
ndarray::Array<double const, 1, 1> lsst::afw::table::python::_getArrayFromCatalog ( CatalogT< Record > const &  catalog,
Key< lsst::geom::Angle > const &  key 
)
Parameters
catalogCatalog
keyKey to column to extract

Definition at line 58 of file catalog.h.

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 }
Key< U > key
Definition: Schema.cc:281

◆ declareCatalog()

template<typename Record >
PyCatalog<Record> lsst::afw::table::python::declareCatalog ( utils::python::WrapperCollection wrappers,
std::string const &  name,
bool  isBase = false 
)

Wrap an instantiation of lsst::afw::table::CatalogT<Record>.

In addition to calling this method you must call addCatalogMethods on the class object in Python.

Template Parameters
RecordRecord type, e.g. BaseRecord or SimpleRecord.
Parameters
[in]wrappersPackage manager class will be added to.
[in]nameName prefix of the record type, e.g. "Base" or "Simple".
[in]isBaseWhether this instantiation is only being used as a base class (used to set the class name).

Definition at line 130 of file catalog.h.

131  {
132  namespace py = pybind11;
133  using namespace pybind11::literals;
134 
135  using Catalog = CatalogT<Record>;
136  using Table = typename Record::Table;
137 
138  std::string fullName;
139  if (isBase) {
140  fullName = "_" + name + "CatalogBase";
141  } else {
142  fullName = name + "Catalog";
143  }
144 
145  // We need py::dynamic_attr() in the class definition to support our Python-side caching
146  // of the associated ColumnView.
147  return wrappers.wrapType(
148  PyCatalog<Record>(wrappers.module, fullName.c_str(), py::dynamic_attr()),
149  [](auto &mod, auto &cls) {
150  /* Constructors */
151  cls.def(py::init<Schema const &>(), "schema"_a);
152  cls.def(py::init<std::shared_ptr<Table> const &>(), "table"_a);
153  cls.def(py::init<Catalog const &>(), "other"_a);
154 
155  /* Static Methods */
156  cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits,
157  "filename"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
158  cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
159  "manager"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
160  // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
161 
162  /* Methods */
163  cls.def("getTable", &Catalog::getTable);
164  cls.def_property_readonly("table", &Catalog::getTable);
165  cls.def("getSchema", &Catalog::getSchema);
166  cls.def_property_readonly("schema", &Catalog::getSchema);
167  cls.def("capacity", &Catalog::capacity);
168  cls.def("__len__", &Catalog::size);
169  cls.def("resize", &Catalog::resize);
170 
171  // Use private names for the following so the public Python method
172  // can manage the _column cache
173  cls.def("_getColumnView", &Catalog::getColumnView);
174  cls.def("_addNew", &Catalog::addNew);
175  cls.def("_extend", [](Catalog &self, Catalog const &other, bool deep) {
176  self.insert(self.end(), other.begin(), other.end(), deep);
177  });
178  cls.def("_extend", [](Catalog &self, Catalog const &other, SchemaMapper const &mapper) {
179  self.insert(mapper, self.end(), other.begin(), other.end());
180  });
181  cls.def("_append",
182  [](Catalog &self, std::shared_ptr<Record> const &rec) { self.push_back(rec); });
183  cls.def("_delitem_", [](Catalog &self, std::ptrdiff_t i) {
184  self.erase(self.begin() + utils::python::cppIndex(self.size(), i));
185  });
186  cls.def("_delslice_", [](Catalog &self, py::slice const &s) {
187  Py_ssize_t start = 0, stop = 0, step = 0, length = 0;
188  if (PySlice_GetIndicesEx(s.ptr(), 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_", [](Catalog &self, int i) {
200  return self.get(utils::python::cppIndex(self.size(), i));
201  });
202  cls.def("isContiguous", &Catalog::isContiguous);
203  cls.def("writeFits",
204  (void (Catalog::*)(std::string const &, std::string const &, int) const) &
205  Catalog::writeFits,
206  "filename"_a, "mode"_a = "w", "flags"_a = 0);
207  cls.def("writeFits",
208  (void (Catalog::*)(fits::MemFileManager &, std::string const &, int) const) &
209  Catalog::writeFits,
210  "manager"_a, "mode"_a = "w", "flags"_a = 0);
211  cls.def("reserve", &Catalog::reserve);
212  cls.def("subset",
213  (Catalog(Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset);
214  cls.def("subset",
215  (Catalog(Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t) const) &
216  Catalog::subset);
217 
218  declareCatalogOverloads<std::int32_t>(cls);
219  declareCatalogOverloads<std::int64_t>(cls);
220  declareCatalogOverloads<float>(cls);
221  declareCatalogOverloads<double>(cls);
222  declareCatalogOverloads<lsst::geom::Angle>(cls);
223 
224  cls.def("_getitem_",
225  [](Catalog const &self, Key<Flag> const &key) -> ndarray::Array<bool const, 1, 0> {
226  return _getArrayFromCatalog(self, key);
227  });
228 
229  });
230 }
ndarray::Array< double const, 1, 1 > _getArrayFromCatalog(CatalogT< Record > const &catalog, Key< lsst::geom::Angle > const &key)
Definition: catalog.h:58
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
def init()
Definition: tests.py:65
ItemVariant const * other
Definition: Schema.cc:56
SchemaMapper * mapper
Definition: SchemaMapper.cc:78
daf::base::PropertySet * set
Definition: fits.cc:902
STL class.
int const step
Key< U > key
Definition: Schema.cc:281
T begin(T... args)
T c_str(T... args)
int end
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18

◆ declareCatalogOverloads()

template<typename T , typename Record >
void lsst::afw::table::python::declareCatalogOverloads ( PyCatalog< Record > &  cls)

Declare field-type-specific overloaded catalog member functions for one field type.

Template Parameters
TField type.
RecordRecord type, e.g. BaseRecord or SimpleRecord.
Parameters
[in]clsCatalog pybind11 class.

Definition at line 80 of file catalog.h.

80  {
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 }
ndarray::Array< double const, 1, 1 > _getArrayFromCatalog(CatalogT< Record > const &catalog, Key< lsst::geom::Angle > const &key)
Definition: catalog.h:58
table::Key< int > b
table::Key< int > a
Key< U > key
Definition: Schema.cc:281
int end

◆ declareColumnView()

template<typename Record >
PyColumnView<Record> lsst::afw::table::python::declareColumnView ( utils::python::WrapperCollection wrappers,
std::string const &  name,
bool  isBase = false 
)

Declare member and static functions for a given instantiation of lsst::afw::table::ColumnViewT<RecordT>.

Template Parameters
RecordRecord type, e.g. BaseRecord or SimpleRecord.
Parameters
[in]wrappersPackage manager class will be added to.
[in]nameName prefix of the record type, e.g. "Base" or "Simple".
[in]isBaseWhether this instantiation is only being used as a base class (used to set the class name).

Definition at line 52 of file columnView.h.

53  {
54  std::string fullName;
55  if (isBase) {
56  fullName = "_" + name + "ColumnViewBase";
57  } else {
58  fullName = name + "ColumnView";
59  }
60  return wrappers.wrapType(PyColumnView<Record>(wrappers.module, fullName.c_str()),
61  [](auto& mod, auto& cls) {
62  cls.def("getTable", &ColumnViewT<Record>::getTable);
63  cls.def_property_readonly("table", &ColumnViewT<Record>::getTable);
64  });
65 };
STL class.
T c_str(T... args)

◆ declareSortedCatalog()

template<typename Record >
PySortedCatalog<Record> lsst::afw::table::python::declareSortedCatalog ( utils::python::WrapperCollection wrappers,
std::string const &  name,
bool  isBase = false 
)

Wrap an instantiation of lsst::afw::table::SortedCatalogT<Record>.

In addition to calling this method (which also instantiates and wraps the CatalogT base class), you must call addCatalogMethods on the class object in Python.

Template Parameters
RecordRecord type, e.g. BaseRecord or SimpleRecord.
Parameters
[in]wrappersPackage manager class will be added to.
[in]nameName prefix of the record type, e.g. "Base" or "Simple".
[in]isBaseWhether this instantiation is only being used as a base class (used to set the class name).

Definition at line 57 of file sortedCatalog.h.

58  {
59  namespace py = pybind11;
60  using namespace pybind11::literals;
61 
62  using Catalog = SortedCatalogT<Record>;
63  using Table = typename Record::Table;
64 
65  auto clsBase = declareCatalog<Record>(wrappers, name, true);
66 
67  std::string fullName;
68  if (isBase) {
69  fullName = "_" + name + "SortedCatalogBase";
70  } else {
71  fullName = name + "Catalog";
72  }
73 
74  // We need py::dynamic_attr() in the class definition to support our Python-side caching
75  // of the associated ColumnView.
76  return wrappers.wrapType(
77  PySortedCatalog<Record>(wrappers.module, fullName.c_str(), py::dynamic_attr()),
78  [clsBase](auto &mod, auto &cls) {
79  /* Constructors */
80  cls.def(pybind11::init<Schema const &>());
82  "table"_a = std::shared_ptr<Table>());
83  cls.def(pybind11::init<Catalog const &>());
84 
85  /* Overridden and Variant Methods */
86  cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits,
87  "filename"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
88  cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
89  "manager"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
90  // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
91 
92  cls.def("subset",
93  (Catalog(Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset);
94  cls.def("subset",
95  (Catalog(Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t) const) &
96  Catalog::subset);
97 
98  // The following three methods shadow those in the base class in C++ (unlike the base class
99  // versions, they do not require a key argument because we assume it's the ID key). In
100  // Python, we make that appear as though the key argument is available but has a default
101  // value. If that key is not None, we delegate to the base class.
102  cls.def("isSorted",
103  [clsBase](py::object const &self, py::object key) -> py::object {
104  if (key.is(py::none())) {
105  key = self.attr("table").attr("getIdKey")();
106  }
107  return clsBase.attr("isSorted")(self, key);
108  },
109  "key"_a = py::none());
110  cls.def("sort",
111  [clsBase](py::object const &self, py::object key) -> py::object {
112  if (key.is(py::none())) {
113  key = self.attr("table").attr("getIdKey")();
114  }
115  return clsBase.attr("sort")(self, key);
116  },
117  "key"_a = py::none());
118  cls.def("find",
119  [clsBase](py::object const &self, py::object const &value,
120  py::object key) -> py::object {
121  if (key.is(py::none())) {
122  key = self.attr("table").attr("getIdKey")();
123  }
124  return clsBase.attr("find")(self, value, key);
125  },
126  "value"_a, "key"_a = py::none());
127 
128  });
129 }
def init()
Definition: tests.py:65
STL class.
Key< U > key
Definition: Schema.cc:281
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18