• 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

0.0
/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp
1
#include "NoiseKriging_binding.hpp"
2

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

5
#include <carma>
6

7
#include <libKriging/NoiseKriging.hpp>
8
#include <libKriging/Trend.hpp>
9

10
#include <random>
11
#include "py_to_cpp_cast.hpp"
12

13
PyNoiseKriging::PyNoiseKriging(const std::string& kernel) : m_internal{new NoiseKriging{kernel}} {}
×
14

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

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

53
PyNoiseKriging::~PyNoiseKriging() {}
×
54

55
PyNoiseKriging::PyNoiseKriging(const PyNoiseKriging& other)
×
56
    : m_internal{std::make_unique<NoiseKriging>(*other.m_internal, ExplicitCopySpecifier{})} {}
×
57

58
PyNoiseKriging PyNoiseKriging::copy() const {
×
59
  return PyNoiseKriging(*this);
×
60
}
61

62
void PyNoiseKriging::fit(const py::array_t<double>& y,
×
63
                         const py::array_t<double>& noise,
64
                         const py::array_t<double>& X,
65
                         const std::string& regmodel,
66
                         bool normalize,
67
                         const std::string& optim,
68
                         const std::string& objective,
69
                         const py::dict& dict) {
70
  arma::mat mat_y = carma::arr_to_col_view<double>(y);
×
71
  arma::mat mat_noise = carma::arr_to_col_view<double>(noise);
×
72
  arma::mat mat_X = carma::arr_to_mat_view<double>(X);
×
73
  NoiseKriging::Parameters parameters{get_entry<arma::vec>(dict, "sigma2"),
×
74
                                      get_entry<bool>(dict, "is_sigma2_estim").value_or(true),
×
75
                                      get_entry<arma::mat>(dict, "theta"),
×
76
                                      get_entry<bool>(dict, "is_theta_estim").value_or(true),
×
77
                                      get_entry<arma::colvec>(dict, "beta"),
×
78
                                      get_entry<bool>(dict, "is_beta_estim").value_or(true)};
×
79
  m_internal->fit(mat_y, mat_noise, mat_X, Trend::fromString(regmodel), normalize, optim, objective, parameters);
×
80
}
×
81

82
std::tuple<py::array_t<double>, py::array_t<double>, py::array_t<double>, py::array_t<double>, py::array_t<double>>
83
PyNoiseKriging::predict(const py::array_t<double>& X_n, bool return_stdev, bool return_cov, bool return_deriv) {
×
84
  arma::mat mat_X = carma::arr_to_mat_view<double>(X_n);
×
85
  auto [y_predict, y_stderr, y_cov, y_mean_deriv, y_stderr_deriv]
×
86
      = m_internal->predict(mat_X, return_stdev, return_cov, return_deriv);
×
87
  return std::make_tuple(carma::col_to_arr(y_predict, true),
×
88
                         carma::col_to_arr(y_stderr, true),
×
89
                         carma::mat_to_arr(y_cov, true),
×
90
                         carma::mat_to_arr(y_mean_deriv, true),
×
91
                         carma::mat_to_arr(y_stderr_deriv, true));
×
92
}
×
93

94
py::array_t<double> PyNoiseKriging::simulate(const int nsim,
×
95
                                             const int seed,
96
                                             const py::array_t<double>& X_n,
97
                                             const py::array_t<double>& with_noise,
98
                                             const bool will_update) {
99
  arma::mat mat_X = carma::arr_to_mat_view<double>(X_n);
×
100
  arma::mat mat_noise = carma::arr_to_col_view<double>(with_noise);
×
101
  auto result = m_internal->simulate(nsim, seed, mat_X, mat_noise, will_update);
×
102
  return carma::mat_to_arr(result, true);
×
103
}
×
104

