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…

                                fail_reasons.append("INVALID_PORCINE_SUBJECT_ID")
                                p_sid_adverse = True
                            sid_canon = _canonical_id_dedup_key(sid)
                            if sid_canon in seen_p_ids:
                                fail_reasons.append("DUPLICATE_PORCINE_SUBJECT_ID")
                                p_sid_adverse = True
                            else:
                                seen_p_ids.add(sid_canon)

                    sp = s.get("species")
                    if sp is not None and (not _is_literal_nonempty_str(sp) or sp != "PORCINE"):
                        fail_reasons.append("R4O_04_PORCINE_SUBJECT_WRONG_SPECIES_FAIL")

                    lot_v = s.get("lot_id")
                    if lot_v is not None and (not _is_literal_nonempty_str(lot_v) or (valid_lot_id is not None and lot_v != valid_lot_id)):
                        fail_reasons.append("R4O_04_PORCINE_SUBJECT_LOT_ID_MISMATCH")

                    # Subject-level measurement dates (Audit R5-04 / R8-05): exact Decimal followup >= 60, capsule & retrieval in [60, followup] (ZERO ungoverned tolerance)
                    p_fu = s.get("followup_duration_days")
                    p_fu_val: Optional[Decimal] = None
                    p_fu_ok = False
                    if p_fu is None:
                        p_missing_date_evidence = True
                    elif not _is_finite_number(p_fu) or _to_exact_decimal(p_fu) <= Decimal("0"):
                        fail_reasons.append("INVALID_PORCINE_FOLLOWUP_DURATION_DAYS")
                    else:
                        p_fu_val = _to_exact_decimal(p_fu)
                        if p_fu_val < PORCINE_MIN_FOLLOWUP_DAYS_DEC:
                            fail_reasons.append("R4O_04_PORCINE_FOLLOWUP_LT_60D_FAIL")
                        else:
                            p_fu_ok = True

                    p_cap_d = s.get("capsule_sampling_day")
                    p_cap_d_val: Optional[Decimal] = None
                    p_cap_d_ok = False
                    if p_cap_d is None:
                        p_missing_date_evidence = True
                    elif not _is_finite_number(p_cap_d) or _to_exact_decimal(p_cap_d) <= Decimal("0"):
                        fail_reasons.append("INVALID_PORCINE_CAPSULE_SAMPLING_DAY")
                    else:
                        p_cap_d_val = _to_exact_decimal(p_cap_d)
                        cap_ok_local = True
                        if p_cap_d_val < PORCINE_MIN_FOLLOWUP_DAYS_DEC:
                            fail_reasons.append("R4O_04_PORCINE_CAPSULE_SAMPLING_DAY_LT_60D_FAIL")
                            cap_ok_local = False
                        if p_fu_val is not None and p_cap_d_val > p_fu_val:
                            fail_reasons.append("R4O_04_PORCINE_CAPSULE_SAMPLING_DAY_EXCEEDS_FOLLOWUP_FAIL")
                            cap_ok_local = False
                        p_cap_d_ok = cap_ok_local

                    p_ret_d = s.get("retrieval_assessment_day")
                    p_ret_d_val: Optional[Decimal] = None
                    p_ret_d_ok = False
                    if p_ret_d is None:
                        p_missing_date_evidence = True
                    elif not _is_finite_number(p_ret_d) or _to_exact_decimal(p_ret_d) <= Decimal("0"):
                        fail_reasons.append("INVALID_PORCINE_RETRIEVAL_ASSESSMENT_DAY")
                    else:
                        p_ret_d_val = _to_exact_decimal(p_ret_d)
                        ret_ok_local = True
                        if p_ret_d_val < PORCINE_MIN_FOLLOWUP_DAYS_DEC:
                            fail_reasons.append("R4O_04_PORCINE_RETRIEVAL_ASSESSMENT_DAY_LT_60D_FAIL")
                            ret_ok_local = False
                        if p_fu_val is not None and p_ret_d_val > p_fu_val:
                            fail_reasons.append("R4O_04_PORCINE_RETRIEVAL_ASSESSMENT_DAY_EXCEEDS_FOLLOWUP_FAIL")
                            ret_ok_local = False
                        p_ret_d_ok = ret_ok_local

                    p_date_alias_adverse = _check_subject_date_aliases(
                        s,
                        gate_tag="R4O_04",
                        species_tag="PORCINE",
                        min_days=PORCINE_MIN_FOLLOWUP_DAYS_DEC,
                        min_days_tag="60D",
                        followup_val=p_fu_val,
                        capsule_day_val=p_cap_d_val,
                        retrieval_day_val=p_ret_d_val,
                        omentectomy_day_val=None,
                        cpep_day_val=None,
                        portal_day_val=None,
                        fail_reasons=fail_reasons,
                    )

                    if (not p_sid_adverse) and p_fu_ok and p_cap_d_ok and p_ret_d_ok and not p_date_alias_adverse:
                        p_visit_eligible_count += 1

                    p_comp_resolved, p_comp_adverse = _check_subject_typed_complications_and_aliases(
                        s,
                        gate_tag="R4O_04",
                        species_tag="PORCINE",
                        fail_reasons=fail_reasons,
                        hold_reasons=hold_reasons,
                    )
                    if p_comp_adverse:
                        p_any_complication = True
                    if not p_comp_resolved:
                        p_all_complications_resolved = False

                    surv_v = s.get("survival_to_endpoint")
                    if surv_v is not None and (not _is_strict_bool(surv_v) or not surv_v):
                        fail_reasons.append("R4O_04_PORCINE_SURVIVAL_TO_ENDPOINT_FAILED")
                    hem_v = s.get("major_hemorrhage_or_dehiscence")
                    if hem_v is not None and (not _is_strict_bool(hem_v) or hem_v):
                        fail_reasons.append("R4O_04_PORCINE_MAJOR_HEMORRHAGE_OR_DEHISCENCE_FAIL")
                    ssafe_v = s.get("surgical_safety_pass")
                    if ssafe_v is not None and (not _is_strict_bool(ssafe_v) or not ssafe_v):
                        fail_reasons.append("R4O_04_PORCINE_SUBJECT_SURGICAL_SAFETY_FAILED")
                    pcap_v = s.get("month3_capsule_um")
                    if pcap_v is not None:
                        if not _is_finite_number(pcap_v):
                            fail_reasons.append("INVALID_PORCINE_MONTH3_CAPSULE_UM")
                        else:
                            pcap_d = _to_exact_decimal(pcap_v)
                            if pcap_d < ZERO_DEC:
                                fail_reasons.append("INVALID_PORCINE_MONTH3_CAPSULE_UM")
                            elif pcap_d > tier_fibrin_max_d:
                                fail_reasons.append("R4O_04_PORCINE_MONTH3_CAPSULE_EXCEEDS_TIER_MAX_FAIL")
                    ploc_v = s.get("localization_verified")
                    if ploc_v is not None and (not _is_strict_bool(ploc_v) or not ploc_v):
                        fail_reasons.append("R4O_04_RESIDUAL_LOCALIZATION_FAILED")
                    _check_retrieval_residual_reconciliation(
                        prefix="PORCINE",
                        retrieval_mass_pct=s.get("retrieval_mass_pct"),
                        total_implanted_cells=s.get("total_implanted_cells"),
                        residual_viable_cells_measured=s.get("residual_viable_cells_measured"),
                        residual_sampling_fraction=s.get("residual_sampling_fraction"),
                        require_ge_99p9=True,
                        fail_reasons=fail_reasons,
                    )

                derived_porcine_eligible_visit_n = p_visit_eligible_count
                if derived_porcine_n >= 6 and not p_missing_date_evidence and p_visit_eligible_count < 6:
                    fail_reasons.append("R4O_04_PORCINE_VISIT_ELIGIBLE_N_LT_6_FAIL")

                if p_any_complication:
                    derived_porcine_zero_complications = False
                elif p_all_complications_resolved:
                    derived_porcine_zero_complications = True
                else:
                    derived_porcine_zero_complications = None

                if p_safe is True and derived_porcine_zero_complications is False:
                    fail_reasons.append("PORCINE_COMPLICATION_CONTRADICTS_AGGREGATE_ATTESTATION")

        dec009_pf = obs.get("dec009_primate_free_pathway")
        if dec009_pf is not None and not _is_strict_bool(dec009_pf):
            fail_reasons.append("INVALID_DEC009_PRIMATE_FREE_PATHWAY_BOOL")
        use_dec009_primate_free = (dec009_pf is True)

        dec009_nhp_exc = obs.get("dec009_fda_cber_nhp_exception_authorized")
        if dec009_nhp_exc is not None and not _is_strict_bool(dec009_nhp_exc):
            fail_reasons.append("INVALID_DEC009_FDA_CBER_NHP_EXCEPTION_BOOL")

        n_subjs = obs.get("nhp_subjects")
        if use_dec009_primate_free:
            large_animal_pathway = "DEC009_0_NHP_PRIMATE_FREE_PRIMARY"
            for p_key, miss_code, fail_code in (
                ("dec009_pillar1_humanized_mouse_efficacy_pass", "MISSING_DEC009_PILLAR1_HUMANIZED_MOUSE_EVIDENCE", "DEC009_PILLAR1_HUMANIZED_MOUSE_FAILED"),
                ("dec009_pillar2_chandler_loop_50donor_pass", "MISSING_DEC009_PILLAR2_CHANDLER_LOOP_EVIDENCE", "DEC009_PILLAR2_CHANDLER_LOOP_FAILED"),
                ("dec009_pillar1_glp_biodistribution_erasure_pass", "MISSING_DEC009_PILLAR1_GLP_ERASURE_EVIDENCE", "DEC009_PILLAR1_GLP_ERASURE_FAILED"),
            ):
                pv = obs.get(p_key)
                if pv is None:
                    hold_reasons.append(miss_code)
                elif not _is_strict_bool(pv) or not pv:
                    fail_reasons.append(fail_code)
            if n_subjs is not None and dec009_nhp_exc is not True:
                fail_reasons.append("DEC009_PRIMATE_FREE_PATHWAY_CONTRADICTS_NHP_COHORT_WITHOUT_FDA_EXCEPTION")
        else:
            large_animal_pathway = "DEC009_FDA_CBER_EXCEPTION_NHP_COHORT"
            if dec009_nhp_exc is False:
                fail_reasons.append("DEC009_UNAUTHORIZED_NHP_COHORT_REJECTED")
            elif dec009_nhp_exc is None and n_subjs is not None:
                hold_reasons.append("MISSING_DEC009_FDA_CBER_NHP_EXCEPTION_AUTHORIZATION")
            if n_subjs is None:
                hold_reasons.append("MISSING_STAGE1_NHP_SUBJECTS")

        if n_subjs is not None and not isinstance(n_subjs, list):
            fail_reasons.append("INVALID_NHP_SUBJECTS_ARRAY")
        elif isinstance(n_subjs, list):
            derived_nhp_n = len(n_subjs)
            if derived_nhp_n < 4:
                fail_reasons.append("R4O_05_NHP_N_LT_4_FAIL")
            seen_n_ids: set[str] = set()
            seen_n_proc_ids: set[str] = set()
            eligible_diabetic_count = 0
            pending_diabetic_eligibility_count = 0
            qual_count = 0
            cpep_extinction_verified_count = 0
            pending_responder_qual_count = 0
            n_visit_eligible_count = 0
            n_any_complication = False
            n_all_complications_resolved = (derived_nhp_n > 0)
            for idx, s in enumerate(n_subjs):
                if not isinstance(s, dict):
                    fail_reasons.append(f"INVALID_NHP_SUBJECT_RECORD:{idx}")
                    n_all_complications_resolved = False
                    continue

                for fkey in REQUIRED_NHP_SUBJECT_FIELDS:
                    if s.get(fkey) is None and fkey not in (
                        "bowel_obstruction_event",
                        "omental_torsion_event",
                        "grade3_adhesion_event",
                        "monitored_complications",
                    ):
                        hold_reasons.append(f"MISSING_NHP_SUBJECT_FIELD:{fkey}")

                sid = s.get("subject_id")
                sid_ok = False
                sid_adverse = False
                if sid is not None:
                    if not isinstance(sid, str) or len(sid.strip()) == 0:
                        fail_reasons.append("INVALID_NHP_SUBJECT_ID")
                        sid_adverse = True
                    else:
                        sid_malformed = not _is_canonical_id_str(sid)
                        if sid_malformed:
                            fail_reasons.append("INVALID_NHP_SUBJECT_ID")
                            sid_adverse = True
                        sid_canon = _canonical_id_dedup_key(sid)
                        if sid_canon in seen_n_ids:
                            fail_reasons.append("DUPLICATE_NHP_SUBJECT_ID")
                            sid_adverse = True
                        else:
                            seen_n_ids.add(sid_canon)
                            if not sid_malformed:
                                sid_ok = True

                sp = s.get("species")
                sp_ok = False
                if sp is not None:
                    sp_ok = _is_literal_nonempty_str(sp) and sp == "NHP"
                    if not sp_ok:
                        fail_reasons.append("R4O_05_NHP_SUBJECT_WRONG_SPECIES_FAIL")

                lot_v = s.get("lot_id")
                lot_ok = False
                if lot_v is not None:
                    lot_ok = _is_literal_nonempty_str(lot_v) and (valid_lot_id is not None and lot_v == valid_lot_id)
                    if not lot_ok:
                        fail_reasons.append("R4O_05_NHP_SUBJECT_LOT_ID_MISMATCH")

                pre_diab = s.get("preimplant_diabetic_verified")
                base_ins = s.get("baseline_exogenous_insulin_u_kg_d")
                base_cpep = s.get("baseline_fasting_c_peptide_ng_ml")
                dose_v = s.get("dose_ieq_per_kg")
                achieved_ii = s.get("achieved_insulin_independence")
                followup_d = s.get("followup_duration_days")
                cap_d = s.get("capsule_sampling_day")