Skip to content

Commit 9fb562e

Browse files
0.24.3
formatting, new fcts: show_exp_table, show_res_table
1 parent 420adfe commit 9fb562e

5 files changed

Lines changed: 75 additions & 44 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.24.2"
10+
version = "0.24.3"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/hyperparameters/values.py

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ def get_dict_with_levels_and_types(fun_control: Dict[str, Any], v: Dict[str, Any
187187
Examples:
188188
>>> from spotpython.hyperdict.light_hyper_dict import LightHyperDict
189189
from spotpython.hyperparameters.values import get_default_hyperparameters_as_array
190-
from spotpython.hyperparameters.values import assign_values, get_var_name, iterate_dict_values, convert_keys, get_dict_with_levels_and_types, get_core_model_from_name, add_core_model_to_fun_control
190+
from spotpython.hyperparameters.values import (assign_values, get_var_name, iterate_dict_values,
191+
convert_keys, get_dict_with_levels_and_types, get_core_model_from_name, add_core_model_to_fun_control)
191192
import pprint
192193
core_model_name="light.regression.NNLinearRegressor"
193194
hyperdict=LightHyperDict
@@ -508,40 +509,6 @@ def get_tuned_architecture(spot_tuner, force_minX=False) -> dict:
508509
return config
509510

510511

511-
def return_conf_list_from_var_dict(
512-
var_dict: Dict[str, np.ndarray],
513-
fun_control: Dict[str, Union[List[str], str]],
514-
default: bool = False,
515-
) -> List[Dict[str, Union[int, float]]]:
516-
"""Return a list of configurations from a dictionary of variables.
517-
518-
This function takes a dictionary of variables and a dictionary of function control as input arguments.
519-
It performs similar steps as generate_one_config_from_var_dict() but returns a list of dictionaries
520-
of hyper parameter values.
521-
522-
Args:
523-
var_dict (dict): A dictionary where keys are variable names and values are numpy arrays.
524-
fun_control (dict): A dictionary which (at least) has an entry with the following key:
525-
"var_type" (list): A list of variable types. If the entry is not "num" the corresponding
526-
value will be converted to the type "int".
527-
528-
Returns:
529-
list: A list of dictionaries of hyper parameter values. Transformations are applied to the values.
530-
531-
Examples:
532-
>>> import numpy as np
533-
>>> from spotpython.hyperparameters.values import return_conf_list_from_var_dict
534-
>>> var_dict = {'a': np.array([1, 3, 5]), 'b': np.array([2, 4, 6])}
535-
>>> fun_control = {'var_type': ['int', 'int']}
536-
>>> return_conf_list_from_var_dict(var_dict, fun_control)
537-
[{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}]
538-
"""
539-
conf_list = []
540-
for values in generate_one_config_from_var_dict(var_dict, fun_control, default=default):
541-
conf_list.append(values)
542-
return conf_list
543-
544-
545512
def modify_boolean_hyper_parameter_levels(fun_control, hyperparameter, levels) -> None:
546513
"""
547514
This function modifies the levels of a boolean hyperparameter in the fun_control dictionary.

src/spotpython/spot/spot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def __init__(
361361

362362
# save experiment move here (spotpython >= v0.24.1)
363363
if self.fun_control.get("save_experiment"):
364-
self.save_experiment(verbosity=self.verbosity)
364+
self.save_experiment(verbosity=self.verbosity)
365365

366366
logger.setLevel(self.log_level)
367367
logger.info(f"Starting the logger at level {self.log_level} for module {__name__}:")
@@ -1139,7 +1139,7 @@ def save_experiment(self, filename=None, path=None, overwrite=True, unpickleable
11391139
# Ensure we don't accidentally try to pickle unpicklable components
11401140
self._close_and_del_spot_writer()
11411141
self._remove_logger_handlers()
1142-
1142+
11431143
S = self._get_pickle_safe_spot_tuner(unpickleables=unpickleables, verbosity=verbosity)
11441144

11451145
# Determine the filename based on PREFIX if not provided

src/spotpython/utils/eda.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,75 @@ def get_stars(input_list) -> list:
4545
return output_list
4646

4747

48+
def show_exp_table(fun_control: dict, tablefmt="github") -> str:
49+
"""Generates a table with the design variables and their bounds.
50+
Can be used for the experiment design, which was not run yet.
51+
Args:
52+
fun_control (dict):
53+
A dictionary with function design variables.
54+
Returns:
55+
(str):
56+
a table with the design variables, their default values, and their bounds.
57+
Use the `print` function to display the table.
58+
"""
59+
default_values = get_default_values(fun_control)
60+
defaults = list(default_values.values())
61+
tab = tabulate(
62+
{
63+
"name": get_var_name(fun_control),
64+
"type": get_var_type(fun_control),
65+
"default": defaults,
66+
"lower": get_bound_values(fun_control, "lower", as_list=True),
67+
"upper": get_bound_values(fun_control, "upper", as_list=True),
68+
"transform": get_transform(fun_control),
69+
},
70+
headers="keys",
71+
tablefmt=tablefmt,
72+
)
73+
return tab
74+
75+
76+
def show_res_table(spot: object = None, tablefmt="github") -> str:
77+
"""
78+
Generates a table with the design variables and their bounds,
79+
after the run was completed.
80+
81+
Args:
82+
spot (object):
83+
A spot object. Defaults to None.
84+
Returns:
85+
(str):
86+
a table with the design variables, their default values, their bounds,
87+
the value and the importance of each hyperparameter.
88+
Use the `print` function to display the table.
89+
"""
90+
fun_control = spot.fun_control
91+
default_values = get_default_values(fun_control)
92+
defaults = list(default_values.values())
93+
res = spot.print_results(print_screen=False, dict=fun_control)
94+
tuned = [item[1] for item in res]
95+
importance = spot.get_importance()
96+
stars = get_stars(importance)
97+
tab = tabulate(
98+
{
99+
"name": get_var_name(fun_control),
100+
"type": get_var_type(fun_control),
101+
"default": defaults,
102+
"lower": get_bound_values(fun_control, "lower", as_list=True),
103+
"upper": get_bound_values(fun_control, "upper", as_list=True),
104+
"tuned": tuned,
105+
"transform": get_transform(fun_control),
106+
"importance": importance,
107+
"stars": stars,
108+
},
109+
headers="keys",
110+
numalign="right",
111+
floatfmt=("", "", "", "", "", "", "", ".2f"),
112+
tablefmt=tablefmt,
113+
)
114+
return tab
115+
116+
48117
def gen_design_table(fun_control: dict, spot: object = None, tablefmt="github") -> str:
49118
"""Generates a table with the design variables and their bounds.
50119
Args:

src/spotpython/utils/file.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
import json
66
import sys
77
import importlib
8-
from spotpython.utils.eda import gen_design_table
9-
from spotpython.utils.init import setup_paths
10-
import copy
11-
import pprint
128

139

1410
# from torch.utils.tensorboard import SummaryWriter
@@ -214,11 +210,10 @@ def load_and_run_spot_python_experiment(PREFIX=None, filename=None) -> object:
214210
215211
"""
216212
S = load_experiment(PREFIX=PREFIX, filename=filename)
217-
S.run()
213+
S.run()
218214
return S
219215

220216

221-
222217
def load_dict_from_file(coremodel, dirname="userModel"):
223218
"""Loads a dictionary from a json file.
224219

0 commit comments

Comments
 (0)