Download faisi/W4-01-external-baselines/src/baselines/eval_service.py from ppJustin/micro: direct link, hf CLI and curl.
- Browser
- Download file 3.99 kB
-
https://huggingface.co/datasets/ppJustin/micro/resolve/main/faisi/W4-01-external-baselines/src/baselines/eval_service.py
- Command line
-
hf download hf://datasets/ppJustin/micro/faisi/W4-01-external-baselines/src/baselines/eval_service.py
-
curl -L -o eval_service.py https://huggingface.co/datasets/ppJustin/micro/resolve/main/faisi/W4-01-external-baselines/src/baselines/eval_service.py
3.99 kB
| """Isolated canonical replay, with V2 audit and case-level alarm stratification.""" | |
| import argparse | |
| import asyncio | |
| from concurrent.futures import ThreadPoolExecutor | |
| import json | |
| from functools import lru_cache | |
| from pathlib import Path | |
| from eval_only.entity_projection import service_key | |
| from rl_env.guided_services import GuidedCases, GuidedTools, run | |
| from rl_env.scaling_services import V2Evaluator | |
| from sandbox.evaluator import decode_diagnosis | |
| def top_alarm(overview): | |
| fields = overview["fields"] | |
| rows = overview["anomalies"] | |
| if not rows: | |
| return None | |
| z = fields.index("robust_z") | |
| entity = fields.index("entity_id") | |
| return max(rows, key=lambda row: row[z])[entity] | |
| class ComparisonEvaluator(V2Evaluator): | |
| def __init__(self, cases): | |
| if cases.allowed["train"]: | |
| raise ValueError("test_evaluator_mounts_train") | |
| super().__init__(cases) | |
| def alarm_stratum(self, dataset, case): | |
| phase = next( | |
| k | |
| for k in ("id_test", "ood_test", "ood_extra") | |
| if (dataset, case) in self.cases.allowed[k] | |
| ) | |
| session = self.cases.open( | |
| {"dataset": dataset, "case_id": case, "phase": phase, "seed": 42} | |
| ) | |
| anon = top_alarm(session.call("get_initial_symptom_overview", {})) | |
| if anon is None: | |
| return False, False | |
| root = decode_diagnosis( | |
| session, | |
| { | |
| "root_entity_id": anon, | |
| "fault_category": "alarm_partition", | |
| "propagation_path": [], | |
| "key_evidence_ids": [], | |
| }, | |
| )["root_entity_id"] | |
| owner = service_key(root, self.cases.snapshot(dataset, case).entities) | |
| return owner in self.projection(dataset, case).roots, True | |
| def compute(self, row): | |
| score, result = super().compute(row) | |
| is_top1, has_alarm = self.alarm_stratum(row["dataset"], row["case_id"]) | |
| return score, result | { | |
| "top1_symptom_root": is_top1, | |
| "has_initial_alarm": has_alarm, | |
| } | |
| def historical(): | |
| scorer = ComparisonEvaluator(GuidedCases()) | |
| partition = { | |
| case: phase | |
| for phase in ("id_test", "ood_test", "ood_extra") | |
| for case in scorer.cases.allowed[phase] | |
| } | |
| for history in sorted(Path("/history").glob("*.jsonl")): | |
| seen = set() | |
| with history.open("rb") as stream, ThreadPoolExecutor(max_workers=12) as pool: | |
| def one(trace): | |
| key = trace["dataset"], trace["case_id"] | |
| if key not in partition: | |
| return None | |
| if trace["mode"] != "test" or trace["session_seed"] != 42: | |
| raise ValueError("unexpected_legacy_trace") | |
| _, row = scorer.compute( | |
| { | |
| "dataset": key[0], | |
| "case_id": key[1], | |
| "phase": partition[key], | |
| "seed": 42, | |
| "step": 0, | |
| "trace": trace, | |
| } | |
| ) | |
| return key, row | {"historical_model": trace["model"]} | |
| for item in pool.map(one, map(json.loads, stream)): | |
| if item is None: | |
| continue | |
| key, row = item | |
| if key in seen: | |
| raise ValueError("duplicate_historical_case") | |
| seen.add(key) | |
| scorer.stream.write(json.dumps(row, allow_nan=False) + "\n") | |
| print(json.dumps({"model": history.stem, "cases": len(seen)}), flush=True) | |
| if __name__ == "__main__": | |
| p = argparse.ArgumentParser(description=__doc__) | |
| p.add_argument("role", choices=("eval", "tools", "historical")) | |
| args = p.parse_args() | |
| if args.role == "historical": | |
| historical() | |
| else: | |
| asyncio.run(run(args.role, {"eval": ComparisonEvaluator, "tools": GuidedTools})) | |