• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libKriging / libKriging / 20281616335

16 Dec 2025 08:23PM UTC coverage: 65.507% (-2.2%) from 67.701%
20281616335

push

github

web-flow
Merge pull request #309 from libKriging/up-bindings

Add new features and comprehensive tests for covMat, model, and Optim class

29 of 456 new or added lines in 10 files covered. (6.36%)

5 existing lines in 2 files now uncovered.

8432 of 12872 relevant lines covered (65.51%)

51481.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.9
/bindings/Python/src/_pylibkriging/Kriging_binding.cpp
1
#include "Kriging_binding.hpp"
2

3
#include "libKriging/utils/lk_armadillo.hpp"
4

5
#include <carma>
6

7
#include <libKriging/Kriging.hpp>
8
#include <libKriging/Trend.hpp>
9
#include "py_to_cpp_cast.hpp"
10

11
#include <random>
12

13
PyKriging::PyKriging(const std::string& kernel) : m_internal{new Kriging{kernel}} {}
×
14

15
PyKriging::PyKriging(const py::array_t<double>& y,
×
16
                     const py::array_t<double>& X,
17
                     const std::string& covType,
18
                     const std::string& regmodel,
19
                     bool normalize,
20
                     const std::string& optim,
21
                     const std::string& objective,
22
                     const Kriging::Parameters& parameters) {
×
23
  arma::colvec mat_y = carma::arr_to_col_view<double>(y);
×
24
  arma::mat mat_X = carma::arr_to_mat_view<double>(X);
×
25
  m_internal = std::make_unique<Kriging>(
×
26
      mat_y, mat_X, covType, Trend::fromString(regmodel), normalize, optim, objective, parameters);
×
27
}
×
28

29
PyKriging::PyKriging(const py::array_t<double>& y,
32✔
30
                     const py::array_t<double>& X,
31
                     const std::string& covType,
32
                     const std::string& regmodel,
33
                     bool normalize,
34
                     const std::string& optim,
35
                     const std::string& objective,
36
                     const py::dict& dict) {
32✔
37
  arma::colvec mat_y = carma::arr_to_col_view<double>(y);
32✔
38
  arma::mat mat_X = carma::arr_to_mat_view<double>(X);
32✔
39
  Kriging::Parameters parameters{get_entry<double>(dict, "sigma2"),
32✔
40
                                 get_entry<bool>(dict, "is_sigma2_estim").value_or(true),
32✔
41
                                 get_entry<arma::mat>(dict, "theta"),
32✔
42
                                 get_entry<bool>(dict, "is_theta_estim").value_or(true),
32✔
43
                                 get_entry<arma::colvec>(dict, "beta"),
32✔
44
                                 get_entry<bool>(dict, "is_beta_estim").value_or(true)};
64✔
45
  m_internal = std::make_unique<Kriging>(
32✔
46
      mat_y, mat_X, covType, Trend::fromString(regmodel), normalize, optim, objective, parameters);
32✔
47
}
32✔
48

49
PyKriging::~PyKriging() {}
56✔
50

51
PyKriging::PyKriging(const PyKriging& other)
14✔
52
    : m_internal{std::make_unique<Kriging>(*other.m_internal, ExplicitCopySpecifier{})} {}
14✔
53

54
PyKriging PyKriging::copy() const {
2✔
55
  return PyKriging(*this);
2✔
56
}
57

58
void PyKriging::fit(const py::array_t<double>& y,
×
59
                    const py::array_t<double>& X,
60
                    const std::string& regmodel,
61
                    bool normalize,
62
                    const std::string& optim,
63
                    const std::string& objective,
64
                    const py::dict& dict) {
65
  arma::mat mat_y = carma::arr_to_col_view<double>(y);
×
66
  arma::mat mat_X = carma::arr_to_mat_view<double>(X);
×
67
  Kriging::Parameters parameters{get_entry<double>(dict, "sigma2"),
×
68
                                 get_entry<bool>(dict, "is_sigma2_estim").value_or(true),
×
69
                                 get_entry<arma::mat>(dict, "theta"),
×
70
                                 get_entry<bool>(dict, "is_theta_estim").value_or(true),
×
71
                                 get_entry<arma::colvec>(dict, "beta"),
×
72
                                 get_entry<bool>(dict, "is_beta_estim").value_or(true)};
×
73
  m_internal->fit(mat_y, mat_X, Trend::fromString(regmodel), normalize, optim, objective, parameters);
×
74
}
×
75

76
std::tuple<py::array_t<double>, py::array_t<double>, py::array_t<double>, py::array_t<double>, py::array_t<double>>
77
PyKriging::predict(const py::array_t<double>& X_n, bool return_stdev, bool return_cov, bool return_deriv) {
13✔
78
  arma::mat mat_X = carma::arr_to_mat_view<double>(X_n);
13✔
79
  auto [y_predict, y_stderr, y_cov, y_mean_deriv, y_stderr_deriv]
13✔
80
      = m_internal->predict(mat_X, return_stdev, return_cov, return_deriv);
13✔
81
  return std::make_tuple(carma::col_to_arr(y_predict, true),
26✔
82
                         carma::col_to_arr(y_stderr, true),
26✔
83
                         carma::mat_to_arr(y_cov, true),
26✔
84
                         carma::mat_to_arr(y_mean_deriv, true),
26✔
85
                         carma::mat_to_arr(y_stderr_deriv, true));
52✔
86
}
13✔
87

