File size: 2,557 Bytes
65f3217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/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)