MUQ  0.4.3
KernelWrapper.cpp
Go to the documentation of this file.
1 #include "AllClassWrappers.h"
2 
5 
15 
16 #include <pybind11/pybind11.h>
17 #include <pybind11/stl.h>
18 #include <pybind11/eigen.h>
19 
20 #include <string>
21 
22 #include <functional>
23 #include <vector>
24 
26 using namespace muq::Approximation;
27 namespace py = pybind11;
28 
30 {
31  // KernelBase class
32  py::class_<KernelBase,
33  std::shared_ptr<KernelBase>> kernBase(m, "CovarianceKernelBase");
34  kernBase
35  //.def(py::init<unsigned, unsigned, unsigned>())
36  //.def(py::init<unsigned, std::vector<unsigned>, unsigned, unsigned>())
37  .def("GetSeperableComponents", &KernelBase::GetSeperableComponents)
38  .def("Evaluate", &KernelBase::Evaluate)
39  .def("BuildCovariance", (Eigen::MatrixXd (KernelBase::*)
40  (Eigen::MatrixXd const&) const)
42  .def("BuildCovariance", (Eigen::MatrixXd (KernelBase::*)
43  (Eigen::MatrixXd const&,
44  Eigen::MatrixXd const&) const)
46  .def("FillCovariance", (void (KernelBase::*)
47  (Eigen::MatrixXd const&,
48  Eigen::MatrixXd const&,
49  Eigen::Ref<Eigen::MatrixXd>) const)
51  .def("FillCovariance", (void (KernelBase::*)
52  (Eigen::MatrixXd const&,
53  Eigen::Ref<Eigen::MatrixXd>) const)
55  .def("FillDerivCovariance", &KernelBase::FillDerivCovariance)
56  .def("GetPosDerivative", &KernelBase::GetPosDerivative)
57  .def("GetParamBounds", &KernelBase::GetParamBounds)
58  .def("GetParams", &KernelBase::GetParams)
59  .def("SetParams", &KernelBase::SetParams)
60  .def("Clone", &KernelBase::Clone)
61  .def("GetStateSpace", &KernelBase::GetStateSpace)
62  .def_readonly("dimInds", &KernelBase::dimInds)
63  .def_readonly("inputDim", &KernelBase::inputDim)
64  .def_readonly("coDim", &KernelBase::coDim)
65  .def_readonly("numParams", &KernelBase::numParams)
66  .def("FillPosDerivBlock", &KernelBase::FillPosDerivBlock)
67  .def("FillBlock", &KernelBase::FillBlock)
68  .def("__mul__", [](std::shared_ptr<KernelBase> a, std::shared_ptr<KernelBase> b) {return a * b;}, py::is_operator())
69  .def("__add__", [](std::shared_ptr<KernelBase> a, std::shared_ptr<KernelBase> b) {return a + b;}, py::is_operator());
70 
71  // ConcatenateKernel class
72  py::class_<ConcatenateKernel, KernelBase, std::shared_ptr<ConcatenateKernel>>
73  concatKern(m, "ConcatenateKernel");
74  concatKern
75  .def(py::init<std::vector<std::shared_ptr<KernelBase>> const&>())
76  .def(py::init<std::shared_ptr<KernelBase> const&,
77  std::shared_ptr<KernelBase> const&>())
78  .def("Clone", &ConcatenateKernel::Clone)
79  .def("FillBlock", &ConcatenateKernel::FillBlock)
80  .def("FillPosDerivBlock", &ConcatenateKernel::FillPosDerivBlock);
81 
82  // KernelImpl<ConstantKernel> class
83  py::class_<KernelImpl<ConstantKernel>, KernelBase,
84  std::shared_ptr<KernelImpl<ConstantKernel>>>
85  kernImplConst(m, "ConstantKernelImpl");
86  kernImplConst
87  .def(py::init<unsigned, unsigned, unsigned>())
88  .def(py::init<unsigned, std::vector<unsigned>, unsigned, unsigned>())
89  .def("Clone", &KernelImpl<ConstantKernel>::Clone)
90  .def("FillBlock", &KernelImpl<ConstantKernel>::FillBlock)
91  .def("FillPosDerivBlock", &KernelImpl<ConstantKernel>::FillPosDerivBlock)
92  .def("FillPosDerivBlockImpl", &KernelImpl<ConstantKernel>::FillPosDerivBlockImpl);
93 
94  // ConstantKernel class
95  py::class_<ConstantKernel, KernelImpl<ConstantKernel>, std::shared_ptr<ConstantKernel>>
96  constKern(m, "ConstantKernel");
97  constKern
98  .def(py::init<unsigned, const double>())
99  .def(py::init<unsigned, const double, const Eigen::Vector2d>())
100  .def(py::init<unsigned, std::vector<unsigned>, const double>())
101  .def(py::init<unsigned, std::vector<unsigned>, const double,
102  const Eigen::Vector2d>())
103  .def(py::init<unsigned, Eigen::MatrixXd const&>())
104  .def(py::init<unsigned, Eigen::MatrixXd const&, const Eigen::Vector2d>())
105  .def(py::init<unsigned, std::vector<unsigned>, Eigen::MatrixXd const&>())
106  .def(py::init<unsigned, std::vector<unsigned>, Eigen::MatrixXd const&,
107  const Eigen::Vector2d>());
108 
109 
110  // LinearTransformKernel class
111  py::class_<LinearTransformKernel, KernelBase,
112  std::shared_ptr<LinearTransformKernel>>
113  linTransKern(m, "LinearTransformKernel");
114  linTransKern
115  .def(py::init<Eigen::MatrixXd const&, std::shared_ptr<KernelBase>>())
116  .def("FillBlock", &LinearTransformKernel::FillBlock)
117  .def("FillPosDerivBlock", &LinearTransformKernel::FillPosDerivBlock)
118  .def("Clone", &LinearTransformKernel::Clone);
119 
120  // KernelImpl<MaternKernel> class
121  py::class_<KernelImpl<MaternKernel>, KernelBase,
122  std::shared_ptr<KernelImpl<MaternKernel>>>
123  kernImplMat(m, "MaternKernelImpl");
124  kernImplMat
125  .def(py::init<unsigned, unsigned, unsigned>())
126  .def(py::init<unsigned, std::vector<unsigned>, unsigned, unsigned>())
127  .def("Clone", &KernelImpl<MaternKernel>::Clone)
128  .def("FillBlock", &KernelImpl<MaternKernel>::FillBlock)
129  .def("FillPosDerivBlock", &KernelImpl<MaternKernel>::FillPosDerivBlock)
130  .def("FillPosDerivBlockImpl", &KernelImpl<MaternKernel>::FillPosDerivBlockImpl);
131 
132  // MaternKernel class
133  py::class_<MaternKernel, KernelImpl<MaternKernel>,
134  std::shared_ptr<MaternKernel>>
135  matKern(m, "MaternKernel");
136  matKern
137  .def(py::init<unsigned, std::vector<unsigned>, double, double, double>())
138  .def(py::init<unsigned, std::vector<unsigned>, double, double, double,Eigen::Vector2d, Eigen::Vector2d>())
139  .def(py::init<unsigned, double, double, double>())
140  .def(py::init<unsigned, double, double, double, Eigen::Vector2d,Eigen::Vector2d>())
141  .def("GetStateSpace", &MaternKernel::GetStateSpace);
142 
143  // KernelImpl<PeriodicKernel> class
144  py::class_<KernelImpl<PeriodicKernel>, KernelBase,
145  std::shared_ptr<KernelImpl<PeriodicKernel>>>
146  kernImplPer(m, "PeriodicKernelImpl");
147  kernImplPer
148  .def(py::init<unsigned, unsigned, unsigned>())
149  .def(py::init<unsigned, std::vector<unsigned>, unsigned, unsigned>())
150  .def("Clone", &KernelImpl<PeriodicKernel>::Clone)
151  .def("FillBlock", &KernelImpl<PeriodicKernel>::FillBlock)
152  .def("FillPosDerivBlock", &KernelImpl<PeriodicKernel>::FillPosDerivBlock)
153  .def("FillPosDerivBlockImpl", &KernelImpl<PeriodicKernel>::FillPosDerivBlockImpl);
154 
155  // PeriodicKernel class
156  py::class_<PeriodicKernel, KernelImpl<PeriodicKernel>,
157  std::shared_ptr<PeriodicKernel>>
158  periodKern(m, "PeriodicKernel");
159  periodKern
160  .def(py::init<unsigned, std::vector<unsigned>, double, double, double>())
161  .def(py::init<unsigned, std::vector<unsigned>, double, double, double, Eigen::Vector2d, Eigen::Vector2d, Eigen::Vector2d>())
162  .def(py::init<unsigned, double, double, double>())
163  .def(py::init<unsigned, double, double, double,Eigen::Vector2d, Eigen::Vector2d, Eigen::Vector2d>())
164  //.def("FillBlockImpl", &PeriodicKernel::FillBlockImpl)
165  .def("GetStateSpace", &PeriodicKernel::GetStateSpace);
166 
167  // ProductKernel class
168  py::class_<ProductKernel, KernelBase, std::shared_ptr<ProductKernel>>
169  productKern(m, "ProductKernel");
170  productKern
171  .def(py::init<std::shared_ptr<KernelBase>, std::shared_ptr<KernelBase>>())
172  .def("FillBlock", &ProductKernel::FillBlock)
173  .def("FillPosDerivBlock", &ProductKernel::FillPosDerivBlock)
174  .def("Clone", &ProductKernel::Clone)
175  .def("GetSeperableComponents", &ProductKernel::GetSeperableComponents)
176  .def("GetStateSpace", &ProductKernel::GetStateSpace);
177 
178  // KernelImpl<SquaredExpKernel> class
179  py::class_<KernelImpl<SquaredExpKernel>, KernelBase,
180  std::shared_ptr<KernelImpl<SquaredExpKernel>>>
181  kernImplSE(m, "SquaredExpKernelImpl");
182  kernImplSE
183  .def(py::init<unsigned, unsigned, unsigned>())
184  .def(py::init<unsigned, std::vector<unsigned>, unsigned, unsigned>())
186  .def("FillBlock", &KernelImpl<SquaredExpKernel>::FillBlock)
187  .def("FillPosDerivBlock", &KernelImpl<SquaredExpKernel>::FillPosDerivBlock)
188  .def("FillPosDerivBlockImpl", &KernelImpl<SquaredExpKernel>::FillPosDerivBlockImpl);
189 
190  // SquaredExpKernel class
191  py::class_<SquaredExpKernel, KernelImpl<SquaredExpKernel>,
192  std::shared_ptr<SquaredExpKernel>>
193  sqExpKern(m, "SquaredExpKernel");
194  sqExpKern
195  .def(py::init<unsigned, std::vector<unsigned>, double, double>())
196  .def(py::init<unsigned, std::vector<unsigned>, double, double, Eigen::Vector2d, Eigen::Vector2d>())
197  .def(py::init<unsigned, double, double, Eigen::Vector2d, Eigen::Vector2d>())
198  .def(py::init<unsigned, double, double>());
199  //.def("FillBlockImpl", &SquaredExpKernel::FillBlockImpl);
200 
201  // SumKernel class
202  py::class_<SumKernel, KernelBase, std::shared_ptr<SumKernel>>
203  sumKern(m, "SumKernel");
204  sumKern
205  .def(py::init<std::shared_ptr<KernelBase>, std::shared_ptr<KernelBase>>())
206  .def("Clone", &SumKernel::Clone)
207  .def("FillBlock", &SumKernel::FillBlock)
208  .def("FillPosDerivBlock", &SumKernel::FillPosDerivBlock)
209  .def("GetStateSpace", &SumKernel::GetStateSpace);
210 
211  // KernelImpl<WhiteNoiseKernel> class
212  py::class_<KernelImpl<WhiteNoiseKernel>, KernelBase,
213  std::shared_ptr<KernelImpl<WhiteNoiseKernel>>>
214  kernImplWN(m, "WhiteNoiseKernelImpl");
215  kernImplWN
216  .def(py::init<unsigned, unsigned, unsigned>())
217  .def(py::init<unsigned, std::vector<unsigned>, unsigned, unsigned>())
219  .def("FillBlock", &KernelImpl<WhiteNoiseKernel>::FillBlock)
220  .def("FillPosDerivBlock", &KernelImpl<WhiteNoiseKernel>::FillPosDerivBlock)
221  .def("FillPosDerivBlockImpl", &KernelImpl<WhiteNoiseKernel>::FillPosDerivBlockImpl);
222 
223  // WhiteNoiseKernel class
224  py::class_<WhiteNoiseKernel, KernelImpl<WhiteNoiseKernel>,
225  std::shared_ptr<WhiteNoiseKernel>>
226  whiteNoiseKern(m, "WhiteNoiseKernel");
227  whiteNoiseKern
228  .def(py::init<unsigned, const double>())
229  .def(py::init<unsigned, const double, const Eigen::Vector2d >());
230  //.def("FillBlockImpl", &WhiteNoiseKernel::FillBlockImpl);
231 }
virtual std::shared_ptr< KernelBase > Clone() const override
virtual void FillBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, Eigen::Ref< Eigen::MatrixXd > block) const override
virtual void FillPosDerivBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, std::vector< int > const &wrts, Eigen::Ref< Eigen::MatrixXd > block) const override
Base class for all covariance kernels.
Definition: KernelBase.h:36
const unsigned int inputDim
Definition: KernelBase.h:132
virtual Eigen::MatrixXd BuildCovariance(Eigen::MatrixXd const &x) const
Definition: KernelBase.cpp:24
virtual std::tuple< std::shared_ptr< muq::Modeling::LinearSDE >, std::shared_ptr< muq::Modeling::LinearOperator >, Eigen::MatrixXd > GetStateSpace(boost::property_tree::ptree sdeOptions=boost::property_tree::ptree()) const
Returns a state space representation of the covariance kernel.
Definition: KernelBase.h:124
virtual Eigen::MatrixXd GetPosDerivative(Eigen::VectorXd const &x1, Eigen::VectorXd const &x2, std::vector< int > const &wrts) const
Returns derivatives of the kernel with respect to the first input, x1.
Definition: KernelBase.cpp:112
const unsigned int coDim
Definition: KernelBase.h:133
virtual void SetParams(Eigen::VectorXd const &params)
Definition: KernelBase.h:111
const unsigned int numParams
Definition: KernelBase.h:134
virtual void FillPosDerivBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, std::vector< int > const &wrts, Eigen::Ref< Eigen::MatrixXd > block) const =0
virtual Eigen::MatrixXd Evaluate(Eigen::VectorXd const &x1, Eigen::VectorXd const &x2) const
Definition: KernelBase.cpp:5
virtual Eigen::VectorXd GetParams() const
Definition: KernelBase.h:109
const std::vector< unsigned int > dimInds
Definition: KernelBase.h:126
virtual void FillDerivCovariance(Eigen::MatrixXd const &xs, Eigen::MatrixXd const &ys, std::vector< int > const &wrts, Eigen::Ref< Eigen::MatrixXd > cov) const
Definition: KernelBase.cpp:87
virtual Eigen::MatrixXd GetParamBounds() const
Definition: KernelBase.h:103
virtual std::vector< std::shared_ptr< KernelBase > > GetSeperableComponents()
Overridden by ProductKernel.
Definition: KernelBase.h:63
virtual void FillCovariance(Eigen::MatrixXd const &xs, Eigen::MatrixXd const &ys, Eigen::Ref< Eigen::MatrixXd > cov) const
Definition: KernelBase.cpp:64
virtual std::shared_ptr< KernelBase > Clone() const =0
virtual void FillBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, Eigen::Ref< Eigen::MatrixXd > block) const =0
Base class in CRTP pattern for covariance kernels.
Definition: KernelImpl.h:26
virtual void FillPosDerivBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, std::vector< int > const &wrts, Eigen::Ref< Eigen::MatrixXd > block) const override
virtual void FillBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, Eigen::Ref< Eigen::MatrixXd > block) const override
virtual std::shared_ptr< KernelBase > Clone() const override
virtual std::tuple< std::shared_ptr< muq::Modeling::LinearSDE >, std::shared_ptr< muq::Modeling::LinearOperator >, Eigen::MatrixXd > GetStateSpace(boost::property_tree::ptree sdeOptions=boost::property_tree::ptree()) const override
Returns a state space representation of the covariance kernel.
virtual std::vector< std::shared_ptr< KernelBase > > GetSeperableComponents() override
Overridden by ProductKernel.
virtual void FillBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, Eigen::Ref< Eigen::MatrixXd > block) const override
virtual std::tuple< std::shared_ptr< muq::Modeling::LinearSDE >, std::shared_ptr< muq::Modeling::LinearOperator >, Eigen::MatrixXd > GetStateSpace(boost::property_tree::ptree sdeOptions=boost::property_tree::ptree()) const override
Returns a state space representation of the covariance kernel.
virtual std::shared_ptr< KernelBase > Clone() const override
Definition: ProductKernel.h:89
virtual void FillPosDerivBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, std::vector< int > const &wrts, Eigen::Ref< Eigen::MatrixXd > block) const override
Definition: ProductKernel.h:46
virtual std::shared_ptr< KernelBase > Clone() const override
Definition: SumKernel.h:30
virtual void FillPosDerivBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, std::vector< int > const &wrts, Eigen::Ref< Eigen::MatrixXd > block) const override
Definition: SumKernel.cpp:40
virtual void FillBlock(Eigen::Ref< const Eigen::VectorXd > const &x1, Eigen::Ref< const Eigen::VectorXd > const &x2, Eigen::Ref< const Eigen::VectorXd > const &params, Eigen::Ref< Eigen::MatrixXd > block) const override
Definition: SumKernel.cpp:25
virtual std::tuple< std::shared_ptr< muq::Modeling::LinearSDE >, std::shared_ptr< muq::Modeling::LinearOperator >, Eigen::MatrixXd > GetStateSpace(boost::property_tree::ptree sdeOptions=boost::property_tree::ptree()) const override
Returns a state space representation of the covariance kernel.
Definition: SumKernel.cpp:54
void KernelWrapper(pybind11::module &m)