88
py::array_t<double> PyKriging::simulate(const int nsim,
1✔
89
                                        const int seed,
90
                                        const py::array_t<double>& X_n,
91
                                        const bool will_update) {
92
  arma::mat mat_X = carma::arr_to_mat_view<double>(X_n);
1✔
93
  auto result = m_internal->simulate(nsim, seed, mat_X, will_update);
1✔
94
  return carma::mat_to_arr(result, true);
2✔
95
}
1✔
96

97
void PyKriging::update(const py::array_t<double>& y_u, const py::array_t<double>& X_u, const bool refit) {
2✔
98
  arma::mat mat_y = carma::arr_to_col<double>(y_u);
2✔
99
  arma::mat mat_X = carma::arr_to_mat<double>(X_u);
2✔
100
  m_internal->update(mat_y, mat_X, refit);
2✔
101
}
2✔
102

103
void PyKriging::update_simulate(const py::array_t<double>& y_u, const py::array_t<double>& X_u) {
×
104
  arma::mat mat_y = carma::arr_to_col<double>(y_u);
×
105
  arma::mat mat_X = carma::arr_to_mat<double>(X_u);
×
106
  m_internal->update_simulate(mat_y, mat_X);
×
107
}
×
108

109
std::string PyKriging::summary() const {
7✔
110
  return m_internal->summary();
7✔
111
}
112

113
void PyKriging::save(const std::string filename) const {
10✔
114
  return m_internal->save(filename);
10✔
115
}
116

117
PyKriging PyKriging::load(const std::string filename) {
10✔
118
  return PyKriging(std::make_unique<Kriging>(Kriging::load(filename)));
10✔
119
}
120

121
std::tuple<double, py::array_t<double>> PyKriging::leaveOneOutFun(const py::array_t<double>& theta,
1✔
122
                                                                  const bool return_grad) {
123
  arma::vec vec_theta = carma::arr_to_col<double>(theta);
1✔
124
  auto [llo, grad] = m_internal->leaveOneOutFun(vec_theta, return_grad, false);
1✔
125
  return {llo, carma::col_to_arr(grad)};
2✔
126
}
1✔
127

128
std::tuple<py::array_t<double>, py::array_t<double>> PyKriging::leaveOneOutVec(const py::array_t<double>& theta) {
×
129
  arma::vec vec_theta = carma::arr_to_col<double>(theta);
×
130
  auto [yhat_mean, yhat_sd] = m_internal->leaveOneOutVec(vec_theta);
×
131
  return {carma::col_to_arr(yhat_mean), carma::col_to_arr(yhat_sd)};
×
132
}
×
133

134
double PyKriging::leaveOneOut() {
×
135
  return m_internal->leaveOneOut();
×
136
}
137

138
std::tuple<double, py::array_t<double>, py::array_t<double>>
139
PyKriging::logLikelihoodFun(const py::array_t<double>& theta, const bool return_grad, const bool want_hess) {
21✔
140
  arma::vec vec_theta = carma::arr_to_col<double>(theta);
21✔
141
  auto [llo, grad, hess] = m_internal->logLikelihoodFun(vec_theta, return_grad, want_hess, false);
21✔
142
  return {
143
      llo,
144
      carma::col_to_arr(grad),
21✔
145
      // carma::mat_to_arr(hess)  // FIXME error in hessian transmission
146
      {}  //
147
  };
42✔
148
}
21✔
149

150
double PyKriging::logLikelihood() {
×
151
  return m_internal->logLikelihood();
×
152
}
153

154
std::tuple<double, py::array_t<double>> PyKriging::logMargPostFun(const py::array_t<double>& theta,
×
155
                                                                  const bool return_grad) {
156
  arma::vec vec_theta = carma::arr_to_col<double>(theta);
×
157
  auto [lmp, grad] = m_internal->logMargPostFun(vec_theta, return_grad, false);
×
158
  return {lmp, carma::col_to_arr(grad)};
×
159
}
×
160

161
double PyKriging::logMargPost() {
×
162
  return m_internal->logMargPost();
×
163
}
164

165
std::string PyKriging::kernel() {
×
166
  return m_internal->kernel();
×
167
}
168

169
std::string PyKriging::optim() {
×
170
  return m_internal->optim();
×
171
}
172

173
std::string PyKriging::objective() {
×
174
  return m_internal->objective();
×
175
}
176

177
py::array_t<double> PyKriging::X() {
×
178
  return carma::mat_to_arr(m_internal->X());
×
179
}
180

