): fail_reasons.append("PER_LEAF_LOADING_CONTRADICTS_REPORTED_LOADING_GT_2PCT_FAIL") l_load_disp = _dec_div_to_display_float(l_ieq_d, l_area_d) if l_load_disp is None: fail_reasons.append("INVALID_PER_LEAF_LOADING_NONFINITE") leaf_ok = False else: all_leaves_numeric_ok = False if l_bot_d is not None and l_bot_d > ZERO_DEC and l_top_d is not None and l_top_d > ZERO_DEC: leaf_min_beds_d.append(min(l_bot_d, l_top_d)) if l_face_d is not None and l_face_d >= ZERO_DEC: leaf_face_o2s_d.append(l_face_d) if l_core_d is not None and l_core_d >= ZERO_DEC: leaf_core_o2s_d.append(l_core_d) if leaf_ok: n_leaves_verified += 1 else: all_leaves_numeric_ok = False if all_leaves_numeric_ok and len(leaf_loading_ratios) == len(leaves_raw): if valid_total_ieq_d is not None and _dec_abs_diff_exceeds( sum_leaf_ieq_d, valid_total_ieq_d, _dec_mul(Decimal("0.01"), valid_total_ieq_d) ): fail_reasons.append("PER_LEAF_IEQ_SUM_DISCREPANCY_GT_1PCT_FAIL") if valid_total_area_d is not None and _dec_abs_diff_exceeds( sum_leaf_area_d, valid_total_area_d, _dec_mul(Decimal("0.01"), valid_total_area_d) ): fail_reasons.append("PER_LEAF_AREA_SUM_DISCREPANCY_GT_1PCT_FAIL") if leaf_face_o2s_d and face_o2_d is not None and face_o2_d > min(leaf_face_o2s_d): fail_reasons.append("REPORTED_WORST_FACE_O2_EXCEEDS_PER_LEAF_MIN_INCONSISTENT") if leaf_core_o2s_d and core_o2_d is not None and core_o2_d > min(leaf_core_o2s_d): fail_reasons.append("REPORTED_WORST_CORE_O2_EXCEEDS_PER_LEAF_MIN_INCONSISTENT") if leaf_min_beds_d and min_bed_po2_d is not None and min_bed_po2_d > min(leaf_min_beds_d): fail_reasons.append("REPORTED_BED_PO2_EXCEEDS_PER_LEAF_MIN_INCONSISTENT") # 6C. Actual Max-Leaf Steric Packing (Review #6 / Audit R4-03 / Audit R12-04): # Runs AFTER per-leaf reconciliation on EVERY leaf's measured loading AND effective_max_load # using exact polynomial Decimal inequalities (pi cancels: vol_1_ieq / v_cl = (150 / d_rms3)^3). computed_pitch_um: Optional[float] = None computed_min_dist_um: Optional[float] = None effective_max_load: Optional[float] = None if ( loading_domain_ok and diam_domain_ok and zc_domain_ok and loading_d is not None and d_rms3_d is not None and d_max_d is not None and z_c_d is not None ): packing_load_ratios: List[Tuple[Decimal, Decimal]] = [(loading_d, ONE_DEC)] if implied_total_loading_ratio is not None: imp_n, imp_d = implied_total_loading_ratio if _ratio_ge_dec(imp_n, imp_d, ONE_DEC) and _ratio_le_dec(imp_n, imp_d, Decimal("1e6")): packing_load_ratios.append(implied_total_loading_ratio) else: fail_reasons.append("IMPLIED_TOTAL_LOADING_OUT_OF_PACKING_DOMAIN") for l_num, l_den in leaf_loading_ratios: if _ratio_ge_dec(l_num, l_den, ONE_DEC) and _ratio_le_dec(l_num, l_den, Decimal("1e6")): packing_load_ratios.append((l_num, l_den)) else: fail_reasons.append("PER_LEAF_LOADING_OUT_OF_PACKING_DOMAIN") eff_max_num, eff_max_den = _max_ratio(packing_load_ratios) d_rms3_cubed = _dec_mul(_dec_mul(d_rms3_d, d_rms3_d), d_rms3_d) d_max_sq = _dec_mul(d_max_d, d_max_d) zc_sq = _dec_mul(z_c_d, z_c_d) pitch_overlap_fail = False cross_overlap_fail = False for p_num, p_den in packing_load_ratios: rhs_bound = _dec_mul(_dec_mul(Decimal("27"), p_num), d_max_sq) lhs_pitch = _dec_mul(_dec_mul(Decimal("1600"), p_den), d_rms3_cubed) if lhs_pitch <= rhs_bound: pitch_overlap_fail = True lhs_mind = _dec_add( _dec_mul(_dec_mul(Decimal("800"), p_den), d_rms3_cubed), _dec_mul(_dec_mul(Decimal("108"), p_num), zc_sq), ) if lhs_mind <= rhs_bound: cross_overlap_fail = True if pitch_overlap_fail: fail_reasons.append("STERIC_SAME_PLANE_PITCH_OVERLAP_FAIL") if cross_overlap_fail: fail_reasons.append("STERIC_CROSS_PLANE_OVERLAP_FAIL") # Display-only float conversion AFTER exact Decimal packing policy decisions effective_max_load = _dec_div_to_display_float(eff_max_num, eff_max_den) emax_pack = min(MAX_EMAX, max(999_999, abs(eff_max_num.adjusted()) + abs(eff_max_den.adjusted()) + 1000)) with localcontext(Context(prec=60, Emax=emax_pack, Emin=-emax_pack)): worst_pitch_sq = (Decimal("1600") * eff_max_den * d_rms3_cubed) / (Decimal("27") * eff_max_num) worst_pitch_d = worst_pitch_sq.sqrt() worst_mind_sq = worst_pitch_sq / Decimal("2") + Decimal("4") * zc_sq worst_mind_d = worst_mind_sq.sqrt() computed_pitch_um = _dec_to_display_float(worst_pitch_d) computed_min_dist_um = _dec_to_display_float(worst_mind_d) if computed_pitch_um is None or computed_min_dist_um is None: fail_reasons.append("UNRESOLVED_OR_NONFINITE_PACKING_METRICS_FAIL") # Monotone UNION of Tier-1 (load<=1200, min_bed>=35, fib<=10) OR Tier-2 (load<=1500, min_bed>=40, fib<=12) envelope_tier = "UNRESOLVED" tier_fibrin_max_d: Decimal = Decimal("10") max_leaf_loading: Optional[float] = None if leaf_loading_ratios: ml_num, ml_den = _max_ratio(leaf_loading_ratios) max_leaf_loading = _dec_div_to_display_float(ml_num, ml_den) if loading_domain_ok and loading_d is not None and min_bed_po2_d is not None and fibrin_d is not None and fibrin_d >= ZERO_DEC: eff_load_ratios: List[Tuple[Decimal, Decimal]] = [(loading_d, ONE_DEC)] if implied_total_loading_ratio is not None: eff_load_ratios.append(implied_total_loading_ratio) eff_load_ratios.extend(leaf_loading_ratios) lv_num, lv_den = _max_ratio(eff_load_ratios) bv_d = min([min_bed_po2_d] + leaf_min_beds_d) fv_d = fibrin_d if valid_site == "R4_OMENTUM_LEAD": tier1_ok = _ratio_le_dec(lv_num, lv_den, Decimal("1200")) and (bv_d >= Decimal("35")) and (fv_d <= Decimal("10")) tier2_ok = _ratio_le_dec(lv_num, lv_den, Decimal("1500")) and (bv_d >= Decimal("40")) and (fv_d <= Decimal("12")) tier_fibrin_max_d = Decimal("12") if (bv_d >= Decimal("40") and _ratio_le_dec(lv_num, lv_den, Decimal("1500"))) else Decimal("10") if tier1_ok and tier2_ok: envelope_tier = "R4O_TIER1_AND_TIER2_UNION" elif tier1_ok: envelope_tier = "R4O_TIER1_PRIMARY_LE1200" elif tier2_ok: envelope_tier = "R4O_TIER2_HIGH_PERFUSION_GATED_LE1500" else: added_tier_fail = False if _ratio_gt_dec(lv_num, lv_den, Decimal("1500")): fail_reasons.append("AREAL_LOADING_EXCEEDS_OMENTUM_ENVELOPE_CAP_1500_IEQ_CM2") added_tier_fail = True if bv_d < Decimal("35"): fail_reasons.append("MIN_BILATERAL_BED_PO2_BELOW_35MMHG_ENVELOPE_FLOOR") added_tier_fail = True if fv_d > Decimal("12"): fail_reasons.append("FIBRIN_CAPSULE_EXCEEDS_12UM_MAX_ENVELOPE_CEILING") added_tier_fail = True if _ratio_gt_dec(lv_num, lv_den, Decimal("1200")) and _ratio_le_dec(lv_num, lv_den, Decimal("1500")) and bv_d < Decimal("40"): fail_reasons.append("TIER2_LOADING_GT_1200_REQUIRES_MIN_BILATERAL_BED_PO2_GE_40MMHG") added_tier_fail = True if _ratio_le_dec(lv_num, lv_den, Decimal("1200")) and Decimal("35") <= bv_d < Decimal("40") and Decimal("10") < fv_d <= Decimal("12"): fail_reasons.append("TIER1_FIBRIN_CAPSULE_EXCEEDS_10UM_CEILING_AT_BED_LT_40MMHG") added_tier_fail = True if _ratio_gt_dec(lv_num, lv_den, loading_d): fail_reasons.append("IMPLIED_OR_LEAF_LOADING_EXCEEDS_ACTIVE_TIER_CEILING_FAIL") added_tier_fail = True if leaf_min_beds_d and min(leaf_min_beds_d) < (Decimal("40") if (_ratio_gt_dec(lv_num, lv_den, Decimal("1200")) or fv_d > Decimal("10")) else Decimal("35")): fail_reasons.append("LEAF_BED_PO2_BELOW_ACTIVE_TIER_FLOOR_FAIL") added_tier_fail = True if not added_tier_fail: fail_reasons.append("OUTSIDE_R4O_TIER1_OR_TIER2_UNION_ENVELOPE") elif valid_site == "R4_SARS_FREE_BACKUP": envelope_tier = "R4S_BACKUP_LE1200" tier_fibrin_max_d = Decimal("10") if _ratio_gt_dec(lv_num, lv_den, Decimal("1200")): fail_reasons.append("SARS_BACKUP_LOADING_EXCEEDS_1200_IEQ_CM2_ENVELOPE_CAP") if _ratio_gt_dec(lv_num, lv_den, loading_d): fail_reasons.append("IMPLIED_OR_LEAF_LOADING_EXCEEDS_ACTIVE_TIER_CEILING_FAIL") if bv_d < Decimal("35"): fail_reasons.append("SARS_BACKUP_MIN_BILATERAL_BED_PO2_BELOW_35MMHG_FAIL") if leaf_min_beds_d and min(leaf_min_beds_d) < Decimal("35"): fail_reasons.append("LEAF_BED_PO2_BELOW_ACTIVE_TIER_FLOOR_FAIL") if fv_d > Decimal("10"): fail_reasons.append("SARS_BACKUP_FIBRIN_CAPSULE_EXCEEDS_10UM_CEILING") r_clean_t50 = obs.get("route_specific_clean_t50_min") if r_clean_t50 is None: hold_reasons.append("MISSING_ROUTE_SPECIFIC_CLEAN_T50_MIN") elif not _is_finite_number(r_clean_t50): fail_reasons.append("R4O_03_CLEAN_T50_GE_2P60MIN_FAIL") else: r_clean_t50_d = _to_exact_decimal(r_clean_t50) if r_clean_t50_d <= ZERO_DEC or r_clean_t50_d >= Decimal("2.60"): fail_reasons.append("R4O_03_CLEAN_T50_GE_2P60MIN_FAIL") r_fib_t50 = obs.get("route_specific_fibrin_t50_min") if r_fib_t50 is None: hold_reasons.append("MISSING_ROUTE_SPECIFIC_FIBRIN_T50_MIN") elif not _is_finite_number(r_fib_t50): fail_reasons.append("R4O_03_FIBRIN_T50_GE_3P00MIN_FAIL") else: r_fib_t50_d = _to_exact_decimal(r_fib_t50) if r_fib_t50_d <= ZERO_DEC or r_fib_t50_d >= Decimal("3.00"): fail_reasons.append("R4O_03_FIBRIN_T50_GE_3P00MIN_FAIL") r_gsis = obs.get("route_specific_gsis_si") if r_gsis is None: hold_reasons.append("MISSING_ROUTE_SPECIFIC_GSIS_SI") elif not _is_finite_number(r_gsis) or _to_exact_decimal(r_gsis) < Decimal("3"): fail_reasons.append("R4O_03_GSIS_SI_LT_3P0_FAIL") # 7. Subject-Level Stage-1 Endpoints (Porcine R4O-04 + Primary DEC-009 0-NHP Primate-Free Pathway # or Conditional FDA CBER Exception NHP Cohort with Decoupled Paired C-Peptide Extinction vs II) large_animal_pathway: Optional[str] = None derived_porcine_n: Optional[int] = None derived_porcine_eligible_visit_n: Optional[int] = None derived_porcine_zero_complications: Optional[bool] = None derived_nhp_n: Optional[int] = None derived_nhp_eligible_visit_n: Optional[int] = None derived_nhp_zero_complications: Optional[bool] = None derived_nhp_eligible_diabetic_n: Optional[int] = None derived_nhp_ii: Optional[int] = None derived_nhp_cpeptide_extinction_verified_count: Optional[int] = None if is_promo: if valid_site == "R4_OMENTUM_LEAD": p_safe = obs.get("porcine_r4o04_zero_bowel_obstruction_torsion_grade3_adhesions") if p_safe is None: hold_reasons.append("MISSING_STAGE1_PORCINE_R4O04_SURGICAL_SAFETY") elif not _is_strict_bool(p_safe): fail_reasons.append("INVALID_PORCINE_SAFETY_BOOL") elif not p_safe: fail_reasons.append("R4O_04_PORCINE_SURGICAL_COMPLICATION_FAIL") p_subjs = obs.get("porcine_subjects") if p_subjs is None: hold_reasons.append("MISSING_STAGE1_PORCINE_SUBJECTS") elif not isinstance(p_subjs, list): fail_reasons.append("INVALID_PORCINE_SUBJECTS_ARRAY") else: derived_porcine_n = len(p_subjs) if derived_porcine_n < 6: fail_reasons.append("R4O_04_PORCINE_N_LT_6_FAIL") seen_p_ids: set[str] = set() p_visit_eligible_count = 0 p_missing_date_evidence = False p_any_complication = False p_all_complications_resolved = (derived_porcine_n > 0) for idx, s in enumerate(p_subjs): if not isinstance(s, dict): fail_reasons.append(f"INVALID_PORCINE_SUBJECT_RECORD:{idx}") p_all_complications_resolved = False continue for fkey in REQUIRED_PORCINE_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_PORCINE_SUBJECT_FIELD:{fkey}") sid = s.get("subject_id") p_sid_adverse = False if sid is not None: if not isinstance(sid, str) or len(sid.strip()) == 0: fail_reasons.append("INVALID_PORCINE_SUBJECT_ID") p_sid_adverse = True else: if not _is_canonical_id_str(sid):