Skip to content

Commit

Permalink
Fix kriging-based surrogates predict_variances when noise0 option…
Browse files Browse the repository at this point in the history
… is given as a `np.ndarray` (#710)

* Add test to show predict_variances bug with ndarray as noise0

* Fix the test for null noise

* Fix doc typo

* Format with ruff 0.9.2

* Bump ruff version 0.9.2 in pre-commit conf
  • Loading branch information
relf authored Jan 23, 2025
1 parent c48ac09 commit 12a4f42
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.6
rev: v0.9.2
hooks:
# Run the linter.
- id: ruff
Expand Down
4 changes: 2 additions & 2 deletions doc/_src_docs/surrogate_models/gpr.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions doc/_src_docs/surrogate_models/gpr.rstx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Gausian process regression
==========================
Gaussian process regression
===========================

SMT implements several surrogate models related to Gaussian process regression:

Expand Down
2 changes: 1 addition & 1 deletion smt/applications/ego.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def optimize(self, fun):
end_time = time.time()
iteration_time = end_time - start_time
if self.options["verbose"]:
print(f"iteration {k+1} took {iteration_time:.2f} seconds")
print(f"iteration {k + 1} took {iteration_time:.2f} seconds")
# Find the optimal point
ind_best = np.argmin(y_data if y_data.ndim == 1 else y_data[:, 0])
x_opt = x_data[ind_best]
Expand Down
2 changes: 1 addition & 1 deletion smt/applications/podi.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def interp_POD_basis(
f"WARNING: Sample of size {n_real} at {n_new} points of a {self.n_DoF}*{self.n_modes} matrix"
)
print(
f"Be sure to get sufficient memory for {n_real*n_new*self.n_DoF*self.n_modes} floats"
f"Be sure to get sufficient memory for {n_real * n_new * self.n_DoF * self.n_modes} floats"
)
# compute the interpolation of the coefficients and the associated variance
coeff, var = self.pred_coeff(mu, compute_var=True)
Expand Down
2 changes: 1 addition & 1 deletion smt/surrogate_models/krg_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ def _predict_variances(
np.dot(self.optimal_par["Ft"].T, rt)
- self._regression_types[self.options["poly"]](X_cont).T,
)
is_noisy = self.options["noise0"] != [0.0] or self.options["eval_noise"]
is_noisy = np.max(self.options["noise0"]) > 0.0 or self.options["eval_noise"]
if is_noisy and is_ri:
A = self.optimal_par["sigma2_ri"]
else:
Expand Down
12 changes: 12 additions & 0 deletions smt/surrogate_models/tests/test_krg_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def test_predict_variance(self):
noise_bounds=[1e-2, 1000.0],
print_global=False,
) # noisy Kriging model with estimated variance
sm_het_noise_estim = KRG(
noise0=np.array([1e-2, 2e-2, 3e-2, 4e-2, 5e-2]), # test with numpy array
use_het_noise=True,
print_global=False,
) # heteroscedastic noisy Kriging model

# training the models
sm_noise_free.set_training_values(xt, yt)
Expand All @@ -65,6 +70,9 @@ def test_predict_variance(self):
sm_noise_estim.set_training_values(xt, yt)
sm_noise_estim.train()

sm_het_noise_estim.set_training_values(xt, yt)
sm_het_noise_estim.train()

# predictions at training points
x = xt

Expand All @@ -79,6 +87,10 @@ def test_predict_variance(self):
self.assert_error(np.linalg.norm(var_noise_fixed), 0.04768, 1e-5)
var_noise_estim = sm_noise_estim.predict_variances(x) # predictive variance
self.assert_error(np.linalg.norm(var_noise_estim), 0.01135, 1e-3)
var_het_noise_estim = sm_het_noise_estim.predict_variances(
x
) # predictive variance
self.assert_error(np.linalg.norm(var_het_noise_estim), 0.02, 1e-3)

def test_null_predict_variance_ri_at_data_points(self):
# defining the training data
Expand Down
4 changes: 2 additions & 2 deletions tutorial/Misc/SMT_SGP_analytic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@
"print(\n",
" \"Considering float64, memory required: ~ %.2f Go\" % (8 * xt.shape[0] ** 2 / 1024**3)\n",
")\n",
"print(f\"Inference cost: ~ {xt.shape[0]**3:.2e} operations\")"
"print(f\"Inference cost: ~ {xt.shape[0] ** 3:.2e} operations\")"
]
},
{
Expand Down Expand Up @@ -1176,7 +1176,7 @@
"print(\n",
" \"Considering float64, memory required: ~ %.2f Mo\" % (8 * 50 * xt.shape[0] / 1024**2)\n",
")\n",
"print(f\"Inference cost: ~ {xt.shape[0]*50**2:.2e} operations\")"
"print(f\"Inference cost: ~ {xt.shape[0] * 50**2:.2e} operations\")"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions tutorial/PODI/SMT_PODI_Airfoil.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@
" contour_gt = ax.tricontourf(\n",
" triang, P_test_list[idx].flatten(), levels=100, cmap=\"viridis\"\n",
" )\n",
" ax.set_title(f\"Ground Truth Sample {idx+1}\")\n",
" ax.set_title(f\"Ground Truth Sample {idx + 1}\")\n",
" ax.set_xlabel(\"X Coordinate\")\n",
" ax.set_ylabel(\"Y Coordinate\")\n",
" fig.colorbar(contour_gt, ax=ax, orientation=\"vertical\")\n",
Expand All @@ -472,7 +472,7 @@
" contour_pred = ax.tricontourf(\n",
" triang, P_pred_list[idx].flatten(), levels=100, cmap=\"viridis\"\n",
" )\n",
" ax.set_title(f\"Predicted PODI Pressure Sample {idx+1}\")\n",
" ax.set_title(f\"Predicted PODI Pressure Sample {idx + 1}\")\n",
" ax.set_xlabel(\"X Coordinate\")\n",
" fig.colorbar(contour_pred, ax=ax, orientation=\"vertical\")\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions tutorial/SBO/SMT_EGO_noisyGP.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -817,10 +817,10 @@
],
"source": [
"print(\n",
" f\"with re-interpolation we get a variance at xdoe < 0.1 {t_noisy.predict_variances(xdoe, is_ri=True)<1e-1}\"\n",
" f\"with re-interpolation we get a variance at xdoe < 0.1 {t_noisy.predict_variances(xdoe, is_ri=True) < 1e-1}\"\n",
")\n",
"print(\n",
" f\"without re-interpolation we get a variance at xdoe > 1 {t_noisy.predict_variances(xdoe, is_ri=False)>1}\"\n",
" f\"without re-interpolation we get a variance at xdoe > 1 {t_noisy.predict_variances(xdoe, is_ri=False) > 1}\"\n",
")"
]
},
Expand Down

0 comments on commit 12a4f42

Please sign in to comment.