181
py::array_t<double> PyKriging::centerX() {
×
182
  return carma::row_to_arr(m_internal->centerX());
×
183
}
184

185
py::array_t<double> PyKriging::scaleX() {
×
186
  return carma::row_to_arr(m_internal->scaleX());
×
187
}
188

189
py::array_t<double> PyKriging::y() {
×
190
  return carma::col_to_arr(m_internal->y());
×
191
}
192

193
double PyKriging::centerY() {
×
194
  return m_internal->centerY();
×
195
}
196

197
double PyKriging::scaleY() {
×
198
  return m_internal->scaleY();
×
199
}
200

201
bool PyKriging::normalize() {
×
202
  return m_internal->normalize();
×
203
}
204

205
std::string PyKriging::regmodel() {
×
206
  return Trend::toString(m_internal->regmodel());
×
207
}
208

209
py::array_t<double> PyKriging::F() {
×
210
  return carma::mat_to_arr(m_internal->F());
×
211
}
212

213
py::array_t<double> PyKriging::T() {
×
214
  return carma::mat_to_arr(m_internal->T());
×
215
}
216

217
py::array_t<double> PyKriging::M() {
×
218
  return carma::mat_to_arr(m_internal->M());
×
219
}
220

221
py::array_t<double> PyKriging::z() {
×
222
  return carma::col_to_arr(m_internal->z());
×
223
}
224

225
py::array_t<double> PyKriging::beta() {
×
226
  return carma::col_to_arr(m_internal->beta());
×
227
}
228

229
bool PyKriging::is_beta_estim() {
×
230
  return m_internal->is_beta_estim();
×
231
}
232

233
py::array_t<double> PyKriging::theta() {
×
234
  return carma::col_to_arr(m_internal->theta());
×
235
}
236

237
bool PyKriging::is_theta_estim() {
×
238
  return m_internal->is_theta_estim();
×
239
}
240

241
double PyKriging::sigma2() {
×
242
  return m_internal->sigma2();
×
243
}
244

245
bool PyKriging::is_sigma2_estim() {
×
246
  return m_internal->is_sigma2_estim();
×
247
}
248

NEW
249
py::array_t<double> PyKriging::covMat(const py::array_t<double>& X1, const py::array_t<double>& X2) {
×
NEW
250
  arma::mat mat_X1 = carma::arr_to_mat<double>(X1);
×
NEW
251
  arma::mat mat_X2 = carma::arr_to_mat<double>(X2);
×
NEW
252
  arma::mat result = m_internal->covMat(mat_X1, mat_X2);
×
NEW
253
  return carma::mat_to_arr(result, true);
×
NEW
254
}
×
255

NEW
256
py::dict PyKriging::model() const {
×
NEW
257
  py::dict d;
×
NEW
258
  d["kernel"] = m_internal->kernel();
×
NEW
259
  d["optim"] = m_internal->optim();
×
NEW
260
  d["objective"] = m_internal->objective();
×
261

NEW
262
  arma::vec theta = m_internal->theta();
×
NEW
263
  d["theta"] = carma::col_to_arr(theta);
×
NEW
264
  d["is_theta_estim"] = m_internal->is_theta_estim();
×
NEW
265
  d["sigma2"] = m_internal->sigma2();
×
NEW
266
  d["is_sigma2_estim"] = m_internal->is_sigma2_estim();
×
267

NEW
268
  arma::mat X = m_internal->X();
×
NEW
269
  d["X"] = carma::mat_to_arr(X);
×
NEW
270
  arma::rowvec centerX = m_internal->centerX();
×
NEW
271
  d["centerX"] = carma::row_to_arr(centerX);
×
NEW
272
  arma::rowvec scaleX = m_internal->scaleX();
×
NEW
273
  d["scaleX"] = carma::row_to_arr(scaleX);
×
NEW
274
  arma::vec y = m_internal->y();
×
NEW
275
  d["y"] = carma::col_to_arr(y);
×
NEW
276
  d["centerY"] = m_internal->centerY();
×
NEW
277
  d["scaleY"] = m_internal->scaleY();
×
NEW
278
  d["normalize"] = m_internal->normalize();
×
NEW
279
  d["regmodel"] = Trend::toString(m_internal->regmodel());
×
280

NEW
281
  arma::vec beta = m_internal->beta();
×
NEW
282
  d["beta"] = carma::col_to_arr(beta);
×
NEW
283
  d["is_beta_estim"] = m_internal->is_beta_estim();
×
NEW
284
  arma::mat F = m_internal->F();
×
NEW
285
  d["F"] = carma::mat_to_arr(F);
×
NEW
286
  arma::mat T = m_internal->T();
×
NEW
287
  d["T"] = carma::mat_to_arr(T);
×
NEW
288
  arma::mat M = m_internal->M();
×
NEW
289
  d["M"] = carma::mat_to_arr(M);
×
NEW
290
  arma::vec z = m_internal->z();
×
NEW
291
  d["z"] = carma::col_to_arr(z);
×
NEW
292
  return d;
×
UNCOV
293
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc