Skip to content

Commit 2c079c5

Browse files
0.26.16
gp_sep documentation cleanup type info sampling a la Forrester/Sobester
1 parent ece9ea2 commit 2c079c5

11 files changed

Lines changed: 707 additions & 896 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 359 additions & 49 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.26.15"
10+
version = "0.26.16"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/gp/gp_sep.py

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from sklearn.base import BaseEstimator, RegressorMixin
1818

1919

20-
def crude_reset(theta, tmin, tmax, m):
20+
def crude_reset(theta, tmin, tmax, m) -> dict:
2121
"""
2222
Check whether any elements of the parameter vector ``theta`` lie below the
2323
corresponding elements of the lower bound ``tmin``. If so, reset ``theta``
@@ -31,12 +31,12 @@ def crude_reset(theta, tmin, tmax, m):
3131
m (int): The dimensionality or number of parameters (used to adjust negative ``tmax`` entries).
3232
3333
Returns:
34-
dict or None: A dictionary containing:
34+
(dict) or None: A dictionary containing:
3535
- "theta" (np.ndarray): The reset parameter values.
3636
- "its" (int): Number of iterations (0, indicating immediate reset).
3737
- "msg" (str): Reason for the reset.
3838
- "conv" (int): Reset code (102).
39-
Returns None if no reset is needed.
39+
Returns None if no reset is needed.
4040
"""
4141
if np.any(theta < tmin):
4242
print("resetting due to init on lower boundary")
@@ -201,7 +201,7 @@ def garg(g, y: np.ndarray = None) -> dict:
201201
and priors for the nugget parameter.
202202
203203
Args:
204-
g: Could be a dictionary, numeric, or None. If numeric, turn it into {"start": g}.
204+
g (dict}: Could be a dictionary, numeric, or None. If numeric, turn it into {"start": g}.
205205
y (np.ndarray): The response vector.
206206
207207
Returns:
@@ -393,13 +393,13 @@ def __init__(
393393
}
394394

395395
# Add these two methods required by scikit-learn
396-
def get_params(self, deep=True):
396+
def get_params(self, deep=True) -> dict:
397397
"""Get parameters for this estimator.
398398
399399
This method is required for scikit-learn compatibility.
400400
401401
Args:
402-
deep: If True, will return the parameters for this estimator and
402+
deep (bool): If True, will return the parameters for this estimator and
403403
contained subobjects that are estimators. Defaults to True.
404404
405405
Returns:
@@ -419,16 +419,16 @@ def get_params(self, deep=True):
419419
"seed": self.seed,
420420
}
421421

422-
def set_params(self, **parameters):
422+
def set_params(self, **parameters: dict) -> "GPsep":
423423
"""Set the parameters of this estimator.
424424
425425
This method is required for scikit-learn compatibility.
426426
427427
Args:
428-
**parameters: Estimator parameters as keyword arguments.
428+
**parameters (dict): Estimator parameters as keyword arguments.
429429
430430
Returns:
431-
self: Estimator instance.
431+
self (GPsep): Estimator instance.
432432
"""
433433
for parameter, value in parameters.items():
434434
setattr(self, parameter, value)
@@ -442,18 +442,24 @@ def fit(self, X: np.ndarray, y: np.ndarray, d=None, g=None, dK: bool = True, aut
442442
"""Fit the GP model with training data and optionally auto-optimize hyperparameters.
443443
444444
Args:
445-
X: array-like of shape (n_samples, n_features)
446-
y: array-like of shape (n_samples,)
447-
d: The length-scale parameters. If None, will be determined
445+
X (np.ndarray):
446+
Array-like of shape (n_samples, n_features).
447+
y (np.ndarray):
448+
Array-like of shape (n_samples,).
449+
d (Optional[Union[np.ndarray, float]]):
450+
The length-scale parameters. If None, will be determined
448451
automatically. Defaults to None.
449-
g: The nugget parameter. If None, will be determined automatically.
450-
Defaults to None.
451-
dK: Flag to indicate whether to calculate derivatives.
452+
g (Optional[float]):
453+
The nugget parameter. If None, will be determined automatically. Defaults to None.
454+
dK (bool):
455+
Flag to indicate whether to calculate derivatives.
452456
Defaults to True.
453-
auto_optimize: Whether to automatically optimize hyperparameters
457+
auto_optimize (Optional[bool]):
458+
Whether to automatically optimize hyperparameters
454459
using MLE. If None, uses the default value from the object.
455460
Defaults to None.
456-
verbosity: Verbosity level for optimization output. Defaults to 0.
461+
verbosity (int):
462+
Verbosity level for optimization output. Defaults to 0.
457463
458464
Returns:
459465
GPsep: The fitted GPsep object.
@@ -685,22 +691,30 @@ def _build(self) -> None:
685691
# # raise RuntimeError("dK calculations have already been initialized.")
686692
# self.DK = diff_covar_sep_symm(self.m, self.X, self.n, self.d, self.K)
687693

