Files
ML-Porosity/tests/test_d2.py
lamlvh cee2ff0859 P2 diagnosis on dev, P3 dev experiments, lock protocol_p3
- P2: SMC tie posterior (numpy + torch), RML/soft-DTW losses, bank training,
  D2 synthetic generator, scripts 09-13, protocol_p2 (locked) and its outcome
  (G-B failed; H1 not rejected, H3 rejected, H4 not rejected).
- Diagnosis on dev realizations 900/901 only (scripts 14-17, run_p3_dev.sh):
  posterior screen, SMC stability (P = 1024, K = 1), sigma_a misspecification
  stress and training, H3 with the SMC posterior, F3 placement on sigma_post.
- protocol_p3: H2 split into H2a (posterior-mean labels vs point ties) and
  H2b (RML vs posterior mean as a function of sigma_post); H3 descriptive only;
  sigma_a arm; new D2 config d2_synth_p3.yaml; main grid on realizations 8-15.
  Locked by configs/protocol_p3.sha256 and tag protocol-p3.
- runlog: per-run logs and runtime index (logs/index.csv).
- Docs: plan §12 items 10-11, theory, technical overview, report; wording
  aligned with Gunning & Glinsky (2006): the tie posterior is not claimed as new.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 06:03:33 +00:00

86 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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()