scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…
# with valid reconciled sampling-unit basis (matching GY-018 and IIP-0044). Clustered/correlated
# lots (deff > 1.0) NEVER certify release PASS!
if is_clustered_deff_gt_1:
_append_unique(holds, "HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE_{reg_tag}_{lin_tag}",
)
arm_pass = False
def _check_ideal_sveh_conclusive_24h_hard_stop() -> None:
"""
R04-B3 + R06-B3 + R07-M1 + R12-B1: Under a concurrent 24-h vehicle-control HOLD (missing, invalid,
or s_veh_l975 < 0.10), ALWAYS enforce deterministic `a24_k > a24_n -> FAIL_HARD_STOP`, and when
`deff` is present evaluate the 3-state ideal s_veh = 1.0 survivor check for ALL 0 <= a24_k <= a24_n:
- Escalate to FAIL_HARD_STOP ONLY when conclusively adverse: `a24_k > a24_n` OR
(when exact confidence is certified) `L_ideal(k_eff, n_eff; alpha_paired_surv) > 0.001`.
- When `L_ideal <= 0.001 < U_ideal`, emit INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD.
"""
if _is_strict_int(a24_n) and a24_n >= 1 and _is_strict_int(a24_k):
if a24_k > a24_n:
_append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_24H_INVALID_SURVIVORS_EXCEED_INPUT_{reg_tag}_{lin_tag}")
lin_detail["conclusive_kill_failure_at_ideal_sveh_24h"] = True
lin_detail["ideal_sveh_24h_adjudication"] = "DETERMINISTIC_HARD_STOP_SURVIVORS_EXCEED_INPUT"
elif deff is None:
lin_detail["conclusive_kill_failure_at_ideal_sveh_24h"] = False
lin_detail["ideal_sveh_24h_adjudication"] = "UNCERTIFIED_MISSING_OR_INVALID_SAMPLING_SPEC"
elif 0 <= a24_k <= a24_n:
n_tr_24_ideal = _exact_floor_div_by_decimal(a24_n, deff)
if n_tr_24_ideal < 1:
lin_detail["conclusive_kill_failure_at_ideal_sveh_24h"] = False
lin_detail["ideal_sveh_24h_adjudication"] = "INDETERMINATE_EFFECTIVE_DENOM_LT_1_UNDER_CONTROL_HOLD"
else:
k_tr_24_eff_lo = (a24_k * n_tr_24_ideal) // a24_n
k_tr_24_eff_hi = (a24_k * n_tr_24_ideal + a24_n - 1) // a24_n
u24_ideal = exact_clopper_pearson_upper_general(k_tr_24_eff_hi, n_tr_24_ideal, alpha_paired_surv, prec=60)
l24_ideal = (
exact_clopper_pearson_lower_calibrant(k_tr_24_eff_lo, n_tr_24_ideal, alpha_paired_surv, prec=60)
if k_tr_24_eff_lo >= 1
else Decimal(0)
)
lin_detail["recomputed_l_surv_24h_ideal_sveh_lower"] = format(l24_ideal, "f")[:24]
lin_detail["recomputed_u_surv_24h_ideal_sveh_upper"] = format(u24_ideal, "f")[:24]
if not is_exact_independent:
lin_detail["conclusive_kill_failure_at_ideal_sveh_24h"] = False
lin_detail["ideal_sveh_24h_adjudication"] = (
"HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE"
if is_clustered_deff_gt_1
else arm_confidence_scope
)
elif l24_ideal > Decimal("0.001"):
_append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_24H_KILL_BELOW_99P9PCT_{reg_tag}_{lin_tag}")
with _clean_dec_ctx(60, ROUND_FLOOR):
kill24_ideal_hi = (Decimal(1) - l24_ideal) * Decimal(100)
lin_detail["recomputed_kill_24h_pct_ideal_sveh_upper_bound"] = _dec_to_float(kill24_ideal_hi, 6)
lin_detail["conclusive_kill_failure_at_ideal_sveh_24h"] = True
lin_detail["ideal_sveh_24h_adjudication"] = "CONCLUSIVE_FAILURE_LOWER_BOUND_EXCEEDS_CAP"
elif u24_ideal > Decimal("0.001"):
lin_detail["conclusive_kill_failure_at_ideal_sveh_24h"] = False
lin_detail["ideal_sveh_24h_adjudication"] = "INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD"
_append_unique(holds, "HOLD_INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}24H_INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD_{reg_tag}_{lin_tag}",
)
else:
lin_detail["conclusive_kill_failure_at_ideal_sveh_24h"] = False
lin_detail["ideal_sveh_24h_adjudication"] = "WITHIN_CAP_AT_IDEAL_SVEH_HELD_BY_CONTROL"
def _check_ideal_sveh_conclusive_72h_hard_stop() -> None:
"""
R04-B3 + R06-B3 + R07-M1 + R12-B1: Under a concurrent 72-h vehicle-control HOLD, ALWAYS enforce
deterministic `a72_k > a72_n -> FAIL_HARD_STOP`, and when `deff` is present evaluate the 3-state
ideal s_veh = 1.0 survivor check for ALL 0 <= a72_k <= a72_n.
"""
if _is_strict_int(a72_n) and a72_n >= 1 and _is_strict_int(a72_k):
if a72_k > a72_n:
_append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_72H_INVALID_SURVIVORS_EXCEED_INPUT_{reg_tag}_{lin_tag}")
lin_detail["conclusive_kill_failure_at_ideal_sveh_72h"] = True
lin_detail["ideal_sveh_72h_adjudication"] = "DETERMINISTIC_HARD_STOP_SURVIVORS_EXCEED_INPUT"
elif deff is None:
lin_detail["conclusive_kill_failure_at_ideal_sveh_72h"] = False
lin_detail["ideal_sveh_72h_adjudication"] = "UNCERTIFIED_MISSING_OR_INVALID_SAMPLING_SPEC"
elif 0 <= a72_k <= a72_n:
n_tr_72_ideal = _exact_floor_div_by_decimal(a72_n, deff)
if n_tr_72_ideal < 1:
lin_detail["conclusive_kill_failure_at_ideal_sveh_72h"] = False
lin_detail["ideal_sveh_72h_adjudication"] = "INDETERMINATE_EFFECTIVE_DENOM_LT_1_UNDER_CONTROL_HOLD"
else:
k_tr_72_eff_lo = (a72_k * n_tr_72_ideal) // a72_n
k_tr_72_eff_hi = (a72_k * n_tr_72_ideal + a72_n - 1) // a72_n
u72_ideal = exact_clopper_pearson_upper_general(k_tr_72_eff_hi, n_tr_72_ideal, alpha_paired_surv, prec=60)
l72_ideal = (
exact_clopper_pearson_lower_calibrant(k_tr_72_eff_lo, n_tr_72_ideal, alpha_paired_surv, prec=60)
if k_tr_72_eff_lo >= 1
else Decimal(0)
)
lin_detail["recomputed_l_surv_72h_ideal_sveh_lower"] = format(l72_ideal, "f")[:24]
lin_detail["recomputed_u_surv_72h_ideal_sveh_upper"] = format(u72_ideal, "f")[:24]
if not is_exact_independent:
lin_detail["conclusive_kill_failure_at_ideal_sveh_72h"] = False
lin_detail["ideal_sveh_72h_adjudication"] = (
"HOLD_CLUSTERED_DEFF_GT_1_REQUIRES_CLUSTER_LEVEL_INFERENCE"
if is_clustered_deff_gt_1
else arm_confidence_scope
)
elif l72_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["conclusive_kill_failure_at_ideal_sveh_72h"] = True
lin_detail["ideal_sveh_72h_adjudication"] = "CONCLUSIVE_FAILURE_LOWER_BOUND_EXCEEDS_CAP"
elif u72_ideal > cap_frac_kill_esc_total:
lin_detail["conclusive_kill_failure_at_ideal_sveh_72h"] = False
lin_detail["ideal_sveh_72h_adjudication"] = "INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD"
_append_unique(holds, "HOLD_INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}72H_INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD_{reg_tag}_{lin_tag}",
)
else:
lin_detail["conclusive_kill_failure_at_ideal_sveh_72h"] = False
lin_detail["ideal_sveh_72h_adjudication"] = "WITHIN_CAP_AT_IDEAL_SVEH_HELD_BY_CONTROL"
# 1. Paired 24-h vehicle-adjusted kill check via EXACT component-probability ratio bound (R06-B2; Bonferroni 0.025 + 0.025 = 0.050)
if not (
_is_strict_int(v24_v) and _is_strict_int(v24_n) and v24_n >= 100 and 0 <= v24_v <= v24_n
and _is_strict_int(a24_n) and a24_n >= 1
and _is_strict_int(a24_k) and 0 <= a24_k <= a24_n
):
holds.append(f"HOLD_GATE0V_KILL_{sup_hd}RAW_24H_COUNTS_REQUIRED_{reg_tag}_{lin_tag}")
arm_pass = False
_check_ideal_sveh_conclusive_24h_hard_stop()
elif deff is None:
arm_pass = False
lin_detail["ratio_bound_method_24h"] = "uncertified_missing_or_invalid_sampling_spec"
lin_detail["bonferroni_alpha_budget_24h"] = {
"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:
b24 = exact_paired_vehicle_ratio_bounds(
a24_k,
a24_n,
v24_v,
v24_n,
alpha_treated=alpha_paired_surv,
alpha_veh=alpha_paired_veh,
deff=deff,
prec=80,
)
s_veh_24h_l975 = b24["s_veh_lower_dec"]
lin_detail["recomputed_s_veh_24h_l975"] = _dec_to_float(s_veh_24h_l975, 6)
lin_detail["ratio_bound_method_24h"] = (
"exact_component_probability_bonferroni_ratio"
if is_exact_independent
else "non_authorizing_variance_adjusted_diagnostic_ratio"
)
lin_detail["bonferroni_alpha_budget_24h"] = {
"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": (
b24["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_24h_l975 < min_s_veh:
holds.append(f"HOLD_GATE0V_KILL_{sup_hd}24H_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.001 (conclusively adverse when exact certified)!
_check_ideal_sveh_conclusive_24h_hard_stop()
elif b24["n_treated_eff"] < 1:
holds.append(f"HOLD_GATE0V_KILL_{sup_hd}24H_INVALID_EFFECTIVE_DENOM_{reg_tag}_{lin_tag}")
arm_pass = False
_check_ideal_sveh_conclusive_24h_hard_stop()
else:
u_ratio_24h = b24["u_ratio_dec"]
l_ratio_24h = b24["l_ratio_dec"]
with _clean_dec_ctx(80, ROUND_FLOOR):
kill_24h_l95_joint = (Decimal(1) - u_ratio_24h) * Decimal(100)
lin_detail["recomputed_n_treated_24h"] = b24["n_treated_eff"]
lin_detail["recomputed_u_treated_24h_upper"] = format(b24["u_treated_dec"], "f")[:24]
lin_detail["recomputed_u_ratio_24h_joint_upper"] = format(u_ratio_24h, "f")[:24]
lin_detail["recomputed_l_ratio_24h_joint_lower"] = format(l_ratio_24h, "f")[:24]
lin_detail["recomputed_kill_24h_pct_joint_l95"] = _dec_to_float(kill_24h_l95_joint, 6)
lin_detail["ratio_estimand_domain_24h"] = "[0, +infinity)"
lin_detail["u_ratio_clamped_to_unity_24h"] = False
lin_detail["treated_survival_exceeds_vehicle_survival_24h"] = b24["treated_survival_exceeds_vehicle_survival"]
lin_detail["ratio_lower_limit_exceeds_unity_24h"] = b24["ratio_lower_limit_exceeds_unity"]
lin_detail["ratio_upper_limit_exceeds_unity_24h"] = b24["ratio_upper_limit_exceeds_unity"]
lin_detail["ratio_bounds_ordered_l_le_u_24h"] = b24["bounds_ordered_l_le_u"]
lin_detail["ratio_bounds_ordered_l_le_point_le_u_24h"] = b24["bounds_ordered_l_le_point_le_u"]
if not is_exact_independent:
lin_detail["diagnostic_24h_empirical_ratio_exceeds_cap"] = bool(
b24["empirical_ratio_point_estimate_dec"] is not None
and b24["empirical_ratio_point_estimate_dec"] > Decimal("0.001")
)
arm_pass = False
elif u_ratio_24h > Decimal("0.001"):
if l_ratio_24h > Decimal("0.001"):
_append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_24H_KILL_BELOW_99P9PCT_{reg_tag}_{lin_tag}")
elif a24_k == 0:
holds.append(f"HOLD_GATE0V_KILL_{sup_hd}24H_UNDERPOWERED_N_EFF_{reg_tag}_{lin_tag}")
else:
holds.append(f"HOLD_GATE0V_KILL_{sup_hd}24H_INDETERMINATE_INTERVAL_STRADDLING_CAP_{reg_tag}_{lin_tag}")
arm_pass = False
# 2. Paired 72-h / pulsed-PK vehicle-adjusted kill check via EXACT component-probability ratio bound (R06-B2; Bonferroni 0.025 + 0.025 = 0.050)
s_veh_72h_l975: Decimal | None = None
s_veh_72h_u975: Decimal | None = None
if not (
_is_strict_int(v72_v) and _is_strict_int(v72_n) and v72_n >= 100 and 0 <= v72_v <= v72_n
and _is_strict_int(a72_n) and a72_n >= 1
and _is_strict_int(a72_k) and 0 <= a72_k <= a72_n
):
holds.append(f"HOLD_GATE0V_KILL_{sup_hd}RAW_72H_COUNTS_REQUIRED_{reg_tag}_{lin_tag}")
arm_pass = False
_check_ideal_sveh_conclusive_72h_hard_stop()
elif deff is None: