-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexp_manager.py
More file actions
215 lines (171 loc) · 6.6 KB
/
exp_manager.py
File metadata and controls
215 lines (171 loc) · 6.6 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/python3
import yaml
import argparse
import os
import subprocess
import sys
import logging
import time
# TODO: use schema for more readbable and safer validation...
def parse_and_validate_config_single(config):
if not "name" in config["DA"].keys():
config["DA"]["name"] = None
return config
def parse_and_validate_config_multiple(config):
# DA/FE.name must contain at least one DA/FE technique.
def validation(key):
if not key in config.keys():
logging.error("No {} (multiple-mode)".format(key))
return False
if not isinstance(config[key], list):
config[key] = [config[key]]
if len(config[key]) == 0:
logging.error("{k} must include at least one available {k} (multiple-mode)".format(k=key))
return False
return True
def validation_w_name(key):
if not "name" in config[key].keys():
logging.error("No name key in {} (multiple-mode)".format(key))
return False
if not isinstance(config[key]["name"], list):
config[key]["name"] = [config[key]["name"]]
if len(config[key]["name"]) == 0:
logging.error("name in {k} must include at least one available {k} (multiple-mode)".format(k=key))
return False
return True
if not (validation("target_id")):
sys.exit(1)
if not (validation_w_name("DA") and validation_w_name("FE")):
sys.exit(1)
if not "repeat" in config.keys() or not int(config["repeat"]) >= 1:
logging.error("repeat must be > 1 (multiple-mode)")
sys.exit(1)
config["repeat"] = int(config["repeat"])
return config
def parse_and_validate_config(fname):
with open(fname) as file:
config = yaml.safe_load(file)
if not "exp_name" in config.keys():
logging.error("No exp_name key in {}".format(fname))
sys.exit(1)
if not "DA" in config.keys():
logging.error("No DA key in {}".format(fname))
sys.exit(1)
if not "seed" in config.keys():
config["seed"] = "default"
if not "mode" in config.keys():
config["mode"] = "single"
if config["mode"] == "single":
return parse_and_validate_config_single(config)
elif config["mode"] == "multiple":
return parse_and_validate_config_multiple(config)
logging.error("Invalid mode: {}".format(config["mode"]))
sys.exit(1)
def exec_DA(exp_name, DA, target, max_timeout, res_path, seed, build=True):
if os.path.exists(res_path):
logging.error("DA: Result already exists.")
sys.exit(1)
env = os.environ.copy()
env["DA"] = DA
env["TARGET_ID"] = target
if build:
res = subprocess.run("data_augmentation/container_build.sh", env=env)
if res.returncode != 0:
logging.error("DA container_build.sh failed")
sys.exit(1)
os.makedirs(res_path)
env["DA_MAX_TIMEOUT"] = str(max_timeout)
env["DA_SEED"] = seed
env["DA_OUTPUT"] = res_path
da_start_time = time.time()
res = subprocess.run("data_augmentation/container_run.sh", env=env)
da_end_time = time.time()
with open(res_path+"/da_time_external.txt", "w") as f:
f.write(str(int(da_end_time - da_start_time))+"\n")
if res.returncode != 0:
logging.error("DA container_run.sh failed")
sys.exit(1)
def exec_FE(exp_name, FE, target, da_timeout, da_path, base_path, build=True):
fe_path = base_path + "/" + str(da_timeout)
if not os.path.exists(da_path):
logging.error("FE: Not Found: DA Result ")
sys.exit(1)
os.makedirs(base_path, exist_ok=True)
os.makedirs(fe_path)
env = os.environ.copy()
env["FE"] = FE
env["TARGET_ID"] = target
env["DA_TIMEOUT"] = str(da_timeout)
env["DA_RESULT"] = da_path
env["FE_OUTPUT"] = fe_path
if build:
res = subprocess.run("feature_extraction/container_build.sh", env=env)
if res.returncode != 0:
logging.error("FE: container_build.sh failed")
sys.exit(1)
res = subprocess.run("feature_extraction/container_run.sh", env=env)
if res.returncode != 0:
logging.error("FE: container_run.sh failed")
sys.exit(1)
def run_single(config, build=True):
if config["DA"]["name"] is not None:
exec_DA(config["exp_name"],
config["DA"]["name"],
config["target_id"],
max(config["time"]),
config["DA"]["output"],
config["seed"],
build=build)
if not "FE" in config.keys():
sys.exit(0)
for da_timeout in config["time"]:
exec_FE(config["exp_name"],
config["FE"]["name"],
config["target_id"],
da_timeout,
config["DA"]["output"],
config["FE"]["output"],
build=build)
def run_multiple(config):
opath = config["output"]
os.makedirs(opath, exist_ok=True)
da_path = opath + "/DA/"
os.makedirs(da_path, exist_ok=True)
fe_path = opath + "/FE/"
os.makedirs(fe_path, exist_ok=True)
for da in config["DA"]["name"]:
for target in config["target_id"]:
for r in range(config["repeat"]):
da_res = da_path + "/" + da + "/" + target + "/" + str(r) + "/"
exec_DA(config["exp_name"],
da,
target,
max(config["time"]),
da_res,
config["seed"],
build=(True if r == 0 else False))
for fe in config["FE"]["name"]:
for da in config["DA"]["name"]:
for target in config["target_id"]:
for r in range(config["repeat"]):
d = da + "/" + target + "/" + str(r) + "/"
da_res = da_path + "/" + d
fe_res = fe_path + "/" + fe + "/" + d
for i, da_timeout in enumerate(config["time"]):
exec_FE(config["exp_name"],
fe,
target,
da_timeout,
da_res,
fe_res,
build=(True if r == 0 and i == 0 else False))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("config")
args = parser.parse_args()
config = parse_and_validate_config(args.config)
assert config["mode"] in ["single", "multiple"]
if config["mode"] == "single":
run_single(config)
elif config["mode"] == "multiple":
run_multiple(config)