Download code/experiments/re_evaluate.py from DeepAuto-AI/MacroLens: direct link, hf CLI and curl.
- Browser
- Download file 3.09 kB
-
https://huggingface.co/datasets/DeepAuto-AI/MacroLens/resolve/main/code/experiments/re_evaluate.py
- Command line
-
hf download hf://datasets/DeepAuto-AI/MacroLens/code/experiments/re_evaluate.py
-
curl -L -o re_evaluate.py https://huggingface.co/datasets/DeepAuto-AI/MacroLens/resolve/main/code/experiments/re_evaluate.py
3.09 kB
| """Re-score saved predictions with the current eval.py (no re-running models). | |
| Reads every ``experiments/predictions/<method>_<task>_seed<seed>[_setX].pkl``, | |
| calls ``ml.score`` with the current ``eval.py``, writes a fresh | |
| ``RunRecord`` JSON to ``experiments/results/canon_reeval_<timestamp>.json``. | |
| Use this whenever ``eval.py`` is patched: regenerates metrics from cached | |
| predictions in ~seconds, no LLM/GPU spend. | |
| """ | |
| from __future__ import annotations | |
| import argparse, json, pickle, pathlib, sys | |
| import numpy as np | |
| import pandas as pd | |
| def main(): | |
| sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) | |
| from whatif_bench import config | |
| from whatif_bench import macrolens as ml | |
| pred_dir = pathlib.Path(__file__).parent / "predictions" | |
| out_dir = pathlib.Path(__file__).parent / "results" | |
| out_path = out_dir / f"canon_reeval_{pd.Timestamp.utcnow().strftime('%Y%m%dT%H%M%SZ')}.json" | |
| records = [] | |
| pkls = sorted(pred_dir.glob("*.pkl")) | |
| print(f"re-evaluating {len(pkls)} prediction files") | |
| for p in pkls: | |
| with open(p, "rb") as f: | |
| d = pickle.load(f) | |
| task = d["task"] | |
| meta_test = d["meta_test"] | |
| y_test = d["y_test"] | |
| y_pred = d["y_pred"] | |
| # cluster keys | |
| if task == "T4": | |
| ck = meta_test["scenario_id"].values if "scenario_id" in meta_test.columns else None | |
| elif task == "T7": | |
| ck = meta_test["address"].values if "address" in meta_test.columns else None | |
| elif "ticker" in meta_test.columns: | |
| ck = meta_test["ticker"].values | |
| else: | |
| ck = None | |
| kw = {"cluster_keys": ck} | |
| if task == "T1" and "close_last" in meta_test.columns: | |
| kw["close_last"] = meta_test["close_last"].values | |
| try: | |
| metrics = ml.score(task, y_test, y_pred, **kw) | |
| except Exception as exc: | |
| print(f" {p.name}: score raised {type(exc).__name__}: {exc}") | |
| continue | |
| # Build a record (mirroring RunRecord essentials) | |
| rec = { | |
| "method_id": d["method_id"], | |
| "task": task, | |
| "granularity": d.get("granularity", "daily"), | |
| "seed": d.get("seed", 42), | |
| "status": "ok", | |
| "ablation_setting": d.get("ablation_setting"), | |
| "timestamp": pd.Timestamp.utcnow().isoformat(), | |
| "metrics": {k: (v.model_dump() if hasattr(v,"model_dump") else v) for k,v in metrics.items()}, | |
| } | |
| records.append(rec) | |
| primary = {"T1":"mse","T2":"median_ape","T3":"overall_mape","T4":"return_mae_pct", | |
| "T5":"median_ape","T6":"overall_mape","T7":"rent_MAPE"}.get(task) | |
| pv = (metrics or {}).get(primary) | |
| pv = pv.value if pv is not None and hasattr(pv, "value") else None | |
| print(f" {d['method_id']:18s} {task} setting={d.get('ablation_setting')} {primary}={pv}") | |
| out_path.write_text(json.dumps(records, indent=2, default=str)) | |
| print(f"\nwrote {len(records)} re-evaluated records to {out_path}") | |
| if __name__ == "__main__": | |
| main() | |