OpenIslet

scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py

IIP-0042 / DEC-121 (CAND-033 / EXP-030): Master Exact 2^20 Multi-Layer Portfolio

script Release v0.8.0 gen-eb955f8266035aba 881 KB · 63 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_iip0042_master_multilayer_portfolio_crosswalk.py · SHA-256: 16964a158458b5cf…

            n_batch_c, k_batch_ge70, k_batch_lt40, l95_batch_pass, u95_batch_pass, l95_batch_fail = _cluster_cp(batch_clusters)
            n_run_c, k_run_ge70, k_run_lt40, l95_run_pass, u95_run_pass, l95_run_fail = _cluster_cp(run_clusters)
            n_joint_c, k_joint_ge70, k_joint_lt40, l95_joint_pass, u95_joint_pass, l95_joint_fail = _cluster_cp(joint_clusters)

            eff_sup_l95_cp_pct = min(sup_l95_cp_donor_pct, l95_batch_pass, l95_run_pass, l95_joint_pass)
            eff_sup_u95_cp_pct = max(sup_u95_cp_donor_pct, u95_batch_pass, u95_run_pass, u95_joint_pass)
            eff_sup_lt40_l95_cp_pct = min(sup_lt40_l95_cp_donor_pct, l95_batch_fail, l95_run_fail, l95_joint_fail)

            derived_summary["suppression_responders_ge_70pct_k"] = k_ge_70
            derived_summary["suppression_nonresponders_lt_40pct_k"] = k_lt_40
            derived_summary["suppression_responder_denominator_n_roster"] = n_roster
            derived_summary["suppression_donor_level_l95_clopper_pearson_pct"] = round(sup_l95_cp_donor_pct, 6)
            derived_summary["suppression_donor_level_nonresponder_lt40_l95_clopper_pearson_pct"] = round(sup_lt40_l95_cp_donor_pct, 6)
            derived_summary["suppression_batch_clusters_n"] = n_batch_c
            derived_summary["suppression_batch_clusters_ge_70pct_k"] = k_batch_ge70
            derived_summary["suppression_batch_clusters_lt_40pct_k"] = k_batch_lt40
            derived_summary["suppression_batch_cluster_l95_clopper_pearson_pct"] = round(l95_batch_pass, 6)
            derived_summary["suppression_batch_cluster_nonresponder_lt40_l95_clopper_pearson_pct"] = round(l95_batch_fail, 6)
            derived_summary["suppression_run_clusters_n"] = n_run_c
            derived_summary["suppression_run_clusters_ge_70pct_k"] = k_run_ge70
            derived_summary["suppression_run_clusters_lt_40pct_k"] = k_run_lt40
            derived_summary["suppression_run_cluster_l95_clopper_pearson_pct"] = round(l95_run_pass, 6)
            derived_summary["suppression_run_cluster_nonresponder_lt40_l95_clopper_pearson_pct"] = round(l95_run_fail, 6)
            derived_summary["suppression_independent_joint_clusters_n"] = n_joint_c
            derived_summary["suppression_independent_joint_clusters_ge_70pct_k"] = k_joint_ge70
            derived_summary["suppression_independent_joint_clusters_lt_40pct_k"] = k_joint_lt40
            derived_summary["suppression_independent_joint_cluster_l95_clopper_pearson_pct"] = round(l95_joint_pass, 6)
            derived_summary["suppression_independent_joint_cluster_nonresponder_lt40_l95_clopper_pearson_pct"] = round(l95_joint_fail, 6)
            derived_summary["suppression_responder_l95_clopper_pearson_pct"] = round(eff_sup_l95_cp_pct, 6)
            derived_summary["suppression_responder_u95_clopper_pearson_pct"] = round(eff_sup_u95_cp_pct, 6)
            derived_summary["suppression_nonresponder_lt40_l95_clopper_pearson_pct"] = round(eff_sup_lt40_l95_cp_pct, 6)

            if authorization_grade_suppression_inference_valid:
                if eff_sup_lt40_l95_cp_pct > _SHIELD_SUPPRESSION_FAIL_L95_GT_PCT:
                    donor_based_bio_fail = True
                    _escalate(
                        "FAIL",
                        f"T-cell suppression cluster-conservative Clopper-Pearson non-responder (<40%) "
                        f"L95={eff_sup_lt40_l95_cp_pct:.3f}% > {_SHIELD_SUPPRESSION_FAIL_L95_GT_PCT:.1f}% FAIL bar "
                        f"(donors={k_lt_40}/{n_roster}, batch_clusters={k_batch_lt40}/{n_batch_c}, "
                        f"run_clusters={k_run_lt40}/{n_run_c}, joint_clusters={k_joint_lt40}/{n_joint_c})",
                    )
                else:
                    if sup_min_v is not None and sup_min_v < 0.0:
                        _escalate(
                            "HOLD",
                            f"T-cell suppression anti-enhancement safety guard triggered: min_donor_suppression={sup_min_v:.2f}% < 0.0% "
                            f"(no donor may exhibit T-cell enhancement)",
                        )
                    if n_roster < _SHIELD_MIN_CONFIRMATORY_DONORS_N:
                        _escalate(
                            "HOLD",
                            f"Exploratory/underpowered T-cell donor cohort (N_roster={n_roster} < {_SHIELD_MIN_CONFIRMATORY_DONORS_N}): "
                            f"exact Clopper-Pearson responder L95={eff_sup_l95_cp_pct:.3f}% < 70.0% confirmatory PASS bar "
                            f"({k_ge_70}/{n_roster} donors >= 70.0%; Student-t is strictly non-authorizing)",
                        )
                    elif n_joint_c < _SHIELD_MIN_CONFIRMATORY_DONORS_N:
                        _escalate(
                            "HOLD",
                            f"Clustered between-donor batch/run structure precludes confirmatory suppression PASS: "
                            f"N_roster={n_roster} collapses to N_batch_clusters={n_batch_c}, N_run_clusters={n_run_c}, "
                            f"N_joint_clusters={n_joint_c} (< {_SHIELD_MIN_CONFIRMATORY_DONORS_N} independent batch/run "
                            f"units required; effective cluster-conservative L95_CP={eff_sup_l95_cp_pct:.3f}% < 70.0%)",
                        )
                    elif eff_sup_l95_cp_pct < _SHIELD_SUPPRESSION_PASS_THRESHOLD_PCT:
                        _escalate(
                            "HOLD",
                            f"T-cell suppression cluster-conservative Clopper-Pearson responder L95={eff_sup_l95_cp_pct:.3f}% "
                            f"< 70.0% confirmatory PASS bar (donors={k_ge_70}/{n_roster}, batch_clusters={k_batch_ge70}/{n_batch_c}, "
                            f"run_clusters={k_run_ge70}/{n_run_c}, joint_clusters={k_joint_ge70}/{n_joint_c})",
                        )
                    if sup_mean_v is not None and sup_mean_v < _SHIELD_SUPPRESSION_PASS_THRESHOLD_PCT:
                        _escalate(
                            "HOLD",
                            f"T-cell suppression cohort mean={sup_mean_v:.2f}% < 70.0% PASS bar",
                        )
            elif donor_sampling_design_valid and not suppression_inference_authorized_by_sampling_design:
                if sup_min_v is not None and sup_min_v < 0.0:
                    _escalate(
                        "HOLD",
                        f"T-cell suppression anti-enhancement safety guard triggered: min_donor_suppression={sup_min_v:.2f}% < 0.0% "
                        f"(no donor may exhibit T-cell enhancement)",
                    )
                _escalate(
                    "HOLD",
                    f"STRATIFIED_COHORT_REQUIRES_TARGET_WEIGHTED_SUPPRESSION_ESTIMATOR: "
                    f"sampling_design_class={sampling_design_class_val!r} is non-authorizing for unweighted "
                    f"Clopper-Pearson population-suppression PASS or unweighted population-rate suppression FAIL "
                    f"(only {sorted(_SHIELD_AUTHORIZING_DONOR_SAMPLING_DESIGNS)} authorizes unweighted "
                    f"population-proportion suppression inference; independent SPR FAIL and "
                    f"fixed-roster within-donor Bonferroni cytotoxicity FAIL remain active)",
                )

        # R09 B2: Fixed pre-registered roster Bonferroni denominator N_roster NEVER shrinks under exclusions,
        # and NEVER authorizes biological cytotoxicity FAIL unless authorization_grade_fwer_bound_valid is True!
        alpha_donor: float | None = (
            _SHIELD_CYTO_DONOR_FAMILY_ALPHA_ONE_SIDED / float(bonferroni_family_denominator_n)
            if bonferroni_family_denominator_n is not None
            else None
        )
        derived_summary["per_donor_bonferroni_alpha_one_sided"] = round(alpha_donor, 10) if alpha_donor is not None else None

        if len(valid_donor_cyto_rates) >= 1:
            unadjusted_donor_l95_diag = max(valid_donor_cp_l95s_unadj)
            worst_cp_u95 = max(valid_donor_cp_u95s)
            total_k = sum(valid_donor_k_list)
            total_neff = sum(valid_donor_neff_list)
            max_obs_rate = max(valid_donor_cyto_rates)

            worst_donor_bonf_lower: float | None = None
            if alpha_donor is not None:
                bonf_donor_lowers = [
                    _clopper_pearson_one_sided_95_pct(k_i, n_i, alpha=alpha_donor)[0]
                    for k_i, n_i in zip(valid_donor_k_list, valid_donor_neff_list)
                ]
                worst_donor_bonf_lower = max(bonf_donor_lowers)

            derived_summary["cytotoxicity_total_k_obs"] = total_k
            derived_summary["cytotoxicity_total_n_eff"] = total_neff
            derived_summary["cytotoxicity_min_n_eff_per_donor"] = min(valid_donor_neff_list)
            derived_summary["cytotoxicity_max_donor_observed_pct"] = round(max_obs_rate, 6)
            derived_summary["simultaneous_worst_donor_bonferroni_lower_pct"] = (
                round(worst_donor_bonf_lower, 6) if worst_donor_bonf_lower is not None else None
            )
            derived_summary["cytotoxicity_calibrated_simultaneous_lower_pct"] = (
                round(worst_donor_bonf_lower, 6) if worst_donor_bonf_lower is not None else None
            )
            derived_summary["unadjusted_donor_l95_diagnostic_only"] = round(unadjusted_donor_l95_diag, 6)

            cohort_t_lower_diag: float | None = None
            cyto_mean = sum(valid_donor_cyto_rates) / float(len(valid_donor_cyto_rates))
            eff_u95 = worst_cp_u95
            derived_summary["cytotoxicity_mean_observed_pct"] = round(cyto_mean, 6)

            if len(valid_donor_cyto_rates) >= _SHIELD_MIN_DONORS_N:
                # Non-authorizing descriptive cohort Student-t summary ONLY (NEVER drives biological FAIL).
                cyto_mean, cohort_t_lower_diag, cohort_u975 = _shield_student_t_ci_pct(
                    valid_donor_cyto_rates,
                    lo_bound=0.0,
                    hi_bound=100.0,
                )
                eff_u95 = max(cohort_u975, worst_cp_u95)
                derived_summary["cytotoxicity_mean_observed_pct"] = round(cyto_mean, 6)
                derived_summary["cohort_mean_cytotoxicity_t_lower_diagnostic_only_pct"] = round(cohort_t_lower_diag, 6)
                derived_summary["cytotoxicity_conservative_u95_pct"] = round(eff_u95, 6)
                derived_summary["cytotoxicity_full_roster_u95_pct"] = (
                    round(eff_u95, 6) if full_roster_cyto_measured_complete else None
                )

                if total_neff < _SHIELD_MIN_CYTO_N_EFF_PER_DONOR:
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Total cytotoxicity n_eff={total_neff} < {_SHIELD_MIN_CYTO_N_EFF_PER_DONOR}")

                # Two-sided validation of optional top-level reported cytotoxicity summary (<= 0.05 pp tolerance)
                top_obs = observation.get("cytotoxicity_observed_pct")
                top_l95 = observation.get("cytotoxicity_l95_pct")
                top_u95 = observation.get("cytotoxicity_u95_pct")
                if top_obs is not None or top_l95 is not None or top_u95 is not None:
                    t_o, t_o_err = _shield_check_num(top_obs, "cytotoxicity_observed_pct", lo=0.0, hi=100.0) if top_obs is not None else (None, None)
                    t_l, t_l_err = _shield_check_num(top_l95, "cytotoxicity_l95_pct", lo=0.0, hi=100.0) if top_l95 is not None else (None, None)
                    t_u, t_u_err = _shield_check_num(top_u95, "cytotoxicity_u95_pct", lo=0.0, hi=100.0) if top_u95 is not None else (None, None)
                    for err in (t_o_err, t_l_err, t_u_err):
                        if err is not None:
                            _escalate("HOLD_INVALID_OR_MISSING_INPUT", err)
                    if t_l is not None and t_u is not None and t_l > t_u:
                        _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Top-level inverted cytotoxicity CI L={t_l} > U95={t_u}")
                    if t_o is not None and t_l is not None and t_o == 0.0 and t_l > 0.0:
                        _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Top-level inconsistent cytotoxicity CI: observed=0.0% with L={t_l:.3f}%")
                    if t_o is not None and abs(t_o - cyto_mean) > _SHIELD_CYTO_CI_TOL_PP:
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"Top-level cytotoxicity observed_pct={t_o:.3f}% disagrees with recomputed cohort mean={cyto_mean:.3f}%",
                        )
                    if t_l is not None and total_k == 0 and t_l > 0.0:
                        _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Top-level cytotoxicity lower bound={t_l:.3f}% contradicts 0 observed events across donors")
                    if t_l is not None and worst_donor_bonf_lower is not None and abs(t_l - worst_donor_bonf_lower) > _SHIELD_CYTO_CI_TOL_PP:
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"Top-level cytotoxicity lower bound={t_l:.3f}% disagrees with fixed-roster Bonferroni lower bound={worst_donor_bonf_lower:.3f}% (two-sided tol {_SHIELD_CYTO_CI_TOL_PP:.2f} pp)",
                        )
                    if t_u is not None and abs(t_u - eff_u95) > _SHIELD_CYTO_CI_TOL_PP:
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"Top-level cytotoxicity U95={t_u:.3f}% disagrees with recomputed conservative U95={eff_u95:.3f}% (two-sided tol {_SHIELD_CYTO_CI_TOL_PP:.2f} pp)",
                        )

            if authorization_grade_fwer_bound_valid and worst_donor_bonf_lower is not None and worst_donor_bonf_lower > 5.0:
                donor_based_bio_fail = True
                diag_txt = f"{cohort_t_lower_diag:.3f}%" if cohort_t_lower_diag is not None else "n/a(<3 valid)"
                _escalate(
                    "FAIL",
                    f"Off-target APC cytotoxicity exact fixed-roster Bonferroni lower bound (H0_all: max_i p_i <= 5.0%, FWER<=0.05) "
                    f"exceeds 5.0% FAIL bar: simultaneous_worst_donor_bonf_lower={worst_donor_bonf_lower:.3f}% "
                    f"(N_roster={bonferroni_family_denominator_n}, N_valid={len(valid_donor_cyto_rates)}, "
                    f"alpha_donor={alpha_donor:.6f}; diagnostic_only_cohort_t_lower={diag_txt})",
                )
            elif authorization_grade_fwer_bound_valid and len(valid_donor_cyto_rates) >= _SHIELD_MIN_DONORS_N:
                if total_k > 0:
                    if eff_u95 > 5.0:
                        _escalate(
                            "HOLD_CYTOTOXICITY_U95_EXCEEDS_5PCT",
                            f"Positive observed off-target APC cytotoxicity (max_donor={max_obs_rate:.3f}%, mean={cyto_mean:.3f}%) "
                            f"with calibrated simultaneous lower={worst_donor_bonf_lower:.3f}% <= 5.0% AND conservative U95={eff_u95:.3f}% > 5.0%",
                        )
                    else:
                        _escalate(
                            "HOLD_OBSERVED_NONZERO_CYTOTOXICITY",
                            f"Positive observed off-target APC cytotoxicity (max_donor={max_obs_rate:.3f}%, mean={cyto_mean:.3f}%) "
                            f"while conservative U95={eff_u95:.3f}% <= 5.0%; cannot PASS",
                        )
                elif eff_u95 > 5.0:
                    _escalate(
                        "HOLD",
                        f"Underpowered 0% cytotoxicity: conservative U95={eff_u95:.3f}% > 5.0% PASS ceiling",
                    )
            if authorization_grade_fwer_bound_valid and not full_roster_cyto_measured_complete and len(valid_excluded_donor_ids) > 0:
                _escalate(
                    "HOLD",
                    f"INCOMPLETE_ROSTER_CYTOTOXICITY_EXCLUSION_PRECLUDES_FULL_ROSTER_PASS: "
                    f"{len(valid_excluded_donor_ids)} of {bonferroni_family_denominator_n} pre-registered rostered "
                    f"donors were excluded pre-assay ({sorted(valid_excluded_donor_ids)}); confirmatory full-roster "
                    f"cytotoxicity PASS and Track-B eligibility require 100% measured valid cytotoxicity evidence across all "
                    f"N_roster={bonferroni_family_denominator_n} rostered donors (conservative full-roster "
                    f"Bonferroni FAIL adjudication remains active at N_roster={bonferroni_family_denominator_n})",
                )

    # 8. Cross-block construct-bound linked expression_batch_ids replication checks
    #    (>= 3 distinct Comp1 batches linked across Comp1 assays; >= 3 distinct Comp2 batches linked across
    #     Comp2 assays; >= 3 distinct Comp1 and >= 3 distinct Comp2 batches linked across cocktail assays)
    derived_summary["distinct_referenced_expression_batches_count"] = len(referenced_batches)
    derived_summary["distinct_linked_comp1_assay_batches_count"] = len(comp1_assay_batches)
    derived_summary["distinct_linked_comp2_assay_batches_count"] = len(comp2_assay_batches)
    derived_summary["distinct_linked_cocktail_comp1_batches_count"] = len(cocktail_comp1_batches)
    derived_summary["distinct_linked_cocktail_comp2_batches_count"] = len(cocktail_comp2_batches)
    if len(referenced_batches) < _SHIELD_MIN_BATCHES_N:
        _escalate(
            "HOLD_INVALID_OR_MISSING_INPUT",
            f"Assay records reference only {len(referenced_batches)} distinct declared passing expression batches (< {_SHIELD_MIN_BATCHES_N} required)",
        )
    if len(comp1_assay_batches) < _SHIELD_MIN_BATCHES_N:
        _escalate(
            "HOLD_INVALID_OR_MISSING_INPUT",
            f"Comp1 assays link only {len(comp1_assay_batches)} distinct passing {_SHIELD_COMP1_LEAD} batches (< {_SHIELD_MIN_BATCHES_N} required)",
        )
    if len(comp2_assay_batches) < _SHIELD_MIN_BATCHES_N:
        _escalate(
            "HOLD_INVALID_OR_MISSING_INPUT",
            f"Comp2 assays link only {len(comp2_assay_batches)} distinct passing {_SHIELD_COMP2_LEAD} batches (< {_SHIELD_MIN_BATCHES_N} required)",
        )
    if len(cocktail_comp1_batches) < _SHIELD_MIN_BATCHES_N:
        _escalate(