OpenIslet

scripts/verify_iip0035_d1b_auto_and_portfolio.py

Deterministic Autologous D1b-Auto-C v2 Mode-B Registry, 45-Cell Stage-0 Contract,

script Release v0.8.0 gen-eb955f8266035aba 288 KB · 23 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0035_d1b_auto_and_portfolio.py · SHA-256: fedb990aeaedd116…

    # Fail closed if caller allocates Bonferroni alpha or declares an estimated bound while omitting the estimate itself!
    _a_eta_res = _safe_exact_decimal(resolved.get("alpha_eta_recovery")) if resolved.get("alpha_eta_recovery") is not None else None
    if sources.get("alpha_eta_recovery") != "default" and sources.get("eta_recovery_l95") == "default":
        if _a_eta_res is None or _a_eta_res > Decimal(0):
            hold_reason = hold_reason or "HOLD_MISSING_ETA_RECOVERY_ESTIMATE_FOR_ALPHA_ALLOCATION"
    if (
        resolved.get("f_eligible_provenance") == "INDEPENDENTLY_ESTIMATED_WITH_ALPHA_ALLOCATION"
        and sources.get("f_eligible") == "default"
    ):
        hold_reason = hold_reason or "HOLD_MISSING_F_ELIGIBLE_ESTIMATE_FOR_ALPHA_ALLOCATION"

    samp_flags = _reconcile_sampling_independence_flags(local_map, prefix=local_prefix, panel_map=panel_map)
    return {
        **resolved,
        **samp_flags,
        "calibration_hold_reason": hold_reason,
        "calibration_sources": sources,
    }


def _has_positive_gate0v_event(record: Any) -> bool:
    if not isinstance(record, dict):
        return False
    d28_k = record.get("day28_vp1_ie1_positive_k")
    return isinstance(d28_k, int) and not isinstance(d28_k, bool) and d28_k >= 1


def _inspect_adverse_gate0v_failure(
    record: Any,
    *,
    day14_min: Decimal = Decimal("3.0"),
    day14_l95_min: Decimal = Decimal("3.0"),
    early_72h_max: Decimal = Decimal("2.0"),
) -> str | None:
    """Detect explicit verdict=='FAIL' or numeric viral-clearance threshold failures on a Gate-0V record
    so adverse Gate-0V failures are preserved symmetrically regardless of whether a lot conflict occurs
    at the panel observation level or inside the nested Gate-0V record."""
    if not isinstance(record, dict):
        return None
    if record.get("gate0v_verdict") == "FAIL":
        return "FAIL_D1_L1B_GATE0V_CD46WT_VIRAL_CLEARANCE_SWITCH_TO_L1A_IL1RN"
    d14 = record.get("cvb_day14_log10_reduction")
    if d14 is not None and not (isinstance(d14, str) and d14 == "UNAVAILABLE"):
        d14_d = _safe_exact_decimal(d14)
        if d14_d is not None and Decimal("0") <= d14_d < day14_min:
            return "FAIL_D1_L1B_GATE0V_CD46WT_VIRAL_CLEARANCE_SWITCH_TO_L1A_IL1RN"
    d14_l95 = record.get("cvb_day14_log10_reduction_l95")
    if d14_l95 is not None and not (isinstance(d14_l95, str) and d14_l95 == "UNAVAILABLE"):
        d14_l95_d = _safe_exact_decimal(d14_l95)
        if d14_l95_d is not None and Decimal("0") <= d14_l95_d < day14_l95_min:
            return "FAIL_D1_L1B_GATE0V_DAY14_L95_BELOW_3LOG_SWITCH_TO_L1A_IL1RN"
    t72 = record.get("cvb4_72h_early_titer_log10_pfu_u95")
    if t72 is not None and not (isinstance(t72, str) and t72 == "UNAVAILABLE"):
        t72_d = _safe_exact_decimal(t72)
        if t72_d is not None and early_72h_max < t72_d <= Decimal("12.0"):
            return "FAIL_D1_L1B_GATE0V_EARLY_72H_TITER_VETO_SWITCH_TO_L1A_IL1RN"
    return None


