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…

                                    row_had_invalid_veh_fab = True
                        if row_had_invalid_veh_fab:
                            withheld_invalid_vehicle_fab_rows_count += 1
                    else:
                        withheld_invalid_fab_rows_count += 1
                else:
                    withheld_invalid_fab_rows_count += 1

        # Adjudicate EVERY distinct endpoint across clinical bracket {1, 5, 15} ug/mL and all required strata
        # R07 B5 + R11 B3: ONLY rows with valid matched normal rabbit IgG controls, passing lot/run positive controls,
        # and validated 0.0 ug/mL vehicle baselines enter biological adjudication; invalid-control/invalid-vehicle rows
        # emit HOLD_INVALID_OR_INCOMPLETE_ASSAY and NEVER trigger permanent biological FAIL!
        cd47_res: dict[str, Any] = {}
        hla_e_res: dict[str, Any] = {}
        disp_res: dict[str, Any] = {}
        c3d_res: dict[str, Any] = {}
        c5b9_res: dict[str, Any] = {}
        lysis_res: dict[str, Any] = {}
        fab2_c3d_res: dict[str, Any] = {}
        fab2_c5b9_res: dict[str, Any] = {}
        fab2_lysis_res: dict[str, Any] = {}
        sc_viab_res: dict[str, Any] = {}
        iec_viab_res: dict[str, Any] = {}
        gsis_res: dict[str, Any] = {}

        for dose in _XREACT_CLINICAL_BRACKET_DOSES_UG_ML:
            for strat in _XREACT_REQUIRED_STRATA:
                cell = by_sd.get((strat, dose), {})
                valid_cell_recs = [r for r in cell.values() if r.get("_control_valid_for_bio") is True]
                valid_fab_recs = [r for r in cell.values() if r.get("_fab_control_valid_for_bio") is True]
                ckey = f"{strat}@{dose:g}"

                cd47_vals = [
                    r["cd47_sirpa_retention_pct"]
                    for r in valid_cell_recs
                    if "cd47_sirpa_retention_pct" in r and _veh_ok(strat, r["donor_id"], "cd47_sirpa_retention_pct")
                ]
                if len(cd47_vals) >= 6:
                    st, info = _eval_cd47(cd47_vals, f"{lot_id}.cd47[{ckey}]")
                    cd47_res[ckey] = info
                    if st != "PASS":
                        _lot_escalate(st, f"CD47 retention [{ckey}] verdict={st} (L95={info['l95_t']}, min={info['min_donor']})")

                # R07 B6 + R16 B1: Direct HLA-E binding retention (absolute >= 80.0% PASS / < 70.0% FAIL AND uncapped paired 100 * ATG / IgG >= 85.0% PASS / < 70.0% FAIL)
                hla_e_vals = [
                    r["hla_e_binding_retention_pct"]
                    for r in valid_cell_recs
                    if "hla_e_binding_retention_pct" in r and _veh_ok(strat, r["donor_id"], "hla_e_binding_retention_pct")
                ]
                hla_e_paired = [
                    100.0 * r["hla_e_binding_retention_pct"] / r["matched_rabbit_igg"]["hla_e_binding_retention_pct"]
                    for r in valid_cell_recs
                    if "hla_e_binding_retention_pct" in r
                    and _veh_ok(strat, r["donor_id"], "hla_e_binding_retention_pct")
                    and isinstance(r.get("matched_rabbit_igg"), dict)
                    and "hla_e_binding_retention_pct" in r["matched_rabbit_igg"]
                    and r["matched_rabbit_igg"]["hla_e_binding_retention_pct"] >= 80.0
                ]
                if len(hla_e_vals) >= 6:
                    st_ha, info_ha = _eval_hla_e_abs(hla_e_vals, f"{lot_id}.hla_e_abs[{ckey}]")
                    hla_e_entry: dict[str, Any] = {"absolute_retention_pct": info_ha}
                    if st_ha != "PASS":
                        _lot_escalate(st_ha, f"HLA-E absolute retention [{ckey}] verdict={st_ha} (L95={info_ha['l95_t']}, min={info_ha['min_donor']})")
                    if len(hla_e_paired) >= 6:
                        st_hp, info_hp = _eval_hla_e_paired(hla_e_paired, f"{lot_id}.hla_e_paired[{ckey}]")
                        hla_e_entry["paired_atg_over_igg_pct"] = info_hp
                        if st_hp != "PASS":
                            _lot_escalate(
                                st_hp,
                                f"HLA-E paired ATG/IgG retention [{ckey}] verdict={st_hp} "
                                f"(L95={info_hp['l95_t']:.4f}% vs 85.0% paired lower-bound criterion, min={info_hp['min_donor']:.4f}%)",
                            )
                    hla_e_res[ckey] = hla_e_entry

                for fkey, bucket, ep_lbl in (
                    ("c3d_pct", c3d_res, "C3d"),
                    ("c5b9_pct", c5b9_res, "C5b-9"),
                    ("lysis_pct", lysis_res, "Direct lysis"),
                ):
                    vals = [
                        r[fkey]
                        for r in valid_cell_recs
                        if fkey in r
                        and fkey not in r.get("_conflicted_linked_endpoints", ())
                        and _veh_ok(strat, r["donor_id"], fkey)
                    ]
                    if len(vals) >= 6:
                        st, info = _eval_upper_5_8(vals, f"{lot_id}.{fkey}[{ckey}]")
                        bucket[ckey] = info
                        if st != "PASS":
                            _lot_escalate(st, f"{ep_lbl} [{ckey}] verdict={st} (L95={info['l95_t']}, U95={info['u95_t']}, max={info['max_donor']})")

                # B2 + R07 B5 + R11 B2/B3: Adjudicate lot-/donor-/run-linked direct F(ab')2 deconvolution measurements biologically
                # strictly on control-valid, vehicle-valid rows!
                for fab_k, fbucket, fep_lbl in (
                    ("fab2_c3d_pct", fab2_c3d_res, "Direct F(ab')2 C3d"),
                    ("fab2_c5b9_pct", fab2_c5b9_res, "Direct F(ab')2 C5b-9"),
                    ("fab2_lysis_pct", fab2_lysis_res, "Direct F(ab')2 lysis"),
                ):
                    fvals = [
                        r["fab2_deconvolution"][fab_k]
                        for r in valid_fab_recs
                        if isinstance(r.get("fab2_deconvolution"), dict)
                        and fab_k in r["fab2_deconvolution"]
                        and _veh_ok(strat, r["donor_id"], fab_k)
                    ]
                    if len(fvals) >= 6:
                        st, info = _eval_upper_5_8(fvals, f"{lot_id}.{fab_k}[{ckey}]")
                        fbucket[ckey] = info
                        if st != "PASS":
                            _lot_escalate(st, f"{fep_lbl} [{ckey}] verdict={st} (L95={info['l95_t']}, U95={info['u95_t']}, max={info['max_donor']})")

                if strat in _XREACT_CD64T_POS_STRATA:
                    d_vals = [
                        r["cd64t_displacement_pct"]
                        for r in valid_cell_recs
                        if "cd64t_displacement_pct" in r and _veh_ok(strat, r["donor_id"], "cd64t_displacement_pct")
                    ]
                    if len(d_vals) >= 6:
                        st, info = _eval_disp(d_vals, f"{lot_id}.cd64t_disp[{ckey}]")
                        disp_res[ckey] = info
                        if st != "PASS":
                            _lot_escalate(st, f"CD64t displacement [{ckey}] verdict={st} (L95={info['l95_t']}, max={info['max_donor']})")

                if strat in _XREACT_SC_ISLET_STRATA:
                    sv_vals = [
                        r["sc_islet_viability_pct"]
                        for r in valid_cell_recs
                        if "sc_islet_viability_pct" in r and _veh_ok(strat, r["donor_id"], "sc_islet_viability_pct")
                    ]
                    if len(sv_vals) >= 6:
                        st, info = _eval_viab(sv_vals, f"{lot_id}.sc_islet_viab[{ckey}]")
                        sc_viab_res[ckey] = info
                        if st != "PASS":
                            _lot_escalate(st, f"SC-islet viability [{ckey}] verdict={st} (L95={info['l95_t']}, U95={info['u95_t']})")
                    gs_vals = [
                        r["gsis_si"]
                        for r in valid_cell_recs
                        if "gsis_si" in r and _veh_ok(strat, r["donor_id"], "gsis_si")
                    ]
                    if len(gs_vals) >= 6:
                        st, info = _eval_gsis(gs_vals, f"{lot_id}.gsis_si[{ckey}]")
                        gsis_res[ckey] = info
                        if st != "PASS":
                            _lot_escalate(st, f"Dynamic GSIS SI [{ckey}] verdict={st} (min_SI={info['min_gsis_si']})")
                    gs_paired_ratios = [
                        r["gsis_si"] / r["matched_rabbit_igg"]["gsis_si"]
                        for r in valid_cell_recs
                        if "gsis_si" in r
                        and _veh_ok(strat, r["donor_id"], "gsis_si")
                        and isinstance(r.get("matched_rabbit_igg"), dict)
                        and "gsis_si" in r["matched_rabbit_igg"]
                        and r["matched_rabbit_igg"]["gsis_si"] >= 2.5
                    ]
                    if len(gs_paired_ratios) >= 6:
                        st_pr, info_pr = _eval_gsis_paired_ratio(gs_paired_ratios, f"{lot_id}.gsis_ratio_vs_igg[{ckey}]")
                        gsis_res[f"{ckey}__paired_ratio_vs_igg"] = info_pr
                        if st_pr != "PASS":
                            _lot_escalate(
                                st_pr,
                                f"Dynamic GSIS paired ATG/IgG ratio [{ckey}] verdict={st_pr} "
                                f"(L95={info_pr['l95_t_ratio']}, min={info_pr['min_donor_ratio']})",
                            )

                if strat in _XREACT_IEC_STRATA:
                    iv_vals = [
                        r["iec_viability_pct"]
                        for r in valid_cell_recs
                        if "iec_viability_pct" in r and _veh_ok(strat, r["donor_id"], "iec_viability_pct")
                    ]
                    if len(iv_vals) >= 6:
                        st, info = _eval_viab(iv_vals, f"{lot_id}.iec_viab[{ckey}]")
                        iec_viab_res[ckey] = info
                        if st != "PASS":
                            _lot_escalate(st, f"iEC viability [{ckey}] verdict={st} (L95={info['l95_t']}, U95={info['u95_t']})")

        # B1(b) + R07 B5/B6 + R11 B3: Exact trapezoidal per-donor dose-response AUC (0..50 & 1..15 ug/mL) vs matched normal rabbit IgG,
        # strictly filtering to control-valid AND vehicle-valid donor dose curves BEFORE biological adjudication!
        auc_res: dict[str, Any] = {}
        if len(common_donors) >= 6:
            ordered_donors = sorted(common_donors)
            for auc_tag, dose_grid in (
                ("auc_0_50", _XREACT_REQUIRED_DOSES_UG_ML),
                ("auc_1_15", _XREACT_CLINICAL_BRACKET_DOSES_UG_ML),
            ):
                for strat in _XREACT_REQUIRED_STRATA:
                    valid_grid_donors = [
                        did
                        for did in ordered_donors
                        if all(by_sd[(strat, d)][did].get("_control_valid_for_bio") is True for d in dose_grid)
                    ]
                    valid_fab_grid_donors = [
                        did
                        for did in ordered_donors
                        if all(by_sd[(strat, d)][did].get("_fab_control_valid_for_bio") is True for d in dose_grid)
                    ]

                    # 1. CD47-SIRPa retention per-donor AUC vs matched rabbit IgG
                    cd47_auc_ratios: list[float] = []
                    for did in valid_grid_donors:
                        if not _veh_ok(strat, did, "cd47_sirpa_retention_pct"):
                            continue
                        recs = [by_sd[(strat, d)][did] for d in dose_grid]
                        if all("cd47_sirpa_retention_pct" in r and isinstance(r.get("matched_rabbit_igg"), dict) and "cd47_sirpa_retention_pct" in r["matched_rabbit_igg"] for r in recs):
                            a_atg = _xreact_trapz_mean(dose_grid, [r["cd47_sirpa_retention_pct"] for r in recs])
                            a_igg = _xreact_trapz_mean(dose_grid, [r["matched_rabbit_igg"]["cd47_sirpa_retention_pct"] for r in recs])
                            if a_igg < 80.0:
                                _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Invalid matched IgG CD47 AUC={a_igg:.2f}% (< 80.0% baseline) for {did}/{strat}/{auc_tag}")
                            else:
                                cd47_auc_ratios.append(100.0 * a_atg / a_igg)
                    if len(cd47_auc_ratios) >= 6:
                        st, info = _eval_cd47(cd47_auc_ratios, f"{lot_id}.cd47_{auc_tag}[{strat}]")
                        auc_res[f"cd47_{auc_tag}@{strat}"] = info
                        if st != "PASS":
                            _lot_escalate(
                                st,
                                f"Per-donor CD47 {auc_tag} vs matched IgG [{strat}] verdict={st} "
                                f"(L95={info['l95_t']:.4f}% vs 85.0% paired lower-bound criterion, min={info['min_donor']:.4f}%)",
                            )

                    # 1b. R07 B6 + R16 B1: Direct HLA-E binding retention per-donor AUC (absolute + uncapped paired 100 * AUC_ATG / AUC_IgG)
                    hla_e_abs_aucs: list[float] = []
                    hla_e_paired_aucs: list[float] = []
                    for did in valid_grid_donors:
                        if not _veh_ok(strat, did, "hla_e_binding_retention_pct"):
                            continue
                        recs = [by_sd[(strat, d)][did] for d in dose_grid]
                        if all("hla_e_binding_retention_pct" in r and isinstance(r.get("matched_rabbit_igg"), dict) and "hla_e_binding_retention_pct" in r["matched_rabbit_igg"] for r in recs):
                            a_atg = _xreact_trapz_mean(dose_grid, [r["hla_e_binding_retention_pct"] for r in recs])
                            a_igg = _xreact_trapz_mean(dose_grid, [r["matched_rabbit_igg"]["hla_e_binding_retention_pct"] for r in recs])
                            if a_igg < 80.0:
                                _lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Invalid matched IgG HLA-E AUC={a_igg:.2f}% (< 80.0% baseline) for {did}/{strat}/{auc_tag}")
                            else:
                                hla_e_abs_aucs.append(a_atg)
                                hla_e_paired_aucs.append(100.0 * a_atg / a_igg)
                    if len(hla_e_abs_aucs) >= 6:
                        st_ha, info_ha = _eval_hla_e_abs(hla_e_abs_aucs, f"{lot_id}.hla_e_{auc_tag}_abs[{strat}]")
                        st_hp, info_hp = _eval_hla_e_paired(hla_e_paired_aucs, f"{lot_id}.hla_e_{auc_tag}_paired[{strat}]")
                        auc_res[f"hla_e_{auc_tag}@{strat}"] = {"absolute_atg_auc": info_ha, "paired_atg_over_igg_auc": info_hp}
                        if st_ha != "PASS":
                            _lot_escalate(st_ha, f"Per-donor HLA-E {auc_tag} absolute [{strat}] verdict={st_ha} (L95={info_ha['l95_t']}, min={info_ha['min_donor']})")
                        if st_hp != "PASS":
                            _lot_escalate(
                                st_hp,
                                f"Per-donor HLA-E {auc_tag} paired vs matched IgG [{strat}] verdict={st_hp} "
                                f"(L95={info_hp['l95_t']:.4f}% vs 85.0% paired lower-bound criterion, min={info_hp['min_donor']:.4f}%)",
                            )

                    # 2. Complement C3d, C5b-9, lysis, and linked F(ab')2 per-donor dose AUC vs matched IgG
                    for fkey, ep_lbl, from_fab in (