scripts/verify_iip0044_foundry_v1_3_biologics.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0044_foundry_v1_3_biologics.py · SHA-256: 1d6c53a0817f6254…
sampling_design_class_val: str | None = None
sampling_protocol_ref_val: str | None = None
if not isinstance(sampling_design_obj, dict):
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
"Missing mandatory typed donor_sampling_and_independence_design block "
"(requires sampling_design_class, independent_donor_sampling_attested=True, "
"between_donor_shared_shock_controls_verified=True, sampling_protocol_ref, "
"between_donor_independence_justification)",
)
else:
sd_ok = True
extra_sd_keys = sorted(set(sampling_design_obj.keys()) - _allowed_sampling_design_keys)
if extra_sd_keys:
sd_ok = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"donor_sampling_and_independence_design contains unexpected/free-text keys {extra_sd_keys}; "
f"strict closed typed schema required",
)
sd_cls = sampling_design_obj.get("sampling_design_class")
sd_cls_valid = type(sd_cls) is str and sd_cls in _SHIELD_ALLOWED_DONOR_SAMPLING_DESIGNS
if not sd_cls_valid:
sd_ok = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"donor_sampling_and_independence_design.sampling_design_class must be in "
f"{sorted(_SHIELD_ALLOWED_DONOR_SAMPLING_DESIGNS)} (got {sd_cls!r}; only "
f"{sorted(_SHIELD_AUTHORIZING_DONOR_SAMPLING_DESIGNS)} authorizes unweighted "
f"Clopper-Pearson population-suppression inference)",
)
if sampling_design_obj.get("independent_donor_sampling_attested") is not True:
sd_ok = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
"donor_sampling_and_independence_design.independent_donor_sampling_attested must be literal boolean True",
)
if sampling_design_obj.get("between_donor_shared_shock_controls_verified") is not True:
sd_ok = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
"donor_sampling_and_independence_design.between_donor_shared_shock_controls_verified must be literal boolean True",
)
sd_ref = sampling_design_obj.get("sampling_protocol_ref")
if type(sd_ref) is not str or not sd_ref or sd_ref != sd_ref.strip() or not _SHIELD_EVIDENCE_REF_RE.match(sd_ref):
sd_ok = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"donor_sampling_and_independence_design.sampling_protocol_ref must be an unpadded ref matching "
f"{_SHIELD_EVIDENCE_REF_RE.pattern} (got {sd_ref!r})",
)
sd_just = sampling_design_obj.get("between_donor_independence_justification")
allowed_just_for_cls = (
_allowed_between_donor_justifications_by_class[sd_cls]
if sd_cls_valid and type(sd_cls) is str
else frozenset()
)
if type(sd_just) is not str or sd_just not in allowed_just_for_cls:
sd_ok = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"donor_sampling_and_independence_design.between_donor_independence_justification "
f"must be a strict class-specific canonical allowlisted declaration compatible with "
f"sampling_design_class={sd_cls!r} (free-text prose and cross-class contradictions "
f"rejected fail-closed; got {sd_just!r})",
)
if sd_ok and type(sd_cls) is str and type(sd_ref) is str:
donor_sampling_design_valid = True
sampling_design_class_val = sd_cls
sampling_protocol_ref_val = sd_ref
suppression_inference_authorized_by_sampling_design = bool(
sd_cls in _SHIELD_AUTHORIZING_DONOR_SAMPLING_DESIGNS
)
# Parse optional documented pre-assay excluded_donor_records for 100% roster reconciliation
excluded_records_valid = True
valid_excluded_donor_ids: set[str] = set()
raw_excluded_donor_ids: set[str] = set()
if "excluded_donor_records" in observation and observation["excluded_donor_records"] is not None:
excl_raw = observation["excluded_donor_records"]
if not isinstance(excl_raw, list):
excluded_records_valid = False
_escalate("HOLD_INVALID_OR_MISSING_INPUT", "excluded_donor_records must be a list of pre-assay exclusion objects")
else:
for e_idx, e_rec in enumerate(excl_raw):
if not isinstance(e_rec, dict):
excluded_records_valid = False
_escalate("HOLD_INVALID_OR_MISSING_INPUT", f"excluded_donor_records[{e_idx}] must be an object")
continue
e_did = e_rec.get("donor_id")
if type(e_did) is str and e_did.strip():
raw_excluded_donor_ids.add(e_did.strip())
if (
type(e_did) is not str
or not e_did
or e_did != e_did.strip()
or not _SHIELD_STRICT_ID_RE.match(e_did)
):
excluded_records_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"excluded_donor_records[{e_idx}].donor_id must be an unpadded canonical ID (got {e_did!r})",
)
continue
if e_did in valid_excluded_donor_ids:
excluded_records_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"excluded_donor_records contains duplicate donor_id {e_did!r}",
)
continue
if roster_verified and e_did not in roster_set:
excluded_records_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"excluded_donor_records[{e_idx}].donor_id={e_did!r} is not in preregistered_donor_family_roster",
)
continue
e_reason = e_rec.get("pre_assay_exclusion_reason")
if type(e_reason) is not str or len(e_reason) < 8 or e_reason != e_reason.strip():
excluded_records_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"excluded_donor_records[{e_idx}].pre_assay_exclusion_reason must be an unpadded string "
f"of length >= 8 (got {e_reason!r})",
)
continue
if e_rec.get("outcome_blind_exclusion_attested") is not True:
excluded_records_valid = False
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"excluded_donor_records[{e_idx}].outcome_blind_exclusion_attested must be literal True",
)
continue
valid_excluded_donor_ids.add(e_did)
valid_donor_sup_vals: list[float] = []
valid_donor_sup_by_id: dict[str, float] = {}
topology_donor_batches: dict[str, set[str]] = {}
topology_donor_runs: dict[str, set[str]] = {}
donors_with_invalid_topology: set[str] = set()
valid_donor_cyto_rates: list[float] = []
valid_donor_cp_l95s_unadj: list[float] = []
valid_donor_cp_u95s: list[float] = []
valid_donor_k_list: list[int] = []
valid_donor_neff_list: list[int] = []
seen_donors: set[str] = set()
raw_submitted_donor_ids: set[str] = set()
conflicted_donors: set[str] = set()
unrostered_submitted_donors: set[str] = set()
excluded_overlap_donors: set[str] = set()
invalid_donors_excluded: list[str] = []
malformed_donor_rows_present = False
_allowed_gy018_keys = frozenset({
"independent_unit_class",
"within_donor_units_physically_independent",
"clustering_controls_present_and_verified",
"shared_well_technical_reads_excluded",
"independence_evidence_record_id",
"independent_experimental_unit",
"predeclared_per_donor_unit_count_n",
"clustering_controls",
})
if not isinstance(donors_raw, list) or len(donors_raw) == 0:
_escalate("HOLD_INVALID_OR_MISSING_INPUT", "Missing typed tcell_blockade_and_apc_cytotoxicity_donors list (>= 3 distinct donors required)")
else:
# Pass 1 (R08 B3 + R10 B1 + R12 0037-B2): Collect ALL raw canonical submitted donor IDs & malformed rows BEFORE
# deduplication/quarantine filtering, and enforce roster membership & overlap on the COMPLETE raw set!
donor_id_counts: dict[str, int] = {}
for d in donors_raw:
if not isinstance(d, dict):
malformed_donor_rows_present = True
continue
did = d.get("donor_id")
if type(did) is not str or not did.strip():
malformed_donor_rows_present = True
continue
did_p = did.strip()
raw_submitted_donor_ids.add(did_p)
donor_id_counts[did_p] = donor_id_counts.get(did_p, 0) + 1
if did != did_p or not _SHIELD_STRICT_ID_RE.match(did):
malformed_donor_rows_present = True
# Roster-integrity checks run on ALL raw submitted donor IDs BEFORE deduplication (R10 B1!)
for raw_did_c in sorted(raw_submitted_donor_ids):
if roster_verified and raw_did_c not in roster_set:
unrostered_submitted_donors.add(raw_did_c)
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"donor[{raw_did_c}]: unrostered ad-hoc donor_id not present in preregistered_donor_family_roster; "
f"rejected fail-closed",
)
if raw_did_c in valid_excluded_donor_ids or raw_did_c in raw_excluded_donor_ids:
excluded_overlap_donors.add(raw_did_c)
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"donor[{raw_did_c}]: donor appears in BOTH submitted records and excluded_donor_records; rejected fail-closed",
)
for raw_ex_did in sorted(raw_excluded_donor_ids):
if roster_verified and raw_ex_did not in roster_set:
unrostered_submitted_donors.add(raw_ex_did)
conflicted_donors = {k for k, cnt in donor_id_counts.items() if cnt > 1}
for cdid in sorted(conflicted_donors):
donors_with_invalid_topology.add(cdid)
_escalate(
"HOLD_INVALID_OR_MISSING_INPUT",
f"Duplicate/conflicting donor_id in functional cohort: {cdid!r} (count={donor_id_counts[cdid]}); "
f"all occurrences excluded from biological adjudication and batch-linkage credit",
)
invalid_donors_excluded.append(cdid)
# Pass 1b (R12 0037-B2): Extract EVERY submitted donor's batch/run dependency links into Union-Find
# topology maps INDEPENDENTLY OF AND BEFORE per-donor outcome/CI/GY-018 validation!
for d in donors_raw:
if not isinstance(d, dict):
continue
did = d.get("donor_id")
if type(did) is not str or not did.strip():
continue
did_t = did.strip()
raw_run_t = d.get("assay_run_id")
if (
type(raw_run_t) is str
and raw_run_t
and raw_run_t == raw_run_t.strip()
and _SHIELD_STRICT_ID_RE.match(raw_run_t)
):
topology_donor_runs.setdefault(did_t, set()).add(raw_run_t)
else:
donors_with_invalid_topology.add(did_t)
if type(raw_run_t) is str and raw_run_t.strip():
topology_donor_runs.setdefault(did_t, set()).add(raw_run_t.strip())
raw_bids_t = d.get("expression_batch_ids")
if isinstance(raw_bids_t, list) and len(raw_bids_t) > 0:
seen_tb: set[str] = set()
tb_ok = True
for b_item in raw_bids_t:
if type(b_item) is str and b_item.strip():
b_norm = b_item.strip()
topology_donor_batches.setdefault(did_t, set()).add(b_norm)
if b_item != b_norm or b_norm in seen_tb:
tb_ok = False
seen_tb.add(b_norm)
else:
tb_ok = False
if not tb_ok or not seen_tb:
donors_with_invalid_topology.add(did_t)
else: