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…
or not et40 or not et100 or et40 == et100
or et40 in seen_conf_trace_ids or et100 in seen_conf_trace_ids
or et40 in seen_e_traces or et100 in seen_e_traces
or t_eh is None or (t_hv is not None and t_eh >= t_hv)
or not (is_finite_num(ez40) and float(ez40) > 0.0 and is_finite_num(ez100) and float(ez100) > 0.0)
):
fails.append(f"FAIL_{prefix}_READOUT2_RULE_S8_EARLIER_WINDOW_TIMING_OR_TRACE_INVALID:{idx_e}"); ok = False; continue
seen_e_b40.add(eb40); seen_e_b100.add(eb100)
seen_e_traces.add(et40); seen_e_traces.add(et100)
if eb40 not in s8_comp_by_o2_block["O2_40"] or abs(float(ez40) - s8_comp_by_o2_block["O2_40"][eb40]) > 1e-6:
fails.append(f"FAIL_{prefix}_READOUT2_RULE_S8_EARLIER_Z2_40_MISMATCH:{idx_e}"); ok = False
if eb100 not in s8_comp_by_o2_block["O2_100"] or abs(float(ez100) - s8_comp_by_o2_block["O2_100"][eb100]) > 1e-6:
fails.append(f"FAIL_{prefix}_READOUT2_RULE_S8_EARLIER_Z2_100_MISMATCH:{idx_e}"); ok = False
if isinstance(o2_40, Mapping) and isinstance(o2_100, Mapping) and o2_40.get("blocks") == o2_100.get("blocks"):
fails.append(f"FAIL_{prefix}_READOUT2_IDENTICAL_COPY_PASTE_40_AND_100MMHG"); ok = False
if require_r2_spec_prov:
r2_prov = r2.get("readout2_specimen_provenance")
r2_prov_keys = frozenset({
"specimen_id", "specimen_lot_id", "intervention_cohort_id", "candidate_gene",
"registered_aliquot_ids", "trace_aliquot_links", "provenance_sha256",
})
if r2_prov is None:
holds.append(f"HOLD_{prefix}_READOUT2_MISSING_INTERVENTION_SPECIMEN_LINKAGE"); ok = False
elif not isinstance(r2_prov, Mapping) or set(r2_prov.keys()) != r2_prov_keys:
fails.append(f"FAIL_{prefix}_READOUT2_INTERVENTION_SPECIMEN_OR_TRACE_LINK_MISMATCH"); ok = False
else:
r2_psha = r2_prov.get("provenance_sha256")
r2_sp = r2_prov.get("specimen_id")
r2_lt = r2_prov.get("specimen_lot_id")
r2_co = r2_prov.get("intervention_cohort_id")
r2_cg = r2_prov.get("candidate_gene")
r2_al = r2_prov.get("registered_aliquot_ids")
r2_tl = r2_prov.get("trace_aliquot_links")
r2_prov_ok = True
if (
not is_valid_sha256_hex(r2_psha)
or sha256_bytes(canonical_json_bytes({k: v for k, v in r2_prov.items() if k != "provenance_sha256"})) != str(r2_psha)
or not all(is_nonempty_str(x) for x in (r2_sp, r2_lt, r2_co, r2_cg))
or not (isinstance(r2_al, list) and len(r2_al) >= 1 and all(is_nonempty_str(a) for a in r2_al))
or not (isinstance(r2_tl, list) and len(r2_tl) >= 1)
):
r2_prov_ok = False
else:
r2_sp_n = str(r2_sp).strip().upper()
r2_lt_n = str(r2_lt).strip().upper()
r2_co_n = str(r2_co).strip().upper()
r2_cg_n = str(r2_cg).strip().upper()
al_norms_r2 = [str(a).strip().upper() for a in r2_al]
if (
len(set(al_norms_r2)) != len(al_norms_r2)
or (is_nonempty_str(expected_specimen_id) and r2_sp_n != str(expected_specimen_id).strip().upper())
or (is_nonempty_str(expected_lot) and r2_lt_n != str(expected_lot).strip().upper())
or (is_nonempty_str(expected_cohort_id) and r2_co_n != str(expected_cohort_id).strip().upper())
or (is_nonempty_str(expected_candidate_gene) and r2_cg_n != str(expected_candidate_gene).strip().upper())
or any(not (an == r2_sp_n or an.startswith(f"{r2_sp_n}-")) for an in al_norms_r2)
):
r2_prov_ok = False
else:
reg_al_set = set(al_norms_r2) | {r2_sp_n}
seen_link_tids: set[str] = set()
ew_s = expected_engagement_window_utc[0] if expected_engagement_window_utc is not None else None
ew_e = expected_engagement_window_utc[1] if expected_engagement_window_utc is not None else None
if ew_s is not None and ew_e is not None and ew_s > ew_e:
r2_prov_ok = False
for tl_row in r2_tl:
if not isinstance(tl_row, Mapping) or set(tl_row.keys()) != {"trace_id", "aliquot_id", "collected_at_utc"}:
r2_prov_ok = False; break
ltid = tl_row.get("trace_id")
laid = tl_row.get("aliquot_id")
t_col = parse_utc_iso_ts(tl_row.get("collected_at_utc"))
if not (is_nonempty_str(ltid) and is_nonempty_str(laid)) or t_col is None:
r2_prov_ok = False; break
ltid_n = str(ltid).strip().upper()
laid_n = str(laid).strip().upper()
if ltid_n in seen_link_tids or laid_n not in reg_al_set:
r2_prov_ok = False; break
if (ew_s is not None and t_col < ew_s) or (ew_e is not None and t_col > ew_e):
r2_prov_ok = False; break
seen_link_tids.add(ltid_n)
if r2_prov_ok and seen_conf_trace_ids and seen_link_tids != seen_conf_trace_ids:
r2_prov_ok = False
if not r2_prov_ok:
fails.append(f"FAIL_{prefix}_READOUT2_INTERVENTION_SPECIMEN_OR_TRACE_LINK_MISMATCH"); ok = False
inlet = r2.get("gate_resp1_inlet_po2_mmhg")
r_flow = r2.get("gate_resp1_flow_ul_min")
r_ieq = r2.get("gate_resp1_dose_ieq")
r_ocr_pmol = r2.get("gate_resp1_ocr_pmol_min_ieq")
r_fd_sha = r2.get("gate_resp1_flow_dose_record_sha256")
snr = r2.get("gate_resp1_snr")
drop = r2.get("gate_resp1_po2_drop_mmhg")
drift = r2.get("gate_resp1_blank_drift_mmhg")
dna_r = r2.get("gate_resp1_dna_recovery_pct")
ocr_r = r2.get("gate_resp1_ocr_ratio")
ocr_cv = r2.get("gate_resp1_ocr_cv_pct")
ocr_n = r2.get("gate_resp1_replicates_n")
def _ocr_ratio_u95(ratio_v: float, cv_pct_v: float, n_rep_v: int) -> float:
# Live wetlab_contract.py:970,3541-3546,4508 Student-t one-sided 95% upper bound with SE >= 0.015 floor!
se_v = max(0.015, (ratio_v * (cv_pct_v / 100.0)) / math.sqrt(float(n_rep_v)))
return ratio_v + student_t_one_sided_crit(0.05, float(n_rep_v - 1)) * se_v
if not (
is_finite_num(inlet) and 34.0 <= float(inlet) <= 37.5
and is_finite_num(r_flow) and 100.0 <= float(r_flow) <= 400.0
and is_finite_num(r_ieq) and 100.0 <= float(r_ieq) <= 500.0
and is_finite_num(r_ocr_pmol) and 0.0 < float(r_ocr_pmol) <= 4.5479
and is_valid_sha256_hex(r_fd_sha)
and is_finite_num(snr) and float(snr) >= 5.0
and is_finite_num(drop) and float(drop) >= 1.0
and is_finite_num(drift) and 0.0 <= float(drift) <= 0.5
and is_finite_num(dna_r) and 85.0 <= float(dna_r) <= 120.0
and is_finite_num(ocr_r) and 0.0 < float(ocr_r) <= 1.097
and is_finite_num(ocr_cv) and 0.0 <= float(ocr_cv) <= 3.0
and is_strict_int(ocr_n) and int(ocr_n) >= 6
):
fails.append(f"FAIL_{prefix}_READOUT2_GATE_RESP1_OCR_FAILED"); ok = False
else:
top_ocr_u95 = _ocr_ratio_u95(float(ocr_r), float(ocr_cv), int(ocr_n))
if top_ocr_u95 > 1.50:
fails.append(f"FAIL_{prefix}_READOUT2_LOT_OCR_UPPER95_EXCEEDS_1P50X_CEILING"); ok = False
elif top_ocr_u95 > 1.097:
holds.append(f"HOLD_{prefix}_READOUT2_LOT_OCR_UPPER95_EXCEEDS_1P097_REQUIRES_S4_DERATING"); ok = False
# Live Playbook:126 dual specimen-keyed OCR qualification:
# whole-preparation OCR <= 4.5479 AND pure-endocrine OCR <= 3.9547 pmol/(min*IEQ),
# both bound to tested lot_id + exposure_id with distinct specimen_ids & content hashes!
ocr_rec_keys = frozenset({
"specimen_id", "lot_id", "exposure_id",
"inlet_po2_mmhg", "flow_ul_min", "dose_ieq",
"ocr_pmol_min_ieq", "ocr_ratio", "ocr_cv_pct", "replicates_n", "record_sha256",
})
wp_rec = r2.get("gate_resp1_whole_prep_ocr_record")
pe_rec = r2.get("gate_resp1_pure_endocrine_ocr_record")
wp_spec_id: Optional[str] = None
wp_exp_id: Optional[str] = None
if wp_rec is None:
holds.append(f"HOLD_{prefix}_READOUT2_WHOLE_PREP_OCR_RECORD_MISSING"); ok = False
elif not isinstance(wp_rec, Mapping) or set(wp_rec.keys()) != ocr_rec_keys:
fails.append(f"FAIL_{prefix}_READOUT2_WHOLE_PREP_OCR_RECORD_MALFORMED"); ok = False
else:
wsha = wp_rec.get("record_sha256")
if not is_valid_sha256_hex(wsha) or sha256_bytes(canonical_json_bytes({k: v for k, v in wp_rec.items() if k != "record_sha256"})) != wsha:
fails.append(f"FAIL_{prefix}_READOUT2_WHOLE_PREP_OCR_HASH_INVALID"); ok = False
if not (is_nonempty_str(wp_rec.get("specimen_id")) and is_nonempty_str(wp_rec.get("lot_id")) and is_nonempty_str(wp_rec.get("exposure_id"))):
fails.append(f"FAIL_{prefix}_READOUT2_WHOLE_PREP_OCR_TRACEABILITY_MISSING"); ok = False
else:
wp_spec_id = str(wp_rec["specimen_id"]).strip().upper()
wp_exp_id = str(wp_rec["exposure_id"]).strip().upper()
if expected_lot is not None and str(wp_rec["lot_id"]).strip() != expected_lot:
fails.append(f"FAIL_{prefix}_READOUT2_WHOLE_PREP_OCR_LOT_MISMATCH"); ok = False
w_ocr = wp_rec.get("ocr_pmol_min_ieq")
if not (is_finite_num(w_ocr) and 0.0 < float(w_ocr) <= 4.5479):
fails.append(f"FAIL_{prefix}_READOUT2_WHOLE_PREP_OCR_EXCEEDS_4P5479"); ok = False
elif is_finite_num(r_ocr_pmol) and abs(float(w_ocr) - float(r_ocr_pmol)) > 1e-4:
fails.append(f"FAIL_{prefix}_READOUT2_WHOLE_PREP_OCR_SCALAR_MISMATCH"); ok = False
if not (
is_finite_num(wp_rec.get("inlet_po2_mmhg")) and 34.0 <= float(wp_rec["inlet_po2_mmhg"]) <= 37.5
and is_finite_num(wp_rec.get("flow_ul_min")) and 100.0 <= float(wp_rec["flow_ul_min"]) <= 400.0
and is_finite_num(wp_rec.get("dose_ieq")) and 100.0 <= float(wp_rec["dose_ieq"]) <= 500.0
and is_finite_num(wp_rec.get("ocr_ratio")) and 0.0 < float(wp_rec["ocr_ratio"]) <= 1.097
and is_finite_num(wp_rec.get("ocr_cv_pct")) and 0.0 <= float(wp_rec["ocr_cv_pct"]) <= 3.0
and is_strict_int(wp_rec.get("replicates_n")) and int(wp_rec["replicates_n"]) >= 6
):
fails.append(f"FAIL_{prefix}_READOUT2_WHOLE_PREP_OCR_ASSAY_QC_FAILED"); ok = False
else:
wp_u95 = _ocr_ratio_u95(float(wp_rec["ocr_ratio"]), float(wp_rec["ocr_cv_pct"]), int(wp_rec["replicates_n"]))
if wp_u95 > 1.50:
fails.append(f"FAIL_{prefix}_READOUT2_WHOLE_PREP_OCR_UPPER95_EXCEEDS_1P50X_CEILING"); ok = False
elif wp_u95 > 1.097:
holds.append(f"HOLD_{prefix}_READOUT2_WHOLE_PREP_OCR_UPPER95_EXCEEDS_1P097_REQUIRES_S4_DERATING"); ok = False
if pe_rec is None:
holds.append(f"HOLD_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_MISSING"); ok = False
elif not isinstance(pe_rec, Mapping) or set(pe_rec.keys()) != ocr_rec_keys:
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_RECORD_MALFORMED"); ok = False
else:
psha = pe_rec.get("record_sha256")
if not is_valid_sha256_hex(psha) or sha256_bytes(canonical_json_bytes({k: v for k, v in pe_rec.items() if k != "record_sha256"})) != psha:
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_HASH_INVALID"); ok = False
if not (is_nonempty_str(pe_rec.get("specimen_id")) and is_nonempty_str(pe_rec.get("lot_id")) and is_nonempty_str(pe_rec.get("exposure_id"))):
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_TRACEABILITY_MISSING"); ok = False
else:
pe_spec_id = str(pe_rec["specimen_id"]).strip().upper()
pe_exp_id = str(pe_rec["exposure_id"]).strip().upper()
if expected_lot is not None and str(pe_rec["lot_id"]).strip() != expected_lot:
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_LOT_MISMATCH"); ok = False
if wp_spec_id is not None and pe_spec_id == wp_spec_id:
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_SPECIMEN_IDENTICAL_TO_WHOLE_PREP"); ok = False
if wp_exp_id is not None and pe_exp_id != wp_exp_id:
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_EXPOSURE_MISMATCH"); ok = False
p_ocr = pe_rec.get("ocr_pmol_min_ieq")
if not (is_finite_num(p_ocr) and 0.0 < float(p_ocr) <= 3.9547):
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_EXCEEDS_3P9547"); ok = False
if not (
is_finite_num(pe_rec.get("inlet_po2_mmhg")) and 34.0 <= float(pe_rec["inlet_po2_mmhg"]) <= 37.5
and is_finite_num(pe_rec.get("flow_ul_min")) and 100.0 <= float(pe_rec["flow_ul_min"]) <= 400.0
and is_finite_num(pe_rec.get("dose_ieq")) and 100.0 <= float(pe_rec["dose_ieq"]) <= 500.0
and is_finite_num(pe_rec.get("ocr_ratio")) and 0.0 < float(pe_rec["ocr_ratio"]) <= 1.097
and is_finite_num(pe_rec.get("ocr_cv_pct")) and 0.0 <= float(pe_rec["ocr_cv_pct"]) <= 3.0
and is_strict_int(pe_rec.get("replicates_n")) and int(pe_rec["replicates_n"]) >= 6
):
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_ASSAY_QC_FAILED"); ok = False
else:
pe_u95 = _ocr_ratio_u95(float(pe_rec["ocr_ratio"]), float(pe_rec["ocr_cv_pct"]), int(pe_rec["replicates_n"]))
if pe_u95 > 1.50:
fails.append(f"FAIL_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_UPPER95_EXCEEDS_1P50X_CEILING"); ok = False
elif pe_u95 > 1.097:
holds.append(f"HOLD_{prefix}_READOUT2_PURE_ENDOCRINE_OCR_UPPER95_EXCEEDS_1P097_REQUIRES_S4_DERATING"); ok = False
# Live Playbook:126 cluster-surface oxygen (pO2 >= 15.0 mmHg, f_GSIS >= 0.858) &
# post-encapsulation outer-band (z in [50, 175] um) confocal morphometry (D_p90 <= 100 um, sphericity >= 0.78)!
morph_keys = frozenset({
"lot_id", "cluster_surface_po2_mmhg", "cluster_surface_f_gsis",
"outer_band_z_min_um", "outer_band_z_max_um",
"outer_band_d_p90_um", "outer_band_sphericity_3d", "record_sha256",
})
m_rec = r2.get("cluster_surface_o2_and_morphometry_record")
if m_rec is None:
holds.append(f"HOLD_{prefix}_READOUT2_MISSING_CLUSTER_SURFACE_O2_MORPHOMETRY"); ok = False
elif not isinstance(m_rec, Mapping) or set(m_rec.keys()) != morph_keys:
fails.append(f"FAIL_{prefix}_READOUT2_CLUSTER_SURFACE_O2_MORPHOMETRY_MALFORMED"); ok = False
else:
msha = m_rec.get("record_sha256")
if not is_valid_sha256_hex(msha) or sha256_bytes(canonical_json_bytes({k: v for k, v in m_rec.items() if k != "record_sha256"})) != msha:
fails.append(f"FAIL_{prefix}_READOUT2_CLUSTER_SURFACE_O2_MORPHOMETRY_HASH_INVALID"); ok = False
if expected_lot is not None and str(m_rec.get("lot_id", "")).strip() != expected_lot:
fails.append(f"FAIL_{prefix}_READOUT2_CLUSTER_SURFACE_O2_MORPHOMETRY_LOT_MISMATCH"); ok = False
if not (
is_finite_num(m_rec.get("cluster_surface_po2_mmhg")) and float(m_rec["cluster_surface_po2_mmhg"]) >= 15.0
and is_finite_num(m_rec.get("cluster_surface_f_gsis")) and 0.858 <= float(m_rec["cluster_surface_f_gsis"]) <= 1.0
and is_finite_num(m_rec.get("outer_band_z_min_um")) and abs(float(m_rec["outer_band_z_min_um"]) - 50.0) <= 1e-6
and is_finite_num(m_rec.get("outer_band_z_max_um")) and abs(float(m_rec["outer_band_z_max_um"]) - 175.0) <= 1e-6
and is_finite_num(m_rec.get("outer_band_d_p90_um")) and 0.0 < float(m_rec["outer_band_d_p90_um"]) <= 100.0
and is_finite_num(m_rec.get("outer_band_sphericity_3d")) and 0.78 <= float(m_rec["outer_band_sphericity_3d"]) <= 1.0
):
fails.append(f"FAIL_{prefix}_READOUT2_CLUSTER_SURFACE_O2_OR_MORPHOMETRY_FAILED"); ok = False
if not _validate_dec134_washout_trace(
r2.get("dec134_washout_time_min_series"),
r2.get("dec134_washout_remaining_peak_pct_series"),
r2.get("dec134_washout_raw_trace_sha256"),
r2.get("dec134_washout_trace_span_min"),
r2.get("dec134_washout_tail_remaining_pct"),
r2.get("dec134_washout_mass_recovery_pct"),
r2.get("dec134_washout_t50_min"),
r2.get("dec134_washout_mean_transit_min"),
r2.get("dec134_washout_t90_min"),
):