Download scripts/split_dataset.py from Chucks90/eryon-data-pipelines: direct link, hf CLI and curl.
- Browser
- Download file 2.89 kB
-
https://huggingface.co/datasets/Chucks90/eryon-data-pipelines/resolve/main/scripts/split_dataset.py
- Command line
-
hf download hf://datasets/Chucks90/eryon-data-pipelines/scripts/split_dataset.py
-
curl -L -o split_dataset.py https://huggingface.co/datasets/Chucks90/eryon-data-pipelines/resolve/main/scripts/split_dataset.py
2.89 kB
| """ | |
| split_dataset.py — ERYON reproducible train/val/test splitter. | |
| Splits at the PATIENT level (not slice level) to prevent leakage. | |
| Writes a splits.json: {series_id: "train" | "val" | "test"}. | |
| Ratios default to 70/15/15. Seed is fixed and written into the output | |
| so splits can be reproduced exactly from the JSON alone. | |
| Usage: | |
| python split_dataset.py \\ | |
| --manifest manifests/lidc/manifest_v1.0.0.jsonl \\ | |
| --train 0.70 --val 0.15 --test 0.15 \\ | |
| --seed 42 \\ | |
| --out manifests/lidc/splits_v1.0.0.json | |
| """ | |
| import argparse | |
| import json | |
| import random | |
| from collections import defaultdict | |
| from pathlib import Path | |
| def load_manifest(path: Path) -> list[dict]: | |
| return [json.loads(line) for line in path.read_text().splitlines() if line.strip()] | |
| def split_patients( | |
| records: list[dict], | |
| train_ratio: float, | |
| val_ratio: float, | |
| seed: int, | |
| ) -> dict[str, str]: | |
| patient_to_series: dict[str, set] = defaultdict(set) | |
| for r in records: | |
| patient_to_series[r["patient_id"]].add(r["series_id"]) | |
| patients = sorted(patient_to_series.keys()) | |
| rng = random.Random(seed) | |
| rng.shuffle(patients) | |
| n = len(patients) | |
| n_train = int(n * train_ratio) | |
| n_val = int(n * val_ratio) | |
| train_patients = set(patients[:n_train]) | |
| val_patients = set(patients[n_train : n_train + n_val]) | |
| test_patients = set(patients[n_train + n_val :]) | |
| series_split: dict[str, str] = {} | |
| for pid, series_set in patient_to_series.items(): | |
| if pid in train_patients: | |
| split = "train" | |
| elif pid in val_patients: | |
| split = "val" | |
| else: | |
| split = "test" | |
| for sid in series_set: | |
| series_split[sid] = split | |
| return series_split | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--manifest", required=True) | |
| parser.add_argument("--train", type=float, default=0.70) | |
| parser.add_argument("--val", type=float, default=0.15) | |
| parser.add_argument("--test", type=float, default=0.15) | |
| parser.add_argument("--seed", type=int, default=42) | |
| parser.add_argument("--out", required=True) | |
| args = parser.parse_args() | |
| assert abs(args.train + args.val + args.test - 1.0) < 1e-6, "Ratios must sum to 1.0" | |
| records = load_manifest(Path(args.manifest)) | |
| splits = split_patients(records, args.train, args.val, args.seed) | |
| out = { | |
| "seed": args.seed, | |
| "ratios": {"train": args.train, "val": args.val, "test": args.test}, | |
| "splits": splits, | |
| } | |
| out_path = Path(args.out) | |
| out_path.parent.mkdir(parents=True, exist_ok=True) | |
| out_path.write_text(json.dumps(out, indent=2)) | |
| counts = {"train": 0, "val": 0, "test": 0} | |
| for s in splits.values(): | |
| counts[s] += 1 | |
| print(f"Split complete: {counts} → {out_path}") | |
| if __name__ == "__main__": | |
| main() | |