OpenIslet

scripts/verify_iip0034_planar_and_portfolio.py

Deterministic Planar Route 3P Physics, Colony-Screening, Exact Decimal DEC-103 R3P-05 Thinning,

script Release v0.8.0 gen-eb955f8266035aba 111 KB · 8 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_iip0034_planar_and_portfolio.py · SHA-256: 24ff92056312d1e8…

    if not scope_ok:
        st_code = (
            "HOLD_INELIGIBLE_OR_UNREGISTERED_PRODUCT_SCOPE"
            if scope_reason.startswith(("PRODUCT_NOT_ELIGIBLE", "CONSTRUCT_SHA256_MISMATCH"))
            else "HOLD_SCOPE_MISMATCH_NO_CROSS_LOT_OR_DEVICE_POOLING"
        )
        return {
            "status": st_code,
            "reason": scope_reason,
            "k": k if is_int_k else None,
            "n_raw": n_raw if is_int_n else None,
            "cap_fraction": cap_float,
        }
    if pvid == "ISLET1_Y_FULLSTACK_V2_CD55_CFH_COMPARATOR" and backup_gate0v_primary_failed is not True:
        return {
            "status": "HOLD_BACKUP_NOT_ACTIVATED_PRIMARY_GATE0V_NOT_FAILED",
            "reason": "BACKUP_CD55_CFH_REQUIRES_EXPLICIT_PRIMARY_CD46_WT_GATE0V_FAILURE",
            "k": k if is_int_k else None,
            "n_raw": n_raw if is_int_n else None,
            "cap_fraction": cap_float,
        }

    # 4. Strict integer & bounds validation (exact arbitrary-precision integer n_raw supported!)
    if k is None or n_raw is None:
        return {"status": "HOLD_MISSING_ATOMIC_INTEGER_K_OR_N", "cap_fraction": cap_float}
    if not is_int_k or not is_int_n:
        return {"status": "HOLD_NON_INTEGER_COUNT_OR_DENOMINATOR_REJECTED", "cap_fraction": cap_float}
    if n_raw <= 0 or k < 0 or k > n_raw:
        return {"status": "HOLD_INVALID_COUNT_BOUNDS", "cap_fraction": cap_float}

    # 5. Explicit fail-closed sampling independence guard (strict isinstance(..., str) BEFORE equality!)
    if (
        not isinstance(sampling_independence_model, str)
        or sampling_independence_model != "INDEPENDENT_BIOLOGICAL_OPPORTUNITIES"
        or clustered_sampling is not False
    ):
        return {
            "status": "HOLD_MISSING_OR_CLUSTERED_INDEPENDENCE_EVIDENCE",
            "cap_fraction": cap_float,
        }

    # 6. Explicit calibration & provenance check (NO implicit 1.0 default; mandatory f_clonogenic_provenance!)
    if eta_spike_recovery_l95 is None or f_clonogenic is None:
        return {"status": "HOLD_MISSING_RECOVERY_OR_CLONOGENIC_FACTOR", "cap_fraction": cap_float}
    eta_d = _safe_exact_decimal(eta_spike_recovery_l95)
    f_d = _safe_exact_decimal(f_clonogenic)
    if eta_d is None or f_d is None:
        return {"status": "HOLD_NONFINITE_RECOVERY_OR_CLONOGENIC_FACTOR", "cap_fraction": cap_float}
    if not (Decimal(0) < eta_d <= Decimal(1)) or not (Decimal(0) < f_d <= Decimal(1)):
        return {"status": "HOLD_OUT_OF_RANGE_RECOVERY_OR_CLONOGENIC_FACTOR", "cap_fraction": cap_float}
    if (eta_d < Decimal(1) or f_d < Decimal(1)) and eta_d < Decimal("0.30"):
        return {"status": "HOLD_SPIKE_RECOVERY_BELOW_0P30_QUALIFICATION_FLOOR", "cap_fraction": cap_float}

    if not isinstance(f_clonogenic_provenance, str) or f_clonogenic_provenance not in VALID_F_CLONOGENIC_PROVENANCE:
        return {
            "status": "HOLD_MISSING_OR_INVALID_CLONOGENIC_PROVENANCE",
            "reason": "REQUIRES_KNOWN_OR_CONSERVATIVELY_ESTABLISHED_OR_INDEPENDENTLY_ESTIMATED_WITH_ALPHA_ALLOCATION",
            "cap_fraction": cap_float,
        }

    # Resolve Bonferroni alpha budget conservatively (joint error <= 0.10 => >= 90% simultaneous coverage).
    # Because eta_spike_recovery_l95 is an L95 (one-sided 95% LCB, alpha = 0.05) bound whenever eta < 1.0,
    # its Bonferroni error share MUST be >= 0.05 whenever eta < 1.0 (preventing under-allocated L95 recovery claims).
    with localcontext() as ctx:
        ctx.prec = 80
        ctx.rounding = ROUND_CEILING
        if f_clonogenic_provenance == "KNOWN_OR_CONSERVATIVELY_ESTABLISHED":
            a_obs_d = Decimal("0.05") if alpha_obs is None else _safe_exact_decimal(alpha_obs)
            a_eta_d = (
                (Decimal("0.05") if eta_d < Decimal(1) else Decimal("0"))
                if alpha_eta_recovery is None
                else _safe_exact_decimal(alpha_eta_recovery)
            )
            a_f_d = Decimal("0")
            if a_obs_d is None or a_eta_d is None or not (Decimal(0) < a_obs_d <= Decimal("0.05")) or a_eta_d < Decimal(0):
                return {"status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION", "cap_fraction": cap_float}
            if eta_d < Decimal(1) and a_eta_d < Decimal("0.05"):
                return {
                    "status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
                    "reason": "ETA_SPIKE_RECOVERY_L95_REQUIRES_ALPHA_ETA_AT_LEAST_0P05",
                    "cap_fraction": cap_float,
                }
            if (a_obs_d + a_eta_d) > Decimal("0.10"):
                return {
                    "status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
                    "reason": "BONFERRONI_ALPHA_SUM_EXCEEDS_0P10_FOR_90PCT_JOINT_COVERAGE",
                    "cap_fraction": cap_float,
                }
        else:
            a_obs_d = _safe_exact_decimal(alpha_obs)
            a_f_d = _safe_exact_decimal(alpha_f_clonogenic)
            a_eta_d = (
                (Decimal("0.05") if eta_d < Decimal(1) else Decimal("0"))
                if alpha_eta_recovery is None
                else _safe_exact_decimal(alpha_eta_recovery)
            )
            if (
                a_obs_d is None
                or a_f_d is None
                or a_eta_d is None
                or not (Decimal(0) < a_obs_d <= Decimal("0.05"))
                or not (Decimal(0) < a_f_d < Decimal("0.10"))
                or a_eta_d < Decimal(0)
            ):
                return {"status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION", "cap_fraction": cap_float}
            if eta_d < Decimal(1) and a_eta_d < Decimal("0.05"):
                return {
                    "status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
                    "reason": "ETA_SPIKE_RECOVERY_L95_REQUIRES_ALPHA_ETA_AT_LEAST_0P05",
                    "cap_fraction": cap_float,
                }
            if (a_obs_d + a_eta_d + a_f_d) > Decimal("0.10"):
                return {
                    "status": "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
                    "reason": "BONFERRONI_ALPHA_SUM_EXCEEDS_0P10_FOR_90PCT_JOINT_COVERAGE",
                    "cap_fraction": cap_float,
                }

    # 7. Exact 80-digit strictly sound conservative Decimal upper bound:
    # Decimal.ln() and Decimal.exp() always round half-even (< 1 ULP error at prec=80), so applying .next_minus()
    # to BOTH a_obs_d.ln() and q_lo.exp() strictly encloses the true real transcendental values from below,
    # guaranteeing u95_upper > exact_real_U95 for EVERY input (immune to binary64 and high-precision straddles!).
    with localcontext() as ctx:
        ctx.prec = 80
        ctx.rounding = ROUND_FLOOR
        eff_lower = f_d * eta_d
        if eff_lower <= Decimal(0):
            return {"status": "HOLD_UNDERFLOW_OR_INVALID_DETECTION_FACTOR", "cap_fraction": cap_float}
        ln_alpha_lo = a_obs_d.ln().next_minus()  # strictly < exact ln(alpha_obs) < 0
        n_d = Decimal(n_raw)
        q_lo = ln_alpha_lo / n_d  # ROUND_FLOOR on negative numerator -> strictly < exact ln(alpha_obs) / n_raw
        if q_lo > Decimal("-1e-60"):
            # Since 1 - exp(q) <= -q for all q <= 0, -q_lo is a strict conservative positive upper bound even at n=10^308!
            ctx.rounding = ROUND_CEILING
            p_raw_upper = -q_lo
        else:
            exp_lo = q_lo.exp().next_minus()  # strictly < exact exp(q_lo) < exact alpha_obs**(1/n_raw)
            ctx.rounding = ROUND_CEILING
            p_raw_upper = Decimal(1) - exp_lo
        ctx.rounding = ROUND_CEILING
        u95_upper = p_raw_upper / eff_lower
        if u95_upper > Decimal(1):
            u95_upper = Decimal(1)

    u95_str = format(u95_upper, ".16e")
    eff_str = format(eff_lower, ".12g")
    u95_display = float(u95_upper) if u95_upper >= Decimal("1e-300") else None

    if u95_upper > cap_d:
        return {
            "status": "HOLD_INCONCLUSIVE_INSUFFICIENT_INDEPENDENT_DENOMINATOR",
            "empirical_clearance_status": "HOLD_MATHEMATICAL_SUBGATE_AND_EMPIRICAL_RETRIEVAL_PENDING",
            "k": 0,
            "n_raw": n_raw,
            "f_clonogenic_provenance": f_clonogenic_provenance,
            "alpha_obs_str": format(a_obs_d, ".6g"),
            "effective_detection_factor_str": eff_str,
            "u95_exact_dec103_str": u95_str,
            "u95_exact_dec103_display": round(u95_display, 16) if u95_display is not None else None,
            "cap_fraction_str": format(cap_d, ".16e"),
            "cap_fraction": cap_float,
        }
    return {
        "status": "PASS",
        "empirical_clearance_status": "MATHEMATICAL_COUNT_SUBGATE_ONLY_EMPIRICAL_RETRIEVAL_AND_WHOLE_PRODUCT_PENDING",
        "k": 0,
        "n_raw": n_raw,
        "f_clonogenic_provenance": f_clonogenic_provenance,
        "alpha_obs_str": format(a_obs_d, ".6g"),
        "effective_detection_factor_str": eff_str,
        "u95_exact_dec103_str": u95_str,
        "u95_exact_dec103_display": round(u95_display, 16) if u95_display is not None else None,
        "cap_fraction_str": format(cap_d, ".16e"),
        "cap_fraction": cap_float,
    }


