scripts/verify_iip0058_armz_ensc_isletsort_v2_and_2p24_portfolio.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0058_armz_ensc_isletsort_v2_and_2p24_portfolio.py · SHA-256: 33439d619c00fcc3…
arch_teratoma_conflict = False
arch_teratoma_incomplete = False
caller_teratoma_conflict = bool(
parent_roster_integrity_fail
or parent_roster_incomplete
or caller_gp2_id_list_fail
or caller_wk_res["hard_fail"]
or caller_dose_res["hard_fail"]
or caller_n_res["hard_fail"]
or caller_ev_res["hard_fail"]
or caller_host_res["hard_fail"]
)
if "teratoma_lot_id" in arch_gp2:
if arch_gp2["teratoma_lot_id"] is None:
holds.append("HOLD_GP2_ARCHIVE_MISSING_TERATOMA_LOT_ID")
arch_teratoma_incomplete = True
elif not _is_nonempty_str(arch_gp2["teratoma_lot_id"]) or (valid_lot is not None and arch_gp2["teratoma_lot_id"] != valid_lot):
failures.append("FAIL_GP2_ARCHIVE_TERATOMA_LOT_ID_MISMATCH")
arch_teratoma_conflict = True
if t_lot is None or not _is_nonempty_str(t_lot) or (valid_lot is not None and t_lot != valid_lot):
caller_teratoma_conflict = True
# Bind and directly audit ALL study-level teratoma aliases via common validators (Audit R12-05)
arch_dose_res = _audit_study_level_int_aliases(
arch_gp2,
role="ARCHIVE",
gate_tag="GP2",
field_tag="DOSE",
alias_keys=STUDY_TERATOMA_DOSE_ALIASES,
min_val=10_000_000,
allow_zero=False,
below_min_fail_code="FAIL_GP2_ARCHIVE_CHALLENGE_DOSE_LT_1E7",
missing_hold_code="HOLD_GP2_ARCHIVE_MISSING_CHALLENGE_DOSE_CELLS",
invalid_fail_code="FAIL_GP2_ARCHIVE_INVALID_CHALLENGE_DOSE_CELLS",
holds=holds,
failures=failures,
)
dc_int, dc_inc, dc_cal = _reconcile_study_level_int_archive_and_caller(
arch_res=arch_dose_res,
caller_res=caller_dose_res,
gate_tag="GP2",
field_tag="DOSE",
mismatch_fail_code="FAIL_GP2_ARCHIVE_CHALLENGE_DOSE_MISMATCH",
holds=holds,
failures=failures,
)
arch_teratoma_conflict = arch_teratoma_conflict or dc_int
arch_teratoma_incomplete = arch_teratoma_incomplete or dc_inc
caller_teratoma_conflict = caller_teratoma_conflict or dc_cal
arch_n_res = _audit_study_level_int_aliases(
arch_gp2,
role="ARCHIVE",
gate_tag="GP2",
field_tag="TERATOMA_N",
alias_keys=STUDY_TERATOMA_N_ALIASES,
min_val=12,
allow_zero=False,
below_min_fail_code="FAIL_GP2_ARCHIVE_TERATOMA_N_INVALID",
missing_hold_code="HOLD_GP2_ARCHIVE_MISSING_TERATOMA_N",
invalid_fail_code="FAIL_GP2_ARCHIVE_TERATOMA_N_INVALID",
holds=holds,
failures=failures,
)
nc_int, nc_inc, nc_cal = _reconcile_study_level_int_archive_and_caller(
arch_res=arch_n_res,
caller_res=caller_n_res,
gate_tag="GP2",
field_tag="TERATOMA_N",
mismatch_fail_code="FAIL_GP2_ARCHIVE_TERATOMA_N_MISMATCH",
holds=holds,
failures=failures,
)
arch_teratoma_conflict = arch_teratoma_conflict or nc_int
arch_teratoma_incomplete = arch_teratoma_incomplete or nc_inc
caller_teratoma_conflict = caller_teratoma_conflict or nc_cal
arch_n = arch_n_res["resolved_val"]
arch_ev_res = _audit_study_level_int_aliases(
arch_gp2,
role="ARCHIVE",
gate_tag="GP2",
field_tag="TERATOMA_EVENTS",
alias_keys=STUDY_TERATOMA_EVENTS_ALIASES,
min_val=0,
allow_zero=True,
nonzero_event_fail_code="FAIL_GP2_ARCHIVE_TERATOMA_DETECTED",
below_min_fail_code="FAIL_GP2_ARCHIVE_TERATOMA_EVENTS_INVALID",
missing_hold_code="HOLD_GP2_ARCHIVE_MISSING_TERATOMA_EVENTS",
invalid_fail_code="FAIL_GP2_ARCHIVE_TERATOMA_EVENTS_INVALID",
holds=holds,
failures=failures,
)
ec_int, ec_inc, ec_cal = _reconcile_study_level_int_archive_and_caller(
arch_res=arch_ev_res,
caller_res=caller_ev_res,
gate_tag="GP2",
field_tag="TERATOMA_EVENTS",
mismatch_fail_code="FAIL_GP2_ARCHIVE_TERATOMA_EVENTS_MISMATCH",
holds=holds,
failures=failures,
)
arch_teratoma_conflict = arch_teratoma_conflict or ec_int
arch_teratoma_incomplete = arch_teratoma_incomplete or ec_inc
caller_teratoma_conflict = caller_teratoma_conflict or ec_cal
arch_ev = arch_ev_res["resolved_val"]
if _is_strict_int(arch_n) and arch_n > 0 and _is_strict_int(arch_ev) and arch_ev > arch_n:
failures.append("FAIL_GP2_ARCHIVE_IMPOSSIBLE_TERATOMA_EVENTS_GT_N")
arch_teratoma_conflict = True
arch_host_res = _audit_study_level_host_aliases(
arch_gp2,
role="ARCHIVE",
gate_tag="GP2",
required_host="SCID-Beige",
alias_keys=STUDY_TERATOMA_HOST_ALIASES,
wrong_host_fail_code="FAIL_GP2_ARCHIVE_HOST_STRAIN_MISMATCH",
invalid_host_fail_code="FAIL_GP2_ARCHIVE_HOST_STRAIN_MISMATCH",
missing_hold_code="HOLD_GP2_ARCHIVE_MISSING_TUMOR_HOST_STRAIN",
holds=holds,
failures=failures,
)
hc_int, hc_inc, hc_cal = _reconcile_study_level_host_archive_and_caller(
arch_res=arch_host_res,
caller_res=caller_host_res,
gate_tag="GP2",
mismatch_fail_code="FAIL_GP2_ARCHIVE_HOST_STRAIN_MISMATCH",
holds=holds,
failures=failures,
)
arch_teratoma_conflict = arch_teratoma_conflict or hc_int
arch_teratoma_incomplete = arch_teratoma_incomplete or hc_inc
caller_teratoma_conflict = caller_teratoma_conflict or hc_cal
# Common study-level follow-up alias validator (Audit R12-05; shared identically by GP2 & EnSC!)
arch_wk_res = _audit_study_level_followup_aliases(
arch_gp2,
role="ARCHIVE",
gate_tag="GP2",
min_weeks=26.0,
alias_keys=STUDY_TERATOMA_FOLLOWUP_ALIASES,
holds=holds,
failures=failures,
)
wc_int, wc_inc, wc_cal = _reconcile_study_level_followup_archive_and_caller(
arch_res=arch_wk_res,
caller_res=caller_wk_res,
gate_tag="GP2",
holds=holds,
failures=failures,
)
arch_teratoma_conflict = arch_teratoma_conflict or wc_int
arch_teratoma_incomplete = arch_teratoma_incomplete or wc_inc
caller_teratoma_conflict = caller_teratoma_conflict or wc_cal
# Audit archived optional ID-list aliases strictly on their own
arch_gp2_id_lists: list[tuple[str, int, set[str], dict[str, str]]] = []
for opt_id_k in ("animal_ids", "teratoma_animal_ids"):
if opt_id_k in arch_gp2:
if arch_gp2[opt_id_k] is None:
arch_teratoma_incomplete = True
if "HOLD_INCOMPLETE_ARCHIVED_ANIMAL_ID_LIST_ALIAS_NULL" not in holds:
holds.append("HOLD_INCOMPLETE_ARCHIVED_ANIMAL_ID_LIST_ALIAS_NULL")
continue
aid_fails: list[str] = []
opt_an, opt_aset, opt_araw = _audit_canonical_id_list(
arch_gp2[opt_id_k], min_distinct=12, expected_count=arch_n, label_prefix="ARCHIVE_GP2_ANIMAL", failures=aid_fails,
)
for af in aid_fails:
if af == "FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID" and af in failures:
continue
failures.append(af)
if aid_fails:
arch_teratoma_conflict = True
if opt_aset is not None:
arch_gp2_id_lists.append((opt_id_k, opt_an, opt_aset, opt_araw))
if len(arch_gp2_id_lists) >= 2:
ref_agi, ref_agr = arch_gp2_id_lists[0][2], arch_gp2_id_lists[0][3]
for _, _, agi, agr in arch_gp2_id_lists[1:]:
if agi != ref_agi:
failures.append("FAIL_GP2_ARCHIVE_ANIMAL_ID_ALIAS_CONFLICT")
arch_teratoma_conflict = True
elif any(agr.get(cid) != ref_agr.get(cid) for cid in ref_agi):
if "FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID" not in failures:
failures.append("FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID")
failures.append("FAIL_GP2_ARCHIVE_ANIMAL_ID_ALIAS_CONFLICT")
arch_teratoma_conflict = True
arch_animals = arch_gp2.get("per_animal_exposures")
arch_id_set: Optional[set[str]] = None
arch_raw_map: dict[str, str] = {}
arch_extra_rosters: list[tuple[str, int, int, dict[str, dict[str, Any]], set[str], dict[str, str]]] = []
for rk in _gp2_extra_roster_aliases:
if rk in arch_gp2:
if arch_gp2[rk] is None:
arch_teratoma_incomplete = True
if "HOLD_INCOMPLETE_ARCHIVED_PER_ANIMAL_ROSTER_ALIAS_NULL" not in holds:
holds.append("HOLD_INCOMPLETE_ARCHIVED_PER_ANIMAL_ROSTER_ALIAS_NULL")
continue
_, rk_ev, rk_unk, rk_dist, rk_recs, rk_ids, rk_raw, rk_int_fail, rk_miss = _audit_gp2_animal_list(
arch_gp2[rk],
expected_n=arch_n,
expected_ev=arch_ev,
expected_lot=valid_lot,
prefix="ARCHIVE_ALIAS_",
)
arch_extra_rosters.append((rk, rk_dist, rk_ev, rk_recs, rk_ids, rk_raw))
if rk_int_fail:
arch_teratoma_conflict = True
if rk_miss or rk_unk > 0:
arch_teratoma_incomplete = True
if len(arch_extra_rosters) >= 2:
for i_ae in range(len(arch_extra_rosters)):
for j_ae in range(i_ae + 1, len(arch_extra_rosters)):
if not _reconcile_gp2_animal_records(
arch_extra_rosters[i_ae][3],
arch_extra_rosters[j_ae][3],
mismatch_code="FAIL_GP2_ARCHIVE_ROSTER_ALIAS_CONFLICT",
alias_conflict_code="FAIL_GP2_ARCHIVE_ROSTER_ALIAS_CONFLICT",
skip_missing_in_a=True,
skip_missing_in_b=True,
):
arch_teratoma_conflict = True
gp2_agg_n_cands = [x for x in (t_n, arch_n) if _is_strict_int(x) and 0 < x <= MAX_CLONOGENIC_CELLS]
gp2_agg_ev_cands = [x for x in (t_ev, arch_ev) if _is_strict_int(x) and x >= 0]
if gp2_agg_n_cands and gp2_agg_ev_cands:
eff_g_agg_n = min(gp2_agg_n_cands)
eff_g_agg_ev = max(gp2_agg_ev_cands)
if 0 < eff_g_agg_n <= MAX_CLONOGENIC_CELLS and 0 <= eff_g_agg_ev <= eff_g_agg_n:
provisional_gp2_agg_u95_pct = _safe_round_finite(clopper_pearson_u95(eff_g_agg_ev, eff_g_agg_n) * 100.0, 4)
else:
provisional_gp2_agg_u95_pct = None
if arch_animals is None:
holds.append("HOLD_GP2_ARCHIVE_MISSING_PER_ANIMAL_EXPOSURES")
if arch_extra_rosters and _reconcile_study_followup_against_animal_floor(
study_res=arch_wk_res,
animal_rosters=[ae[3] for ae in arch_extra_rosters],
role="ARCHIVE",
gate_tag="GP2",
failures=failures,
):
arch_teratoma_conflict = True
# Still reconcile any present archive extra rosters against archive ID-lists & caller overlapping present values
if arch_extra_rosters:
ref_ex_ids, ref_ex_raw = arch_extra_rosters[0][4], arch_extra_rosters[0][5]
for _, _, opt_aset, opt_araw in arch_gp2_id_lists:
if opt_aset != ref_ex_ids: