og2_small / scripts /species_overlap.py
AutomatedScientist's picture
Add files using upload-large-folder tool
6f32cc3 verified
Raw History Blame Contribute Delete
1.64 kB
"""Species overlap between splits (S__ tags of the kept windows): how many of a split's species also occur in train.
python species_overlap.py [--root .] [--splits heldout valid] [--ref train]
"""
from __future__ import annotations
import argparse
import json
import re
from pathlib import Path
def species(root: Path, split: str) -> dict[str, int]:
"""species -> bases in the split's selected windows."""
sel_path = root / split / "selected.json"
sel = json.loads(sel_path.read_text())["shards"] if sel_path.exists() else None
out: dict[str, int] = {}
for jl in sorted((root / split).glob("*/*.jsonl")):
rows = [json.loads(line) for line in jl.read_text().splitlines()]
ids = sel.get(str(jl.with_suffix("").relative_to(root)), []) if sel is not None else range(len(rows))
for i in ids:
m = re.search(r"S__([^;|]*)", rows[i]["tag"])
if m:
out[m.group(1)] = out.get(m.group(1), 0) + rows[i]["len"]
return out
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--root", type=Path, default=Path("."))
ap.add_argument("--splits", nargs="+", default=["heldout", "valid"])
ap.add_argument("--ref", default="train")
a = ap.parse_args()
ref = species(a.root, a.ref)
for split in a.splits:
sp = species(a.root, split)
shared = set(sp) & set(ref)
tagged = sum(sp.values())
print(f"{split}: {len(sp)} species, {len(shared)} also in {a.ref} "
f"({sum(sp[s] for s in shared) / max(tagged, 1):.1%} of its tagged bases)")
if __name__ == "__main__":
main()