lsst.shapelet  13.0-4-g5a043c4+6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
matrixBuilder.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  *
4  * This product includes software developed by the
5  * LSST Project (http://www.lsst.org/).
6  * See the COPYRIGHT file
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 #include "pybind11/pybind11.h"
23 
24 #include "numpy/arrayobject.h"
25 #include "ndarray/pybind11.h"
26 
29 
30 namespace py = pybind11;
31 using namespace pybind11::literals;
32 
33 namespace lsst {
34 namespace shapelet {
35 
36 namespace {
37 
38 template <typename T>
39 py::class_<MatrixBuilder<T>, std::shared_ptr<MatrixBuilder<T>>> declareMatrixBuilder(
40  py::module &mod, std::string const &suffix) {
41  using Class = MatrixBuilder<T>;
42 
43  py::class_<Class, std::shared_ptr<Class>> cls(mod, ("MatrixBuilder" + suffix).c_str());
44 
45  cls.def(py::init<ndarray::Array<T const, 1, 1> const &, ndarray::Array<T const, 1, 1> const &, int>(),
46  "x"_a, "y"_a, "order"_a);
47  cls.def(py::init<ndarray::Array<T const, 1, 1> const &, ndarray::Array<T const, 1, 1> const &, int,
48  ShapeletFunction const &>(),
49  "x"_a, "y"_a, "order"_a, "psf"_a);
50  cls.def(py::init<ndarray::Array<T const, 1, 1> const &, ndarray::Array<T const, 1, 1> const &,
51  MultiShapeletBasis const &>(),
52  "x"_a, "y"_a, "basis"_a);
53  cls.def(py::init<ndarray::Array<T const, 1, 1> const &, ndarray::Array<T const, 1, 1> const &,
54  MultiShapeletBasis const &, MultiShapeletFunction const &>(),
55  "x"_a, "y"_a, "basis"_a, "psf"_a);
56 
57  cls.def("getDataSize", &Class::getDataSize);
58  cls.def("getBasisSize", &Class::getBasisSize);
59  cls.def("allocateOutput", &Class::allocateOutput);
60 
61  cls.def("__call__",
62  (void (Class::*)(ndarray::Array<T, 2, -1> const &, afw::geom::ellipses::Ellipse const &) const) &
63  Class::operator());
64  cls.def("__call__", (ndarray::Array<T, 2, -2> (Class::*)(afw::geom::ellipses::Ellipse const &) const) &
65  Class::operator());
66 
67  return cls;
68 }
69 
70 template <typename T>
71 py::class_<MatrixBuilderWorkspace<T>, std::shared_ptr<MatrixBuilderWorkspace<T>>>
72 declareMatrixBuilderWorkspace(py::module &mod, std::string const &suffix) {
73  using Class = MatrixBuilderWorkspace<T>;
74 
75  py::class_<Class, std::shared_ptr<Class>> cls(mod, ("MatrixBuilderWorkspace" + suffix).c_str());
76 
77  cls.def(py::init<int>(), "size"_a);
78  cls.def(py::init<Class const &>(), "other"_a);
79 
80  cls.def("getRemaining", &Class::getRemaining);
81 
82  return cls;
83 }
84 
85 template <typename T>
86 py::class_<MatrixBuilderFactory<T>, std::shared_ptr<MatrixBuilderFactory<T>>> declareMatrixBuilderFactory(
87  py::module &mod, std::string const &suffix) {
88  using Class = MatrixBuilderFactory<T>;
89 
90  py::class_<Class, std::shared_ptr<Class>> cls(mod, ("MatrixBuilderFactory" + suffix).c_str());
91 
92  cls.def(py::init<ndarray::Array<T const, 1, 1> const &, ndarray::Array<T const, 1, 1> const &, int>(),
93  "x"_a, "y"_a, "order"_a);
94  cls.def(py::init<ndarray::Array<T const, 1, 1> const &, ndarray::Array<T const, 1, 1> const &, int,
95  ShapeletFunction const &>(),
96  "x"_a, "y"_a, "order"_a, "psf"_a);
97  cls.def(py::init<ndarray::Array<T const, 1, 1> const &, ndarray::Array<T const, 1, 1> const &,
98  MultiShapeletBasis const &>(),
99  "x"_a, "y"_a, "basis"_a);
100  cls.def(py::init<ndarray::Array<T const, 1, 1> const &, ndarray::Array<T const, 1, 1> const &,
101  MultiShapeletBasis const &, MultiShapeletFunction const &>(),
102  "x"_a, "y"_a, "basis"_a, "psf"_a);
103 
104  cls.def("__call__", (MatrixBuilder<T> (Class::*)() const) & Class::operator());
105  cls.def("__call__", (MatrixBuilder<T> (Class::*)(typename Class::Workspace &) const) & Class::operator(),
106  "workspace"_a);
107 
108  cls.def("getDataSize", &Class::getDataSize);
109  cls.def("getBasisSize", &Class::getBasisSize);
110  cls.def("computeWorkspace", &Class::computeWorkspace);
111 
112  return cls;
113 }
114 
115 template <typename T>
116 void declareMatrixBuilderTemplates(py::module &mod, std::string const &suffix) {
117  auto clsMatrixBuilder = declareMatrixBuilder<T>(mod, suffix);
118  auto clsMatrixBuilderWorkspace = declareMatrixBuilderWorkspace<T>(mod, suffix);
119  auto clsMatrixBuilderFactory = declareMatrixBuilderFactory<T>(mod, suffix);
120 
121  clsMatrixBuilder.attr("Workspace") = clsMatrixBuilderWorkspace;
122  clsMatrixBuilder.attr("Factory") = clsMatrixBuilderFactory;
123 
124  clsMatrixBuilderFactory.attr("Workspace") = clsMatrixBuilderWorkspace;
125  clsMatrixBuilderFactory.attr("Builder") = clsMatrixBuilder;
126 }
127 
128 } // <anonymous>
129 
130 PYBIND11_PLUGIN(matrixBuilder) {
131  py::module::import("lsst.afw.geom");
132  py::module mod("matrixBuilder");
133 
134  if (_import_array() < 0) {
135  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
136  return nullptr;
137  }
138 
139  declareMatrixBuilderTemplates<float>(mod, "F");
140  declareMatrixBuilderTemplates<double>(mod, "D");
141 
142  return mod.ptr();
143 }
144 
145 } // shapelet
146 } // lsst
PYBIND11_PLUGIN(basisEvaluator)