Files
ML-Porosity/tests/test_tdr.py
lamlvh a76e61398a P0 + P1: ml_porosity package, audited F3 data, LOWO baselines, protocol v2
P0
- ml_porosity/: survey geometry, LAS/TDR/SEG-Y readers (4 ms delay fix,
  invalid time scalar, header LUT for incomplete cubes), anti-aliased
  log-to-time resampling, CNN-GRU v2 with the thesis loss, 41+ unit tests.
- Full F3 Demo 2023 under data/ (text well/fault files tracked, bulk data
  ignored); the filtered copy and its cleanup notes are removed.
- SHA-256 manifests for F3, Volve, NLOG and the thesis outputs.

P1
- configs/protocol.yaml v2, locked by SHA-256 at 2026-10-26T04:28:09Z,
  before the first LOWO run (04:30:50Z); 05_lowo.py refuses to run on a
  hash mismatch. This commit postdates the runs.
- Wavelet/synthetic tie QC, residual misregistration, LOWO 5 seeds for
  B0a, B0b, B1, B2, B3a, B3b (scripts 04-06); gate G-A: no pivot.
- Volve D3: horizons, well picks, checkshots and the ST10010 cube read
  from data/Volve (scripts/07).
- D-1 label check against measured NLOG density (scripts/08): PHIE_DT
  stays the primary label.
- docs/plan.md, Volve and NLOG inventories, report.html.

Full-text reference PDFs in refs/ stay local (ignored).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 08:41:11 +00:00

160 lines
5.7 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 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
# ---------- tổng hợp, không cần dữ liệu ----------
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)
# Anchor lệch tới ±40 ms so với sonic, kiểu drift của F06-1.
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]) # ở 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)
# ngoài phạm vi anchor: dịch đều theo anchor gần nhất
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():
# Log tuyến tính theo thời gian được giữ nguyên; sin 200 Hz (trên Nyquist 125 Hz của 4 ms) bị lọc.
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 # khoảng trống 50 ms
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)]))
# ---------- trên dữ liệu F3 ----------
needs_f3 = pytest.mark.skipif(not (f3_wells_dir() / "Lasfiles").exists(), reason="thiếu dữ liệu giếng F3")
@needs_f3
@pytest.mark.data
@pytest.mark.parametrize(
"name,n_anchors,lo,hi",
[ # plan mục 1.2b và 1.2e: TWT tích phân DT − marker TD trong 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 mục 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)