OpenIslet

scripts/verify_iip0035_d1b_auto_and_portfolio.py

Deterministic Autologous D1b-Auto-C v2 Mode-B Registry, 45-Cell Stage-0 Contract,

script Release v0.8.0 gen-eb955f8266035aba 288 KB · 23 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_iip0035_d1b_auto_and_portfolio.py · SHA-256: fedb990aeaedd116…

        reasons.append("HOLD_D1B_AUTO_V2_INCOMPLETE_D101_D109_PANEL")

    dedup: List[str] = []
    for r in reasons:
        if r not in dedup:
            dedup.append(r)
    ordered = (
        [r for r in dedup if r.startswith("FAIL_HARD_STOP_")]
        + [r for r in dedup if r.startswith("FAIL_") and not r.startswith("FAIL_HARD_STOP_")]
        + [r for r in dedup if not r.startswith("FAIL_")]
    )
    reference_criteria_met = len(ordered) == 0

    g0v_cp = g0v.get("cp_result") if isinstance(g0v, dict) else None
    cal_summary: Dict[str, Any] = {
        "d1_04_ap1903": _cal_meta_from_cp(ap_cp, _ap_cal_kw),
        "d1_08_hlac0304": _cal_meta_from_cp(hlac03_cp, _hlac03_cal_kw),
        "d1_08_t1d_serum_c5b9": _cal_meta_from_cp(t1d_cp, _t1d_cal_kw),
        "l1b_gate0v_day28": _cal_meta_from_cp(g0v_cp),
    }
    sampling_prov_by_assay: Dict[str, Any] = {
        "d1_04_ap1903": _ap_cal_kw.get("sampling_independence_provenance"),
        "d1_08_hlac0304": _hlac03_cal_kw.get("sampling_independence_provenance"),
        "d1_08_t1d_serum_c5b9": _t1d_cal_kw.get("sampling_independence_provenance"),
        "l1b_gate0v_day28": g0v_cp.get("sampling_independence_provenance") if isinstance(g0v_cp, dict) else None,
    }
    panel_cov_regime = _aggregate_panel_coverage_regime(cal_summary)

    # Surface ALL assumption-only / default-calibration paths visibly at headline output
    assumption_only_flags: List[str] = _collect_assumption_only_flags(cal_summary)

    is_l1b_arm = bool(is_fullstack and not has_pvid_conflict and effective_l1_arm == "D1AUTO_V2_L1B_CD46WT_CD64T_GATE0V_COMPARATOR")
    if not is_l1b_arm:
        g0v_standalone_status = str(g0v.get("reason") or ("NOT_APPLICABLE_L1A_NO_CD46" if is_fullstack else "UNRESOLVED_PRODUCT_GATE0V_NOT_EVALUATED"))
        g0v_lims_verified = None if (eff_g0v_record is None and not g0v_conflict) else False
    else:
        if g0v.get("cleared") is True and (lot_invalid or lot_conflict):
            g0v_standalone_status = "INVALID_LOT_BINDING_PREVENTS_GATE0V_MATCH"
        elif g0v.get("cleared") is True:
            g0v_standalone_status = "STANDALONE_SUBCHECK_PASSED_LIVE_LIMS_LEDGER_UNVERIFIED"
        else:
            g0v_standalone_status = str(g0v.get("reason") or "HOLD_D1_L1B_GATE0V_UNVERIFIED")
        g0v_lims_verified = False

    is_canonical_eval = bool(
        require_canonical_sha
        and contract["staged_registry_verified"]
        and contract["canonical_sha_verified"]
        and contract["playbook_canonical_sha_verified"]
    )
    if reference_criteria_met:
        top_status = (
            "REFERENCE_CRITERIA_MET_EMPIRICAL_RELEASE_PENDING"
            if is_canonical_eval
            else "NON_CANONICAL_DIAGNOSTIC_CRITERIA_MET_UNPINNED"
        )
    else:
        top_status = ordered[0]

    return {
        "gate_id": "STAGE0_GATE_AUTOLOGOUS_D1B_AUTO_C_V2_D101_D109_IIP_0035",
        "decision_id": "DEC-114",
        "contribution_id": "IIP-0035",
        "controlling_contract_block_id": contract["gate_id"],
        "controlling_contract_schema_version": contract["schema_version"],
        "controlling_contract_sha256": contract["sha256"],
        "controlling_staged_registry_verified": contract["staged_registry_verified"],
        "controlling_contract_canonical_sha_verified": contract["canonical_sha_verified"],
        "controlling_contract_on_disk_verified": contract["on_disk_decision_tree_verified"],
        "controlling_contract_in_memory_matches_canonical_sha": contract["in_memory_block_matches_canonical_sha"],
        "controlling_playbook_sec4120_sha256": contract["playbook_sec4120_sha256"],
        "controlling_playbook_footer_verified": contract["playbook_footer_verified"],
        "controlling_playbook_canonical_sha_verified": contract["playbook_canonical_sha_verified"],
        "controlling_contract_cells_loaded": contract["cells_loaded"],
        "controlling_contract_external_refs": contract["external_refs"],
        "evaluation_mode": "CANONICAL_PINNED" if is_canonical_eval else "NON_CANONICAL_DIAGNOSTIC_UNPINNED",
        "evaluation_kind": "REFERENCE_CONTRACT_COMPUTATION_NOT_EMPIRICAL_RELEASE_DECISION",
        "reference_scenario_scope": (
            "SINGLE_LOCUS_L1_DONOR_REFERENCE_SCENARIO_KEY_NOT_ASSEMBLED_MULTILOCUS_MANIFEST"
            if is_fullstack
            else "UNRESOLVED_OR_NON_CANONICAL_PRODUCT_SCOPE"
        ),
        "assembled_multilocus_manifest_bound": False,
        "clinical_or_batch_release_authorized": False,
        "canonical_product_variant_id": pvid or None,
        "effective_locus1_donor_arm": effective_l1_arm,
        "evaluated_lot_id": lot_id_eff if _is_canonical_identifier(lot_id_eff) else None,
        "d1_01_raw_hla_c_input_status": d1_01_raw_hla_c_input_status,
        "d1_01_allele_derivation_mode": d1_01_allele_derivation_mode,
        "d1_02_offgrid_guard_evidence_mode": d1_02_offgrid_guard_evidence_mode,
        "d1_08_allele_branch": d1_08_allele_branch,
        "d1_08_non_c0304_screen_attestation_scope": (
            "EXTERNALLY_ADJUDICATED_CALLER_ATTESTATION_PENDING_NORMATIVE_PROTOCOL"
            if d1_08_allele_branch == "NON_HLAC0304_MATCHED_ALLELE_SCREEN_AND_C5B9"
            else None
        ),
        "d1_08_assay_coverage_limit_note": (
            "Evaluates specified CD8+ peptide and T1D serum C5b-9 complement readouts only; "
            "GY-023 cellular ADCC/NK-ADCC and longitudinal proteostasis remain open requirements."
        ),
        "global_gate0v_standalone_subcheck_status": g0v_standalone_status,
        "global_gate0v_lims_ledger_verified": g0v_lims_verified,
        "panel_coverage_scope": "PER_ASSAY_INDIVIDUAL_COVERAGE_NOT_SIMULTANEOUS_PANEL_FAMILYWISE_GUARANTEE",
        "coverage_regime": panel_cov_regime,
        "rare_event_calibration_summary": cal_summary,
        "sampling_design_provenance_by_assay": sampling_prov_by_assay,
        "assumption_only_path_flags": assumption_only_flags,
        "d1_04_ap1903_u95_str": ap_cp.get("u95_exact_sound_decimal_str") if ap_cp else None,
        "d1_08_hlac0304_u95_pct_str": hlac03_cp.get("u95_exact_sound_decimal_pct_str") if hlac03_cp else None,
        "d1_08_t1d_serum_c5b9_u95_pct_str": t1d_cp.get("u95_exact_sound_decimal_pct_str") if t1d_cp else None,
        "empirical_clearance_status": (
            "MATHEMATICAL_SUBGATE_REFERENCE_CHECK_ONLY_EMPIRICAL_STAGE0_PENDING"
            if (reference_criteria_met and is_canonical_eval)
            else (
                "NON_CANONICAL_DIAGNOSTIC_UNPINNED_NO_RELEASE"
                if reference_criteria_met
                else "HOLD_OR_FAIL_STAGE0_GATE"
            )
        ),
        "reference_criteria_met": reference_criteria_met if is_canonical_eval else False,
        "diagnostic_criteria_met": reference_criteria_met,
        "status": top_status,
        "reasons": ordered,
    }


