Datasets:
Download validate_dataset.py from asterbraid/sedweave-data: direct link, hf CLI and curl.
- Browser
- Download file 4.2 kB
-
https://huggingface.co/datasets/asterbraid/sedweave-data/resolve/main/validate_dataset.py
- Command line
-
hf download hf://datasets/asterbraid/sedweave-data/validate_dataset.py
-
curl -L -o validate_dataset.py https://huggingface.co/datasets/asterbraid/sedweave-data/resolve/main/validate_dataset.py
4.2 kB
| from __future__ import annotations | |
| import argparse | |
| import collections | |
| import concurrent.futures | |
| import hashlib | |
| import json | |
| import shutil | |
| import subprocess | |
| from pathlib import Path | |
| from sedweave.execution import run_sed | |
| def sed_version() -> str: | |
| executable = shutil.which("sed") | |
| if executable is None: | |
| return "sed unavailable" | |
| result = subprocess.run([executable, "--version"], capture_output=True, text=True, check=False) | |
| output = (result.stdout or result.stderr).strip().splitlines() | |
| if result.returncode == 0 and output: | |
| return output[0] | |
| return "unknown sed" | |
| def validate_row(row: dict) -> tuple[str, str | None]: | |
| try: | |
| if run_sed(row["command"], row["input_text"]) != row["expected_output"]: | |
| return row["id"], "primary output mismatch" | |
| if run_sed(row["command"], row["validation_input"]) != row["validation_output"]: | |
| return row["id"], "validation output mismatch" | |
| return row["id"], None | |
| except Exception as error: | |
| return row["id"], str(error)[:500] | |
| def main() -> None: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("data", type=Path) | |
| parser.add_argument("--workers", type=int, default=8) | |
| parser.add_argument("--output", type=Path) | |
| args = parser.parse_args() | |
| rows = [json.loads(line) for line in args.data.open(encoding="utf-8")] | |
| ids = [row["id"] for row in rows] | |
| if len(ids) != len(set(ids)): | |
| raise ValueError("Duplicate row IDs") | |
| by_id = {row["id"]: row for row in rows} | |
| for row in rows: | |
| if row.get("paraphrase_of"): | |
| original = by_id.get(row["paraphrase_of"]) | |
| if original is None: | |
| raise ValueError(f"Missing paraphrase source: {row['id']}") | |
| for key in ("command", "input_text", "expected_output", "validation_input", "validation_output", "split"): | |
| if row[key] != original[key]: | |
| raise ValueError(f"Paraphrase changed {key}: {row['id']}") | |
| if row["split"] != "train": | |
| raise ValueError(f"Paraphrase outside train split: {row['id']}") | |
| errors = [] | |
| with concurrent.futures.ProcessPoolExecutor(max_workers=args.workers) as executor: | |
| for completed, (row_id, error) in enumerate(executor.map(validate_row, rows, chunksize=20), 1): | |
| if error: | |
| errors.append({"id": row_id, "error": error}) | |
| if completed % 5000 == 0: | |
| print(f"validated {completed}/{len(rows)}", flush=True) | |
| split_integrity = {} | |
| for left, right in ( | |
| ("train", "validation"), ("train", "test"), ("validation", "test"), | |
| ("train", "challenge"), ("validation", "challenge"), ("test", "challenge"), | |
| ): | |
| left_rows = [row for row in rows if row["split"] == left and not row.get("paraphrase_of")] | |
| right_rows = [row for row in rows if row["split"] == right and not row.get("paraphrase_of")] | |
| split_integrity[f"{left}-{right}"] = { | |
| "input_overlap": len({row["input_text"] for row in left_rows} & {row["input_text"] for row in right_rows}), | |
| "request_command_overlap": len( | |
| {(row["request"], row["command"]) for row in left_rows} | |
| & {(row["request"], row["command"]) for row in right_rows} | |
| ), | |
| } | |
| summary = { | |
| "data": str(args.data), | |
| "sha256": hashlib.sha256(args.data.read_bytes()).hexdigest(), | |
| "rows": len(rows), | |
| "executions": len(rows) * 2, | |
| "sed_version": sed_version(), | |
| "errors": errors, | |
| "splits": dict(collections.Counter(row["split"] for row in rows)), | |
| "sources": dict(collections.Counter(row["source"] for row in rows)), | |
| "families": dict(collections.Counter(row["family"] for row in rows)), | |
| "unique_requests": len({row["request"] for row in rows}), | |
| "unique_commands": len({row["command"] for row in rows}), | |
| "split_integrity": split_integrity, | |
| } | |
| text = json.dumps(summary, indent=2) | |
| if args.output: | |
| args.output.write_text(text, encoding="utf-8") | |
| print(text) | |
| if errors: | |
| raise SystemExit(1) | |
| if __name__ == "__main__": | |
| main() | |