scripts/verify_wave7_cellgemma_2p24_rebase.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_wave7_cellgemma_2p24_rebase.py · SHA-256: 5f31a6369f25031d…
and all(is_finite_num(x) and float(x) >= 0.0 for x in raw_ex["treated_by_unit_tech"][uj])
and all(is_finite_num(x) and float(x) > 0.0 for x in raw_ex["vehicle_by_unit_tech"][uj])
for uj in range(len(eu_norm))
)
):
fails.append(f"FAIL_GATE_B0A_EXACT_MALFORMED_UNIT_REPLICATES:{i_e}"); ex_rows_ok = False; b0a_ex_ok = False; break
af_e: list[float] = []
for uj, tw in enumerate(tr_e):
recomp_tw = [
100.0 * float(raw_ex["treated_by_unit_tech"][uj][tk]) / float(raw_ex["vehicle_by_unit_tech"][uj][tk])
for tk in range(len(tw))
]
if any(abs(recomp_tw[tk] - float(tw[tk])) > 0.5 for tk in range(len(tw))):
fails.append(f"FAIL_GATE_B0A_EXACT_NORMALIZED_PCT_MISMATCH_VS_MATCHED_VEHICLE:{i_e}:{eu_norm[uj]}"); ex_rows_ok = False; b0a_ex_ok = False; break
tw_mean = sum(recomp_tw) / len(recomp_tw)
if abs(tw_mean - float(arr_e[uj])) > 0.05:
fails.append(f"FAIL_GATE_B0A_EXACT_BLOCK_AGGREGATION_MISMATCH:{i_e}:{eu_norm[uj]}"); ex_rows_ok = False; b0a_ex_ok = False; break
af_e.append(tw_mean)
if not ex_rows_ok:
break
if abs((sum(af_e) / len(af_e)) - rg_e[i_e]) > 0.5:
fails.append(f"FAIL_GATE_B0A_EXACT_GRID_MEAN_MISMATCH:{i_e}"); b0a_ex_ok = False
sb_e = sample_t_one_sided_bounds(af_e, alpha=0.05 / float(len(cg_e)))
if sb_e is None:
holds.append("HOLD_GATE_B0A_EXACT_DEGENERATE_ZERO_VARIANCE_UNESTIMABLE")
l95_e = float("-inf")
else:
l95_e = sb_e[1]
if l95_e >= 85.0 and q_idx_e == i_e - 1:
q_idx_e = i_e
if ex_rows_ok:
max_q_safe_e = cg_e[q_idx_e] if q_idx_e >= 0 else -1.0
if csafe_exact > max_q_safe_e + 1e-9:
fails.append("FAIL_GATE_B0A_EXACT_CSAFE_EXCEEDS_QUALIFIED_TESTED_SAFE_CONC"); b0a_ex_ok = False
elif rg_e[0] < 95.0 or not any(rg_e[i] < 85.0 for i in range(len(cg_e)) if cg_e[i] > max_q_safe_e + 1e-9):
fails.append("FAIL_GATE_B0A_EXACT_GRID_NOT_TWO_SIDED_BRACKETED"); b0a_ex_ok = False
if not (is_finite_num(ret) and 85.0 <= float(ret) <= 100.0):
fails.append("FAIL_GATE_B0A_EXACT_CASSETTE_RETENTION_BELOW_85PCT"); b0a_ex_ok = False
if spec is not None and not _validate_lineage_hour0_density(
spec, b0a_ex_ev.get("hour0_density_at_csafe_by_lineage"),
required_lineages=REQUIRED_HOUR0_LINEAGES_FULL, fails=fails, prefix="GATE_B0A_EXACT",
):
b0a_ex_ok = False
b2be_ev = _validate_content_receipt(
rec.get("b2b_exact_evidence"),
expected_stage="B-2b-exact", expected_prod=prod_s, expected_bank=bank_s,
expected_lot=lot_s, expected_route=route_s, expected_scope="exact_product",
expected_coating_identity=coat_id,
expected_parent=b2bb_ev,
min_prereq_receipts=[("BANK_MATERIALIZATION", mat_ev), ("B-0a-exact", b0a_ex_ev), ("B-1", b1_ev), ("B-2", b2_ev)],
allowed_extra_keys=b2b_common_extras | frozenset({"c_safe_max_operative_l95_free_nM"}),
fails=fails, holds=holds,
missing_hold="HOLD_GATE_B2B_EXACT_MISSING",
fail_code="FAIL_GATE_B2B_EXACT_FAILED",
mismatch_code="FAIL_GATE_B2B_EXACT_SCOPE_MISMATCH",
)
b2be_ok = False
csafe_op_val: Optional[float] = None
if b2be_ev is not None:
cs_op = b2be_ev.get("c_safe_max_operative_l95_free_nM")
if not is_finite_num(cs_op) or float(cs_op) <= 0.0:
fails.append("FAIL_GATE_B2B_EXACT_MISSING_OPERATIVE_CSAFE")
elif csafe_bench is not None and csafe_exact is not None:
derived_op = min(csafe_bench, csafe_exact)
if abs(float(cs_op) - derived_op) > 1e-9:
fails.append("FAIL_GATE_B2B_EXACT_OPERATIVE_NOT_EQUAL_TO_MIN_BENCH_AND_EXACT")
else:
csafe_op_val = derived_op
if ec50_u95 is not None and csafe_op_val / ec50_u95 < 2.0:
fails.append("FAIL_GATE_B2B_EXACT_OPERATIVE_MARGIN_BELOW_2X")
elif ec50_u95 is not None and lloq_parent_map_f is not None and lloq_bmt_map_f is not None and fu_parent_map_f is not None and fu_bmt_map_f is not None:
b2be_ok = _validate_b2b_exposure_block(
b2be_ev, ec50_u95=ec50_u95, lloq_parent_by_matrix=lloq_parent_map_f,
lloq_bmt_by_matrix=lloq_bmt_map_f, fu_parent_by_matrix=fu_parent_map_f,
fu_bmt_by_matrix=fu_bmt_map_f, ceiling_l95=csafe_op_val,
expected_prod=prod_s, expected_bank=bank_s, expected_lot=lot_s, expected_route=route_s,
expected_scope="exact_product", expected_coating_identity=coat_id,
fails=fails, holds=holds, prefix="GATE_B2B_EXACT",
upstream_sample_ledger=b12_combined_ledger,
)
# Gate B-3: construct-specific component shields + matched blank-vehicle Readout-2 (>= 90% Fieller!) + Readout-3B (SIRPa-low + <= +2 pp delta!)
b3_extras = frozenset({
"transgene_cassette_retention_pct", "shield_retention_by_component",
"b3_cytokine_challenge_shield_panel",
"il10_suppression_pct", "post_washout_72h_il10_recovery_pct",
"il10_washout_recovery_assay",
"nkg2a_mfi_pct_ctrl", "cd94_mfi_pct_ctrl",
"readout3b_evidence", "readout2_evidence", "hour0_density_by_lineage",
})
b3_ev = _validate_content_receipt(
rec.get("b3_evidence"),
expected_stage="B-3", expected_prod=prod_s, expected_bank=bank_s,
expected_lot=lot_s, expected_route=route_s, expected_scope="exact_product",
expected_coating_identity=coat_id,
expected_parent=b2be_ev, min_prereq_receipts=[("BANK_MATERIALIZATION", mat_ev)],
allowed_extra_keys=b3_extras, fails=fails, holds=holds,
missing_hold="HOLD_GATE_B3_EXACT_MATRIX_MISSING",
fail_code="FAIL_GATE_B3_EXACT_MATRIX_FAILED",
mismatch_code="FAIL_GATE_B3_SCOPE_MISMATCH",
)
b3_ok = b3_ev is not None and b3_ev.get("verdict") == "PASS"
blank_veh_40 = coat_id["matched_blank_vehicle_lot_id"] if coat_id is not None else None
if b3_ev is not None:
cas = b3_ev.get("transgene_cassette_retention_pct")
smap = b3_ev.get("shield_retention_by_component")
il10 = b3_ev.get("il10_suppression_pct")
il10_rec = b3_ev.get("post_washout_72h_il10_recovery_pct")
nkg_mfi = b3_ev.get("nkg2a_mfi_pct_ctrl")
cd94_mfi = b3_ev.get("cd94_mfi_pct_ctrl")
if not (is_finite_num(cas) and 85.0 <= float(cas) <= 100.0):
fails.append("FAIL_GATE_B3_CASSETTE_RETENTION_BELOW_85PCT"); b3_ok = False
req_comp = SHIELD_COMPONENTS_BY_PRODUCT.get(prod_s, frozenset())
if not isinstance(smap, Mapping) or set(smap.keys()) != req_comp:
fails.append("FAIL_GATE_B3_INCOMPLETE_COMPONENT_SHIELD_MAP"); b3_ok = False
else:
for cname in sorted(req_comp):
cv = smap.get(cname)
if not (is_finite_num(cv) and 85.0 <= float(cv) <= 100.0):
fails.append(f"FAIL_GATE_B3_SHIELD_COMPONENT_BELOW_85PCT:{cname}"); b3_ok = False
# Exact-product per-challenge matched-control block shield retention panel (Draft B s5: IFNG+TNFA, IFNG+IL1B, IFNA+low_IFNG)
# quantitatively linked to B-2b-exact exposure window [days0_10_parent_lower_free_nM, u95_joint_peak_free_nM]!
b3_cpanel = b3_ev.get("b3_cytokine_challenge_shield_panel")
b3_req_challenges = frozenset({"IFNG_PLUS_TNFA", "IFNG_PLUS_IL1B", "IFNA_PLUS_LOW_IFNG"})
b3_panel_keys = frozenset({
"lot_id", "matched_blank_vehicle_lot_id", "b2b_exact_receipt_sha256",
"exposure_started_at_utc", "assessed_at_utc",
"measured_free_parent_nM", "measured_free_bmt_nM",
"challenges", "panel_receipt_sha256",
})
b2be_p_lo = float(b2be_ev["days0_10_parent_lower_free_nM"]) if (b2be_ev is not None and is_finite_num(b2be_ev.get("days0_10_parent_lower_free_nM"))) else None
b2be_j_hi = float(b2be_ev["u95_joint_peak_free_nM"]) if (b2be_ev is not None and is_finite_num(b2be_ev.get("u95_joint_peak_free_nM"))) else None
t_b3_end_c = parse_utc_iso_ts(b3_ev.get("recorded_at_utc"))
t_b2be_end_c = parse_utc_iso_ts(b2be_ev.get("recorded_at_utc")) if b2be_ev is not None else None
if b3_cpanel is None:
holds.append("HOLD_GATE_B3_MISSING_CYTOKINE_CHALLENGE_SHIELD_PANEL"); b3_ok = False
elif not isinstance(b3_cpanel, Mapping) or set(b3_cpanel.keys()) != b3_panel_keys:
fails.append("FAIL_GATE_B3_CYTOKINE_CHALLENGE_SHIELD_PANEL_INVALID"); b3_ok = False
else:
psha_b3 = b3_cpanel.get("panel_receipt_sha256")
t_pexp = parse_utc_iso_ts(b3_cpanel.get("exposure_started_at_utc"))
t_pass = parse_utc_iso_ts(b3_cpanel.get("assessed_at_utc"))
mfp = b3_cpanel.get("measured_free_parent_nM")
mfb = b3_cpanel.get("measured_free_bmt_nM")
chals = b3_cpanel.get("challenges")
if (
not is_valid_sha256_hex(psha_b3)
or sha256_bytes(canonical_json_bytes({k: v for k, v in b3_cpanel.items() if k != "panel_receipt_sha256"})) != str(psha_b3)
or str(b3_cpanel.get("lot_id", "")).strip() != lot_s
or (blank_veh_40 is not None and str(b3_cpanel.get("matched_blank_vehicle_lot_id", "")).strip() != blank_veh_40)
or (b2be_ev is not None and str(b3_cpanel.get("b2b_exact_receipt_sha256", "")) != str(b2be_ev.get("receipt_sha256", "")))
or t_pexp is None or t_pass is None
or (t_pass - t_pexp) < timedelta(hours=24)
or (t_b2be_end_c is not None and t_pexp < t_b2be_end_c)
or (t_b3_end_c is not None and t_pass > t_b3_end_c)
or not isinstance(chals, Mapping)
):
fails.append("FAIL_GATE_B3_CYTOKINE_CHALLENGE_SHIELD_PANEL_INVALID"); b3_ok = False
elif set(chals.keys()) != b3_req_challenges:
holds.append("HOLD_GATE_B3_INCOMPLETE_REQUIRED_CYTOKINE_CHALLENGES"); b3_ok = False
elif not (
is_finite_num(mfp) and is_finite_num(mfb) and float(mfp) > 0.0 and float(mfb) >= 0.0
and (b2be_p_lo is None or float(mfp) >= b2be_p_lo - 1e-9)
and (b2be_j_hi is None or float(mfp) + float(mfb) <= b2be_j_hi + 1e-9)
and (csafe_op_val is None or float(mfp) + float(mfb) < csafe_op_val)
):
fails.append("FAIL_GATE_B3_CYTOKINE_CHALLENGE_EXPOSURE_UNLINKED_TO_B2B_EXACT"); b3_ok = False
else:
chal_req_k = frozenset({"block_ids", "matched_vehicle_record_id", "cassette_retention_raw", "components"})
min_cas_across = float("inf")
min_comp_across: dict[str, float] = {cn: float("inf") for cn in req_comp}
cpanel_struct_ok = True
for chal_nm in sorted(b3_req_challenges):
crow = chals[chal_nm]
if not isinstance(crow, Mapping) or set(crow.keys()) != chal_req_k or not is_nonempty_str(crow.get("matched_vehicle_record_id")):
fails.append(f"FAIL_GATE_B3_CYTOKINE_CHALLENGE_MALFORMED:{chal_nm}"); b3_ok = False; cpanel_struct_ok = False; break
bids_c = crow.get("block_ids")
cas_raw = crow.get("cassette_retention_raw")
comps_c = crow.get("components")
if not (
isinstance(bids_c, list) and len(bids_c) >= 3 and all(is_nonempty_str(b) for b in bids_c)
and len({str(b).strip().upper() for b in bids_c}) == len(bids_c)
and isinstance(cas_raw, Mapping) and set(cas_raw.keys()) == {"treated", "vehicle"}
and isinstance(cas_raw["treated"], list) and len(cas_raw["treated"]) == len(bids_c)
and isinstance(cas_raw["vehicle"], list) and len(cas_raw["vehicle"]) == len(bids_c)
and all(is_finite_num(x) and float(x) >= 0.0 for x in cas_raw["treated"])
and all(is_finite_num(x) and float(x) > 0.0 for x in cas_raw["vehicle"])
and isinstance(comps_c, Mapping) and set(comps_c.keys()) == req_comp
):
fails.append(f"FAIL_GATE_B3_CYTOKINE_CHALLENGE_MALFORMED:{chal_nm}"); b3_ok = False; cpanel_struct_ok = False; break
cas_pcts = [100.0 * float(cas_raw["treated"][bi]) / float(cas_raw["vehicle"][bi]) for bi in range(len(bids_c))]
cas_m = sum(cas_pcts) / len(cas_pcts)
min_cas_across = min(min_cas_across, cas_m)
if cas_m < 85.0:
fails.append(f"FAIL_GATE_B3_CHALLENGE_CASSETTE_BELOW_85PCT:{chal_nm}"); b3_ok = False
for cname in sorted(req_comp):
cm_raw = comps_c[cname]
if not (
isinstance(cm_raw, Mapping) and set(cm_raw.keys()) == {"treated", "vehicle"}
and isinstance(cm_raw["treated"], list) and len(cm_raw["treated"]) == len(bids_c)
and isinstance(cm_raw["vehicle"], list) and len(cm_raw["vehicle"]) == len(bids_c)
and all(is_finite_num(x) and float(x) >= 0.0 for x in cm_raw["treated"])
and all(is_finite_num(x) and float(x) > 0.0 for x in cm_raw["vehicle"])
):
fails.append(f"FAIL_GATE_B3_CYTOKINE_CHALLENGE_MALFORMED:{chal_nm}:{cname}"); b3_ok = False; cpanel_struct_ok = False; break
c_pcts = [100.0 * float(cm_raw["treated"][bi]) / float(cm_raw["vehicle"][bi]) for bi in range(len(bids_c))]
c_m = sum(c_pcts) / len(c_pcts)
min_comp_across[cname] = min(min_comp_across[cname], c_m)
if c_m < 85.0:
fails.append(f"FAIL_GATE_B3_CHALLENGE_SHIELD_COMPONENT_BELOW_85PCT:{chal_nm}:{cname}"); b3_ok = False
if not cpanel_struct_ok:
break
if cpanel_struct_ok:
if is_finite_num(cas) and float(cas) > min_cas_across + 0.5:
fails.append("FAIL_GATE_B3_SUMMARY_OVERSTATES_CHALLENGE_MIN"); b3_ok = False
if isinstance(smap, Mapping):
for cname in sorted(req_comp):
if is_finite_num(smap.get(cname)) and float(smap[cname]) > min_comp_across[cname] + 0.5:
fails.append(f"FAIL_GATE_B3_SUMMARY_OVERSTATES_CHALLENGE_MIN:{cname}"); b3_ok = False
if not (is_finite_num(il10) and 0.0 <= float(il10) < 20.0):
fails.append("FAIL_GATE_B3_IL10_SUPPRESSION_GE_20PCT"); b3_ok = False
if not (is_finite_num(il10_rec) and 90.0 <= float(il10_rec) <= 120.0):
fails.append("FAIL_GATE_B3_POST_WASHOUT_72H_IL10_RECOVERY_LT_90PCT"); b3_ok = False
il10_assay = b3_ev.get("il10_washout_recovery_assay")
il10_assay_keys = frozenset({
"assay_id", "lot_id", "matched_blank_vehicle_lot_id",
"exposure_started_at_utc", "washout_started_at_utc",
"recovery_collected_at_utc", "recorded_at_utc",
"exposure_measured_free_parent_nM",
"donor_records", "assay_receipt_sha256",
})
if il10_assay is None:
holds.append("HOLD_GATE_B3_IL10_WASHOUT_RECOVERY_ASSAY_MISSING"); b3_ok = False
elif isinstance(il10_assay, Mapping) and il10_assay.get("exposure_measured_free_parent_nM") is None:
holds.append("HOLD_GATE_B3_IL10_WASHOUT_MISSING_MEASURED_EXPOSURE"); b3_ok = False
elif not isinstance(il10_assay, Mapping) or set(il10_assay.keys()) != il10_assay_keys:
fails.append("FAIL_GATE_B3_IL10_WASHOUT_RECOVERY_ASSAY_INVALID"); b3_ok = False
else:
isha = il10_assay.get("assay_receipt_sha256")
t_iexp = parse_utc_iso_ts(il10_assay.get("exposure_started_at_utc"))
t_iwash = parse_utc_iso_ts(il10_assay.get("washout_started_at_utc"))
t_irecov = parse_utc_iso_ts(il10_assay.get("recovery_collected_at_utc"))
t_irec = parse_utc_iso_ts(il10_assay.get("recorded_at_utc"))
t_b3_r = parse_utc_iso_ts(b3_ev.get("recorded_at_utc"))
t_b2be_r = parse_utc_iso_ts(b2be_ev.get("recorded_at_utc")) if b2be_ev is not None else None
il10_donors = il10_assay.get("donor_records")
if (
not is_valid_sha256_hex(isha)
or sha256_bytes(canonical_json_bytes({k: v for k, v in il10_assay.items() if k != "assay_receipt_sha256"})) != str(isha)