OpenIslet

scripts/verify_iip0056_route4_omentum_and_exvivo_rest.py

OpenIslet Wave-6 Round-14 Prospective Candidate W6-C2 (Staged Slot IIP-0047 -> Provisional

script Release v0.8.0 gen-eb955f8266035aba 466 KB · 37 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_iip0056_route4_omentum_and_exvivo_rest.py · SHA-256: 25e12d578ba80b55…

        if min_bed_po2_d < Decimal("35"):
            fail_reasons.append("MIN_BILATERAL_BED_PO2_BELOW_35MMHG_ENVELOPE_FLOOR")

    if "measured_bed_po2_mmhg" in obs and obs["measured_bed_po2_mmhg"] is not None:
        leg_bed = obs["measured_bed_po2_mmhg"]
        if not _is_finite_number(leg_bed):
            fail_reasons.append("INVALID_MEASURED_BED_PO2_MMHG")
        else:
            leg_bed_d = _to_exact_decimal(leg_bed)
            if leg_bed_d <= ZERO_DEC:
                fail_reasons.append("INVALID_MEASURED_BED_PO2_MMHG")
            elif min_bed_po2_d is not None and leg_bed_d > min_bed_po2_d:
                fail_reasons.append("MEASURED_BED_PO2_EXCEEDS_MIN_BILATERAL_FACE_PO2_INCONSISTENT")
            elif min_bed_po2_d is not None:
                min_bed_po2_d = min(min_bed_po2_d, leg_bed_d)

    fibrin_um = obs.get("measured_fibrin_capsule_um")
    fibrin_d: Optional[Decimal] = None
    if fibrin_um is None:
        hold_reasons.append("MISSING_MEASURED_FIBRIN_CAPSULE_UM")
    elif not _is_finite_number(fibrin_um):
        fail_reasons.append("INVALID_MEASURED_FIBRIN_CAPSULE_UM")
    else:
        fibrin_d = _to_exact_decimal(fibrin_um)
        if fibrin_d < ZERO_DEC:
            fail_reasons.append("INVALID_MEASURED_FIBRIN_CAPSULE_UM")
            fibrin_d = None

    face_o2 = obs.get("measured_worst_face_o2_mmhg")
    face_o2_d: Optional[Decimal] = None
    if face_o2 is None:
        hold_reasons.append("MISSING_MEASURED_WORST_FACE_O2_MMHG")
    elif not _is_finite_number(face_o2):
        fail_reasons.append("INVALID_MEASURED_WORST_FACE_O2_MMHG")
    else:
        face_o2_d = _to_exact_decimal(face_o2)
        if face_o2_d < ZERO_DEC:
            fail_reasons.append("INVALID_MEASURED_WORST_FACE_O2_MMHG")
        elif face_o2_d < Decimal("15"):
            fail_reasons.append("MEASURED_WORST_FACE_O2_LT_15MMHG_FAIL")
        elif max_bed_po2_d is not None and face_o2_d > max_bed_po2_d:
            fail_reasons.append("MEASURED_WORST_FACE_O2_EXCEEDS_BED_PO2_INCONSISTENT")

    core_o2 = obs.get("measured_worst_core_o2_mmhg")
    core_o2_d: Optional[Decimal] = None
    if core_o2 is None:
        hold_reasons.append("MISSING_MEASURED_WORST_CORE_O2_MMHG")
    elif not _is_finite_number(core_o2):
        fail_reasons.append("INVALID_MEASURED_WORST_CORE_O2_MMHG")
    else:
        core_o2_d = _to_exact_decimal(core_o2)
        if core_o2_d < ZERO_DEC:
            fail_reasons.append("INVALID_MEASURED_WORST_CORE_O2_MMHG")
        elif core_o2_d < Decimal("5"):
            fail_reasons.append("MEASURED_WORST_CORE_O2_LT_5MMHG_FAIL")
        elif face_o2_d is not None and core_o2_d > face_o2_d:
            fail_reasons.append("MEASURED_WORST_CORE_O2_EXCEEDS_FACE_O2_INCONSISTENT")

    lot_ocr = obs.get("measured_lot_ocr_mult")
    if lot_ocr is None:
        hold_reasons.append("MISSING_MEASURED_LOT_OCR_MULT")
    elif not _is_finite_number(lot_ocr):
        fail_reasons.append("INVALID_MEASURED_LOT_OCR_MULT")
    else:
        lot_ocr_d = _to_exact_decimal(lot_ocr)
        if lot_ocr_d <= ZERO_DEC:
            fail_reasons.append("INVALID_MEASURED_LOT_OCR_MULT")
        elif lot_ocr_d > Decimal("1.097"):
            fail_reasons.append("MEASURED_LOT_OCR_MULT_EXCEEDS_1P097_FAIL")

    # 6B. Per-Leaf Measured Area/IEQ Reconciliation & Matrix Transport Coverage (Audit F4 / Review #6 / Audit R4-03 / Audit R12-04)
    scale_mode = obs.get("scale_mode")
    valid_scale_mode: Optional[str] = None
    if scale_mode is None:
        hold_reasons.append("MISSING_SCALE_MODE")
    elif not _is_literal_nonempty_str(scale_mode) or scale_mode not in ALLOWED_SCALE_MODES:
        fail_reasons.append("INVALID_SCALE_MODE")
    else:
        valid_scale_mode = scale_mode

    total_ieq_v = obs.get("total_implanted_ieq")
    valid_total_ieq_d: Optional[Decimal] = None
    if total_ieq_v is None:
        hold_reasons.append("MISSING_TOTAL_IMPLANTED_IEQ")
    elif not _is_finite_number(total_ieq_v):
        fail_reasons.append("INVALID_TOTAL_IMPLANTED_IEQ")
    else:
        t_ieq_d = _to_exact_decimal(total_ieq_v)
        if not (ONE_DEC <= t_ieq_d <= Decimal("1e9")):
            fail_reasons.append("INVALID_TOTAL_IMPLANTED_IEQ")
        else:
            valid_total_ieq_d = t_ieq_d

    total_area_v = obs.get("total_loaded_area_cm2")
    valid_total_area_d: Optional[Decimal] = None
    if total_area_v is None:
        hold_reasons.append("MISSING_TOTAL_LOADED_AREA_CM2")
    elif not _is_finite_number(total_area_v):
        fail_reasons.append("INVALID_TOTAL_LOADED_AREA_CM2")
    else:
        t_area_d = _to_exact_decimal(total_area_v)
        if not (Decimal("0.01") <= t_area_d <= Decimal("1e6")):
            fail_reasons.append("INVALID_TOTAL_LOADED_AREA_CM2")
        else:
            valid_total_area_d = t_area_d

    implied_total_loading_ratio: Optional[Tuple[Decimal, Decimal]] = None
    implied_total_loading: Optional[float] = None
    if valid_total_ieq_d is not None and valid_total_area_d is not None:
        implied_total_loading_ratio = (valid_total_ieq_d, valid_total_area_d)
        if loading_domain_ok and loading_d is not None and _ratio_rel_diff_exceeds(
            valid_total_ieq_d, valid_total_area_d, loading_d, Decimal("0.02")
        ):
            fail_reasons.append("IMPLIED_TOTAL_LOADING_CONTRADICTS_REPORTED_LOADING_GT_2PCT_FAIL")
        implied_total_loading = _dec_div_to_display_float(valid_total_ieq_d, valid_total_area_d)
        if implied_total_loading is None or not (implied_total_loading > 0.0):
            fail_reasons.append("INVALID_IMPLIED_TOTAL_LOADING_NONFINITE")

    leaves_raw = obs.get("per_leaf_records")
    leaf_loading_ratios: List[Tuple[Decimal, Decimal]] = []
    leaf_min_beds_d: List[Decimal] = []
    leaf_face_o2s_d: List[Decimal] = []
    leaf_core_o2s_d: List[Decimal] = []
    sum_leaf_ieq_d = ZERO_DEC
    sum_leaf_area_d = ZERO_DEC
    all_leaves_numeric_ok = True
    n_leaves_verified = 0

    if leaves_raw is None:
        hold_reasons.append("MISSING_PER_LEAF_RECORDS")
        all_leaves_numeric_ok = False
    elif not isinstance(leaves_raw, list) or len(leaves_raw) == 0:
        fail_reasons.append("INVALID_OR_EMPTY_PER_LEAF_RECORDS")
        all_leaves_numeric_ok = False
    else:
        if valid_scale_mode == "CLINICAL_HUMAN_SCALE" and len(leaves_raw) < 2:
            fail_reasons.append("CLINICAL_SCALE_REQUIRES_AT_LEAST_2_SEPARATED_LEAVES")
        # Review #6 / Audit R4-03: ANY >100,000 IEQ implant with <2 leaves ALWAYS fails regardless of scale_mode!
        if valid_total_ieq_d is not None and valid_total_ieq_d > SINGLE_LEAF_MAX_TOTAL_IEQ_DEC and len(leaves_raw) < 2:
            fail_reasons.append("TOTAL_IEQ_GT_100K_REQUIRES_AT_LEAST_2_SEPARATED_LEAVES")

        seen_leaf_ids: set[str] = set()
        for l_idx, leaf in enumerate(leaves_raw):
            if not isinstance(leaf, dict):
                fail_reasons.append(f"INVALID_PER_LEAF_RECORD:{l_idx}")
                all_leaves_numeric_ok = False
                continue
            missing_lf = False
            for fkey in REQUIRED_PER_LEAF_FIELDS:
                if leaf.get(fkey) is None:
                    hold_reasons.append(f"MISSING_PER_LEAF_FIELD:{fkey}")
                    missing_lf = True
            if missing_lf:
                all_leaves_numeric_ok = False
                continue

            lid = leaf["leaf_id"]
            if not _is_literal_nonempty_str(lid):
                fail_reasons.append("INVALID_LEAF_ID")
                all_leaves_numeric_ok = False
            elif lid in seen_leaf_ids:
                fail_reasons.append("DUPLICATE_LEAF_ID")
                all_leaves_numeric_ok = False
            else:
                seen_leaf_ids.add(lid)

            l_ieq = leaf["loaded_ieq"]
            l_area = leaf["measured_area_cm2"]
            l_h = leaf["measured_thickness_um"]
            l_d = leaf["measured_d_o2_matrix_m2_s"]
            l_face = leaf["measured_face_o2_mmhg"]
            l_core = leaf["measured_core_o2_mmhg"]
            l_bot = leaf["bed_face_bot_po2_mmhg"]
            l_top = leaf["bed_face_top_po2_mmhg"]
            l_cov = leaf["direct_o2_sensor_coverage_verified"]

            l_ieq_d: Optional[Decimal] = _to_exact_decimal(l_ieq) if _is_finite_number(l_ieq) else None
            l_area_d: Optional[Decimal] = _to_exact_decimal(l_area) if _is_finite_number(l_area) else None
            l_h_d: Optional[Decimal] = _to_exact_decimal(l_h) if _is_finite_number(l_h) else None
            l_d_dec: Optional[Decimal] = _to_exact_decimal(l_d) if _is_finite_number(l_d) else None
            l_face_d: Optional[Decimal] = _to_exact_decimal(l_face) if _is_finite_number(l_face) else None
            l_core_d: Optional[Decimal] = _to_exact_decimal(l_core) if _is_finite_number(l_core) else None
            l_bot_d: Optional[Decimal] = _to_exact_decimal(l_bot) if _is_finite_number(l_bot) else None
            l_top_d: Optional[Decimal] = _to_exact_decimal(l_top) if _is_finite_number(l_top) else None

            leaf_ok = True
            if l_ieq_d is None or not (ONE_DEC <= l_ieq_d <= Decimal("1e9")):
                fail_reasons.append("INVALID_LEAF_LOADED_IEQ")
                leaf_ok = False
            if l_area_d is None or not (Decimal("0.01") <= l_area_d <= Decimal("1e6")):
                fail_reasons.append("INVALID_LEAF_MEASURED_AREA_CM2")
                leaf_ok = False
            if l_h_d is None or not (Decimal("100") <= l_h_d <= Decimal("120")):
                fail_reasons.append("LEAF_MEASURED_THICKNESS_OUTSIDE_100_TO_120UM_FAIL")
                leaf_ok = False
            elif diam_domain_ok and zc_domain_ok and d_max_d is not None and z_c_d is not None:
                l_half_h = _dec_mul(Decimal("0.5"), l_h_d)
                l_outer = _dec_add(z_c_d, _dec_mul(Decimal("0.5"), d_max_d))
                l_wall_clear = _dec_sub(l_half_h, l_outer)
                if l_outer > l_half_h:
                    fail_reasons.append("LEAF_CLUSTER_PROTRUDES_OUTSIDE_SLAB_FAIL")
                elif l_wall_clear < MIN_GY028_WALL_CLEARANCE_UM_DEC:
                    fail_reasons.append("LEAF_GY028_WALL_CLEARANCE_LT_7P5UM_FAIL")

            if l_d_dec is None or l_d_dec <= ZERO_DEC or l_d_dec > D_O2_MAX_AQUEOUS_M2_S_DEC:
                fail_reasons.append("INVALID_LEAF_MEASURED_D_O2_MATRIX_M2_S")
                leaf_ok = False
            elif l_d_dec < D_O2_LOW_MODELED_M2_S_DEC:
                fail_reasons.append("LEAF_MEASURED_D_O2_MATRIX_BELOW_LOW_D_FLOOR_FAIL")
                leaf_ok = False

            if l_bot_d is None or l_bot_d <= ZERO_DEC:
                fail_reasons.append("INVALID_LEAF_BED_FACE_BOT_PO2_MMHG")
                leaf_ok = False
            if l_top_d is None or l_top_d <= ZERO_DEC:
                fail_reasons.append("INVALID_LEAF_BED_FACE_TOP_PO2_MMHG")
                leaf_ok = False

            if l_face_d is None or l_face_d < ZERO_DEC:
                fail_reasons.append("INVALID_LEAF_MEASURED_FACE_O2_MMHG")
                leaf_ok = False
            elif l_face_d < Decimal("15"):
                fail_reasons.append("LEAF_MEASURED_FACE_O2_LT_15MMHG_FAIL")
                leaf_ok = False

            if l_core_d is None or l_core_d < ZERO_DEC:
                fail_reasons.append("INVALID_LEAF_MEASURED_CORE_O2_MMHG")
                leaf_ok = False
            elif l_core_d < Decimal("5"):
                fail_reasons.append("LEAF_MEASURED_CORE_O2_LT_5MMHG_FAIL")
                leaf_ok = False

            if not _is_strict_bool(l_cov):
                fail_reasons.append("INVALID_LEAF_DIRECT_O2_SENSOR_COVERAGE_BOOL")
                leaf_ok = False
            elif not l_cov:
                fail_reasons.append("LEAF_DIRECT_O2_SENSOR_COVERAGE_UNVERIFIED_FAIL")
                leaf_ok = False

            if l_face_d is not None and l_core_d is not None and l_core_d > l_face_d:
                fail_reasons.append("LEAF_CORE_O2_EXCEEDS_FACE_O2_INCONSISTENT")
            if l_face_d is not None and l_bot_d is not None and l_top_d is not None and l_face_d > max(l_bot_d, l_top_d):
                fail_reasons.append("LEAF_FACE_O2_EXCEEDS_LEAF_BED_PO2_INCONSISTENT")

            if l_ieq_d is not None and l_ieq_d > ZERO_DEC and l_area_d is not None and l_area_d > ZERO_DEC:
                sum_leaf_ieq_d = _dec_add(sum_leaf_ieq_d, l_ieq_d)
                sum_leaf_area_d = _dec_add(sum_leaf_area_d, l_area_d)
                leaf_loading_ratios.append((l_ieq_d, l_area_d))
                if loading_domain_ok and loading_d is not None and _ratio_rel_diff_exceeds(
                    l_ieq_d, l_area_d, loading_d, Decimal("0.02")