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…
if not (is_strict_bool(r2_ev.get("ki67_edu_referred")) and r2_ev["ki67_edu_referred"]):
fails.append(f"FAIL_CHECK_D2_UNREFERRED_PROLIFERATION_ELEVATION:{lid}")
else:
holds.append(f"HOLD_CHECK_D2_PROLIFERATION_REFERRED:{lid}")
else:
fails.append("FAIL_CHECK_D2_INVALID_STATUS")
# Check D-3(a/b): exact verbatim statuses!
if not (is_strict_bool(rec.get("check_d3_c3_explicitly_untested")) and rec["check_d3_c3_explicitly_untested"]):
fails.append("FAIL_CHECK_D3_C3_MUST_REMAIN_UNTESTED")
d3a_status = rec.get("check_d3a_status")
d3a_verified_hashes: list[str] = []
d3a_max_recorded_ts: Optional[datetime] = None
if d3a_status == "NOT_ATTEMPTED":
_check_forbidden_keys(rec, D3A_EVAL_KEYS, fails, "FAIL_CHECK_D3A_NOT_ATTEMPTED_FORBIDS_EVAL_FIELDS")
elif d3a_status == "EVALUATED":
d3a_lots = rec.get("check_d3a_per_lot_records")
if not isinstance(d3a_lots, list) or len(d3a_lots) < 3:
fails.append("FAIL_CHECK_D3A_REQUIRES_AT_LEAST_3_INDEPENDENT_LOT_RECORDS")
else:
seen_3a: set[str] = set()
seen_3a_nt: set[str] = set()
seen_3a_aid: set[str] = set()
seen_3a_blood_donors: set[str] = set()
seen_3a_blood_splits: set[str] = set()
seen_3a_source_preps: set[str] = set()
seen_3a_meas_sigs: set[tuple[Any, ...]] = set()
seen_3a_hashes: set[str] = set()
seen_3a_runs: set[str] = set()
seen_3a_elisa_rows: set[tuple[str, str]] = set()
seen_3a_inflam: set[str] = set()
d3a_lot_allowed = frozenset({
"receipt_sha256", "source_iec_preparation_id", "lot_id", "comparator_nt_lot_id", "assay_record_id",
"paired_assay_run_id", "raw_elisa_record_sha256", "raw_elisa_well_or_row_id", "inflammatory_challenge_condition",
"blood_donor_id", "matched_blood_split_id",
"assay_acquired_at_utc", "recorded_at_utc",
"iec_ptprc_pct", "cd31_pecam1_pct", "plvap_pos_pct", "cdh5_ve_cadherin_pct",
"f3_kd_pct", "cxcl10_retention_pct", "cd274_retention_pct",
"cleaved_casp3_fold",
"pos_ctrl_tat_fold", "neg_ctrl_tat_fold",
"tat_assay_lloq_ng_ml", "tat_f3_below_lloq",
"chandler_tat_nt_ctrl_ng_ml", "chandler_tat_f3_sirna_ng_ml",
})
for idx, lrec in enumerate(d3a_lots):
if not isinstance(lrec, Mapping):
fails.append(f"FAIL_CHECK_D3A_MALFORMED_LOT:{idx}"); continue
_check_allowed_keys(lrec, d3a_lot_allowed, fails, f"CHECK_D3A_LOT_{idx}")
raw_lid = lrec.get("lot_id")
raw_nt = lrec.get("comparator_nt_lot_id")
raw_aid = lrec.get("assay_record_id")
if not is_nonempty_str(raw_lid) or not is_nonempty_str(raw_nt):
fails.append("FAIL_CHECK_D3A_DUPLICATE_OR_EMPTY_LOT_ID"); continue
lid = str(raw_lid).strip()
nt_l = str(raw_nt).strip()
if lid.upper() in seen_3a or lid.upper() == nt_l.upper():
fails.append("FAIL_CHECK_D3A_DUPLICATE_OR_EMPTY_LOT_ID"); continue
if nt_l.upper() in seen_3a_nt:
fails.append(f"FAIL_CHECK_D3A_REUSED_COMPARATOR_NT_LOT_ACROSS_LOTS:{lid}")
if not is_nonempty_str(raw_aid) or str(raw_aid).strip().upper() in seen_3a_aid:
fails.append(f"FAIL_CHECK_D3A_DUPLICATE_OR_MISSING_ASSAY_RECORD_ID:{lid}")
else:
seen_3a_aid.add(str(raw_aid).strip().upper())
raw_prep = lrec.get("source_iec_preparation_id")
if not is_nonempty_str(raw_prep):
fails.append(f"FAIL_CHECK_D3A_DUPLICATE_OR_MISSING_SOURCE_IEC_PREP_ID:{lid}")
else:
prep_u = str(raw_prep).strip().upper()
if prep_u in seen_3a_source_preps or prep_u == lid.upper() or prep_u == nt_l.upper():
fails.append(f"FAIL_CHECK_D3A_DUPLICATE_OR_MISSING_SOURCE_IEC_PREP_ID:{lid}")
else:
seen_3a_source_preps.add(prep_u)
raw_bd = lrec.get("blood_donor_id")
raw_bs = lrec.get("matched_blood_split_id")
if not is_nonempty_str(raw_bd):
fails.append(f"FAIL_CHECK_D3A_MISSING_BLOOD_DONOR_ID:{lid}")
else:
seen_3a_blood_donors.add(str(raw_bd).strip().upper())
if not is_nonempty_str(raw_bs) or str(raw_bs).strip().upper() in seen_3a_blood_splits:
fails.append(f"FAIL_CHECK_D3A_DUPLICATE_OR_MISSING_BLOOD_SPLIT_ID:{lid}")
else:
seen_3a_blood_splits.add(str(raw_bs).strip().upper())
seen_3a.add(lid.upper())
seen_3a_nt.add(nt_l.upper())
if not seen_3a.isdisjoint(seen_3a_nt):
fails.append(f"FAIL_CHECK_D3A_TREATED_COMPARATOR_LOT_ID_OVERLAP_OR_CYCLE:{lid}")
rsha = lrec.get("receipt_sha256")
if not is_valid_sha256_hex(rsha) or compute_receipt_sha256(lrec) != rsha or rsha in seen_3a_hashes:
fails.append(f"FAIL_CHECK_D3A_LOT_CONTENT_HASH_INVALID:{lid}")
else:
seen_3a_hashes.add(rsha)
d3a_verified_hashes.append(str(rsha))
t_3a_acq = parse_utc_iso_ts(lrec.get("assay_acquired_at_utc"))
t_3a_rec = parse_utc_iso_ts(lrec.get("recorded_at_utc"))
if t_3a_acq is None or t_3a_rec is None or t_3a_acq > t_3a_rec:
fails.append(f"FAIL_CHECK_D3A_INVALID_OR_MISSING_TIMESTAMPS:{lid}")
elif d3a_max_recorded_ts is None or t_3a_rec > d3a_max_recorded_ts:
d3a_max_recorded_ts = t_3a_rec
ptprc = lrec.get("iec_ptprc_pct")
cd31 = lrec.get("cd31_pecam1_pct")
plv = lrec.get("plvap_pos_pct")
cdh5 = lrec.get("cdh5_ve_cadherin_pct")
kd = lrec.get("f3_kd_pct")
cx = lrec.get("cxcl10_retention_pct")
cd = lrec.get("cd274_retention_pct")
c3_f = lrec.get("cleaved_casp3_fold")
pct = lrec.get("pos_ctrl_tat_fold")
nct = lrec.get("neg_ctrl_tat_fold")
raw_run = lrec.get("paired_assay_run_id")
elisa_sha = lrec.get("raw_elisa_record_sha256")
elisa_row = lrec.get("raw_elisa_well_or_row_id")
inflam_c = lrec.get("inflammatory_challenge_condition")
if (
not is_nonempty_str(raw_run) or str(raw_run).strip().upper() in seen_3a_runs
or not is_valid_sha256_hex(elisa_sha) or not is_nonempty_str(elisa_row)
):
fails.append(f"FAIL_CHECK_D3A_PAIRED_ASSAY_PROVENANCE_INVALID:{lid}")
else:
seen_3a_runs.add(str(raw_run).strip().upper())
elisa_key = (str(elisa_sha).lower(), str(elisa_row).strip().upper())
if elisa_key in seen_3a_elisa_rows:
fails.append(f"FAIL_CHECK_D3A_OVERLAPPING_ELISA_SOURCE_ROW:{lid}")
else:
seen_3a_elisa_rows.add(elisa_key)
if inflam_c not in ("BASAL_UNSTIMULATED", "TNFA_IFNG_PRIMED"):
fails.append("FAIL_CHECK_D3A_INCONSISTENT_OR_INVALID_INFLAMMATORY_CONDITION")
else:
seen_3a_inflam.add(str(inflam_c))
tat_lloq = lrec.get("tat_assay_lloq_ng_ml")
tat_blq = lrec.get("tat_f3_below_lloq")
tat_nt = lrec.get("chandler_tat_nt_ctrl_ng_ml")
tat_f3 = lrec.get("chandler_tat_f3_sirna_ng_ml")
if not (is_finite_num(ptprc) and float(ptprc) == 0.0):
fails.append(f"FAIL_CHECK_D3A_NON_ZERO_PTPRC_CONTAMINATION:{lid}")
if not (is_finite_num(cd31) and 90.0 <= float(cd31) <= 100.0 and is_finite_num(plv) and 85.0 <= float(plv) <= 100.0 and is_finite_num(cdh5) and 90.0 <= float(cdh5) <= 100.0):
fails.append(f"FAIL_CHECK_D3A_POSITIVE_ENDOTHELIAL_IDENTITY_BELOW_FLOOR:{lid}")
if not (is_finite_num(kd) and 60.0 <= float(kd) <= 100.0):
fails.append(f"FAIL_CHECK_D3A_F3_KD_BELOW_60PCT:{lid}")
if not (is_finite_num(cx) and 85.0 <= float(cx) <= 200.0):
fails.append(f"FAIL_CHECK_D3A_CXCL10_RETENTION_BELOW_85PCT:{lid}")
if not (is_finite_num(cd) and 85.0 <= float(cd) <= 200.0):
fails.append(f"FAIL_CHECK_D3A_CD274_RETENTION_BELOW_85PCT:{lid}")
if not (is_finite_num(c3_f) and 0.0 < float(c3_f) <= 1.2):
fails.append(f"FAIL_CHECK_D3A_APOPTOSIS_INCREASE:{lid}")
if not (is_finite_num(pct) and float(pct) >= 3.0 and is_finite_num(nct) and 0.0 <= float(nct) <= 1.5):
fails.append(f"FAIL_CHECK_D3A_CHANDLER_CONTROLS_INVALID:{lid}")
if not (is_finite_num(tat_lloq) and 0.0 < float(tat_lloq) <= 10.0 and is_strict_bool(tat_blq) and "chandler_tat_f3_sirna_ng_ml" in lrec):
fails.append(f"FAIL_CHECK_D3A_TAT_LLOQ_OR_CENSORING_INVALID:{lid}")
elif not (is_finite_num(tat_nt) and float(tat_nt) > 0.0):
fails.append(f"FAIL_CHECK_D3A_NO_FUNCTIONAL_TAT_REDUCTION:{lid}")
elif float(tat_nt) < 3.0 * float(tat_lloq):
holds.append(f"HOLD_CHECK_D3A_TAT_COMPARATOR_NEAR_OR_BELOW_LLOQ:{lid}")
elif tat_blq is True:
if tat_f3 is not None:
fails.append(f"FAIL_CHECK_D3A_TAT_LLOQ_OR_CENSORING_INVALID:{lid}")
else:
eff_tat_f3 = float(tat_lloq)
red_pct = 100.0 * (float(tat_nt) - eff_tat_f3) / float(tat_nt)
if not (20.0 <= red_pct <= 100.0):
fails.append(f"FAIL_CHECK_D3A_NO_FUNCTIONAL_TAT_REDUCTION:{lid}")
msig = tuple(
round(float(x), 6) if is_finite_num(x) else None
for x in (cd31, plv, cdh5, kd, cx, cd, c3_f, pct, nct, tat_nt, eff_tat_f3)
)
if msig in seen_3a_meas_sigs:
fails.append(f"FAIL_CHECK_D3A_IDENTICAL_COPY_PASTE_ACROSS_LOTS:{lid}")
seen_3a_meas_sigs.add(msig)
else:
if not (is_finite_num(tat_f3) and float(tat_f3) >= 0.0):
fails.append(f"FAIL_CHECK_D3A_NO_FUNCTIONAL_TAT_REDUCTION:{lid}")
elif float(tat_f3) < float(tat_lloq):
fails.append(f"FAIL_CHECK_D3A_TAT_QUANTIFIED_BELOW_LLOQ:{lid}")
else:
eff_tat_f3 = float(tat_f3)
red_pct = 100.0 * (float(tat_nt) - eff_tat_f3) / float(tat_nt)
if not (20.0 <= red_pct <= 100.0):
fails.append(f"FAIL_CHECK_D3A_NO_FUNCTIONAL_TAT_REDUCTION:{lid}")
msig = tuple(
round(float(x), 6) if is_finite_num(x) else None
for x in (cd31, plv, cdh5, kd, cx, cd, c3_f, pct, nct, tat_nt, eff_tat_f3)
)
if msig in seen_3a_meas_sigs:
fails.append(f"FAIL_CHECK_D3A_IDENTICAL_COPY_PASTE_ACROSS_LOTS:{lid}")
seen_3a_meas_sigs.add(msig)
if len(seen_3a_inflam) > 1:
fails.append("FAIL_CHECK_D3A_INCONSISTENT_OR_INVALID_INFLAMMATORY_CONDITION")
if len(seen_3a_blood_donors) < 3:
fails.append("FAIL_CHECK_D3A_INSUFFICIENT_INDEPENDENT_BLOOD_DONORS")
else:
fails.append("FAIL_CHECK_D3A_INVALID_STATUS")
d3b_status = rec.get("check_d3b_status")
if d3b_status == "NOT_ATTEMPTED":
_check_forbidden_keys(rec, D3B_EVAL_KEYS, fails, "FAIL_CHECK_D3B_NOT_ATTEMPTED_FORBIDS_EVAL_FIELDS")
elif d3b_status == "EVALUATED":
d3b_scope = rec.get("d3b_assay_scope")
if d3b_scope not in ("IEC_ONLY_NON_ASSEMBLED", "ASSEMBLED_ROUTE_QUALIFICATION"):
fails.append("FAIL_CHECK_D3B_UNDECLARED_ASSAY_SCOPE")
need_g31_a = (d3b_scope == "ASSEMBLED_ROUTE_QUALIFICATION")
if need_g31_a and d3a_status != "EVALUATED":
fails.append("FAIL_CHECK_D3B_ASSEMBLED_REQUIRES_D3A_EVALUATED_FIRST")
d3b_raw_p = rec.get("d3b_pilot_receipt")
d3b_exp_nt = str(d3b_raw_p.get("nt_control_lot_id", "")).strip() if isinstance(d3b_raw_p, Mapping) and is_nonempty_str(d3b_raw_p.get("nt_control_lot_id")) else None
spec_3b, _, b_fails, b_holds, mat_3b, sw_3b, g0v_3b, g31_3b = _validate_canonical_binding_v6(
rec, allow_design_a=True, coating_gate=False, phase="EXACT_PRODUCT",
require_materialization=True, require_switches=True,
require_gate31_on_design_a_r1=need_g31_a,
expected_comparator_nt_lot_id=d3b_exp_nt,
)
fails.extend(b_fails)
holds.extend(b_holds)
prod_3b = str(rec.get("product_id", "")).strip()
bank_3b = str(rec.get("bank_id", "")).strip()
lot_3b = str(rec.get("lot_id", "")).strip()
route_3b = str(rec.get("route_id", "")).strip().upper()
d3b_prereqs: list[tuple[str, Optional[Mapping[str, Any]]]] = [
("BANK_MATERIALIZATION", mat_3b), ("NATIVE_SAFETY_SWITCH", sw_3b), ("GATE-0V-KILL", g0v_3b),
]
if g31_3b is not None:
d3b_prereqs.append(("GATE-31", g31_3b))
d3b_rcpt_extras = frozenset({
"d3b_assay_scope", "assay_acquired_at_utc", "f3_kd_pct_in_pilot",
"d3a_lot_receipt_sha256s",
"treated_lot_id", "nt_control_lot_id", "same_product_nt_sirna",
"parental_attribution_included",
"f3_sirna_shield_by_component", "nt_sirna_shield_by_component", "parental_shield_by_component",
"hour0_density_by_lineage",
})
d3b_ev = _validate_content_receipt(
rec.get("d3b_pilot_receipt"),
expected_stage="CHECK_D3B_PILOT", expected_prod=prod_3b, expected_bank=bank_3b,
expected_lot=lot_3b, expected_route=route_3b, expected_scope="exact_product",
expected_parent=mat_3b, min_prereq_receipts=d3b_prereqs,
allowed_extra_keys=d3b_rcpt_extras, fails=fails, holds=holds,
missing_hold="HOLD_CHECK_D3B_TIMED_PILOT_RECEIPT_MISSING",
fail_code="FAIL_CHECK_D3B_PILOT_RECEIPT_FAILED",
mismatch_code="FAIL_CHECK_D3B_PILOT_RECEIPT_SCOPE_MISMATCH",
)
if d3b_ev is not None:
if d3b_ev.get("d3b_assay_scope") != d3b_scope:
fails.append("FAIL_CHECK_D3B_ASSAY_SCOPE_NOT_BOUND_IN_RECEIPT_HASH")
f3_kd_3b = d3b_ev.get("f3_kd_pct_in_pilot")
if not (is_finite_num(f3_kd_3b) and 60.0 <= float(f3_kd_3b) <= 100.0):
fails.append("FAIL_CHECK_D3B_TARGET_ENGAGEMENT_F3_KD_BELOW_60PCT")
t_acq = parse_utc_iso_ts(d3b_ev.get("assay_acquired_at_utc"))
t_3b = parse_utc_iso_ts(d3b_ev.get("recorded_at_utc"))
if t_acq is None or (t_3b is not None and t_acq > t_3b):
fails.append("FAIL_CHECK_D3B_INVALID_ASSAY_ACQUISITION_TIMESTAMP")
else:
for plbl, prec in d3b_prereqs: