Download scripts/star/detector/split_pool.py from pourasrp/star_v1: direct link, hf CLI and curl.
- Browser
- Download file 4.71 kB
-
https://huggingface.co/datasets/pourasrp/star_v1/resolve/main/scripts/star/detector/split_pool.py
- Command line
-
hf download hf://datasets/pourasrp/star_v1/scripts/star/detector/split_pool.py
-
curl -L -o split_pool.py https://huggingface.co/datasets/pourasrp/star_v1/resolve/main/scripts/star/detector/split_pool.py
4.71 kB
| #!/usr/bin/env python3 | |
| """Split the 1,000-trajectory probe pool into dev (30%) and held-out (70%). | |
| STDLIB ONLY. | |
| Rules | |
| ----- | |
| - Rules, regexes and worked examples may be developed on DEV ONLY. | |
| - Labelling sheets are drawn from HELD-OUT ONLY (make_sheets.py --restrict-to heldout_ids.txt). | |
| Procedure (deterministic, fully logged) | |
| --------------------------------------- | |
| 1. Read the pool ids, de-duplicate, and SORT them. Sorting makes the split independent of the | |
| order the probe happened to sample in. | |
| 2. Shuffle with random.Random(SPLIT_SEED); the first round(0.30 * N) are dev, the rest held-out. | |
| 3. LEAK GUARD: any trajectory already inspected during rule development (worked examples, | |
| hand-read spot checks, regenerable detector hits that were hand-read) that landed in held-out | |
| is MOVED to dev. Those trajectories shaped the rules; labelling them as held-out evidence would | |
| measure the rules on their own training data. Every moved id is logged. | |
| 4. Write dev_ids.txt, heldout_ids.txt and split.json (seed, counts, moved ids, SHA-256 of each | |
| sorted list so the split can be verified later without re-running). | |
| Usage | |
| ----- | |
| python split_pool.py --pool pool_ids.txt --inspected inspected_ids.txt --outdir . | |
| """ | |
| import argparse | |
| import hashlib | |
| import json | |
| import random | |
| import sys | |
| SPLIT_SEED = 20260925 | |
| DEV_FRACTION = 0.30 | |
| def read_ids(path): | |
| ids = [] | |
| with open(path, encoding="utf-8") as fh: | |
| for line in fh: | |
| line = line.strip() | |
| if line and not line.startswith("#"): | |
| ids.append(line) | |
| return ids | |
| def sha(ids): | |
| return hashlib.sha256("\n".join(sorted(ids)).encode("utf-8")).hexdigest() | |
| def main(): | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| ap.add_argument("--pool", required=True, help="one trajectory_id per line (the 1,000 pool)") | |
| ap.add_argument("--inspected", required=True, help="ids already inspected during rule development") | |
| ap.add_argument("--outdir", default=".") | |
| ap.add_argument("--expect", type=int, default=1000, help="expected pool size; mismatch aborts") | |
| args = ap.parse_args() | |
| raw = read_ids(args.pool) | |
| pool = sorted(set(raw)) | |
| if len(raw) != len(pool): | |
| print(f"NOTE: {len(raw) - len(pool)} duplicate id(s) in pool removed.") | |
| if len(pool) != args.expect: | |
| sys.exit(f"ERROR: pool has {len(pool)} ids, expected {args.expect}. Refusing to split.") | |
| inspected = set(read_ids(args.inspected)) | |
| not_in_pool = sorted(inspected - set(pool)) | |
| rng = random.Random(SPLIT_SEED) | |
| shuffled = list(pool) | |
| rng.shuffle(shuffled) | |
| n_dev = round(DEV_FRACTION * len(shuffled)) | |
| dev = set(shuffled[:n_dev]) | |
| heldout = set(shuffled[n_dev:]) | |
| moved = sorted(inspected & heldout) | |
| dev |= set(moved) | |
| heldout -= set(moved) | |
| assert not (dev & heldout), "dev and held-out overlap" | |
| assert dev | heldout == set(pool), "split does not cover the pool" | |
| assert not (inspected & heldout), "an inspected id leaked into held-out" | |
| dev_s, ho_s = sorted(dev), sorted(heldout) | |
| with open(f"{args.outdir}/dev_ids.txt", "w", encoding="utf-8") as fh: | |
| fh.write("\n".join(dev_s) + "\n") | |
| with open(f"{args.outdir}/heldout_ids.txt", "w", encoding="utf-8") as fh: | |
| fh.write("\n".join(ho_s) + "\n") | |
| record = { | |
| "split_seed": SPLIT_SEED, | |
| "dev_fraction_target": DEV_FRACTION, | |
| "pool_size": len(pool), | |
| "pool_sha256": sha(pool), | |
| "random_split": {"dev": n_dev, "heldout": len(pool) - n_dev}, | |
| "inspected_ids_supplied": len(inspected), | |
| "inspected_ids_not_in_pool": not_in_pool, | |
| "moved_heldout_to_dev": moved, | |
| "final": { | |
| "dev": len(dev_s), | |
| "heldout": len(ho_s), | |
| "dev_fraction_actual": round(len(dev_s) / len(pool), 4), | |
| }, | |
| "dev_sha256": sha(dev_s), | |
| "heldout_sha256": sha(ho_s), | |
| } | |
| with open(f"{args.outdir}/split.json", "w", encoding="utf-8") as fh: | |
| json.dump(record, fh, indent=2) | |
| print(f"seed={SPLIT_SEED} pool={len(pool)}") | |
| print(f"random split : dev {n_dev} / held-out {len(pool) - n_dev}") | |
| print(f"moved to dev : {len(moved)} inspected id(s) that had landed in held-out") | |
| print(f"final : dev {len(dev_s)} ({record['final']['dev_fraction_actual']:.1%}) " | |
| f"/ held-out {len(ho_s)}") | |
| if not_in_pool: | |
| print(f"note : {len(not_in_pool)} inspected id(s) are not in this pool " | |
| f"(e.g. from the earlier 200-sample); no action needed") | |
| print(f"heldout_sha256 : {record['heldout_sha256']}") | |
| if __name__ == "__main__": | |
| main() | |