l_t = u_t = act_l = act_u = float("nan") l_t = act_l = float("-inf"); u_t = act_u = float("inf") else: l_t, u_t = tb_lot[1], tb_lot[2] act_l, act_u = tb_act[1], tb_act[2] u_0b_ser.append(u_t) l_0b_ser.append(max(0.0, l_t)) act_u_0b_ser.append(act_u) act_l_0b_ser.append(max(0.0, act_l)) u95_0b_max = max(u95_0b_max, u_t) act_u95_0b_max = max(act_u95_0b_max, act_u) if 24.0 - 1e-9 <= hf_v <= 336.0 + 1e-9: l95_0b_min = min(l95_0b_min, l_t) act_l95_0b_min = min(act_l95_0b_min, act_l) if abs(hf_v - 336.0) <= 1e-6: d14_u95_0b = u_t act_d14_u95_0b = act_u if not tp0b_ok or hrs0b[0] > 6.0 + 1e-9 or not any(abs(h - 24.0) <= 1e-6 for h in hrs0b) or abs(hrs0b[-1] - 336.0) > 1e-6 or any((hrs0b[i] - hrs0b[i - 1]) > 72.0 + 1e-9 for i in range(1, len(hrs0b))): fails.append("FAIL_GATE_A0B_LOCAL_EXPOSURE_SCHEDULE_GAP_OR_MALFORMED"); a0b_ok = False elif zero_disp_0b_pk: holds.append("HOLD_GATE_A0B_LOCAL_PK_ZERO_DISPERSION_UNESTIMABLE"); a0b_ok = False else: env0b = _validate_pfd_local_pk_envelope( a0b_ev.get("a0b_local_pk_envelope"), hrs=hrs0b, u_ser=u_0b_ser, l_ser=l_0b_ser, exp_scope="bench_unedited", exp_comp="BENCH_LOCAL_PERI_GRAFT", tag_p="GATE_A0B", ) env0b_act = _validate_pfd_local_pk_envelope( a0b_ev.get("a0b_local_pk_envelope"), hrs=hrs0b, u_ser=act_u_0b_ser, l_ser=act_l_0b_ser, exp_scope="bench_unedited", exp_comp="BENCH_LOCAL_PERI_GRAFT", tag_p="GATE_A0B_ACTIVE_LOT", ) if env0b is not None else None if env0b is None or env0b_act is None: a0b_ok = False else: u95_0b_max = max(u95_0b_max, env0b[0]) l95_0b_min = min(l95_0b_min, env0b[1]) act_u95_0b_max = max(act_u95_0b_max, env0b_act[0]) act_l95_0b_min = min(act_l95_0b_min, env0b_act[1]) if act_u95_0b_max >= all4_min or (eff_floor_0b is not None and act_l95_0b_min < eff_floor_0b): fails.append("FAIL_GATE_A0B_ACTIVE_LOT_EXPOSURE_OUT_OF_WINDOW"); a0b_ok = False if max(u95_0b_max, act_u95_0b_max) >= all4_min or float(pk_u95) + 0.5 < max(u95_0b_max, act_u95_0b_max): fails.append("FAIL_GATE_A0B_LOCAL_TIMECOURSE_PEAK_BREACHES_CEILING_OR_CLAIMED"); a0b_ok = False if (eff_floor_0b is not None and min(l95_0b_min, act_l95_0b_min) < eff_floor_0b) or float(sust_lo) - 0.5 > min(l95_0b_min, act_l95_0b_min): fails.append("FAIL_GATE_A0B_LOCAL_TIMECOURSE_TROUGH_BELOW_IC50_OR_CLAIMED"); a0b_ok = False if d14_u95_0b is None or act_d14_u95_0b is None or float(d14_u95) + 0.5 < max(d14_u95_0b, act_d14_u95_0b): fails.append("FAIL_GATE_A0B_DAY14_LOCAL_U95_UNDERSTATES_TIMECOURSE"); a0b_ok = False def _validate_a1_elution_fields(ev: Mapping[str, Any], tag: str) -> bool: ok_el = True t50_d = ev.get("elution_t50_days") u14 = ev.get("day14_usable_delivery_fraction_pct") exh = ev.get("day35_depot_exhaustion_pct") res = ev.get("day35_residual_parent_pct") mr = ev.get("day35_terminal_mass_recovery_pct") d1_fr = ev.get("day1_intact_parent_free_uM") d14_fr = ev.get("day14_intact_parent_free_uM") d35_fr = ev.get("day35_intact_parent_free_uM") pk_mg = ev.get("peak_daily_eluted_mg") cl_l = ev.get("assumed_human_clearance_l_per_day") if not (is_strict_bool(ev.get("day14_elution_verified")) and ev["day14_elution_verified"]): fails.append(f"FAIL_{tag}_DAY14_ELUTION_NOT_VERIFIED"); ok_el = False if not (is_finite_num(t50_d) and 7.0 <= float(t50_d) <= 14.0): fails.append(f"FAIL_{tag}_ELUTION_T50_OUT_OF_7_14_DAYS"); ok_el = False if not (is_finite_num(u14) and 0.0 < float(u14) <= 85.0): fails.append(f"FAIL_{tag}_DAY14_USABLE_FRACTION_GT_85PCT"); ok_el = False if not (is_finite_num(exh) and is_finite_num(res) and float(exh) > 90.0 and 0.0 <= float(res) < 10.0 and abs((float(exh) + float(res)) - 100.0) <= 0.1): fails.append(f"FAIL_{tag}_DAY35_EXHAUSTION_OR_RESIDUAL_FAILED"); ok_el = False if not is_finite_num(mr) or not (90.0 <= float(mr) <= 110.0): fails.append(f"FAIL_{tag}_DAY35_MASS_RECOVERY_OUT_OF_90_110"); ok_el = False tr_a1 = _validate_pfd_release_trace_and_peak(ev, min_horizon_h=840.0, tag=tag) res_mg = ev.get("day35_residual_parent_mg") deg_mg = ev.get("day35_degradant_mass_mg") if tr_a1 is None: ok_el = False else: cmax_a1, tf_a1, cf_a1, lf_a1 = tr_a1 if cmax_a1 >= 0.20: ok_el = False # Reconcile Day-1 burst (<= 15% on BOTH A-1 traces!), post-Day-1-burst Day-14 usable fraction (IIP_DRAFT_A:117-122), # t50 crossing of 0.5 * cum_at_840h, strict Day-35 (840 h!) mass balance, & TRUE sliding 24-h cumulative release! cum24_a1 = _interp_cum_at_hour(tf_a1, cf_a1, 24.0) cum336_a1 = _interp_cum_at_hour(tf_a1, cf_a1, 336.0) cum840_a1 = _interp_cum_at_hour(tf_a1, cf_a1, 840.0) if cum24_a1 is None or cum24_a1 <= 0.0 or (100.0 * cum24_a1 / lf_a1) > 15.0 + 1e-9: fails.append(f"FAIL_{tag}_DAY1_BURST_EXCEEDS_15PCT_ON_A1_TRACE"); ok_el = False if cum336_a1 is None or cum24_a1 is None or cum336_a1 <= cum24_a1: fails.append(f"FAIL_{tag}_ZERO_OR_CONTRADICTORY_DAY14_RELEASE"); ok_el = False else: # Dossier IIP_DRAFT_A:117-122 definition: usable Day-14 delivery strictly AFTER Day-1 burst = (cum_336h - cum_24h) / load! recomp_u14 = 100.0 * (cum336_a1 - cum24_a1) / lf_a1 if recomp_u14 > 85.0 or (is_finite_num(u14) and abs(float(u14) - recomp_u14) > 0.5): fails.append(f"FAIL_{tag}_DAY14_USABLE_FRACTION_MISMATCH_WITH_TRACE"); ok_el = False # Reconcile t50 strictly against 0.5 * cum_at_840h: ENTIRE monotone crossing bracket [t50_lo_d, t50_hi_d] # must stay inside [7.0, 14.0] days (straddling bracket -> HOLD, outside -> FAIL)! if cum840_a1 is None or cum840_a1 <= 0.0: fails.append(f"FAIL_{tag}_MISSING_EXACT_DAY35_840H_CUMULATIVE"); ok_el = False else: half_rel = 0.5 * cum840_a1 t50_lo_d = t50_hi_d = None for idx_t in range(1, len(tf_a1)): if tf_a1[idx_t] > 840.0 + 1e-9: break if cf_a1[idx_t - 1] <= half_rel <= cf_a1[idx_t] and cf_a1[idx_t] > cf_a1[idx_t - 1]: t50_lo_d = tf_a1[idx_t - 1] / 24.0 t50_hi_d = tf_a1[idx_t] / 24.0 break if t50_lo_d is None or t50_hi_d is None or (is_finite_num(t50_d) and not (t50_lo_d - 1e-6 <= float(t50_d) <= t50_hi_d + 1e-6)): fails.append(f"FAIL_{tag}_T50_CONTRADICTS_RELEASE_TRACE"); ok_el = False elif t50_hi_d < 7.0 - 1e-9 or t50_lo_d > 14.0 + 1e-9: fails.append(f"FAIL_{tag}_T50_OUT_OF_7_14D_WINDOW"); ok_el = False elif t50_lo_d < 7.0 - 1e-9 or t50_hi_d > 14.0 + 1e-9: holds.append(f"HOLD_{tag}_T50_CROSSING_BRACKET_STRADDLES_7_TO_14D_WINDOW"); ok_el = False if not (is_finite_num(res_mg) and float(res_mg) >= 0.0 and is_finite_num(deg_mg) and float(deg_mg) >= 0.0): fails.append(f"FAIL_{tag}_MISSING_TERMINAL_RESIDUAL_OR_DEGRADANT_MASS_MG"); ok_el = False else: recomp_res_pct = 100.0 * float(res_mg) / lf_a1 recomp_exh_pct = 100.0 - recomp_res_pct recomp_mr_pct = 100.0 * (cum840_a1 + float(res_mg) + float(deg_mg)) / lf_a1 # Apply Day-35 advancement thresholds DIRECTLY to raw recomputed mass fractions (zero tolerance bypass!) if recomp_res_pct >= 10.0 or recomp_exh_pct <= 90.0: fails.append(f"FAIL_{tag}_RAW_RECOMPUTED_DAY35_EXHAUSTION_LE_90PCT"); ok_el = False if is_finite_num(res) and abs(float(res) - recomp_res_pct) > 0.5: fails.append(f"FAIL_{tag}_DAY35_RESIDUAL_MISMATCH_WITH_MASS_RECORD"); ok_el = False if is_finite_num(exh) and abs(float(exh) - recomp_exh_pct) > 0.5: fails.append(f"FAIL_{tag}_DAY35_EXHAUSTION_MISMATCH_WITH_MASS_RECORD"); ok_el = False if not (90.0 <= recomp_mr_pct <= 110.0) or (is_finite_num(mr) and abs(float(mr) - recomp_mr_pct) > 0.5): fails.append(f"FAIL_{tag}_TERMINAL_MASS_RECOVERY_MISMATCH_WITH_MASS_RECORD"); ok_el = False # TRUE sliding 24-hour cumulative release upper bound (sums multi-interval bursts within any 24-h window!) max_24h_mg = _recompute_max_24h_cumulative_mg(tf_a1, cf_a1) if is_finite_num(pk_mg) and float(pk_mg) + 1e-6 < max_24h_mg: fails.append(f"FAIL_{tag}_PEAK_DAILY_ELUTED_UNDERSTATES_TRACE"); ok_el = False if not all(is_finite_num(x) for x in (d1_fr, d14_fr, d35_fr, pk_mg, cl_l)): fails.append(f"FAIL_{tag}_MISSING_MEASURED_PARENT_OR_SYSTEMIC_ELUTION_DATA"); ok_el = False else: if float(d1_fr) <= 0.0 or float(d14_fr) <= 0.0 or float(d35_fr) < 0.0 or float(d1_fr) < float(d14_fr) - 1e-12: fails.append(f"FAIL_{tag}_INVALID_INTACT_PARENT_PROFILE"); ok_el = False eff_floor_a1 = ic50_u95_op if ic50_u95_op is not None else ic50_u95_val wash_floor_a1 = ic50_u95_wash_op if ic50_u95_wash_op is not None else ic50_u95_val if eff_floor_a1 is not None: if float(d14_fr) < eff_floor_a1: fails.append(f"FAIL_{tag}_DAY14_INTACT_PARENT_BELOW_IC50_U95"); ok_el = False if wash_floor_a1 is not None: if float(d35_fr) >= 0.10 * wash_floor_a1: fails.append(f"FAIL_{tag}_DAY35_INTACT_PARENT_NOT_EXHAUSTED"); ok_el = False if all4_ceiling_val is not None and float(d1_fr) >= all4_ceiling_val: fails.append(f"FAIL_{tag}_DAY1_INTACT_PARENT_EXCEEDS_CEILING"); ok_el = False # 1-compartment PK consistency: CL = (ln(2)/t_half)*24*Vd AND conservative joint corner CL_min = ln(2)/4.0*24*35.0 (~145.56 L/day)! th_v = ev.get("assumed_human_t_half_h") vd_v = ev.get("assumed_human_vd_l") cl_min_corner = (math.log(2.0) / 4.0) * 24.0 * 35.0 if not (100.0 <= float(cl_l) <= 500.0) or float(pk_mg) <= 0.0: fails.append(f"FAIL_{tag}_INVALID_CLEARANCE_OR_PEAK_ELUTED_MG"); ok_el = False else: if is_finite_num(th_v) and is_finite_num(vd_v) and float(th_v) > 0.0 and float(vd_v) > 0.0: cl_1c = (math.log(2.0) / float(th_v)) * 24.0 * float(vd_v) if abs(float(cl_l) - cl_1c) > max(2.0, 0.02 * cl_1c): fails.append(f"FAIL_{tag}_PK_CLEARANCE_INCONSISTENT_WITH_THALF_AND_VD"); ok_el = False eff_cl = min(float(cl_l), cl_min_corner) recomp_sys_ug_ml = float(pk_mg) / eff_cl if recomp_sys_ug_ml >= 0.20: fails.append(f"FAIL_{tag}_RECOMPUTED_SYSTEMIC_PLASMA_CAP_BREACH"); ok_el = False # Time-resolved local free-parent exposure (t0 <= 6 h..336 h..840 h) + continuous Lipschitz envelope! a1_tp = ev.get("a1_local_free_parent_timepoints") if a1_tp is None: holds.append(f"HOLD_{tag}_MISSING_LOCAL_FREE_PARENT_TIMEPOINTS"); ok_el = False elif not (isinstance(a1_tp, list) and len(a1_tp) >= 7): fails.append(f"FAIL_{tag}_INSUFFICIENT_LOCAL_FREE_PARENT_TIMEPOINTS"); ok_el = False else: tp1_ok = True hrs1: list[float] = [] u1_ser: list[float] = [] l1_ser: list[float] = [] act_u1_ser: list[float] = [] act_l1_ser: list[float] = [] u_act_max = 0.0 l_act_min = float("inf") act_lot_u_max = 0.0 act_lot_l_min = float("inf") d1_u = d14_l = d35_u = None act_d1_u = act_d14_l = act_d35_u = None alpha_a1 = 0.05 / float(4 * len(a1_tp)) zero_disp_a1 = False active_lot_norm_a1 = str(coat_id.get("coating_lot_id", "")).strip().upper() if coat_id is not None else "" a1_unit_keys = frozenset({"coating_lot_id", "independent_unit_id", "specimen_id", "technical_well_readings_uM", "unit_mean_uM"}) for r1 in a1_tp: if not isinstance(r1, Mapping) or set(r1.keys()) != {"hour_h", "independent_unit_records", "free_parent_uM_replicates"}: tp1_ok = False; break hh = r1.get("hour_h") urecs = r1.get("independent_unit_records") rr = r1.get("free_parent_uM_replicates") if not ( is_finite_num(hh) and float(hh) > (hrs1[-1] if hrs1 else 0.0) and isinstance(urecs, list) and len(urecs) >= 3 and isinstance(rr, list) and len(rr) == len(urecs) and all(is_finite_num(x) and float(x) >= 0.0 for x in rr) ): tp1_ok = False; break seen_u1: set[str] = set() seen_s1: set[str] = set() by_lot_a1: dict[str, list[float]] = {} u_means1: list[float] = [] for urow in urecs: if not isinstance(urow, Mapping) or set(urow.keys()) != a1_unit_keys: tp1_ok = False; break ulot = urow.get("coating_lot_id") uid = urow.get("independent_unit_id") spid = urow.get("specimen_id") tw = urow.get("technical_well_readings_uM") um = urow.get("unit_mean_uM") if not ( is_nonempty_str(ulot) and is_nonempty_str(uid) and is_nonempty_str(spid) and isinstance(tw, list) and len(tw) >= 1 and all(is_finite_num(x) and float(x) >= 0.0 for x in tw) and is_finite_num(um) and abs((sum(float(x) for x in tw) / len(tw)) - float(um)) <= 1e-4 ): tp1_ok = False; break un, sn, ln_u = str(uid).strip().upper(), str(spid).strip().upper(), str(ulot).strip().upper() if un in seen_u1 or sn in seen_s1: tp1_ok = False; break seen_u1.add(un); seen_s1.add(sn) by_lot_a1.setdefault(ln_u, []).append(float(um)) u_means1.append(float(um)) if ( not tp1_ok or len(by_lot_a1) < 3 or (active_lot_norm_a1 and len(by_lot_a1.get(active_lot_norm_a1, [])) < 3) or any(abs(u_means1[i] - float(rr[i])) > 1e-4 for i in range(len(u_means1))) ): tp1_ok = False; break hf_v = float(hh) hrs1.append(hf_v) lot_means1 = [sum(vals) / len(vals) for vals in by_lot_a1.values()] act_vals1 = by_lot_a1[active_lot_norm_a1] if active_lot_norm_a1 else lot_means1 tb_lot = sample_t_one_sided_bounds(lot_means1, alpha=alpha_a1) tb_act = sample_t_one_sided_bounds(act_vals1, alpha=alpha_a1) if tb_lot is None or tb_act is None: zero_disp_a1 = True l_t = act_l = float("-inf") u_t = act_u = float("inf") else: l_t, u_t = tb_lot[1], tb_lot[2] act_l, act_u = tb_act[1], tb_act[2] u1_ser.append(u_t) l1_ser.append(max(0.0, l_t)) act_u1_ser.append(act_u) act_l1_ser.append(max(0.0, act_l)) if hf_v <= 336.0 + 1e-9: u_act_max = max(u_act_max, u_t) act_lot_u_max = max(act_lot_u_max, act_u) if 24.0 - 1e-9 <= hf_v <= 336.0 + 1e-9: l_act_min = min(l_act_min, l_t) act_lot_l_min = min(act_lot_l_min, act_l) if abs(hf_v - 24.0) <= 1e-6: