LSST Applications g013ef56533+b8d55c8942,g083dd6704c+a047e97985,g199a45376c+0ba108daf9,g1fd858c14a+7a3b874d60,g210f2d0738+7416ca6900,g262e1987ae+1d557ba9a3,g29ae962dfc+519d34895e,g2cef7863aa+aef1011c0b,g30d7c61c20+36d16ea71a,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+cb326ad149,g47891489e3+f459a6810c,g53246c7159+8c5ae1fdc5,g54cd7ddccb+890c8e1e5d,g5a60e81ecd+6240c63dbc,g64539dfbff+7416ca6900,g67b6fd64d1+f459a6810c,g6ebf1fc0d4+8c5ae1fdc5,g74acd417e5+0bae3c876a,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+f459a6810c,g8d7436a09f+dee7680868,g8ea07a8fe4+81eaaadc04,g90f42f885a+34c0557caf,g97be763408+14b8164b5b,g98a1a72a9c+8389601a76,g98df359435+fff771c62d,gb8cb2b794d+6728931916,gbf99507273+8c5ae1fdc5,gc2a301910b+7416ca6900,gca7fc764a6+f459a6810c,gd7ef33dd92+f459a6810c,gdab6d2f7ff+0bae3c876a,ge410e46f29+f459a6810c,ge41e95a9f2+7416ca6900,geaed405ab2+e3b4b2a692,gf9a733ac38+8c5ae1fdc5,w.2025.43
LSST Data Management Base Package
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,...
A helper class for subdividing pybind11 module across multiple translation units (i....
Definition python.h:242
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
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
Definition python.h:448
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