scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…
except ValueError:
return False
return bool(val >= fl)
def _passes_unrounded_15mmhg_floor(
raw_face_min_mmhg: float,
floor_mmhg: float = CANONICAL_NW08_PO2_FLOOR_MMHG,
) -> bool:
"""
Strictly evaluate the IMMUTABLE canonical 15.0 mmHg GSIS floor against the native UNROUNDED
float64 FV interface minimum BEFORE any 4-decimal display rounding (so 14.999959999870875
strictly FAILS even though it formats to 15.0000!). Rejects any attempt to pass a non-15.0
floor to this canonical release predicate.
"""
try:
fl = _safe_finite_float(floor_mmhg, "floor_mmhg")
except ValueError as exc:
raise ValueError(
f"Canonical NW-08 15.0 mmHg release predicate requires floor_mmhg == "
f"{CANONICAL_NW08_PO2_FLOOR_MMHG}; got invalid value"
) from exc
if fl != CANONICAL_NW08_PO2_FLOOR_MMHG:
raise ValueError(
f"Canonical NW-08 15.0 mmHg release predicate requires floor_mmhg == "
f"{CANONICAL_NW08_PO2_FLOOR_MMHG}; got {floor_mmhg!r}"
)
return _passes_unrounded_diagnostic_floor(raw_face_min_mmhg, CANONICAL_NW08_PO2_FLOOR_MMHG)
def _regularized_incomplete_beta(a: float, b: float, x: float) -> float:
"""Stdlib-only regularized incomplete beta I_x(a, b) via Lentz's continued fraction (zero assert)."""
if x <= 0.0:
return 0.0
if x >= 1.0:
return 1.0
log_bt = (
math.lgamma(a + b)
- math.lgamma(a)
- math.lgamma(b)
+ a * math.log(x)
+ b * math.log1p(-x)
)
bt = math.exp(log_bt)
def _betacf(pa: float, pb: float, px: float) -> float:
fpmin = 1.0e-300
eps = 3.0e-15
qab = pa + pb
qap = pa + 1.0
qam = pa - 1.0
c = 1.0
d = 1.0 - qab * px / qap
if abs(d) < fpmin:
d = fpmin
d = 1.0 / d
h = d
for m in range(1, 201):
m2 = 2 * m
aa = m * (pb - m) * px / ((qam + m2) * (pa + m2))
d = 1.0 + aa * d
if abs(d) < fpmin:
d = fpmin
c = 1.0 + aa / c
if abs(c) < fpmin:
c = fpmin
d = 1.0 / d
h *= d * c
aa = -(pa + m) * (qab + m) * px / ((pa + m2) * (qap + m2))
d = 1.0 + aa * d
if abs(d) < fpmin:
d = fpmin
c = 1.0 + aa / c
if abs(c) < fpmin:
c = fpmin
d = 1.0 / d
delta = d * c
h *= delta
if abs(delta - 1.0) < eps:
break
return h
if x < (a + 1.0) / (a + b + 2.0):
return bt * _betacf(a, b, x) / a
return 1.0 - bt * _betacf(b, a, 1.0 - x) / b
def exact_student_t_cdf(t_val: float, df: int) -> float:
"""Exact Student-t CDF F_t(t_val; df) for integer df >= 1 (pure Python stdlib)."""
if isinstance(df, bool) or not isinstance(df, int) or df < 1 or df > MAX_NW08_STUDENT_T_DF:
raise ValueError(f"HOLD_NW08_OCR_INVALID_DEGREES_OF_FREEDOM:df must be int in [1, {MAX_NW08_STUDENT_T_DF}]; got {df!r}")
ft = _safe_finite_float(t_val, "t_val")
if ft == 0.0:
return 0.5
x = float(df) / (float(df) + ft * ft)
ib = _regularized_incomplete_beta(0.5 * float(df), 0.5, x)
return (1.0 - 0.5 * ib) if ft > 0.0 else (0.5 * ib)
def _dec_atan_high_prec(u: Decimal, prec: int = 60) -> Decimal:
"""High-precision Decimal arctan(u) for u >= 0 under isolated clean Decimal context."""
with _clean_dec_ctx(prec + 12, ROUND_HALF_EVEN):
if u > Decimal("0.25"):
return Decimal(2) * _dec_atan_high_prec(
u / (Decimal(1) + (Decimal(1) + u * u).sqrt()), prec
)
u2 = u * u
p_term = u
tot = Decimal(0)
tol = Decimal(10) ** (-(prec + 8))
for idx in range(300):
add = p_term / Decimal(2 * idx + 1)
tot = (tot + add) if (idx % 2 == 0) else (tot - add)
if abs(add) < tol:
break
p_term *= u2
return tot
@lru_cache(maxsize=4)
def _dec_pi_high_prec(prec: int = 60) -> Decimal:
"""High-precision Decimal pi via Machin's formula under isolated clean Decimal context."""
with _clean_dec_ctx(prec + 12, ROUND_HALF_EVEN):
return Decimal(4) * (
Decimal(4) * _dec_atan_high_prec(Decimal(1) / Decimal(5), prec)
- _dec_atan_high_prec(Decimal(1) / Decimal(239), prec)
)
def _exact_student_t_cdf_dec(t_dec: Decimal, df: int, prec: int = 55) -> Decimal:
"""
Exact finite-sum Student-t CDF F_t(t_dec; df) in high-precision Decimal for integer df >= 1,
t_dec >= 0 (pure Python stdlib, clean-context isolated).
"""
with _clean_dec_ctx(prec, ROUND_HALF_EVEN):
d = Decimal(df)
x = d / (d + t_dec * t_dec)
if df % 2 == 0:
k = df // 2
term = Decimal(1)
s = term
for j in range(1, k):
term = term * Decimal(2 * j - 1) / Decimal(2 * j) * x
s += term
return Decimal("0.5") + Decimal("0.5") * (t_dec / (t_dec * t_dec + d).sqrt()) * s
k = (df - 1) // 2
pi_d = _dec_pi_high_prec(prec + 6)
u = t_dec / d.sqrt()
if k == 0:
return Decimal("0.5") + _dec_atan_high_prec(u, prec + 6) / pi_d
term = Decimal(1)
s = term
for j in range(1, k):
term = term * Decimal(2 * j) / Decimal(2 * j + 1) * x
s += term
return Decimal("0.5") + (_dec_atan_high_prec(u, prec + 6) + u * x * s) / pi_d
# R17-B2: Provably upper-enclosed 60-digit standard-normal 95% one-sided quantile z_{0.95}
# (exact Phi^{-1}(0.95) = 1.644853626951472714863848907991632136083195744275322071769672094...).
_EXACT_Z95_UPPER_40D: Decimal = MIN_NW08_CONSERVATIVE_Z_ONE_SIDED_95_DEC
def _exact_standard_normal_cdf_dec(z_dec: Decimal, prec: int = 80) -> Decimal:
"""
R17-B2 High-precision standard-normal CDF Phi(z_dec) in isolated clean Decimal context via
exact alternating Taylor series erf(z / sqrt(2)) = (2/sqrt(pi)) * sum (-1)^n x^(2n+1) / (n!(2n+1)).
Used to verify 1 - Phi(z) <= 0.05 strictly for upper normal quantile bounds.
"""
with _clean_dec_ctx(prec + 16, ROUND_HALF_EVEN):
z = Decimal(z_dec)
if z == Decimal(0):
return Decimal("0.5")
az = z.copy_abs()
x = az / Decimal(2).sqrt()
x2 = x * x
term = x
total = term
tol = Decimal(10) ** (-(prec + 12))
for n in range(1, 600):
term = -term * x2 / Decimal(n)
add = term / Decimal(2 * n + 1)
total += add
if add.copy_abs() < tol:
break
pi_d = _dec_pi_high_prec(prec + 12)
erf_val = Decimal(2) * total / pi_d.sqrt()
if z > Decimal(0):
return (Decimal(1) + erf_val) / Decimal(2)
return (Decimal(1) - erf_val) / Decimal(2)
def _float64_round_up_from_fraction(target: Fraction) -> float:
"""
R17-B2 Exact IEEE-754 float64 upward rounding of a finite non-negative rational `target`:
returns the smallest finite float64 `f` such that `Fraction(f) >= target` via `math.nextafter`.
"""
if target < 0:
raise ValueError(f"Expected non-negative rational target; got {target!r}")
if target == 0:
return 0.0
try:
f_val = float(target)
except OverflowError as exc:
raise ValueError("Rational target overflowed IEEE-754 float64 range") from exc
if not math.isfinite(f_val):
raise ValueError("Rational target overflowed IEEE-754 float64 range")
while Fraction(f_val) < target:
f_val = math.nextafter(f_val, math.inf)
if not math.isfinite(f_val):
raise ValueError("Upward-rounded float64 overflowed IEEE-754 float64 range")
return f_val
def _compute_conservative_upper_float64_vasc_scale(
ocr_u95_dec: Decimal,
baseline_ocr_float: float,
baseline_ocr_dec: Decimal,
) -> Tuple[float, float]:
"""
R17-B2 Derive the native 3D FVM solver's IEEE-754 float64 vascular-voxel scaling multiplier
directly from exact rational `Fraction(ocr_u95_dec) / Fraction(baseline_ocr_dec)` and
`Fraction(ocr_u95_dec)`, rounding strictly upward via `math.nextafter(..., math.inf)` so:
1. `Fraction(mult_f64) >= Fraction(ocr_u95_dec) / Fraction(baseline_ocr_dec)`,
2. `Fraction(baseline_ocr_float * mult_f64) >= Fraction(ocr_u95_dec)`,
3. `Fraction(baseline_ocr_float) * Fraction(mult_f64) >= Fraction(ocr_u95_dec)`, and
4. Whenever `ocr_u95_dec > baseline_ocr_dec`, `mult_f64 > 1.0` strictly.
Returns `(mult_f64, effective_solver_ocr_f64)`.
"""
target_ocr = Fraction(ocr_u95_dec)
base_dec_frac = Fraction(baseline_ocr_dec)
if (
target_ocr <= 0
or base_dec_frac <= 0
or (not math.isfinite(baseline_ocr_float))
or baseline_ocr_float <= 0.0
):
raise ValueError("Require finite strictly positive OCR and baseline OCR for vascular scaling")
if target_ocr == base_dec_frac:
return 1.0, float(baseline_ocr_float)
target_ratio = target_ocr / base_dec_frac
base_float_frac = Fraction(baseline_ocr_float)
mult_f64 = _float64_round_up_from_fraction(target_ratio)
if target_ocr > base_dec_frac and mult_f64 <= 1.0:
mult_f64 = math.nextafter(1.0, math.inf)
while True:
eff_ocr = baseline_ocr_float * mult_f64
if not math.isfinite(eff_ocr) or eff_ocr <= 0.0:
raise ValueError("Computed effective solver vascular OCR overflowed IEEE-754 float64 range")
if (