LSST Applications g04e9c324dd+8c5ae1fdc5,g134cb467dc+b203dec576,g18429d2f64+358861cd2c,g199a45376c+0ba108daf9,g1fd858c14a+dd066899e3,g262e1987ae+ebfced1d55,g29ae962dfc+72fd90588e,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+b668f15bc5,g4595892280+3897dae354,g47891489e3+abcf9c3559,g4d44eb3520+fb4ddce128,g53246c7159+8c5ae1fdc5,g67b6fd64d1+abcf9c3559,g67fd3c3899+1f72b5a9f7,g74acd417e5+cb6b47f07b,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+abcf9c3559,g8d7436a09f+bcf525d20c,g8ea07a8fe4+9f5ccc88ac,g90f42f885a+6054cc57f1,g97be763408+06f794da49,g9dd6db0277+1f72b5a9f7,ga681d05dcb+7e36ad54cd,gabf8522325+735880ea63,gac2eed3f23+abcf9c3559,gb89ab40317+abcf9c3559,gbf99507273+8c5ae1fdc5,gd8ff7fe66e+1f72b5a9f7,gdab6d2f7ff+cb6b47f07b,gdc713202bf+1f72b5a9f7,gdfd2d52018+8225f2b331,ge365c994fd+375fc21c71,ge410e46f29+abcf9c3559,geaed405ab2+562b3308c0,gf9a733ac38+8c5ae1fdc5,w.2025.35
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