scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…
v72_n,
a72_n,
n_in if is_infected_arm else None,
rn if is_infected_arm else None,
frn if is_infected_arm else None,
)
if _is_strict_int(d_n) and d_n >= 1
]
if sampling_tracker is not None and arm_denoms:
max_arm_n = max(arm_denoms)
if max_arm_n > sampling_tracker["max_binomial_denominator_n"]:
sampling_tracker["max_binomial_denominator_n"] = max_arm_n
raw_puc = lblock.get("per_unit_counts")
arm_estimand_specs: List[Tuple[str, Any, Any, bool]] = [
("veh_24h", v24_v, v24_n, False),
("ap1903_24h", a24_k, a24_n, False),
("veh_72h", v72_v, v72_n, False),
("ap1903_72h", a72_k, a72_n, False),
]
if is_infected_arm:
arm_estimand_specs.append(("infected_viral_pos_72h", vk_raw, n_in, True))
if direct_inf_veh_k is not None:
arm_estimand_specs.append(("direct_infected_veh_72h", direct_inf_veh_k, v72_n, False))
arm_estimand_specs.append(("spike_calibrant", rk, rn, False))
arm_estimand_specs.append(("fragile_infected_spike_calibrant", frk, frn, False))
if sampling_unit_class == "RECONCILED_MULTI_WELL_CELL_ASSAY_WITH_UNIT_PARTITION":
sub_parsed: Dict[str, Dict[str, Any]] = {}
sub_cross_ok: Dict[str, bool] = {}
for sub_key, exp_k, exp_n, is_zt_viral in arm_estimand_specs:
if sampling_tracker is not None:
sampling_tracker["partitions_checked"] += 1
sub_raw = raw_puc.get(sub_key) if isinstance(raw_puc, dict) else None
p_info = _inspect_per_unit_counts(
sub_raw,
expected_units_n=indep_units_n,
expected_k=exp_k if _is_strict_int(exp_k) else None,
expected_n=exp_n if _is_strict_int(exp_n) else None,
)
sub_parsed[sub_key] = p_info
sub_cross_ok[sub_key] = bool(p_info["reconciled_ok"])
if is_zt_viral and p_info["any_pos_k"]:
_append_unique(hard_stops, "HARD_STOP_ADVERSE_PER_UNIT_POST_AP1903_VIRAL_ESCAPE")
_append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}POST_AP1903_VIABLE_VIRAL_SURVIVOR_{reg_tag}_{lin_tag}")
if not p_info["reconciled_ok"]:
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}SAMPLING_UNIT_PARTITION_MISMATCH_{reg_tag}_{lin_tag}_{sub_key.upper()}_{p_info['reason_code']}",
)
# R13-B2: Cross-estimand same-unit ID set matching & within-unit subset inequality reconciliation!
# All paired estimands in the same arm MUST share the exact same canonical unit_id set
# (`len == independent_biological_units_n >= 6`), and within EVERY individual unit `u`:
# - `direct_infected_veh_72h`: `n_u == veh_72h.n_u` and `0 <= k_u <= veh_72h.k_u <= veh_72h.n_u`
# - `infected_viral_pos_72h` (under SAME_72H_ALIQUOT): `n_u == ap1903_72h.n_u` and `0 <= k_u <= ap1903_72h.k_u <= ap1903_72h.n_u`
ref_uid_set: set[str] | None = None
if "veh_72h" in sub_parsed and sub_parsed["veh_72h"]["reconciled_ok"]:
ref_uid_set = set(sub_parsed["veh_72h"]["unit_map"].keys())
else:
for sk_i, pi_i in sub_parsed.items():
if pi_i["reconciled_ok"] and pi_i["unit_map"]:
ref_uid_set = set(pi_i["unit_map"].keys())
break
if ref_uid_set is not None:
for sub_key, p_info in sub_parsed.items():
if p_info["reconciled_ok"] and set(p_info["unit_map"].keys()) != ref_uid_set:
sub_cross_ok[sub_key] = False
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}SAMPLING_UNIT_ID_SET_MISMATCH_{reg_tag}_{lin_tag}_{sub_key.upper()}",
)
# Pair A: direct_infected_veh_72h <= veh_72h (same unit IDs, same n_u, k_inf_u <= k_veh72_u <= n_u)
if (
"direct_infected_veh_72h" in sub_parsed
and "veh_72h" in sub_parsed
and sub_parsed["direct_infected_veh_72h"]["unit_map"]
and sub_parsed["veh_72h"]["unit_map"]
):
dir_map = sub_parsed["direct_infected_veh_72h"]["unit_map"]
v72_map = sub_parsed["veh_72h"]["unit_map"]
if set(dir_map.keys()) != set(v72_map.keys()):
sub_cross_ok["direct_infected_veh_72h"] = False
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}DIRECT_INFECTED_VEH_UNIT_ID_MISMATCH_{reg_tag}_{lin_tag}",
)
else:
for uid_key, (dir_k_u, dir_n_u) in dir_map.items():
v72_k_u, v72_n_u = v72_map[uid_key]
if dir_n_u != v72_n_u or not (0 <= dir_k_u <= v72_k_u <= v72_n_u):
sub_cross_ok["direct_infected_veh_72h"] = False
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}DIRECT_INFECTED_VEH_UNIT_SUBSET_CONTRADICTION_{reg_tag}_{lin_tag}",
)
break
# Pair B: under SAME_72H_ALIQUOT, infected_viral_pos_72h <= ap1903_72h (same unit IDs, same n_u, k_vp_u <= k_a72_u <= n_u)
if (
is_infected_arm
and lblock.get("infected_surveillance_aliquot_linkage_mode") == "SAME_72H_ALIQUOT"
and "infected_viral_pos_72h" in sub_parsed
and "ap1903_72h" in sub_parsed
and sub_parsed["infected_viral_pos_72h"]["unit_map"]
and sub_parsed["ap1903_72h"]["unit_map"]
):
vp_map = sub_parsed["infected_viral_pos_72h"]["unit_map"]
a72_map = sub_parsed["ap1903_72h"]["unit_map"]
if set(vp_map.keys()) != set(a72_map.keys()):
sub_cross_ok["infected_viral_pos_72h"] = False
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}INFECTED_VIRAL_POS_UNIT_ID_MISMATCH_{reg_tag}_{lin_tag}",
)
else:
for uid_key, (vp_k_u, vp_n_u) in vp_map.items():
a72_k_u, a72_n_u = a72_map[uid_key]
if vp_n_u != a72_n_u or not (0 <= vp_k_u <= a72_k_u <= a72_n_u):
sub_cross_ok["infected_viral_pos_72h"] = False
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_SAMPLING_UNIT_SUBSET_OR_ID_CONTRADICTION")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}INFECTED_VIRAL_POS_UNIT_SUBSET_CONTRADICTION_{reg_tag}_{lin_tag}",
)
break
if sampling_tracker is not None:
for sub_key, _ek, _en, _iz in arm_estimand_specs:
if sub_parsed[sub_key]["reconciled_ok"] and sub_cross_ok.get(sub_key, False):
sampling_tracker["partitions_reconciled"] += 1
elif sampling_unit_class == "INDEPENDENT_SINGLE_CELL_OR_LIMITING_DILUTION_UNIT":
if "per_unit_counts" in lblock:
# R13-B1: Single-cell mode NEVER accepts submitted `per_unit_counts` (adverse unit counts
# were already escalated to FAIL_HARD_STOP by the pre-schema scanner; clean unexpected
# partitions fail closed with HOLD_UNEXPECTED_PER_UNIT_COUNTS_IN_SINGLE_CELL_MODE!).
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_UNEXPECTED_PER_UNIT_COUNTS_IN_SINGLE_CELL_MODE")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}UNEXPECTED_PER_UNIT_COUNTS_IN_SINGLE_CELL_MODE_{reg_tag}_{lin_tag}",
)
if not (
_is_strict_int(indep_units_n)
and indep_units_n >= MIN_INDEPENDENT_BIOLOGICAL_UNITS_N
and all(indep_units_n >= d_n for d_n in arm_denoms)
):
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS")
_append_unique(
holds,
f"HOLD_GATE0V_KILL_{sup_hd}INDEPENDENT_UNIT_COUNT_BELOW_DENOMINATOR_{reg_tag}_{lin_tag}",
)
if is_infected_arm and not (
_is_strict_int(rk) and _is_strict_int(rn) and rn >= 1 and 0 <= rk <= rn
and _is_strict_int(frk) and _is_strict_int(frn) and frn >= 1 and 0 <= frk <= frn
):
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
_append_unique(holds, "HOLD_SAMPLING_UNIT_PARTITION_MISMATCH_OR_INSUFFICIENT_UNITS")
else:
arm_partition_ok = False
if sampling_tracker is not None:
sampling_tracker["all_partitions_reconciled"] = False
if isinstance(raw_puc, dict) and is_infected_arm:
p_inf_info = _inspect_per_unit_counts(
raw_puc.get("infected_viral_pos_72h"),
expected_units_n=indep_units_n,
expected_k=vk_raw if _is_strict_int(vk_raw) else None,
expected_n=n_in if _is_strict_int(n_in) else None,
)
if p_inf_info["any_pos_k"]:
_append_unique(hard_stops, "HARD_STOP_ADVERSE_PER_UNIT_POST_AP1903_VIRAL_ESCAPE")
_append_unique(hard_stops, f"FAIL_HARD_STOP_{sup_hs}POST_AP1903_VIABLE_VIRAL_SURVIVOR_{reg_tag}_{lin_tag}")
is_clustered_deff_gt_1 = bool(deff is not None and deff > Decimal("1.0"))
if not sampling_spec_valid or deff is None:
is_exact_independent = False
arm_confidence_scope = "UNCERTIFIED_MISSING_OR_INVALID_SAMPLING_SPEC"
elif is_clustered_deff_gt_1:
is_exact_independent = False
arm_confidence_scope = "NON_AUTHORIZING_VARIANCE_ADJUSTED_DIAGNOSTIC_ONLY"
elif not arm_partition_ok:
is_exact_independent = False
arm_confidence_scope = "UNCERTIFIED_SAMPLING_UNIT_PARTITION_MISMATCH"
elif sampling_unit_class == "INDEPENDENT_SINGLE_CELL_OR_LIMITING_DILUTION_UNIT":
is_exact_independent = True
arm_confidence_scope = "EXACT_INDEPENDENT_BINOMIAL_BONFERRONI_DEFF_EQ_1_UNIT_MATCHED"
else:
is_exact_independent = True
arm_confidence_scope = "CONDITIONAL_ON_EXTERNALLY_VALIDATED_WITHIN_WELL_INDEPENDENCE_DEFF_EQ_1"
arm_pass = bool(is_exact_independent)
lin_detail: Dict[str, Any] = {
"clustering_design_effect_deff": format(deff, "f") if deff is not None else None,
"exact_directional_confidence_certified": is_exact_independent,
"confidence_scope": arm_confidence_scope,
"sampling_unit_partition_reconciled": bool(arm_partition_ok and sampling_spec_valid),
}
if is_infected_arm:
if sampling_unit_class == "RECONCILED_MULTI_WELL_CELL_ASSAY_WITH_UNIT_PARTITION":
_arm_cal_h_ok = bool(
sub_parsed.get("spike_calibrant", {}).get("reconciled_ok")
and sub_cross_ok.get("spike_calibrant", False)
)
_arm_cal_f_ok = bool(
sub_parsed.get("fragile_infected_spike_calibrant", {}).get("reconciled_ok")
and sub_cross_ok.get("fragile_infected_spike_calibrant", False)
)
lin_detail["spike_calibrant_sampling_units_reconciled"] = bool(
_arm_cal_h_ok and _arm_cal_f_ok and sampling_spec_valid
)
elif sampling_unit_class == "INDEPENDENT_SINGLE_CELL_OR_LIMITING_DILUTION_UNIT":
lin_detail["spike_calibrant_sampling_units_reconciled"] = bool(
arm_partition_ok and sampling_spec_valid
)
else:
lin_detail["spike_calibrant_sampling_units_reconciled"] = False
# R11-B3: Narrow exact Clopper-Pearson directional confidence authorization strictly to deff == 1.0