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…

        arm_pass = False
        lin_detail["ratio_bound_method_72h"] = "uncertified_missing_or_invalid_sampling_spec"
        lin_detail["bonferroni_alpha_budget_72h"] = {
            "alpha_veh": str(alpha_paired_veh),
            "alpha_surv": str(alpha_paired_surv),
            "union_bound_sum": str(alpha_paired_veh + alpha_paired_surv),
            "exact_directional_confidence_certified": False,
            "confidence_scope": arm_confidence_scope,
            "joint_confidence_lower_bound": None,
            "confidence_statement_type": "UNCERTIFIED_MISSING_OR_INVALID_SAMPLING_SPEC",
            "scope": "uncertified_missing_or_invalid_sampling_spec",
        }
    else:
        b72 = exact_paired_vehicle_ratio_bounds(
            a72_k,
            a72_n,
            v72_v,
            v72_n,
            alpha_treated=alpha_paired_surv,
            alpha_veh=alpha_paired_veh,
            deff=deff,
            prec=80,
        )
        s_veh_72h_l975 = b72["s_veh_lower_dec"]
        s_veh_72h_u975 = b72["s_veh_upper_dec"]
        lin_detail["recomputed_s_veh_72h_l975"] = _dec_to_float(s_veh_72h_l975, 6)
        lin_detail["ratio_bound_method_72h"] = (
            "exact_component_probability_bonferroni_ratio"
            if is_exact_independent
            else "non_authorizing_variance_adjusted_diagnostic_ratio"
        )
        lin_detail["bonferroni_alpha_budget_72h"] = {
            "alpha_veh": str(alpha_paired_veh),
            "alpha_surv": str(alpha_paired_surv),
            "union_bound_sum": str(alpha_paired_veh + alpha_paired_surv),
            "exact_directional_confidence_certified": is_exact_independent,
            "confidence_scope": arm_confidence_scope,
            "joint_confidence_lower_bound": "0.950" if is_exact_independent else None,
            "confidence_statement_type": (
                b72["confidence_statement_type"]
                if is_exact_independent
                else (
                    "NON_AUTHORIZING_VARIANCE_ADJUSTED_DIAGNOSTIC_ONLY_NOT_EXACT_CONFIDENCE"
                    if is_clustered_deff_gt_1
                    else arm_confidence_scope
                )
            ),
            "directional_one_sided_confidence_note": (
                "Upper (u_ratio) and lower (l_ratio) ratio limits are separate directional one-sided "
                "confidence statements at >= 1 - (alpha_veh + alpha_surv) = 0.950 each strictly at "
                "deff == 1.0 under a reconciled per-estimand sampling-unit basis, NOT a simultaneous "
                "two-sided interval at that unsplit alpha."
                if is_exact_independent
                else "NON-AUTHORIZING / UNCERTIFIED: exact directional Clopper-Pearson confidence is "
                "NOT certified without a valid reconciled deff == 1.0 sampling-unit basis."
            ),
            "scope": (
                "per_lineage_per_regime_estimand_deff_eq_1"
                if is_exact_independent
                else (
                    "non_authorizing_variance_adjusted_diagnostic_only_deff_gt_1"
                    if is_clustered_deff_gt_1
                    else "uncertified_sampling_unit_basis"
                )
            ),
        }
        if s_veh_72h_l975 < min_s_veh:
            holds.append(f"HOLD_GATE0V_KILL_{sup_hd}72H_VEHICLE_SURVIVAL_L975_BELOW_0P10_{reg_tag}_{lin_tag}")
            arm_pass = False
            # R04-B3 + R06-B3: Escalate to FAIL_HARD_STOP ONLY when L_ideal > 0.0001 (conclusively adverse when exact certified)!
            _check_ideal_sveh_conclusive_72h_hard_stop()
        elif b72["n_treated_eff"] < 1:
            holds.append(f"HOLD_GATE0V_KILL_{sup_hd}72H_INVALID_EFFECTIVE_DENOM_{reg_tag}_{lin_tag}")
            arm_pass = False
            _check_ideal_sveh_conclusive_72h_hard_stop()
        else:
            u_ratio_72h = b72["u_ratio_dec"]
            l_ratio_72h = b72["l_ratio_dec"]
            lin_detail["recomputed_n_treated_72h"] = b72["n_treated_eff"]
            lin_detail["recomputed_u_treated_72h_upper"] = format(b72["u_treated_dec"], "f")[:24]
            lin_detail["recomputed_u_surv_72h_joint_upper"] = format(u_ratio_72h, "f")[:24]
            lin_detail["recomputed_l_surv_72h_joint_lower"] = format(l_ratio_72h, "f")[:24]
            lin_detail["recomputed_kill_72h_clears_4log10"] = bool(is_exact_independent and u_ratio_72h <= cap_frac_kill_esc_total)
            lin_detail["ratio_estimand_domain_72h"] = "[0, +infinity)"
            lin_detail["u_ratio_clamped_to_unity_72h"] = False
            lin_detail["treated_survival_exceeds_vehicle_survival_72h"] = b72["treated_survival_exceeds_vehicle_survival"]
            lin_detail["ratio_lower_limit_exceeds_unity_72h"] = b72["ratio_lower_limit_exceeds_unity"]
            lin_detail["ratio_upper_limit_exceeds_unity_72h"] = b72["ratio_upper_limit_exceeds_unity"]
            lin_detail["ratio_bounds_ordered_l_le_u_72h"] = b72["bounds_ordered_l_le_u"]
            lin_detail["ratio_bounds_ordered_l_le_point_le_u_72h"] = b72["bounds_ordered_l_le_point_le_u"]
            if not is_exact_independent:
                lin_detail["diagnostic_72h_empirical_ratio_exceeds_cap"] = bool(
                    b72["empirical_ratio_point_estimate_dec"] is not None
                    and b72["empirical_ratio_point_estimate_dec"] > cap_frac_kill_esc_total
                )
                arm_pass = False
            elif u_ratio_72h > cap_frac_kill_esc_total:
                if l_ratio_72h > cap_frac_kill_esc_total:
                    _append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_72H_PK_KILL_BELOW_4LOG10_{reg_tag}_{lin_tag}")
                elif a72_k == 0:
                    holds.append(f"HOLD_GATE0V_KILL_{sup_hd}72H_UNDERPOWERED_N_EFF_{reg_tag}_{lin_tag}")
                else:
                    holds.append(f"HOLD_GATE0V_KILL_{sup_hd}72H_INDETERMINATE_INTERVAL_STRADDLING_CAP_{reg_tag}_{lin_tag}")
                arm_pass = False

    # 3. Infected-arm adjudication (always enforced for INFECTED_AP1903_REGIMES, plus
    #    unconditional adverse checks if infected-arm fields are submitted on any other arm!)
    if not is_infected_arm:
        casp_extra = _to_exact_decimal_nonneg(casp_fold_raw)
        if casp_extra is not None and casp_extra < Decimal("5.0"):
            hard_stops.append(f"FAIL_HARD_STOP_{sup_hs}VIRAL_CASPASE_BLOCKADE_REQUIRES_NON_CASPASE_BACKUP_SWITCH_{reg_tag}_{lin_tag}")
            arm_pass = False
        if _is_strict_int(vk_raw) and vk_raw >= 1:
            hard_stops.append(f"FAIL_HARD_STOP_{sup_hs}POST_AP1903_VIABLE_VIRAL_SURVIVOR_{reg_tag}_{lin_tag}")
            arm_pass = False
        return arm_pass, lin_detail

    # Infected arm: full caspase-9/3 blockade, viral survivor, aliquot linkage, dual calibrant,
    # Estimand 1, and Estimand 2 adjudication (identical rigor for required & supplemental arms!)
    casp_fold = _to_exact_decimal_nonneg(casp_fold_raw)
    casp_sha = lblock.get("viral_caspase_stratification_evidence_sha256")
    if casp_fold is None or casp_fold < Decimal(0) or casp_fold > Decimal("10000"):
        holds.append(f"HOLD_GATE0V_KILL_{sup_hd}MISSING_CASPASE9_3_ACTIVITY_{reg_tag}_{lin_tag}")
        arm_pass = False
    elif casp_fold < Decimal("5.0"):
        hard_stops.append(
            f"FAIL_HARD_STOP_{sup_hs}VIRAL_CASPASE_BLOCKADE_REQUIRES_NON_CASPASE_BACKUP_SWITCH_{reg_tag}_{lin_tag}"
        )
        arm_pass = False
    if not _is_valid_sha256_hex(casp_sha):
        holds.append(f"HOLD_GATE0V_KILL_{sup_hd}MISSING_VIRAL_CASPASE_EVIDENCE_SHA256_{reg_tag}_{lin_tag}")
        arm_pass = False

    surv_n = lblock.get("post_ap1903_viable_survivors_total_n")
    surv_scored_n = lblock.get("survivors_scored_n")
    vk = vk_raw
    aliquot_id = lblock.get("aliquot_linkage_id")
    aliquot_mode = lblock.get("infected_surveillance_aliquot_linkage_mode")
    supplied_escape_mode = lblock.get("infected_cell_escape_mode")
    pt_k = lblock.get("pretreat_viral_pos_k")
    pt_n = lblock.get("pretreat_scored_n")
    pre_dissoc_k = lblock.get("pre_dissociation_spike_in_attested")
    spike_k_sha = lblock.get("pre_dissociation_spike_sop_evidence_sha256")

    # Unconditional hard-stop veto on ANY viable viral-positive survivor (never masked by HOLD!)
    if _is_strict_int(vk) and vk >= 1:
        _append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}POST_AP1903_VIABLE_VIRAL_SURVIVOR_{reg_tag}_{lin_tag}")
        arm_pass = False

    # R07-B2: Strictly enforce the published SAME_72H_ALIQUOT count-reconciliation rule for
    # release authorization (`n_in == a72_n` and `surv_n == a72_k`). Any non-same-aliquot mode
    # (e.g. PAIRED_HIGH_INPUT_ALIQUOT, even with a syntactically valid 64-hex digest) fails closed
    # to non-authorizing HOLD (`HOLD_NON_SAME_ALIQUOT_UNVERIFIED_COUNT_RECONCILIATION`) and NEVER
    # authorizes PASS, while downstream adverse checks still run to preserve FAIL_HARD_STOP!
    aliquot_reconciled_ok = False
    if not _is_valid_identifier(aliquot_id):
        _append_unique(holds, f"HOLD_GATE0V_KILL_{sup_hd}MISSING_ALIQUOT_LINKAGE_{reg_tag}_{lin_tag}")
        arm_pass = False
    if aliquot_mode != "SAME_72H_ALIQUOT":
        if not _is_valid_identifier(aliquot_mode):
            _append_unique(holds, f"HOLD_GATE0V_KILL_{sup_hd}MISSING_ALIQUOT_LINKAGE_{reg_tag}_{lin_tag}")
        _append_unique(holds, "HOLD_NON_SAME_ALIQUOT_UNVERIFIED_COUNT_RECONCILIATION")
        _append_unique(
            holds,
            f"HOLD_GATE0V_KILL_{sup_hd}NON_SAME_ALIQUOT_UNVERIFIED_COUNT_RECONCILIATION_{reg_tag}_{lin_tag}",
        )
        if n_in != a72_n or surv_n != a72_k:
            _append_unique(holds, f"HOLD_GATE0V_KILL_{sup_hd}ALIQUOT_SURVIVOR_COUNT_MISMATCH_{reg_tag}_{lin_tag}")
        arm_pass = False
    else:
        if n_in != a72_n or surv_n != a72_k:
            _append_unique(holds, f"HOLD_GATE0V_KILL_{sup_hd}ALIQUOT_SURVIVOR_COUNT_MISMATCH_{reg_tag}_{lin_tag}")
            arm_pass = False
        elif _is_valid_identifier(aliquot_id):
            aliquot_reconciled_ok = True
    lin_detail["aliquot_count_reconciliation_verified"] = aliquot_reconciled_ok
    lin_detail["aliquot_release_authorizing_eligible"] = aliquot_reconciled_ok

    # Always adjudicate pre-dissociation spike attestation & dual calibrant independently of vehicle survival
    if pre_dissoc_k is not True or not _is_valid_sha256_hex(spike_k_sha):
        holds.append(f"HOLD_GATE0V_KILL_{sup_hd}PRE_DISSOCIATION_SPIKE_OR_DIGEST_MISSING_{reg_tag}_{lin_tag}")
        arm_pass = False
    eta_kill: Decimal | None = None
    if not (
        _is_strict_int(rk) and _is_strict_int(rn) and rn >= 1 and 0 <= rk <= rn
        and _is_strict_int(frk) and _is_strict_int(frn) and frn >= 1 and 0 <= frk <= frn
    ):
        holds.append(f"HOLD_GATE0V_KILL_{sup_hd}MISSING_OR_INVALID_DUAL_CALIBRANT_{reg_tag}_{lin_tag}")
        arm_pass = False
    else:
        # Intersection-union principle: min(L_h(alpha_eta), L_f(alpha_eta)) has error <= alpha_eta!
        eta_k_h = exact_clopper_pearson_lower_calibrant(rk, rn, alpha_e1_eta, prec=80)
        eta_k_f = exact_clopper_pearson_lower_calibrant(frk, frn, alpha_e1_eta, prec=80)
        eta_kill = min(eta_k_h, eta_k_f)
        lin_detail["eta_kill_effective_lower_80d"] = format(eta_kill, "f")[:24]
        if eta_kill < min_eta_cal:
            holds.append(f"HOLD_GATE0V_KILL_{sup_hd}CALIBRANT_BELOW_0P80_{reg_tag}_{lin_tag}")
            arm_pass = False

    if not (_is_strict_int(n_in) and n_in >= 1):
        holds.append(f"HOLD_GATE0V_KILL_{sup_hd}MISSING_PRE_AP1903_INPUT_CELLS_{reg_tag}_{lin_tag}")
        arm_pass = False
    elif deff is None:
        # R12-B1: Preserve deterministic `surv_n > n_in` hard stop even when sampling spec is missing/invalid!
        if _is_strict_int(surv_n) and surv_n > n_in:
            _append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_72H_INFECTED_ALIQUOT_SURVIVORS_EXCEED_INPUT_{reg_tag}_{lin_tag}")
            lin_detail["ideal_sveh_72h_infected_aliquot_adjudication"] = "DETERMINISTIC_HARD_STOP_SURVIVORS_EXCEED_INPUT"
        else:
            lin_detail["ideal_sveh_72h_infected_aliquot_adjudication"] = "UNCERTIFIED_MISSING_OR_INVALID_SAMPLING_SPEC"
        lin_detail["estimand2_release_authorizing_eligible"] = False
        arm_pass = False
    elif s_veh_72h_l975 is None or s_veh_72h_l975 < min_s_veh:
        # R04-B3 + R06-B3 + R07-M1: Under a concurrent 72-h vehicle-control HOLD, evaluate the
        # 3-state ideal s_veh = 1.0 check for ALL 0 <= surv_n <= n_in (including zero-event
        # straddles such as 0/1,000!). Escalate to FAIL_HARD_STOP ONLY when conclusively adverse!
        if _is_strict_int(surv_n):
            if surv_n > n_in:
                _append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_72H_INFECTED_ALIQUOT_SURVIVORS_EXCEED_INPUT_{reg_tag}_{lin_tag}")
                lin_detail["ideal_sveh_72h_infected_aliquot_adjudication"] = "DETERMINISTIC_HARD_STOP_SURVIVORS_EXCEED_INPUT"
                arm_pass = False
            elif 0 <= surv_n <= n_in:
                n_tot_ideal = _exact_floor_div_by_decimal(n_in, deff)
                if n_tot_ideal < 1:
                    lin_detail["ideal_sveh_72h_infected_aliquot_adjudication"] = "INDETERMINATE_EFFECTIVE_DENOM_LT_1_UNDER_CONTROL_HOLD"
                else:
                    k_tot_eff_lo = (surv_n * n_tot_ideal) // n_in
                    k_tot_eff_hi = (surv_n * n_tot_ideal + n_in - 1) // n_in
                    u_tot_ideal = exact_clopper_pearson_upper_general(k_tot_eff_hi, n_tot_ideal, alpha_paired_surv, prec=60)
                    l_tot_ideal = (
                        exact_clopper_pearson_lower_calibrant(k_tot_eff_lo, n_tot_ideal, alpha_paired_surv, prec=60)
                        if k_tot_eff_lo >= 1
                        else Decimal(0)
                    )
                    lin_detail["recomputed_l_surv_72h_infected_aliquot_ideal_sveh_lower"] = format(l_tot_ideal, "f")[:24]
                    lin_detail["recomputed_u_surv_72h_infected_aliquot_ideal_sveh_upper"] = format(u_tot_ideal, "f")[:24]
                    if not is_exact_independent:
                        lin_detail["ideal_sveh_72h_infected_aliquot_adjudication"] = (
                            "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE"
                            if is_clustered_deff_gt_1
                            else arm_confidence_scope
                        )
                        arm_pass = False
                    elif l_tot_ideal > cap_frac_kill_esc_total:
                        _append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_72H_PK_KILL_BELOW_4LOG10_{reg_tag}_{lin_tag}")
                        lin_detail["ideal_sveh_72h_infected_aliquot_adjudication"] = "CONCLUSIVE_FAILURE_LOWER_BOUND_EXCEEDS_CAP"
                        arm_pass = False
                    elif u_tot_ideal > cap_frac_kill_esc_total:
                        lin_detail["ideal_sveh_72h_infected_aliquot_adjudication"] = "INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD"
                        _append_unique(holds, "HOLD_INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD")
                        _append_unique(