scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py · SHA-256: 16964a158458b5cf…
f"Duplicate/conflicting commercial lot_id: {clid!r} (count={lot_id_counts[clid]}); excluded from biological adjudication",
)
lot_ids_seen: set[str] = set()
per_lot_verdicts: dict[str, dict[str, Any]] = {}
all_linked_fab_c3d: list[float] = []
all_linked_fab_c5b9: list[float] = []
all_linked_fab_lysis: list[float] = []
withheld_cross_run_fab_c3d: list[float] = []
withheld_cross_run_fab_c5b9: list[float] = []
withheld_cross_run_fab_lysis: list[float] = []
invalid_vehicle_fab_c3d: list[float] = []
invalid_vehicle_fab_c5b9: list[float] = []
invalid_vehicle_fab_lysis: list[float] = []
withheld_cross_run_fab_rows_count = 0
withheld_invalid_fab_rows_count = 0
withheld_invalid_vehicle_fab_rows_count = 0
for idx, lot in enumerate(lots):
if not isinstance(lot, dict):
_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Lot entry {idx} is not a dict")
continue
raw_lid = lot.get("lot_id")
lot_id = raw_lid.strip() if type(raw_lid) is str else ""
if not lot_id:
_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", f"Missing commercial lot_id: {raw_lid!r}")
continue
if lot_id in conflicted_lot_ids:
continue
lot_ids_seen.add(lot_id)
lot_state = "PASS"
lot_endpoints: dict[str, Any] = {}
def _lot_escalate(st: str, msg: str) -> None:
nonlocal lot_state
if _XREACT_PRECEDENCE[st] > _XREACT_PRECEDENCE[lot_state]:
lot_state = st
_escalate(st, f"lot={lot_id}: {msg}")
# R08 B2 + R09 B1: Check optional lot-level independent matched normal rabbit IgG isotype provenance
lot_ind_igg_valid, lot_ind_igg_explicit_bad, lot_ind_igg_id, lot_ind_igg_err = _parse_independent_igg_provenance(
lot, f"lot={lot_id}"
)
if lot_ind_igg_err is not None:
_lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", lot_ind_igg_err)
# R09 B1: Two-pass order-independent reconciliation of traceable lot/run independent_matched_igg_controls
# (from lot.independent_matched_igg_controls AND any lot-matched controls.independent_matched_igg_controls).
# Every independent matched-IgG claim on any lot/row MUST resolve to a verified, non-conflicted,
# dose-complete, lysis-valid run record here BEFORE authorizing biological FAIL adjudication!
lot_ind_ctrl_raw = lot.get("independent_matched_igg_controls")
ind_ctrl_entries: list[Any] = []
if lot_ind_ctrl_raw is not None:
if isinstance(lot_ind_ctrl_raw, dict):
ind_ctrl_entries.append(lot_ind_ctrl_raw)
elif isinstance(lot_ind_ctrl_raw, list) and len(lot_ind_ctrl_raw) > 0:
ind_ctrl_entries.extend(lot_ind_ctrl_raw)
else:
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"independent_matched_igg_controls must be a dict or non-empty list of per-run control dicts (got {lot_ind_ctrl_raw!r})",
)
ind_ctrl_entries.extend(top_ind_igg_entries_by_lot.get(lot_id, []))
candidate_valid_ind_igg_by_run: dict[str, dict[str, Any]] = {}
conflicted_or_invalid_ind_igg_run_ids: set[str] = set()
ind_ctrl_run_counts: dict[str, int] = {}
for ic_item in ind_ctrl_entries:
if isinstance(ic_item, dict):
ic_run = ic_item.get("plate_or_run_id")
ic_run_c = ic_run.strip() if type(ic_run) is str else ""
if ic_run_c:
ind_ctrl_run_counts[ic_run_c] = ind_ctrl_run_counts.get(ic_run_c, 0) + 1
for dup_irun in sorted(r for r, cnt in ind_ctrl_run_counts.items() if cnt > 1):
conflicted_or_invalid_ind_igg_run_ids.add(dup_irun)
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"Duplicate/conflicting plate_or_run_id={dup_irun!r} inside independent_matched_igg_controls "
f"(count={ind_ctrl_run_counts[dup_irun]}); run independent matched-IgG provenance invalidated prior to biological adjudication",
)
for ic_idx, ic_item in enumerate(ind_ctrl_entries):
if not isinstance(ic_item, dict):
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"independent_matched_igg_controls[{ic_idx}] must be a dict (got {type(ic_item).__name__})",
)
continue
ic_ok = True
ic_lid = ic_item.get("lot_id")
if type(ic_lid) is not str or not ic_lid or ic_lid != ic_lid.strip() or ic_lid != lot_id:
ic_ok = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"independent_matched_igg_controls[{ic_idx}].lot_id={ic_lid!r} must be an unpadded exact match to parent lot_id={lot_id!r}",
)
ic_run = ic_item.get("plate_or_run_id")
ic_run_norm = ic_run.strip() if type(ic_run) is str else ""
if type(ic_run) is not str or not ic_run or ic_run != ic_run.strip():
ic_ok = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"independent_matched_igg_controls[{ic_idx}].plate_or_run_id must be a non-empty unpadded string (got {ic_run!r})",
)
elif ind_ctrl_run_counts.get(ic_run, 0) > 1:
ic_ok = False
ic_prov_ok, _, ic_cid, ic_prov_err = _parse_independent_igg_provenance(
ic_item, f"{lot_id}.independent_matched_igg_controls[{ic_idx}]"
)
if not ic_prov_ok or ic_cid is None:
ic_ok = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
ic_prov_err or f"independent_matched_igg_controls[{ic_idx}] missing valid independent matched-IgG provenance",
)
ic_doses_raw = ic_item.get("matched_doses_ug_ml")
if not isinstance(ic_doses_raw, list):
ic_ok = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"independent_matched_igg_controls[{ic_idx}] missing matched_doses_ug_ml list covering {_XREACT_REQUIRED_DOSES_UG_ML}",
)
else:
ic_doses: set[float] = set()
for d_i, dv in enumerate(ic_doses_raw):
df, derr = _check_xreact_domain_number(
dv, f"{lot_id}.independent_matched_igg_controls[{ic_idx}].matched_doses_ug_ml[{d_i}]", lo=0.0, hi=None
)
if derr is not None or df is None:
ic_ok = False
_lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", derr or "Invalid independent matched-IgG dose")
else:
ic_doses.add(df)
miss_ic_doses = sorted(set(_XREACT_REQUIRED_DOSES_UG_ML) - ic_doses)
if miss_ic_doses:
ic_ok = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"independent_matched_igg_controls[{ic_idx}] missing required doses ug/mL: {miss_ic_doses}",
)
ic_lysis, ic_lerr = _check_xreact_domain_number(
ic_item.get("max_lysis_pct"),
f"{lot_id}.independent_matched_igg_controls[{ic_idx}].max_lysis_pct",
lo=0.0,
hi=100.0,
)
if ic_lerr is not None or ic_lysis is None:
ic_ok = False
_lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", ic_lerr or "Invalid independent matched-IgG max_lysis_pct")
elif ic_lysis > 5.0:
ic_ok = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"independent_matched_igg_controls[{ic_idx}].max_lysis_pct={ic_lysis:.2f}% > 5.0% baseline limit",
)
if ic_run_norm:
if ic_ok and ic_cid is not None and ic_lysis is not None:
candidate_valid_ind_igg_by_run[ic_run_norm] = {
"control_lot_id": ic_cid,
"max_lysis_pct": ic_lysis,
}
else:
conflicted_or_invalid_ind_igg_run_ids.add(ic_run_norm)
valid_ind_igg_by_run: dict[str, dict[str, Any]] = {
r: v for r, v in candidate_valid_ind_igg_by_run.items() if r not in conflicted_or_invalid_ind_igg_run_ids
}
# R06 B2 + R07 B4 + R08 B1: Two-pass order-independent reconciliation of lot_positive_controls.
# Pass 1 scans ALL positive-control records for this lot, counting occurrences of each plate_or_run_id
# and checking validity (exact lot_id match, non-empty plate_or_run_id, cd3>=90.0%, b6h12>=90.0%).
# ANY plate_or_run_id with >1 record (duplicate/conflicting) OR any failing/invalid record is added to
# conflicted_or_invalid_ctrl_run_ids and EXCLUDED from passing_ctrl_run_ids BEFORE biological adjudication
# (so [pass, zero-CD3] and [zero-CD3, pass] produce identical HOLD_INVALID_OR_INCOMPLETE_ASSAY!).
lot_pos_ctrl = lot.get("lot_positive_controls")
lot_pos_ctrl_valid = True
candidate_passing_ctrl_run_ids: set[str] = set()
conflicted_or_invalid_ctrl_run_ids: set[str] = set()
ctrl_run_counts: dict[str, int] = {}
if isinstance(lot_pos_ctrl, dict):
lpc_entries: list[Any] = [lot_pos_ctrl]
elif isinstance(lot_pos_ctrl, list) and len(lot_pos_ctrl) > 0:
lpc_entries = lot_pos_ctrl
else:
lpc_entries = []
lot_pos_ctrl_valid = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
"Missing mandatory lot-specific lot_positive_controls block (dict or non-empty per-run list) bound to lot_id and plate_or_run_id",
)
# Pass 1a: Count occurrences of every non-empty plate_or_run_id across ALL records in lot_positive_controls
for lpc_item in lpc_entries:
if isinstance(lpc_item, dict):
lpc_run = lpc_item.get("plate_or_run_id")
lpc_run_c = lpc_run.strip() if type(lpc_run) is str else ""
if lpc_run_c:
ctrl_run_counts[lpc_run_c] = ctrl_run_counts.get(lpc_run_c, 0) + 1
for dup_run_id in sorted(r for r, cnt in ctrl_run_counts.items() if cnt > 1):
lot_pos_ctrl_valid = False
conflicted_or_invalid_ctrl_run_ids.add(dup_run_id)
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"Duplicate/conflicting plate_or_run_id={dup_run_id!r} inside lot_positive_controls (count={ctrl_run_counts[dup_run_id]}); run invalidated prior to biological adjudication",
)
# Pass 1b: Validate every entry and separate candidate passing runs from invalid/conflicted runs
for lpc_idx, lpc_item in enumerate(lpc_entries):
if not isinstance(lpc_item, dict):
lot_pos_ctrl_valid = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"lot_positive_controls[{lpc_idx}] must be a dict (got {type(lpc_item).__name__})",
)
continue
entry_ok = True
lpc_lid = lpc_item.get("lot_id")
if type(lpc_lid) is not str or lpc_lid.strip() != lot_id:
entry_ok = False
lot_pos_ctrl_valid = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"lot_positive_controls[{lpc_idx}].lot_id={lpc_lid!r} does not match parent lot_id={lot_id!r}",
)
lpc_run = lpc_item.get("plate_or_run_id")
lpc_run_c = lpc_run.strip() if type(lpc_run) is str else ""
if not lpc_run_c:
entry_ok = False
lot_pos_ctrl_valid = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"lot_positive_controls[{lpc_idx}].plate_or_run_id must be a non-empty string (got {lpc_run!r})",
)
elif ctrl_run_counts.get(lpc_run_c, 0) > 1:
entry_ok = False
lpc_cd3, lpc_cd3_err = _check_xreact_domain_number(
lpc_item.get("cd3_pos_ctrl_pct"), f"{lot_id}.lot_positive_controls[{lpc_idx}].cd3_pos_ctrl_pct", lo=0.0, hi=100.0
)
if lpc_cd3_err is not None or lpc_cd3 is None:
entry_ok = False
lot_pos_ctrl_valid = False
_lot_escalate("HOLD_INVALID_OR_INCOMPLETE_ASSAY", lpc_cd3_err or "Invalid lot_positive_controls.cd3_pos_ctrl_pct")
elif lpc_cd3 < 90.0:
entry_ok = False
lot_pos_ctrl_valid = False
_lot_escalate(
"HOLD_INVALID_OR_INCOMPLETE_ASSAY",
f"Lot-specific CD3+ T-cell ATG positive control ({lpc_run_c or lpc_idx}) {lpc_cd3:.2f}% < 90.0% minimum lot activity threshold",
)
lpc_b6, lpc_b6_err = _check_xreact_domain_number(