cs4486-amp-pd-checkpoints / code /ampd /protein_representation.py
fryabcfry's picture
Publish AMP-PD checkpoints, portable loaders and verified metrics (part 3)
04ae9bb verified
Raw History Blame Contribute Delete
8.48 kB
"""Fold-fitted protein representations for the zero-history distribution MLP."""
from __future__ import annotations
import json
from pathlib import Path
import warnings
import numpy as np
import torch
from ampd.current_protein import MONTHS
from ampd.evaluation.decision import smape_optimal_point
from ampd.models import build_model
WIDTH = 16
METHODS = ("none", "pca", "select", "dae", "missing", "count", "shuffle_pca")
def current_process(months):
months = np.asarray(months, dtype=float)
flags = np.where(months[:, None] == MONTHS, 1., -1.)
return np.c_[flags, np.full((len(months), 3), -1.), months]
def standardize_fit(x):
mean, scale = x.mean(axis=0), x.std(axis=0)
return mean, np.where(scale > 1e-12, scale, 1.)
def protein_matrix(proteins, params):
p = np.log1p(np.asarray(proteins, dtype=float))
p = np.where(np.isfinite(p), p, params["p_impute"])
return np.clip((p - params["p_mean"]) / params["p_scale"], -5., 5.)
def encode(method, proteins, params):
if method == "none":
return np.zeros((len(proteins), WIDTH))
if method == "count":
return np.isfinite(proteins).sum(axis=1, keepdims=True).astype(float)
if method == "missing":
x = (~np.isfinite(proteins)).astype(float)
x = (x - params["missing_mean"]) / params["missing_scale"]
return (x - params["pca_center"]) @ params["pca_components"].T
x = protein_matrix(proteins, params)
if method in {"pca", "shuffle_pca"}:
return (x - params["pca_center"]) @ params["pca_components"].T
if method == "select":
return x[:, params["selected_indices"]]
if method == "dae":
hidden = np.maximum(x @ params["encoder_w1"].T + params["encoder_b1"], 0)
return hidden @ params["encoder_w2"].T + params["encoder_b2"]
raise ValueError(method)
def fit_dae(x, observed, seed, epochs=100):
"""Train only on observed training abundances; no UPDRS input or objective."""
torch.manual_seed(seed)
encoder = torch.nn.Sequential(torch.nn.Linear(x.shape[1], 64), torch.nn.ReLU(),
torch.nn.Linear(64, WIDTH))
decoder = torch.nn.Sequential(torch.nn.Linear(WIDTH, 64), torch.nn.ReLU(),
torch.nn.Linear(64, x.shape[1]))
model = torch.nn.Sequential(encoder, decoder)
optimizer = torch.optim.AdamW(model.parameters(), lr=.001, weight_decay=.0001)
tensor = torch.tensor(x, dtype=torch.float32)
mask = torch.tensor(observed, dtype=torch.float32)
rng = np.random.default_rng(seed)
history = []
for epoch in range(epochs):
total, denominator = 0., 0.
order = rng.permutation(len(x))
for start in range(0, len(x), 64):
rows = order[start:start + 64]
clean = tensor[rows]
noisy = clean * (torch.rand_like(clean) >= .2) + .1 * torch.randn_like(clean)
reconstruction = model(noisy)
squared = ((reconstruction - clean) ** 2 * mask[rows]).sum()
count = mask[rows].sum().clamp_min(1)
loss = squared / count
optimizer.zero_grad()
loss.backward()
optimizer.step()
total += float(squared.detach())
denominator += float(count)
history.append({"epoch": epoch + 1, "reconstruction_mse": total / denominator})
return {"encoder_w1": encoder[0].weight.detach().numpy(),
"encoder_b1": encoder[0].bias.detach().numpy(),
"encoder_w2": encoder[2].weight.detach().numpy(),
"encoder_b2": encoder[2].bias.detach().numpy()}, history
def fit_representation(months, proteins, method, y=None, seed=42, dae_epochs=100):
from sklearn.decomposition import PCA
from sklearn.linear_model import Ridge
if method not in METHODS:
raise ValueError(method)
process = current_process(months)
process_mean, process_scale = standardize_fit(process)
params = {"process_mean": process_mean, "process_scale": process_scale}
history = []
if method not in {"none", "count", "missing"}:
log = np.log1p(proteins)
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
impute = np.nan_to_num(np.nanmedian(log, axis=0))
filled = np.where(np.isfinite(log), log, impute)
mean, scale = standardize_fit(filled)
params.update(p_impute=impute, p_mean=mean, p_scale=scale)
x = protein_matrix(proteins, params)
if method in {"pca", "shuffle_pca", "missing"}:
if method == "missing":
missing = (~np.isfinite(proteins)).astype(float)
mean, scale = standardize_fit(missing)
params.update(missing_mean=mean, missing_scale=scale)
x = (missing - mean) / scale
pca = PCA(n_components=min(WIDTH, min(x.shape)), svd_solver="full").fit(x)
params.update(pca_components=pca.components_, pca_center=pca.mean_)
elif method == "select":
if y is None:
raise ValueError("Supervised selection requires training labels")
# Remove the linear effect of the allowed current-month inputs when
# ranking proteins. This uses training labels only, never inference labels.
z = (process - process_mean) / process_scale
residual_x = x - Ridge(alpha=100.).fit(z, x).predict(z)
scores = []
for j in range(y.shape[1]):
valid = np.isfinite(y[:, j])
if valid.sum() < 3:
continue
residual_y = y[valid, j] - Ridge(alpha=100.).fit(z[valid], y[valid, j]).predict(z[valid])
a = residual_x[valid] - residual_x[valid].mean(axis=0)
b = residual_y - residual_y.mean()
denom = np.sqrt((a * a).sum(axis=0) * (b * b).sum())
scores.append(np.divide(np.abs(a.T @ b), denom, out=np.zeros(x.shape[1]), where=denom > 1e-12))
if not scores:
raise ValueError("Insufficient training labels for supervised selection")
score = np.mean(scores, axis=0)
indices = np.argsort(-score, kind="stable")[:WIDTH]
params.update(selected_indices=indices, selection_scores=score)
elif method == "dae":
encoder, history = fit_dae(x, np.isfinite(proteins), seed, dae_epochs)
params.update(encoder)
features = encode(method, proteins, params)
mean, scale = standardize_fit(features)
params.update(feature_mean=mean, feature_scale=scale)
return params, history
def transform_representation(months, proteins, method, params):
process = (current_process(months) - params["process_mean"]) / params["process_scale"]
features = (encode(method, proteins, params) - params["feature_mean"]) / params["feature_scale"]
padded = np.zeros((len(months), WIDTH))
padded[:, :features.shape[1]] = features
return np.c_[process, padded].astype(np.float32)
class RepresentationPredictor:
"""Stateless zero-history inference, including the fitted feature extractor."""
def __init__(self, bundle):
bundle = Path(bundle)
self.spec = json.loads((bundle / "model.json").read_text())
with np.load(bundle / "extractor.npz", allow_pickle=False) as archive:
self.params = dict(archive)
self.models = []
for seed in self.spec["seeds"]:
model = build_model(self.spec["model"], 14 + WIDTH, 16)
model.load_state_dict(torch.load(bundle / f"seed_{seed}.pt", map_location="cpu", weights_only=True))
self.models.append(model.eval())
def predict(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")
months = np.asarray([e["visit_month"] for e in events], dtype=float)
proteins = np.asarray([e["proteins"] for e in events], dtype=float)
if np.any(proteins < 0):
raise ValueError("Protein abundance must be nonnegative")
x = transform_representation(months, proteins, self.spec["method"], self.params)
with torch.no_grad():
probabilities = torch.stack([model(torch.from_numpy(x)).softmax(-1) for model in self.models]).mean(0)
return smape_optimal_point(probabilities).numpy()