105
void PyNoiseKriging::update(const py::array_t<double>& y_u,
×
106
                            const py::array_t<double>& noise_u,
107
                            const py::array_t<double>& X_u,
108
                            const bool refit) {
109
  arma::mat mat_y = carma::arr_to_col<double>(y_u);
×
110
  arma::mat mat_noise = carma::arr_to_col<double>(noise_u);
×
111
  arma::mat mat_X = carma::arr_to_mat<double>(X_u);
×
112
  m_internal->update(mat_y, mat_noise, mat_X, refit);
×
113
}
×
114

115
void PyNoiseKriging::update_simulate(const py::array_t<double>& y_u,
×
116
                                     const py::array_t<double>& noise_u,
117
                                     const py::array_t<double>& X_u) {
118
  arma::mat mat_y = carma::arr_to_col<double>(y_u);
×
119
  arma::mat mat_noise = carma::arr_to_col<double>(noise_u);
×
120
  arma::mat mat_X = carma::arr_to_mat<double>(X_u);
×
121
  m_internal->update_simulate(mat_y, mat_noise, mat_X);
×
122
}
×
123

124
std::string PyNoiseKriging::summary() const {
×
125
  return m_internal->summary();
×
126
}
127

128
void PyNoiseKriging::save(const std::string filename) const {
×
129
  return m_internal->save(filename);
×
130
}
131

132
PyNoiseKriging PyNoiseKriging::load(const std::string filename) {
×
133
  return PyNoiseKriging(std::make_unique<NoiseKriging>(NoiseKriging::load(filename)));
×
134
}
135

136
std::tuple<double, py::array_t<double>> PyNoiseKriging::logLikelihoodFun(const py::array_t<double>& theta_sigma2,
×
137
                                                                         const bool return_grad) {
138
  arma::vec vec_theta_sigma2 = carma::arr_to_col<double>(theta_sigma2);
×
139
  auto [llo, grad] = m_internal->logLikelihoodFun(vec_theta_sigma2, return_grad, false);
×
140
  return {llo, carma::col_to_arr(grad)};
×
141
}
×
142

143
double PyNoiseKriging::logLikelihood() {
×
144
  return m_internal->logLikelihood();
×
145
}
146

NEW
147
py::array_t<double> PyNoiseKriging::covMat(const py::array_t<double>& X1, const py::array_t<double>& X2) {
×
NEW
148
  arma::mat mat_X1 = carma::arr_to_mat<double>(X1);
×
NEW
149
  arma::mat mat_X2 = carma::arr_to_mat<double>(X2);
×
NEW
150
  arma::mat result = m_internal->covMat(mat_X1, mat_X2);
×
NEW
151
  return carma::mat_to_arr(result, true);
×
NEW
152
}
×
153

NEW
154
py::dict PyNoiseKriging::model() const {
×
NEW
155
  py::dict d;
×
NEW
156
  d["kernel"] = m_internal->kernel();
×
NEW
157
  d["optim"] = m_internal->optim();
×
NEW
158
  d["objective"] = m_internal->objective();
×
159

NEW
160
  arma::vec theta = m_internal->theta();
×
NEW
161
  d["theta"] = carma::col_to_arr(theta);
×
NEW
162
  d["is_theta_estim"] = m_internal->is_theta_estim();
×
NEW
163
  d["sigma2"] = m_internal->sigma2();
×
NEW
164
  d["is_sigma2_estim"] = m_internal->is_sigma2_estim();
×
165

NEW
166
  arma::mat X = m_internal->X();
×
NEW
167
  d["X"] = carma::mat_to_arr(X);
×
NEW
168
  arma::rowvec centerX = m_internal->centerX();
×
NEW
169
  d["centerX"] = carma::row_to_arr(centerX);
×
NEW
170
  arma::rowvec scaleX = m_internal->scaleX();
×
NEW
171
  d["scaleX"] = carma::row_to_arr(scaleX);
×
NEW
172
  arma::vec y = m_internal->y();
×
NEW
173
  d["y"] = carma::col_to_arr(y);
×
NEW
174
  d["centerY"] = m_internal->centerY();
×
NEW
175
  d["scaleY"] = m_internal->scaleY();
×
NEW
176
  arma::vec noise = m_internal->noise();
×
NEW
177
  d["noise"] = carma::col_to_arr(noise);
×
NEW
178
  d["normalize"] = m_internal->normalize();
×
NEW
179
  d["regmodel"] = Trend::toString(m_internal->regmodel());
×
180

NEW
181
  arma::vec beta = m_internal->beta();
×
NEW
182
  d["beta"] = carma::col_to_arr(beta);
×
NEW
183
  d["is_beta_estim"] = m_internal->is_beta_estim();
×
NEW
184
  arma::mat F = m_internal->F();
×
NEW
185
  d["F"] = carma::mat_to_arr(F);
×
NEW
186
  arma::mat T = m_internal->T();
×
NEW
187
  d["T"] = carma::mat_to_arr(T);
×
NEW
188
  arma::mat M = m_internal->M();
×
NEW
189
  d["M"] = carma::mat_to_arr(M);
×
NEW
190
  arma::vec z = m_internal->z();
×
NEW
191
  d["z"] = carma::col_to_arr(z);
×
NEW
192
  return d;
×
NEW
193
}
×
194

