File size: 1,635 Bytes
fd5366a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2026 CLSA contributors
# SPDX-License-Identifier: MIT
"""Shared runner for experiment scripts: provenance, config, blocking."""
from __future__ import annotations

import argparse
import inspect
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))

from clsa.util import load_config, write_result  # noqa: E402


def run(name: str, fn, config_name: str = "controller", notes: str = "") -> dict:
    ap = argparse.ArgumentParser()
    ap.add_argument("--model", default="primary")
    args = ap.parse_args()
    cfg = load_config(config_name)
    try:
        # Experiments take a backbone key; E8 takes an episode count, so only pass
        # the model where the signature asks for one.
        first = next(iter(inspect.signature(fn).parameters), None)
        payload = fn(args.model) if first in ("model", "backbone_key") else fn()
        payload.pop("raw_rows", None)
        Path("results").mkdir(exist_ok=True)
        path = write_result(name, payload, config=cfg, seed=int(cfg.get("seed", 0)), notes=notes)
        print(f"[{name}] wrote {path}")
        return payload
    except Exception as exc:  # noqa: BLE001 - blocked experiments must be recorded
        import traceback

        payload = {
            "blocked": True,
            "error": f"{type(exc).__name__}: {exc}",
            "traceback": traceback.format_exc()[-2000:],
        }
        path = write_result(
            name, payload, config=cfg, seed=int(cfg.get("seed", 0)), notes=f"BLOCKED: {exc}"
        )
        print(f"[{name}] BLOCKED -> {path}\n{exc}")
        return payload