#!/usr/bin/env python3 """job 2754 / route 116 -- the pre-registered CONSEQUENCE of A5's failure. prereg-arrangement.md said: "If A5 fails, its falsifier localises the failure (a long entering-prime-only stretch inside the winning run) and the target becomes that stretch instead." This measures that stretch. For each row already measured by arrangement-stats.py (the window is recovered exactly from the saved (P, first_slot, K) by walking the alive sequence): J = number of maximal entering-prime-only stretches inside W maxstretch = the longest of them M_e = K*(P, E) over the P-tile alive sequence on modulus P*prod(E) -- computable from (P,E) ALONE, without R Checks written before the run: C1 sum of stretch lengths == S (control; must hold) C2 J <= F + 1 (the stretches sit between fragments) C3 maxstretch <= M_e (each stretch is a sub-run of an E-alone run) C4 S <= J * M_e (follows from C3) C5 marginal <= fragsum + J*M_e - A (the refined derived bound) C2-C5 are arguments, not observations: a violation is an instrument defect (exit 3), not evidence. python arrangement-stretch.py --in arrangement-composite.json [--in ...] """ import argparse import importlib.util import json import os import sys import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) spec = importlib.util.spec_from_file_location("asx", os.path.join(HERE, "arrangement-stats.py")) asx = importlib.util.module_from_spec(spec) spec.loader.exec_module(asx) def measure(row): P, R, E, K = row["P"], tuple(row["R"]), tuple(row["E"]), row["K"] slots = asx.walk_window(P, row["first_slot"], K, row["modulus"]) fR = np.zeros(slots.size, dtype=bool) fE = np.zeros(slots.size, dtype=bool) for q in R: fR |= (slots % q == 0) | ((slots + 2) % q == 0) for q in E: fE |= (slots % q == 0) | ((slots + 2) % q == 0) only = fE & ~fR stretches = [ln for (_, _, ln) in asx.run_list(only)[0]] Le = P for q in E: Le *= q M_e = asx.km.kstar(P, E) # exact on modulus P*prod(E) out = dict(row) out.update({"J": len(stretches), "maxstretch": max(stretches, default=0), "stretch_sum": int(sum(stretches)), "M_e": M_e, "C1": int(sum(stretches)) == row["S"], "C2": len(stretches) <= row["frag_count"] + 1, "C3": max(stretches, default=0) <= M_e, "C4": row["S"] <= len(stretches) * M_e, "C5": row["marginal"] <= row["frag_sum"] + len(stretches) * M_e - row["A"], "bound_A7": (row["frag_count"] - 1) * row["A"] + row["S"], "bound_stretch": row["frag_sum"] + len(stretches) * M_e - row["A"]}) return out def main(): ap = argparse.ArgumentParser() ap.add_argument("--in", dest="ins", action="append", required=True) ap.add_argument("--out", default="arrangement-stretch.json") a = ap.parse_args() allrows, bad = [], [] for f in a.ins: d = json.load(open(os.path.join(HERE, f), encoding="utf-8")) rows = [measure(r) for r in d["rows"]] for c in ("C1", "C2", "C3", "C4", "C5"): n = sum(1 for r in rows if not r[c]) if n: bad.append({"file": f, "check": c, "violations": n, "example": {k: r[k] for k in ("P", "R", "E", "A", "K", "marginal", "frag_count", "S", "J", "maxstretch", "M_e")}}) allrows += rows print("%-34s rows %4d J<=F+1 %d maxstretch<=M_e %d S<=J*M_e %d C5 %d" % (f, len(rows), sum(0 if r["C2"] else 1 for r in rows), sum(0 if r["C3"] else 1 for r in rows), sum(0 if r["C4"] else 1 for r in rows), sum(0 if r["C5"] else 1 for r in rows))) n = len(allrows) uniq = sorted({(r["J"], r["maxstretch"], r["M_e"]) for r in allrows}) tight7 = [r["bound_A7"] - r["marginal"] for r in allrows] tights = [r["bound_stretch"] - r["marginal"] for r in allrows] res = {"tool": "job2732/arrangement-stretch.py", "version": "1.0.0", "rows": n, "files": a.ins, "defect_violations": bad, "J_hist": {str(k): sum(1 for r in allrows if r["J"] == k) for k in sorted({r["J"] for r in allrows})}, "maxstretch_hist": {str(k): sum(1 for r in allrows if r["maxstretch"] == k) for k in sorted({r["maxstretch"] for r in allrows})}, "max_M_e": max((r["M_e"] for r in allrows), default=None), "count_M_e_ge_A": sum(1 for r in allrows if r["M_e"] >= r["A"]), "count_maxstretch_eq_M_e": sum(1 for r in allrows if r["maxstretch"] == r["M_e"]), "distinct_J_maxstretch_Me": len(uniq), "A7_slack": {"min": min(tight7), "median": sorted(tight7)[n // 2], "max": max(tight7)}, "stretch_bound_slack": {"min": min(tights), "median": sorted(tights)[n // 2], "max": max(tights)}, "stretch_bound_beats_A7": sum(1 for x, y in zip(tights, tight7) if x < y), "rows_with_no_R_kill_in_window": sum(1 for r in allrows if r["frag_sum"] == 0), "rows_with_F_le_1": sum(1 for r in allrows if r["frag_count"] <= 1)} two = [r for r in allrows if len(r["E"]) == 2] for key, field in (("J", "J"), ("maxstretch", "maxstretch")): cells = {} for r in two: cells.setdefault((r["A"], r["E"][0], r["E"][1]), set()).add(r[field]) res["determinacy" + key] = { "cells": len(cells), "cells_with_two_values": sum(1 for s in cells.values() if len(s) > 1), "verdict": ("FAIL -- single-step data" if sum(1 for s in cells.values() if len(s) > 1) == 0 else "PASS -- not single-step data")} print(json.dumps({k: v for k, v in res.items() if k not in ("defect_violations",)}, indent=1, sort_keys=True)) with open(os.path.join(HERE, a.out), "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, indent=1, sort_keys=True) fh.write("\n") print("wrote " + a.out) return 3 if bad else 0 if __name__ == "__main__": sys.exit(main())