195
std::string PyNoiseKriging::kernel() {
×
196
  return m_internal->kernel();
×
197
}
198

199
std::string PyNoiseKriging::optim() {
×
200
  return m_internal->optim();
×
201
}
202

203
std::string PyNoiseKriging::objective() {
×
204
  return m_internal->objective();
×
205
}
206

207
py::array_t<double> PyNoiseKriging::X() {
×
208
  return carma::mat_to_arr(m_internal->X());
×
209
}
210

211
py::array_t<double> PyNoiseKriging::centerX() {
×
212
  return carma::row_to_arr(m_internal->centerX());
×
213
}
214

215
py::array_t<double> PyNoiseKriging::scaleX() {
×
216
  return carma::row_to_arr(m_internal->scaleX());
×
217
}
218

219
py::array_t<double> PyNoiseKriging::y() {
×
220
  return carma::col_to_arr(m_internal->y());
×
221
}
222

223
double PyNoiseKriging::centerY() {
×
224
  return m_internal->centerY();
×
225
}
226

227
double PyNoiseKriging::scaleY() {
×
228
  return m_internal->scaleY();
×
229
}
230

231
py::array_t<double> PyNoiseKriging::noise() {
×
232
  return carma::col_to_arr(m_internal->noise());
×
233
}
234

235
bool PyNoiseKriging::normalize() {
×
236
  return m_internal->normalize();
×
237
}
238

239
std::string PyNoiseKriging::regmodel() {
×
240
  return Trend::toString(m_internal->regmodel());
×
241
}
242

243
py::array_t<double> PyNoiseKriging::F() {
×
244
  return carma::mat_to_arr(m_internal->F());
×
245
}
246

247
py::array_t<double> PyNoiseKriging::T() {
×
248
  return carma::mat_to_arr(m_internal->T());
×
249
}
250

251
py::array_t<double> PyNoiseKriging::M() {
×
252
  return carma::mat_to_arr(m_internal->M());
×
253
}
254

255
py::array_t<double> PyNoiseKriging::z() {
×
256
  return carma::col_to_arr(m_internal->z());
×
257
}
258

259
py::array_t<double> PyNoiseKriging::beta() {
×
260
  return carma::col_to_arr(m_internal->beta());
×
261
}
262

263
bool PyNoiseKriging::is_beta_estim() {
×
264
  return m_internal->is_beta_estim();
×
265
}
266

267
py::array_t<double> PyNoiseKriging::theta() {
×
268
  return carma::col_to_arr(m_internal->theta());
×
269
}
270

271
bool PyNoiseKriging::is_theta_estim() {
×
272
  return m_internal->is_theta_estim();
×
273
}
274

275
double PyNoiseKriging::sigma2() {
×
276
  return m_internal->sigma2();
×
277
}
278

279
bool PyNoiseKriging::is_sigma2_estim() {
×
280
  return m_internal->is_sigma2_estim();
×
281
}
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