- 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>
75 lines
3.2 KiB
Python
75 lines
3.2 KiB
Python
"""B1: post-stack model-based AI inversion (PyLops) with a horizon-guided interpolated LFM, followed by φ = a + b·AI."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
from pylops.avo.poststack import PoststackInversion, PoststackLinearModelling
|
|
from scipy import signal
|
|
|
|
from ml_porosity.data.wavelet import estimate_wavelet_ls
|
|
|
|
|
|
def fill_edges(x: np.ndarray) -> np.ndarray:
|
|
"""Replace NaNs by linear interpolation, holding values constant at both ends."""
|
|
x = np.asarray(x, dtype=float).copy()
|
|
ok = np.isfinite(x)
|
|
x[~ok] = np.interp(np.flatnonzero(~ok), np.flatnonzero(ok), x[ok])
|
|
return x
|
|
|
|
|
|
def lowpass(x: np.ndarray, f_c_hz: float, dt_ms: float = 4.0, order: int = 4) -> np.ndarray:
|
|
sos = signal.butter(order, f_c_hz, fs=1000.0 / dt_ms, output="sos")
|
|
return signal.sosfiltfilt(sos, x, padtype="even")
|
|
|
|
|
|
def strat_map(t: np.ndarray, h_from: np.ndarray, h_to: np.ndarray) -> np.ndarray:
|
|
"""Map times at the target well to stratigraphically equivalent times at the source well: piecewise-linear
|
|
interpolation through the horizon pairs (h_from → h_to), with a parallel shift beyond the outermost horizons."""
|
|
ok = np.isfinite(h_from) & np.isfinite(h_to)
|
|
a, b = h_from[ok], h_to[ok]
|
|
order = np.argsort(a)
|
|
a, b = a[order], b[order]
|
|
keep = np.concatenate([[True], (np.diff(a) > 0) & (np.diff(b) > 0)])
|
|
a, b = a[keep], b[keep]
|
|
out = np.interp(t, a, b)
|
|
out[t < a[0]] = t[t < a[0]] + (b[0] - a[0])
|
|
out[t > a[-1]] = t[t > a[-1]] + (b[-1] - a[-1])
|
|
return out
|
|
|
|
|
|
def build_lfm(t, target_h, target_xy, wells: dict, f_c_hz: float = 6.0, power: float = 2.0) -> np.ndarray:
|
|
"""wells[name] = {"logai": (L,), "h": (H,), "xy": (2,)} on a common t axis. Returns the LFM of log(AI)."""
|
|
num = np.zeros_like(t, dtype=float)
|
|
den = 0.0
|
|
for w in wells.values():
|
|
lf = lowpass(fill_edges(w["logai"]), f_c_hz)
|
|
tw = strat_map(t, np.asarray(target_h), np.asarray(w["h"]))
|
|
val = np.interp(tw, t, lf)
|
|
d = max(float(np.hypot(*(np.asarray(target_xy) - np.asarray(w["xy"])))), 1.0)
|
|
wt = d ** (-power)
|
|
num += wt * val
|
|
den += wt
|
|
return num / den
|
|
|
|
|
|
def operator_reflectivity(logai: np.ndarray, nspike: int = 33) -> np.ndarray:
|
|
"""Reflectivity following the exact convention of PoststackLinearModelling (unit-spike wavelet)."""
|
|
spike = np.zeros(nspike)
|
|
spike[nspike // 2] = 1.0
|
|
G0 = PoststackLinearModelling(spike, len(logai), explicit=True).A
|
|
return G0 @ logai
|
|
|
|
|
|
def estimate_wavelet(train_logai: list[np.ndarray], train_seis: list[np.ndarray], mask: np.ndarray,
|
|
dt_ms: float = 4.0, length_ms: float = 128.0, damping: float = 0.05) -> np.ndarray:
|
|
refl = [operator_reflectivity(fill_edges(m))[mask] for m in train_logai]
|
|
return estimate_wavelet_ls(refl, [s[mask] for s in train_seis], dt_ms, length_ms, damping)
|
|
|
|
|
|
def invert(seis: np.ndarray, wav: np.ndarray, lfm: np.ndarray, e: float) -> np.ndarray:
|
|
"""Inverted log(AI): Tikhonov regularisation towards the LFM with epsI = e·trace(GᵀG)/n."""
|
|
G = PoststackLinearModelling(wav, len(seis), explicit=True).A
|
|
eps = e * np.trace(G.T @ G) / len(seis)
|
|
m, _ = PoststackInversion(seis, wav, m0=lfm, explicit=True, epsI=eps, simultaneous=False)
|
|
return np.asarray(m).ravel()
|