bdfd9-v3-fabric-defect-classification / scripts /summarize_validation_runs.py
ImtiazulIslam's picture
Optimize and isolate 25-run validation matrix configuration
07a09ab verified
Raw History Blame Contribute Delete
1.86 kB
#!/usr/bin/env python3
"""Aggregate runs by architecture using validation-only metrics."""
from pathlib import Path
import json,csv,collections,statistics
ROOT=Path(__file__).resolve().parents[1]
config=json.loads((ROOT/"experiments"/"matrix_config.json").read_text())
expected={f"{model}_seed{seed}_e{config['epochs']}_s{config['size']}_b{config['batch_size']}_w{config['workers']}_leakage_controlled" for model in config["models"] for seed in config["seeds"]}
rows=[]
for run_name in sorted(expected):
p=ROOT/"experiments"/run_name/"results.json"
if not p.is_file():continue
r=json.loads(p.read_text());m=r["validation_metrics_threshold_tuned"]
rows.append({"run":p.parent.name,"model":r["model"],"seed":int(r["seed"]),"best_epoch":r["best_checkpoint_epoch"],"val_macro_f1":m["macro_f1"],"val_micro_f1":m["micro_f1"],"val_hamming_loss":m["hamming_loss"],"val_subset_accuracy":m["subset_accuracy"]})
rows.sort(key=lambda x:(x["model"],x["seed"]))
with (ROOT/"experiments"/"validation_runs.csv").open("w",newline="") as f:
if rows:
w=csv.DictWriter(f,fieldnames=list(rows[0]));w.writeheader();w.writerows(rows)
groups=collections.defaultdict(list)
for r in rows:groups[r["model"]].append(r["val_macro_f1"])
summary=[]
for model,vals in groups.items():
summary.append({"model":model,"n_seeds":len(vals),"val_macro_f1_mean":statistics.mean(vals),"val_macro_f1_sd":statistics.stdev(vals) if len(vals)>1 else 0.0,"val_macro_f1_min":min(vals),"val_macro_f1_max":max(vals)})
summary.sort(key=lambda x:x["val_macro_f1_mean"],reverse=True)
with (ROOT/"experiments"/"validation_model_comparison.csv").open("w",newline="") as f:
if summary:
w=csv.DictWriter(f,fieldnames=list(summary[0]));w.writeheader();w.writerows(summary)
print("VALIDATION-ONLY MODEL RANKING")
for r in summary:print(r)
if not summary:print("No completed experiment results found.")