lsst.afw  13.0-91-g2a3e2ee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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/daf/base/Persistable.h"
29 #include "lsst/afw/table/io/python.h" // for declarePersistableFacade
30 #include "lsst/afw/math/Function.h"
31 
32 namespace py = pybind11;
33 using namespace pybind11::literals;
34 
35 namespace lsst {
36 namespace afw {
37 namespace math {
38 
39 namespace {
40 
41 template <typename ReturnT>
42 void declareFunction(py::module &mod, std::string const &suffix) {
43  auto const name = "Function" + suffix;
44 
45  table::io::python::declarePersistableFacade<Function<ReturnT>>(mod, name.c_str());
46 
47  py::class_<Function<ReturnT>, std::shared_ptr<Function<ReturnT>>,
48  table::io::PersistableFacade<Function<ReturnT>>, table::io::Persistable>
49  cls(mod, name.c_str());
50 
51  cls.def(py::init<unsigned int>(), "nParams"_a);
52  cls.def(py::init<std::vector<double> const &>(), "params"_a);
53 
54  cls.def("getNParameters", &Function<ReturnT>::getNParameters);
55  cls.def("getParameters", &Function<ReturnT>::getParameters, py::return_value_policy::copy);
56  cls.def("getParameter", &Function<ReturnT>::getParameter, "index"_a);
57  cls.def("isLinearCombination", &Function<ReturnT>::isLinearCombination);
58  cls.def("setParameter", &Function<ReturnT>::setParameter, "index"_a, "value"_a);
59  cls.def("setParameters", &Function<ReturnT>::setParameters);
60  cls.def("toString", &Function<ReturnT>::toString, "prefix"_a = "");
61 }
62 
63 template <typename ReturnT>
64 void declareFunction1(py::module &mod, const std::string &suffix) {
65  auto const name = "Function1" + suffix;
66 
67  table::io::python::declarePersistableFacade<Function1<ReturnT>>(mod, name.c_str());
68 
69  py::class_<Function1<ReturnT>, std::shared_ptr<Function1<ReturnT>>,
70  table::io::PersistableFacade<Function1<ReturnT>>, Function<ReturnT>>
71  cls(mod, name.c_str());
72 
73  cls.def("clone", &Function1<ReturnT>::clone);
74  cls.def("__call__", &Function1<ReturnT>::operator(), "x"_a);
75  cls.def("toString", &Function1<ReturnT>::toString, "prefix"_a = "");
76  cls.def("computeCache", &Function1<ReturnT>::computeCache, "n"_a);
77 }
78 
79 template <typename ReturnT>
80 void declareFunction2(py::module &mod, const std::string &suffix) {
81  auto const name = "Function2" + suffix;
82 
83  table::io::python::declarePersistableFacade<Function2<ReturnT>>(mod, name.c_str());
84 
85  py::class_<Function2<ReturnT>, std::shared_ptr<Function2<ReturnT>>,
86  table::io::PersistableFacade<Function2<ReturnT>>, Function<ReturnT>>
87  cls(mod, name.c_str());
88 
89  cls.def("clone", &Function2<ReturnT>::clone);
90  cls.def("__call__", &Function2<ReturnT>::operator(), "x"_a, "y"_a);
91  cls.def("toString", &Function2<ReturnT>::toString, "prefix"_a = "");
92  cls.def("getDFuncDParameters", &Function2<ReturnT>::getDFuncDParameters, "x"_a, "y"_a);
93 }
94 
95 template <typename ReturnT>
96 void declareBasePolynomialFunction2(py::module &mod, const std::string &suffix) {
97  auto const name = "BasePolynomialFunction2" + suffix;
98 
99  py::class_<BasePolynomialFunction2<ReturnT>, std::shared_ptr<BasePolynomialFunction2<ReturnT>>,
100  Function2<ReturnT>>
101  cls(mod, name.c_str());
102 
103  cls.def("getOrder", &BasePolynomialFunction2<ReturnT>::getOrder);
104  cls.def("isLinearCombination", &BasePolynomialFunction2<ReturnT>::isLinearCombination);
105  cls.def_static("nParametersFromOrder", &BasePolynomialFunction2<ReturnT>::nParametersFromOrder,
106  "order"_a);
107  cls.def_static("orderFromNParameters", &BasePolynomialFunction2<ReturnT>::orderFromNParameters,
108  "nParameters"_a);
109  cls.def("getDFuncDParameters", &BasePolynomialFunction2<ReturnT>::getDFuncDParameters, "x"_a, "y"_a);
110 }
111 
112 template <typename ReturnT>
113 void declareNullFunction1(py::module &mod, const std::string &suffix) {
114  auto const name = "NullFunction1" + suffix;
115 
116  py::class_<NullFunction1<ReturnT>, std::shared_ptr<NullFunction1<ReturnT>>, Function1<ReturnT>> cls(
117  mod, name.c_str());
118 
119  cls.def(py::init<>());
120 
121  cls.def("clone", &NullFunction1<ReturnT>::clone);
122 }
123 
124 template <typename ReturnT>
125 void declareNullFunction2(py::module &mod, const std::string &suffix) {
126  auto const name = "NullFunction2" + suffix;
127 
128  py::class_<NullFunction2<ReturnT>, std::shared_ptr<NullFunction2<ReturnT>>, Function2<ReturnT>> cls(
129  mod, name.c_str());
130 
131  cls.def(py::init<>());
132 
133  cls.def("clone", &NullFunction2<ReturnT>::clone);
134 }
135 
136 template <typename ReturnT>
137 void declareAllFunctions(py::module &mod, const std::string &suffix) {
138  declareFunction<ReturnT>(mod, suffix);
139  declareFunction1<ReturnT>(mod, suffix);
140  declareFunction2<ReturnT>(mod, suffix);
141  declareBasePolynomialFunction2<ReturnT>(mod, suffix);
142  declareNullFunction1<ReturnT>(mod, suffix);
143  declareNullFunction2<ReturnT>(mod, suffix);
144 };
145 
146 } // namespace <anonymous>
147 
148 PYBIND11_PLUGIN(_function) {
149  py::module mod("_function", "Python wrapper for afw _function library");
150 
151  declareAllFunctions<float>(mod, "F");
152  declareAllFunctions<double>(mod, "D");
153 
154  return mod.ptr();
155 }
156 
157 } // namespace math
158 } // namespace afw
159 } // namespace lsst
PYBIND11_PLUGIN(_cameraPoint)
Definition: cameraPoint.cc:35
table::Key< std::string > name
Definition: ApCorrMap.cc:71
double const _a
Definition: RandomImage.cc:77