def _evaluate_structured_lot_matched_gate0v_record(
    record: Any,
    *,
    evaluated_lot_id: Any,
    day14_min: Decimal = Decimal("3.0"),
    day14_l95_min: Decimal = Decimal("3.0"),
    early_72h_max: Decimal = Decimal("2.0"),
    day28_u95_max: Decimal = Decimal("0.0005"),
    day28_n_min: int = 5990,
    panel_observations: Dict[str, Any] | None = None,
) -> Dict[str, Any]:
    """Validate structured lot-matched global_gate0v_record (NEVER accepts a bare caller boolean).

    Preserves unconditional FAIL_HARD_STOP precedence for ANY recorded positive Day-28 VP1/IE1 event (k >= 1)
    even when lot metadata is missing, malformed, or mismatched (recording both the hard stop and lot hold!).
    """
    pos_event = _has_positive_gate0v_event(record)
    lot_hold: str | None = None
    if not _is_canonical_identifier(evaluated_lot_id):
        lot_hold = "HOLD_D1_INVALID_OR_MISSING_EVALUATED_LOT_ID"
    elif record is None:
        return {
            "cleared": False,
            "failed": False,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "HOLD_D1_L1B_STRUCTURED_LOT_MATCHED_GLOBAL_GATE0V_RECORD_REQUIRED",
        }
    elif isinstance(record, bool) or not isinstance(record, dict):
        return {
            "cleared": False,
            "failed": False,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "HOLD_D1_L1B_BARE_BOOLEAN_OR_NON_DICT_GATE0V_REJECTED",
        }
    else:
        mcb_lot_id = record.get("mcb_lot_id")
        if not _is_canonical_identifier(mcb_lot_id):
            lot_hold = "HOLD_D1_L1B_GATE0V_MISSING_OR_NONCANONICAL_MCB_LOT_ID"
        else:
            _nested_lot_vals: List[str] = []
            _nested_lot_bad = False
            for _nlk in _LOT_ALIAS_KEYS:
                if _nlk not in record:
                    continue
                _nlv = record[_nlk]
                if _nlv is None:
                    _nested_lot_bad = True
                elif not _is_canonical_identifier(_nlv):
                    _nested_lot_bad = True
                elif _nlv not in _nested_lot_vals:
                    _nested_lot_vals.append(_nlv)
            if _nested_lot_bad:
                lot_hold = "HOLD_D1_L1B_GATE0V_MISSING_OR_NONCANONICAL_MCB_LOT_ID"
            elif len(_nested_lot_vals) > 1:
                lot_hold = "HOLD_D1_CONFLICTING_LOT_ID_BINDING"
            elif mcb_lot_id != evaluated_lot_id:
                lot_hold = "HOLD_D1_L1B_GATE0V_MCB_LOT_ID_MISMATCH"

    if lot_hold is not None:
        if pos_event:
            return {
                "cleared": False,
                "failed": True,
                "hard_stop": True,
                "lot_binding_invalid": True,
                "reason": "FAIL_HARD_STOP_D1_L1B_GATE0V_DAY28_VP1_IE1_POSITIVE_EVENT_SWITCH_TO_L1A_IL1RN",
                "lot_hold_reason": lot_hold,
                "adverse_event_pending_lot_attribution": True,
            }
        # Preserve explicit verdict=='FAIL' or numeric viral-clearance failure alongside nested lot-attribution hold!
        _adv_fail_reason = _inspect_adverse_gate0v_failure(
            record, day14_min=day14_min, day14_l95_min=day14_l95_min, early_72h_max=early_72h_max,
        )
        return {
            "cleared": False,
            "failed": bool(_adv_fail_reason is not None),
            "hard_stop": False,
            "lot_binding_invalid": True,
            "reason": _adv_fail_reason if _adv_fail_reason is not None else lot_hold,
            "lot_hold_reason": lot_hold,
        }
    mcb_lot_id = record.get("mcb_lot_id")

    d14 = record.get("cvb_day14_log10_reduction")
    d14_l95 = record.get("cvb_day14_log10_reduction_l95")
    t72_u95 = record.get("cvb4_72h_early_titer_log10_pfu_u95")
    d28_k = record.get("day28_vp1_ie1_positive_k")
    d28_n = record.get("day28_vp1_ie1_cells_scored_n")
    verdict = record.get("gate0v_verdict")

    # 1. Unconditional FAIL_HARD_STOP precedence for ANY recorded positive Day-28 VP1/IE1 event (k >= 1),
    #    even if Day-14 reduction, L95, 72h early titer, n, or gate0v_verdict is missing/PENDING.
    if isinstance(d28_k, int) and not isinstance(d28_k, bool) and d28_k >= 1:
        return {
            "cleared": False,
            "failed": True,
            "hard_stop": True,
            "lot_binding_invalid": False,
            "reason": "FAIL_HARD_STOP_D1_L1B_GATE0V_DAY28_VP1_IE1_POSITIVE_EVENT_SWITCH_TO_L1A_IL1RN",
        }
    if verdict == "FAIL":
        return {
            "cleared": False,
            "failed": True,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "FAIL_D1_L1B_GATE0V_CD46WT_VIRAL_CLEARANCE_SWITCH_TO_L1A_IL1RN",
        }
    d14_present = not (d14 is None or (isinstance(d14, str) and d14 == "UNAVAILABLE"))
    d14_l95_present = not (d14_l95 is None or (isinstance(d14_l95, str) and d14_l95 == "UNAVAILABLE"))
    t72_present = not (t72_u95 is None or (isinstance(t72_u95, str) and t72_u95 == "UNAVAILABLE"))
    d28_k_present = not (d28_k is None or (isinstance(d28_k, str) and d28_k == "UNAVAILABLE"))
    d28_n_present = not (d28_n is None or (isinstance(d28_n, str) and d28_n == "UNAVAILABLE"))
    d14_d = _safe_exact_decimal(d14) if d14_present else None
    d14_l95_d = _safe_exact_decimal(d14_l95) if d14_l95_present else None
    t72_u95_d = _safe_exact_decimal(t72_u95) if t72_present else None
    # Reject invalid / out-of-range numeric fields BEFORE returning incomplete HOLD for missing sibling fields!
    if (
        (d14_present and (d14_d is None or not (Decimal("0") <= d14_d <= Decimal("12.0"))))
        or (d14_l95_present and (d14_l95_d is None or not (Decimal("0") <= d14_l95_d <= Decimal("12.0"))))
        or (d14_d is not None and d14_l95_d is not None and d14_l95_d > d14_d)
        or (t72_present and (t72_u95_d is None or not (Decimal("0") <= t72_u95_d <= Decimal("12.0"))))
        or (d28_k_present and (isinstance(d28_k, bool) or not isinstance(d28_k, int) or d28_k < 0))
        or (d28_n_present and (isinstance(d28_n, bool) or not isinstance(d28_n, int) or d28_n < 1))
        or (d28_k_present and d28_n_present and d28_k > d28_n)
    ):
        return {
            "cleared": False,
            "failed": True,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "FAIL_D1_GLOBAL_GATE0V_INVALID_OR_OUT_OF_RANGE",
        }
    if d14_d is not None and Decimal("0") <= d14_d < day14_min:
        return {
            "cleared": False,
            "failed": True,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "FAIL_D1_L1B_GATE0V_CD46WT_VIRAL_CLEARANCE_SWITCH_TO_L1A_IL1RN",
        }
    if d14_l95_d is not None and Decimal("0") <= d14_l95_d < day14_l95_min:
        return {
            "cleared": False,
            "failed": True,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "FAIL_D1_L1B_GATE0V_DAY14_L95_BELOW_3LOG_SWITCH_TO_L1A_IL1RN",
        }
    if t72_u95_d is not None and early_72h_max < t72_u95_d <= Decimal("12.0"):
        return {
            "cleared": False,
            "failed": True,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "FAIL_D1_L1B_GATE0V_EARLY_72H_TITER_VETO_SWITCH_TO_L1A_IL1RN",
        }

    if any(
        v is None or (isinstance(v, str) and v == "UNAVAILABLE")
        for v in (d14, d14_l95, t72_u95, d28_k, d28_n, verdict)
    ):
        return {
            "cleared": False,
            "failed": False,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "HOLD_D1_L1B_GATE0V_INCOMPLETE_RECORD",
        }
    if (
        d14_d is None or not (Decimal("0") <= d14_d <= Decimal("12.0"))
        or d14_l95_d is None or not (Decimal("0") <= d14_l95_d <= Decimal("12.0"))
        or d14_l95_d > d14_d
        or t72_u95_d is None or not (Decimal("0") <= t72_u95_d <= Decimal("12.0"))
    ):
        return {
            "cleared": False,
            "failed": True,
            "hard_stop": False,
            "lot_binding_invalid": False,
            "reason": "FAIL_D1_GLOBAL_GATE0V_INVALID_OR_OUT_OF_RANGE",
        }
    if not (isinstance(d28_k, int) and not isinstance(d28_k, bool)) or not (isinstance(d28_n, int) and not isinstance(d28_n, bool)):
        return {
            "cleared": False,
            "failed": True,
            "hard_stop": False,
            "lot_binding_invalid": False,