OpenIslet

scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py

IIP-0043 / DEC-122 (CAND-034 / EXP-031 — Bucket 1 Closure, Stage S3-R2 Remediated)

script Release v0.8.0 gen-eb955f8266035aba 1008 KB · 76 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…

    acc_p22 = [15.05 + 0.04 * ((96.0 / n) ** 2.2) for n in (48, 72, 96)]
    m_acc_cap2 = _adjudicate_nw08_three_grid_convergence(acc_p22[0], acc_p22[1], acc_p22[2], r2_std_ladder)
    acc_straddle = [14.99 + 0.04 * ((96.0 / n) ** 1.5) for n in (48, 72, 96)]
    m_acc_hold = _adjudicate_nw08_three_grid_convergence(acc_straddle[0], acc_straddle[1], acc_straddle[2], r2_std_ladder)
    acc_fail = [14.90 + 0.02 * ((96.0 / n) ** 1.5) for n in (48, 72, 96)]
    m_acc_fail = _adjudicate_nw08_three_grid_convergence(acc_fail[0], acc_fail[1], acc_fail[2], r2_std_ladder)
    ok_14 = bool(
        m_acc_pass["verdict_from_mesh_policy"] == "PASS"
        and m_acc_pass["mesh_convergence_resolved"] is True
        and m_acc_pass["interval_valid"] is True
        and m_acc_pass["order_qualified"] is True
        and m_acc_pass["convergence_regime"] == "MONOTONE_CONVERGING_GCI"
        and abs(float(m_acc_pass["apparent_order_p"]) - 1.5) < 1e-5
        and abs(float(m_acc_pass["richardson_extrapolated_mmhg"]) - 15.05) < 1e-6
        and abs(float(m_acc_pass["conservative_po2_lower_bound_mmhg"]) - 15.04) < 1e-6
        and m_acc_cap2["verdict_from_mesh_policy"] == "PASS"
        and m_acc_cap2["convergence_regime"] == "MONOTONE_CONVERGING_GCI_FORMAL_ORDER_CAPPED"
        and abs(float(m_acc_cap2["observed_order_p_raw"]) - 2.2) < 1e-5
        and float(m_acc_cap2["apparent_order_p"]) == 2.0
        and m_acc_cap2["interval_valid"] is True
        and float(m_acc_cap2["conservative_po2_lower_bound_mmhg"]) >= 15.0
        and m_acc_hold["verdict_from_mesh_policy"] == "HOLD"
        and m_acc_hold["hold_reason_token"] == NW08_HOLD_TOKEN_STRADDLES_FLOOR
        and m_acc_hold["straddles_canonical_15mmhg_floor"] is True
        and abs(float(m_acc_hold["conservative_po2_lower_bound_mmhg"]) - 14.98) < 1e-6
        and m_acc_fail["verdict_from_mesh_policy"] == "FAIL"
        and m_acc_fail["interval_valid"] is True
        and float(m_acc_fail["conservative_po2_upper_bound_mmhg"]) < 15.0
    )
    tests.append({
        "test": "r15_b2_asymptotic_order_p1p5_and_capped_p2p2_acceptance_controls_pass_hold_fail",
        "p1p5_pass_grid_values": [round(x, 9) for x in acc_p15],
        "p1p5_pass_observed_order_p": m_acc_pass["apparent_order_p"],
        "p1p5_pass_richardson_mmhg": m_acc_pass["richardson_extrapolated_mmhg"],
        "p1p5_pass_lower_bound_mmhg": m_acc_pass["conservative_po2_lower_bound_mmhg"],
        "p1p5_pass_verdict": m_acc_pass["verdict_from_mesh_policy"],
        "p2p2_capped_raw_order_p": m_acc_cap2["observed_order_p_raw"],
        "p2p2_capped_effective_order_p": m_acc_cap2["apparent_order_p"],
        "p2p2_capped_verdict": m_acc_cap2["verdict_from_mesh_policy"],
        "p1p5_straddle_verdict": m_acc_hold["verdict_from_mesh_policy"],
        "p1p5_fail_verdict": m_acc_fail["verdict_from_mesh_policy"],
        "passed": ok_14,
    })

    # 15. R16-B1: Strictly reject uncertainty_se <= 0 (== 0) under BOTH inferential models
    #     (`STUDENT_T_ESTIMATED_SE` and `KNOWN_SIGMA_NORMAL`) with
    #     `HOLD_NW08_INFERENTIAL_MODEL_REQUIRES_POSITIVE_SE`, while retaining explicit
    #     `ZERO_UNCERTAINTY_DETERMINISTIC_POINT` for SE == 0 and positive-SE `STUDENT_T_ESTIMATED_SE`!
    r16_b1_zero_rejects: Dict[str, bool] = {}
    for z_tag, z_kw in (
        (
            "student_t_se_zero_float",
            {
                "uncertainty_se": 0.0,
                "ocr_uncertainty_model": NW08_OCR_UNCERTAINTY_MODEL_STUDENT_T,
                "ocr_replicates_n": 6,
            },
        ),
        (
            "student_t_se_zero_int",
            {
                "uncertainty_se": 0,
                "ocr_uncertainty_model": NW08_OCR_UNCERTAINTY_MODEL_STUDENT_T,
                "ocr_degrees_of_freedom": 5,
            },
        ),
        (
            "known_sigma_se_zero_float",
            {
                "uncertainty_se": 0.0,
                "ocr_uncertainty_model": NW08_OCR_UNCERTAINTY_MODEL_KNOWN_SIGMA,
                "ocr_uncertainty_provenance": "calibrated_respirometer_known_sigma_cert_v1",
            },
        ),
        (
            "known_sigma_se_zero_dec",
            {
                "uncertainty_se": Decimal("0.0"),
                "ocr_uncertainty_model": NW08_OCR_UNCERTAINTY_MODEL_KNOWN_SIGMA,
                "ocr_uncertainty_provenance": "calibrated_respirometer_known_sigma_cert_v1",
            },
        ),
    ):
        got_tok = False
        try:
            evaluate_nw08_measured_ocr_trigger(
                "R1_390_stress",
                1.25,
                z_kw["uncertainty_se"],
                baseline_min_face_mmhg=15.261956,
                baseline_vasc_ocr_mult=1.2067,
                p_bed_mmhg=35.0,
                ocr_uncertainty_model=z_kw.get("ocr_uncertainty_model"),
                ocr_replicates_n=z_kw.get("ocr_replicates_n"),
                ocr_degrees_of_freedom=z_kw.get("ocr_degrees_of_freedom"),
                ocr_uncertainty_provenance=z_kw.get("ocr_uncertainty_provenance"),
            )
        except ValueError as exc_z:
            got_tok = NW08_HOLD_TOKEN_INFERENTIAL_REQUIRES_POSITIVE_SE in str(exc_z)
        r16_b1_zero_rejects[z_tag] = got_tok

    t_det_ctrl = evaluate_nw08_measured_ocr_trigger(
        "R2_DZ40",
        1.097,
        0.0,
        baseline_min_face_mmhg=24.0468,
        baseline_vasc_ocr_mult=1.097,
        p_bed_mmhg=40.0,
        ocr_uncertainty_model=NW08_OCR_UNCERTAINTY_MODEL_ZERO_POINT,
    )
    ok_15 = bool(
        all(r16_b1_zero_rejects.values())
        and t_det_ctrl["ocr_uncertainty_model"] == NW08_OCR_UNCERTAINTY_MODEL_ZERO_POINT
        and t_det_ctrl["measured_ocr_u95"] == 1.097
        and t_df5_trig["ocr_uncertainty_model"] == NW08_OCR_UNCERTAINTY_MODEL_STUDENT_T
        and abs(t_df5_trig["measured_ocr_u95"] - 1.370903) <= 1e-6
    )
    tests.append({
        "test": "r16_b1_reject_zero_se_under_student_t_and_known_sigma_retain_deterministic_and_pos_se",
        "zero_se_rejections": r16_b1_zero_rejects,
        "explicit_deterministic_model": t_det_ctrl["ocr_uncertainty_model"],
        "explicit_deterministic_u95": t_det_ctrl["measured_ocr_u95"],
        "positive_se_student_t_n6_u95": t_df5_trig["measured_ocr_u95"],
        "passed": ok_15,
    })

    # 16. R16-M1: Provably outward-conservative upper enclosure on `exact_student_t_one_sided_95(df)`
    #     across df >= 1 (zero inward error at df=1 Cauchy cot(pi/20), df=2, df=5 displaying exact
    #     2.0150484, through df=10**9, plus hostile ambient Decimal context immunity)!
    with _clean_dec_ctx(80, ROUND_HALF_EVEN):
        s5_ref = Decimal(5).sqrt()
        exact_cot_pi20 = (Decimal(5) + Decimal(2) * s5_ref).sqrt() + s5_ref + Decimal(1)
        exact_t2 = (Decimal(162) / Decimal(19)).sqrt()
    t1_val = exact_student_t_one_sided_95(1)
    t2_val = exact_student_t_one_sided_95(2)
    t5_val = exact_student_t_one_sided_95(5)
    t_sweep_ok = bool(
        Decimal.from_float(t1_val) >= exact_cot_pi20
        and Decimal(str(t1_val)) >= exact_cot_pi20
        and Decimal.from_float(t2_val) >= exact_t2
        and Decimal(str(t2_val)) >= exact_t2
        and round(t5_val, 7) == 2.0150484
        and abs(t5_val - 2.015048373333021) < 1e-11
    )
    p95_dec = Decimal("0.95")
    for chk_df in (1, 2, 3, 4, 5, 6, 10, 20, 50, 100, 500, 1000, 2000):
        tv = exact_student_t_one_sided_95(chk_df)
        if not (
            _exact_student_t_cdf_dec(Decimal.from_float(tv), chk_df, 55) >= p95_dec
            and _exact_student_t_cdf_dec(Decimal(str(tv)), chk_df, 55) >= p95_dec
        ):
            t_sweep_ok = False
    for large_df in (2001, 5000, 10000, 100000, 1_000_000, 1_000_000_000):
        tv_l = exact_student_t_one_sided_95(large_df)
        if not (tv_l > float(_EXACT_Z95_UPPER_40D) or Decimal.from_float(tv_l) >= _EXACT_Z95_UPPER_40D):
            t_sweep_ok = False
    # Verify exact_student_t_one_sided_95 is immune to hostile ambient context (prec=4, Inexact trap)
    exact_student_t_one_sided_95.cache_clear()
    hostile_t_ok = True
    with localcontext(Context(prec=4, rounding=ROUND_FLOOR, Emin=-2, Emax=100, traps=[decimal.Inexact])):
        try:
            if exact_student_t_one_sided_95(1) != t1_val or exact_student_t_one_sided_95(5) != t5_val:
                hostile_t_ok = False
        except Exception:
            hostile_t_ok = False
    ok_16 = bool(t_sweep_ok and hostile_t_ok)
    tests.append({
        "test": "r16_m1_student_t_quantile_outward_upper_enclosure_df1_cauchy_df5_and_large_df",
        "t_df1_float": t1_val,
        "t_df1_ge_exact_cot_pi_over_20": bool(Decimal.from_float(t1_val) >= exact_cot_pi20),
        "t_df5_7dp": round(t5_val, 7),
        "all_tested_df_outward_ge_0p95": t_sweep_ok,
        "hostile_decimal_context_immune": hostile_t_ok,
        "passed": ok_16,
    })

    # 17. R16-M3: Explicit `mesh_uncertainty_status` and `order_qualified`/`interval_valid` separation
    #     across 3-grid asymptotic GCI qualified, 3-grid monotone non-asymptotic fallback (R3P_35_VPIO120),
    #     3-grid oscillatory ASME V&V-20 half-range, and 3-grid slow-order unqualified HOLD!
    r3p_std_ladder = (("coarse", 28, 32), ("medium", 42, 48), ("fine", 56, 64))
    m_vpio_fb = _adjudicate_nw08_three_grid_convergence(15.1005, 15.1059, 15.3084, r3p_std_ladder)
    m_osc_r1 = _adjudicate_nw08_three_grid_convergence(15.0741, 14.9753, 15.0638, r2_std_ladder)
    ok_17 = bool(
        m_acc_pass["order_qualified"] is True
        and m_acc_pass["interval_valid"] is True
        and m_acc_pass["grids_run"] == 3
        and m_acc_pass["mesh_uncertainty_status"] == NW08_MESH_STATUS_THREE_GRID_ASYMPTOTIC_GCI_QUALIFIED
        and m_vpio_fb["verdict_from_mesh_policy"] == "PASS"
        and m_vpio_fb["order_qualified"] is False
        and m_vpio_fb["interval_valid"] is True
        and m_vpio_fb["grids_run"] == 3
        and m_vpio_fb["mesh_uncertainty_status"] == NW08_MESH_STATUS_THREE_GRID_MONOTONE_FALLBACK
        and m_osc_r1["verdict_from_mesh_policy"] == "HOLD"
        and m_osc_r1["order_qualified"] is False
        and m_osc_r1["interval_valid"] is True
        and m_osc_r1["mesh_uncertainty_status"] == NW08_MESH_STATUS_THREE_GRID_OSCILLATORY_BOUND
        and m_slow["order_qualified"] is False
        and m_slow["interval_valid"] is False
        and m_slow["mesh_uncertainty_status"] == NW08_MESH_STATUS_THREE_GRID_UNQUALIFIED_ORDER_HOLD
    )
    tests.append({
        "test": "r16_m3_mesh_uncertainty_status_separates_asymptotic_gci_fallback_oscillatory_and_hold",
        "asymptotic_gci_status": m_acc_pass["mesh_uncertainty_status"],
        "monotone_fallback_status": m_vpio_fb["mesh_uncertainty_status"],
        "monotone_fallback_order_qualified": m_vpio_fb["order_qualified"],
        "oscillatory_status": m_osc_r1["mesh_uncertainty_status"],
        "slow_order_hold_status": m_slow["mesh_uncertainty_status"],
        "passed": ok_17,
    })

    # 18. R17-B2: Provably upper-enclosed known-sigma normal quantile z_{0.95} (rejecting old
    #     under-covering 1.6448536269514722 / 1.6448536269514726) AND Sol's exact sub-ULP positive-SE
    #     example (ocr=1.097, SE=1e-20, Student-t df=5) strictly stepping ocr_u95_float > 1.097 and
    #     delta_vasc_pct_u95 > 0.0 while canonical SE=0 deterministic point remains monotone!
    t_r17_sub_ulp_t5 = evaluate_nw08_measured_ocr_trigger(
        "R2_DZ40",
        1.097,
        1e-20,
        baseline_min_face_mmhg=19.19079251185101,
        baseline_vasc_ocr_mult=1.097,
        p_bed_mmhg=40.0,
        ocr_uncertainty_model=NW08_OCR_UNCERTAINTY_MODEL_STUDENT_T,
        ocr_degrees_of_freedom=5,
    )
    t_r17_sub_ulp_ks = evaluate_nw08_measured_ocr_trigger(
        "R2_DZ40",
        1.097,
        1e-20,
        baseline_min_face_mmhg=19.19079251185101,
        baseline_vasc_ocr_mult=1.097,
        p_bed_mmhg=40.0,
        z_one_sided_95=MIN_NW08_CONSERVATIVE_Z_ONE_SIDED_95,
        ocr_uncertainty_model=NW08_OCR_UNCERTAINTY_MODEL_KNOWN_SIGMA,
        ocr_uncertainty_provenance="NIST_CALIBRATED_KNOWN_SIGMA_SEAHORSE_ASSAY",
    )
    t_r17_det0 = evaluate_nw08_measured_ocr_trigger(
        "R2_DZ40",
        1.097,
        0.0,
        baseline_min_face_mmhg=19.19079251185101,
        baseline_vasc_ocr_mult=1.097,
        p_bed_mmhg=40.0,
        ocr_uncertainty_model=NW08_OCR_UNCERTAINTY_MODEL_ZERO_POINT,
    )
    ks_old_z_rejected = False
    try:
        evaluate_nw08_measured_ocr_trigger(
            "R2_DZ40",
            1.097,
            1e-20,