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…
iso_v38 = None
if iso_spec is not None and iso_spec.loader is not None:
iso_v38 = importlib.util.module_from_spec(iso_spec)
iso_spec.loader.exec_module(iso_v38)
r14_b3_shared_lock = bool(
wrapped_v38._FVM_SOLVER_LOCK is _FVM_SOLVER_LOCK
and iso_v38 is not None
and iso_v38 is not sys.modules[__name__]
and iso_v38._FVM_SOLVER_LOCK is _FVM_SOLVER_LOCK
)
scripts_str_b3 = str(openislet_root / "scripts")
added_b3 = False
if scripts_str_b3 not in sys.path:
sys.path.insert(0, scripts_str_b3)
added_b3 = True
try:
import run_layer4_tissue_and_metabolic_twin as l4_b3 # noqa: E402
orig_cbs = (
l4_b3._solve_3d_oxygen_strict_iip_0019,
l4_b3._compute_exact_fv_interface_stats_iip_0019,
)
ser_a = dispatch_and_solve_nw08_measured_ocr(
"R3P_35", 1.3164, 0.0, openislet_root=openislet_root, p_bed_mmhg=35.0
)
ser_b = wrapped_v38.dispatch_and_solve_nw08_measured_ocr(
"R3P_30", 1.3164, 0.0, openislet_root=openislet_root, p_bed_mmhg=30.0
)
ref_pair = (
(ser_a["face_min_mmhg_unrounded"], ser_a["adjudicated_verdict"]),
(ser_b["face_min_mmhg_unrounded"], ser_b["adjudicated_verdict"]),
)
all_attempts_ok = True
for peer_mod in (wrapped_v38, iso_v38):
if peer_mod is None:
all_attempts_ok = False
continue
for _attempt in range(2):
barrier_b3 = threading.Barrier(2)
def _conc_call(mod_obj: Any, rt: str, pb: float):
barrier_b3.wait(timeout=10)
out_c = mod_obj.dispatch_and_solve_nw08_measured_ocr(
rt, 1.3164, 0.0, openislet_root=openislet_root, p_bed_mmhg=pb
)
return (out_c["face_min_mmhg_unrounded"], out_c["adjudicated_verdict"])
with ThreadPoolExecutor(max_workers=2) as pool_b3:
fa = pool_b3.submit(_conc_call, sys.modules[__name__], "R3P_35", 35.0)
fb = pool_b3.submit(_conc_call, peer_mod, "R3P_30", 30.0)
got_pair = (fa.result(), fb.result())
restored_now = bool(
orig_cbs
== (
l4_b3._solve_3d_oxygen_strict_iip_0019,
l4_b3._compute_exact_fv_interface_stats_iip_0019,
)
)
if got_pair != ref_pair or not restored_now:
all_attempts_ok = False
r14_b3_callbacks_restored = bool(
orig_cbs
== (
l4_b3._solve_3d_oxygen_strict_iip_0019,
l4_b3._compute_exact_fv_interface_stats_iip_0019,
)
)
r14_b3_all_matched = all_attempts_ok
finally:
if added_b3 and scripts_str_b3 in sys.path:
sys.path.remove(scripts_str_b3)
ok_27 = bool(r14_b3_shared_lock and r14_b3_callbacks_restored and r14_b3_all_matched)
tests.append({
"test": "r14_b3_concurrent_direct_and_composite_wrapper_shared_lock_and_callback_isolation",
"shared_process_lock": r14_b3_shared_lock,
"callbacks_restored": r14_b3_callbacks_restored,
"all_concurrent_attempts_matched_serial": r14_b3_all_matched,
"passed": ok_27,
})
# 28. R15-B2: Live dispatch & `_evaluate_nw08_mesh_convergence` end-to-end harness test on standard R2
# [48, 72, 96] ladder reproducing (a) the exact synthetic slow-order counterexample (p=0.06,
# true limit 14.95 < 15.0: 15.012547946, 15.011044645, 15.010000000 -> HOLD exit 2,
# HOLD_NW08_MESH_CONVERGENCE_UNQUALIFIED_SLOW_OR_NON_ASYMPTOTIC_ORDER, passes_15mmhg=False,
# interval_valid=False, NO clearance bound) and (b) the asymptotic p=1.5 clearance control
# (true limit 15.05 > 15.0: 15.163137085, 15.111584029, 15.090000000 -> PASS exit 0,
# Richardson 15.05, lower bound 15.04 >= 15.0)!
from unittest.mock import patch as _mock_patch
r2_cat_spec = _build_nw08_route_catalog(openislet_root)["R2_DZ40"]
# Warm native R2_DZ40 baseline cache BEFORE patching so candidate solve only uses patched grid outputs.
_get_native_unrounded_baseline_mmhg(openislet_root, "R2_DZ40", r2_cat_spec, float(r2_cat_spec["baseline_shared_ocr"]))
cur_mod = sys.modules[__name__]
slow_face_map = {48: 15.012547946, 72: 15.011044645, 96: 15.010000000}
with _mock_patch.object(
cur_mod, "_solve_nw08_grid_level_cached", side_effect=lambda *a, nx, **kw: (slow_face_map[int(nx)], 40.0)
):
mc_slow_direct = _evaluate_nw08_mesh_convergence(
openislet_root,
"R2_DZ40",
r2_cat_spec,
vasc_scale=1.0,
native_coarse_face_mmhg=slow_face_map[48],
verified_p_bed=40.0,
force_check=True,
)
with _mock_patch.object(
cur_mod,
"_execute_single_3d_fvm_vascular_scaled_solve_with_meta",
side_effect=lambda *a, **kw: ({}, slow_face_map[48], 40.0),
):
rec_slow_res, rec_slow_rc = evaluate_nw08_measured_ocr_record(
{"route_id": "R2_DZ40", "measured_ocr": 1.3164, "uncertainty_se": 0.0},
openislet_root=openislet_root,
)
acc_face_map = {n: 15.05 + 0.04 * ((96.0 / n) ** 1.5) for n in (48, 72, 96)}
with _mock_patch.object(
cur_mod, "_solve_nw08_grid_level_cached", side_effect=lambda *a, nx, **kw: (acc_face_map[int(nx)], 40.0)
):
mc_acc_direct = _evaluate_nw08_mesh_convergence(
openislet_root,
"R2_DZ40",
r2_cat_spec,
vasc_scale=1.0,
native_coarse_face_mmhg=acc_face_map[48],
verified_p_bed=40.0,
force_check=True,
)
with _mock_patch.object(
cur_mod,
"_execute_single_3d_fvm_vascular_scaled_solve_with_meta",
side_effect=lambda *a, **kw: ({}, acc_face_map[48], 40.0),
):
rec_acc_res, rec_acc_rc = evaluate_nw08_measured_ocr_record(
{"route_id": "R2_DZ40", "measured_ocr": 1.3164, "uncertainty_se": 0.0},
openislet_root=openislet_root,
)
ok_28 = bool(
mc_slow_direct["verdict_from_mesh_policy"] == "HOLD"
and mc_slow_direct["hold_reason_token"] == NW08_HOLD_TOKEN_UNQUALIFIED_ORDER
and mc_slow_direct["interval_valid"] is False
and mc_slow_direct["order_qualified"] is False
and mc_slow_direct["conservative_po2_lower_bound_mmhg"] is None
and mc_slow_direct["apparent_order_p"] is not None
and abs(mc_slow_direct["apparent_order_p"] - 0.06) < 1e-4
and rec_slow_rc == 2
and rec_slow_res["adjudicated_verdict"] == "HOLD"
and rec_slow_res["passes_15mmhg_floor_unrounded"] is False
and rec_slow_res["release_authorizing"] is False
and rec_slow_res["solver_cleared_oxygen_floor_diagnostic"] is False
and NW08_HOLD_TOKEN_UNQUALIFIED_ORDER in (rec_slow_res.get("holds") or [])
and mc_acc_direct["verdict_from_mesh_policy"] == "PASS"
and mc_acc_direct["interval_valid"] is True
and mc_acc_direct["order_qualified"] is True
and abs(float(mc_acc_direct["apparent_order_p"]) - 1.5) < 1e-5
and abs(float(mc_acc_direct["richardson_extrapolated_mmhg"]) - 15.05) < 1e-6
and abs(float(mc_acc_direct["conservative_po2_lower_bound_mmhg"]) - 15.04) < 1e-6
and rec_acc_rc == 0
and rec_acc_res["adjudicated_verdict"] == "PASS"
and rec_acc_res["passes_15mmhg_floor_unrounded"] is True
and rec_acc_res["release_authorizing"] is False
)
tests.append({
"test": "r15_b2_live_mesh_harness_slow_p0p06_counterexample_hold_and_asymptotic_p1p5_pass",
"slow_counterexample_direct_verdict": mc_slow_direct["verdict_from_mesh_policy"],
"slow_counterexample_observed_order_p": mc_slow_direct["apparent_order_p"],
"slow_counterexample_record_rc": rec_slow_rc,
"slow_counterexample_holds": rec_slow_res.get("holds"),
"slow_counterexample_lower_bound_mmhg": mc_slow_direct["conservative_po2_lower_bound_mmhg"],
"asymptotic_p1p5_direct_verdict": mc_acc_direct["verdict_from_mesh_policy"],
"asymptotic_p1p5_observed_order_p": mc_acc_direct["apparent_order_p"],
"asymptotic_p1p5_richardson_mmhg": mc_acc_direct["richardson_extrapolated_mmhg"],
"asymptotic_p1p5_lower_bound_mmhg": mc_acc_direct["conservative_po2_lower_bound_mmhg"],
"asymptotic_p1p5_record_rc": rec_acc_rc,
"passed": ok_28,
})
# 29. R16-B1: Standalone (API, record, file) AND coupled zero-SE inferential-model rejection
# (`STUDENT_T_ESTIMATED_SE` & `KNOWN_SIGMA_NORMAL` with `uncertainty_se == 0` -> HOLD BEFORE
# solving with `HOLD_NW08_INFERENTIAL_MODEL_REQUIRES_POSITIVE_SE`, `solver_executed=False`,
# `release_authorizing=False`, exit 2; `ValueError` on direct API), retaining explicit
# `ZERO_UNCERTAINTY_DETERMINISTIC_POINT` control (PASS exit 0), positive-SE Student-t control
# (`R1_390_stress, OCR=1.25, SE=0.06, n=6, df=5` -> `u95=1.370903`, coarse `14.9808 mmHg` -> FAIL exit 3),
# AND coupled `evaluate_nw08_measured_ocr_file` zero-SE HOLD!
api_zero_st_rejected = False
api_zero_ks_rejected = False
try:
dispatch_and_solve_nw08_measured_ocr(
"R1_390_stress",
1.25,
0.0,
openislet_root=openislet_root,
ocr_uncertainty_model=NW08_OCR_UNCERTAINTY_MODEL_STUDENT_T,
ocr_replicates_n=6,
)
except ValueError as exc_st0:
api_zero_st_rejected = NW08_HOLD_TOKEN_INFERENTIAL_REQUIRES_POSITIVE_SE in str(exc_st0)
try:
dispatch_and_solve_nw08_measured_ocr(
"R2_DZ40",
1.097,
0.0,
openislet_root=openislet_root,
ocr_uncertainty_model=NW08_OCR_UNCERTAINTY_MODEL_KNOWN_SIGMA,
ocr_uncertainty_provenance="calibrated_respirometer_known_sigma_cert_v1",
)
except ValueError as exc_ks0:
api_zero_ks_rejected = NW08_HOLD_TOKEN_INFERENTIAL_REQUIRES_POSITIVE_SE in str(exc_ks0)
with tempfile.TemporaryDirectory() as td_r16_b1:
td_16b1 = Path(td_r16_b1)
f_st0_r2 = td_16b1 / "r16_b1_r2_student_t_se0.json"
f_st0_r2.write_text(
json.dumps({
"route_id": "R2_DZ40",
"measured_ocr": 1.097,
"uncertainty_se": 0.0,
"ocr_uncertainty_model": NW08_OCR_UNCERTAINTY_MODEL_STUDENT_T,
"ocr_replicates_n": 6,
}),
encoding="utf-8",
)
f_st0_r1 = td_16b1 / "r16_b1_r1_student_t_se0.json"
f_st0_r1.write_text(
json.dumps({
"route_id": "R1_390_stress",
"measured_ocr": 1.25,
"uncertainty_se": 0.0,
"ocr_uncertainty_model": NW08_OCR_UNCERTAINTY_MODEL_STUDENT_T,
"ocr_replicates_n": 6,
}),
encoding="utf-8",
)
f_ks0_r2 = td_16b1 / "r16_b1_r2_known_sigma_se0.json"
f_ks0_r2.write_text(
json.dumps({
"route_id": "R2_DZ40",
"measured_ocr": 1.097,
"uncertainty_se": 0.0,
"ocr_uncertainty_model": NW08_OCR_UNCERTAINTY_MODEL_KNOWN_SIGMA,
"ocr_uncertainty_provenance": "calibrated_respirometer_known_sigma_cert_v1",
}),
encoding="utf-8",
)
f_det0_r2 = td_16b1 / "r16_b1_r2_deterministic_se0.json"
f_det0_r2.write_text(
json.dumps({