688-
def _check_is_fitted(self):
694+
def _check_is_fitted(self) -> None:
695+
"""
696+
Check if the GPsep instance is fitted.
697+
"""
689698
if not self._is_fitted:
690699
raise ValueError("This GPsep instance is not fitted yet. Call 'fit' with " "appropriate arguments before using 'predict'.")
691700

692701
def predict(self, X: np.ndarray, lite: bool = False, nonug: bool = False, return_full=False, return_std=False) -> float:
693702
"""Predict the Gaussian Process output at new input points.
694703
695704
Args:
696-
X: The predictive locations.
697-
lite: Flag to indicate whether to compute only the diagonal
705+
X (np.ndarray):
706+
The predictive locations.
707+
lite (bool):
708+
Flag to indicate whether to compute only the diagonal
698709
of Sigma. Defaults to False.
699-
nonug: Flag to indicate whether to exclude nugget.
710+
nonug (bool):
711+
Flag to indicate whether to exclude nugget.
700712
Defaults to False.
701-
return_full: Flag to indicate whether to return the full dictionary,
713+
return_full (bool):
714+
Flag to indicate whether to return the full dictionary,
702715
which includes the mean, Sigma, df, and llik. Defaults to False.
703-
return_std: Flag to indicate whether to return the standard deviation.
716+
return_std (bool):
717+
Flag to indicate whether to return the standard deviation.
704718
Only applicable when return_full is False. Defaults to False.
705719
706720
Returns:
@@ -710,34 +724,34 @@ def predict(self, X: np.ndarray, lite: bool = False, nonug: bool = False, return
710724
- Otherwise: Mean predictions
711725
712726
Examples:
713-
import numpy as np
714-
from spotpython.gp.gp_sep import newGPsep
715-
import matplotlib.pyplot as plt
716-
# Simple sine data
717-
X = np.linspace(0, 2 * np.pi, 7).reshape(-1, 1)
718-
y = np.sin(X)
719-
# New GP fit
720-
gpsep = newGPsep(X, y, d=2, g=0.000001)
721-
# Make predictions
722-
XX = np.linspace(-1, 2 * np.pi + 1, 499).reshape(-1, 1)
723-
p = gpsep.predict(XX, lite=False)
724-
# Sample from the predictive distribution
725-
N = 100
726-
mean = p["mean"]
727-
Sigma = p["Sigma"]
728-
df = p["df"]
729-
# Generate samples from the multivariate t-distribution
730-
yy = np.random.multivariate_normal(mean, Sigma, N)
731-
yy = yy.T
732-
# Plot the results
733-
plt.figure(figsize=(10, 6))
734-
for i in range(N):
735-
plt.plot(XX, yy[:, i], color="gray", linewidth=0.5)
736-
plt.scatter(X, y, color="black", s=50, zorder=5)
737-
plt.xlabel("x")
738-
plt.ylabel("f-hat(x)")
739-
plt.title("Predictive Distribution")
740-
plt.show()
727+
import numpy as np
728+
from spotpython.gp.gp_sep import newGPsep
729+
import matplotlib.pyplot as plt
730+
# Simple sine data
731+
X = np.linspace(0, 2 * np.pi, 7).reshape(-1, 1)
732+
y = np.sin(X)
733+
# New GP fit
734+
gpsep = newGPsep(X, y, d=2, g=0.000001)
735+
# Make predictions
736+
XX = np.linspace(-1, 2 * np.pi + 1, 499).reshape(-1, 1)
737+
p = gpsep.predict(XX, lite=False)
738+
# Sample from the predictive distribution
739+
N = 100
740+
mean = p["mean"]
741+
Sigma = p["Sigma"]
742+
df = p["df"]
743+
# Generate samples from the multivariate t-distribution
744+
yy = np.random.multivariate_normal(mean, Sigma, N)
745+
yy = yy.T
746+
# Plot the results
747+
plt.figure(figsize=(10, 6))
748+
for i in range(N):
749+
plt.plot(XX, yy[:, i], color="gray", linewidth=0.5)
750+
plt.scatter(X, y, color="black", s=50, zorder=5)
751+
plt.xlabel("x")
752+
plt.ylabel("f-hat(x)")
753+
plt.title("Predictive Distribution")
754+
plt.show()
741755
"""
742756
self._check_is_fitted()
743757
# if X is a pandas dataframe, convert it to a numpy array

