lsst.afw  13.0-94-ga1c444047
gaussianProcess.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 
23 #include <pybind11/pybind11.h>
24 //#include <pybind11/operators.h>
25 #include <pybind11/stl.h>
26 
27 #include "numpy/arrayobject.h"
28 #include "ndarray/pybind11.h"
29 #include "ndarray/converter.h"
30 
32 
33 namespace py = pybind11;
34 
35 using namespace lsst::afw::math;
36 
37 template <typename T>
38 void declareKdTree(py::module &mod, const std::string &suffix) {
39  py::class_<KdTree<T>> clsKdTree(mod, ("KdTree" + suffix).c_str());
40  clsKdTree.def(py::init<>());
41  clsKdTree.def("Initialize", &KdTree<T>::Initialize);
42  clsKdTree.def("removePoint", &KdTree<T>::removePoint);
43  clsKdTree.def("getData", (T (KdTree<T>::*)(int, int) const) & KdTree<T>::getData);
44  clsKdTree.def("getData", (ndarray::Array<T, 1, 1> (KdTree<T>::*)(int) const) & KdTree<T>::getData);
45  clsKdTree.def("addPoint", &KdTree<T>::addPoint);
46  clsKdTree.def("getNPoints", &KdTree<T>::getNPoints);
47  clsKdTree.def("getTreeNode", &KdTree<T>::getTreeNode);
48  clsKdTree.def("findNeighbors", &KdTree<T>::findNeighbors);
49 };
50 
51 template <typename T>
52 void declareCovariograms(py::module &mod, const std::string &suffix) {
53  /* Covariogram */
54  py::class_<Covariogram<T>, std::shared_ptr<Covariogram<T>>> clsCovariogram(
55  mod, ("Covariogram" + suffix).c_str());
56  clsCovariogram.def(py::init<>());
57  clsCovariogram.def("__call__", &Covariogram<T>::operator());
58 
59  /* SquaredExpCovariogram */
60  py::class_<SquaredExpCovariogram<T>, std::shared_ptr<SquaredExpCovariogram<T>>, Covariogram<T>>
61  clsSquaredExpCovariogram(mod, ("SquaredExpCovariogram" + suffix).c_str());
62  clsSquaredExpCovariogram.def(py::init<>());
63  clsSquaredExpCovariogram.def("__call__", &SquaredExpCovariogram<T>::operator());
64  clsSquaredExpCovariogram.def("setEllSquared", &SquaredExpCovariogram<T>::setEllSquared);
65 
66  /* SquaredExpCovariogram */
67  py::class_<NeuralNetCovariogram<T>, std::shared_ptr<NeuralNetCovariogram<T>>, Covariogram<T>>
68  clsNeuralNetCovariogram(mod, ("NeuralNetCovariogram" + suffix).c_str());
69  clsNeuralNetCovariogram.def(py::init<>());
70  clsNeuralNetCovariogram.def("setSigma0", &NeuralNetCovariogram<T>::setSigma0);
71  clsNeuralNetCovariogram.def("setSigma1", &NeuralNetCovariogram<T>::setSigma1);
72 };
73 
74 template <typename T>
75 void declareGaussianProcess(py::module &mod, const std::string &suffix) {
76  py::class_<GaussianProcess<T>> clsGaussianProcess(mod, ("GaussianProcess" + suffix).c_str());
77  /* Constructors */
78  clsGaussianProcess.def(py::init<ndarray::Array<T, 2, 2> const &, ndarray::Array<T, 1, 1> const &,
79  std::shared_ptr<Covariogram<T>> const &>());
80  clsGaussianProcess.def(py::init<ndarray::Array<T, 2, 2> const &, ndarray::Array<T, 1, 1> const &,
81  ndarray::Array<T, 1, 1> const &, ndarray::Array<T, 1, 1> const &,
82  std::shared_ptr<Covariogram<T>> const &>());
83  clsGaussianProcess.def(py::init<ndarray::Array<T, 2, 2> const &, ndarray::Array<T, 2, 2> const &,
84  std::shared_ptr<Covariogram<T>> const &>());
85  clsGaussianProcess.def(py::init<ndarray::Array<T, 2, 2> const &, ndarray::Array<T, 1, 1> const &,
86  ndarray::Array<T, 1, 1> const &, ndarray::Array<T, 2, 2> const &,
87  std::shared_ptr<Covariogram<T>> const &>());
88  /* Members */
89  clsGaussianProcess.def(
90  "interpolate",
91  (T (GaussianProcess<T>::*)(ndarray::Array<T, 1, 1>, ndarray::Array<T, 1, 1> const &, int) const) &
93  clsGaussianProcess.def("interpolate",
94  (void (GaussianProcess<T>::*)(ndarray::Array<T, 1, 1>, ndarray::Array<T, 1, 1>,
95  ndarray::Array<T, 1, 1> const &, int) const) &
96  GaussianProcess<T>::interpolate);
97  clsGaussianProcess.def("selfInterpolate",
98  (T (GaussianProcess<T>::*)(ndarray::Array<T, 1, 1>, int, int) const) &
100  clsGaussianProcess.def(
101  "selfInterpolate",
102  (void (GaussianProcess<T>::*)(ndarray::Array<T, 1, 1>, ndarray::Array<T, 1, 1>, int, int) const) &
103  GaussianProcess<T>::selfInterpolate);
104  clsGaussianProcess.def("setLambda", &GaussianProcess<T>::setLambda);
105  clsGaussianProcess.def("setCovariogram", &GaussianProcess<T>::setCovariogram);
106  clsGaussianProcess.def("addPoint", (void (GaussianProcess<T>::*)(ndarray::Array<T, 1, 1> const &, T)) &
108  clsGaussianProcess.def("addPoint", (void (GaussianProcess<T>::*)(ndarray::Array<T, 1, 1> const &,
109  ndarray::Array<T, 1, 1> const &)) &
110  GaussianProcess<T>::addPoint);
111  clsGaussianProcess.def("batchInterpolate",
112  (void (GaussianProcess<T>::*)(ndarray::Array<T, 1, 1>, ndarray::Array<T, 1, 1>,
113  ndarray::Array<T, 2, 2> const &) const) &
115  clsGaussianProcess.def(
116  "batchInterpolate",
117  (void (GaussianProcess<T>::*)(ndarray::Array<T, 1, 1>, ndarray::Array<T, 2, 2> const &) const) &
118  GaussianProcess<T>::batchInterpolate);
119  clsGaussianProcess.def("batchInterpolate",
120  (void (GaussianProcess<T>::*)(ndarray::Array<T, 2, 2>, ndarray::Array<T, 2, 2>,
121  ndarray::Array<T, 2, 2> const &) const) &
122  GaussianProcess<T>::batchInterpolate);
123  clsGaussianProcess.def(
124  "batchInterpolate",
125  (void (GaussianProcess<T>::*)(ndarray::Array<T, 2, 2>, ndarray::Array<T, 2, 2> const &) const) &
126  GaussianProcess<T>::batchInterpolate);
127  clsGaussianProcess.def("setKrigingParameter", &GaussianProcess<T>::setKrigingParameter);
128  clsGaussianProcess.def("removePoint", &GaussianProcess<T>::removePoint);
129  clsGaussianProcess.def("getNPoints", &GaussianProcess<T>::getNPoints);
130  clsGaussianProcess.def("getData",
131  (void (GaussianProcess<T>::*)(ndarray::Array<T, 2, 2>, ndarray::Array<T, 1, 1>,
132  ndarray::Array<int, 1, 1>) const) &
134  clsGaussianProcess.def("getData",
135  (void (GaussianProcess<T>::*)(ndarray::Array<T, 2, 2>, ndarray::Array<T, 2, 2>,
136  ndarray::Array<int, 1, 1>) const) &
137  GaussianProcess<T>::getData);
138 };
139 
141  py::module mod("_gaussianProcess", "Python wrapper for afw _gaussianProcess library");
142 
143  if (_import_array() < 0) {
144  PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import");
145  return nullptr;
146  };
147 
148  declareCovariograms<double>(mod, "D");
149  declareGaussianProcess<double>(mod, "D");
150  declareKdTree<double>(mod, "D");
151 
152  /* Module level */
153 
154  /* Member types and enums */
155 
156  /* Constructors */
157 
158  /* Operators */
159 
160  /* Members */
161 
162  return mod.ptr();
163 }
Stores values of a function sampled on an image and allows you to interpolate the function to unsampl...
The parent class of covariogram functions for use in Gaussian Process interpolation.
void declareKdTree(py::module &mod, const std::string &suffix)
The data for GaussianProcess is stored in a KD tree to facilitate nearest-neighbor searches...
a Covariogram that recreates a neural network with one hidden layer and infinite units in that layer ...
void declareCovariograms(py::module &mod, const std::string &suffix)
void declareGaussianProcess(py::module &mod, const std::string &suffix)
PYBIND11_PLUGIN(_approximate)
Definition: approximate.cc:56