LSST Applications  21.0.0+c4f5df5339,21.0.0+e70536a077,21.0.0-1-ga51b5d4+7c60f8a6ea,21.0.0-10-gcf60f90+74aa0801fd,21.0.0-12-g63909ac9+643a1044a5,21.0.0-15-gedb9d5423+1041c3824f,21.0.0-2-g103fe59+a356b2badb,21.0.0-2-g1367e85+6d3f3f41db,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g5242d73+6d3f3f41db,21.0.0-2-g7f82c8f+8d7c04eab9,21.0.0-2-g8f08a60+9c9a9cfcc8,21.0.0-2-ga326454+8d7c04eab9,21.0.0-2-gde069b7+bedfc5e1fb,21.0.0-2-gecfae73+6cb6558258,21.0.0-2-gfc62afb+6d3f3f41db,21.0.0-3-g21c7a62+f6e98b25aa,21.0.0-3-g357aad2+bd62456bea,21.0.0-3-g4be5c26+6d3f3f41db,21.0.0-3-g65f322c+03a4076c01,21.0.0-3-g7d9da8d+c4f5df5339,21.0.0-3-gaa929c8+c6b98066dc,21.0.0-3-gc44e71e+a26d5c1aea,21.0.0-3-ge02ed75+04b527a9d5,21.0.0-35-g64f566ff+b27e5ef93e,21.0.0-4-g591bb35+04b527a9d5,21.0.0-4-g88306b8+8773047b2e,21.0.0-4-gc004bbf+80a0b7acb7,21.0.0-4-gccdca77+a5c54364a0,21.0.0-4-ge8fba5a+ccfc328107,21.0.0-5-gdf36809+87b8d260e6,21.0.0-6-g00874e7+7eeda2b6ba,21.0.0-6-g2d4f3f3+e70536a077,21.0.0-6-g4e60332+04b527a9d5,21.0.0-6-g5ef7dad+f53629abd8,21.0.0-7-gc8ca178+b63e69433b,21.0.0-8-gfbe0b4b+c6b98066dc,w.2021.06
LSST Data Management Base Package
function.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008-2016 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
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 <memory>
23 #include <string>
24 
25 #include <pybind11/pybind11.h>
26 #include <pybind11/stl.h>
27 
28 #include "lsst/afw/table/io/python.h" // for addPersistableMethods
29 #include "lsst/afw/math/Function.h"
30 
31 namespace py = pybind11;
32 using namespace pybind11::literals;
33 
34 namespace lsst {
35 namespace afw {
36 namespace math {
37 
38 namespace {
39 
40 template <typename ReturnT>
41 void declareFunction(py::module &mod, std::string const &suffix) {
42  auto const name = "Function" + suffix;
43 
44  py::class_<Function<ReturnT>, std::shared_ptr<Function<ReturnT>>> cls(mod, name.c_str());
45 
46  cls.def(py::init<unsigned int>(), "nParams"_a);
47  cls.def(py::init<std::vector<double> const &>(), "params"_a);
48 
49  table::io::python::addPersistableMethods<Function<ReturnT>>(cls);
50 
51  cls.def("getNParameters", &Function<ReturnT>::getNParameters);
52  cls.def("getParameters", &Function<ReturnT>::getParameters, py::return_value_policy::copy);
53  cls.def("getParameter", &Function<ReturnT>::getParameter, "index"_a);
54  cls.def("isLinearCombination", &Function<ReturnT>::isLinearCombination);
55  cls.def("setParameter", &Function<ReturnT>::setParameter, "index"_a, "value"_a);
56  cls.def("setParameters", &Function<ReturnT>::setParameters);
57  cls.def("toString", &Function<ReturnT>::toString, "prefix"_a = "");
58 }
59 
60 template <typename ReturnT>
61 void declareFunction1(py::module &mod, const std::string &suffix) {
62  auto const name = "Function1" + suffix;
63 
64  py::class_<Function1<ReturnT>, std::shared_ptr<Function1<ReturnT>>, Function<ReturnT>> cls(mod,
65  name.c_str());
66 
67  table::io::python::addPersistableMethods<Function1<ReturnT>>(cls);
68 
69  cls.def("clone", &Function1<ReturnT>::clone);
70  cls.def("__call__", &Function1<ReturnT>::operator(), "x"_a);
71  cls.def("toString", &Function1<ReturnT>::toString, "prefix"_a = "");
72  cls.def("computeCache", &Function1<ReturnT>::computeCache, "n"_a);
73 }
74 
75 template <typename ReturnT>
76 void declareFunction2(py::module &mod, const std::string &suffix) {
77  auto const name = "Function2" + suffix;
78 
79  py::class_<Function2<ReturnT>, std::shared_ptr<Function2<ReturnT>>, Function<ReturnT>> cls(mod,
80  name.c_str());
81 
82  table::io::python::addPersistableMethods<Function2<ReturnT>>(cls);
83 
84  cls.def("clone", &Function2<ReturnT>::clone);
85  cls.def("__call__", &Function2<ReturnT>::operator(), "x"_a, "y"_a);
86  cls.def("toString", &Function2<ReturnT>::toString, "prefix"_a = "");
87  cls.def("getDFuncDParameters", &Function2<ReturnT>::getDFuncDParameters, "x"_a, "y"_a);
88 }
89 
90 template <typename ReturnT>
91 void declareBasePolynomialFunction2(py::module &mod, const std::string &suffix) {
92  auto const name = "BasePolynomialFunction2" + suffix;
93 
94  py::class_<BasePolynomialFunction2<ReturnT>, std::shared_ptr<BasePolynomialFunction2<ReturnT>>,
95  Function2<ReturnT>>
96  cls(mod, name.c_str());
97 
98  cls.def("getOrder", &BasePolynomialFunction2<ReturnT>::getOrder);
99  cls.def("isLinearCombination", &BasePolynomialFunction2<ReturnT>::isLinearCombination);
100  cls.def_static("nParametersFromOrder", &BasePolynomialFunction2<ReturnT>::nParametersFromOrder,
101  "order"_a);
102  cls.def_static("orderFromNParameters", &BasePolynomialFunction2<ReturnT>::orderFromNParameters,
103  "nParameters"_a);
104  cls.def("getDFuncDParameters", &BasePolynomialFunction2<ReturnT>::getDFuncDParameters, "x"_a, "y"_a);
105 }
106 
107 template <typename ReturnT>
108 void declareNullFunction1(py::module &mod, const std::string &suffix) {
109  auto const name = "NullFunction1" + suffix;
110 
111  py::class_<NullFunction1<ReturnT>, std::shared_ptr<NullFunction1<ReturnT>>, Function1<ReturnT>> cls(
112  mod, name.c_str());
113 
114  cls.def(py::init<>());
115 
116  cls.def("clone", &NullFunction1<ReturnT>::clone);
117 }
118 
119 template <typename ReturnT>
120 void declareNullFunction2(py::module &mod, const std::string &suffix) {
121  auto const name = "NullFunction2" + suffix;
122 
123  py::class_<NullFunction2<ReturnT>, std::shared_ptr<NullFunction2<ReturnT>>, Function2<ReturnT>> cls(
124  mod, name.c_str());
125 
126  cls.def(py::init<>());
127 
128  cls.def("clone", &NullFunction2<ReturnT>::clone);
129 }
130 
131 template <typename ReturnT>
132 void declareAllFunctions(py::module &mod, const std::string &suffix) {
133  declareFunction<ReturnT>(mod, suffix);
134  declareFunction1<ReturnT>(mod, suffix);
135  declareFunction2<ReturnT>(mod, suffix);
136  declareBasePolynomialFunction2<ReturnT>(mod, suffix);
137  declareNullFunction1<ReturnT>(mod, suffix);
138  declareNullFunction2<ReturnT>(mod, suffix);
139 };
140 
141 } // namespace <anonymous>
142 
143 PYBIND11_MODULE(function, mod) {
144  declareAllFunctions<float>(mod, "F");
145  declareAllFunctions<double>(mod, "D");
146 }
147 
148 } // namespace math
149 } // namespace afw
150 } // namespace lsst
PYBIND11_MODULE(function, mod)
Definition: function.cc:143
def init()
Definition: tests.py:59
A base class for image defects.