fryabcfry's picture
Publish AMP-PD checkpoints, portable loaders and verified metrics (part 4)
0d48417 verified
Raw History Blame Contribute Delete
8.89 kB
"""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