File size: 1,296 Bytes
69def8e | 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 | import json
import os
import datetime
import subprocess
def get_git_hash() -> str:
try:
res = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], stderr=subprocess.DEVNULL)
return res.decode('utf-8').strip()
except Exception:
return "unknown"
class Tracker:
def __init__(self, log_file="outputs/runs.json"):
self.log_file = log_file
os.makedirs(os.path.dirname(self.log_file), exist_ok=True)
if not os.path.exists(self.log_file):
with open(self.log_file, 'w') as f:
json.dump([], f)
def log_run(self, run_id, config_name, data_version, is_sweep, threshold, metrics, note):
with open(self.log_file, 'r') as f:
runs = json.load(f)
run_data = {
"run_id": run_id,
"timestamp": datetime.datetime.now().isoformat(),
"commit": get_git_hash(),
"config": config_name,
"data_version": data_version,
"is_sweep": is_sweep,
"threshold": threshold,
"metrics": metrics,
"note": note
}
runs.append(run_data)
with open(self.log_file, 'w') as f:
json.dump(runs, f, indent=2)
return run_data
|