-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_batch.py
More file actions
282 lines (240 loc) · 9.36 KB
/
Copy pathgenerate_batch.py
File metadata and controls
282 lines (240 loc) · 9.36 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
# gen_run_from_config.py
# Usage: python gen_run_from_config.py <batch_yaml> <target_config_yaml> <out_bash>
import sys, os, pathlib, json
try:
import yaml # pyyaml for reading only
except Exception:
print("Please `pip install pyyaml`", file=sys.stderr)
sys.exit(1)
# This updater edits TOP-LEVEL keys in the target config file (config.yaml)
UPDATE_CONFIG_FN = r"""update_config_inline() {
python - "$TARGET_CONFIG" <<'PY'
import os, sys
from ruamel.yaml import YAML
cfg_file = sys.argv[1]
yaml = YAML()
yaml.preserve_quotes = True
yaml.indent(mapping=2, sequence=4, offset=2)
with open(cfg_file, "r") as f:
data = yaml.load(f) or {}
def parse_csv(s):
s = (s or "").strip()
if not s: return []
out = []
for v in s.split(","):
v = v.strip()
if not v: continue
try:
out.append(int(v))
except ValueError:
pass
return out
env = os.environ
# Update top-level keys in the target config file
if "FRAME_GENERATOR" in env:
data["frame_generator"] = env.get("FRAME_GENERATOR")
if "RECON_MODEL" in env:
data["reconstruction_model"] = env.get("RECON_MODEL")
if "FRAME_ACCUMULATOR" in env:
data["frame_accumulator"] = env.get("FRAME_ACCUMULATOR")
if "GPS_TOLERANCE" in env:
try:
data["gps_tolerance"] = int(env.get("GPS_TOLERANCE"))
except Exception:
data["gps_tolerance"] = env.get("GPS_TOLERANCE")
# timewindows is top-level in your config.yaml; preserve other keys under timewindows
tw = data.get("timewindows", {}) or {}
tw_list = parse_csv(env.get("TW_LIST", ""))
tw_events = parse_csv(env.get("TW_EVENTS", ""))
if tw_list:
tw["list"] = tw_list
if tw_events:
tw["num_events"] = tw_events
data["timewindows"] = tw
with open(cfg_file, "w") as f:
yaml.dump(data, f)
print(f"[config] updated {cfg_file} (top-level) with FRAME_GENERATOR={env.get('FRAME_GENERATOR')} GPS_TOLERANCE={env.get('GPS_TOLERANCE')}")
PY
}
"""
# Baseline updater: unchanged behavior (writes/merges baseline config YAML)
UPDATE_BASELINE_FN = r"""update_baseline_inline() {
# ENV:
# BASELINE_NAME (required) e.g. sparse_event
# BASELINE_JSON (required) JSON object of overrides
# BASELINE_CFG_PATH (optional) explicit path to YAML
# BASELINE_DIR (optional) directory to search (default: ./baselines)
python - <<'PY'
import os, sys, json
from pathlib import Path
from ruamel.yaml import YAML
name = os.environ.get("BASELINE_NAME")
if not name:
print("[baseline] BASELINE_NAME is required", file=sys.stderr); sys.exit(2)
raw = os.environ.get("BASELINE_JSON","").strip()
if not raw:
print(f"[baseline] no options for {name}; skipping", file=sys.stderr); sys.exit(0)
try:
overrides = json.loads(raw)
if not isinstance(overrides, dict):
raise ValueError("BASELINE_JSON must be a JSON object")
except Exception as e:
print(f"[baseline] invalid BASELINE_JSON: {e}", file=sys.stderr); sys.exit(2)
explicit = os.environ.get("BASELINE_CFG_PATH", "").strip()
base_dir = os.environ.get("BASELINE_DIR", "").strip() or "baselines"
candidates = []
if explicit:
candidates.append(Path(explicit))
else:
for d in [base_dir, "baselines", "baseline_configs"]:
candidates.append(Path(d) / f"{name}.yaml")
candidates.append(Path(d) / f"{name}.yml")
target = None
for p in candidates:
if p.exists():
target = p; break
if target is None:
target = candidates[0]
yaml = YAML()
yaml.preserve_quotes = True
yaml.indent(mapping=2, sequence=4, offset=2)
data = {}
if target.exists():
try:
with target.open("r") as f:
data = yaml.load(f) or {}
except Exception:
data = {}
# shallow merge (override top-level keys)
for k, v in overrides.items():
data[k] = v
target.parent.mkdir(parents=True, exist_ok=True)
with target.open("w") as f:
yaml.dump(data, f)
print(f"[baseline] updated {target} with {overrides}")
PY
}
"""
def csv_join(vals):
return ",".join(str(v) for v in vals) if vals else ""
def _norm_baseline_cfg(bc):
"""
Normalize baseline_config to {name: {overrides}}.
Accepts either:
- {"sparse_event": {...}, "lens": {...}}
- [{"sparse_event": {...}}, {"lens": {...}}]
"""
if not bc:
return {}
if isinstance(bc, dict):
return {str(k): (v or {}) for k, v in bc.items()}
if isinstance(bc, list):
out = {}
for item in bc:
if isinstance(item, dict):
for k, v in item.items():
out[str(k)] = v or {}
return out
return {}
def main():
# Expect three args: batch_yaml, target_config_yaml, out_bash
if len(sys.argv) != 4:
print("Usage: python gen_run_from_config.py <batch_yaml> <target_config_yaml> <out_bash>")
sys.exit(1)
batch_cfg_path = pathlib.Path(sys.argv[1]).resolve()
target_cfg_path = pathlib.Path(sys.argv[2]).resolve()
out_bash = pathlib.Path(sys.argv[3]).resolve()
try:
batch_cfg = yaml.safe_load(batch_cfg_path.read_text()) or {}
except Exception as e:
print(f"Failed to read {batch_cfg_path}: {e}", file=sys.stderr)
sys.exit(1)
experiments = batch_cfg.get("batch_experiments", [])
if not experiments:
print("No 'batch_experiments' found in batch YAML.", file=sys.stderr)
sys.exit(1)
lines = []
lines.append("#!/usr/bin/env bash")
lines.append("set -euo pipefail")
# Set TARGET_CONFIG to the target config file (this is the file that will be edited)
lines.append(f'TARGET_CONFIG="{target_cfg_path}"')
lines.append("")
# helper functions
lines.append(UPDATE_CONFIG_FN)
lines.append("")
lines.append(UPDATE_BASELINE_FN)
lines.append("")
exp_idx = 0
for exp in experiments:
if not isinstance(exp, dict):
continue
if "dataset" not in exp or "reference" not in exp:
continue
exp_idx += 1
dataset = str(exp["dataset"])
reference = str(exp["reference"])
queries = [str(q) for q in (exp.get("queries", []) or [])]
baselines = [str(b) for b in (exp.get("baselines", []) or [])]
econf = exp.get("config", {}) or {}
fg = str(econf.get("frame_generator", "frames"))
recon = str(econf.get("reconstruction_model", "e2vid"))
accum = str(econf.get("frame_accumulator", "polarity"))
tw = econf.get("timewindows", {}) or {}
tw_list = csv_join(tw.get("list", []))
tw_events = csv_join(tw.get("num_events", []))
gps_val = econf.get("gps_tolerance", None)
tw = econf.get("timewindows", {}) or {}
if gps_val in (None, "", []):
gps_val = tw.get("gps_tolerance", None)
gps_env = ""
if gps_val not in (None, ""):
try:
gps_env = f' GPS_TOLERANCE="{int(gps_val)}"'
except Exception:
s = str(gps_val).strip()
if s != "":
gps_env = f' GPS_TOLERANCE="{s}"' # last resort, still non-empty
bc_map = _norm_baseline_cfg(exp.get("baseline_config", {}) or {})
lines.append(f'echo "=== Experiment {exp_idx}: {dataset} (ref={reference}) ==="')
# IMPORTANT: this update_config_inline call now targets TARGET_CONFIG (config.yaml),
# so the top-level keys in config.yaml will be updated (timewindows, frame_generator, gps_tolerance, etc.)
lines.append(
f'FRAME_GENERATOR="{fg}" TW_LIST="{tw_list}" TW_EVENTS="{tw_events}" '
f'RECON_MODEL="{recon}" FRAME_ACCUMULATOR="{accum}"{gps_env} '
f'update_config_inline "{target_cfg_path}"'
)
lines.append("")
for q in queries:
for b in baselines:
overrides = bc_map.get(b, {}) or {}
# If overrides has a 'method' key which is a list, expand into multiple runs
if isinstance(overrides.get("method"), list):
methods = overrides["method"]
# keep other keys unchanged
other_overrides = {k: v for k, v in overrides.items() if k != "method"}
for m in methods:
# produce override where method is a single string (not a list)
ov = dict(other_overrides)
ov["method"] = m
ov_json = json.dumps(ov)
# update baseline config for this single-method run
lines.append(f'BASELINE_NAME="{b}" BASELINE_JSON=\'{ov_json}\' update_baseline_inline')
cmd = f'python eventlab_run.py "{b}" "{dataset}" "{reference}" "{q}"'
lines.append(f'echo "+ {cmd} (method={m})"')
lines.append(cmd)
else:
# normal path: either no overrides, or overrides without a method-list
if overrides:
ov_json = json.dumps(overrides)
lines.append(f'BASELINE_NAME="{b}" BASELINE_JSON=\'{ov_json}\' update_baseline_inline')
cmd = f'python eventlab_run.py "{b}" "{dataset}" "{reference}" "{q}"'
lines.append(f'echo "+ {cmd}"')
lines.append(cmd)
lines.append("")
out = "\n".join(lines) + "\n"
out_bash.write_text(out)
os.chmod(out_bash, 0o755)
print(f"Wrote {out_bash}")
if __name__ == "__main__":
main()