import json import numpy as np import pytest import yaml from ml_porosity.data.synth_d2 import D2Realization, load_horizon_grid from ml_porosity.paths import REPO_ROOT, f3_root from ml_porosity.registration.warp_prior import WarpHyper P = yaml.safe_load(open(REPO_ROOT / "configs" / "protocol_p2.yaml")) INP = P["inputs"] CFG = yaml.safe_load(open(REPO_ROOT / INP["d2_config"]["path"])) NEEDED = [f3_root() / ".survey", REPO_ROOT / CFG["horizon_cache"], REPO_ROOT / INP["warp_prior"]["path"], REPO_ROOT / INP["wavelet"]["path"]] pytestmark = pytest.mark.skipif(not all(p.exists() for p in NEEDED), reason="missing D2 inputs (run scripts/09, 10)") RID = P["grid"]["dev_realizations"][0] SIGMA_A = CFG["anchors"]["sigma_a_ms"] @pytest.fixture(scope="module") def make(): hg = load_horizon_grid(CFG["horizons"], REPO_ROOT / CFG["horizon_cache"]) prior = json.loads((REPO_ROOT / INP["warp_prior"]["path"]).read_text())["fits"][INP["warp_prior"]["fit"]] wav = np.load(REPO_ROOT / INP["wavelet"]["path"])[INP["wavelet"]["key"]] return lambda rid: D2Realization(CFG, rid, hg, WarpHyper(**prior["hyper"]), wav) @pytest.fixture(scope="module") def R(make): return make(RID) def test_realization_is_deterministic(make, R): R2 = make(RID) for a, b in zip(R.wells, R2.wells): assert (a.name, a.il, a.xl) == (b.name, b.il, b.xl) for k in ("seis_clean", "noise_unit", "phi_t", "z_g", "boundaries_t", "eps_a"): np.testing.assert_array_equal(getattr(a, k), getattr(b, k)) assert a.z_b == b.z_b assert not np.allclose(make(RID + 1).wells[0].phi_t, R.wells[0].phi_t) def test_wells_are_spatially_separated(R): xy = np.array([[w.x, w.y] for w in R.wells]) d = np.hypot(*(xy[:, None] - xy[None]).transpose(2, 0, 1)) np.fill_diagonal(d, np.inf) nt = CFG["wells"]["n_test"] assert len(R.test) == nt and len(R.pool) == CFG["wells"]["n_train_pool"] assert d[:nt].min() >= CFG["wells"]["min_dist_test_m"] assert d[nt:, nt:].min() >= CFG["wells"]["min_dist_train_m"] def test_common_random_numbers_across_drift(R): w = R.pool[0] obs = {c: R.observe(w, c, P["grid"]["snr_db"], SIGMA_A) for c in P["grid"]["drift_factors"]} ref = obs[1.0] for c, o in obs.items(): for k in ("seis", "y_true", "t_k"): np.testing.assert_array_equal(o[k], ref[k]) np.testing.assert_allclose(o["g"], c * ref["g"], atol=1e-12) assert o["b"] == pytest.approx(c * ref["b"]) np.testing.assert_allclose(obs[0.0]["t_of_tau_true"], R.tau_fine, atol=1e-9) np.testing.assert_allclose(obs[0.0]["tau_k"], w.boundaries_t, atol=1e-9) def test_anchors_and_density_subsets(R): for w in R.wells: bt = w.boundaries_t assert np.all(np.diff(bt) > 0) and R.t_ext[0] <= bt[0] and bt[-1] <= R.t_ext[-1] assert len(w.eps_a) == len(bt) and 10 <= len(bt) <= 20 # F3: 14–17 markers per well w = R.pool[0] n = len(w.boundaries_t) sizes = {d: len(R.observe(w, 1.0, None, SIGMA_A, d)["tau_k"]) for d in ("full", "half", "topbottom", "none")} assert sizes == {"full": n, "half": (n + 1) // 2, "topbottom": 2, "none": 0} def test_max_drift_stays_in_domain(R): c = max(P["grid"]["drift_factors"]) for w in R.wells: o = R.observe(w, c, None, SIGMA_A) assert np.all(np.diff(o["t_of_tau_true"]) > 0) assert R.tau_fine[0] <= o["tau_k"].min() and o["tau_k"].max() <= R.tau_fine[-1] assert np.isfinite(R.true_tau_at(o, R.t_ext)).all()