LSST Applications g013ef56533+7c9321ec0f,g042eb84c57+c6cfa41bc3,g199a45376c+0ba108daf9,g1fd858c14a+fcad0d0313,g210f2d0738+c0f94c6586,g262e1987ae+a7e710680e,g29ae962dfc+fb55f2edb0,g2ac17093b6+61d6563b1e,g2b1d02342f+df6f932764,g2cef7863aa+aef1011c0b,g2f7ad74990+c0f94c6586,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+53cf87ae69,g47891489e3+4316d04fff,g511e8cfd20+baa56acf6c,g53246c7159+8c5ae1fdc5,g54cd7ddccb+fd7ad03fde,g64539dfbff+c0f94c6586,g67b6fd64d1+4316d04fff,g67fd3c3899+c0f94c6586,g6985122a63+4316d04fff,g74acd417e5+ca833bee28,g786e29fd12+668abc6043,g81db2e9a8d+b2ec8e584f,g87389fa792+8856018cbb,g89139ef638+4316d04fff,g8d7436a09f+0a24083b20,g8ea07a8fe4+760ca7c3fc,g90f42f885a+033b1d468d,g97be763408+11eb8fd5b8,gbf99507273+8c5ae1fdc5,gcdda8b9158+e4c84c9d5c,gce8aa8abaa+8c5ae1fdc5,gd7ef33dd92+4316d04fff,gdab6d2f7ff+ca833bee28,ge410e46f29+4316d04fff,geaed405ab2+c4bbc419c6,gf9a733ac38+8c5ae1fdc5,w.2025.40
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