def compute_rare_event_and_gate_matrix_audit(
    by_id: Dict[str, Dict[str, Any]],
    *,
    repo_root: Path = ROOT,
) -> Dict[str, Any]:
    """Deterministic audit of 80-digit sound Decimal Clopper-Pearson boundaries & standalone D1-01..D1-09 matrix."""
    # 1. Exact 80-digit sound Decimal Clopper-Pearson boundaries
    cp_cases = [
        (
            "d1_04_ap1903_k0_n299573225_HOLD_above_1e8",
            evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_04_AP1903", k=0, n_raw=299_573_225, max_u95_fraction=Decimal("1e-8")
            ),
            "HOLD_INCONCLUSIVE_INSUFFICIENT_INDEPENDENT_DENOMINATOR",
        ),
        (
            "d1_04_ap1903_k0_n299573226_PASS_at_1e8",
            evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_04_AP1903", k=0, n_raw=299_573_226, max_u95_fraction=Decimal("1e-8")
            ),
            "PASS",
        ),
        (
            "d1_04_ap1903_k1_FAIL_HARD_STOP",
            evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_04_AP1903", k=1, n_raw=300_000_000, max_u95_fraction=Decimal("1e-8")
            ),
            "FAIL_HARD_STOP",
        ),
        (
            "d1_04_ap1903_underallocated_alpha_eta_0p01_HOLD",
            evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_04_AP1903", k=0, n_raw=700_000_000, max_u95_fraction=Decimal("1e-8"),
                eta_recovery_l95=Decimal("0.50"), alpha_obs=Decimal("0.05"), alpha_eta_recovery=Decimal("0.01"),
            ),
            "HOLD_INSUFFICIENT_BONFERRONI_ALPHA_ALLOCATION",
        ),
        (
            "d1_04_ap1903_huge_n_1e308_nonzero_sci_PASS",
            evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_04_AP1903", k=0, n_raw=10**308, max_u95_fraction=Decimal("1e-8")
            ),
            "PASS",
        ),
        (
            "d1_08_k0_n58_HOLD_5p0340pct",
            evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_08_HLAC0304_AND_C5B9", k=0, n_raw=58, max_u95_fraction=Decimal("0.05")
            ),
            "HOLD_INCONCLUSIVE_INSUFFICIENT_INDEPENDENT_DENOMINATOR",
        ),
        (
            "d1_08_k0_n59_PASS_4p9508pct",
            evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_08_HLAC0304_AND_C5B9", k=0, n_raw=59, max_u95_fraction=Decimal("0.05")
            ),
            "PASS",
        ),
        (
            "d1_08_k1_n1000_FAIL_HARD_STOP",
            evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_08_HLAC0304_AND_C5B9", k=1, n_raw=1000, max_u95_fraction=Decimal("0.05")
            ),
            "FAIL_HARD_STOP",
        ),
    ]
    cp_results: List[Dict[str, Any]] = []
    for cname, res, exp_st in cp_cases:
        if res["status"] != exp_st:
            raise ValueError(f"Rare-event audit case {cname} returned {res['status']!r}, expected {exp_st!r}")
        cp_results.append({"case": cname, **res})
    huge_res = next(r for r in cp_results if r["case"] == "d1_04_ap1903_huge_n_1e308_nonzero_sci_PASS")
    if huge_res["u95_exact_sound_decimal_str"].startswith("0.0"):
        raise ValueError("Huge-n sound Decimal bound must not print as zero")

    # 2. Standalone D1-01..D1-09 reference matrix
    base_obs: Dict[str, Any] = {
        "d1_01_4digit_hla_abc_kir_genotyping_verified": True,
        "d1_auto_hlac_sp3c4c_genotype_class": "ELIGIBLE_VL9_COMPETENT_HLAC",
        "d1_02_n1_phased_wgs_offgrid_guard_011_cleared": True,
        "obs_d1_02_min_mismatches_to_patient_hlac_e": 3,
        "obs_d1_02_wt_hlac_allele_retention_pct": 99.5,
        "obs_d1_02_hlac_bystander_ct_pct": 0.1,
        "obs_d1_02_chr6p21_interlocus_del_pct": 0.0,
        "d1_auto_cipsc_source_arm": "ADSC_SUN_PROTECTED_CIPSC",
        "d1_03_head_to_head_adsc_vs_blood_comparator_attested": True,
        "d1_03_adsc_vs_blood_bcor_tp53_ppm1d_wgs_clean": True,
        "obs_d1_03_chrm_numt_amplicon_depth_x": 1500.0,
        "obs_d1_03_chrm_novel_nonsyn_het_pct": 0.2,
        "d1_04_chr13_clybl_tandem_404bp_independent_from_chr19_verified": True,
        "obs_d1_04_g0_beta_htmpk_leak_pct": 0.05,
        "obs_d1_04_hlac07_ryafsgvay_elispot_above_bg_pct": 0.05,
        "obs_d1_04_dec053_ap1903_icasp9_survivor_frac": 0.0,
        "obs_d1_04_ap1903_survivor_count_k": 0,
        "obs_d1_04_ap1903_effective_trials_n": 299_573_226,
        "d1_04_azt_kill_measured_after_forced_chr19_loss": True,
        "obs_d1_04_dec053_azt_dividing_log10_kill": 4.5,
        "obs_d1_05_l3_day28_retention_pct": 88.0,
        "obs_d1_05_l4_day28_retention_pct": 86.0,
        "obs_d1_05_l3_l4_day28_retention_pct": 86.0,
        "obs_d1_05_fsp1_ferroptosis_survival_pct": 87.0,
        "obs_d1_06_kir3dl1_missing_bw4_nk_lysis_pct": 4.2,
        "obs_d1_07_vtcn1_nrd1_sb7h4_shedding_pct": 5.0,
        "d1_08_patient_carries_hlac0304": True,
        "d1_08_hlac0304_positive_control_valid": True,
        "obs_d1_08_hlac0304_tcell_lysis_u95_pct": 4.96,
        "obs_d1_08_hlac0304_lysed_events_k": 0,
        "obs_d1_08_hlac0304_scored_targets_n": 59,
        "obs_d1_08_t1d_serum_c5b9_u95_pct": 4.96,
        "obs_d1_08_t1d_serum_c5b9_pos_events_k": 0,
        "obs_d1_08_t1d_serum_c5b9_scored_cells_n": 59,
        "obs_d1_09_autologous_turnaround_weeks": 36.0,
    }
    valid_g0v = {
        "mcb_lot_id": "MCB_AUTO_LOT_01",
        "cvb_day14_log10_reduction": 3.8,
        "cvb_day14_log10_reduction_l95": 3.2,
        "cvb4_72h_early_titer_log10_pfu_u95": 1.5,
        "day28_vp1_ie1_positive_k": 0,
        "day28_vp1_ie1_cells_scored_n": 6000,
        "gate0v_verdict": "PASS",
    }
    p_l1a = by_id["ISLET1_D1_AUTO_V2_L1A_CIITA_CD64T_IL1RN"]
    p_l1b = by_id["ISLET1_D1_AUTO_V2_L1B_CIITA_CD64T_CD46WT_COMPARATOR"]