Download examples/make_study_split.py from Biogod/HESRT: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/datasets/Biogod/HESRT/resolve/main/examples/make_study_split.py
- Command line
-
hf download hf://datasets/Biogod/HESRT/examples/make_study_split.py
-
curl -L -o make_study_split.py https://huggingface.co/datasets/Biogod/HESRT/resolve/main/examples/make_study_split.py
2.48 kB
| """Illustrative study/content-grouped split, not an official benchmark protocol.""" | |
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| from pathlib import Path | |
| import pandas as pd | |
| from hesrt_io import RELEASE_TAG, load_catalog | |
| def assign_groups(frame: pd.DataFrame, seed: str) -> pd.DataFrame: | |
| """Keep each GSE and any exact image/expression duplicates in one component.""" | |
| frame = frame.reset_index(drop=True) | |
| parent = list(range(len(frame))) | |
| def root(i: int) -> int: | |
| while parent[i] != i: | |
| parent[i] = parent[parent[i]] | |
| i = parent[i] | |
| return i | |
| for column in ["gse_id", "image_sha256", "expression_sha256"]: | |
| first: dict[str, int] = {} | |
| for i, value in enumerate(frame[column]): | |
| if pd.isna(value) or not str(value).strip(): | |
| continue | |
| value = str(value) | |
| if value in first: | |
| parent[root(i)] = root(first[value]) | |
| else: | |
| first[value] = i | |
| labels: dict[int, str] = {} | |
| for i, gsm in enumerate(frame.gsm_id): | |
| r = root(i) | |
| labels[r] = min(labels.get(r, str(gsm)), str(gsm)) | |
| result = frame[["gse_id", "gsm_id"]].copy() | |
| result["leakage_group"] = [labels[root(i)] for i in range(len(frame))] | |
| def split(group: str) -> str: | |
| fraction = int(hashlib.sha256((seed + ":" + group).encode()).hexdigest()[:16], 16) / 2**64 | |
| return "train" if fraction < 0.8 else "validation" if fraction < 0.9 else "test" | |
| result["split"] = result.leakage_group.map(split) | |
| return result | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--revision", default=RELEASE_TAG) | |
| parser.add_argument("--seed", default="hesrt-example-0") | |
| parser.add_argument("--output", type=Path, default=Path("study_split.csv")) | |
| args = parser.parse_args() | |
| frame, revision = load_catalog(args.revision) | |
| result = assign_groups(frame, args.seed) | |
| result["revision"] = revision | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| result.to_csv(args.output, index=False) | |
| print(result.groupby("split").agg(samples=("gsm_id", "size"), studies=("gse_id", "nunique"))) | |
| print("Approximate 80/10/10 group allocation; not stratified and not donor-disjoint certification.") | |
| print("Resolve shared donors, near-duplicates, cohort overlap and task eligibility separately.") | |
| if __name__ == "__main__": | |
| main() | |