"""Consistency check of the certificate file against the pattern list, for (H1). Usage: check_certificate_file.py [patterns.json [certificates.jsonl]] (defaults: the files next to this script) verify_all.py checks each certificate on its own: it rebuilds the integer tensors of the pattern named by the record and checks the certificate exactly at the threshold written in the record. It does not check how the file as a whole relates to the pattern list. This script checks the following, with exact rational arithmetic where numbers occur. (1) patterns.json is a list of 7,962 patterns with pid equal to the position in the list, each with out-neighbor (0, 1) (straight) or (1, 0) (turn) (a turn to W is represented by its mirror image, a turn to E). (2) every line of certificates.jsonl is a JSON record, and every pid 0, ..., 7961 occurs exactly once. (3) every record is in the covered slot model ('model' = 'covered'), which is the model of (H1) in the paper. (4) every record has tip radius 3/5, and ring.TIPRAD, which the checker uses, equals 3/5 (the environment variable TIPRAD is unset or equal to 3/5). (5) the threshold of every record is the threshold of its step type: 961/1000 for a straight pattern (out-neighbor (0, 1) = N, the continuation of the arrival step from s = (0, -1)) and 187/200 for a turn (out-neighbor (1, 0) = E); the field 'out' of the record equals the out-neighbor of the pattern. (6) every record is an operator certificate with an inversion set S of distinct slot indices and a boolean 'leafred', or a closed-form certificate. (7) this interpreter runs with assertions enabled (python without -O). verify_all.py refuses to run with -O. The type convention of (5) is the one of ring.py (N = (0, 1)), closed_exact.py (threshold cs if out == (0, 1), ct otherwise) and run_prod.py (TGT = {(0, 1): 961/1000, (1, 0): 187/200}). Prints one line per check and 'CERTIFICATE FILE CHECK PASS' if all checks pass; exits with status 1 otherwise.""" import sys, os, json, collections from fractions import Fraction as Fr HERE = os.path.dirname(os.path.abspath(__file__)) PAT = sys.argv[1] if len(sys.argv) > 1 else os.path.join(HERE, 'patterns.json') CER = sys.argv[2] if len(sys.argv) > 2 else os.path.join(HERE, 'certificates.jsonl') NPAT = 7962 STRAIGHT, TURN = (0, 1), (1, 0) THRESHOLD = {STRAIGHT: Fr(961, 1000), TURN: Fr(187, 200)} failures = [] def report(name, ok, detail=''): print('%-62s %s%s' % (name, 'ok' if ok else 'FAILED', (' ' + detail) if detail else '')) if not ok: failures.append(name) # (7) assertions report('(7) assertions enabled (no python -O)', __debug__ and sys.flags.optimize == 0) # (1) the pattern list P = json.load(open(PAT)) ok = isinstance(P, list) and len(P) == NPAT and all(r['pid'] == i for i, r in enumerate(P)) outs = collections.Counter(tuple(r['out']) for r in P) ok = ok and set(outs) <= {STRAIGHT, TURN} report('(1) %d patterns, pid = position, out-neighbor N or E' % NPAT, ok, 'straight %d, turn %d' % (outs[STRAIGHT], outs[TURN])) # (2) one record per pattern recs = [] for k, line in enumerate(open(CER)): if line.strip(): recs.append(json.loads(line)) pids = collections.Counter(r['pid'] for r in recs) dup = sorted(p for p, c in pids.items() if c > 1) missing = sorted(set(range(NPAT)) - set(pids)) extra = sorted(set(pids) - set(range(NPAT))) report('(2) every pid 0..%d occurs exactly once' % (NPAT - 1), not dup and not missing and not extra, '%d records%s%s%s' % (len(recs), ', duplicated %s' % dup[:10] if dup else '', ', missing %s' % missing[:10] if missing else '', ', unknown %s' % extra[:10] if extra else '')) # (3) model bad = [r['pid'] for r in recs if r.get('model') != 'covered'] report("(3) model = 'covered' in every record", not bad, ('e.g. pid %s' % bad[:10]) if bad else '') # (4) tip radius bad = [r['pid'] for r in recs if Fr(r.get('tiprad', '0')) != Fr(3, 5)] import ring # reads the environment variable TIPRAD, default 3/5 report('(4) tip radius 3/5 in every record and in ring.TIPRAD', not bad and ring.TIPRAD == Fr(3, 5), 'ring.TIPRAD = %s%s' % (ring.TIPRAD, ('; records with another radius: %s' % bad[:10]) if bad else '')) # (5) threshold by type bad = [] count = collections.Counter() for r in recs: if not 0 <= r['pid'] < NPAT: continue out = tuple(P[r['pid']]['out']) if out not in THRESHOLD or tuple(r.get('out', ())) != out or Fr(r['target']) != THRESHOLD[out]: bad.append(r['pid']) else: count[(out, r['target'])] += 1 report('(5) threshold 961/1000 for straight, 187/200 for turn', not bad, ', '.join('%s %s: %d' % ('straight' if o == STRAIGHT else 'turn', t, n) for (o, t), n in sorted(count.items())) + (('; wrong: %s' % bad[:10]) if bad else '')) # (6) form of the certificates bad = [] kinds = collections.Counter() for r in recs: kinds[r.get('kind')] += 1 if r.get('kind') == 'operator': S = r.get('S') if not (isinstance(S, list) and all(isinstance(i, int) and i >= 0 for i in S) and len(set(S)) == len(S) and isinstance(r.get('leafred'), bool)): bad.append(r['pid']) elif r.get('kind') != 'closed': bad.append(r['pid']) report('(6) every record is an operator or closed-form certificate', not bad, ', '.join('%s %d' % (k, n) for k, n in sorted(kinds.items())) + (('; malformed: %s' % bad[:10]) if bad else '')) if failures: print('CERTIFICATE FILE CHECK FAILED:', failures) sys.exit(1) print('CERTIFICATE FILE CHECK PASS')