File size: 8,894 Bytes
0d48417 | 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | """Target-specific supervised projections and metric-aligned corrections."""
from __future__ import annotations
import json
from pathlib import Path
import warnings
import numpy as np
import torch
from ampd.current_protein import errors
from ampd.omics_boost import transform_pls
from ampd.protein_representation import RepresentationPredictor
DIMS = (1, 2, 4, 8)
ALPHAS = (10., 100., 1000.)
PENALTIES = (.001, .01)
STEPS = (100, 300)
WEIGHTS = (0., .1, .25, .5, 1.)
METHODS = ("ridge", "smape_linear", "smape_mlp", "prototype", "missing_ridge")
def input_values(proteins, missing=False):
return (~np.isfinite(proteins)).astype(float) if missing else proteins
def fit_target_projection(proteins, response, components, missing=False):
from sklearn.cross_decomposition import PLSRegression
values = input_values(proteins, missing)
log = np.log1p(values)
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
median = np.nan_to_num(np.nanmedian(log, axis=0))
filled = np.where(np.isfinite(log), log, median)
mean, scale = filled.mean(0), filled.std(0)
scale = np.where(scale > 1e-12, scale, 1.)
x = np.clip((filled-mean)/scale, -5., 5.)
valid = np.isfinite(response)
if valid.sum() <= components:
raise ValueError("Too few observed current training labels")
center = x[valid].mean(0)
if np.std(response[valid]) < 1e-12:
rotations = np.zeros((x.shape[1], components))
else:
model = PLSRegression(n_components=components, scale=False, max_iter=1000).fit(x[valid], response[valid])
rotations = model.x_rotations_
z = (x-center) @ rotations
zscale = z.std(0)
return {"impute": median, "mean": mean, "scale": scale, "center": center,
"rotations": rotations, "z_mean": z.mean(0), "z_scale": np.where(zscale > 1e-12, zscale, 1.)}
def project(proteins, params, missing=False):
return transform_pls(input_values(proteins, missing), params)
def blend(base, prediction, weight):
if weight == 0:
return base.copy()
return np.clip(base + weight * (prediction-base), 0., 86.)
def choose_weight(y, base, prediction):
scores = [float(np.nanmean(errors(y, blend(base, prediction, w)))) for w in WEIGHTS]
minimum = min(scores)
index = next(i for i, score in enumerate(scores) if score <= minimum + 1e-10)
return WEIGHTS[index], scores[index]
def fit_direct(x, y, baseline, penalty, nonlinear, seed, max_steps=300, snapshots=STEPS):
"""Full-batch exact SMAPE + correction-size penalty, zero initialized output.
Center hidden activations on the training partition, so the correction has
no free constant offset. Prediction uses the stored training mean only.
"""
torch.manual_seed(seed)
xt = torch.tensor(x, dtype=torch.float64)
yt = torch.tensor(np.nan_to_num(y), dtype=torch.float64)
bt = torch.tensor(baseline, dtype=torch.float64)
mask = torch.tensor(np.isfinite(y), dtype=torch.bool)
if nonlinear:
w1 = torch.nn.Parameter(torch.randn(16, x.shape[1], dtype=torch.float64) / np.sqrt(x.shape[1]))
b1 = torch.nn.Parameter(torch.zeros(16, dtype=torch.float64))
w2 = torch.nn.Parameter(torch.zeros(4, 16, dtype=torch.float64))
trainable = [w1, b1, w2]
else:
w2 = torch.nn.Parameter(torch.zeros(4, x.shape[1], dtype=torch.float64))
trainable = [w2]
optimizer = torch.optim.Adam(trainable, lr=.03)
result, logs = {}, []
for step in range(1, max_steps+1):
hidden = torch.tanh(xt @ w1.T+b1) if nonlinear else xt
delta = (hidden-hidden.mean(0)) @ w2.T
point = torch.clamp(bt+delta, 0., 86.)
per_value = (point-yt).abs() / ((point.abs()+yt.abs())/2+1)
loss = per_value[mask].mean() + penalty * (delta**2).mean()
if not torch.isfinite(loss):
raise ValueError("Nonfinite SMAPE optimization")
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(trainable, 5.)
optimizer.step()
if step in snapshots or step == max_steps:
with torch.no_grad():
h = torch.tanh(xt @ w1.T+b1) if nonlinear else xt
params = {"nonlinear": np.array(nonlinear), "hidden_mean": h.mean(0).numpy().copy(),
"output_w": w2.detach().numpy().copy()}
if nonlinear:
params.update(input_w=w1.detach().numpy().copy(), input_b=b1.detach().numpy().copy())
result[step] = params
prediction = np.clip(baseline + direct_delta(x, params), 0, 86)
logs.append({"step": step, "train_smape": float(np.nanmean(errors(y, prediction))), "objective": float(loss.detach())})
return result, logs
def direct_delta(x, params):
hidden = np.tanh(x @ params["input_w"].T + params["input_b"]) if bool(params["nonlinear"]) else x
return (hidden-params["hidden_mean"]) @ params["output_w"].T
def fit_prototype(x, y, baseline, clusters, seed):
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=clusters, random_state=seed, n_init=10).fit(x)
residual = y-baseline
probabilities = np.empty((clusters, 4, 173))
for h in range(4):
valid = np.isfinite(residual[:, h])
label = np.rint(np.clip(residual[valid, h], -86, 86)).astype(int)+86
global_count = np.bincount(label, minlength=173).astype(float)+.001
prior = global_count/global_count.sum()
for cluster in range(clusters):
selected = valid & (kmeans.labels_ == cluster)
label = np.rint(np.clip(residual[selected, h], -86, 86)).astype(int)+86
count = np.bincount(label, minlength=173).astype(float)+5*prior
probabilities[cluster, h] = count/count.sum()
return {"centers": kmeans.cluster_centers_, "probabilities": probabilities}
def prototype_point(x, baseline, params, bandwidth):
distance = ((x[:, None, :]-params["centers"][None, :, :])**2).mean(2)
logits = -distance/(2*bandwidth**2)
weights = np.exp(logits-logits.max(1, keepdims=True))
weights /= weights.sum(1, keepdims=True)
probability = np.einsum("nk,khc->nhc", weights, params["probabilities"])
residual = np.arange(-86, 87, dtype=float)
candidates = np.arange(87, dtype=float)
points = np.empty_like(baseline, dtype=float)
for horizon in range(4):
for value in np.unique(baseline[:, horizon]):
use = baseline[:, horizon] == value
possible = np.clip(value+residual, 0, 86)
cost = 200*np.abs(possible[:, None]-candidates[None, :])/(possible[:, None]+candidates[None, :]+2)
points[use, horizon] = (probability[use, horizon] @ cost).argmin(1)
return points
class TargetPLSPredictor:
def __init__(self, bundle):
bundle = Path(bundle)
self.spec = json.loads((bundle / "model.json").read_text())
self.base = RepresentationPredictor(bundle / "baseline")
self.models = {}
for method in METHODS:
self.models[method] = []
for target in range(4):
with np.load(bundle / method / f"target_{target}.npz", allow_pickle=False) as archive:
self.models[method].append(dict(archive))
with np.load(bundle / "shared_pls.npz", allow_pickle=False) as archive:
self.shared = dict(archive)
def predict(self, events):
baseline = self.base.predict(events) # Enforces current-only schema.
p = np.asarray([e["proteins"] for e in events], dtype=float)
from ampd.protein_residual import correct
result = {"none": baseline, "shared_pls": correct(baseline,
transform_pls(p, self.shared) @ self.shared["coef"].T, self.shared["weights"])}
for method in METHODS:
result[method] = np.empty_like(baseline, dtype=float)
for target, model in enumerate(self.models[method]):
selection = self.spec["selections"][method][target]
z = project(p, model, method == "missing_ridge")
base = baseline[:, target*4:(target+1)*4]
if method in {"ridge", "missing_ridge"}:
prediction = np.clip(base+z @ model["coef"].T, 0, 86)
elif method in {"smape_linear", "smape_mlp"}:
prediction = np.clip(base+direct_delta(z, model), 0, 86)
else:
prediction = prototype_point(z, base, model, selection["bandwidth"])
result[method][:, target*4:(target+1)*4] = blend(base, prediction, selection["weight"])
result["selected"] = baseline.copy().astype(float)
for target, method in enumerate(self.spec["selected"]):
result["selected"][:, target*4:(target+1)*4] = result[method][:, target*4:(target+1)*4]
return result
|