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…
- Rejects any present numeric value < min_weeks (26.0 wk) as FAIL.
- Rejects disagreement (> 1e-12) among multiple present non-None values as FAIL (hard_fail=True).
- Accounts for explicit None or complete omission under the 5-state evidence rules:
* ARCHIVE: any explicit None alias OR complete omission -> governed incomplete-archive HOLD
('HOLD_INCOMPLETE_ARCHIVED_STUDY_FOLLOWUP_ALIAS'), incomplete=True (NEVER silently PASSes!).
* CALLER: complete omission (zero keys present) -> HOLD ('MISSING_teratoma_followup_weeks');
explicit None on any present key marks has_explicit_none=True so reconciliation against
a present archive value fails closed (CALLER_STUDY_FOLLOWUP_ALIAS_NULL_OR_MISMATCH).
"""
present_keys = [k for k in alias_keys if k in rec]
explicit_none_keys = [k for k in present_keys if rec[k] is None]
non_none_items = [(k, rec[k]) for k in present_keys if rec[k] is not None]
valid_vals: list[tuple[str, float]] = []
has_invalid = False
has_below_min = False
has_alias_conflict = False
for k, v in non_none_items:
if not _is_num(v) or float(v) <= 0.0 or float(v) > max_weeks:
has_invalid = True
else:
fv = float(v)
valid_vals.append((k, fv))
if fv < min_weeks:
has_below_min = True
if has_invalid:
code = f"FAIL_{gate_tag}_{role}_INVALID_STUDY_FOLLOWUP_WEEKS"
if code not in failures:
failures.append(code)
if has_below_min:
code = f"FAIL_{gate_tag}_{role}_STUDY_FOLLOWUP_LT_{int(min_weeks)}WK"
if code not in failures:
failures.append(code)
if len(valid_vals) >= 2 and any(abs(v - valid_vals[0][1]) > 1e-12 for _, v in valid_vals[1:]):
has_alias_conflict = True
code = f"FAIL_{gate_tag}_{role}_STUDY_FOLLOWUP_ALIAS_CONFLICT"
if code not in failures:
failures.append(code)
has_explicit_none = bool(explicit_none_keys)
all_missing = len(non_none_items) == 0
incomplete = False
if role == "ARCHIVE":
if all_missing or has_explicit_none:
incomplete = True
hcode = "HOLD_INCOMPLETE_ARCHIVED_STUDY_FOLLOWUP_ALIAS"
if hcode not in holds:
holds.append(hcode)
else:
if all_missing:
incomplete = True
if "MISSING_teratoma_followup_weeks" not in holds:
holds.append("MISSING_teratoma_followup_weeks")
elif has_explicit_none:
incomplete = True
hard_fail = bool(has_invalid or has_below_min or has_alias_conflict)
resolved_wk: Optional[float] = valid_vals[0][1] if (valid_vals and not has_alias_conflict and not has_invalid) else None
return {
"present_keys": present_keys,
"explicit_none_keys": explicit_none_keys,
"valid_vals": valid_vals,
"resolved_wk": resolved_wk,
"has_invalid": has_invalid,
"has_below_min": has_below_min,
"has_alias_conflict": has_alias_conflict,
"has_explicit_none": has_explicit_none,
"all_missing": all_missing,
"incomplete": incomplete,
"hard_fail": hard_fail,
}
def _reconcile_study_level_followup_archive_and_caller(
*,
arch_res: Mapping[str, Any],
caller_res: Mapping[str, Any],
gate_tag: str,
holds: list[str],
failures: list[str],
) -> tuple[bool, bool, bool]:
"""Reconcile audited archive and caller study-level follow-up alias summaries (Audit R12-05).
Returns (arch_internal_conflict, arch_incomplete, caller_conflict).
"""
arch_internal_conflict = bool(arch_res["hard_fail"])
arch_incomplete = bool(arch_res["incomplete"])
caller_conflict = bool(caller_res["hard_fail"])
arch_vals: list[tuple[str, float]] = list(arch_res["valid_vals"])
caller_vals: list[tuple[str, float]] = list(caller_res["valid_vals"])
if arch_vals:
if caller_res["has_explicit_none"]:
code = "CALLER_STUDY_FOLLOWUP_ALIAS_NULL_OR_MISMATCH"
if code not in failures:
failures.append(code)
caller_conflict = True
if caller_vals:
if any(abs(av - cv) > 1e-12 for _, av in arch_vals for _, cv in caller_vals):
code = f"FAIL_{gate_tag}_CALLER_ARCHIVE_STUDY_FOLLOWUP_MISMATCH"
if code not in failures:
failures.append(code)
caller_conflict = True
else:
if caller_res["has_explicit_none"] and caller_vals:
if "HOLD_CALLER_STUDY_FOLLOWUP_ALIAS_NULL" not in holds:
holds.append("HOLD_CALLER_STUDY_FOLLOWUP_ALIAS_NULL")
caller_conflict = True
return arch_internal_conflict, arch_incomplete, caller_conflict
def _reconcile_study_followup_against_animal_floor(
*,
study_res: Mapping[str, Any],
animal_rosters: Sequence[Mapping[str, Mapping[str, Any]]],
role: str,
gate_tag: str,
failures: list[str],
) -> bool:
"""Verify no present study-level follow-up claim exceeds the minimum present per-animal
follow-up floor across audited rosters (Audit R12-05). Returns True on contradiction."""
study_vals = [v for _, v in study_res["valid_vals"]]
if not study_vals:
return False
animal_wks = [
float(rec["followup_weeks"])
for roster in animal_rosters
for rec in roster.values()
if rec.get("followup_weeks") is not None and _is_num(rec.get("followup_weeks"))
]
if not animal_wks:
return False
min_animal_wk = min(animal_wks)
if any(sv > min_animal_wk + 1e-12 for sv in study_vals):
code = f"FAIL_{gate_tag}_{role}_STUDY_FOLLOWUP_EXCEEDS_ANIMAL_FLOOR"
if code not in failures:
failures.append(code)
return True
return False
def _audit_study_level_int_aliases(
rec: Mapping[str, Any],
*,
role: str,
gate_tag: str,
field_tag: str,
alias_keys: Sequence[str],
min_val: int,
max_val: int = MAX_CLONOGENIC_CELLS,
allow_zero: bool = False,
nonzero_event_fail_code: Optional[str] = None,
below_min_fail_code: str,
missing_hold_code: str,
invalid_fail_code: str,
holds: list[str],
failures: list[str],
) -> dict[str, Any]:
"""Common study-level strict-integer teratoma alias validator shared identically by GP2 & EnSC
across BOTH archive and caller payloads (Audit R12-05)."""
present_keys = [k for k in alias_keys if k in rec]
explicit_none_keys = [k for k in present_keys if rec[k] is None]
non_none_items = [(k, rec[k]) for k in present_keys if rec[k] is not None]
valid_vals: list[tuple[str, int]] = []
has_invalid = False
has_below_min = False
has_alias_conflict = False
has_nonzero_event = False
for k, v in non_none_items:
low_ok = (v >= 0) if allow_zero else (v > 0)
if not _is_strict_int(v) or not low_ok or v > max_val:
has_invalid = True
else:
valid_vals.append((k, v))
if v < min_val:
has_below_min = True
if nonzero_event_fail_code is not None and v > 0:
has_nonzero_event = True
if has_invalid:
if invalid_fail_code not in failures:
failures.append(invalid_fail_code)
if has_below_min:
if below_min_fail_code not in failures:
failures.append(below_min_fail_code)
if has_nonzero_event and nonzero_event_fail_code is not None:
if nonzero_event_fail_code not in failures:
failures.append(nonzero_event_fail_code)
if len(valid_vals) >= 2 and any(v != valid_vals[0][1] for _, v in valid_vals[1:]):
has_alias_conflict = True
code = f"FAIL_{gate_tag}_{role}_STUDY_{field_tag}_ALIAS_CONFLICT"
if code not in failures:
failures.append(code)
has_explicit_none = bool(explicit_none_keys)
all_missing = len(non_none_items) == 0
incomplete = False
if role == "ARCHIVE":
if all_missing or has_explicit_none:
incomplete = True
if missing_hold_code not in holds:
holds.append(missing_hold_code)
else:
if all_missing:
incomplete = True
if missing_hold_code not in holds:
holds.append(missing_hold_code)
elif has_explicit_none:
incomplete = True
hard_fail = bool(has_invalid or has_below_min or has_alias_conflict)
resolved_val: Optional[int] = valid_vals[0][1] if (valid_vals and not has_alias_conflict and not has_invalid) else None
return {
"present_keys": present_keys,
"explicit_none_keys": explicit_none_keys,
"valid_vals": valid_vals,
"resolved_val": resolved_val,
"has_invalid": has_invalid,
"has_below_min": has_below_min,
"has_alias_conflict": has_alias_conflict,
"has_explicit_none": has_explicit_none,
"all_missing": all_missing,
"incomplete": incomplete,
"hard_fail": hard_fail,
}
def _reconcile_study_level_int_archive_and_caller(
*,
arch_res: Mapping[str, Any],
caller_res: Mapping[str, Any],
gate_tag: str,
field_tag: str,
mismatch_fail_code: str,
holds: list[str],
failures: list[str],
) -> tuple[bool, bool, bool]:
"""Reconcile audited archive and caller study-level strict-integer aliases (Audit R12-05)."""
arch_internal_conflict = bool(arch_res["hard_fail"])
arch_incomplete = bool(arch_res["incomplete"])
caller_conflict = bool(caller_res["hard_fail"])
arch_vals: list[tuple[str, int]] = list(arch_res["valid_vals"])
caller_vals: list[tuple[str, int]] = list(caller_res["valid_vals"])