"""Controls for the four parser defects the first run of `screen.py` produced. A screen that reports "one disagreement" is worth nothing unless its parsers can be shown to be capable of NOT reporting one for a reason that is actually about the data. Each control below reproduces the defective rule in-line, shows the phantom it makes, and then asserts that the corrected parser in `screen.py` does not make it. The controls run off the same bytes the screen read. C1 a data row treated as a table header -> phantom cells (T_11..T_23, p = 43) C2 a loose integer reader on a witness row -> values 72/150/222 read as L C3 positional pairing of a report's blocks -> phantom cell (T_29, p = 2) C4 #656's `row_ge2_ge3` read as a value row -> five phantom disagreements """ import importlib.util import json import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) RAW = os.path.join(HERE, "raw") spec = importlib.util.spec_from_file_location("screen", os.path.join(HERE, "screen.py")) screen = importlib.util.module_from_spec(spec) spec.loader.exec_module(screen) FAIL = [] def check(name, ok, observed): print("%-72s %s %s" % (name, "PASS" if ok else "FAIL", observed)) if not ok: FAIL.append(name) def main(): cells = json.load(open(os.path.join(HERE, "cells.json"), encoding="utf-8")) S = json.load(open(os.path.join(HERE, "screen.json"), encoding="utf-8")) have = {(c["level"], c["p"]) for c in cells} values = {c["value"] for c in cells} # ---- C1 ------------------------------------------------------------------------------------ md622 = open(os.path.join(RAW, "622-L-TABLE.md"), encoding="utf-8").read() lines = [ln.strip() for ln in md622.splitlines() if ln.strip().startswith("|")] old = None for i, ln in enumerate(lines): cols = [c.strip() for c in ln.strip("|").split("|")] if sum(1 for c in cols if screen.INT_RE.match(c)) >= 5 and i + 2 < len(lines): old = ("header", cols, lines[i + 2]) break print("\nC1 the first run read this all-numeric DATA row as a column header:") print(" %s" % (old[1] if old else "?")) print(" and paired it with the next row %s" % (old[2] if old else "?")) phantom_p = 43 if old else None check("C1 old rule DOES invent cells with the `entries` column as their value (not vacuous)", old is not None and old[1][1] == "3" and "43" in old[1] and int(old[1][2]) > 4, "phantom columns %s -> value %s at a cell whose true value is 1 or 2" % (old[1] if old else None, old[1][2] if old else None)) check("C1 those phantom values (38..43) appear in NO cell of the corrected screen -- L <= 4 here", all(v <= 4 for v in values), "p=43 cells %s carry values %s" % (sorted(c for c in have if c[1] == 43)[:4], sorted({x["value"] for x in cells if x["p"] == 43}))) p43 = [d for d in S["disagreements"] if d["cell"]["p"] == 43] check("C1 every p = 43 disagreement is an instrument difference, none is the phantom column", all(d["class"]["class"] == "alternative_instrument" for d in p43), "p=43 disagreements: %s" % [(d["cell"], d["class"]["class"]) for d in p43]) tables = screen.md_tables(md622) grids = [t for t in tables if len(t["header"]) >= 6 and all(screen.INT_RE.match(c) for c in t["header"][1:])] check("C1 corrected md_tables() classifies exactly ONE table as a prime grid", len(grids) == 1, "%d tables found, %d prime grids" % (len(tables), len(grids))) check("C1 the corrected rule refuses that data row as a header (no table starts 'T_5')", not [t for t in tables if t["header"] and t["header"][0] == "T_5"], "headers: %s" % [t["header"][:2] for t in tables]) # ---- C2 ------------------------------------------------------------------------------------ md637 = open(os.path.join(RAW, "637-L-TABLE-31.md"), encoding="utf-8").read() witness = [ln for ln in md637.splitlines() if ln.startswith("| witness gaps")] loose = [] for c in witness[0].strip("|").split("|")[1:]: m = screen.re.match(r"^\s*\**\s*(\d+)", c) if m: loose.append(int(m.group(1))) print("\nC2 the first run's loose integer reader would take the witness-gap row as values:") print(" %s" % loose[:6]) check("C2 the loose reader DOES invent values far outside the L range (not vacuous)", bool(loose) and max(loose) > 10, "max would be %s" % (max(loose) if loose else None)) check("C2 corrected screen's every cell value is <= 4, the known maximum of L here", max(values) == 4, "values seen: %s" % sorted(values)) check("C2 strict intval refuses a comma list and a dash", screen.intval("72,150,222") is None and screen.intval("-") is None, "intval('72,150,222') = %s" % screen.intval("72,150,222")) # ---- C3 ------------------------------------------------------------------------------------ rep = open(os.path.join(RAW, "637-report.md"), encoding="utf-8").read() rlines = rep.splitlines() positional = [] for i, line in enumerate(rlines): if "L(T_29,p)" in line and i: hdr = [int(x) for x in screen.re.findall(r"[0-9]+", rlines[i - 1])] vals = [int(x) for x in screen.re.findall(r"[0-9]+", line.split(")", 1)[-1])] positional = list(zip(hdr[-len(vals):], vals)) print("\nC3 the first run paired the value line with the line ABOVE it, which here is the other " "row of the block:") print(" first pairs: %s" % positional[:4]) check("C3 old positional pairing DOES invent a non-prime column (not vacuous)", any(not screen.is_prime(p) for p, _ in positional), "non-prime columns %s" % [p for p, _ in positional if not screen.is_prime(p)][:6]) check("C3 corrected screen has no cell at a non-prime column", all(screen.is_prime(p) for _, p in have), "all %d distinct columns prime" % len(have)) check("C3 corrected parser still finds the T_29 row by searching upward", sum(1 for c in cells if c["source"] == "637:report" and c["level"] == 29) == 35, "%d cells at level 29 in 637:report" % sum(1 for c in cells if c["source"] == "637:report" and c["level"] == 29)) # ---- C4 ------------------------------------------------------------------------------------ d = json.load(open(os.path.join(RAW, "656-job1418-level31.json"), encoding="utf-8")) pairs = d["level31"]["row_ge2_ge3"] as_values = {int(k): v[0] for k, v in pairs.items()} print("\nC4 #656's `row_ge2_ge3` read as a value row would put %s at (T_31, 37) and %s at " "(T_31, 41)" % (as_values.get(37), as_values.get(41))) dis = {(x["cell"]["level"], x["cell"]["p"]) for x in S["disagreements"]} check("C4 the value-row reading WOULD invent five disagreements (not vacuous)", as_values.get(37) == 2 and as_values.get(41) == 2 and as_values.get(53) == 2, "37->%s 41->%s 53->%s" % (as_values.get(37), as_values.get(41), as_values.get(53))) check("C4 corrected screen has no disagreement at those cells", not ({(29, 31), (29, 37), (31, 37), (31, 41), (31, 53)} & dis), "disagreements %s" % sorted(dis)) check("C4 corrected screen records the five [2, true] entries as BOUNDS, not cells", len(S["bounds"]) == 5 and {b["bound"] for b in S["bounds"]} == {">=3"}, "%d bounds" % len(S["bounds"])) print("\nVERDICT: %s" % ("PASS" if not FAIL else "FAIL (%s)" % ", ".join(FAIL))) return 1 if FAIL else 0 if __name__ == "__main__": sys.exit(main())