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…

                si = [float(x) for x in s_il]
                pos_c = [c for c in cg if c > 0.0]
                if cg[0] != 0.0 or any(cg[i] >= cg[i + 1] for i in range(len(cg) - 1)) or not pos_c:
                    fails.append("FAIL_GATE_B0A_INVALID_CONC_GRID_ORDER"); b0a_ok = False
                elif not (min(pos_c) < ec50_u95 < csafe_bench < max(cg)):
                    fails.append("FAIL_GATE_B0A_BOUNDS_NOT_OBSERVATION_BRACKETED"); b0a_ok = False
                else:
                    # Statistically qualified two-sided bracketing across ALL 3 Draft B s5 biological efficacy endpoints
                    # (beta cytokine protection, NK cytokine suppression, endothelial IBMIR marker suppression):
                    if any(abs(eg[i] - min(ebg[i], eng[i], eig[i])) > 0.5 for i in range(len(cg))):
                        fails.append("FAIL_GATE_B0A_OPERATIVE_EFFICACY_GRID_NOT_CONJUNCTIVE_MIN_OF_3_ENDPOINTS"); b0a_ok = False
                    veh_ok = (max(eg[0], ebg[0], eng[0], eig[0]) <= 15.0 and ss[0] >= 95.0 and sn[0] >= 95.0 and sc94[0] >= 95.0 and si[0] >= 95.0)
                    eff_at_or_below = [eg[i] for i in range(len(cg)) if cg[i] <= ec50_u95 + 1e-9]
                    max_below_50_c = max((cg[i] for i in range(len(cg)) if eg[i] < 50.0), default=0.0)
                    eff_crossed = any(v < 50.0 for v in eff_at_or_below) and any(v >= 50.0 for v in eff_at_or_below) and (eg[-1] >= 50.0) and (ec50_u95 >= max_below_50_c - 1e-9)
                    s_reps = b0a_ev.get("safety_replicates_by_conc")
                    rep_keys = ("sprouting_pct", "nkg2a_pct", "cd94_pct", "il10_retention_pct")
                    rep_floors = (90.0, 85.0, 85.0, 80.0001)
                    rep_grids = (ss, sn, sc94, si)
                    if not isinstance(s_reps, list) or len(s_reps) != len(cg):
                        fails.append("FAIL_GATE_B0A_MISSING_SAFETY_REPLICATES_BY_CONC"); b0a_ok = False
                    else:
                        qual_safe_idx = -1
                        reps_ok = True
                        s_row_req_keys = set(rep_keys) | {"unit_ids", "matched_vehicle_record_id", "raw_unit_measurements"}
                        s_first_uids: Optional[list[str]] = None
                        for i_c, rmap in enumerate(s_reps):
                            if isinstance(rmap, Mapping) and rmap.get("raw_unit_measurements") is None:
                                holds.append("HOLD_GATE_B0A_MISSING_MATCHED_VEHICLE_RAW_DATA"); reps_ok = False; b0a_ok = False; break
                            if not isinstance(rmap, Mapping) or set(rmap.keys()) != s_row_req_keys or not is_nonempty_str(rmap.get("matched_vehicle_record_id")):
                                fails.append(f"FAIL_GATE_B0A_MALFORMED_SAFETY_REPLICATES:{i_c}"); reps_ok = False; b0a_ok = False; break
                            mv_rec_s = str(rmap["matched_vehicle_record_id"]).strip().upper()
                            raw_s_map = rmap.get("raw_unit_measurements")
                            if not isinstance(raw_s_map, Mapping) or set(raw_s_map.keys()) != set(rep_keys):
                                fails.append(f"FAIL_GATE_B0A_MALFORMED_SAFETY_REPLICATES:{i_c}"); reps_ok = False; b0a_ok = False; break
                            su_raw = rmap.get("unit_ids")
                            if not (isinstance(su_raw, list) and len(su_raw) >= 6 and all(is_nonempty_str(u) for u in su_raw)):
                                fails.append(f"FAIL_GATE_B0A_UNDERPOWERED_OR_INVALID_SAFETY_UNITS:{i_c}"); reps_ok = False; b0a_ok = False; break
                            su_norm = [str(u).strip().upper() for u in su_raw]
                            if len(set(su_norm)) != len(su_norm):
                                fails.append(f"FAIL_GATE_B0A_DUPLICATE_SAFETY_UNIT_IDS:{i_c}"); reps_ok = False; b0a_ok = False; break
                            if s_first_uids is None:
                                s_first_uids = su_norm
                            elif su_norm != s_first_uids:
                                fails.append(f"FAIL_GATE_B0A_SAFETY_UNIT_IDS_MISMATCH:{i_c}"); reps_ok = False; b0a_ok = False; break
                            all_ep_l95_safe = True
                            for rk, rfl, rgr in zip(rep_keys, rep_floors, rep_grids):
                                arr = rmap.get(rk)
                                ep_raw_s = raw_s_map.get(rk)
                                if not (
                                    isinstance(arr, list) and len(arr) == len(su_norm) and all(is_finite_num(x) for x in arr)
                                    and isinstance(ep_raw_s, Mapping) and set(ep_raw_s.keys()) == {"vehicle_record_id", "treated", "vehicle"}
                                    and is_nonempty_str(ep_raw_s.get("vehicle_record_id"))
                                    and str(ep_raw_s["vehicle_record_id"]).strip().upper() == mv_rec_s
                                    and isinstance(ep_raw_s.get("treated"), list) and len(ep_raw_s["treated"]) == len(su_norm)
                                    and isinstance(ep_raw_s.get("vehicle"), list) and len(ep_raw_s["vehicle"]) == len(su_norm)
                                    and all(is_finite_num(x) and float(x) >= 0.0 for x in ep_raw_s["treated"])
                                    and all(is_finite_num(x) and float(x) > 0.0 for x in ep_raw_s["vehicle"])
                                ):
                                    fails.append(f"FAIL_GATE_B0A_INVALID_SAFETY_REPLICATES:{i_c}:{rk}"); reps_ok = False; b0a_ok = False; all_ep_l95_safe = False; break
                                recomp_s = [100.0 * float(ep_raw_s["treated"][uj]) / float(ep_raw_s["vehicle"][uj]) for uj in range(len(su_norm))]
                                sub_s = [float(x) for x in arr]
                                if any(abs(recomp_s[uj] - sub_s[uj]) > 0.5 for uj in range(len(su_norm))):
                                    fails.append(f"FAIL_GATE_B0A_NORMALIZED_PCT_MISMATCH_VS_MATCHED_VEHICLE:{i_c}:{rk}"); reps_ok = False; b0a_ok = False; all_ep_l95_safe = False; break
                                af = recomp_s
                                m_af = sum(af) / len(af)
                                if abs(m_af - rgr[i_c]) > 0.5:
                                    fails.append(f"FAIL_GATE_B0A_GRID_MEAN_MISMATCH_VS_REPLICATES:{i_c}:{rk}"); reps_ok = False; b0a_ok = False
                                alpha_safe_b0a = 0.05 / float(4 * len(cg))
                                sb = sample_t_one_sided_bounds(af, alpha=alpha_safe_b0a)
                                if sb is None:
                                    holds.append("HOLD_GATE_B0A_DEGENERATE_ZERO_VARIANCE_UNESTIMABLE")
                                    l95_af = float("-inf")
                                else:
                                    # Audit Gaussian/unimodal Student-t precondition at n >= 6 (Q-Q r >= 0.88 & Shapiro-Wilk p >= 0.05)
                                    # AND enforce unconditional single-unit safety floor (no individual unit below floor)!
                                    qq_s = paired_diff_normality_qq_r(af)
                                    sw_s = shapiro_wilk_test(af)
                                    if qq_s is None or qq_s < 0.88 or sw_s is None or sw_s[1] < 0.05:
                                        if cg[i_c] <= csafe_bench + 1e-9:
                                            holds.append(f"HOLD_GATE_B0A_NONNORMAL_OR_SKEWED_SAFETY_REPLICATES:{i_c}:{rk}")
                                        l95_af = float("-inf")
                                    else:
                                        l95_af = sb[1]
                                if l95_af < rfl or min(af) < rfl:
                                    all_ep_l95_safe = False
                            if all_ep_l95_safe and qual_safe_idx == i_c - 1:
                                qual_safe_idx = i_c
                        if reps_ok:
                            max_qual_safe_c = cg[qual_safe_idx] if qual_safe_idx >= 0 else -1.0
                            safe_breached_above = any(
                                (ss[i] < 90.0 or sn[i] < 85.0 or sc94[i] < 85.0 or si[i] <= 80.0)
                                for i in range(len(cg)) if cg[i] > max_qual_safe_c + 1e-9
                            )
                            if csafe_bench > max_qual_safe_c + 1e-9:
                                fails.append("FAIL_GATE_B0A_CSAFE_EXCEEDS_QUALIFIED_TESTED_SAFE_CONC"); b0a_ok = False
                            elif not (veh_ok and eff_crossed and safe_breached_above):
                                fails.append("FAIL_GATE_B0A_RAW_CONC_RESPONSE_CONTRADICTS_WINDOW"); b0a_ok = False
                    # Independent efficacy unit replicates + content-hashed analysis receipt verifying EC50_eff_U95!
                    e_reps = b0a_ev.get("efficacy_replicates_by_conc")
                    b0a_an = b0a_ev.get("b0a_analysis_receipt")
                    if e_reps is None or b0a_an is None:
                        holds.append("HOLD_GATE_B0A_MISSING_EFFICACY_REPLICATES_OR_ANALYSIS_RECEIPT"); b0a_ok = False
                    elif not isinstance(e_reps, list) or len(e_reps) != len(cg):
                        fails.append("FAIL_GATE_B0A_MALFORMED_EFFICACY_REPLICATES"); b0a_ok = False
                    else:
                        an_keys = frozenset({
                            "analysis_method", "efficacy_combination_rule", "efficacy_endpoints_tested",
                            "efficacy_unit_ids", "safety_unit_ids",
                            "raw_efficacy_replicates_sha256", "raw_safety_replicates_sha256",
                            "analysis_record_sha256",
                            "ec50_eff_u95_free_nM", "c_safe_max_bench_l95_free_nM", "receipt_sha256",
                        })
                        req_eff_eps = [
                            "beta_cytokine_protection_pct",
                            "iec_ibmir_marker_suppression_pct",
                            "nk_cytokine_suppression_pct",
                        ]
                        if not isinstance(b0a_an, Mapping) or set(b0a_an.keys()) != an_keys:
                            fails.append("FAIL_GATE_B0A_MALFORMED_ANALYSIS_RECEIPT"); b0a_ok = False
                        else:
                            asha = b0a_an.get("receipt_sha256")
                            euids = b0a_an.get("efficacy_unit_ids")
                            suids_an = b0a_an.get("safety_unit_ids")
                            eps_t = b0a_an.get("efficacy_endpoints_tested")
                            if (
                                not is_valid_sha256_hex(asha) or compute_receipt_sha256(b0a_an) != asha
                                or b0a_an.get("analysis_method") != "SIMULTANEOUS_BONFERRONI_GRID_BRACKET_STUDENT_T"
                                or b0a_an.get("efficacy_combination_rule") != "CONJUNCTIVE_MAX_EC50_U95_ACROSS_ALL_3_ENDPOINTS"
                                or not (isinstance(eps_t, list) and sorted(str(x) for x in eps_t) == req_eff_eps)
                                or b0a_an.get("raw_efficacy_replicates_sha256") != sha256_bytes(canonical_json_bytes(e_reps))
                                or (s_reps is not None and b0a_an.get("raw_safety_replicates_sha256") != sha256_bytes(canonical_json_bytes(s_reps)))
                                or b0a_an.get("analysis_record_sha256") != b0a_ev.get("analysis_record_sha256")
                                or not (isinstance(euids, list) and len(euids) >= 6 and all(is_nonempty_str(u) for u in euids) and len({str(u).strip().upper() for u in euids}) == len(euids))
                                or not (isinstance(suids_an, list) and len(suids_an) >= 6 and all(is_nonempty_str(u) for u in suids_an) and len({str(u).strip().upper() for u in suids_an}) == len(suids_an))
                                or (s_reps is not None and isinstance(s_reps, list) and s_reps and isinstance(s_reps[0], Mapping) and isinstance(s_reps[0].get("unit_ids"), list) and [str(u).strip().upper() for u in suids_an] != [str(u).strip().upper() for u in s_reps[0]["unit_ids"]])
                                or not (is_finite_num(b0a_an.get("ec50_eff_u95_free_nM")) and abs(float(b0a_an["ec50_eff_u95_free_nM"]) - ec50_u95) <= 1e-6)
                                or not (is_finite_num(b0a_an.get("c_safe_max_bench_l95_free_nM")) and abs(float(b0a_an["c_safe_max_bench_l95_free_nM"]) - csafe_bench) <= 1e-6)
                            ):
                                fails.append("FAIL_GATE_B0A_ANALYSIS_RECEIPT_MISMATCH"); b0a_ok = False
                            else:
                                exp_eu = [str(u).strip().upper() for u in euids]
                                eff_l95_min_by_i: list[float] = []
                                ereps_ok = True
                                ep_grid_pairs = (
                                    ("beta_cytokine_protection_pct", ebg),
                                    ("nk_cytokine_suppression_pct", eng),
                                    ("iec_ibmir_marker_suppression_pct", eig),
                                )
                                for i_c, erow in enumerate(e_reps):
                                    if isinstance(erow, Mapping) and (erow.get("matched_vehicle_record_id") is None or erow.get("raw_unit_measurements") is None):
                                        holds.append("HOLD_GATE_B0A_MISSING_MATCHED_VEHICLE_RAW_DATA"); ereps_ok = False; b0a_ok = False; break
                                    if not isinstance(erow, Mapping) or set(erow.keys()) != {"unit_ids", "matched_vehicle_record_id", "raw_unit_measurements", "beta_cytokine_protection_pct", "nk_cytokine_suppression_pct", "iec_ibmir_marker_suppression_pct"} or not is_nonempty_str(erow.get("matched_vehicle_record_id")):
                                        fails.append(f"FAIL_GATE_B0A_MALFORMED_EFFICACY_REPLICATES:{i_c}"); ereps_ok = False; b0a_ok = False; break
                                    mv_rec_e = str(erow["matched_vehicle_record_id"]).strip().upper()
                                    raw_e_map = erow.get("raw_unit_measurements")
                                    if not isinstance(raw_e_map, Mapping) or set(raw_e_map.keys()) != {k for k, _ in ep_grid_pairs}:
                                        fails.append(f"FAIL_GATE_B0A_MALFORMED_EFFICACY_REPLICATES:{i_c}"); ereps_ok = False; b0a_ok = False; break
                                    ru = erow.get("unit_ids")
                                    if not (isinstance(ru, list) and [str(u).strip().upper() for u in ru] == exp_eu):
                                        fails.append(f"FAIL_GATE_B0A_INVALID_EFFICACY_REPLICATES:{i_c}"); ereps_ok = False; b0a_ok = False; break
                                    ep_l95s: list[float] = []
                                    for epk, ep_grid in ep_grid_pairs:
                                        rv = erow.get(epk)
                                        ep_raw_e = raw_e_map.get(epk)
                                        if not (
                                            isinstance(rv, list) and len(rv) == len(exp_eu) and all(is_finite_num(x) for x in rv)
                                            and isinstance(ep_raw_e, Mapping) and set(ep_raw_e.keys()) == {"vehicle_record_id", "treated", "vehicle"}
                                            and is_nonempty_str(ep_raw_e.get("vehicle_record_id"))
                                            and str(ep_raw_e["vehicle_record_id"]).strip().upper() == mv_rec_e
                                            and isinstance(ep_raw_e.get("treated"), list) and len(ep_raw_e["treated"]) == len(exp_eu)
                                            and isinstance(ep_raw_e.get("vehicle"), list) and len(ep_raw_e["vehicle"]) == len(exp_eu)
                                            and all(is_finite_num(x) and float(x) >= 0.0 for x in ep_raw_e["treated"])
                                            and all(is_finite_num(x) and float(x) > 0.0 for x in ep_raw_e["vehicle"])
                                        ):
                                            fails.append(f"FAIL_GATE_B0A_INVALID_EFFICACY_REPLICATES:{i_c}"); ereps_ok = False; b0a_ok = False; break
                                        recomp_e = [100.0 * (1.0 - float(ep_raw_e["treated"][uj]) / float(ep_raw_e["vehicle"][uj])) for uj in range(len(exp_eu))]
                                        sub_e = [float(x) for x in rv]
                                        if any(abs(recomp_e[uj] - sub_e[uj]) > 0.5 for uj in range(len(exp_eu))):
                                            fails.append(f"FAIL_GATE_B0A_EFFICACY_NORMALIZED_PCT_MISMATCH_VS_MATCHED_VEHICLE:{i_c}:{epk}"); ereps_ok = False; b0a_ok = False; break
                                        rf = recomp_e
                                        if abs((sum(rf) / len(rf)) - ep_grid[i_c]) > 0.5:
                                            fails.append(f"FAIL_GATE_B0A_EFFICACY_GRID_MISMATCH_VS_REPLICATES:{i_c}"); ereps_ok = False; b0a_ok = False
                                        alpha_eff_b0a = 0.05 / float(3 * len(cg))
                                        eb = sample_t_one_sided_bounds(rf, alpha=alpha_eff_b0a)
                                        if eb is None:
                                            holds.append("HOLD_GATE_B0A_DEGENERATE_ZERO_VARIANCE_UNESTIMABLE")
                                            ep_l95s.append(float("-inf"))
                                        else:
                                            qq_e = paired_diff_normality_qq_r(rf)
                                            sw_e = shapiro_wilk_test(rf)
                                            if qq_e is None or qq_e < 0.88 or sw_e is None or sw_e[1] < 0.05:
                                                if cg[i_c] <= ec50_u95 + 1e-9:
                                                    holds.append(f"HOLD_GATE_B0A_NONNORMAL_OR_SKEWED_EFFICACY_REPLICATES:{i_c}:{epk}")
                                                ep_l95s.append(float("-inf"))
                                            else:
                                                ep_l95s.append(eb[1])
                                    if not ereps_ok:
                                        break