|
4 | 4 | from scipy.optimize import differential_evolution |
5 | 5 | import numpy as np |
6 | 6 | import socket |
| 7 | +import copy |
7 | 8 | import datetime |
8 | 9 | from dateutil.tz import tzlocal |
| 10 | +from importlib.metadata import version, PackageNotFoundError |
9 | 11 | from spotpython.hyperparameters.values import ( |
10 | 12 | add_core_model_to_fun_control, |
11 | 13 | get_core_model_from_name, |
@@ -43,6 +45,7 @@ def fun_control_init( |
43 | 45 | enable_progress_bar=False, |
44 | 46 | EXPERIMENT_NAME=None, |
45 | 47 | eval=None, |
| 48 | + force_run=False, |
46 | 49 | fun_evals=15, |
47 | 50 | fun_repeats=1, |
48 | 51 | horizon=None, |
@@ -162,6 +165,10 @@ def fun_control_init( |
162 | 165 | The name of the experiment. |
163 | 166 | Default is None. If None, the experiment name is generated based on the |
164 | 167 | current date and time. |
| 168 | + force_run (bool): |
| 169 | + Whether to force the run or not. If a result file (PREFIX+"_run.pkl") exists, the run is mot |
| 170 | + performed and the result is loaded from the file. |
| 171 | + Default is False. |
165 | 172 | fun_evals (int): |
166 | 173 | The number of function evaluations. |
167 | 174 | fun_repeats (int): |
@@ -382,7 +389,7 @@ def fun_control_init( |
382 | 389 | L.seed_everything(seed) |
383 | 390 |
|
384 | 391 | if PREFIX is None: |
385 | | - PREFIX = _init_PREFIX() |
| 392 | + PREFIX = _init_prefix() |
386 | 393 |
|
387 | 394 | CHECKPOINT_PATH, DATASET_PATH, RESULTS_PATH, TENSORBOARD_PATH = setup_paths(TENSORBOARD_CLEAN) |
388 | 395 | spot_tensorboard_path = create_spot_tensorboard_path(tensorboard_log, PREFIX) |
@@ -420,6 +427,7 @@ def fun_control_init( |
420 | 427 | "devices": devices, |
421 | 428 | "enable_progress_bar": enable_progress_bar, |
422 | 429 | "eval": eval, |
| 430 | + "force_run": force_run, |
423 | 431 | "fun_evals": fun_evals, |
424 | 432 | "fun_repeats": fun_repeats, |
425 | 433 | "horizon": horizon, |
@@ -521,21 +529,26 @@ def fun_control_init( |
521 | 529 | return fun_control |
522 | 530 |
|
523 | 531 |
|
524 | | -def _init_PREFIX() -> str: |
525 | | - """Initialize the PREFIX for the experiment name. |
| 532 | +def _init_prefix() -> str: |
| 533 | + """Initialize the prefix for the experiment name. |
| 534 | + Attempts to derive the prefix from the package version. If unsuccessful, |
| 535 | + defaults to '000'. |
526 | 536 |
|
527 | 537 | Returns: |
528 | | - PREFIX (str): |
529 | | - The PREFIX for the experiment name. |
| 538 | + str: The prefix for the experiment name. |
530 | 539 |
|
531 | 540 | Examples: |
532 | | - >>> from spotpython.utils.init import _init_PREFIX |
533 | | - >>> _init_PREFIX() |
| 541 | + >>> from spotpython.utils.init import _init_prefix |
| 542 | + >>> _init_prefix() |
534 | 543 | '00' |
535 | 544 | """ |
536 | | - # set the prefix to the actual date and time |
537 | | - PREFIX = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") |
538 | | - return PREFIX |
| 545 | + DEFAULT_PREFIX = "000" |
| 546 | + try: |
| 547 | + package_version = version("package_name") |
| 548 | + except PackageNotFoundError: |
| 549 | + package_version = DEFAULT_PREFIX |
| 550 | + |
| 551 | + return package_version |
539 | 552 |
|
540 | 553 |
|
541 | 554 | def setup_paths(tensorboard_clean) -> tuple: |
@@ -584,8 +597,11 @@ def setup_paths(tensorboard_clean) -> tuple: |
584 | 597 | now = datetime.datetime.now() |
585 | 598 | os.makedirs("runs_OLD", exist_ok=True) |
586 | 599 | # use [:-1] to remove "/" from the end of the path |
587 | | - TENSORBOARD_PATH_OLD = "runs_OLD/" + TENSORBOARD_PATH[:-1] + "_" + now.strftime("%Y_%m_%d_%H_%M_%S") |
| 600 | + TENSORBOARD_PATH_OLD = "runs_OLD/" + TENSORBOARD_PATH[:-1] + "_" + now.strftime("%Y_%m_%d_%H_%M_%S") + "_" + "0" |
588 | 601 | print(f"Moving TENSORBOARD_PATH: {TENSORBOARD_PATH} to TENSORBOARD_PATH_OLD: {TENSORBOARD_PATH_OLD}") |
| 602 | + # if TENSORBOARD_PATH_OLD already exists, change the name increasing the number at the end |
| 603 | + while os.path.exists(TENSORBOARD_PATH_OLD): |
| 604 | + TENSORBOARD_PATH_OLD = copy.deepcopy(TENSORBOARD_PATH_OLD[:-1] + str(int(TENSORBOARD_PATH_OLD[-1]) + 1)) |
589 | 605 | os.rename(TENSORBOARD_PATH[:-1], TENSORBOARD_PATH_OLD) |
590 | 606 |
|
591 | 607 | os.makedirs(TENSORBOARD_PATH, exist_ok=True) |
|
0 commit comments