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…

                            holds,
                            f"HOLD_GATE0V_KILL_{sup_hd}72H_INDETERMINATE_INTERVAL_STRADDLING_CAP_UNDER_CONTROL_HOLD_{reg_tag}_{lin_tag}",
                        )
                        arm_pass = False
                    else:
                        lin_detail["ideal_sveh_72h_infected_aliquot_adjudication"] = "WITHIN_CAP_AT_IDEAL_SVEH_HELD_BY_CONTROL"
        if direct_inf_veh_k is None and _is_strict_int(pt_k) and _is_strict_int(pt_n) and pt_n >= 1000 and 1 <= pt_k <= pt_n:
            lin_detail["infected_cell_escape_mode"] = "CONDITIONAL_INDIRECT_DIAGNOSTIC_ONLY_UNVERIFIED_SURVIVAL_INEQUALITY"
            lin_detail["estimand2_release_authorizing_eligible"] = False
            _append_unique(holds, "HOLD_INDIRECT_CONDITIONAL_ESCAPE_UNSUPPORTED_FOR_RELEASE")
            arm_pass = False
    elif s_veh_72h_l975 is not None and s_veh_72h_l975 >= min_s_veh:
        n_in_eff = _exact_floor_div_by_decimal(n_in, deff)
        lin_detail["aliquot_linkage_id"] = aliquot_id if isinstance(aliquot_id, str) else None
        lin_detail["infected_surveillance_aliquot_linkage_mode"] = aliquot_mode
        lin_detail["pre_ap1903_lineage_input_cells_n"] = n_in
        lin_detail["estimand1_n_treated_eff"] = n_in_eff

        if not (_is_strict_int(surv_n) and 0 <= surv_n <= n_in):
            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}")
            holds.append(f"HOLD_GATE0V_KILL_{sup_hd}INVALID_POST_SURVIVORS_COUNT_{reg_tag}_{lin_tag}")
            arm_pass = False
        elif n_in_eff < 1:
            holds.append(f"HOLD_GATE0V_KILL_{sup_hd}EFFECTIVE_DENOM_ZERO_{reg_tag}_{lin_tag}")
            arm_pass = False
        else:
            b_tot = exact_paired_vehicle_ratio_bounds(
                surv_n,
                n_in,
                v72_v,
                v72_n,
                alpha_treated=alpha_paired_surv,
                alpha_veh=alpha_paired_veh,
                deff=deff,
                prec=80,
            )
            if is_exact_independent and b_tot["l_ratio_dec"] > cap_frac_kill_esc_total:
                _append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}AP1903_72H_INFECTED_ALIQUOT_SURVIVORS_EXCEED_CAP_{reg_tag}_{lin_tag}")
                arm_pass = False
            if not (_is_strict_int(surv_scored_n) and surv_scored_n == surv_n):
                holds.append(f"HOLD_GATE0V_KILL_{sup_hd}SURVIVORS_SCORED_MISMATCH_{reg_tag}_{lin_tag}")
                arm_pass = False
            elif not (_is_strict_int(vk) and 0 <= vk <= surv_scored_n):
                holds.append(f"HOLD_GATE0V_KILL_{sup_hd}INVALID_POST_VIRAL_POS_COUNT_{reg_tag}_{lin_tag}")
                arm_pass = False
            elif eta_kill is not None:
                # ESTIMAND 1 (R06-B2 + R11-B3 + R12-B1): Exact component-probability ratio bound strictly at deff == 1.0 with reconciled sampling basis!
                # B4 budget: alpha_veh(0.025) + alpha_eta(0.005) + alpha_escape(0.005) = 0.035 <= 0.050 (>= 96.5% joint)
                if eta_kill <= Decimal(0):
                    u_esc_total = Decimal(1)
                else:
                    e1_ratio = exact_component_probability_ratio_upper(
                        vk,
                        n_in,
                        alpha_e1_esc,
                        (s_veh_72h_l975, eta_kill),
                        deff=deff,
                        prec=80,
                    )
                    u_esc_total = e1_ratio["u_ratio_dec"]
                lin_detail["estimand1_ratio_bound_method"] = (
                    "exact_component_probability_bonferroni_ratio"
                    if is_exact_independent
                    else "non_authorizing_variance_adjusted_diagnostic_ratio"
                )
                lin_detail["estimand1_bonferroni_alpha_budget"] = {
                    "alpha_veh": str(alpha_paired_veh),
                    "alpha_eta": str(alpha_e1_eta),
                    "alpha_escape": str(alpha_e1_esc),
                    "union_bound_sum": str(alpha_paired_veh + alpha_e1_eta + alpha_e1_esc),
                    "exact_directional_confidence_certified": is_exact_independent,
                    "confidence_scope": arm_confidence_scope,
                    "joint_confidence_lower_bound": "0.965" if is_exact_independent else None,
                    "confidence_statement_type": (
                        "DIRECTIONAL_ONE_SIDED_UPPER_AT_1_MINUS_ALPHA_SUM"
                        if is_exact_independent
                        else (
                            "NON_AUTHORIZING_VARIANCE_ADJUSTED_DIAGNOSTIC_ONLY_NOT_EXACT_CONFIDENCE"
                            if is_clustered_deff_gt_1
                            else arm_confidence_scope
                        )
                    ),
                    "scope": (
                        "per_lineage_per_regime_estimand_not_familywise_across_all_5x2_arms"
                        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"
                        )
                    ),
                }
                lin_detail["estimand1_u_esc_per_total_input_80d"] = format(u_esc_total, "f")[:24]
                lin_detail["post_ap1903_viable_survivors_total_n"] = surv_n
                lin_detail["survivors_scored_n"] = surv_scored_n
                lin_detail["post_ap1903_viable_viral_pos_k"] = vk
                if vk == 0 and eta_kill > Decimal(0) and u_esc_total > cap_frac_kill_esc_total:
                    holds.append(f"HOLD_GATE0V_KILL_{sup_hd}ESTIMAND1_TOTAL_INPUT_TOO_SMALL_{reg_tag}_{lin_tag}")
                    arm_pass = False

                # ESTIMAND 2 (B4 + R06-B1 + R06-B2 + R11-B3 + R12-B1): Conditional per pre-treatment infected cells at risk!
                e2_ratio_factors: Tuple[Decimal, ...] | None = None
                e2_alpha_escape: Decimal | None = None
                if direct_inf_veh_k is not None:
                    if supplied_escape_mode not in (None, "DIRECT_INFECTED_VEHICLE_DENOMINATOR"):
                        holds.append(f"HOLD_GATE0V_KILL_{sup_hd}CONFLICTING_INFECTED_CELL_ESCAPE_MODE_{reg_tag}_{lin_tag}")
                        arm_pass = False
                    if not (_is_strict_int(direct_inf_veh_k) and _is_strict_int(v72_v) and _is_strict_int(v72_n) and 1 <= direct_inf_veh_k <= v72_v <= v72_n):
                        holds.append(f"HOLD_GATE0V_KILL_{sup_hd}INVALID_DIRECT_INFECTED_VEH_SURVIVOR_COUNT_{reg_tag}_{lin_tag}")
                        arm_pass = False
                    else:
                        q_inf_veh_l975 = exact_clopper_pearson_lower_calibrant(direct_inf_veh_k, v72_n, alpha_e2_dir_inf_veh, prec=60)
                        e2_ratio_factors = (q_inf_veh_l975, eta_kill)
                        e2_alpha_escape = alpha_e2_dir_esc
                        lin_detail["infected_cell_escape_mode"] = "DIRECT_INFECTED_VEHICLE_DENOMINATOR"
                        lin_detail["estimand2_denominator_mode"] = (
                            "DIRECT_EXACT_BINOMIAL_RATIO_WITHOUT_INDIRECT_SURVIVAL_INEQUALITY_CONDITIONAL_ON_REPRESENTATIVE_PAIRED_ALIQUOTS"
                        )
                        lin_detail["estimand2_sampling_model_qualifier"] = (
                            "DIRECT_EXACT_BINOMIAL_RATIO_WITHOUT_INDIRECT_SURVIVAL_INEQUALITY_CONDITIONAL_ON_REPRESENTATIVE_PAIRED_ALIQUOTS"
                        )
                        lin_detail["estimand2_ratio_bound_method"] = (
                            "exact_component_probability_bonferroni_ratio"
                            if is_exact_independent
                            else "non_authorizing_variance_adjusted_diagnostic_ratio"
                        )
                        lin_detail["estimand2_release_authorizing_eligible"] = bool(is_exact_independent)
                        lin_detail["estimand2_bonferroni_alpha_budget"] = {
                            "alpha_inf_veh": str(alpha_e2_dir_inf_veh),
                            "alpha_eta": str(alpha_e2_dir_eta),
                            "alpha_escape": str(alpha_e2_dir_esc),
                            "union_bound_sum": str(alpha_e2_dir_inf_veh + alpha_e2_dir_eta + alpha_e2_dir_esc),
                            "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": (
                                "DIRECTIONAL_ONE_SIDED_UPPER_AT_1_MINUS_ALPHA_SUM"
                                if is_exact_independent
                                else (
                                    "NON_AUTHORIZING_VARIANCE_ADJUSTED_DIAGNOSTIC_ONLY_NOT_EXACT_CONFIDENCE"
                                    if is_clustered_deff_gt_1
                                    else arm_confidence_scope
                                )
                            ),
                            "scope": (
                                "per_lineage_per_regime_estimand_not_familywise_across_all_5x2_arms"
                                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"
                                )
                            ),
                        }
                        lin_detail["estimand2_joint_coverage_lower_bound"] = (
                            (
                                "0.950 per lineage x regime estimand at deff == 1.0 (direct exact component-ratio Bonferroni: "
                                "0.025 inf_veh + 0.005 eta + 0.020 escape = 0.050; conditional on valid measured "
                                "fractions, calibrated detection efficiency eta, reconciled sampling-unit basis, and "
                                "representative paired aliquots)"
                            )
                            if is_exact_independent
                            else (
                                "NON-AUTHORIZING / UNCERTIFIED: does NOT certify 0.950 directional confidence "
                                "without a valid reconciled deff == 1.0 sampling-unit basis."
                            )
                        )
                        lin_detail["estimand2_direct_infected_veh_frac_l975"] = _dec_to_float(q_inf_veh_l975, 6)
                        lin_detail["estimand2_identifiability_assumption"] = (
                            "Removes the unverified indirect survival inequality (s_veh_infected >= s_veh_bulk) "
                            "by directly measuring viable infected survivors in the 72-h vehicle control; exact "
                            "binomial component-ratio bound applies strictly at deff == 1.0 under a reconciled "
                            "sampling-unit basis and remains conditional on valid measured fractions, calibrated "
                            "detection efficiency (eta), externally validated within-well independence (for multi-well "
                            "assays), and representative paired aliquots."
                        )
                elif supplied_escape_mode == "DIRECT_INFECTED_VEHICLE_DENOMINATOR":
                    holds.append(f"HOLD_GATE0V_KILL_{sup_hd}MISSING_DIRECT_INFECTED_VEH_SURVIVOR_COUNT_{reg_tag}_{lin_tag}")
                    arm_pass = False
                elif not (_is_strict_int(pt_k) and _is_strict_int(pt_n) and pt_n >= 1000 and 1 <= pt_k <= pt_n):
                    holds.append(f"HOLD_GATE0V_KILL_{sup_hd}MISSING_PRETREAT_INFECTED_DENOMINATOR_{reg_tag}_{lin_tag}")
                    arm_pass = False
                else:
                    # R06-B1: Indirect marginal estimation is DIAGNOSTIC ONLY — NEVER authorizes PASS!
                    s_veh_72h_l985 = exact_clopper_pearson_lower_calibrant(v72_v, v72_n, alpha_e2_ind_veh, prec=60)
                    p_inf_l985 = exact_clopper_pearson_lower_calibrant(pt_k, pt_n, alpha_e2_ind_prev, prec=60)
                    e2_ratio_factors = (s_veh_72h_l985, p_inf_l985, eta_kill)
                    e2_alpha_escape = alpha_e2_ind_esc
                    lin_detail["infected_cell_escape_mode"] = "CONDITIONAL_INDIRECT_DIAGNOSTIC_ONLY_UNVERIFIED_SURVIVAL_INEQUALITY"
                    lin_detail["estimand2_denominator_mode"] = "CONDITIONAL_INDIRECT_DIAGNOSTIC_ONLY_UNVERIFIED_SURVIVAL_INEQUALITY"
                    lin_detail["estimand2_ratio_bound_method"] = (
                        "exact_component_probability_bonferroni_ratio"
                        if is_exact_independent
                        else "non_authorizing_variance_adjusted_diagnostic_ratio"
                    )
                    lin_detail["estimand2_release_authorizing_eligible"] = False
                    lin_detail["estimand2_bonferroni_alpha_budget"] = {
                        "alpha_veh": str(alpha_e2_ind_veh),
                        "alpha_prev": str(alpha_e2_ind_prev),
                        "alpha_eta": str(alpha_e2_ind_eta),
                        "alpha_escape": str(alpha_e2_ind_esc),
                        "union_bound_sum": str(alpha_e2_ind_veh + alpha_e2_ind_prev + alpha_e2_ind_eta + alpha_e2_ind_esc),
                        "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": (
                            "DIRECTIONAL_ONE_SIDED_UPPER_AT_1_MINUS_ALPHA_SUM"
                            if is_exact_independent
                            else (
                                "NON_AUTHORIZING_VARIANCE_ADJUSTED_DIAGNOSTIC_ONLY_NOT_EXACT_CONFIDENCE"
                                if is_clustered_deff_gt_1
                                else arm_confidence_scope
                            )
                        ),
                        "scope": (
                            "diagnostic_only_per_lineage_per_regime_estimand_conditional_on_unverified_survival_inequality"
                            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"
                            )
                        ),
                    }
                    lin_detail["estimand2_joint_coverage_lower_bound"] = (
                        (
                            "0.950 diagnostic-only at deff == 1.0 conditional on unverified s_veh_infected >= s_veh_bulk "
                            "(indirect exact component-ratio Bonferroni: 0.015 veh + 0.015 prev + 0.005 eta + 0.015 escape = 0.050)"
                        )
                        if is_exact_independent
                        else (
                            "NON-AUTHORIZING DIAGNOSTIC ONLY: does NOT certify 0.950 directional confidence."
                        )
                    )
                    lin_detail["estimand2_s_veh_72h_l985"] = _dec_to_float(s_veh_72h_l985, 6)
                    lin_detail["estimand2_pretreat_infected_frac_l985"] = _dec_to_float(p_inf_l985, 6)
                    lin_detail["estimand2_identifiability_assumption"] = (
                        "DIAGNOSTIC ONLY (UNSUPPORTED FOR RELEASE): relies on unverified survival inequality "
                        "s_veh_infected >= s_veh_bulk; supply direct pretreat_infected_veh_survivor_n out of "
                        "veh_72h_input_n (DIRECT_INFECTED_VEHICLE_DENOMINATOR) at deff == 1.0 with reconciled "
                        "sampling-unit partition for release-authorizing PASS."
                    )
                    _append_unique(holds, "HOLD_INDIRECT_CONDITIONAL_ESCAPE_UNSUPPORTED_FOR_RELEASE")
                    _append_unique(
                        holds,
                        f"HOLD_GATE0V_KILL_{sup_hd}INDIRECT_CONDITIONAL_ESCAPE_UNSUPPORTED_FOR_RELEASE_{reg_tag}_{lin_tag}",
                    )
                    arm_pass = False