env_allowed = { "k_elim_local_max_per_h", "k_rise_local_max_per_h", "k_elim_parent_max_per_h", "k_rise_parent_max_per_h", "k_elim_bmt_max_per_h", "k_rise_bmt_max_per_h", "k_elim_sys_max_per_h", "k_rise_sys_max_per_h", "efficacy_window_start_h", "envelope_model_sha256", "coating_article_id", "evidence_scope", "compartment_id", "envelope_qualification_method", "pilot_sampling_max_gap_h", "burst_phase_covered", "washout_phase_covered", "source_study_ids", "envelope_receipt_sha256", } if not isinstance(env_raw, Mapping) or set(env_raw.keys()) != env_allowed: fails.append(f"FAIL_{prefix}_MISSING_QUANTITATIVE_INTERSAMPLE_ENVELOPE"); return None ke_l = env_raw.get("k_elim_local_max_per_h") kr_l = env_raw.get("k_rise_local_max_per_h") ke_p = env_raw.get("k_elim_parent_max_per_h") kr_p = env_raw.get("k_rise_parent_max_per_h") ke_b = env_raw.get("k_elim_bmt_max_per_h") kr_b = env_raw.get("k_rise_bmt_max_per_h") ke_s = env_raw.get("k_elim_sys_max_per_h") kr_s = env_raw.get("k_rise_sys_max_per_h") t_eff0 = env_raw.get("efficacy_window_start_h") sha = env_raw.get("envelope_model_sha256") r_sha = env_raw.get("envelope_receipt_sha256") s_ids = env_raw.get("source_study_ids") e_art = env_raw.get("coating_article_id") e_scp = env_raw.get("evidence_scope") e_cmp = env_raw.get("compartment_id") e_mth = env_raw.get("envelope_qualification_method") e_gap = env_raw.get("pilot_sampling_max_gap_h") scope_bind_ok = ( is_nonempty_str(e_art) and (expected_coating_article_id is None or e_art == expected_coating_article_id) and e_scp in ("bench_unedited", "exact_product") and (expected_evidence_scope is None or e_scp == expected_evidence_scope) and e_cmp in ("BENCH_ELUTION_RECIRCULATION", "INVIVO_PERIGRAFT_AND_SYSTEMIC") and (expected_compartment_id is None or e_cmp == expected_compartment_id) and e_mth in ("DENSE_PILOT_TIMECOURSE_BOUND", "PHYSICAL_DEPOT_MASS_BALANCE_UPPER_BOUND") and is_finite_num(e_gap) and 0.0 < float(e_gap) <= 2.0 ) if not scope_bind_ok: fails.append(f"FAIL_{prefix}_ENVELOPE_ARTICLE_PHASE_OR_COMPARTMENT_MISMATCH"); return None if not ( is_finite_num(ke_l) and 0.0001 <= float(ke_l) <= 1.0 and is_finite_num(kr_l) and 0.0001 <= float(kr_l) <= 1.0 and is_finite_num(ke_p) and 0.0001 <= float(ke_p) <= 1.0 and is_finite_num(kr_p) and 0.0001 <= float(kr_p) <= 1.0 and is_finite_num(ke_b) and 0.0001 <= float(ke_b) <= 1.0 and is_finite_num(kr_b) and 0.0001 <= float(kr_b) <= 1.0 and is_finite_num(ke_s) and 0.0001 <= float(ke_s) <= 1.0 and is_finite_num(kr_s) and 0.0001 <= float(kr_s) <= 1.0 and is_finite_num(t_eff0) and 0.0 <= float(t_eff0) <= max_efficacy_start_h + 1e-9 and is_valid_sha256_hex(sha) and is_strict_bool(env_raw.get("burst_phase_covered")) and env_raw["burst_phase_covered"] and is_strict_bool(env_raw.get("washout_phase_covered")) and env_raw["washout_phase_covered"] and isinstance(s_ids, list) and len(s_ids) >= 1 and all(is_nonempty_str(x) for x in s_ids) and is_valid_sha256_hex(r_sha) and sha256_bytes(canonical_json_bytes({k: v for k, v in env_raw.items() if k != "envelope_receipt_sha256"})) == str(r_sha) ): fails.append(f"FAIL_{prefix}_INVALID_INTERSAMPLE_ENVELOPE_PARAMS"); return None if not hours_seen or len(hours_seen) != len(u_local_series) or len(hours_seen) != len(u_sys_series) or len(hours_seen) != len(l_parent_all_series): return None kel, krl, kep, krp, kes, krs = float(ke_l), float(kr_l), float(ke_p), float(kr_p), float(ke_s), float(kr_s) t_start_eff = float(t_eff0) t_end_eff = 240.0 def _seg_peak(u_prev: float, u_curr: float, dt: float, ke: float, kr: float) -> float: up = max(u_prev, 1e-12) uc = max(u_curr, 1e-12) return math.exp((ke * math.log(up) + kr * math.log(uc) + ke * kr * dt) / (ke + kr)) def _seg_trough_on_subinterval(t1: float, l1: float, t2: float, l2: float, a: float, b: float, ke: float, kr: float) -> float: """Exact inf_{t in [a, b]} max(l1*exp(-ke*(t-t1)), l2*exp(-kr*(t2-t))) for t1 <= a <= b <= t2.""" if l1 <= 0.0 and l2 <= 0.0: return 0.0 if l1 <= 0.0: return l2 * math.exp(-kr * (t2 - a)) if l2 <= 0.0: return l1 * math.exp(-ke * (b - t1)) t_cross = (math.log(l1) - math.log(l2) + ke * t1 + kr * t2) / (ke + kr) t_star = max(a, min(b, t_cross)) return max(l1 * math.exp(-ke * (t_star - t1)), l2 * math.exp(-kr * (t2 - t_star))) # Empirical inter-sample log-slope consistency: declared rate bounds CANNOT be slower than observed data slopes! slope_breach = False for i in range(1, len(hours_seen)): dt = hours_seen[i] - hours_seen[i - 1] if dt <= 0.0: continue if u_local_series[i - 1] > 0.0 and u_local_series[i] > 0.0: s_loc = math.log(u_local_series[i] / u_local_series[i - 1]) / dt if (-s_loc) > kel + 1e-6 or s_loc > krl + 1e-6: slope_breach = True if u_sys_series[i - 1] > 0.0 and u_sys_series[i] > 0.0: s_sys = math.log(u_sys_series[i] / u_sys_series[i - 1]) / dt if (-s_sys) > kes + 1e-6 or s_sys > krs + 1e-6: slope_breach = True if hours_seen[i] <= t_end_eff + 1e-9 and l_parent_all_series[i - 1] > 0.0 and l_parent_all_series[i] > 0.0: s_par = math.log(l_parent_all_series[i] / l_parent_all_series[i - 1]) / dt if (-s_par) > kep + 1e-6 or s_par > krp + 1e-6: slope_breach = True if slope_breach: fails.append(f"FAIL_{prefix}_ENVELOPE_RATE_CONTRADICTS_OBSERVED_SLOPE") # Continuous peaks across [0, t_0] AND every interior interval [t_{i-1}, t_i] over ALL 21 days! cont_pk_loc = u_local_series[0] * math.exp(kel * hours_seen[0]) cont_pk_sys = u_sys_series[0] * math.exp(kes * hours_seen[0]) for i in range(1, len(hours_seen)): dt = hours_seen[i] - hours_seen[i - 1] cont_pk_loc = max(cont_pk_loc, u_local_series[i], _seg_peak(u_local_series[i - 1], u_local_series[i], dt, kel, krl)) cont_pk_sys = max(cont_pk_sys, u_sys_series[i], _seg_peak(u_sys_series[i - 1], u_sys_series[i], dt, kes, krs)) # Full parent efficacy window [t_start_eff, 240.0 h] coverage check: # When efficacy starts at Day-0 first blood contact (max_efficacy_start_h == 0.0), require an early # initial sample (hours_seen[0] <= 2.0 h) so a late first sample cannot extrapolate backward ungrounded! if max_efficacy_start_h <= 1e-9 and hours_seen[0] > 2.0 + 1e-9: holds.append(f"HOLD_{prefix}_MISSING_EARLY_INITIAL_EXPOSURE_SAMPLE_FOR_T0_BOUNDARY") return cont_pk_loc, -1.0, cont_pk_sys if hours_seen[-1] < t_end_eff - 1e-9: holds.append(f"HOLD_{prefix}_PARENT_EFFICACY_WINDOW_UNCOVERED_THROUGH_240H") return cont_pk_loc, -1.0, cont_pk_sys trough_candidates: list[float] = [] # Left head [t_start_eff, min(t_0, 240.0)] if first sample is strictly after t_start_eff if hours_seen[0] > t_start_eff + 1e-9: t0 = hours_seen[0] l0 = l_parent_all_series[0] trough_candidates.append(0.0 if l0 <= 0.0 else l0 * math.exp(-krp * (t0 - t_start_eff))) # Every observed sample inside [t_start_eff, 240.0] for h_i, l_i in zip(hours_seen, l_parent_all_series): if t_start_eff - 1e-9 <= h_i <= t_end_eff + 1e-9: trough_candidates.append(max(0.0, l_i)) # Every interior segment [t_{i-1}, t_i] intersecting [t_start_eff, 240.0] (including right-straddle across 240.0 h!) for i in range(1, len(hours_seen)): t1, t2 = hours_seen[i - 1], hours_seen[i] a = max(t1, t_start_eff) b = min(t2, t_end_eff) if a <= b + 1e-9: trough_candidates.append( _seg_trough_on_subinterval(t1, l_parent_all_series[i - 1], t2, l_parent_all_series[i], a, max(a, b), kep, krp) ) cont_tr_parent = min(trough_candidates) if trough_candidates else -1.0 return cont_pk_loc, cont_tr_parent, cont_pk_sys def _compute_continuous_bmt_parent_ratio_bounds( hours_seen: Sequence[float], u_bmt_free_series: Sequence[float], l_bmt_free_series: Sequence[float], l_parent_all_series: Sequence[float], u_parent_all_series: Sequence[float], env_raw: Any, *, fails: list[str], prefix: str, ) -> tuple[float, float, bool]: """ Exact TWO-SIDED interval-wide continuous free BMT/parent ratio envelope [min_ratio, max_ratio] across [0, t_0] and every [t_{i-1}, t_i], using unified familywise Bonferroni concentration bounds: - Upper supremum: max_t B_upper(t) / P_lower(t) (concave piecewise-linear in log space -> exact max at {t1, t2, t_Bu, t_Pl}) - Lower infimum: min_t B_lower(t) / P_upper(t) (convex piecewise-linear in log space -> exact min at {t1, t2, t_Bl, t_Pu}) """ n_h = len(hours_seen) if ( not isinstance(env_raw, Mapping) or not hours_seen or any(len(s) != n_h for s in (u_bmt_free_series, l_bmt_free_series, l_parent_all_series, u_parent_all_series)) ): return 0.0, 0.0, False ke_l, kr_l = env_raw.get("k_elim_local_max_per_h"), env_raw.get("k_rise_local_max_per_h") ke_p, kr_p = env_raw.get("k_elim_parent_max_per_h"), env_raw.get("k_rise_parent_max_per_h") ke_b, kr_b = env_raw.get("k_elim_bmt_max_per_h"), env_raw.get("k_rise_bmt_max_per_h") if not all(is_finite_num(x) and float(x) > 0.0 for x in (ke_l, kr_l, ke_p, kr_p, ke_b, kr_b)): return 0.0, 0.0, False kel, krl, kep, krp, keb, krb = float(ke_l), float(kr_l), float(ke_p), float(kr_p), float(ke_b), float(kr_b) # Verify empirical BMT log-slope consistency against dedicated metabolite-specific rate bounds (keb, krb)! bmt_slope_breach = False for i in range(1, n_h): dt = hours_seen[i] - hours_seen[i - 1] if dt > 0.0 and u_bmt_free_series[i - 1] > 0.0 and u_bmt_free_series[i] > 0.0: s_bmt = math.log(u_bmt_free_series[i] / u_bmt_free_series[i - 1]) / dt if (-s_bmt) > keb + 1e-6 or s_bmt > krb + 1e-6: bmt_slope_breach = True if bmt_slope_breach: fails.append(f"FAIL_{prefix}_ENVELOPE_RATE_CONTRADICTS_OBSERVED_BMT_SLOPE") min_ratio = float("inf") max_ratio = 0.0 unbounded = False # Pre-first-sample interval [0, t_0] using dedicated BMT rates (keb, krb) & parent rates (kep, krp)! t0 = max(0.0, hours_seen[0]) ub0, lb0 = u_bmt_free_series[0], l_bmt_free_series[0] lp0, up0 = l_parent_all_series[0], u_parent_all_series[0] if lp0 <= 0.0: if ub0 > 0.0: unbounded = True elif ub0 > 0.0: max_ratio = max(max_ratio, (ub0 / lp0) * math.exp((keb + krp) * t0)) if lb0 <= 0.0: min_ratio = 0.0 elif up0 > 0.0: min_ratio = min(min_ratio, (lb0 / up0) * math.exp(-(krb + max(kep, kel)) * t0)) for i in range(1, n_h): t1, t2 = hours_seen[i - 1], hours_seen[i] ub1, ub2 = u_bmt_free_series[i - 1], u_bmt_free_series[i] lb1, lb2 = l_bmt_free_series[i - 1], l_bmt_free_series[i] lp1, lp2 = l_parent_all_series[i - 1], l_parent_all_series[i] up1, up2 = u_parent_all_series[i - 1], u_parent_all_series[i] dt = max(1e-12, t2 - t1) # Upper ratio envelope B_upper(t) / P_lower(t) governed strictly by metabolite-specific (keb, krb)! if lp1 <= 0.0 or lp2 <= 0.0: if ub1 > 0.0 or ub2 > 0.0: unbounded = True elif ub1 > 0.0 or ub2 > 0.0: ub1_p, ub2_p = max(ub1, 1e-12), max(ub2, 1e-12) obs_p_dec = max(0.0, math.log(lp1 / lp2) / dt) obs_p_ris = max(0.0, math.log(lp2 / lp1) / dt) kep_i = max(kep, kel if t2 > 240.0 + 1e-9 else 0.0, obs_p_dec) krp_i = max(krp, krl if t2 > 240.0 + 1e-9 else 0.0, obs_p_ris) t_bu = (math.log(ub2_p) - math.log(ub1_p) + krb * t1 + keb * t2) / (krb + keb) t_pl = (math.log(lp1) - math.log(lp2) + kep_i * t1 + krp_i * t2) / (kep_i + krp_i) for tc in (t1, t2, max(t1, min(t2, t_bu)), max(t1, min(t2, t_pl))): b_u = min(ub1_p * math.exp(krb * (tc - t1)), ub2_p * math.exp(keb * (t2 - tc))) p_l = max(lp1 * math.exp(-kep_i * (tc - t1)), lp2 * math.exp(-krp_i * (t2 - tc))) if p_l <= 0.0: unbounded = True else: max_ratio = max(max_ratio, b_u / p_l) # Lower ratio envelope B_lower(t) / P_upper(t) governed strictly by metabolite-specific (keb, krb)! if lb1 <= 0.0 or lb2 <= 0.0: min_ratio = 0.0 elif up1 > 0.0 and up2 > 0.0: obs_b_dec = max(0.0, math.log(lb1 / lb2) / dt) obs_b_ris = max(0.0, math.log(lb2 / lb1) / dt) keb_i = max(keb, obs_b_dec) krb_i = max(krb, obs_b_ris) obs_pu_dec = max(0.0, math.log(up1 / up2) / dt) obs_pu_ris = max(0.0, math.log(up2 / up1) / dt) kep_u = max(kep, kel if t2 > 240.0 + 1e-9 else 0.0, obs_pu_dec) krp_u = max(krp, krl if t2 > 240.0 + 1e-9 else 0.0, obs_pu_ris) t_bl = (math.log(lb1) - math.log(lb2) + keb_i * t1 + krb_i * t2) / (keb_i + krb_i) t_pu = (math.log(up2) - math.log(up1) + krp_u * t1 + kep_u * t2) / (krp_u + kep_u) for tc in (t1, t2, max(t1, min(t2, t_bl)), max(t1, min(t2, t_pu))): b_l = max(lb1 * math.exp(-keb_i * (tc - t1)), lb2 * math.exp(-krb_i * (t2 - tc))) p_u = min(up1 * math.exp(krp_u * (tc - t1)), up2 * math.exp(kep_u * (t2 - tc))) if p_u > 0.0: