Skip to content

Commit df771b9

Browse files
0.24.1
save experiment fixed
1 parent 3a4cdb6 commit df771b9

4 files changed

Lines changed: 123 additions & 21 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 116 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8184,26 +8184,126 @@
81848184
"name": "stdout",
81858185
"output_type": "stream",
81868186
"text": [
8187-
"Experiment saved to test_save_and_load_experiment_04_exp.pkl\n",
81888187
"No tensorboard log created.\n",
8188+
"Attribute fun_control is picklable and will be included in the experiment file.\n",
8189+
"Attribute design_control is picklable and will be included in the experiment file.\n",
8190+
"Attribute optimizer_control is picklable and will be included in the experiment file.\n",
8191+
"Attribute surrogate_control is picklable and will be included in the experiment file.\n",
8192+
"Attribute eps is picklable and will be included in the experiment file.\n",
8193+
"Attribute fun is picklable and will be included in the experiment file.\n",
8194+
"Attribute rng is picklable and will be included in the experiment file.\n",
8195+
"Attribute lower is picklable and will be included in the experiment file.\n",
8196+
"Attribute k is picklable and will be included in the experiment file.\n",
8197+
"Attribute upper is picklable and will be included in the experiment file.\n",
8198+
"Attribute var_type is picklable and will be included in the experiment file.\n",
8199+
"Attribute var_name is picklable and will be included in the experiment file.\n",
8200+
"Attribute all_lower is picklable and will be included in the experiment file.\n",
8201+
"Attribute all_upper is picklable and will be included in the experiment file.\n",
8202+
"Attribute ident is picklable and will be included in the experiment file.\n",
8203+
"Attribute red_dim is picklable and will be included in the experiment file.\n",
8204+
"Attribute all_var_type is picklable and will be included in the experiment file.\n",
8205+
"Attribute all_var_name is picklable and will be included in the experiment file.\n",
8206+
"Attribute fun_evals is picklable and will be included in the experiment file.\n",
8207+
"Attribute fun_repeats is picklable and will be included in the experiment file.\n",
8208+
"Attribute max_time is picklable and will be included in the experiment file.\n",
8209+
"Attribute noise is picklable and will be included in the experiment file.\n",
8210+
"Attribute tolerance_x is picklable and will be included in the experiment file.\n",
8211+
"Attribute ocba_delta is picklable and will be included in the experiment file.\n",
8212+
"Attribute log_level is picklable and will be included in the experiment file.\n",
8213+
"Attribute show_models is picklable and will be included in the experiment file.\n",
8214+
"Attribute show_progress is picklable and will be included in the experiment file.\n",
8215+
"Attribute infill_criterion is picklable and will be included in the experiment file.\n",
8216+
"Attribute n_points is picklable and will be included in the experiment file.\n",
8217+
"Attribute max_surrogate_points is picklable and will be included in the experiment file.\n",
8218+
"Attribute progress_file is picklable and will be included in the experiment file.\n",
8219+
"Attribute tkagg is picklable and will be included in the experiment file.\n",
8220+
"Attribute verbosity is picklable and will be included in the experiment file.\n",
8221+
"Attribute de_bounds is picklable and will be included in the experiment file.\n",
8222+
"Attribute design is picklable and will be included in the experiment file.\n",
8223+
"Attribute optimizer is picklable and will be included in the experiment file.\n",
8224+
"Attribute surrogate is picklable and will be included in the experiment file.\n",
8225+
"Attribute X is picklable and will be included in the experiment file.\n",
8226+
"Attribute y is picklable and will be included in the experiment file.\n",
8227+
"Attribute counter is picklable and will be included in the experiment file.\n",
8228+
"Attribute min_y is picklable and will be included in the experiment file.\n",
8229+
"Attribute min_X is picklable and will be included in the experiment file.\n",
8230+
"Attribute min_mean_X is picklable and will be included in the experiment file.\n",
8231+
"Attribute min_mean_y is picklable and will be included in the experiment file.\n",
8232+
"Attribute mean_X is picklable and will be included in the experiment file.\n",
8233+
"Attribute mean_y is picklable and will be included in the experiment file.\n",
8234+
"Attribute var_y is picklable and will be included in the experiment file.\n",
8235+
"Picklable instance created: {'fun_control': {'PREFIX': 'test_save_and_load_experiment_04', 'CHECKPOINT_PATH': 'runs/saved_models/', 'DATASET_PATH': 'data/', 'RESULTS_PATH': 'results/', 'TENSORBOARD_PATH': 'runs/', 'TENSORBOARD_CLEAN': False, '_L_in': None, '_L_out': None, '_L_cond': None, '_torchmetric': None, 'accelerator': 'auto', 'converters': None, 'core_model': None, 'core_model_name': None, 'counter': 0, 'data': None, 'data_dir': './data', 'data_full_train': None, 'data_module': None, 'data_set': None, 'data_set_name': None, 'data_test': None, 'data_val': None, 'db_dict_name': None, 'design': None, 'device': None, 'devices': 'auto', 'enable_progress_bar': False, 'eval': None, 'force_run': True, 'fun_evals': 15, 'fun_repeats': 1, 'horizon': None, 'hyperdict': None, 'infill_criterion': 'y', 'k_folds': 3, 'log_every_n_steps': 50, 'log_graph': False, 'log_level': 50, 'loss_function': None, 'lower': array([-1, -1]), 'max_time': 1, 'max_surrogate_points': 30, 'metric_river': None, 'metric_sklearn': None, 'metric_sklearn_name': None, 'metric_torch': None, 'metric_params': {}, 'model_dict': {}, 'noise': False, 'n_points': 1, 'n_samples': None, 'n_total': None, 'num_nodes': 1, 'num_sanity_val_steps': 2, 'num_workers': 0, 'ocba_delta': 0, 'oml_grace_period': None, 'optimizer': None, 'path': None, 'penalty_NA': None, 'precision': '32', 'prep_model': None, 'prep_model_name': None, 'progress_file': None, 'save_experiment': True, 'save_result': True, 'save_model': False, 'scaler': None, 'scaler_name': None, 'scenario': None, 'seed': 123, 'show_batch_interval': 1000000, 'show_config': False, 'show_models': False, 'show_progress': True, 'shuffle': None, 'sigma': 0.0, 'spot_tensorboard_path': None, 'strategy': 'auto', 'target_column': None, 'target_type': None, 'task': None, 'tensorboard_log': False, 'tensorboard_start': False, 'tensorboard_stop': False, 'test': None, 'test_seed': 1234, 'test_size': 0.4, 'tkagg': False, 'tolerance_x': 0, 'train': None, 'upper': array([1, 1]), 'var_name': None, 'var_type': ['num'], 'verbosity': 2, 'weights': 1.0, 'weight_coeff': 0.0, 'weights_entry': None}, 'design_control': {'init_size': 7, 'repeats': 1}, 'optimizer_control': {'max_iter': 1000, 'seed': 125}, 'surrogate_control': {'log_level': 50, 'noise': False, 'model_optimizer': <function differential_evolution at 0x13a877f60>, 'model_fun_evals': 10000, 'min_theta': -3.0, 'max_theta': 2.0, 'n_theta': 2, 'p_val': 2.0, 'n_p': 1, 'optim_p': False, 'min_Lambda': 1e-09, 'max_Lambda': 1, 'seed': 124, 'theta_init_zero': True, 'var_type': ['num', 'num'], 'metric_factorial': 'canberra'}, 'eps': np.float64(1.4901161193847656e-08), 'fun': <bound method Analytical.fun_sphere of analytical(offset=0.0, sigma=0.0, seed=126)>, 'rng': Generator(PCG64) at 0x159EAAB20, 'lower': array([-1, -1]), 'k': 2, 'upper': array([1, 1]), 'var_type': ['num', 'num'], 'var_name': ['x0', 'x1'], 'all_lower': array([-1, -1]), 'all_upper': array([1, 1]), 'ident': array([False, False]), 'red_dim': np.False_, 'all_var_type': ['num', 'num'], 'all_var_name': ['x0', 'x1'], 'fun_evals': 15, 'fun_repeats': 1, 'max_time': 1, 'noise': False, 'tolerance_x': 0, 'ocba_delta': 0, 'log_level': 50, 'show_models': False, 'show_progress': True, 'infill_criterion': 'y', 'n_points': 1, 'max_surrogate_points': 30, 'progress_file': None, 'tkagg': False, 'verbosity': 2, 'de_bounds': [[np.int64(-1), np.int64(1)], [np.int64(-1), np.int64(1)]], 'design': <spotpython.design.spacefilling.SpaceFilling object at 0x159e7d190>, 'optimizer': <function differential_evolution at 0x13a877f60>, 'surrogate': <spotpython.build.kriging.Kriging object at 0x15992e270>, 'X': None, 'y': None, 'counter': 0, 'min_y': None, 'min_X': None, 'min_mean_X': None, 'min_mean_y': None, 'mean_X': None, 'mean_y': None, 'var_y': None}\n",
8236+
"Experiment saved to test_save_and_load_experiment_04_exp.pkl\n",
81898237
"Loaded experiment from test_save_and_load_experiment_04_exp.pkl\n",
81908238
"S: Spot\n",
81918239
"S_loaded: Spot\n",
8192-
"<spotpython.spot.spot.Spot object at 0x14f48a240>\n"
8240+
"<spotpython.spot.spot.Spot object at 0x14ef77e60>\n",
8241+
"No spot_writer available.\n",
8242+
"spotpython tuning: 0.0054610613669814016 [#####-----] 53.33% \n",
8243+
"No spot_writer available.\n",
8244+
"spotpython tuning: 0.0009832082973447918 [######----] 60.00% \n",
8245+
"No spot_writer available.\n",
8246+
"spotpython tuning: 3.553659184723499e-05 [#######---] 66.67% \n",
8247+
"No spot_writer available.\n",
8248+
"spotpython tuning: 3.553659184723499e-05 [#######---] 73.33% \n",
8249+
"No spot_writer available.\n",
8250+
"spotpython tuning: 3.553659184723499e-05 [########--] 80.00% \n",
8251+
"No spot_writer available.\n",
8252+
"spotpython tuning: 3.553659184723499e-05 [#########-] 86.67% \n",
8253+
"No spot_writer available.\n",
8254+
"spotpython tuning: 3.553659184723499e-05 [#########-] 93.33% \n",
8255+
"No spot_writer available.\n",
8256+
"spotpython tuning: 3.553659184723499e-05 [##########] 100.00% Done...\n",
8257+
"\n",
8258+
"Experiment saved to test_save_and_load_experiment_04_res.pkl\n",
8259+
"No spot_writer available.\n",
8260+
"spotpython tuning: 0.0054610613669814016 [#####-----] 53.33% \n",
8261+
"No spot_writer available.\n",
8262+
"spotpython tuning: 0.0009832082973447918 [######----] 60.00% \n",
8263+
"No spot_writer available.\n",
8264+
"spotpython tuning: 3.553659184723499e-05 [#######---] 66.67% \n",
8265+
"No spot_writer available.\n",
8266+
"spotpython tuning: 3.553659184723499e-05 [#######---] 73.33% \n",
8267+
"No spot_writer available.\n",
8268+
"spotpython tuning: 3.553659184723499e-05 [########--] 80.00% \n",
8269+
"No spot_writer available.\n",
8270+
"spotpython tuning: 3.553659184723499e-05 [#########-] 86.67% \n",
8271+
"No spot_writer available.\n",
8272+
"spotpython tuning: 3.553659184723499e-05 [#########-] 93.33% \n",
8273+
"No spot_writer available.\n",
8274+
"spotpython tuning: 3.553659184723499e-05 [##########] 100.00% Done...\n",
8275+
"\n",
8276+
"Experiment saved to test_save_and_load_experiment_04_res.pkl\n",
8277+
"Loaded experiment from test_save_and_load_experiment_04_res.pkl\n",
8278+
"No spot_writer available.\n",
8279+
"spotpython tuning: 0.09066627114202976 [#####-----] 53.33% \n",
8280+
"No spot_writer available.\n",
8281+
"spotpython tuning: 0.09066626972235009 [######----] 60.00% \n",
8282+
"No spot_writer available.\n",
8283+
"spotpython tuning: 0.09066626838073348 [#######---] 66.67% \n",
8284+
"No spot_writer available.\n",
8285+
"spotpython tuning: 0.09066626689421296 [#######---] 73.33% \n",
8286+
"No spot_writer available.\n",
8287+
"spotpython tuning: 0.09066626491590751 [########--] 80.00% \n",
8288+
"No spot_writer available.\n",
8289+
"spotpython tuning: 0.09066626191827036 [#########-] 86.67% \n",
8290+
"No spot_writer available.\n",
8291+
"spotpython tuning: 0.09066625628125086 [#########-] 93.33% \n",
8292+
"No spot_writer available.\n",
8293+
"spotpython tuning: 0.09066624187947465 [##########] 100.00% Done...\n",
8294+
"\n",
8295+
"Experiment saved to test_save_and_load_experiment_04_res.pkl\n"
81938296
]
81948297
},
81958298
{
8196-
"ename": "AttributeError",
8197-
"evalue": "'NoneType' object has no attribute 'fit'",
8198-
"output_type": "error",
8199-
"traceback": [
8200-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
8201-
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
8202-
"Cell \u001b[0;32mIn[1], line 99\u001b[0m\n\u001b[1;32m 96\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m np\u001b[38;5;241m.\u001b[39marray_equal(S_loaded\u001b[38;5;241m.\u001b[39my, S\u001b[38;5;241m.\u001b[39my):\n\u001b[1;32m 97\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mResponse vector mismatch: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mS_loaded\u001b[38;5;241m.\u001b[39my\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m != \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mS\u001b[38;5;241m.\u001b[39my\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 99\u001b[0m \u001b[43mS_loaded\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 101\u001b[0m S\u001b[38;5;241m.\u001b[39mrun()\n\u001b[1;32m 102\u001b[0m S_loaded_2 \u001b[38;5;241m=\u001b[39m load_result(PREFIX)\n",
8203-
"File \u001b[0;32m~/miniforge3/envs/spot312/lib/python3.12/site-packages/spotpython/spot/spot.py:814\u001b[0m, in \u001b[0;36mSpot.run\u001b[0;34m(self, X_start)\u001b[0m\n\u001b[1;32m 812\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minitialize_design(X_start)\n\u001b[1;32m 813\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mupdate_stats()\n\u001b[0;32m--> 814\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit_surrogate\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 815\u001b[0m timeout_start \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[1;32m 816\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mshould_continue(timeout_start):\n",
8204-
"File \u001b[0;32m~/miniforge3/envs/spot312/lib/python3.12/site-packages/spotpython/spot/spot.py:1495\u001b[0m, in \u001b[0;36mSpot.fit_surrogate\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1493\u001b[0m X_S \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mX\n\u001b[1;32m 1494\u001b[0m y_S \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39my\n\u001b[0;32m-> 1495\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msurrogate\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m(X_S, y_S)\n\u001b[1;32m 1496\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1497\u001b[0m logger\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mX and y have different sizes. Surrogate not fitted.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
8205-
"\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'fit'"
8206-
]
8299+
"data": {
8300+
"text/plain": [
8301+
"<spotpython.spot.spot.Spot at 0x159f41d30>"
8302+
]
8303+
},
8304+
"execution_count": 1,
8305+
"metadata": {},
8306+
"output_type": "execute_result"
82078307
}
82088308
],
82098309
"source": [
@@ -8263,7 +8363,8 @@
82638363
" PREFIX=PREFIX,\n",
82648364
" lower=np.array([-1, -1]),\n",
82658365
" upper=np.array([1, 1]),\n",
8266-
" verbosity=1\n",
8366+
" verbosity=2,\n",
8367+
" log_level=50\n",
82678368
")\n",
82688369
"\n",
82698370
"design_control = design_control_init(init_size=7)\n",
@@ -8273,7 +8374,7 @@
82738374
"S = Spot(\n",
82748375
" fun=fun,\n",
82758376
" fun_control=fun_control,\n",
8276-
" design_control=design_control,\n",
8377+
" design_control=design_control\n",
82778378
")\n",
82788379
"\n",
82798380
"# Load the experiment\n",

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

src/spotpython/spot/spot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,6 @@ def __init__(
330330
self.mean_y = None
331331
self.var_y = None
332332

333-
# save experiment must be called before the spot_writer is initialized
334-
if self.fun_control.get("save_experiment"):
335-
self.save_experiment(verbosity=self.verbosity)
336-
337333
# Tensorboard must be initialized before the surrogate model:
338334
self.init_spot_writer()
339335

@@ -363,6 +359,10 @@ def __init__(
363359
counter=self.design_control["init_size"] * self.design_control["repeats"] - 1,
364360
)
365361

362+
# save experiment move here (spotpython >= v0.24.1)
363+
if self.fun_control.get("save_experiment"):
364+
self.save_experiment(verbosity=self.verbosity)
365+
366366
logger.setLevel(self.log_level)
367367
logger.info(f"Starting the logger at level {self.log_level} for module {__name__}:")
368368
logger.debug("In Spot() init(): fun_control: %s", self.fun_control)
@@ -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

test/test_save_and_load_experiment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from spotpython.fun.objectivefunctions import Analytical
55
from spotpython.utils.init import fun_control_init, design_control_init
66
from spotpython.utils.file import load_experiment
7+
import numpy as np
78

89
def _compare_dicts(dict1, dict2, ignore_keys=None):
910
"""

0 commit comments

Comments
 (0)