lsst.afw  14.0-35-g29d3a87a7
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  * LSST Data Management System
5  * Copyright 2008-2017 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #include "pybind11/pybind11.h"
26 
29 
30 namespace lsst {
31 namespace afw {
32 namespace table {
33 namespace python {
34 
35 template <typename Record>
36 using PySortedCatalog =
37  pybind11::class_<SortedCatalogT<Record>, std::shared_ptr<SortedCatalogT<Record>>, CatalogT<Record>>;
38 
52 template <typename Record>
54  bool isBase = false) {
55  namespace py = pybind11;
56  using namespace pybind11::literals;
57 
59  using Table = typename Record::Table;
60 
61  auto clsBase = declareCatalog<Record>(mod, name, true);
62 
63  std::string fullName;
64  if (isBase) {
65  fullName = "_" + name + "SortedCatalogBase";
66  } else {
67  fullName = name + "Catalog";
68  }
69 
70  // We need py::dynamic_attr() below to support our Python-side caching of the associated ColumnView.
71  PySortedCatalog<Record> cls(mod, fullName.c_str(), py::dynamic_attr());
72 
73  /* Constructors */
74  cls.def(pybind11::init<Schema const &>());
75  cls.def(pybind11::init<std::shared_ptr<Table> const &>(), "table"_a = std::shared_ptr<Table>());
76  cls.def(pybind11::init<Catalog const &>());
77 
78  /* Overridden and Variant Methods */
79  cls.def_static("readFits", (Catalog(*)(std::string const &, int, int)) & Catalog::readFits, "filename"_a,
80  "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
81  cls.def_static("readFits", (Catalog(*)(fits::MemFileManager &, int, int)) & Catalog::readFits,
82  "manager"_a, "hdu"_a = fits::DEFAULT_HDU, "flags"_a = 0);
83  // readFits taking Fits objects not wrapped, because Fits objects are not wrapped.
84 
85  cls.def("subset", (Catalog (Catalog::*)(ndarray::Array<bool const, 1> const &) const) & Catalog::subset);
86  cls.def("subset",
87  (Catalog (Catalog::*)(std::ptrdiff_t, std::ptrdiff_t, std::ptrdiff_t) const) & Catalog::subset);
88 
89  // The following three methods shadow those in the base class in C++ (unlike the base class versions,
90  // they do not require a ley argument because we assume it's the ID key). In Python, we make that appear
91  // as though the key argument is available but has a default value. If that key is not None, we delegate
92  // to the base class.
93  cls.def("isSorted",
94  [clsBase](py::object const &self, py::object key) -> py::object {
95  if (key == py::none()) {
96  key = self.attr("table").attr("getIdKey")();
97  }
98  return clsBase.attr("isSorted")(self, key);
99  },
100  "key"_a = py::none());
101  cls.def("sort",
102  [clsBase](py::object const &self, py::object key) -> py::object {
103  if (key == py::none()) {
104  key = self.attr("table").attr("getIdKey")();
105  }
106  return clsBase.attr("sort")(self, key);
107  },
108  "key"_a = py::none());
109  cls.def("find",
110  [clsBase](py::object const &self, py::object const &value, py::object key) -> py::object {
111  if (key == py::none()) {
112  key = self.attr("table").attr("getIdKey")();
113  }
114  return clsBase.attr("find")(self, value, key);
115  },
116  "value"_a, "key"_a = py::none());
117 
118  return cls;
119 };
120 }
121 }
122 }
123 } // lsst::afw::table::python
124 
125 #endif // !AFW_TABLE_PYBIND11_CATALOG_H_INCLUDED
table::Key< std::string > name
Definition: ApCorrMap.cc:71
A custom container class for records, based on std::vector.
Definition: Catalog.h:97
STL class.
A base class for image defects.
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:63
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:102
PySortedCatalog< Record > declareSortedCatalog(pybind11::module &mod, std::string const &name, bool isBase=false)
Wrap an instantiation of lsst::afw::table::SortedCatalogT<Record>.
Definition: sortedCatalog.h:53
pybind11::class_< SortedCatalogT< Record >, std::shared_ptr< SortedCatalogT< Record > >, CatalogT< Record > > PySortedCatalog
Definition: sortedCatalog.h:37
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18