Download scripts/evaluate.py from Upwork-AIML/Conditional_Causal_Difficulty_for_LLMs: direct link, hf CLI and curl.
- Browser
- Download file 7.06 kB
-
https://huggingface.co/Upwork-AIML/Conditional_Causal_Difficulty_for_LLMs/resolve/main/scripts/evaluate.py
- Command line
-
hf download hf://Upwork-AIML/Conditional_Causal_Difficulty_for_LLMs/scripts/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/Upwork-AIML/Conditional_Causal_Difficulty_for_LLMs/resolve/main/scripts/evaluate.py
7.06 kB
| #!/usr/bin/env python | |
| """Evaluate SCE vs baselines on a held-out split. | |
| Each run is saved to outputs/runs/{run_id}/ with: | |
| table2_main.md main results table | |
| table3_ablation.md differencing vs concat ablation | |
| scatter_dhat_vs_d.png predicted vs measured difficulty scatter | |
| eval_summary.json full metrics dict | |
| manifest.json config, git info, timestamps, all metrics | |
| A one-line summary is appended to outputs/runs.jsonl (master log). | |
| Usage: | |
| python scripts/evaluate.py | |
| python scripts/evaluate.py --backbone hashing --seed 7 | |
| python scripts/evaluate.py --run-id 20260624_120000_abcd # tie to a train run | |
| """ | |
| import argparse | |
| import sys | |
| import time | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) | |
| from causal_difficulty.config import load_config | |
| from causal_difficulty.eval.reports import evaluate, write_results_md | |
| from causal_difficulty.io_utils import format_dataset_line, labeled_pairs_path | |
| from causal_difficulty.run_log import RunLogger, experiment_name_from_config, make_run_id | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__, | |
| formatter_class=argparse.RawDescriptionHelpFormatter) | |
| parser.add_argument("--config", default="default") | |
| parser.add_argument("--backbone", default=None, | |
| help="hashing (default) | openai_embed | hf_lora") | |
| parser.add_argument("--seed", type=int, default=7) | |
| parser.add_argument("--run-id", default=None, | |
| help="Reuse an existing run ID (e.g. from train.py output).") | |
| parser.add_argument("--data-dir", default=None, | |
| help="Override the data directory where labeled-pairs JSONL files are read from.") | |
| parser.add_argument("--outputs-dir", default=None, | |
| help="Override the outputs directory for runs and reports.") | |
| parser.add_argument("--cache-dir", default=None, | |
| help="Override the cache directory (embeddings, etc.).") | |
| parser.add_argument("--checkpoint", default=None, | |
| help="Path to sce_heads.pt to load instead of re-training SCE " | |
| "(e.g. outputs/gsmic_openai/runs/<run-id>/sce_heads.pt). " | |
| "All baselines are still fitted on the train split.") | |
| parser.add_argument("--sce-only", action="store_true", | |
| help="Skip all baselines; evaluate SCE heads only.") | |
| parser.add_argument("--split-seed", type=int, default=None, | |
| help="Seed for the train/holdout job split (default: --seed). " | |
| "Set it to hold the split fixed while --seed varies the " | |
| "model init; must match train.py's --split-seed.") | |
| parser.add_argument("--allow-unverified-checkpoint", action="store_true", | |
| help="Score a --checkpoint that cannot be shown disjoint from " | |
| "this holdout (no split sidecar, or an overlapping one). " | |
| "Off by default.") | |
| args = parser.parse_args() | |
| cfg = load_config(args.config) | |
| if args.backbone: | |
| cfg.encoder["backbone"] = args.backbone | |
| if args.data_dir: | |
| cfg.paths["data_dir"] = str(Path(args.data_dir).expanduser().resolve()) | |
| if args.outputs_dir: | |
| cfg.paths["outputs_dir"] = str(Path(args.outputs_dir).expanduser().resolve()) | |
| if args.cache_dir: | |
| cfg.paths["cache_dir"] = str(Path(args.cache_dir).expanduser().resolve()) | |
| out_dir = Path(cfg.paths.outputs_dir) | |
| experiment = experiment_name_from_config(args.config) | |
| run_id = args.run_id or make_run_id(experiment) | |
| logger = RunLogger(run_id, out_dir, cfg=cfg) | |
| print(f"Run ID : {run_id}") | |
| print(f"Experiment: {experiment}") | |
| print(f"Backbone : {cfg.encoder.backbone}") | |
| print(f"Data : {labeled_pairs_path(cfg, args.config)}") | |
| print(f"Output : {logger.run_dir}") | |
| print() | |
| checkpoint = Path(args.checkpoint).expanduser().resolve() if args.checkpoint else None | |
| t0 = time.time() | |
| summary = evaluate(cfg, config_name=args.config, seed=args.seed, out_dir=logger.run_dir, | |
| checkpoint=checkpoint, sce_only=args.sce_only, | |
| split_seed=args.split_seed, | |
| allow_unverified_checkpoint=args.allow_unverified_checkpoint) | |
| elapsed = time.time() - t0 | |
| print(f"Pairs : {summary['n_pairs']}") | |
| if summary.get("dataset"): | |
| print(f"Dataset : {format_dataset_line(summary['dataset'])}") | |
| print() | |
| # also copy the main outputs to flat outputs/ for convenience | |
| for fname in ("table2_main.md", "table3_ablation.md", | |
| "scatter_dhat_vs_d.png", "eval_summary.json"): | |
| src = logger.run_dir / fname | |
| if src.exists(): | |
| import shutil | |
| shutil.copy2(src, out_dir / fname) | |
| import math | |
| def _fmt(x): | |
| return " n/a " if x is None or (isinstance(x, float) and math.isnan(x)) \ | |
| else f"{x:7.3f}" | |
| # print D_avg table | |
| results = summary["results"] | |
| col_w = 33 | |
| header = (f"{'Method':<{col_w}} {'Spearman':>9} {'MAE':>7}" | |
| f" {'AUROC':>7} {'NDCG@10':>9} {'|rho_C|':>9}") | |
| print("── vs D_avg (mean difficulty) ──") | |
| print(header) | |
| print("-" * len(header)) | |
| for name, m in results.items(): | |
| print(f"{name:<{col_w}} {_fmt(m['spearman'])} {_fmt(m['mae'])} {_fmt(m['auroc'])} " | |
| f"{_fmt(m['ndcg@10'])} {_fmt(m['deconfound_|rho_C|'])}") | |
| # print D_wc table | |
| results_wc = summary.get("results_wc", {}) | |
| if results_wc: | |
| print() | |
| print("── vs D_wc (worst-case difficulty) ──") | |
| print(header) | |
| print("-" * len(header)) | |
| for name, m in results_wc.items(): | |
| print(f"{name:<{col_w}} {_fmt(m['spearman'])} {_fmt(m['mae'])} {_fmt(m['auroc'])} " | |
| f"{_fmt(m['ndcg@10'])} {_fmt(m['deconfound_|rho_C|'])}") | |
| # build eval summary for the manifest | |
| eval_summary = { | |
| "seed": args.seed, | |
| "elapsed_seconds": round(elapsed, 1), | |
| "n_holdout_jobs": summary["n_holdout_jobs"], | |
| "n_train_jobs": summary["n_train_jobs"], | |
| "n_pairs": summary["n_pairs"], | |
| "results": { | |
| name: {k: round(v, 4) for k, v in m.items() | |
| if v is not None and isinstance(v, float)} | |
| for name, m in results.items() | |
| }, | |
| } | |
| manifest = logger.save(phase="eval", extra={ | |
| "dataset": summary.get("dataset", {}), | |
| "eval": eval_summary, | |
| }) | |
| Path("experiments").mkdir(exist_ok=True) | |
| write_results_md(Path("experiments/RESULTS.md"), summary, | |
| backbone=cfg.encoder.backbone) | |
| print(f"\nRun ID : {run_id}") | |
| print(f"Manifest -> {manifest}") | |
| print(f"Outputs -> {logger.run_dir}/") | |
| print(f"Elapsed : {elapsed:.1f}s") | |
| print(f"\nAll runs log: {out_dir / 'runs.jsonl'}") | |
| if __name__ == "__main__": | |
| main() | |