Download scripts/export_audit_sample.py from continuum-oss/ccrb: direct link, hf CLI and curl.
- Browser
- Download file 3.88 kB
-
https://huggingface.co/datasets/continuum-oss/ccrb/resolve/main/scripts/export_audit_sample.py
- Command line
-
hf download hf://datasets/continuum-oss/ccrb/scripts/export_audit_sample.py
-
curl -L -o export_audit_sample.py https://huggingface.co/datasets/continuum-oss/ccrb/resolve/main/scripts/export_audit_sample.py
3.88 kB
| """Export a stratified 15% human-audit sample of CCRB (query, document, grade) pairs.""" | |
| from __future__ import annotations | |
| import argparse | |
| import csv | |
| import random | |
| import sys | |
| from collections import defaultdict | |
| from pathlib import Path | |
| _REPO_ROOT = Path(__file__).resolve().parent.parent | |
| if str(_REPO_ROOT) not in sys.path: | |
| sys.path.insert(0, str(_REPO_ROOT)) | |
| from scripts.pipeline_utils import TRACKS, load_jsonl, load_qrels | |
| def export_audit_sample( | |
| data_dir: Path, | |
| output_csv: Path, | |
| *, | |
| fraction: float = 0.15, | |
| seed: int = 20261129, | |
| ) -> dict[str, int]: | |
| """Sample `fraction` of queries per track (stratified by query_type/split) for human QA.""" | |
| rng = random.Random(seed) | |
| output_csv.parent.mkdir(parents=True, exist_ok=True) | |
| counts: dict[str, int] = {} | |
| with output_csv.open("w", encoding="utf-8", newline="") as handle: | |
| writer = csv.writer(handle) | |
| writer.writerow( | |
| [ | |
| "track", | |
| "split", | |
| "query_type", | |
| "query_id", | |
| "query", | |
| "document_id", | |
| "doc_title", | |
| "doc_text_preview", | |
| "assigned_grade", | |
| "human_grade", | |
| "notes", | |
| ] | |
| ) | |
| for track in TRACKS: | |
| queries = [obj for _ln, obj in load_jsonl(data_dir / "queries" / f"{track}.jsonl")] | |
| docs = { | |
| str(obj["document_id"]): obj | |
| for _ln, obj in load_jsonl(data_dir / "documents" / f"{track}.jsonl") | |
| } | |
| qrels = load_qrels(data_dir / "qrels" / f"{track}.tsv") | |
| by_stratum: dict[tuple[str, str], list[dict]] = defaultdict(list) | |
| for qobj in queries: | |
| key = (str(qobj.get("split", "")), str(qobj.get("query_type", ""))) | |
| by_stratum[key].append(qobj) | |
| sampled_queries: list[dict] = [] | |
| for _key, group in sorted(by_stratum.items()): | |
| k = max(1, round(len(group) * fraction)) | |
| sampled_queries.extend(rng.sample(group, min(k, len(group)))) | |
| counts[track] = len(sampled_queries) | |
| for qobj in sampled_queries: | |
| qid = str(qobj["query_id"]) | |
| pairs = qrels.get(qid, {}) | |
| positives = [(did, rel) for did, rel in pairs.items() if rel >= 1] | |
| negatives = [(did, rel) for did, rel in pairs.items() if rel == 0] | |
| chosen = sorted(positives, key=lambda x: -x[1])[:3] + negatives[:2] | |
| for did, rel in chosen: | |
| doc = docs.get(did, {}) | |
| writer.writerow( | |
| [ | |
| track, | |
| qobj.get("split", ""), | |
| qobj.get("query_type", ""), | |
| qid, | |
| str(qobj.get("query", ""))[:600], | |
| did, | |
| str(doc.get("title", ""))[:200], | |
| str(doc.get("text", ""))[:500], | |
| rel, | |
| "", | |
| "", | |
| ] | |
| ) | |
| return counts | |
| def main(argv: list[str] | None = None) -> int: | |
| parser = argparse.ArgumentParser(description="Export stratified human-audit sample CSV.") | |
| parser.add_argument("--data-dir", type=Path, default=Path("data")) | |
| parser.add_argument("--output", type=Path, default=Path("results/human_audit_sample.csv")) | |
| parser.add_argument("--fraction", type=float, default=0.15) | |
| args = parser.parse_args(argv) | |
| counts = export_audit_sample(args.data_dir, args.output, fraction=args.fraction) | |
| print("Exported human-audit queries per track:", counts) | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |