cs4486-amp-pd-checkpoints / code /ampd /omics_boost.py
fryabcfry's picture
Publish AMP-PD checkpoints, portable loaders and verified metrics (part 3)
04ae9bb verified
Raw History Blame Contribute Delete
7.56 kB
"""Supervised latent and nonlinear current-omics correction branches."""
from __future__ import annotations
import json
from pathlib import Path
import warnings
import numpy as np
import torch # Load the compatible OpenMP runtime before LightGBM on macOS.
import lightgbm as lgb
from ampd.current_protein import errors
from ampd.protein_representation import RepresentationPredictor
from ampd.protein_residual import correct, fit_residual, choose_weights
TREE_METHODS = ("month", "quality", "protein", "ratio", "combined")
BIOLOGY = ("pls", "protein", "ratio", "combined")
QUANTILES = (.25, .5)
COMPONENTS = (2, 4, 8, 16)
ALPHAS = (10., 100., 1000.)
def log_ratio(proteins, peptides, parents):
parents = np.asarray(parents, dtype=int)
denominator = np.full_like(peptides, np.nan, dtype=float)
known = parents >= 0
denominator[:, known] = proteins[:, parents[known]]
return np.log1p(peptides) - np.log1p(denominator)
def omics_features(proteins, peptides, parents, method):
if method == "month":
return np.empty((len(proteins), 0))
counts = np.c_[np.isfinite(proteins).sum(1), np.isfinite(peptides).sum(1)]
if method == "quality":
return np.c_[counts, ~np.isfinite(proteins), ~np.isfinite(peptides)].astype(float)
if method == "protein":
return np.c_[counts, np.log1p(proteins)]
ratios = log_ratio(proteins, peptides, parents)
if method == "ratio":
return np.c_[counts, ratios]
if method == "combined":
return np.c_[counts, np.log1p(proteins), ratios]
raise ValueError(method)
def request_features(months, features):
"""One current sample is expanded to four requested prediction horizons."""
horizon = np.tile([0., 6., 12., 24.], len(months))
month = np.repeat(months, 4)
return np.c_[month, horizon, month+horizon, np.repeat(features, 4, axis=0)]
def fit_pls(proteins, residuals, components):
from sklearn.cross_decomposition import PLSRegression
log = np.log1p(proteins)
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.)
# Current UPDRS1-3 only: no invented labels for missing future horizons.
response = residuals[:, [0, 4, 8]]
complete = np.isfinite(response).all(1)
if complete.sum() <= components:
raise ValueError("Insufficient complete training responses for PLS")
response = response[complete]
yscale = response.std(0)
response = (response-response.mean(0)) / np.where(yscale > 1e-12, yscale, 1.)
model = PLSRegression(n_components=components, scale=False, max_iter=1000).fit(x[complete], response)
center = x[complete].mean(0)
z = (x-center) @ model.x_rotations_
zscale = z.std(0)
return {"impute": median, "mean": mean, "scale": scale, "center": center,
"rotations": model.x_rotations_, "z_mean": z.mean(0),
"z_scale": np.where(zscale > 1e-12, zscale, 1.)}
def transform_pls(proteins, params):
log = np.log1p(proteins)
filled = np.where(np.isfinite(log), log, params["impute"])
x = np.clip((filled-params["mean"])/params["scale"], -5., 5.)
z = (x-params["center"]) @ params["rotations"]
return (z-params["z_mean"]) / params["z_scale"]
def select_pls(train_p, train_y, train_base, gate_p, gate_y, gate_base):
best, candidates = None, []
for components in COMPONENTS:
params = fit_pls(train_p, train_y-train_base, components)
x, v = transform_pls(train_p, params), transform_pls(gate_p, params)
for alpha in ALPHAS:
coef = fit_residual(x, train_y, train_base, alpha)
weights, score, _ = choose_weights(gate_y, gate_base, v @ coef.T)
row = {"components": components, "alpha": alpha, "weights": weights, "gate_smape": score}
candidates.append(row)
if best is None or score < best["gate_smape"] - 1e-10:
best = row
return best, candidates
def train_tree(x, residual, quantile, seed, rounds=400, validation=None):
params = {"objective": "quantile", "alpha": quantile, "metric": "None", "learning_rate": .03,
"num_leaves": 7, "min_data_in_leaf": 40, "lambda_l2": 10., "max_bin": 63,
"feature_fraction": .8, "bagging_fraction": .8, "bagging_freq": 1,
"num_threads": 1, "verbosity": -1, "seed": seed,
"deterministic": True, "force_col_wise": True, "feature_pre_filter": False}
train = lgb.Dataset(x, label=residual, free_raw_data=True)
if validation is None:
return lgb.train(params, train, num_boost_round=rounds), {}
vx, vy, base = validation
valid = lgb.Dataset(vx, label=vy-base, reference=train, free_raw_data=True)
def evaluate(prediction, dataset):
return "smape", float(np.mean(errors(vy, np.clip(base+prediction, 0, 86)))), False
log = {}
model = lgb.train(params, train, num_boost_round=rounds, valid_sets=[valid], valid_names=["gate"],
feval=evaluate, callbacks=[lgb.early_stopping(35, verbose=False), lgb.record_evaluation(log)])
return model, log
class OmicsPredictor:
def __init__(self, bundle):
bundle = Path(bundle)
self.spec = json.loads((bundle / "model.json").read_text())
self.base = RepresentationPredictor(bundle / "baseline")
with np.load(bundle / "pls.npz", allow_pickle=False) as archive:
self.pls = dict(archive)
self.trees = {name: [lgb.Booster(model_file=str(bundle / name / f"target_{target}.txt"))
for target in range(4)] for name in TREE_METHODS}
def predict(self, events):
for event in events:
if set(event) != {"visit_id", "visit_month", "proteins", "peptides"}:
raise ValueError("Only current visit metadata, proteins and peptides are accepted")
if len(event["proteins"]) != len(self.base.spec["proteins"]) or len(event["peptides"]) != len(self.spec["peptides"]):
raise ValueError("Assay vocabulary mismatch")
protein_events = [{k: e[k] for k in ["visit_id", "visit_month", "proteins"]} for e in events]
baseline = self.base.predict(protein_events)
months = np.asarray([e["visit_month"] for e in events], dtype=float)
proteins = np.asarray([e["proteins"] for e in events], dtype=float)
peptides = np.asarray([e["peptides"] for e in events], dtype=float)
if np.any(peptides < 0):
raise ValueError("Peptide abundance must be nonnegative")
result = {"none": baseline}
z = transform_pls(proteins, self.pls)
result["pls"] = correct(baseline, z @ self.pls["coef"].T, self.pls["weights"])
for method, models in self.trees.items():
features = omics_features(proteins, peptides, self.spec["parents"], method)
x = request_features(months, features)
residual = np.concatenate([model.predict(x, num_threads=1).reshape(len(events), 4) for model in models], axis=1)
result[method] = correct(baseline, residual, self.spec["weights"][method])
result["selected"] = baseline.copy().astype(float)
for target, name in enumerate(self.spec["selected"]):
result["selected"][:, target*4:(target+1)*4] = result[name][:, target*4:(target+1)*4]
return result