def compute_r3p05_dec103_thinning_boundary_audit() -> Dict[str, Any]:
    """Deterministic fail-closed R3P-05 DEC-103 exact 50-digit Decimal boundary & single-dose feasibility audit."""
    valid_scope = {
        "product_variant_id": "ISLET1_Y_FULLSTACK_V2_B2M_11TU",
        "construct_sha256": "e77a358ca5bc55c9e0a3070ab46b115c3314660d3a9be6221ef9e91d698cab4e",
        "donor_seedline_id": "DONOR_SEED_A",
        "mcb_id": "MCB_01",
        "lot_id": "LOT_A",
        "device_assembly_id": "DEV_A",
        "physical_route_id": "ROUTE_3P_MODULAR_PLANAR_ENVELOPE",
    }
    valid_ctx = {
        "f_clonogenic": 1.0,
        "f_clonogenic_provenance": "KNOWN_OR_CONSERVATIVELY_ESTABLISHED",
        "expected_scope": valid_scope,
        "observed_scope": dict(valid_scope),
        "sampling_independence_model": "INDEPENDENT_BIOLOGICAL_OPPORTUNITIES",
        "clustered_sampling": False,
    }
    lot_bad = dict(valid_scope, lot_id="LOT_B")
    dev_bad = dict(valid_scope, device_assembly_id="DEV_B")
    padded_lot = dict(valid_scope, lot_id=" LOT_A ")
    ctrl_char_lot = dict(valid_scope, lot_id="LOT\x00A")
    zwsp_lot = dict(valid_scope, lot_id="LOT\u200bA")
    ctrl_scope = dict(
        valid_scope,
        product_variant_id="ISLET1_Y_FULLSTACK_V2_CTRL_EQUAL_SPACING",
        construct_sha256="d5f69bd613cc0d598b846ba061154dc522b8ec6fb064610cfad01562505d2925",
    )
    backup_scope = dict(
        valid_scope,
        product_variant_id="ISLET1_Y_FULLSTACK_V2_CD55_CFH_COMPARATOR",
        construct_sha256="d7291ca6e610f8251ab04bbf6fc62d7747e7baaf0e680300f46b895920aab9d8",
    )
    extra_key_scope_a = dict(valid_scope, extra_unverified_tag="A")
    extra_key_scope_b = dict(valid_scope, extra_unverified_tag="B")
    nondigest_scope = dict(valid_scope, construct_sha256="not-a-sha256")
    partial_2key = {"lot_id": "LOT_A", "device_assembly_id": "DEV_A"}

    cases = [
        # Single-dose Blueprint feasibility counterexamples (93,350 clonogenic opportunities -> ~32.1x / 64.2x above 1e-6!)
        ("blueprint_single_dose_93350_eta1p0_HOLD_unachievable_in_single_dose", evaluate_r3p05_dec103_thinning(k=0, n_raw=93_350, eta_spike_recovery_l95=1.0, **valid_ctx)),
        ("blueprint_single_dose_93350_eta0p5_HOLD_unachievable_in_single_dose", evaluate_r3p05_dec103_thinning(k=0, n_raw=93_350, eta_spike_recovery_l95=0.50, **valid_ctx)),
        ("blueprint_single_dose_933p5M_raw_f1e4_eta1p0_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=933_500_000, eta_spike_recovery_l95=1.0, f_clonogenic=1e-4, f_clonogenic_provenance="KNOWN_OR_CONSERVATIVELY_ESTABLISHED", expected_scope=valid_scope, observed_scope=dict(valid_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        # Standard exact boundaries
        ("eta1p0_n2995730_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=2_995_730, eta_spike_recovery_l95=1.0, **valid_ctx)),
        ("eta1p0_n2995731_PASS", evaluate_r3p05_dec103_thinning(k=0, n_raw=2_995_731, eta_spike_recovery_l95=1.0, **valid_ctx)),
        ("eta0p5_n5991462_HOLD_rejects_floor_approximation", evaluate_r3p05_dec103_thinning(k=0, n_raw=5_991_462, eta_spike_recovery_l95=0.50, **valid_ctx)),
        ("eta0p5_n5991463_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=5_991_463, eta_spike_recovery_l95=0.50, **valid_ctx)),
        ("eta0p5_n5991464_PASS", evaluate_r3p05_dec103_thinning(k=0, n_raw=5_991_464, eta_spike_recovery_l95=0.50, **valid_ctx)),
        # Binary64 ULP tie counterexample & huge-n non-zero scientific display
        ("binary64_ulp_straddle_n5991464_cap4p999999206572849e7_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=5_991_464, eta_spike_recovery_l95=1.0, max_u95_fraction=4.999999206572849e-7, **valid_ctx)),
        ("huge_n_1e308_nonzero_sci_display_PASS", evaluate_r3p05_dec103_thinning(k=0, n_raw=10**308, eta_spike_recovery_l95=1.0, **valid_ctx)),
        # Provenance & 3-way Bonferroni alpha allocation
        ("missing_f_clonogenic_provenance_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=3_000_000, eta_spike_recovery_l95=1.0, f_clonogenic=1.0, expected_scope=valid_scope, observed_scope=dict(valid_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        ("estimated_f_clono_without_alpha_allocation_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=6_000_000, eta_spike_recovery_l95=0.50, f_clonogenic=1.0, f_clonogenic_provenance="INDEPENDENTLY_ESTIMATED_WITH_ALPHA_ALLOCATION", expected_scope=valid_scope, observed_scope=dict(valid_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        ("estimated_f_clono_alpha_sum_exceeds_0p10_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=8_000_000, eta_spike_recovery_l95=0.50, f_clonogenic=1.0, f_clonogenic_provenance="INDEPENDENTLY_ESTIMATED_WITH_ALPHA_ALLOCATION", alpha_obs=0.04, alpha_f_clonogenic=0.04, expected_scope=valid_scope, observed_scope=dict(valid_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        ("estimated_f_clono_underallocated_l95_eta_0p01_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=10_000_000, eta_spike_recovery_l95=0.50, f_clonogenic=1.0, f_clonogenic_provenance="INDEPENDENTLY_ESTIMATED_WITH_ALPHA_ALLOCATION", alpha_obs=Decimal("0.01"), alpha_eta_recovery=Decimal("0.01"), alpha_f_clonogenic=Decimal("0.08"), expected_scope=valid_scope, observed_scope=dict(valid_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        ("estimated_f_clono_valid_3way_bonferroni_PASS", evaluate_r3p05_dec103_thinning(k=0, n_raw=7_377_760, eta_spike_recovery_l95=0.50, f_clonogenic=1.0, f_clonogenic_provenance="INDEPENDENTLY_ESTIMATED_WITH_ALPHA_ALLOCATION", alpha_obs=Decimal("0.025"), alpha_f_clonogenic=Decimal("0.025"), alpha_eta_recovery=Decimal("0.05"), expected_scope=valid_scope, observed_scope=dict(valid_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        # Observed-zero hard veto & attribution status
        ("k1_n5000000_FAIL_observed_zero_veto", evaluate_r3p05_dec103_thinning(k=1, n_raw=5_000_000, eta_spike_recovery_l95=1.0, **valid_ctx)),
        ("k1_overflow_cap_1e400_FAIL_HARD_STOP", evaluate_r3p05_dec103_thinning(k=1, n_raw=5_000_000, eta_spike_recovery_l95=1.0, max_u95_fraction=10**400, **valid_ctx)),
        ("k0_overflow_cap_1e400_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=3_000_000, eta_spike_recovery_l95=1.0, max_u95_fraction=10**400, **valid_ctx)),
        ("relaxed_cap_0p01_n600_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=600, eta_spike_recovery_l95=0.50, max_u95_fraction=0.01, **valid_ctx)),
        ("nan_cap_fraction_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=1, eta_spike_recovery_l95=1.0, max_u95_fraction=float("nan"), **valid_ctx)),
        # Scope, backup, identifier hygiene & NumPy array resistance
        ("excluded_control_scope_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=3_000_000, eta_spike_recovery_l95=1.0, f_clonogenic=1.0, f_clonogenic_provenance="KNOWN_OR_CONSERVATIVELY_ESTABLISHED", expected_scope=ctrl_scope, observed_scope=dict(ctrl_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        ("inactive_backup_without_gate0v_failure_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=3_000_000, eta_spike_recovery_l95=1.0, f_clonogenic=1.0, f_clonogenic_provenance="KNOWN_OR_CONSERVATIVELY_ESTABLISHED", expected_scope=backup_scope, observed_scope=dict(backup_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        ("activated_backup_with_gate0v_failure_CONDITIONAL_ARITHMETIC_PASS", evaluate_r3p05_dec103_thinning(k=0, n_raw=2_995_731, eta_spike_recovery_l95=1.0, f_clonogenic=1.0, f_clonogenic_provenance="KNOWN_OR_CONSERVATIVELY_ESTABLISHED", expected_scope=backup_scope, observed_scope=dict(backup_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False, backup_gate0v_primary_failed=True)),
        ("extra_scope_key_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=3_000_000, eta_spike_recovery_l95=1.0, f_clonogenic=1.0, f_clonogenic_provenance="KNOWN_OR_CONSERVATIVELY_ESTABLISHED", expected_scope=extra_key_scope_a, observed_scope=extra_key_scope_b, sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        ("non_digest_construct_sha256_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=3_000_000, eta_spike_recovery_l95=1.0, f_clonogenic=1.0, f_clonogenic_provenance="KNOWN_OR_CONSERVATIVELY_ESTABLISHED", expected_scope=nondigest_scope, observed_scope=dict(nondigest_scope), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),
        ("whitespace_padded_lot_id_HOLD", evaluate_r3p05_dec103_thinning(k=0, n_raw=3_000_000, eta_spike_recovery_l95=1.0, f_clonogenic=1.0, f_clonogenic_provenance="KNOWN_OR_CONSERVATIVELY_ESTABLISHED", expected_scope=padded_lot, observed_scope=dict(padded_lot), sampling_independence_model="INDEPENDENT_BIOLOGICAL_OPPORTUNITIES", clustered_sampling=False)),