The Principia Machina/TPC-PY/gen sponsor corpus.py
Appearance
"""gen_sponsor_corpus 0.1.0: builds sponsor_corpus.json for tpc_sponsor
0.1.0. Expectations are computed by tpc_sponsor.py and pinned; hand_
checked cases were worked by hand (the evidence cases against the closed
forms E = (a0 + F) / (a0 + b0 + F + A) and the Beta(1, 1) interval)."""
import json
import os
import sys
here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, here)
from tpc_sponsor import run_case # noqa: E402
cases = []
def add(name, fn, args, hand=False):
cases.append({"name": name, "fn": fn, "args": args, "hand_checked": hand,
"expected": run_case(fn, args)})
DAY = 86400
WIN = 30 * DAY
G = {"CE-F1": 1, "CE-F2": 1}
H = {"CE-A1": 2, "CE-A2": 3, "CE-A3": 3, "CE-A4": 2, "CE-A5": 3}
def f1(named=True, pred=None, blocks=100, viol=None, taken=False):
return {"named": named, "predecessor": pred, "blocks": blocks,
"first_violation_block": viol, "period_taken": taken}
def f2(declared=True, blocks=100, viol=None, canary=None):
return {"declared": declared, "blocks": blocks, "first_violation_block": viol,
"first_canary_failure_block": canary}
# ---- conduct_resolve -------------------------------------------------------
add("S01 CE-F1: 100 clean blocks, no predecessor: FOR", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1()}, True)
add("S02 CE-F1: 99 blocks: PENDING (window boundary)", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(blocks=99)}, True)
add("S03 CE-F1: 101 blocks: FOR", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(blocks=101)}, True)
add("S04 CE-F1: a confirmed violation at block 100: NEUTRAL (inclusive)", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(blocks=150, viol=100)}, True)
add("S05 CE-F1: a confirmed violation at block 101: FOR (outside the window)", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(blocks=150, viol=101)}, True)
add("S06 CE-F1: a violation at block 5 with 5 blocks landed: NEUTRAL at once", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(blocks=5, viol=5)}, True)
add("S07 CE-F1: the predecessor GREEN with nothing pending: FOR", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(pred={"state": "GREEN", "pending": 0})}, True)
add("S08 CE-F1: the predecessor YELLOW: NEUTRAL", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(pred={"state": "YELLOW", "pending": 0})}, True)
add("S09 CE-F1: the predecessor GREEN with one PENDING item: NEUTRAL", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(pred={"state": "GREEN", "pending": 1})}, True)
add("S10 CE-F1: not named: NEUTRAL", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(named=False)}, True)
add("S11 CE-F1: the RATING PERIOD's CE-F1 already taken: NEUTRAL", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(taken=True)}, True)
add("S12 CE-F1: zero blocks: PENDING", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(blocks=0)}, True)
add("S13 CE-F2: 100 clean blocks after the declared change: FOR", "resolve", {"event": {"kind": "CE-F2"}, "facts": f2()}, True)
add("S14 CE-F2: 99 blocks: PENDING", "resolve", {"event": {"kind": "CE-F2"}, "facts": f2(blocks=99)}, True)
add("S15 CE-F2: a canary failure at block 100: NEUTRAL", "resolve", {"event": {"kind": "CE-F2"}, "facts": f2(blocks=120, canary=100)}, True)
add("S16 CE-F2: a canary failure at block 101: FOR", "resolve", {"event": {"kind": "CE-F2"}, "facts": f2(blocks=120, canary=101)}, True)
add("S17 CE-F2: a violation at block 1: NEUTRAL", "resolve", {"event": {"kind": "CE-F2"}, "facts": f2(blocks=1, viol=1)}, True)
add("S18 CE-F2: not declared: NEUTRAL (CE-A1's matter)", "resolve", {"event": {"kind": "CE-F2"}, "facts": f2(declared=False)}, True)
add("S19 CE-A1: audit CONFIRMED: AGAINST", "resolve", {"event": {"kind": "CE-A1"}, "facts": {"audit": "CONFIRMED"}}, True)
add("S20 CE-A1: audit OPEN: PENDING", "resolve", {"event": {"kind": "CE-A1"}, "facts": {"audit": "OPEN"}}, True)
add("S21 CE-A1: audit DISMISSED: NEUTRAL", "resolve", {"event": {"kind": "CE-A1"}, "facts": {"audit": "DISMISSED"}}, True)
add("S22 CE-A2: audit CONFIRMED: AGAINST", "resolve", {"event": {"kind": "CE-A2"}, "facts": {"audit": "CONFIRMED"}}, True)
add("S23 CE-A3: audit OPEN: PENDING", "resolve", {"event": {"kind": "CE-A3"}, "facts": {"audit": "OPEN"}}, True)
add("S24 CE-A4: admitted 30 days after a YELLOW agent's lapse: AGAINST (window boundary, inclusive)", "resolve", {"event": {"kind": "CE-A4"}, "facts": {"admission": 1000 + WIN, "exit": 1000, "state": "YELLOW", "pending": 0, "window": WIN}}, True)
add("S25 CE-A4: admitted 30 days and one second after: NEUTRAL", "resolve", {"event": {"kind": "CE-A4"}, "facts": {"admission": 1001 + WIN, "exit": 1000, "state": "YELLOW", "pending": 0, "window": WIN}}, True)
add("S26 CE-A4: admitted before the exit, inside the window: AGAINST (either side)", "resolve", {"event": {"kind": "CE-A4"}, "facts": {"admission": 1000, "exit": 1000 + WIN, "state": "ORANGE", "pending": 2, "window": WIN}}, True)
add("S27 CE-A4: the other agent GREEN with nothing pending: NEUTRAL", "resolve", {"event": {"kind": "CE-A4"}, "facts": {"admission": 2000, "exit": 1000, "state": "GREEN", "pending": 0, "window": WIN}}, True)
add("S28 CE-A4: GREEN with a PENDING item: AGAINST", "resolve", {"event": {"kind": "CE-A4"}, "facts": {"admission": 2000, "exit": 1000, "state": "GREEN", "pending": 1, "window": WIN}}, True)
add("S29 CE-A4: SUSPENDED, same second: AGAINST", "resolve", {"event": {"kind": "CE-A4"}, "facts": {"admission": 1000, "exit": 1000, "state": "SUSPENDED", "pending": 0, "window": 0}}, True)
add("S30 CE-A5: removed after a canary failure: AGAINST", "resolve", {"event": {"kind": "CE-A5"}, "facts": {"removed": True, "cause": "CANARY"}}, True)
add("S31 CE-A5: removed after a trip: AGAINST", "resolve", {"event": {"kind": "CE-A5"}, "facts": {"removed": True, "cause": "TRIP"}}, True)
add("S32 CE-A5: removed for another reason: NEUTRAL", "resolve", {"event": {"kind": "CE-A5"}, "facts": {"removed": True, "cause": "OTHER"}}, True)
add("S33 CE-A5: the decision item open: PENDING", "resolve", {"event": {"kind": "CE-A5"}, "facts": {"removed": False, "cause": "CANARY"}}, True)
add("S34 an unknown kind is refused", "resolve", {"event": {"kind": "CE-X"}, "facts": {}}, True)
add("S35 facts of another kind are refused", "resolve", {"event": {"kind": "CE-F1"}, "facts": {"audit": "OPEN"}}, True)
add("S36 a violation block of 0 is refused (blocks are 1-based)", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(viol=0)}, True)
add("S37 a boolean blocks count is refused", "resolve", {"event": {"kind": "CE-F1"}, "facts": f1(blocks=True)}, True)
add("S38 an unknown audit state is refused", "resolve", {"event": {"kind": "CE-A2"}, "facts": {"audit": "MAYBE"}}, True)
add("S39 an unknown state at exit is refused", "resolve", {"event": {"kind": "CE-A4"}, "facts": {"admission": 2000, "exit": 1000, "state": "RED", "pending": 0, "window": WIN}}, True)
add("S40 CE-A5 with a null cause, removed: NEUTRAL", "resolve", {"event": {"kind": "CE-A5"}, "facts": {"removed": True, "cause": None}}, True)
# ---- user_evidence ---------------------------------------------------------
def ev(kind, res, w=None):
d = {"kind": kind, "resolution": res}
if w is not None:
d["weight"] = w
return d
base = {"g": G, "h": H, "a0": 1, "b0": 1, "half_life": 500}
def uargs(events, ages, **over):
a = dict(base)
a.update(over)
a["events"] = events
a["ages"] = ages
return a
add("E01 no events: the prior, E 0.5, interval 0.025..0.975, n 0", "evidence", uargs([], []), True)
add("E02 one CE-F1 FOR at age 0: F 1, E 2/3", "evidence", uargs([ev("CE-F1", "FOR")], [0]), True)
add("E03 one CE-A5 AGAINST at age 0: A 3, E 1/5", "evidence", uargs([ev("CE-A5", "AGAINST")], [0]), True)
add("E04 a CE-F1 FOR at age 500 (one half-life): F 0.5", "evidence", uargs([ev("CE-F1", "FOR")], [500]), True)
add("E05 a CE-A1 AGAINST at age 1000 (two half-lives): A 0.5", "evidence", uargs([ev("CE-A1", "AGAINST")], [1000]), True)
add("E06 agent events carry their own weights: FOR 0.8 and AGAINST 2 at age 0", "evidence", uargs([ev("AGENT", "FOR", 0.8), ev("AGENT", "AGAINST", 2)], [0, 0]), True)
add("E07 both parts: two agent FOR events, a CE-F2 FOR, a CE-A4 AGAINST, aged", "evidence", uargs([ev("AGENT", "FOR", 1), ev("AGENT", "FOR", 0.5), ev("CE-F2", "FOR"), ev("CE-A4", "AGAINST")], [10, 250, 0, 700]), False)
add("E08 a NEUTRAL conduct event adds nothing and is not counted", "evidence", uargs([ev("CE-F1", "NEUTRAL"), ev("CE-A2", "NEUTRAL"), ev("CE-F2", "FOR")], [0, 0, 0]), True)
add("E09 a PENDING event is refused (resolved events only)", "evidence", uargs([ev("CE-A1", "PENDING")], [0]), True)
add("E10 a FOR resolution on an AGAINST kind is refused", "evidence", uargs([ev("CE-A1", "FOR")], [0]), True)
add("E11 a weight on a conduct event is refused", "evidence", uargs([ev("CE-F1", "FOR", 5)], [0]), True)
add("E12 an agent event without a weight is refused", "evidence", uargs([ev("AGENT", "FOR")], [0]), True)
add("E13 ages of the wrong length are refused", "evidence", uargs([ev("CE-F1", "FOR")], []), True)
add("E14 a negative age is refused", "evidence", uargs([ev("CE-F1", "FOR")], [-1]), True)
add("E15 h missing a kind is refused", "evidence", uargs([ev("CE-F1", "FOR")], [0], h={"CE-A1": 2}), True)
add("E16 a0 of 0 is refused", "evidence", uargs([ev("CE-F1", "FOR")], [0], a0=0), True)
add("E17 half_life of 0 is refused", "evidence", uargs([ev("CE-F1", "FOR")], [0], half_life=0), True)
add("E18 tuning weights as declared: CE-A2 3, CE-A4 2, CE-F1 1", "evidence", uargs([ev("CE-A2", "AGAINST"), ev("CE-A4", "AGAINST"), ev("CE-F1", "FOR")], [0, 0, 0]), True)
add("E19 other tuning weights: g 2, h 1", "evidence", uargs([ev("CE-A2", "AGAINST"), ev("CE-F1", "FOR")], [0, 0], g={"CE-F1": 2, "CE-F2": 2}, h={"CE-A1": 1, "CE-A2": 1, "CE-A3": 1, "CE-A4": 1, "CE-A5": 1}), True)
add("E20 a thin file's neighbour: 25 FOR of weight 1 and 3 AGAINST of weight 1, unaged", "evidence", uargs([ev("AGENT", "FOR", 1)] * 25 + [ev("AGENT", "AGAINST", 1)] * 3, [0] * 28), False)
add("E21 another prior: a0 2, b0 2", "evidence", uargs([ev("CE-F1", "FOR")], [0], a0=2, b0=2), True)
add("E22 a very old event: age 50000 at half-life 500", "evidence", uargs([ev("CE-F1", "FOR")], [50000]), True)
add("E23 an unknown kind is refused", "evidence", uargs([ev("CE-Z", "FOR")], [0]), True)
# ---- whitewash_count -------------------------------------------------------
def a(agent, t):
return {"agent": agent, "time": t}
st = {"A": {"state": "YELLOW", "pending": 0}, "B": {"state": "GREEN", "pending": 0},
"C": {"state": "GREEN", "pending": 2}, "D": {"state": "SUSPENDED", "pending": 1}}
add("W01 one admission within the window of a YELLOW agent's lapse: 1", "whitewash", {"admissions": [a("N1", 1000)], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
add("W02 the retired agent GREEN with nothing pending: 0", "whitewash", {"admissions": [a("N1", 1000)], "retirements": [a("B", 500)], "states": st, "window": WIN}, True)
add("W03 GREEN with a PENDING item: 1", "whitewash", {"admissions": [a("N1", 1000)], "retirements": [a("C", 500)], "states": st, "window": WIN}, True)
add("W04 exactly the window apart: 1 (inclusive)", "whitewash", {"admissions": [a("N1", 500 + WIN)], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
add("W05 one second past the window: 0", "whitewash", {"admissions": [a("N1", 501 + WIN)], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
add("W06 the admission before the exit, inside the window: 1", "whitewash", {"admissions": [a("N1", 400)], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
add("W07 two admissions, one troubled exit: each counts once: 2", "whitewash", {"admissions": [a("N1", 600), a("N2", 700)], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
add("W08 one admission, two troubled exits: counts once, the first named", "whitewash", {"admissions": [a("N1", 600)], "retirements": [a("D", 550), a("A", 500)], "states": st, "window": WIN}, True)
add("W09 the same account retired and re-admitted: not another agent: 0", "whitewash", {"admissions": [a("A", 600)], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
add("W10 no retirements: 0", "whitewash", {"admissions": [a("N1", 600)], "retirements": [], "states": st, "window": WIN}, True)
add("W11 no admissions: 0", "whitewash", {"admissions": [], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
add("W12 a retired agent without a state entry is refused", "whitewash", {"admissions": [a("N1", 600)], "retirements": [a("Z", 500)], "states": st, "window": WIN}, True)
add("W13 a negative window is refused", "whitewash", {"admissions": [a("N1", 600)], "retirements": [a("A", 500)], "states": st, "window": -1}, True)
add("W14 a window of 0: only the same second counts", "whitewash", {"admissions": [a("N1", 500), a("N2", 501)], "retirements": [a("A", 500)], "states": st, "window": 0}, True)
add("W15 an admission missing its time is refused", "whitewash", {"admissions": [{"agent": "N1"}], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
add("W16 a bad state value is refused", "whitewash", {"admissions": [a("N1", 600)], "retirements": [a("A", 500)], "states": {"A": {"state": "RED", "pending": 0}}, "window": WIN}, True)
add("W17 agent names with | and unicode, escaped in the render", "whitewash", {"admissions": [a("N|1", 600)], "retirements": [a("αβ", 500)], "states": {"αβ": {"state": "ORANGE", "pending": 0}}, "window": WIN}, True)
add("W18 mixed: three admissions, three exits, two count", "whitewash", {"admissions": [a("N1", 100), a("N2", 5000000), a("N3", 2000000)], "retirements": [a("A", 200), a("B", 4999999), a("C", 1000000)], "states": st, "window": WIN}, True)
add("W19 states not an object is refused", "whitewash", {"admissions": [a("N1", 600)], "retirements": [a("A", 500)], "states": [], "window": WIN}, True)
add("W20 an empty agent name is refused", "whitewash", {"admissions": [a("", 600)], "retirements": [a("A", 500)], "states": st, "window": WIN}, True)
json.dump({"corpus": "sponsor", "version": "0.1.0", "cases": cases},
open(os.path.join(here, "sponsor_corpus.json"), "w"), indent=1,
sort_keys=True, ensure_ascii=False)
print("cases", len(cases))