File size: 3,217 Bytes
7d56523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Check an exported (or downloaded) brainphoque-minimal-14 dataset.

Usage: python3 validate.py DATASET_DIR [--rows-per-file 2000] [--expected counts.tsv]
For every Parquet file under DATASET_DIR/data: checks that all rows have the file's length, that
programs are distinct and sorted in enumeration order, and re-runs a random sample of rows with bf.py,
comparing halting, steps and output length. Prints rows per length and per class; with --expected
(a TSV of length, rows) the totals must match. Exits non-zero on any failure.
"""
import argparse, glob, os, re, sys
import numpy as np, pyarrow.parquet as pq

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import bf

RANK = np.zeros(256, np.uint8)                                      # symbol -> rank in enumeration order
for i, c in enumerate(bf.SYMBOLS): RANK[ord(c)] = i

def main():
    ap = argparse.ArgumentParser(); ap.add_argument('root')
    ap.add_argument('--rows-per-file', type=int, default=2000); ap.add_argument('--expected')
    a = ap.parse_args(); rng = np.random.default_rng(0)
    files = sorted(glob.glob(os.path.join(a.root, 'data', 'length_*', '*.parquet')))
    rows, classes, failures = {}, {}, 0
    for path in files:
        L = int(re.search(r'length_(\d+)', path).group(1))
        t = pq.read_table(path, columns=['program', 'length', 'halts', 'steps', 'output_length', 'output_class'])
        progs = t.column('program').to_pylist()
        ok = all(len(p) == L for p in progs) and set(t.column('length').to_pylist()) == {L}
        if ok and len(progs) > 1:                                          # sorted and distinct
            r = RANK[np.frombuffer(''.join(progs).encode(), np.uint8).reshape(-1, L)]
            diff = r[1:] != r[:-1]; first = diff.argmax(1); rows_ = np.arange(len(first))
            ok &= bool(diff.any(1).all() and (r[1:][rows_, first] > r[:-1][rows_, first]).all())
        if not ok: failures += 1; print(f"{path}: wrong length, order or duplicates")
        halts, steps, olen = (t.column(c).to_numpy() for c in ('halts', 'steps', 'output_length'))
        for i in rng.choice(len(progs), min(len(progs), a.rows_per_file), replace=False):
            st, s, out = bf.run(progs[i])
            if (st, s, len(out)) != (bf.HALT if halts[i] else bf.FULL, steps[i], olen[i]):
                failures += 1; print(f"{path}: {progs[i]} replays as {(st, s, len(out))}")
        rows[L] = rows.get(L, 0) + t.num_rows
        for c, n in zip(*np.unique(t.column('output_class').to_pandas().astype(str), return_counts=True)):
            classes[(L, c)] = classes.get((L, c), 0) + int(n)
    for L in sorted(rows): print(f"length {L}: {rows[L]:,} rows")
    print(f"total: {sum(rows.values()):,} rows in {len(files)} files")
    for c in sorted({c for _, c in classes}):
        print(f"  {c}: {sum(n for (L, cc), n in classes.items() if cc == c):,}")
    if a.expected:
        for line in open(a.expected):
            L, n = map(int, line.split())
            if rows.get(L, 0) != n: failures += 1; print(f"length {L}: {rows.get(L, 0)} rows, expected {n}")
    print("OK" if not failures else f"{failures} FAILURES"); sys.exit(1 if failures else 0)

if __name__ == '__main__':
    main()