File size: 5,904 Bytes
04ae9bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | """Frozen protein AE latent -> MLP residual prediction."""
from __future__ import annotations
import json
from pathlib import Path
import numpy as np
import torch
from sklearn.linear_model import Ridge
from ampd.ae_latent_selection import MONTHS, encode, errors, month_features, quality_features
from ampd.evaluation.decision import smape_optimal_point
from ampd.models import build_model
ARMS = ("all_latent_r", "selected_latent_r", "residual_ranked_r", "residual_fdr_r", "month_r", "missingness_r")
ALPHAS = (1., 10., 100., 1000.)
WEIGHTS = (0., .1, .25, .5, 1.)
TOP_K = (2, 4, 8, 16)
def current_process(months):
months = np.asarray(months, dtype=float)
return np.c_[np.where(months[:, None] == MONTHS, 1., -1.), -np.ones((len(months), 3)), months]
def fit_baseline_pre(months):
x = current_process(months)
scale = x.std(0)
return {"process_mean": x.mean(0), "process_scale": np.where(scale > 0, scale, 1.),
"feature_mean": np.zeros(16), "feature_scale": np.ones(16)}
def baseline_features(months, pre):
process = (current_process(months)-pre["process_mean"])/pre["process_scale"]
zeros = np.broadcast_to(-pre["feature_mean"]/pre["feature_scale"], (len(months), 16))
return np.c_[process, zeros].astype(np.float32)
class FrozenMonthMLP:
def __init__(self, bundle):
bundle = Path(bundle)
self.spec = json.loads((bundle/"model.json").read_text())
if self.spec["method"] != "none":
raise ValueError("Residual baseline must be the no-protein model")
self.pre = dict(np.load(bundle/"extractor.npz", allow_pickle=False))
self.models = []
for seed in self.spec["seeds"]:
model = build_model(self.spec["model"], 30, 16)
model.load_state_dict(torch.load(bundle/f"seed_{seed}.pt", weights_only=True, map_location="cpu"))
self.models.append(model.eval())
def predict(self, months):
x = torch.tensor(baseline_features(months, self.pre))
with torch.no_grad():
probabilities = torch.stack([model(x).softmax(-1) for model in self.models]).mean(0)
return smape_optimal_point(probabilities).numpy()
def fit_feature_pre(months, proteins, latent):
out = {}
for name, x in [("latent", latent), ("month", month_features(months)), ("quality", quality_features(proteins))]:
scale = x.std(0)
out[f"{name}_mean"], out[f"{name}_scale"] = x.mean(0), np.where(scale > 1e-12, scale, 1.)
return out
def features(months, proteins, latent, arm, pre):
if arm == "month_r":
name, x = "month", month_features(months)
elif arm == "missingness_r":
name, x = "quality", quality_features(proteins)
else:
name, x = "latent", latent
return (x-pre[f"{name}_mean"])/pre[f"{name}_scale"]
def fit_residual(x, y, baseline, arm, mask, alpha):
coefficients = []
for head in range(16):
active = np.ones(x.shape[1], dtype=bool) if arm in {"month_r", "missingness_r"} else mask[head//4]
valid = np.isfinite(y[:, head])
model = Ridge(alpha=alpha, fit_intercept=False, solver="cholesky").fit(
x[valid]*active, y[valid, head]-baseline[valid, head])
coefficients.append(model.coef_*active)
return np.stack(coefficients)
def correct(baseline, residual, weights):
weights = np.repeat(np.asarray(weights, dtype=float), 4)
prediction = np.clip(baseline+residual*weights, 0., 86.)
return np.where(weights == 0, baseline, prediction)
def choose_weights(y, baseline, residual):
weights = []
for target in range(4):
cols = slice(4*target, 4*(target+1))
scores = [float(np.nanmean(errors(y[:, cols], np.clip(baseline[:, cols]+w*residual[:, cols], 0, 86)))) for w in WEIGHTS]
selected = next(i for i, value in enumerate(scores) if value <= min(scores)+1e-10)
weights.append(WEIGHTS[selected])
return weights, float(np.nanmean(errors(y, correct(baseline, residual, weights))))
class AEResidualPredictor:
def __init__(self, bundle, arms=ARMS):
bundle = Path(bundle)
self.spec = json.loads((bundle/"model.json").read_text())
self.baseline = FrozenMonthMLP(bundle/"baseline")
self.pre = dict(np.load(bundle/"protein_preprocessor.npz", allow_pickle=False))
self.network = dict(np.load(bundle/"autoencoder_numeric.npz", allow_pickle=False))
self.feature_pre = dict(np.load(bundle/"feature_preprocessor.npz", allow_pickle=False))
if any(arm not in ARMS for arm in arms):
raise ValueError("Unknown residual arm")
self.heads = {arm: dict(np.load(bundle/f"{arm}.npz", allow_pickle=False)) for arm in arms}
def encode(self, proteins):
return encode(proteins, self.pre, self.network)
def components(self, events):
for event in events:
if set(event) != {"visit_id", "visit_month", "proteins"}:
raise ValueError("Only current visit_id, visit_month and proteins are accepted")
if len(event["proteins"]) != len(self.spec["proteins"]):
raise ValueError("Protein vocabulary mismatch")
proteins = np.asarray([e["proteins"] for e in events], dtype=float)
months = np.asarray([e["visit_month"] for e in events], dtype=float)
latent = self.encode(proteins)
baseline = self.baseline.predict(months)
residuals, predictions = {}, {"baseline": baseline}
for arm, head in self.heads.items():
x = features(months, proteins, latent, arm, self.feature_pre)
residuals[arm] = x @ head["coef"].T
predictions[arm] = correct(baseline, residuals[arm], head["weights"])
return {"latent": latent, "baseline": baseline, "raw_residuals": residuals, "predictions": predictions}
def predict(self, events):
return self.components(events)["predictions"]
|