forked from yvchao/cvar_sensing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexp_helper.py
66 lines (58 loc) · 2.03 KB
/
exp_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import subprocess
import sys
from pathlib import Path
from .exp_config import conda_path
def get_conda_env():
# Get current python executable's conda environment name.
python_executable_path = sys.executable
env_path = os.path.dirname(os.path.dirname(python_executable_path))
if f"{conda_path}" in env_path:
env_name = os.path.basename(env_path)
return env_name
else:
return "Not in a Conda environment"
def get_cmd(
script: Path | str,
venv: str,
gpu: int | None = 0,
epochs: int | None = 200,
parameters: dict[str, int | float] = {},
):
script = Path(script)
env = get_conda_env()
if env != venv:
cmd = f"{conda_path} run -n {venv} python".split(" ")
else:
cmd = ["python"]
cmd += [script.as_posix()]
if gpu is not None:
cmd += ["--gpu", f"{gpu}"]
if epochs is not None:
cmd += ["--epochs", f"{epochs}"]
for k, v in parameters.items():
cmd += [f"--{k}", f"{v}"]
return cmd
def run_script(cmd: list[str], exp_subdir: str):
# Get the experiment python script file stem name.
script_base = Path([x for x in cmd if x.endswith(".py")][0]).stem
# Get any non gpu/epoch parameters & their values to append.
params = "_".join(
[
f"{x.replace('--', '')}={cmd[idx + 1]}"
for idx, x in enumerate(cmd)
if x.startswith("--")
if x not in ("--gpu", "--epochs")
]
)
# Get working directory - assumed to be the parent directory of this script.
wd = Path(__file__).parent / exp_subdir
# Construct the log file path using all above details.
log_file = wd / Path(f"{script_base + ('_' + params if params != '' else '')}.log")
log_file.touch(exist_ok=True)
# Print user info and run the script.
print(f" Running command: {' '.join(cmd)}")
print(f" In working directory: {wd}")
print(f" Output logged to: {log_file}")
with open(log_file, "w") as f:
return subprocess.Popen(cmd, stdout=f, stderr=f)