Download faisi/src/eval_only/plan_work.py from ppJustin/micro: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/datasets/ppJustin/micro/resolve/main/faisi/src/eval_only/plan_work.py
- Command line
-
hf download hf://datasets/ppJustin/micro/faisi/src/eval_only/plan_work.py
-
curl -L -o plan_work.py https://huggingface.co/datasets/ppJustin/micro/resolve/main/faisi/src/eval_only/plan_work.py
3.31 kB
| """Generate a split and case allowlist inside the trusted evaluation environment.""" | |
| import argparse | |
| from collections import Counter, defaultdict | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import random | |
| import stat | |
| from sandbox.snapshot import CASE_ID, DATASETS | |
| def load_truth(directory, dataset): | |
| path = Path(directory) / f"{dataset}.jsonl" | |
| if stat.S_IMODE(path.stat().st_mode) != 0o444: | |
| raise ValueError("unexpected_truth_permissions") | |
| rows = {} | |
| with path.open(encoding="utf-8") as stream: | |
| for line in stream: | |
| row = json.loads(line) | |
| case = row["case_id"] | |
| if not CASE_ID.fullmatch(case) or case in rows or row["split"] not in {"train", "val", "test"}: | |
| raise ValueError("invalid_truth_inventory") | |
| rows[case] = row | |
| return rows | |
| def stratified(cases, count, seed): | |
| strata = defaultdict(list) | |
| for row in cases: | |
| strata[(row["system_name"], str(row["fault_category"]))].append(row["case_id"]) | |
| if count > len(cases) or not strata: | |
| raise ValueError("invalid_stratified_sample_size") | |
| if count < len(strata): | |
| raise ValueError("sample_too_small_for_strata") | |
| total_extra = len(cases) - len(strata) | |
| extra = count - len(strata) | |
| numerators = {key: extra * (len(items) - 1) for key, items in strata.items()} | |
| quotas = {key: 1 + numerators[key] // max(1, total_extra) for key in strata} | |
| left = count - sum(quotas.values()) | |
| by_remainder = sorted(strata, key=lambda key: (-(numerators[key] % max(1, total_extra)), key)) | |
| for key in by_remainder[:left]: | |
| quotas[key] += 1 | |
| rng = random.Random(seed) | |
| return sorted(case for key in sorted(strata) for case in rng.sample(sorted(strata[key]), quotas[key])) | |
| def make_worklist(directory, snapshot_root): | |
| plan = {} | |
| for dataset in DATASETS: | |
| rows = load_truth(directory, dataset) | |
| if set(rows) != {p.name for p in (Path(snapshot_root) / dataset).iterdir()}: | |
| raise ValueError("case_inventories_differ") | |
| train = [row for row in rows.values() if row["split"] == "train"] | |
| plan[dataset] = { | |
| "test": sorted(row["case_id"] for row in rows.values() if row["split"] == "test"), | |
| "train": stratified(train, 300, 42) if dataset == "propagation_bench" else sorted( | |
| row["case_id"] for row in train | |
| ), | |
| } | |
| return {"version": 1, "splits": plan} | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--vault", default="/data/vault", type=Path) | |
| parser.add_argument("--snapshots", default="/data/snapshots", type=Path) | |
| parser.add_argument("--output", default="/output/worklist.json", type=Path) | |
| args = parser.parse_args() | |
| plan = make_worklist(args.vault, args.snapshots) | |
| text = json.dumps(plan, sort_keys=True, separators=(",", ":")) + "\n" | |
| temp = args.output.with_suffix(".tmp") | |
| temp.write_text(text) | |
| temp.replace(args.output) | |
| print(json.dumps({"worklist_sha256": hashlib.sha256(text.encode()).hexdigest(), | |
| "counts": {dataset: {mode: len(ids) for mode, ids in splits.items()} | |
| for dataset, splits in plan["splits"].items()}})) | |
| if __name__ == "__main__": | |
| main() | |