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_D1_05_REPORTED_MIN_L3_L4_COMPARISON_INCONCLUSIVE")
    if day28_ret is not None and day28_ret < thr.get("min_l3_l4_ret_min", Decimal("80.0")):
        reasons.append("FAIL_D1_05_L3_L4_STAGE7_DAY28_RETENTION_BELOW_80PCT")
    if fsp1_surv is not None and fsp1_surv < thr.get("fsp1_surv_min", Decimal("80.0")):
        reasons.append("FAIL_D1_05_FSP1_FERROPTOSIS_SURVIVAL_BELOW_80PCT")

    # D1-06 & D1-07
    kir_nk = _dec("obs_d1_06_kir3dl1_missing_bw4_nk_lysis_pct")
    vtcn1_shed = _dec("obs_d1_07_vtcn1_nrd1_sb7h4_shedding_pct")
    if kir_nk is None:
        reasons.append(cell_mdd.get("D1-06", "HOLD_D1_06_MISSING_KIR3DL1_NK_ASSAY"))
    elif kir_nk > thr.get("kir_nk_max", Decimal("8.0")):
        reasons.append("FAIL_D1_06_KIR3DL1_MISSING_BW4_NK_LYSIS_EXCEEDS_8PCT")
    if vtcn1_shed is None:
        reasons.append(cell_mdd.get("D1-07", "HOLD_D1_07_MISSING_VTCN1_SHEDDING_ASSAY"))
    elif vtcn1_shed > thr.get("vtcn1_shed_max", Decimal("10.0")):
        reasons.append("FAIL_D1_07_VTCN1_NRD1_SB7H4_SHEDDING_EXCEEDS_10PCT_MUTAGENESIS_REQUIRED")

    # D1-08 (Two-branch allele-conditional residual CD8+ T-cell screen & intact T1D serum C5b-9)
    hlac03_u95_pct_cap = thr.get("hlac03_u95_pct_max", Decimal("5.0"))
    t1d_c5b9_u95_pct_cap = thr.get("t1d_c5b9_u95_pct_max", Decimal("5.0"))

    carries_c0304_present = "d1_08_patient_carries_hlac0304" in observations
    carries_c0304 = observations.get("d1_08_patient_carries_hlac0304")
    carries_c0304_raw_explicit_unavail_or_invalid = False
    if carries_c0304_present and carries_c0304 is None:
        carries_c0304_raw_explicit_unavail_or_invalid = True
    elif isinstance(carries_c0304, str) and carries_c0304 == "UNAVAILABLE":
        explicitly_unavailable.append("d1_08_patient_carries_hlac0304")
        carries_c0304 = None
        carries_c0304_raw_explicit_unavail_or_invalid = True
    elif carries_c0304 is not None and not isinstance(carries_c0304, bool):
        invalid_or_oob_keys.append("d1_08_patient_carries_hlac0304")
        carries_c0304 = None
        carries_c0304_raw_explicit_unavail_or_invalid = True
    if derived_carries_hlac0304 is not None:
        if isinstance(carries_c0304, bool) and carries_c0304 != derived_carries_hlac0304:
            reasons.append("FAIL_D1_08_HLAC0304_CARRIER_STATUS_MISMATCH_WITH_RAW_HLA_C_GENOTYPE")
            if derived_carries_hlac0304 is True:
                carries_c0304 = True
        elif carries_c0304 is None and not carries_c0304_raw_explicit_unavail_or_invalid:
            carries_c0304 = derived_carries_hlac0304
    elif unlisted_hlac_blocks_d1_08_negative and carries_c0304 is None and not carries_c0304_raw_explicit_unavail_or_invalid:
        reasons.append("HOLD_D1_08_UNLISTED_RAW_HLAC_ALLELE_BLOCKS_NEGATIVE_DERIVATION")

    hlac03_t = _dec("obs_d1_08_hlac0304_tcell_lysis_u95_pct")
    hlac03_k = _strict_int("obs_d1_08_hlac0304_lysed_events_k")
    hlac03_n = _strict_int("obs_d1_08_hlac0304_scored_targets_n")
    t1d_c5b9 = _dec("obs_d1_08_t1d_serum_c5b9_u95_pct")
    t1d_k = _strict_int("obs_d1_08_t1d_serum_c5b9_pos_events_k")
    t1d_n = _strict_int("obs_d1_08_t1d_serum_c5b9_scored_cells_n")

    _hlac03_cal_kw = _cal_kwargs("obs_d1_08_hlac0304")
    _t1d_cal_kw = _cal_kwargs("obs_d1_08_t1d_serum_c5b9")

    hlac03_cp: Dict[str, Any] | None = None
    d1_08_branch_ok = False
    c0304_pc: Any = None
    ma_pc: Any = None
    ma_ok: Any = None
    if carries_c0304 is True:
        d1_08_allele_branch = "HLAC0304_CARRIER_INSULIN_ACHAIN_AND_C5B9"
        c0304_pc = observations.get("d1_08_hlac0304_positive_control_valid")
        if isinstance(c0304_pc, str) and c0304_pc == "UNAVAILABLE":
            explicitly_unavailable.append("d1_08_hlac0304_positive_control_valid")
            c0304_pc = None
        elif c0304_pc is not None and not isinstance(c0304_pc, bool):
            invalid_or_oob_keys.append("d1_08_hlac0304_positive_control_valid")
            c0304_pc = None
        elif c0304_pc is False:
            reasons.append("FAIL_D1_08_HLAC0304_POSITIVE_CONTROL_INVALID")
        if c0304_pc is None:
            reasons.append("HOLD_D1_08_HLAC0304_POSITIVE_CONTROL_VALIDITY_REQUIRED")
        else:
            d1_08_branch_ok = bool(c0304_pc is True)

        if hlac03_k is None or hlac03_n is None or hlac03_n < 1 or hlac03_k > hlac03_n:
            if hlac03_k is not None and hlac03_k >= 1:
                reasons.append("FAIL_HARD_STOP_D1_08_HLAC0304_LYSIS_EVENT_DETECTED")
            else:
                reasons.append("HOLD_D1_08_HLAC0304_GY018_INTEGER_KN_REQUIRED")
                if hlac03_t is None:
                    reasons.append("HOLD_D1_08_HLAC0304_REPORTED_U95_REQUIRED")
        else:
            hlac03_cp = evaluate_sound_decimal_clopper_pearson_zero_event(
                gate_id="D1_08_HLAC0304_TCELL_LYSIS",
                k=hlac03_k,
                n_raw=hlac03_n,
                max_u95_fraction=_exact_pct_to_frac(hlac03_u95_pct_cap),
                reported_u95_pct=hlac03_t,
                **_hlac03_cal_kw,
            )
            if hlac03_cp["status"] == "FAIL_HARD_STOP":
                reasons.append("FAIL_HARD_STOP_D1_08_HLAC0304_LYSIS_EVENT_DETECTED")
                if hlac03_t is not None and hlac03_t <= Decimal(0):
                    reasons.append("FAIL_D1_08_INCONSISTENT_ZERO_UPPER_BOUND_WITH_EVENTS")
            elif hlac03_cp["status"] != "PASS":
                reasons.append(
                    "HOLD_D1_08_HLAC0304_INSUFFICIENT_INDEPENDENT_DENOMINATOR"
                    if hlac03_cp["status"] == "HOLD_INCONCLUSIVE_INSUFFICIENT_INDEPENDENT_DENOMINATOR"
                    else f"HOLD_D1_08_HLAC0304_{hlac03_cp['status']}"
                )
                if (
                    hlac03_t is not None
                    and hlac03_k == 0
                    and hlac03_t <= hlac03_u95_pct_cap
                    and hlac03_cp.get("reported_u95_meets_exact_bound") is False
                ):
                    reasons.append("FAIL_D1_08_REPORTED_U95_BELOW_EXACT_CLOPPER_PEARSON_BOUND")
            else:
                if hlac03_t is None:
                    reasons.append("HOLD_D1_08_HLAC0304_REPORTED_U95_REQUIRED")
                elif hlac03_t > hlac03_u95_pct_cap:
                    reasons.append("HOLD_D1_08_HLAC0304_REPORTED_U95_ABOVE_GATE_LIMIT")
                elif hlac03_t <= Decimal(0) or hlac03_cp.get("reported_u95_meets_exact_bound") is False:
                    reasons.append("FAIL_D1_08_REPORTED_U95_BELOW_EXACT_CLOPPER_PEARSON_BOUND")
                elif hlac03_cp.get("reported_u95_meets_exact_bound") is None:
                    reasons.append("HOLD_D1_08_REPORTED_U95_EXACT_FLOOR_INCONCLUSIVE")
    elif carries_c0304 is False:
        d1_08_allele_branch = "NON_HLAC0304_MATCHED_ALLELE_SCREEN_AND_C5B9"
        ma_pc = observations.get("d1_08_matched_allele_positive_control_valid")
        ma_ok = observations.get("d1_08_non_c0304_matched_allele_screen_passed")
        if isinstance(ma_pc, str) and ma_pc == "UNAVAILABLE":
            explicitly_unavailable.append("d1_08_matched_allele_positive_control_valid")
            ma_pc = None
        elif ma_pc is not None and not isinstance(ma_pc, bool):
            invalid_or_oob_keys.append("d1_08_matched_allele_positive_control_valid")
            ma_pc = None
        elif ma_pc is False:
            reasons.append("FAIL_D1_08_MATCHED_ALLELE_POSITIVE_CONTROL_INVALID")
        if ma_pc is None:
            reasons.append("HOLD_D1_08_MATCHED_ALLELE_POSITIVE_CONTROL_VALIDITY_REQUIRED")

        if isinstance(ma_ok, str) and ma_ok == "UNAVAILABLE":
            explicitly_unavailable.append("d1_08_non_c0304_matched_allele_screen_passed")
            ma_ok = None
        elif ma_ok is not None and not isinstance(ma_ok, bool):
            invalid_or_oob_keys.append("d1_08_non_c0304_matched_allele_screen_passed")
            ma_ok = None
        elif ma_ok is False:
            reasons.append("FAIL_D1_08_MATCHED_ALLELE_CD8_SCREEN_FAILED")
        if ma_ok is None:
            reasons.append("HOLD_D1_08_MATCHED_ALLELE_CD8_SCREEN_REQUIRED")

        d1_08_branch_ok = bool(ma_pc is True and ma_ok is True)
        if hlac03_k is not None and hlac03_k >= 1:
            reasons.append("FAIL_HARD_STOP_D1_08_HLAC0304_LYSIS_EVENT_DETECTED")
        if _hlac03_cal_kw["sampling_independence_conflict"]:
            reasons.append("HOLD_D1_08_HLAC0304_HOLD_CONFLICTING_SAMPLING_INDEPENDENCE_DECLARATIONS")
        elif _hlac03_cal_kw["sampling_malformed"]:
            reasons.append("HOLD_D1_08_HLAC0304_HOLD_MALFORMED_SAMPLING_INDEPENDENCE_DECLARATION")
        elif _hlac03_cal_kw["clustered_sampling"]:
            reasons.append("HOLD_D1_08_HLAC0304_HOLD_NON_INDEPENDENT_OR_CLUSTERED_SAMPLING_REJECTED")
    else:
        d1_08_allele_branch = "UNSPECIFIED_HLAC0304_CARRIER_STATUS_HOLD"
        reasons.append("HOLD_D1_08_HLAC0304_CARRIER_STATUS_REQUIRED")
        if hlac03_k is not None and hlac03_k >= 1:
            reasons.append("FAIL_HARD_STOP_D1_08_HLAC0304_LYSIS_EVENT_DETECTED")
        if _hlac03_cal_kw["sampling_independence_conflict"]:
            reasons.append("HOLD_D1_08_HLAC0304_HOLD_CONFLICTING_SAMPLING_INDEPENDENCE_DECLARATIONS")
        elif _hlac03_cal_kw["sampling_malformed"]:
            reasons.append("HOLD_D1_08_HLAC0304_HOLD_MALFORMED_SAMPLING_INDEPENDENCE_DECLARATION")
        elif _hlac03_cal_kw["clustered_sampling"]:
            reasons.append("HOLD_D1_08_HLAC0304_HOLD_NON_INDEPENDENT_OR_CLUSTERED_SAMPLING_REJECTED")

    t1d_cp: Dict[str, Any] | None = None
    if t1d_k is None or t1d_n is None or t1d_n < 1 or t1d_k > t1d_n:
        if t1d_k is not None and t1d_k >= 1:
            reasons.append("FAIL_HARD_STOP_D1_08_T1D_SERUM_C5B9_EVENT_DETECTED")
        else:
            reasons.append("HOLD_D1_08_T1D_SERUM_C5B9_GY018_INTEGER_KN_REQUIRED")
            if t1d_c5b9 is None:
                reasons.append("HOLD_D1_08_T1D_SERUM_C5B9_REPORTED_U95_REQUIRED")
    else:
        t1d_cp = evaluate_sound_decimal_clopper_pearson_zero_event(
            gate_id="D1_08_T1D_SERUM_C5B9",
            k=t1d_k,
            n_raw=t1d_n,
            max_u95_fraction=_exact_pct_to_frac(t1d_c5b9_u95_pct_cap),
            reported_u95_pct=t1d_c5b9,
            **_t1d_cal_kw,
        )
        if t1d_cp["status"] == "FAIL_HARD_STOP":
            reasons.append("FAIL_HARD_STOP_D1_08_T1D_SERUM_C5B9_EVENT_DETECTED")
            if t1d_c5b9 is not None and t1d_c5b9 <= Decimal(0):
                reasons.append("FAIL_D1_08_INCONSISTENT_ZERO_UPPER_BOUND_WITH_EVENTS")
        elif t1d_cp["status"] != "PASS":
            reasons.append(
                "HOLD_D1_08_T1D_SERUM_C5B9_INSUFFICIENT_INDEPENDENT_DENOMINATOR"
                if t1d_cp["status"] == "HOLD_INCONCLUSIVE_INSUFFICIENT_INDEPENDENT_DENOMINATOR"
                else f"HOLD_D1_08_T1D_SERUM_C5B9_{t1d_cp['status']}"
            )
            if (
                t1d_c5b9 is not None
                and t1d_k == 0
                and t1d_c5b9 <= t1d_c5b9_u95_pct_cap
                and t1d_cp.get("reported_u95_meets_exact_bound") is False
            ):
                reasons.append("FAIL_D1_08_REPORTED_U95_BELOW_EXACT_CLOPPER_PEARSON_BOUND")
        else:
            if t1d_c5b9 is None:
                reasons.append("HOLD_D1_08_T1D_SERUM_C5B9_REPORTED_U95_REQUIRED")
            elif t1d_c5b9 > t1d_c5b9_u95_pct_cap:
                reasons.append("HOLD_D1_08_T1D_SERUM_C5B9_REPORTED_U95_ABOVE_GATE_LIMIT")
            elif t1d_c5b9 <= Decimal(0) or t1d_cp.get("reported_u95_meets_exact_bound") is False:
                reasons.append("FAIL_D1_08_REPORTED_U95_BELOW_EXACT_CLOPPER_PEARSON_BOUND")
            elif t1d_cp.get("reported_u95_meets_exact_bound") is None:
                reasons.append("HOLD_D1_08_REPORTED_U95_EXACT_FLOOR_INCONCLUSIVE")
    if (
        carries_c0304 is None
        or (carries_c0304 is True and (c0304_pc is None or hlac03_k is None or hlac03_n is None or hlac03_t is None))
        or (carries_c0304 is False and (ma_pc is None or ma_ok is None))
        or t1d_k is None
        or t1d_n is None
        or t1d_c5b9 is None
    ):
        reasons.append(cell_mdd.get("D1-08", "HOLD_D1_08_MISSING_HLAC0304_OR_C5B9_GY018_INTEGER_KN"))

    # D1-09
    turnaround = _dec("obs_d1_09_autologous_turnaround_weeks")
    if turnaround is None:
        reasons.append(cell_mdd.get("D1-09", "HOLD_D1_09_MISSING_TURNAROUND_OR_GATE0V_PROVENANCE"))
        if effective_l1_arm == "D1AUTO_V2_L1B_CD46WT_CD64T_GATE0V_COMPARATOR":
            reasons.append("HOLD_D1_09_MISSING_TURNAROUND_WEEKS")
    elif turnaround > thr.get("turnaround_max", Decimal("41.0")):
        reasons.append("FAIL_D1_09_AUTOLOGOUS_SEQUENTIAL_TURNAROUND_EXCEEDS_41WKS")

    if invalid_or_oob_keys:
        reasons.append("FAIL_D1_INVALID_OR_OUT_OF_RANGE_OBSERVATION")
    if explicitly_unavailable:
        reasons.append("HOLD_D1_OBSERVATION_EXPLICITLY_UNAVAILABLE")

    required_nums = (
        wt_hlac, bystander, chr6_del, chrm_depth, chrm_het,
        htmpk_leak, elispot, azt_kill,
        l3_ret, l4_ret, fsp1_surv, kir_nk, vtcn1_shed, turnaround,
    )
    if (
        any(v is None for v in required_nums)
        or hlac_cls is None
        or cipsc_src is None
        or effective_l1_arm is None
        or geno_ok is not True
        or not wgs_ok_effective
        or h2h_ok is not True
        or bcor_ok is not True
        or chr13_ok is not True
        or azt_chr19_loss_ok is not True
        or not d1_08_branch_ok
    ):