Datasets:
Download preview_subset.py from josh-hud/code-pii: direct link, hf CLI and curl.
- Browser
- Download file 4.31 kB
-
https://huggingface.co/datasets/josh-hud/code-pii/resolve/main/preview_subset.py
- Command line
-
hf download hf://datasets/josh-hud/code-pii/preview_subset.py
-
curl -L -o preview_subset.py https://huggingface.co/datasets/josh-hud/code-pii/resolve/main/preview_subset.py
4.31 kB
| #!/usr/bin/env python3 | |
| """Build a seeded uniform-random subset of a generated JSONL dataset directly as Parquet. | |
| Every *cluster* is kept with probability --frac, decided by a hash of (seed, cluster_id), so clusters | |
| stay whole (coreference labels remain usable), the sample is reproducible, independent of file order, | |
| and stratification follows automatically (language / label / positive-negative mix match the parent in | |
| expectation). Entity tables remain embedded on each retained cluster's doc_index == 0 record. | |
| Usage: python preview_subset.py --src /tmp/poc/v2 --out /tmp/poc/preview_parquet --frac 0.36 --seed 42 --group 9 --workers 4 | |
| """ | |
| import argparse, collections, glob, hashlib, json, os | |
| from multiprocessing import Pool | |
| import pyarrow as pa, pyarrow.parquet as pq | |
| from to_parquet import SCHEMA, normalize | |
| def keep(rid, seed, frac): | |
| h = hashlib.blake2b(f"{seed}:{rid}".encode(), digest_size=8).digest() | |
| return int.from_bytes(h, "big") / 2 ** 64 < frac | |
| def work(args): | |
| srcs, dst, seed, frac = args | |
| C = collections.Counter | |
| st = {"files": 0, "clusters": 0, "entities": 0, "injected": 0, "spans": 0, "negs": 0, "label": C(), "lang": C(), "value_type": C(), "context": C(), "jsonl_bytes": 0} | |
| w = pq.ParquetWriter(dst + ".tmp", SCHEMA, compression="zstd"); buf = [] | |
| def flush(): | |
| if buf: | |
| w.write_table(pa.Table.from_pylist(buf, schema=SCHEMA)); buf.clear() | |
| for src in srcs: | |
| with open(src, encoding="utf-8") as fh: | |
| for line in fh: | |
| r = json.loads(line) | |
| if not keep(r["cluster_id"], seed, frac): | |
| continue | |
| normalize(r) | |
| st["clusters"] += r["doc_index"] == 0 | |
| buf.append(r); st["files"] += 1; st["jsonl_bytes"] += len(line) | |
| st["injected"] += bool(r["spans"]); st["spans"] += len(r["spans"]); st["negs"] += len(r["hard_negatives"]) | |
| st["lang"][r["language"]] += 1 | |
| for s in r["spans"]: | |
| st["label"][s["label"]] += 1; st["value_type"][s["value_type"]] += 1; st["context"][s["context"]] += 1 | |
| if len(buf) >= 10000: | |
| flush() | |
| flush(); w.close(); os.replace(dst + ".tmp", dst) | |
| st["parquet_bytes"] = os.path.getsize(dst); st["file"] = os.path.basename(dst) | |
| print(st["file"], st["files"], "records", f"{st['parquet_bytes']/1e6:.0f} MB", flush=True) | |
| return st | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--src", required=True); ap.add_argument("--out", required=True) | |
| ap.add_argument("--frac", type=float, default=0.36); ap.add_argument("--seed", type=int, default=42) | |
| ap.add_argument("--group", type=int, default=9, help="JSONL parts per output parquet file") | |
| ap.add_argument("--workers", type=int, default=4) | |
| a = ap.parse_args() | |
| os.makedirs(a.out, exist_ok=True) | |
| parts = sorted(glob.glob(os.path.join(a.src, "part-?????.jsonl"))) | |
| jobs = [] | |
| for i in range(0, len(parts), a.group): | |
| chunk = parts[i:i + a.group] | |
| s, e = os.path.basename(chunk[0])[5:10], os.path.basename(chunk[-1])[5:10] | |
| jobs.append((chunk, os.path.join(a.out, f"train-{s}-{e}.parquet"), a.seed, a.frac)) | |
| with Pool(a.workers) as pool: | |
| stats = pool.map(work, jobs) | |
| C = collections.Counter | |
| agg = {"frac": a.frac, "seed": a.seed, "source_parts": len(parts), "files": 0, "clusters": 0, "entities": 0, "injected": 0, "spans": 0, "negs": 0, "jsonl_bytes": 0, "parquet_bytes": 0, | |
| "label": C(), "lang": C(), "value_type": C(), "context": C(), "per_file": []} | |
| for st in stats: | |
| for k in ("files", "clusters", "entities", "injected", "spans", "negs", "jsonl_bytes", "parquet_bytes"): agg[k] += st[k] | |
| for k in ("label", "lang", "value_type", "context"): agg[k].update(st[k]) | |
| agg["per_file"].append({"file": st["file"], "records": st["files"], "bytes": st["parquet_bytes"]}) | |
| for k in ("label", "lang", "value_type", "context"): agg[k] = dict(agg[k].most_common()) | |
| json.dump(agg, open(os.path.join(a.out, "preview_stats.json"), "w"), indent=1) | |
| print(json.dumps({k: agg[k] for k in ("files", "clusters", "entities", "injected", "spans", "negs", "jsonl_bytes", "parquet_bytes")}, indent=1)) | |
| if __name__ == "__main__": | |
| main() | |