PatchBench / build_dataset.py
StevenShen3641's picture
Add PatchBench task metadata (213 tasks)
e7ed651 verified
Raw
History Blame Contribute Delete
2.06 kB
#!/usr/bin/env python3
"""Build the Hugging Face dataset files for PatchBench from benchmark/data/metadata.json.
hf/.venv/bin/python3 hf/build_dataset.py [--metadata PATH] [--out DIR]
Writes `data/test-00000-of-00001.parquet`, one row per task, sorted by numeric
id, every column a string. Needs pyarrow; the dataset card (README.md) is
maintained by hand.
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import pyarrow as pa
import pyarrow.parquet as pq
ROOT = Path(__file__).resolve().parent.parent
IMAGE_TEMPLATE = "anonymous3619/vulpatch:{id}-vul"
# Column order in the emitted table. `id` and `image` are prepended.
FIELDS = (
"project",
"repo_addr",
"base_commit",
"inverted_commit",
"fuzzer",
"sanitizer_type",
"command",
"sanitizer_report",
"patch",
)
COLUMNS = ("id", "image", *FIELDS)
SCHEMA = pa.schema([(name, pa.string()) for name in COLUMNS])
def build(metadata: Path, out: Path) -> int:
meta = json.loads(metadata.read_text())
rows = []
for task_id in sorted(meta, key=int):
entry = meta[task_id]
missing = [f for f in FIELDS if f not in entry]
if missing:
raise SystemExit(f"task {task_id} is missing {missing}")
row = {"id": task_id, "image": IMAGE_TEMPLATE.format(id=task_id)}
row.update({f: entry[f] for f in FIELDS})
rows.append(row)
table = pa.Table.from_pylist(rows, schema=SCHEMA)
target = out / "data" / "test-00000-of-00001.parquet"
target.parent.mkdir(parents=True, exist_ok=True)
pq.write_table(table, target, compression="snappy")
print(f"{len(rows)} tasks -> {target} ({target.stat().st_size / 1e6:.1f} MB)")
return len(rows)
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--metadata", type=Path, default=ROOT / "benchmark" / "data" / "metadata.json")
ap.add_argument("--out", type=Path, default=ROOT / "hf")
args = ap.parse_args()
build(args.metadata, args.out)
if __name__ == "__main__":
main()