lsst.afw gf03f0b42f3+e27ba6bf39
Loading...
Searching...
No Matches
sortedCatalog.h
Go to the documentation of this file.
1#ifndef AFW_TABLE_PYBIND11_SORTEDCATALOG_H_INCLUDED
2#define AFW_TABLE_PYBIND11_SORTEDCATALOG_H_INCLUDED
3/*
4 * This file is part of afw.
5 *
6 * Developed for the LSST Data Management System.
7 * This product includes software developed by the LSST Project
8 * (https://www.lsst.org).
9 * See the COPYRIGHT file at the top-level directory of this distribution
10 * for details of code ownership.
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26#include "pybind11/pybind11.h"
27
29
32
33namespace lsst {
34namespace afw {
35namespace table {
36namespace python {
37
38template <typename Record>
39using PySortedCatalog = pybind11::classh<SortedCatalogT<Record>, CatalogT<Record>>;
40
55template <typename Record>
57 std::string const &name, bool isBase = false) {
58 namespace py = pybind11;
59 using namespace pybind11::literals;
60
62 using Table = typename Record::Table;
63
64 auto clsBase = declareCatalog<Record>(wrappers, name, true);
65
66 std::string fullName;
67 if (isBase) {
68 fullName = "_" + name + "SortedCatalogBase";
69 } else {
70 fullName = name + "Catalog";
71 }
72
73 // We need py::dynamic_attr() in the class definition to support our Python-side caching
74 // of the associated ColumnView.
75 return wrappers.wrapType(
76 PySortedCatalog<Record>(wrappers.module, fullName.c_str(), py::dynamic_attr()),
77 [clsBase](auto &mod, auto &cls) {
78 /* Constructors */
79 cls.def(pybind11::init<Schema const &>());
80 cls.def(pybind11::init<std::shared_ptr<Table> const &>(),
81 "table"_a = std::shared_ptr<Table>());
82 cls.def(pybind11::init<Catalog const &>());
83
84 /* Overridden and Variant Methods */
85 cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits,
86 "filename"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
87 cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
88 "manager"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
89 // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
90
91 cls.def("subset",
92 (Catalog(Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset);
93 cls.def("subset",
94 (Catalog(Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t) const) &
95 Catalog::subset);
96
97 // The following three methods shadow those in the base class in C++ (unlike the base class
98 // versions, they do not require a key argument because we assume it's the ID key). In
99 // Python, we make that appear as though the key argument is available but has a default
100 // value. If that key is not None, we delegate to the base class.
101 cls.def("isSorted",
102 [clsBase](py::object const &self, py::object key) -> py::object {
103 if (key.is(py::none())) {
104 key = self.attr("table").attr("getIdKey")();
105 }
106 return clsBase.attr("isSorted")(self, key);
107 },
108 "key"_a = py::none());
109 cls.def("sort",
110 [clsBase](py::object const &self, py::object key) -> py::object {
111 if (key.is(py::none())) {
112 key = self.attr("table").attr("getIdKey")();
113 }
114 return clsBase.attr("sort")(self, key);
115 },
116 "key"_a = py::none());
117 cls.def("find",
118 [clsBase](py::object const &self, py::object const &value,
119 py::object key) -> py::object {
120 if (key.is(py::none())) {
121 key = self.attr("table").attr("getIdKey")();
122 }
123 return clsBase.attr("find")(self, value, key);
124 },
125 "value"_a, "key"_a = py::none());
126
127 });
128}
129
130} // namespace python
131} // namespace table
132} // namespace afw
133} // namespace lsst
134
135#endif // !AFW_TABLE_PYBIND11_CATALOG_H_INCLUDED
T c_str(T... args)
A custom container class for records, based on std::vector.
Definition Catalog.h:98
Custom catalog class for record/table subclasses that are guaranteed to have an ID,...
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
PyCatalog< Record > declareCatalog(cpputils::python::WrapperCollection &wrappers, std::string const &name, bool isBase=false)
Wrap an instantiation of lsst::afw::table::CatalogT<Record>.
Definition catalog.h:276
PySortedCatalog< Record > declareSortedCatalog(cpputils::python::WrapperCollection &wrappers, std::string const &name, bool isBase=false)
Wrap an instantiation of lsst::afw::table::SortedCatalogT<Record>.
pybind11::classh< SortedCatalogT< Record >, CatalogT< Record > > PySortedCatalog