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>
This commit is contained in:
2026-10-27 06:03:33 +00:00
parent a76e61398a
commit cee2ff0859
84 changed files with 8793 additions and 460 deletions

View File

@@ -0,0 +1,93 @@
"""Point ties for B3a–d on D2 (plan §5.3) and warp-error diagnostics.
Every tie returns t(τ) on the fine τ grid (strictly increasing), so that `labels_for_warps` can be shared:
- B3a: single-point tie at the shallowest anchor, t = τ + (t_0 − τ_0) — analogous to the thesis's "integrated DT
hung at the first marker" (no drift correction).
- B3b: marker-calibrated sonic (tdr.calibrate_to_anchors) after removing anchors that break monotonicity.
- B3c: B3b + local shifts from synthetic–seismic correlation (synthetic.local_shifts), as in a manual tie.
- B3d: M2-weighted mean warp, Σ_s π_s t_s(τ) (the plan states MAP; the mean is more stable than the MAP over a
finite importance sample — recorded as a deviation in protocol_p2).
"""
from __future__ import annotations
import numpy as np
from ml_porosity.data.synthetic import local_shifts
from ml_porosity.data.tdr import TDR, calibrate_to_anchors
from ml_porosity.registration.labels import invert_warps
def clean_monotone(tau_k, t_k) -> tuple[np.ndarray, np.ndarray]:
"""Longest strictly increasing subsequence of (τ_k, t_k) sorted by τ (pick noise can make t non-monotone)."""
o = np.argsort(tau_k)
tau, t = np.asarray(tau_k, float)[o], np.asarray(t_k, float)[o]
n = len(t)
if n == 0:
return tau, t
best, prev = np.ones(n, int), np.full(n, -1)
for i in range(n):
for j in range(i):
if t[j] < t[i] and tau[j] < tau[i] and best[j] + 1 > best[i]:
best[i], prev[i] = best[j] + 1, j
k, keep = int(np.argmax(best)), []
while k >= 0:
keep.append(k)
k = prev[k]
keep = keep[::-1]
return tau[keep], t[keep]
def tie_b3a(tau_k, t_k, tau: np.ndarray) -> np.ndarray:
k = int(np.argmin(tau_k))
return tau + (t_k[k] - tau_k[k])
def tie_b3b(tau_k, t_k, tau: np.ndarray) -> np.ndarray:
tk, ttk = clean_monotone(tau_k, t_k)
if len(tk) < 2:
return tie_b3a(tk, ttk, tau) if len(tk) else tau.copy()
return calibrate_to_anchors(TDR(tau, tau, "sonic"), tk, ttk).time_at(tau)
def tie_b3c(t_b3b: np.ndarray, tau: np.ndarray, syn: np.ndarray, seis: np.ndarray, t_out: np.ndarray,
dt_ms: float = 4.0, r_min: float = 0.3, smooth: int = 3, **kw) -> np.ndarray:
"""syn: synthetic under the B3b tie on t_out; seis: centre trace on t_out. Shifts s(t) (positive = synthetic
must be later) from windows with r ≥ r_min, smoothed by a moving average over `smooth` windows, then
t(τ) = t_B3b(τ) + s(t_B3b(τ)); strict monotonicity is enforced (dt/dτ ≥ 0.05)."""
c, s, r = local_shifts(syn, seis, dt_ms, **kw)
ok = r >= r_min
if ok.sum() == 0:
return t_b3b.copy()
tc, sc = t_out[c[ok]], s[ok]
if smooth > 1 and len(sc) >= smooth:
sc = np.convolve(np.pad(sc, smooth // 2, mode="edge"), np.ones(smooth) / smooth, "valid")
t = t_b3b + np.interp(t_b3b, tc, sc)
return np.maximum.accumulate(t - 0.05 * tau) + 0.05 * tau
def warp_mean(t_of_tau: np.ndarray, logw: np.ndarray) -> np.ndarray:
w = np.exp(logw - np.max(logw))
return (w / w.sum()) @ np.atleast_2d(t_of_tau)
def warp_errors(t_of_tau: np.ndarray, tau: np.ndarray, t_true_of_tau: np.ndarray, t_eval: np.ndarray,
logw: np.ndarray | None = None) -> dict:
"""Warp error in τ(t) on t_eval (ms). One warp: RMSE. Several warps: RMSE of the weighted mean,
90% coverage (weighted 5–95% quantiles) and mean interval width."""
tau_true = invert_warps(t_true_of_tau, tau, t_eval)[0]
T = invert_warps(t_of_tau, tau, t_eval)
if len(T) == 1:
return {"warp_rmse_ms": float(np.sqrt(np.nanmean((T[0] - tau_true) ** 2)))}
w = np.ones(len(T)) if logw is None else np.exp(logw - np.max(logw))
w = w / w.sum()
mean = w @ T
o = np.argsort(T, 0)
cw = np.cumsum(w[o], 0)
Ts = np.take_along_axis(T, o, 0)
lo = Ts[np.argmax(cw >= 0.05, 0), np.arange(T.shape[1])]
hi = Ts[np.argmax(cw >= 0.95, 0), np.arange(T.shape[1])]
return {"warp_rmse_ms": float(np.sqrt(np.mean((mean - tau_true) ** 2))),
"warp_cov90": float(np.mean((tau_true >= lo) & (tau_true <= hi))),
"warp_width90_ms": float(np.mean(hi - lo))}