File size: 4,580 Bytes
77f4ff4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
"""Verify the built zkml-audit-benchmark HF dataset.

Checks:
  1. Every file in MANIFEST.json exists and has the recorded SHA256.
  2. Every artifact JSON validates against the v2 schema (if jsonschema installed).
  3. Every artifact's pair_id exists in pairs.parquet.
  4. Parquet files load correctly via pyarrow.

Usage (from the hf-dataset/ directory):
    python scripts/verify_dataset.py
"""

from __future__ import annotations

import hashlib
import json
import sys
from pathlib import Path

try:
    import pyarrow.parquet as pq
except ImportError:
    sys.exit("pyarrow is required: pip install pyarrow")


def sha256_file(path: Path) -> str:
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(1 << 16), b""):
            h.update(chunk)
    return h.hexdigest()


def verify(hf_root: Path) -> bool:
    ok = True

    # --- 1. MANIFEST.json ---
    manifest_path = hf_root / "MANIFEST.json"
    if not manifest_path.exists():
        print("FAIL: MANIFEST.json not found")
        return False

    with open(manifest_path, "r", encoding="utf-8") as f:
        manifest = json.load(f)

    entries = manifest.get("files", [])
    print(f"Checking {len(entries)} manifest entries...")

    for entry in entries:
        fpath = hf_root / entry["path"]
        if not fpath.exists():
            print(f"  FAIL: missing {entry['path']}")
            ok = False
            continue
        actual = sha256_file(fpath)
        if actual != entry["sha256"]:
            print(f"  FAIL: sha256 mismatch for {entry['path']}")
            print(f"    expected: {entry['sha256']}")
            print(f"    actual:   {actual}")
            ok = False
        else:
            pass  # silent on success

    if ok:
        print(f"  OK: all {len(entries)} files match")

    # --- 2. Schema validation (optional) ---
    schema_path = hf_root / "schema" / "artifact.v2.schema.json"
    try:
        import jsonschema
        with open(schema_path, "r", encoding="utf-8") as f:
            schema = json.load(f)

        artifact_dirs = [d for d in (hf_root / "artifacts").iterdir() if d.is_dir()]
        art_count = 0
        for adir in sorted(artifact_dirs):
            for af in sorted(adir.glob("*.json")):
                with open(af, "r", encoding="utf-8") as f:
                    art = json.load(f)
                aid = art.get("artifact_id", af.stem)
                try:
                    jsonschema.validate(art, schema)
                except jsonschema.ValidationError as e:
                    print(f"  FAIL: {aid}{e.message}")
                    ok = False
                art_count += 1

        print(f"  OK: {art_count} artifacts validated against v2 schema")
    except ImportError:
        print("  SKIP: jsonschema not installed; skipping artifact schema validation")

    # --- 3. Parquet integrity ---
    pairs_path = hf_root / "data" / "pairs.parquet"
    artifacts_path = hf_root / "data" / "artifacts.parquet"

    if not pairs_path.exists():
        print("FAIL: data/pairs.parquet not found")
        ok = False
    if not artifacts_path.exists():
        print("FAIL: data/artifacts.parquet not found")
        ok = False

    if pairs_path.exists() and artifacts_path.exists():
        pairs_table = pq.read_table(pairs_path)
        artifacts_table = pq.read_table(artifacts_path)

        pair_ids = set(pairs_table.column("pair_id").to_pylist())
        art_pair_ids = set(artifacts_table.column("pair_id").to_pylist())

        orphan = art_pair_ids - pair_ids
        if orphan:
            print(f"  FAIL: artifact pair_ids not in pairs table: {orphan}")
            ok = False
        else:
            print(f"  OK: {len(pairs_table)} pairs, {len(artifacts_table)} artifacts, all pair_ids valid")

        # Check per-pair artifact counts match
        for pid in pair_ids:
            expected = [
                r["artifact_count"]
                for r in pairs_table.to_pylist()
                if r["pair_id"] == pid
            ][0]
            actual = sum(1 for r in artifacts_table.to_pylist() if r["pair_id"] == pid)
            if expected != actual:
                print(f"  FAIL: {pid} artifact_count={expected} but found {actual} artifact rows")
                ok = False

    # --- Summary ---
    if ok:
        print("\n✓ All checks passed")
    else:
        print("\n✗ Some checks failed")

    return ok


if __name__ == "__main__":
    hf_root = Path(__file__).resolve().parent.parent
    sys.exit(0 if verify(hf_root) else 1)