src/spotpython/plot/contour.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def simple_contour(
1313
max_z=None,
1414
n_samples=100,
1515
n_levels=30,
16-
):
16+
) -> None:
1717
"""
1818
Simple contour plot
1919
@@ -28,6 +28,9 @@ def simple_contour(
2828
n_samples (int, optional): _description_. Defaults to 100.
2929
n_levels (int, optional): _description_. Defaults to 5.
3030
31+
Returns:
32+
None
33+
3134
Examples:
3235
>>> import matplotlib.pyplot as plt
3336
import numpy as np
@@ -86,7 +89,7 @@ def plotModel(
8689
point_color_below="blue",
8790
point_color_above="red",
8891
atol=1e-6,
89-
):
92+
) -> None:
9093
"""
9194
Generate 2D contour and 3D surface plots for a model's predictions.
9295
@@ -379,39 +382,39 @@ def plotCombinations(
379382
point_color_below="blue",
380383
point_color_above="red",
381384
atol=1e-6,
382-
):
385+
) -> None:
383386
"""Plot model surfaces for multiple combinations of input variables.
384387
385388
This function generates contour and 3D surface plots for all specified
386389
combinations of input variables, avoiding redundancies and meaningless combinations.
387390
388391
Args:
389-
model: A fitted model with a predict method.
390-
X: Array of training points (optional). If provided, used to derive bounds and dimension count.
391-
lower: Array-like lower bounds for each dimension. If None, derived from X.
392-
upper: Array-like upper bounds for each dimension. If None, derived from X.
393-
x_vars: List of indices for x-axis variables. Defaults to all if None or empty.
394-
y_vars: List of indices for y-axis variables. Defaults to all if None or empty.
395-
min_z: Min value for color scale. If None, auto-calculated.
396-
max_z: Max value for color scale. If None, auto-calculated.
397-
var_type: List of variable types. If None, assumed numeric.
398-
var_name: List of variable names. If None, named x0, x1, ...
399-
show: Whether to display the plots. Defaults to True.
400-
save_dir: Directory for saving plots. If None, not saved.
401-
n_grid: Number of grid points along each axis. Defaults to 50.
402-
contour_levels: Number of contour levels. Defaults to 10.
403-
dpi: DPI for saving figures. Defaults to 200.
404-
title_prefix: Prefix string for plot titles.
405-
figsize: Figure size (width, height). Defaults to (12, 6).
406-
use_min: Use lower bounds for non-plotted dimensions. Defaults to False.
407-
use_max: Use upper bounds for non-plotted dimensions. Defaults to False.
408-
margin: Fraction of range added as margin to bounds when derived from X. Defaults to 0.1.
409-
aspect_equal: Whether to set equal aspect ratio. Defaults to False.
410-
legend_fontsize: Font size for labels and legends. Defaults to 12.
392+
model (object): A fitted model with a predict method.
393+
X (Optional[np.ndarray]): Array of training points. If provided, used to derive bounds and dimension count.
394+
lower (Optional[Union[np.ndarray, List[float]]]): Array-like lower bounds for each dimension. If None, derived from X.
395+
upper (Optional[Union[np.ndarray, List[float]]]): Array-like upper bounds for each dimension. If None, derived from X.
396+
x_vars (Optional[List[int]]): List of indices for x-axis variables. Defaults to all if None or empty.
397+
y_vars (Optional[List[int]]): List of indices for y-axis variables. Defaults to all if None or empty.
398+
min_z (Optional[float]): Min value for color scale. If None, auto-calculated.
399+
max_z (Optional[float]): Max value for color scale. If None, auto-calculated.
400+
var_type (Optional[List[str]]): List of variable types. If None, assumed numeric.
401+
var_name (Optional[List[str]]): List of variable names. If None, named x0, x1, ...
402+
show (bool): Whether to display the plots. Defaults to True.
403+
save_dir (Optional[str]): Directory for saving plots. If None, not saved.
404+
n_grid (int): Number of grid points along each axis. Defaults to 50.
405+
contour_levels (int): Number of contour levels. Defaults to 10.
406+
dpi (int): DPI for saving figures. Defaults to 200.
407+
title_prefix (str): Prefix string for plot titles.
408+
figsize (Tuple[float, float]): Figure size (width, height). Defaults to (12, 6).
409+
use_min (bool): Use lower bounds for non-plotted dimensions. Defaults to False.
410+
use_max (bool): Use upper bounds for non-plotted dimensions. Defaults to False.
411+
margin (float): Fraction of range added as margin to bounds when derived from X. Defaults to 0.1.
412+
aspect_equal (bool): Whether to set equal aspect ratio. Defaults to False.
413+
legend_fontsize (int): Font size for labels and legends. Defaults to 12.
411414
cmap (str): Colormap for the plots. Defaults to "viridis".
412-
X_points: Original data points to plot.
413-
y_points: Original target values to plot.
414-
plot_points (bool): Whether to plot X_points.
415+
X_points (Optional[np.ndarray]): Original data points to plot.
416+
y_points (Optional[np.ndarray]): Original target values to plot.
417+
plot_points (bool): Whether to plot X_points. Defaults to True.
415418
points_color (str): Color for data points. Defaults to "white".
416419
points_size (int): Marker size for data points. Defaults to 30.
417420
point_color_below (str): Color if actual z < predicted z. Defaults to "lightgrey".

src/spotpython/utils/effects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def screening(X, fun, xi, p, labels, range=None, print=False) -> pd.DataFrame:
5959
Args:
6060
X (np.ndarray): The screening plan matrix, typically structured
6161
within a [0,1]^k box.
62-
fun: The objective function to evaluate at each
62+
fun (object): The objective function to evaluate at each
6363
design point in the screening plan.
6464
xi (float): The elementary effect step length factor.
6565
p (int): Number of discrete levels along each dimension.

src/spotpython/utils/optimize.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from scipy.optimize import minimize
33

44

5-
def run_minimize_with_restarts(objective, gradient, x0, bounds, n_restarts_optimizer=5, method="L-BFGS-B", maxit=100, verb=0, random_state=None):
5+
def run_minimize_with_restarts(objective, gradient, x0, bounds, n_restarts_optimizer=5, method="L-BFGS-B", maxit=100, verb=0, random_state=None) -> "minimize":
66
"""
77
Runs multiple restarts of the minimize() function and returns the best found result.
88
@@ -18,7 +18,10 @@ def run_minimize_with_restarts(objective, gradient, x0, bounds, n_restarts_optim
1818
random_state (int, optional): Seed for the random-number generator to ensure reproducibility.
1919
2020
Returns:
21-
OptimizeResult: The best optimization result among all restarts.
21+
OptimizeResult (object): The best optimization result among all restarts,
22+
represented as a ``OptimizeResult`` object. Important attributes are:
23+
``x`` the solution array, ``success`` a Boolean flag indicating if the optimizer
24+
exited successfully and ``message`` which describes the cause of the termination.
2225
"""
2326
if random_state is not None:
2427
np.random.seed(random_state)

0 commit comments

Comments
 (0)