Skip to content

Commit 834facc

Browse files
0.12.5
modified handling of boolean hyperparameters
1 parent 898e642 commit 834facc

3 files changed

Lines changed: 92 additions & 10 deletions

File tree

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.12.4"
10+
version = "0.12.5"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/hyperparameters/values.py

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,27 @@ def assign_values(X: np.array, var_list: list) -> dict:
265265
return result
266266

267267

268+
def modify_boolean_hyper_parameter_levels(fun_control, hyperparameter, levels) -> None:
269+
"""
270+
This function modifies the levels of a boolean hyperparameter in the fun_control dictionary.
271+
It also sets the lower and upper bounds of the hyperparameter to 0 and len(levels) - 1, respectively.
272+
273+
Args:
274+
fun_control (dict):
275+
fun_control dictionary
276+
hyperparameter (str):
277+
hyperparameter name
278+
levels (list):
279+
list of levels
280+
281+
Returns:
282+
None.
283+
"""
284+
fun_control["core_model_hyper_dict"][hyperparameter].update({"levels": levels})
285+
fun_control["core_model_hyper_dict"][hyperparameter].update({"lower": levels[0]})
286+
fun_control["core_model_hyper_dict"][hyperparameter].update({"upper": levels[1]})
287+
288+
268289
def modify_hyper_parameter_levels(fun_control, hyperparameter, levels) -> None:
269290
"""
270291
This function modifies the levels of a hyperparameter in the fun_control dictionary.
@@ -655,10 +676,17 @@ def replace_levels_with_positions(hyper_dict, hyper_dict_values) -> dict:
655676
'stop_mem_management': 0}
656677
"""
657678
hyper_dict_values_new = copy.deepcopy(hyper_dict_values)
658-
for key, value in hyper_dict_values.items():
659-
if key in hyper_dict.keys():
660-
if "levels" in hyper_dict[key].keys():
661-
hyper_dict_values_new[key] = hyper_dict[key]["levels"].index(value)
679+
# generate an error if the following code fails and write an error message:
680+
try:
681+
for key, value in hyper_dict_values.items():
682+
if key in hyper_dict.keys():
683+
if "levels" in hyper_dict[key].keys():
684+
hyper_dict_values_new[key] = hyper_dict[key]["levels"].index(value)
685+
except Exception as e:
686+
print("!!! Warning: ", e)
687+
print("Did you modify lower and upper bounds so that the default values are not included?")
688+
print("Returning 'None'.")
689+
return None
662690
return hyper_dict_values_new
663691

664692

@@ -900,10 +928,13 @@ def get_default_hyperparameters_as_array(fun_control) -> np.array:
900928
"""
901929
X0 = get_default_values(fun_control)
902930
X0 = replace_levels_with_positions(fun_control["core_model_hyper_dict"], X0)
903-
X0 = get_values_from_dict(X0)
904-
X0 = np.array([X0])
905-
X0.shape[1]
906-
return X0
931+
if X0 is None:
932+
return None
933+
else:
934+
X0 = get_values_from_dict(X0)
935+
X0 = np.array([X0])
936+
X0.shape[1]
937+
return X0
907938

908939

909940
def get_default_hyperparameters_for_core_model(fun_control) -> dict:
@@ -1037,10 +1068,19 @@ def set_control_hyperparameter_value(control_dict, hyperparameter, value) -> Non
10371068
None.
10381069
10391070
"""
1071+
print(f"Setting hyperparameter {hyperparameter} to value {value}.")
10401072
vt = get_var_type_from_var_name(fun_control=control_dict, var_name=hyperparameter)
1041-
if vt == "factor":
1073+
print(f"Variable type is {vt}.")
1074+
core_type = get_core_model_parameter_type_from_var_name(fun_control=control_dict, var_name=hyperparameter)
1075+
print(f"Core type is {core_type}.")
1076+
if vt == "factor" and core_type != "bool":
1077+
print("Calling modify_hyper_parameter_levels().")
10421078
modify_hyper_parameter_levels(fun_control=control_dict, hyperparameter=hyperparameter, levels=value)
1079+
elif vt == "factor" and core_type == "bool":
1080+
print("Calling modify_boolean_hyper_parameter_levels().")
1081+
modify_boolean_hyper_parameter_levels(fun_control=control_dict, hyperparameter=hyperparameter, levels=value)
10431082
else:
1083+
print("Calling modify_hyper_parameter_bounds().")
10441084
modify_hyper_parameter_bounds(fun_control=control_dict, hyperparameter=hyperparameter, bounds=value)
10451085

10461086

@@ -1118,6 +1158,30 @@ def get_var_type_from_var_name(fun_control, var_name) -> str:
11181158
return var_type_list[var_name_list.index(var_name)]
11191159

11201160

1161+
def get_core_model_parameter_type_from_var_name(fun_control, var_name) -> str:
1162+
"""
1163+
Extracts the core_model_parameter_type value from a dictionary for a specified key.
1164+
1165+
Args:
1166+
fun_control (dict):
1167+
The dictionary containing the information.
1168+
var_name (str):
1169+
The key for which to extract the core_model_parameter_type value.
1170+
1171+
Returns:
1172+
(str):
1173+
The core_model_parameter_type value if available, else None.
1174+
"""
1175+
# Check if the key exists in the dictionary and it has a 'core_model_parameter_type' entry
1176+
if (
1177+
var_name in fun_control["core_model_hyper_dict"]
1178+
and "core_model_parameter_type" in fun_control["core_model_hyper_dict"][var_name]
1179+
):
1180+
return fun_control["core_model_hyper_dict"][var_name]["core_model_parameter_type"]
1181+
else:
1182+
return None
1183+
1184+
11211185
def get_ith_hyperparameter_name_from_fun_control(fun_control, key, i):
11221186
"""
11231187
Get the ith hyperparameter name from the fun_control dictionary.

src/spotPython/spot/spot.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,7 @@ def plot_contour(
15361536
contour_levels=10,
15371537
dpi=200,
15381538
title="",
1539+
verbosity=0,
15391540
) -> None:
15401541
"""Plot the contour of any dimension.
15411542
@@ -1560,6 +1561,8 @@ def plot_contour(
15601561
dpi of the plot. Default is 200.
15611562
title (str):
15621563
title of the plot
1564+
verbosity (int):
1565+
verbosity level. Default is 0.
15631566
15641567
Returns:
15651568
None
@@ -1608,6 +1611,21 @@ def plot_contour(
16081611
max_z = np.max(Z)
16091612
ax = fig.add_subplot(221)
16101613
# plot predicted values:
1614+
if verbosity > 0:
1615+
print(f"self.lower[i]: {self.lower[i]}")
1616+
print(f"self.upper[i]: {self.upper[i]}")
1617+
print(f"self.lower[j]: {self.lower[j]}")
1618+
print(f"self.upper[j]: {self.upper[j]}")
1619+
print(f"x: {x}")
1620+
print(f"y: {y}")
1621+
print(f"X: {X}")
1622+
print(f"Y: {Y}")
1623+
print(f"z0: {z0}")
1624+
print(f"zz: {zz}")
1625+
print(f"zs: {zs}")
1626+
print(f"Z: {Z}")
1627+
print(f"min_z: {min_z}")
1628+
print(f"max_z: {max_z}")
16111629
plt.contourf(X, Y, Z, contour_levels, zorder=1, cmap="jet", vmin=min_z, vmax=max_z)
16121630
if self.var_name is None:
16131631
plt.xlabel("x" + str(i))

0 commit comments

Comments
 (0)