- 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>
160 lines
5.6 KiB
Python
160 lines
5.6 KiB
Python
import numpy as np
|
||
import pytest
|
||
|
||
from ml_porosity.data.resample import log_to_time
|
||
from ml_porosity.data.tdr import (
|
||
TDR,
|
||
calibrate_to_anchors,
|
||
legacy_sonic_tdr,
|
||
misfit_at_anchors,
|
||
sonic_tdr,
|
||
)
|
||
from ml_porosity.paths import f3_wells_dir
|
||
|
||
# ---------- synthetic, no data required ----------
|
||
|
||
|
||
def constant_velocity_log(v=2000.0, z0=0.0, z1=1000.0, dz=0.5):
|
||
md = np.arange(z0, z1 + dz / 2, dz)
|
||
return md, np.full_like(md, 1e6 / v) # DT (µs/m)
|
||
|
||
|
||
def test_tdr_rejects_non_monotone():
|
||
with pytest.raises(ValueError):
|
||
TDR(np.array([0.0, 1.0, 1.0]), np.array([0.0, 1.0, 2.0]))
|
||
with pytest.raises(ValueError):
|
||
TDR(np.array([0.0, 1.0, 2.0]), np.array([0.0, 2.0, 1.0]))
|
||
|
||
|
||
def test_from_pairs_drop_flat():
|
||
pairs = np.array([[0, 0], [10, 5], [11, 5], [20, 10]], dtype=float)
|
||
with pytest.raises(ValueError):
|
||
TDR.from_pairs(pairs)
|
||
t = TDR.from_pairs(pairs, drop_flat=True)
|
||
np.testing.assert_array_equal(t.md, [0, 10, 20])
|
||
|
||
|
||
def test_sonic_constant_velocity():
|
||
md, dt = constant_velocity_log(v=2000.0)
|
||
t = sonic_tdr(md, dt, md0=100.0, t0_ms=50.0)
|
||
# TWT = t0 + 2·(z − z0)/v
|
||
np.testing.assert_allclose(t.time_at([100, 600]), [50.0, 550.0], atol=1e-9)
|
||
np.testing.assert_allclose(t.interval_velocity(), 2000.0)
|
||
|
||
|
||
def test_legacy_starts_at_zero():
|
||
md, dt = constant_velocity_log(z0=30.0)
|
||
t = legacy_sonic_tdr(md, dt)
|
||
assert t.twt_ms[0] == 0.0
|
||
|
||
|
||
def test_calibration_honors_anchors_and_is_monotone():
|
||
md, dt = constant_velocity_log(v=2000.0, z1=1500.0)
|
||
base = sonic_tdr(md, dt, md0=0.0, t0_ms=0.0)
|
||
# Anchors offset by up to ±40 ms from the sonic, an F06-1-style drift.
|
||
za = np.array([100.0, 400.0, 700.0, 1000.0, 1300.0])
|
||
ta = za + np.array([5.0, -20.0, 30.0, -40.0, 10.0]) # at 2000 m/s, TWT (ms) = MD (m)
|
||
cal = calibrate_to_anchors(base, za, ta)
|
||
np.testing.assert_allclose(misfit_at_anchors(cal, za, ta), 0.0, atol=1e-9)
|
||
assert np.all(np.diff(cal.twt_ms) > 0)
|
||
# outside the anchor range: constant shift from the nearest anchor
|
||
assert cal.time_at(50.0) == pytest.approx(base.time_at(50.0) + 5.0)
|
||
assert cal.time_at(1400.0) == pytest.approx(base.time_at(1400.0) + 10.0)
|
||
|
||
|
||
def test_calibration_noop_when_sonic_exact():
|
||
md, dt = constant_velocity_log(v=2500.0)
|
||
base = sonic_tdr(md, dt, md0=0.0, t0_ms=0.0)
|
||
za = np.array([200.0, 500.0, 800.0])
|
||
cal = calibrate_to_anchors(base, za, base.time_at(za))
|
||
np.testing.assert_allclose(cal.time_at(md), base.time_at(md), atol=1e-9)
|
||
|
||
|
||
def test_calibration_rejects_crossing_anchors():
|
||
md, dt = constant_velocity_log()
|
||
base = sonic_tdr(md, dt, md0=0.0, t0_ms=0.0)
|
||
with pytest.raises(ValueError):
|
||
calibrate_to_anchors(base, [100.0, 200.0], [300.0, 250.0])
|
||
|
||
|
||
def test_log_to_time_antialias():
|
||
# A log linear in time is preserved; a 200 Hz sine (above the 125 Hz Nyquist of 4 ms) is filtered out.
|
||
md, dt = constant_velocity_log(v=2000.0, dz=0.15)
|
||
tdr = sonic_tdr(md, dt, md0=0.0, t0_ms=0.0)
|
||
t_log = tdr.time_at(md)
|
||
t_out = np.arange(200.0, 800.0, 4.0)
|
||
ramp = log_to_time(md, 0.1 + 1e-4 * t_log, tdr, t_out)
|
||
np.testing.assert_allclose(ramp, 0.1 + 1e-4 * t_out, atol=1e-4)
|
||
hf = log_to_time(md, np.sin(2 * np.pi * 200.0 * t_log / 1000.0), tdr, t_out)
|
||
assert np.max(np.abs(hf)) < 0.02
|
||
|
||
|
||
def test_log_to_time_marks_gaps():
|
||
md, dt = constant_velocity_log(v=2000.0, dz=0.15)
|
||
tdr = sonic_tdr(md, dt, md0=0.0, t0_ms=0.0)
|
||
v = np.ones_like(md)
|
||
v[(md > 400) & (md < 450)] = np.nan # 50 ms gap
|
||
out = log_to_time(md, v, tdr, np.arange(300.0, 600.0, 4.0))
|
||
t_out = np.arange(300.0, 600.0, 4.0)
|
||
assert np.all(np.isnan(out[(t_out > 402) & (t_out < 448)]))
|
||
assert np.all(np.isfinite(out[(t_out < 398) | (t_out > 452)]))
|
||
|
||
|
||
# ---------- on F3 data ----------
|
||
|
||
needs_f3 = pytest.mark.skipif(not (f3_wells_dir() / "Lasfiles").exists(), reason="missing F3 well data")
|
||
|
||
|
||
@needs_f3
|
||
@pytest.mark.data
|
||
@pytest.mark.parametrize(
|
||
"name,n_anchors,lo,hi",
|
||
[ # plan §1.2b and §1.2e: integrated-DT TWT − TD marker within 600–1100 ms
|
||
("F02-1", 11, -44, 2),
|
||
("F03-2", 9, -14, -3),
|
||
("F03-4", 9, -52, -42),
|
||
("F06-1", 10, -66, -21),
|
||
],
|
||
)
|
||
def test_f3_legacy_drift_matches_plan(name, n_anchors, lo, hi):
|
||
from ml_porosity.data.las_io import load_well
|
||
|
||
w = load_well(name)
|
||
cs = w.checkshot[w.checkshot.twt_ms.between(600, 1100)]
|
||
assert len(cs) == n_anchors
|
||
d = misfit_at_anchors(legacy_sonic_tdr(w.md, w.curve("DT")), cs.md, cs.twt_ms)
|
||
assert d.min() == pytest.approx(lo, abs=1.0)
|
||
assert d.max() == pytest.approx(hi, abs=1.0)
|
||
|
||
|
||
@needs_f3
|
||
@pytest.mark.data
|
||
@pytest.mark.parametrize("name", ["F02-1", "F03-2", "F03-4", "F06-1"])
|
||
def test_f3_calibrated_tdr(name):
|
||
from ml_porosity.data.las_io import load_well
|
||
|
||
w = load_well(name)
|
||
cs = w.checkshot
|
||
base = sonic_tdr(w.md, w.curve("DT"), md0=cs.md.iloc[0], t0_ms=cs.twt_ms.iloc[0])
|
||
cal = calibrate_to_anchors(base, cs.md, cs.twt_ms)
|
||
np.testing.assert_allclose(misfit_at_anchors(cal, cs.md, cs.twt_ms), 0.0, atol=1e-6)
|
||
assert np.all(np.diff(cal.twt_ms) > 0)
|
||
v = cal.interval_velocity()
|
||
assert 1000 < np.percentile(v, 1) and np.percentile(v, 99) < 6000
|
||
|
||
|
||
@needs_f3
|
||
@pytest.mark.data
|
||
@pytest.mark.parametrize(
|
||
"name,mean_ms",
|
||
[("F02-1", 40.5), ("F03-2", 40.2), ("F03-4", 25.2), ("F06-1", -14.6)], # plan §1.2c
|
||
)
|
||
def test_f3_dt_model_offset_matches_plan(name, mean_ms):
|
||
from ml_porosity.data.las_io import load_td_source, load_well
|
||
|
||
cs = load_well(name).checkshot
|
||
cs = cs[cs.md.between(500, 1300)]
|
||
dm = TDR.from_pairs(load_td_source(name, "dt_model"))
|
||
d = misfit_at_anchors(dm, cs.md, cs.twt_ms)
|
||
assert d.mean() == pytest.approx(mean_ms, abs=0.2)
|