"""Shrinkable corrections around a frozen, zero-history baseline.""" from __future__ import annotations import json from pathlib import Path import numpy as np from ampd.current_protein import errors from ampd.protein_representation import RepresentationPredictor, transform_representation BRANCHES = ("pca", "select", "dae", "month", "missing", "count", "shuffle_pca") PROTEIN_BRANCHES = ("pca", "select", "dae") ALPHAS = (10., 100., 1000., 10000.) WEIGHTS = (0., .1, .25, .5, 1.) def branch_features(months, proteins, method, params): full = transform_representation(months, proteins, "none" if method == "month" else method, params) # Float64 keeps single-sample and batch residual BLAS paths consistent. return (full[:, :14] if method == "month" else full[:, 14:]).astype(np.float64) def fit_residual(x, y, baseline, alpha): from sklearn.linear_model import Ridge coefficients = [] for j in range(y.shape[1]): observed = np.isfinite(y[:, j]) if not observed.any(): raise ValueError("Residual training head has no observed labels") model = Ridge(alpha=alpha, fit_intercept=False, solver="cholesky").fit( x[observed], y[observed, j] - baseline[observed, j]) coefficients.append(model.coef_) return np.stack(coefficients) def correct(baseline, residual, weights): weights = np.asarray(weights, dtype=float) if weights.shape == (4,): weights = np.repeat(weights, 4) result = np.clip(baseline + residual * weights, 0., 86.) # Zero gate is an exact baseline fallback, including any baseline precision. return np.where(weights == 0, baseline, result) def choose_weights(y, baseline, residual): selected, losses = [], [] for target in range(4): cols = slice(target * 4, (target + 1) * 4) scores = [float(np.nanmean(errors(y[:, cols], np.clip( baseline[:, cols] + weight * residual[:, cols], 0., 86.)))) for weight in WEIGHTS] minimum = min(scores) # Ties prefer the smaller correction, starting with zero. index = next(i for i, score in enumerate(scores) if score <= minimum + 1e-10) selected.append(WEIGHTS[index]) losses.append(scores) score = float(np.nanmean(errors(y, correct(baseline, residual, selected)))) return selected, score, losses def select_correction(x, y, baseline, gate_x, gate_y, gate_baseline): candidates, best = [], None for alpha in ALPHAS: coef = fit_residual(x, y, baseline, alpha) weights, score, target_losses = choose_weights(gate_y, gate_baseline, gate_x @ coef.T) candidate = {"alpha": alpha, "weights": weights, "gate_smape": score, "target_weight_scores": target_losses} candidates.append(candidate) if best is None or score < best["gate_smape"] - 1e-10: best = candidate return best, candidates class ResidualPredictor: """A frozen baseline plus fitted numeric corrections; no historical state.""" def __init__(self, bundle): bundle = Path(bundle) self.spec = json.loads((bundle / "model.json").read_text()) self.baseline = RepresentationPredictor(bundle / "baseline") self.params, self.corrections = {}, {} for name in self.spec["branches"]: with np.load(bundle / name / "extractor.npz", allow_pickle=False) as archive: self.params[name] = dict(archive) with np.load(bundle / name / "residual.npz", allow_pickle=False) as archive: self.corrections[name] = dict(archive) def predict(self, events): baseline = self.baseline.predict(events) # Also enforces the strict input schema. months = np.asarray([e["visit_month"] for e in events], dtype=float) proteins = np.asarray([e["proteins"] for e in events], dtype=float) result = {"none": baseline} for name in self.spec["branches"]: x = branch_features(months, proteins, name, self.params[name]) correction = self.corrections[name] result[name] = correct(baseline, x @ correction["coef"].T, correction["weights"]) result["selected"] = result[self.spec["selected"]].copy() return result