"""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))}