Skip to content

Commit d4d344f

Browse files
0.10.57
- get_tensorboard_path(), get_spot_tensorboard_path(), and get_experimentname() moved from utils.file.py to utils.init.py - save_experiment() updated: stores spot_tuner, fun_control, design_control, surrogate_control, and optimizer_control - load_experiment() changed accordingly - new test: test_file_save_load
1 parent abcc4d3 commit d4d344f

7 files changed

Lines changed: 292 additions & 81 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 122 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,19 +2977,9 @@
29772977
},
29782978
{
29792979
"cell_type": "code",
2980-
"execution_count": 1,
2980+
"execution_count": null,
29812981
"metadata": {},
2982-
"outputs": [
2983-
{
2984-
"name": "stdout",
2985-
"output_type": "stream",
2986-
"text": [
2987-
"['Sigmoid', 'Tanh', 'ReLU', 'LeakyReLU', 'ELU', 'Swish']\n",
2988-
"['Adadelta', 'Adagrad', 'Adam', 'AdamW', 'SparseAdam', 'Adamax', 'ASGD', 'NAdam', 'RAdam', 'RMSprop', 'Rprop', 'SGD']\n",
2989-
"['Default', 'Kaiming', 'Xavier']\n"
2990-
]
2991-
}
2992-
],
2982+
"outputs": [],
29932983
"source": [
29942984
"import tkinter as tk\n",
29952985
"from spotPython.hyperdict.light_hyper_dict import LightHyperDict\n",
@@ -3047,21 +3037,136 @@
30473037
},
30483038
{
30493039
"cell_type": "code",
3050-
"execution_count": 2,
3040+
"execution_count": null,
3041+
"metadata": {},
3042+
"outputs": [],
3043+
"source": [
3044+
"create_gui(model = 'TransformerLightRegression')"
3045+
]
3046+
},
3047+
{
3048+
"cell_type": "code",
3049+
"execution_count": null,
3050+
"metadata": {},
3051+
"outputs": [],
3052+
"source": []
3053+
},
3054+
{
3055+
"cell_type": "markdown",
3056+
"metadata": {},
3057+
"source": [
3058+
"# save Load Test"
3059+
]
3060+
},
3061+
{
3062+
"cell_type": "code",
3063+
"execution_count": 14,
3064+
"metadata": {},
3065+
"outputs": [],
3066+
"source": [
3067+
"import os\n",
3068+
"from spotPython.utils.file import save_experiment, load_experiment\n",
3069+
"import numpy as np\n",
3070+
"from math import inf\n",
3071+
"from spotPython.spot import spot\n",
3072+
"from spotPython.utils.init import (\n",
3073+
" fun_control_init,\n",
3074+
" design_control_init,\n",
3075+
" surrogate_control_init,\n",
3076+
" optimizer_control_init)\n",
3077+
"from spotPython.fun.objectivefunctions import analytical\n",
3078+
"\n",
3079+
"def test_file_save_load():\n",
3080+
" fun = analytical().fun_branin\n",
3081+
"\n",
3082+
" fun_control = fun_control_init(\n",
3083+
" PREFIX=\"branin\",\n",
3084+
" SUMMARY_WRITER=False,\n",
3085+
" lower = np.array([0, 0]),\n",
3086+
" upper = np.array([10, 10]),\n",
3087+
" fun_evals=8,\n",
3088+
" fun_repeats=1,\n",
3089+
" max_time=inf,\n",
3090+
" noise=False,\n",
3091+
" tolerance_x=0,\n",
3092+
" ocba_delta=0,\n",
3093+
" var_type=[\"num\", \"num\"],\n",
3094+
" infill_criterion=\"ei\",\n",
3095+
" n_points=1,\n",
3096+
" seed=123,\n",
3097+
" log_level=20,\n",
3098+
" show_models=False,\n",
3099+
" show_progress=True)\n",
3100+
" design_control = design_control_init(\n",
3101+
" init_size=5,\n",
3102+
" repeats=1)\n",
3103+
" surrogate_control = surrogate_control_init(\n",
3104+
" model_fun_evals=10000,\n",
3105+
" min_theta=-3,\n",
3106+
" max_theta=3,\n",
3107+
" n_theta=2,\n",
3108+
" theta_init_zero=True,\n",
3109+
" n_p=1,\n",
3110+
" optim_p=False,\n",
3111+
" var_type=[\"num\", \"num\"],\n",
3112+
" seed=124)\n",
3113+
" optimizer_control = optimizer_control_init(\n",
3114+
" max_iter=1000,\n",
3115+
" seed=125)\n",
3116+
" spot_tuner = spot.Spot(fun=fun,\n",
3117+
" fun_control=fun_control,\n",
3118+
" design_control=design_control,\n",
3119+
" surrogate_control=surrogate_control,\n",
3120+
" optimizer_control=optimizer_control)\n",
3121+
" # Call the save_experiment function\n",
3122+
" pkl_name = save_experiment(\n",
3123+
" spot_tuner=spot_tuner,\n",
3124+
" fun_control=fun_control,\n",
3125+
" design_control=None,\n",
3126+
" surrogate_control=None,\n",
3127+
" optimizer_control=None\n",
3128+
" )\n",
3129+
"\n",
3130+
" # Verify that the pickle file is created\n",
3131+
" assert os.path.exists(pkl_name)\n",
3132+
"\n",
3133+
" # Call the load_experiment function\n",
3134+
" spot_tuner_1, fun_control_1, design_control_1, surrogate_control_1, optimizer_control_1 = load_experiment(pkl_name)\n",
3135+
"\n",
3136+
" # Verify the name of the pickle file\n",
3137+
" assert pkl_name == f\"spot_{fun_control['PREFIX']}experiment.pickle\"\n",
3138+
"\n",
3139+
" # Clean up the temporary directory\n",
3140+
" os.remove(pkl_name)\n"
3141+
]
3142+
},
3143+
{
3144+
"cell_type": "markdown",
3145+
"metadata": {},
3146+
"source": []
3147+
},
3148+
{
3149+
"cell_type": "code",
3150+
"execution_count": 15,
30513151
"metadata": {},
30523152
"outputs": [
3153+
{
3154+
"name": "stderr",
3155+
"output_type": "stream",
3156+
"text": [
3157+
"Seed set to 123\n"
3158+
]
3159+
},
30533160
{
30543161
"name": "stdout",
30553162
"output_type": "stream",
30563163
"text": [
3057-
"['Tanh', 'ReLU']\n",
3058-
"['Adadelta', 'Adagrad', 'Adam', 'AdamW', 'Adamax', 'NAdam', 'RAdam', 'RMSprop', 'Rprop', 'SGD']\n",
3059-
"['Default', 'Kaiming', 'Xavier']\n"
3164+
"Experiment saved as spot_braninexperiment.pickle\n"
30603165
]
30613166
}
30623167
],
30633168
"source": [
3064-
"create_gui(model = 'TransformerLightRegression')"
3169+
"test_file_save_load()"
30653170
]
30663171
},
30673172
{

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

src/spotPython/spot/spot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ def objective_function(X, fun_control=None):
120120
y = x0**2 + 10*x1**2
121121
return y
122122
fun_control = fun_control_init(
123-
lower = np.array([0, 0])
124-
upper = np.array([10, 10])
123+
lower = np.array([0, 0]),
124+
upper = np.array([10, 10]),
125125
fun_evals=8,
126126
fun_repeats=1,
127127
max_time=inf,

src/spotPython/utils/file.py

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import torchvision
22
import torchvision.transforms as transforms
3-
import socket
4-
from datetime import datetime
5-
from dateutil.tz import tzlocal
63
import pickle
7-
import os
4+
from spotPython.utils.init import (
5+
design_control_init,
6+
surrogate_control_init,
7+
optimizer_control_init,
8+
)
9+
10+
# from torch.utils.tensorboard import SummaryWriter
811

912

1013
def load_data(data_dir="./data"):
@@ -30,27 +33,6 @@ def load_data(data_dir="./data"):
3033
return trainset, testset
3134

3235

33-
def get_experiment_name(prefix: str = "00") -> str:
34-
"""Returns a unique experiment name with a given prefix.
35-
36-
Args:
37-
prefix (str, optional): Prefix for the experiment name. Defaults to "00".
38-
39-
Returns:
40-
str: Unique experiment name.
41-
42-
Examples:
43-
>>> from spotPython.utils.file import get_experiment_name
44-
>>> get_experiment_name(prefix="00")
45-
00_ubuntu_2021-08-31_14-30-00
46-
"""
47-
start_time = datetime.now(tzlocal())
48-
HOSTNAME = socket.gethostname().split(".")[0]
49-
experiment_name = prefix + "_" + HOSTNAME + "_" + str(start_time).split(".", 1)[0].replace(" ", "_")
50-
experiment_name = experiment_name.replace(":", "-")
51-
return experiment_name
52-
53-
5436
def save_pickle(obj, filename: str):
5537
"""Saves an object as a pickle file.
5638
Add .pkl to the filename.
@@ -88,46 +70,42 @@ def load_pickle(filename: str):
8870
return obj
8971

9072

91-
def get_spot_tensorboard_path(experiment_name):
92-
"""Get the path to the spot tensorboard files.
93-
94-
Args:
95-
experiment_name (str): The name of the experiment.
96-
97-
Returns:
98-
spot_tensorboard_path (str): The path to the folder where the spot tensorboard files are saved.
99-
"""
100-
spot_tensorboard_path = os.environ.get("PATH_TENSORBOARD", "runs/spot_logs/")
101-
spot_tensorboard_path = os.path.join(spot_tensorboard_path, experiment_name)
102-
return spot_tensorboard_path
103-
104-
105-
def get_tensorboard_path(fun_control):
106-
"""Get the path to the tensorboard files.
107-
108-
Args:
109-
fun_control (dict): The function control dictionary.
110-
111-
Returns:
112-
tensorboard_path (str): The path to the folder where the tensorboard files are saved.
113-
"""
114-
return fun_control["TENSORBOARD_PATH"]
115-
116-
117-
def save_experiment(spot_tuner, fun_control) -> str:
73+
def save_experiment(
74+
spot_tuner, fun_control, design_control=None, surrogate_control=None, optimizer_control=None
75+
) -> str:
11876
"""
11977
Saves the experiment as a pickle file.
12078
12179
Args:
12280
spot_tuner (object): The spot tuner object.
12381
fun_control (dict): The function control dictionary.
82+
design_control (dict, optional): The design control dictionary. Defaults to None.
83+
surrogate_control (dict, optional): The surrogate control dictionary. Defaults to None.
84+
optimizer_control (dict, optional): The optimizer control dictionary. Defaults to None.
12485
12586
Returns:
12687
PKL_NAME (str):
12788
Name of the pickle file. Build as "spot_" + PREFIX + "experiment.pickle".
12889
12990
"""
130-
experiment = {"spot_tuner": spot_tuner, "fun_control": fun_control}
91+
if design_control is None:
92+
design_control = design_control_init()
93+
if surrogate_control is None:
94+
surrogate_control = surrogate_control_init()
95+
if optimizer_control is None:
96+
optimizer_control = optimizer_control_init()
97+
# remove the key "spot_writer" from the fun_control dictionary,
98+
# because it is not serializable.
99+
# TODO: It will be re-added when the experiment is loaded.
100+
fun_control.pop("spot_writer", None)
101+
102+
experiment = {
103+
"spot_tuner": spot_tuner,
104+
"fun_control": fun_control,
105+
"design_control": design_control,
106+
"surrogate_control": surrogate_control,
107+
"optimizer_control": optimizer_control,
108+
}
131109
PREFIX = fun_control["PREFIX"]
132110
PKL_NAME = "spot_" + PREFIX + "experiment.pickle"
133111
with open(PKL_NAME, "wb") as handle:
@@ -146,10 +124,18 @@ def load_experiment(PKL_NAME):
146124
Returns:
147125
spot_tuner (object): The spot tuner object.
148126
fun_control (dict): The function control dictionary.
127+
design_control (dict): The design control dictionary.
128+
surrogate_control (dict): The surrogate control dictionary.
129+
optimizer_control (dict): The optimizer control dictionary.
149130
150131
"""
151132
with open(PKL_NAME, "rb") as handle:
152133
experiment = pickle.load(handle)
153134
spot_tuner = experiment["spot_tuner"]
154135
fun_control = experiment["fun_control"]
155-
return spot_tuner, fun_control
136+
design_control = experiment["design_control"]
137+
surrogate_control = experiment["surrogate_control"]
138+
optimizer_control = experiment["optimizer_control"]
139+
# TODO: Add the key "spot_writer" to the fun_control dictionary,
140+
# because it was not saved in the pickle file.
141+
return spot_tuner, fun_control, design_control, surrogate_control, optimizer_control

0 commit comments

Comments
 (0)