OpenIslet

scripts/verify_wave7_cellgemma_2p24_rebase.py

Wave-7 CellGemma-Islet (IIP-0062 to IIP-0065 / GATE-40 to GATE-43) Pure-CPU

script Release v0.8.0 gen-eb955f8266035aba 1.37 MB · 90 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_wave7_cellgemma_2p24_rebase.py · SHA-256: 5f31a6369f25031d…

                            fails.append(f"FAIL_GATE_C1_{o2_tag}_DUAL_L95_SUPERIORITY_VS_BCL2L11_FAILED")

    if not (0.0 <= float(ctrl_nonbeta_t0) <= 100.0) or float(ctrl_nonbeta_t0) < 50.0:
        fails.append(f"FAIL_GATE_C1_{o2_tag}_STRESSED_CONTROL_ASSAY_VALIDITY_FLOOR")
    if float(nonbeta_surv) < 90.0:
        fails.append(f"FAIL_GATE_C1_{o2_tag}_NONBETA_LINEAGE_DAMAGE")
    if not (is_strict_bool(blk.get("t0_original_plated_denominators_used")) and blk["t0_original_plated_denominators_used"]):
        fails.append(f"FAIL_GATE_C1_{o2_tag}_MISSING_T0_DENOMINATOR_RULE")

    # Block-resolved 5-lineage timecourse plated-t0 & surviving counts (reconciling EVERY individual
    # block for INS_beta AND evaluating EVERY post-0h timepoint & per-block floor for all 4 non-beta lineages)!
    ltc = blk.get("lineage_timecourse_counts")
    if ltc is None:
        holds.append(f"HOLD_GATE_C1_{o2_tag}_MISSING_LINEAGE_TIMECOURSE_COUNTS")
    elif not isinstance(ltc, Mapping) or set(ltc.keys()) != C1_REQUIRED_5_LINEAGES:
        fails.append(f"FAIL_GATE_C1_{o2_tag}_INCOMPLETE_5_LINEAGE_TIMECOURSE_COUNTS")
    else:
        ltc_ok = True
        recomp_nonbeta_min_mean = 100.0
        recomp_nonbeta_min_blk = 100.0
        bids_norm_ltc = [str(x).strip().upper() for x in bids]
        ins_beta_blk_mismatch = False
        for lin_nm in sorted(C1_REQUIRED_5_LINEAGES):
            b_entries = ltc.get(lin_nm)
            if not isinstance(b_entries, list) or len(b_entries) != expected_n:
                ltc_ok = False; break
            ref_hrs_lin: Optional[list[float]] = None
            nonbeta_rels_by_tp: list[list[float]] = []
            for b_i, b_obj in enumerate(b_entries):
                if (
                    not isinstance(b_obj, Mapping) or set(b_obj.keys()) != {"block_id", "timepoints"}
                    or not is_nonempty_str(b_obj.get("block_id"))
                    or str(b_obj["block_id"]).strip().upper() != bids_norm_ltc[b_i]
                    or not isinstance(b_obj.get("timepoints"), list) or len(b_obj["timepoints"]) < 2
                ):
                    ltc_ok = False; break
                l_rows = b_obj["timepoints"]
                prev_h_l = -1.0
                t0_tr = t0_ct = None
                hrs_b: list[float] = []
                term_tr_pct = term_ct_pct = None
                for r_i, lr in enumerate(l_rows):
                    if not isinstance(lr, Mapping) or set(lr.keys()) != {
                        "hour_h", "treated_plated_t0_n", "treated_surviving_n", "ctrl_plated_t0_n", "ctrl_surviving_n",
                    }:
                        ltc_ok = False; break
                    hh_l = lr.get("hour_h")
                    tp0, ts, cp0, cs = lr.get("treated_plated_t0_n"), lr.get("treated_surviving_n"), lr.get("ctrl_plated_t0_n"), lr.get("ctrl_surviving_n")
                    if not (
                        is_finite_num(hh_l) and float(hh_l) > prev_h_l and (r_i > 0 or abs(float(hh_l)) <= 1e-12)
                        and all(is_strict_int(x) for x in (tp0, ts, cp0, cs))
                        and int(tp0) > 0 and int(cp0) > 0 and 0 <= int(ts) <= int(tp0) and 0 <= int(cs) <= int(cp0)
                    ):
                        ltc_ok = False; break
                    prev_h_l = float(hh_l)
                    hrs_b.append(prev_h_l)
                    if r_i == 0:
                        t0_tr, t0_ct = int(tp0), int(cp0)
                        if int(ts) != int(tp0) or int(cs) != int(cp0):
                            ltc_ok = False; break
                    else:
                        if int(tp0) != t0_tr or int(cp0) != t0_ct:
                            ltc_ok = False; break
                    tr_p = 100.0 * float(int(ts)) / float(int(tp0))
                    ct_p = 100.0 * float(int(cs)) / float(int(cp0))
                    term_tr_pct, term_ct_pct = tr_p, ct_p
                    if lin_nm != "INS_beta" and r_i > 0:
                        if ct_p <= 0.0:
                            ltc_ok = False; break
                        rel_p = 100.0 * tr_p / ct_p
                        while len(nonbeta_rels_by_tp) < r_i:
                            nonbeta_rels_by_tp.append([])
                        nonbeta_rels_by_tp[r_i - 1].append(rel_p)
                        recomp_nonbeta_min_blk = min(recomp_nonbeta_min_blk, rel_p)
                if not ltc_ok or prev_h_l < 24.0 - 1e-9 or term_tr_pct is None or term_ct_pct is None:
                    ltc_ok = False; break
                if ref_hrs_lin is None:
                    ref_hrs_lin = hrs_b
                elif len(hrs_b) != len(ref_hrs_lin) or any(abs(a - b) > 1e-9 for a, b in zip(hrs_b, ref_hrs_lin)):
                    ltc_ok = False; break
                if lin_nm == "INS_beta":
                    if abs(term_tr_pct - eff_tr_f[b_i]) > 0.5 or abs(term_ct_pct - eff_ctrl_f[b_i]) > 0.5:
                        ins_beta_blk_mismatch = True
            if not ltc_ok:
                break
            if lin_nm != "INS_beta":
                for tp_rels in nonbeta_rels_by_tp:
                    if len(tp_rels) != expected_n:
                        ltc_ok = False; break
                    recomp_nonbeta_min_mean = min(recomp_nonbeta_min_mean, sum(tp_rels) / expected_n)
                if not ltc_ok:
                    break
        if ins_beta_blk_mismatch:
            fails.append(f"FAIL_GATE_C1_{o2_tag}_INS_BETA_TIMECOURSE_MISMATCH_VS_PRIMARY")
        if not ltc_ok:
            fails.append(f"FAIL_GATE_C1_{o2_tag}_INVALID_5_LINEAGE_TIMECOURSE_COUNTS")
        elif recomp_nonbeta_min_mean < 90.0 or recomp_nonbeta_min_blk < 80.0 or float(nonbeta_surv) > recomp_nonbeta_min_mean + 0.5:
            fails.append(f"FAIL_GATE_C1_{o2_tag}_NONBETA_TIMECOURSE_COUNTS_BELOW_FLOOR_OR_MISMATCH")

    div = blk.get("death_pathway_diversion_assay")
    pmlkl = blk.get("pmlkl_flag_triggered")
    if not is_strict_bool(pmlkl):
        fails.append(f"FAIL_GATE_C1_{o2_tag}_MISSING_PMLKL_BOOL")
    div_req_scalars = (
        "pmlkl_fold_vs_ctrl", "ripk3_fold_vs_ctrl", "cleaved_casp3_fold_vs_ctrl",
        "zvad_rescue_delta_pp", "necrostatin1s_rescue_delta_pp", "gsk872_rescue_delta_pp",
    )
    div_req_keys = set(div_req_scalars) | {
        "diversion_assay_record_id", "locked_timecourse_schedule_h",
        "lytic_ldh_or_sytox_timecourse_pct", "lineage_block_diversion_records",
    }
    if div is None:
        holds.append(f"HOLD_GATE_C1_{o2_tag}_MISSING_UNCONDITIONAL_DIVERSION_ASSAY")
    elif (
        not isinstance(div, Mapping) or set(div.keys()) != div_req_keys
        or not is_nonempty_str(div.get("diversion_assay_record_id"))
        or not all(is_finite_num(div.get(k)) for k in div_req_scalars)
        or not isinstance(div.get("lytic_ldh_or_sytox_timecourse_pct"), list)
        or not isinstance(div.get("locked_timecourse_schedule_h"), list)
    ):
        fails.append(f"FAIL_GATE_C1_{o2_tag}_MALFORMED_DIVERSION_ASSAY")
    else:
        pm_f, r3_f = float(div["pmlkl_fold_vs_ctrl"]), float(div["ripk3_fold_vs_ctrl"])
        c3_f = float(div["cleaved_casp3_fold_vs_ctrl"])
        n1_d, g8_d = float(div["necrostatin1s_rescue_delta_pp"]), float(div["gsk872_rescue_delta_pp"])
        sched_raw = div["locked_timecourse_schedule_h"]
        lytic_tc = div["lytic_ldh_or_sytox_timecourse_pct"]
        lb_div = div["lineage_block_diversion_records"]

        # Locked GY-016 timecourse coverage: >= 4 points (0 h, intermediate <= 12 h, 24 h, terminal >= 48 h, max gap <= 24 h)!
        sched_valid_nums = isinstance(sched_raw, list) and len(sched_raw) >= 2 and all(is_finite_num(x) for x in sched_raw)
        sched_f = [float(x) for x in sched_raw] if sched_valid_nums else []
        sched_covered = (
            sched_valid_nums
            and len(sched_f) >= 4
            and abs(sched_f[0]) <= 1e-9
            and any(0.0 < h <= 12.0 + 1e-9 for h in sched_f)
            and any(abs(h - 24.0) <= 1e-6 for h in sched_f)
            and sched_f[-1] >= 48.0 - 1e-9
            and all(sched_f[k] < sched_f[k + 1] and (sched_f[k + 1] - sched_f[k]) <= 24.0 + 1e-9 for k in range(len(sched_f) - 1))
        )
        if not sched_covered:
            holds.append(f"HOLD_GATE_C1_{o2_tag}_DIVERSION_TIMECOURSE_COVERAGE_INCOMPLETE")

        lytic_valid = True
        lytic_elevated = False
        prev_lh = -1.0
        for lrow in lytic_tc:
            if (
                not isinstance(lrow, Mapping)
                or set(lrow.keys()) != {"hour_h", "treated_lytic_pct", "ctrl_lytic_pct"}
                or not all(is_finite_num(lrow.get(k)) for k in ("hour_h", "treated_lytic_pct", "ctrl_lytic_pct"))
            ):
                lytic_valid = False; break
            lh, lt, lc = float(lrow["hour_h"]), float(lrow["treated_lytic_pct"]), float(lrow["ctrl_lytic_pct"])
            if lh <= prev_lh or not (0.0 <= lt <= 100.0 and 0.0 <= lc <= 100.0):
                lytic_valid = False; break
            prev_lh = lh
            if lh > 0.0 and (lt > 20.0 or lt > lc + 5.0):
                lytic_elevated = True
        if lytic_valid and (not lytic_tc or float(lytic_tc[0]["hour_h"]) != 0.0 or prev_lh < 24.0):
            lytic_valid = False
        if sched_covered and lytic_valid and [float(r["hour_h"]) for r in lytic_tc] != sched_f:
            holds.append(f"HOLD_GATE_C1_{o2_tag}_DIVERSION_TIMECOURSE_COVERAGE_INCOMPLETE")

        # Live GY-016 mandatory lineage-gated (INS_beta & PLVAP_iEC), block- & specimen-linked diversion + rescue!
        gy016_req_lineages = ("INS_beta", "PLVAP_iEC")
        lb_row_keys = frozenset({
            "block_id", "specimen_id", "rescue_assay_record_id",
            "pmlkl_fold_vs_ctrl", "ripk3_fold_vs_ctrl", "cleaved_casp3_fold_vs_ctrl",
            "zvad_rescue_delta_pp", "necrostatin1s_rescue_delta_pp", "gsk872_rescue_delta_pp",
            "paired_viability_timecourse_pct", "lytic_ldh_or_sytox_timecourse_pct",
        })
        worst_pm, worst_r3, worst_n1, worst_g8 = pm_f, r3_f, n1_d, g8_d
        worst_beta_c3 = c3_f
        lb_schema_ok = True
        if not isinstance(lb_div, Mapping) or not all(lin in lb_div for lin in gy016_req_lineages):
            holds.append(f"HOLD_GATE_C1_{o2_tag}_DIVERSION_MISSING_REQUIRED_LINEAGE")
        else:
            bids_norm_d = [str(x).strip().upper() for x in bids]
            seen_d_sp: set[str] = set()
            seen_d_aid: set[str] = set()
            for lin_nm in gy016_req_lineages:
                rows_l = lb_div.get(lin_nm)
                if not isinstance(rows_l, list) or len(rows_l) != expected_n:
                    lb_schema_ok = False; break
                for bi_d, brow in enumerate(rows_l):
                    if (
                        not isinstance(brow, Mapping) or set(brow.keys()) != lb_row_keys
                        or not is_nonempty_str(brow.get("block_id"))
                        or str(brow["block_id"]).strip().upper() != bids_norm_d[bi_d]
                        or not is_nonempty_str(brow.get("specimen_id"))
                        or not is_nonempty_str(brow.get("rescue_assay_record_id"))
                        or not all(is_finite_num(brow.get(k)) for k in div_req_scalars)
                        or not isinstance(brow.get("paired_viability_timecourse_pct"), list)
                        or not isinstance(brow.get("lytic_ldh_or_sytox_timecourse_pct"), list)
                    ):
                        lb_schema_ok = False; break
                    sp_d = str(brow["specimen_id"]).strip().upper()
                    aid_d = str(brow["rescue_assay_record_id"]).strip().upper()
                    if sp_d in seen_d_sp or aid_d in seen_d_aid:
                        lb_schema_ok = False; break
                    seen_d_sp.add(sp_d); seen_d_aid.add(aid_d)
                    bpm, br3, bc3 = float(brow["pmlkl_fold_vs_ctrl"]), float(brow["ripk3_fold_vs_ctrl"]), float(brow["cleaved_casp3_fold_vs_ctrl"])
                    bn1, bg8 = float(brow["necrostatin1s_rescue_delta_pp"]), float(brow["gsk872_rescue_delta_pp"])
                    if bpm <= 0.0 or br3 <= 0.0 or bc3 <= 0.0:
                        lb_schema_ok = False; break
                    worst_pm = max(worst_pm, bpm)
                    worst_r3 = max(worst_r3, br3)
                    worst_n1 = max(worst_n1, bn1)
                    worst_g8 = max(worst_g8, bg8)
                    if lin_nm == "INS_beta":
                        worst_beta_c3 = max(worst_beta_c3, bc3)
                    # Check per-lineage paired viability timecourse reconciled to lineage_timecourse_counts!
                    pv_tc = brow["paired_viability_timecourse_pct"]
                    bl_tc = brow["lytic_ldh_or_sytox_timecourse_pct"]
                    if not (
                        len(pv_tc) == len(sched_f) and len(bl_tc) == len(sched_f)
                        and all(isinstance(pvr, Mapping) and set(pvr.keys()) == {"hour_h", "treated_viab_pct", "ctrl_viab_pct"} and all(is_finite_num(pvr[k]) for k in pvr) for pvr in pv_tc)
                        and all(isinstance(blr, Mapping) and set(blr.keys()) == {"hour_h", "treated_lytic_pct", "ctrl_lytic_pct"} and all(is_finite_num(blr[k]) for k in blr) for blr in bl_tc)
                    ):
                        lb_schema_ok = False; break
                    if [float(r["hour_h"]) for r in pv_tc] != sched_f or [float(r["hour_h"]) for r in bl_tc] != sched_f:
                        lb_schema_ok = False; break
                    for blr in bl_tc:
                        bh_l, bl_t, bl_c = float(blr["hour_h"]), float(blr["treated_lytic_pct"]), float(blr["ctrl_lytic_pct"])
                        if not (0.0 <= bl_t <= 100.0 and 0.0 <= bl_c <= 100.0):
                            lb_schema_ok = False; break
                        if bh_l > 0.0 and (bl_t > 20.0 or bl_t > bl_c + 5.0):
                            lytic_elevated = True
                    if not lb_schema_ok:
                        break
                    # Link directly against lineage_timecourse_counts[lin_nm][bi_d] at every shared timepoint!
                    if isinstance(ltc, Mapping) and isinstance(ltc.get(lin_nm), list) and len(ltc[lin_nm]) == expected_n:
                        ltc_tp_map = {
                            float(tp["hour_h"]): (
                                100.0 * float(int(tp["treated_surviving_n"])) / float(int(tp["treated_plated_t0_n"])),
                                100.0 * float(int(tp["ctrl_surviving_n"])) / float(int(tp["ctrl_plated_t0_n"])),
                            )
                            for tp in ltc[lin_nm][bi_d].get("timepoints", [])
                            if isinstance(tp, Mapping) and is_finite_num(tp.get("hour_h")) and is_strict_int(tp.get("treated_plated_t0_n")) and int(tp["treated_plated_t0_n"]) > 0
                        }
                        for pvr in pv_tc:
                            ph_v = float(pvr["hour_h"])
                            if ph_v in ltc_tp_map:
                                exp_tr_v, exp_ct_v = ltc_tp_map[ph_v]
                                if abs(float(pvr["treated_viab_pct"]) - exp_tr_v) > 0.5 or abs(float(pvr["ctrl_viab_pct"]) - exp_ct_v) > 0.5:
                                    lb_schema_ok = False; break
                    if not lb_schema_ok:
                        break