Datasets:
Download scripts/validate_dataset.py from Craftquantities/craftquantities-open-data: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/datasets/Craftquantities/craftquantities-open-data/resolve/main/scripts/validate_dataset.py
- Command line
-
hf download hf://datasets/Craftquantities/craftquantities-open-data/scripts/validate_dataset.py
-
curl -L -o validate_dataset.py https://huggingface.co/datasets/Craftquantities/craftquantities-open-data/resolve/main/scripts/validate_dataset.py
2.56 kB
| #!/usr/bin/env python3 | |
| """Validate a built dataset folder: every Parquet file reads back with the same rows as its JSONL twin, checksums match | |
| the manifest, and the test vectors equal the site's validation corpus (validation.json) one for one. | |
| python validate_dataset.py <dataset-dir> <raw-dir> | |
| """ | |
| import hashlib | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import pyarrow.parquet as pq | |
| DS, RAW = Path(sys.argv[1]), Path(sys.argv[2]) | |
| manifest = json.loads((DS / "manifest.json").read_text(encoding="utf-8")) | |
| problems = [] | |
| for f in manifest["files"]: | |
| p = DS / f["path"] | |
| if hashlib.sha256(p.read_bytes()).hexdigest() != f["sha256"]: | |
| problems.append(f"checksum {f['path']}") | |
| if p.suffix == ".parquet": | |
| t = pq.read_table(p) | |
| jl = [json.loads(line) for line in (DS / "jsonl" / f"{f['table']}.jsonl").read_text(encoding="utf-8").splitlines() if line] | |
| if t.num_rows != f["rows"] or len(jl) != f["rows"]: | |
| problems.append(f"rows {f['table']}: parquet {t.num_rows}, jsonl {len(jl)}, manifest {f['rows']}") | |
| # the first and last rows read back identically from both formats | |
| back = t.to_pylist() | |
| for i in {0, len(jl) - 1}: | |
| if back[i] != jl[i]: | |
| problems.append(f"row {i} of {f['table']} differs between parquet and jsonl") | |
| print(f"read back {f['path']}: {t.num_rows} rows, {t.num_columns} columns") | |
| validation = json.loads((RAW / "validation.json").read_text(encoding="utf-8")) | |
| site = {} | |
| for c in validation["calculators"]: | |
| for pos, v in enumerate(c["vectors"]): | |
| site[f"{c['slug']}#{pos}"] = v | |
| vecs = [json.loads(line) for line in (DS / "jsonl" / "test_vectors.jsonl").read_text(encoding="utf-8").splitlines() if line] | |
| if len(site) != len(vecs) or len(site) != validation["vectorCount"]: | |
| problems.append(f"vector count: dataset {len(vecs)}, validation.json {len(site)} / {validation['vectorCount']}") | |
| for v in vecs: | |
| s = site.get(v["vector_id"]) | |
| if s is None or s["expected"] != v["expected"] or s["unit"] != v["unit"] or s["inputs"] != json.loads(v["inputs"]) or s.get("toleranceDecimalPlaces") != v["tolerance_decimal_places"]: | |
| problems.append(f"vector {v['vector_id']} differs from validation.json") | |
| print(f"test vectors vs validation.json: {len(vecs)} compared") | |
| bad_counts = [c for c in manifest["api_count_checks"] if not c["match"]] | |
| print("api count checks:", "all match" if not bad_counts else bad_counts) | |
| print("PROBLEMS:", problems if problems else "none") | |
| sys.exit(1 if problems or bad_counts else 0) | |