|
6 | 6 | surrogate_control_init, |
7 | 7 | optimizer_control_init, |
8 | 8 | ) |
| 9 | +import os |
| 10 | +import json |
| 11 | +import sys |
| 12 | +import importlib |
9 | 13 |
|
10 | 14 | # from torch.utils.tensorboard import SummaryWriter |
11 | 15 |
|
@@ -203,3 +207,42 @@ def load_experiment(PKL_NAME): |
203 | 207 | surrogate_control = experiment["surrogate_control"] |
204 | 208 | optimizer_control = experiment["optimizer_control"] |
205 | 209 | 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