Skip to content

Commit 4bd279f

Browse files
0.10.60
new functions: - load_dict_from_file() - load_core_model_from_file
1 parent ab74deb commit 4bd279f

2 files changed

Lines changed: 44 additions & 1 deletion

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

src/spotPython/utils/file.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
surrogate_control_init,
77
optimizer_control_init,
88
)
9+
import os
10+
import json
11+
import sys
12+
import importlib
913

1014
# from torch.utils.tensorboard import SummaryWriter
1115

@@ -203,3 +207,42 @@ def load_experiment(PKL_NAME):
203207
surrogate_control = experiment["surrogate_control"]
204208
optimizer_control = experiment["optimizer_control"]
205209
return spot_tuner, fun_control, design_control, surrogate_control, optimizer_control
210+
211+
212+
def load_dict_from_file(coremodel, dirname="userModel"):
213+
"""Loads a dictionary from a json file.
214+
215+
Args:
216+
coremodel (str): Name of the core model.
217+
dirname (str, optional): Directory name. Defaults to "userModel".
218+
219+
Returns:
220+
dict (dict): Dictionary with the core model.
221+
222+
"""
223+
file_path = os.path.join(dirname, f"{coremodel}.json")
224+
if os.path.isfile(file_path):
225+
with open(file_path, "r") as f:
226+
dict_tmp = json.load(f)
227+
dict = dict_tmp[coremodel]
228+
else:
229+
print(f"The file {file_path} does not exist.")
230+
dict = None
231+
return dict
232+
233+
234+
def load_core_model_from_file(coremodel, dirname="userModel"):
235+
"""Loads a core model from a python file.
236+
237+
Args:
238+
coremodel (str): Name of the core model.
239+
dirname (str, optional): Directory name. Defaults to "userModel".
240+
241+
Returns:
242+
coremodel (object): Core model.
243+
244+
"""
245+
sys.path.insert(0, "./" + dirname)
246+
module = importlib.import_module(coremodel)
247+
core_model = getattr(module, coremodel)
248+
return core_model

0 commit comments

Comments
 (0)