Skip to content

Commit

Permalink
make_exp_dir incr exp dir if exp already exists to find a new exp dir…
Browse files Browse the repository at this point in the history
… instead of erroring
  • Loading branch information
madeline-scyphers committed Oct 12, 2023
1 parent 085127a commit 0b85da6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
36 changes: 26 additions & 10 deletions boa/wrappers/wrapper_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def make_experiment_dir(
to ensure uniqueness
exist_ok
Whether it is ok if the directory already exists or not
(will throw an error if set to False and it already exists)
(will create a new directory with a .1, .2 etc appended to the name if the directory already exists)
Returns
-------
Expand All @@ -297,29 +297,45 @@ def make_experiment_dir(
"`make_experiment_dir` must take either a `output_dir` and `experiment_name` "
"or an `experiment_dir`, not both and not neither."
)
if experiment_dir:
return _mk_exp_dir_from_exp_dir(exp_dir=experiment_dir, append_timestamp=append_timestamp, exist_ok=exist_ok)
return _mk_exp_dir_from_output_dir(
output_dir=output_dir, experiment_name=experiment_name, append_timestamp=append_timestamp, exist_ok=exist_ok
)
retries = 0
while True:
try:
add_on = f".{retries}" if retries else ""
if experiment_dir:
return _mk_exp_dir_from_exp_dir(
exp_dir=experiment_dir, append_timestamp=append_timestamp, exist_ok=exist_ok, add_on=add_on
)
return _mk_exp_dir_from_output_dir(
output_dir=output_dir,
experiment_name=experiment_name,
append_timestamp=append_timestamp,
exist_ok=exist_ok,
add_on=add_on,
)
except FileExistsError:
retries += 1


def _mk_exp_dir_from_output_dir(
output_dir: PathLike, experiment_name: str = "", append_timestamp: bool = True, exist_ok: bool = False
output_dir: PathLike, experiment_name: str = "", append_timestamp: bool = True, exist_ok: bool = False, add_on=""
):
ts = get_dt_now_as_str() if append_timestamp else ""
exp_name = "_".join(name for name in [experiment_name, ts] if name)
exp_name = "_".join(name for name in [experiment_name, ts] if name) + add_on
ex_dir = pathlib.Path(output_dir).expanduser() / exp_name
ex_dir.mkdir(exist_ok=exist_ok)
return ex_dir


def _mk_exp_dir_from_exp_dir(exp_dir: PathLike, append_timestamp: bool = True, exist_ok: bool = False):
def _mk_exp_dir_from_exp_dir(exp_dir: PathLike, append_timestamp: bool = True, exist_ok: bool = False, add_on=""):
exp_dir = pathlib.Path(exp_dir)
output_dir = exp_dir.parent
experiment_name = exp_dir.name
return _mk_exp_dir_from_output_dir(
output_dir=output_dir, experiment_name=experiment_name, append_timestamp=append_timestamp, exist_ok=exist_ok
output_dir=output_dir,
experiment_name=experiment_name,
append_timestamp=append_timestamp,
exist_ok=exist_ok,
add_on=add_on,
)


Expand Down
12 changes: 12 additions & 0 deletions tests/1unit_tests/test_wrapper_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from boa import make_experiment_dir


def test_make_new_exp_dir_when_exp_dir_already_exists(tmp_path):
exp_name = "test_exp"
kw = dict(output_dir=tmp_path, experiment_name=exp_name, append_timestamp=False, exist_ok=False)
dirs = set()
for _ in range(10):
exp_dir = make_experiment_dir(**kw)
dirs.add(exp_dir)
assert exp_dir.exists()
assert len(dirs) == 10

4 comments on commit 0b85da6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
__main__.py90594%83, 135, 166, 211, 219
async_opt.py1061883%137, 182, 189, 194, 212–213, 219–220, 222–223, 225, 232–237, 241
ax_instantiation_utils.py47197%123
controller.py71297%73, 166
definitions.py80100% 
instantiation_base.py40197%39
metaclasses.py72395%50–51, 57
plot.py12120%15, 17–18, 20–21, 24, 27, 34, 40–41, 44–45
plotting.py1412880%51–52, 59, 94–95, 251–253, 301–305, 345, 429–432, 434–441, 443, 448
registry.py80100% 
runner.py51492%52, 86–88
scheduler.py811482%38, 128–131, 138–139, 153, 160–161, 168–169, 264–265
storage.py1241587%83, 115–117, 177, 195, 273–281
template.py22577%33–35, 40–41
utils.py992079%179, 193–194, 219, 229–233, 235–237, 239, 241, 245–250
config
   __main__.py00100% 
   config.py3073488%33, 211, 217, 221, 223–224, 226, 282, 409, 412, 420–421, 429, 603, 605, 607, 613–617, 619, 690, 723, 744, 751–752, 760, 773, 783–784, 796, 821, 824
   converters.py751382%14, 22, 41, 46–48, 51–52, 65, 67, 71, 79, 95
metrics
   metric_funcs.py34488%58, 80–81, 83
   metrics.py1011585%127, 304–305, 310–311, 314, 320, 331–333, 337–341
   modular_metric.py1202083%38–41, 43–50, 64, 131, 142, 184, 235–236, 255–256
   synthetic_funcs.py39489%31, 35, 58, 65
scripts
   moo.py30196%44
   run_branin.py34197%56
   script_wrappers.py31293%57–58
   synth_func_cli.py210100% 
wrappers
   base_wrapper.py1953084%63–64, 89–92, 94, 102, 110, 115, 147, 156, 167, 215–216, 218, 267, 269, 271, 275, 330, 402, 416, 589, 591–594, 602, 612
   script_wrapper.py103793%193, 204–205, 273, 279, 327, 332
   synthetic_wrapper.py16287%16, 28
   wrapper_utils.py147993%147–148, 244, 296, 420–422, 430–431
TOTAL222527087% 

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
__main__.py90594%83, 135, 166, 211, 219
async_opt.py1061883%137, 182, 189, 192, 212–213, 219–220, 222–223, 225, 232–237, 241
ax_instantiation_utils.py47197%123
controller.py71297%73, 166
definitions.py80100% 
instantiation_base.py40197%39
metaclasses.py72395%50–51, 57
plot.py12120%15, 17–18, 20–21, 24, 27, 34, 40–41, 44–45
plotting.py1412880%51–52, 59, 94–95, 251–253, 301–305, 345, 429–432, 434–441, 443, 448
registry.py80100% 
runner.py51492%52, 86–88
scheduler.py811482%38, 128–131, 138–139, 153, 160–161, 168–169, 264–265
storage.py1241587%83, 115–117, 177, 195, 273–281
template.py22577%33–35, 40–41
utils.py992079%179, 193–194, 219, 229–233, 235–237, 239, 241, 245–250
config
   __main__.py00100% 
   config.py3073488%33, 211, 217, 221, 223–224, 226, 282, 409, 412, 420–421, 429, 603, 605, 607, 613–617, 619, 690, 723, 744, 751–752, 760, 773, 783–784, 796, 821, 824
   converters.py751382%14, 22, 41, 46–48, 51–52, 65, 67, 71, 79, 95
metrics
   metric_funcs.py34488%58, 80–81, 83
   metrics.py1011585%127, 304–305, 310–311, 314, 320, 331–333, 337–341
   modular_metric.py1202083%38–41, 43–50, 64, 131, 142, 184, 235–236, 255–256
   synthetic_funcs.py39489%31, 35, 58, 65
scripts
   moo.py30196%44
   run_branin.py34197%56
   script_wrappers.py31293%57–58
   synth_func_cli.py210100% 
wrappers
   base_wrapper.py1953084%63–64, 89–92, 94, 102, 110, 115, 147, 156, 167, 215–216, 218, 267, 269, 271, 275, 330, 402, 416, 589, 591–594, 602, 612
   script_wrapper.py103793%193, 204–205, 273, 279, 327, 332
   synthetic_wrapper.py16287%16, 28
   wrapper_utils.py147993%147–148, 244, 296, 420–422, 430–431
TOTAL222527087% 

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
__main__.py90594%83, 135, 166, 211, 219
async_opt.py1061883%137, 182, 189, 194, 212–213, 219–220, 222–223, 225, 232–237, 241
ax_instantiation_utils.py47197%123
controller.py71297%73, 166
definitions.py80100% 
instantiation_base.py40197%39
metaclasses.py72395%50–51, 57
plot.py12120%15, 17–18, 20–21, 24, 27, 34, 40–41, 44–45
plotting.py1412880%51–52, 59, 94–95, 251–253, 301–305, 345, 429–432, 434–441, 443, 448
registry.py80100% 
runner.py51492%52, 86–88
scheduler.py811482%38, 128–131, 138–139, 153, 160–161, 168–169, 264–265
storage.py1241587%83, 115–117, 177, 195, 273–281
template.py22577%33–35, 40–41
utils.py992079%179, 193–194, 219, 229–233, 235–237, 239, 241, 245–250
config
   __main__.py00100% 
   config.py3073488%33, 211, 217, 221, 223–224, 226, 282, 409, 412, 420–421, 429, 603, 605, 607, 613–617, 619, 690, 723, 744, 751–752, 760, 773, 783–784, 796, 821, 824
   converters.py751382%14, 22, 41, 46–48, 51–52, 65, 67, 71, 79, 95
metrics
   metric_funcs.py34488%58, 80–81, 83
   metrics.py1011585%127, 304–305, 310–311, 314, 320, 331–333, 337–341
   modular_metric.py1202083%38–41, 43–50, 64, 131, 142, 184, 235–236, 255–256
   synthetic_funcs.py39489%31, 35, 58, 65
scripts
   moo.py30196%44
   run_branin.py34197%56
   script_wrappers.py31293%57–58
   synth_func_cli.py210100% 
wrappers
   base_wrapper.py1953084%63–64, 89–92, 94, 102, 110, 115, 147, 156, 167, 215–216, 218, 267, 269, 271, 275, 330, 402, 416, 589, 591–594, 602, 612
   script_wrapper.py103793%193, 204–205, 273, 279, 327, 332
   synthetic_wrapper.py16287%16, 28
   wrapper_utils.py147993%147–148, 244, 296, 420–422, 430–431
TOTAL222527087% 

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
__main__.py90594%83, 135, 166, 211, 219
async_opt.py1061883%137, 182, 189, 192, 212–213, 219–220, 222–223, 225, 232–237, 241
ax_instantiation_utils.py47197%123
controller.py71297%73, 166
definitions.py80100% 
instantiation_base.py40197%39
metaclasses.py72395%50–51, 57
plot.py12120%15, 17–18, 20–21, 24, 27, 34, 40–41, 44–45
plotting.py1412880%51–52, 59, 94–95, 251–253, 301–305, 345, 429–432, 434–441, 443, 448
registry.py80100% 
runner.py51492%52, 86–88
scheduler.py811482%38, 128–131, 138–139, 153, 160–161, 168–169, 264–265
storage.py1241587%83, 115–117, 177, 195, 273–281
template.py22577%33–35, 40–41
utils.py992079%179, 193–194, 219, 229–233, 235–237, 239, 241, 245–250
config
   __main__.py00100% 
   config.py3073488%33, 211, 217, 221, 223–224, 226, 282, 409, 412, 420–421, 429, 603, 605, 607, 613–617, 619, 690, 723, 744, 751–752, 760, 773, 783–784, 796, 821, 824
   converters.py751382%14, 22, 41, 46–48, 51–52, 65, 67, 71, 79, 95
metrics
   metric_funcs.py34488%58, 80–81, 83
   metrics.py1011585%127, 304–305, 310–311, 314, 320, 331–333, 337–341
   modular_metric.py1202083%38–41, 43–50, 64, 131, 142, 184, 235–236, 255–256
   synthetic_funcs.py39489%31, 35, 58, 65
scripts
   moo.py30196%44
   run_branin.py34197%56
   script_wrappers.py31293%57–58
   synth_func_cli.py210100% 
wrappers
   base_wrapper.py1953084%63–64, 89–92, 94, 102, 110, 115, 147, 156, 167, 215–216, 218, 267, 269, 271, 275, 330, 402, 416, 589, 591–594, 602, 612
   script_wrapper.py103793%193, 204–205, 273, 279, 327, 332
   synthetic_wrapper.py16287%16, 28
   wrapper_utils.py147993%147–148, 244, 296, 420–422, 430–431
TOTAL222527087% 

Please sign in to comment.