File size: 6,279 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 139 140 141 142 143 144 145 146 147 148 149 150 151 | """Current-sample protein baselines; prediction needs no patient history or ID.
Only training routines import sklearn. Exported bundles contain numeric learned
parameters and an assay vocabulary, never patient records or labels.
"""
from __future__ import annotations
import json
from pathlib import Path
import warnings
import numpy as np
MONTHS = np.array([0, 6, 12, 18, 24, 36, 48, 60, 72, 84])
FACTORS = np.array([0., .25, .5, .75, 1.])
MODES = {"month", "protein", "month_protein", "month_missing",
"month_count", "month_protein_missing"}
def errors(y, p):
return 200 * np.abs(y - p) / (np.abs(y) + np.abs(p) + 2)
def shuffle_abundance(proteins, months, seed):
"""Training-only negative control; preserve each sample's missingness.
Independently permute observed values of each assay within current month.
This is an exploratory ablation, not a patient-exchangeable permutation test.
"""
rng = np.random.default_rng(seed)
result = np.array(proteins, copy=True)
for month in np.unique(months):
for column in range(result.shape[1]):
rows = np.flatnonzero((months == month) & np.isfinite(result[:, column]))
result[rows, column] = rng.permutation(result[rows, column])
return result
def extra_features(months, proteins, mode):
months = np.asarray(months, dtype=float)
blocks = []
if mode.startswith("month"):
blocks.extend([months[:, None] / 84, (months[:, None] / 84) ** 2,
(months[:, None] == MONTHS).astype(float)])
if "missing" in mode:
blocks.append((~np.isfinite(proteins)).astype(float))
if mode == "month_count":
blocks.append(np.isfinite(proteins).sum(axis=1, keepdims=True).astype(float))
return np.concatenate(blocks, axis=1) if blocks else np.empty((len(months), 0))
def fit_transformer(months, proteins, mode, n_pca):
from sklearn.decomposition import PCA
if mode not in MODES:
raise ValueError(mode)
extra = extra_features(months, proteins, mode)
params = {"e_mean": extra.mean(axis=0), "e_scale": extra.std(axis=0)}
params["e_scale"] = np.where(params["e_scale"] > 1e-12, params["e_scale"], 1.)
if "protein" in mode:
p = np.where(np.isfinite(proteins), np.log1p(proteins), np.nan)
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
impute = np.nanmedian(p, axis=0)
impute = np.nan_to_num(impute)
p = np.where(np.isfinite(p), p, impute)
mean, scale = p.mean(axis=0), p.std(axis=0)
scale = np.where(scale > 1e-12, scale, 1.)
p = (p - mean) / scale
params.update(p_impute=impute, p_mean=mean, p_scale=scale)
if n_pca:
pca = PCA(n_components=min(n_pca, min(p.shape)), svd_solver="full").fit(p)
params.update(p_components=pca.components_, p_center=pca.mean_)
return params
def transform(months, proteins, mode, params):
extra = extra_features(months, proteins, mode)
blocks = [(extra - params["e_mean"]) / params["e_scale"]]
if "protein" in mode:
p = np.where(np.isfinite(proteins), np.log1p(proteins), np.nan)
p = np.where(np.isfinite(p), p, params["p_impute"])
p = (p - params["p_mean"]) / params["p_scale"]
if "p_components" in params:
p = (p - params["p_center"]) @ params["p_components"].T
blocks.append(p)
return np.concatenate(blocks, axis=1)
def fit_heads(x, y, alpha):
from sklearn.linear_model import Ridge
coefficients, intercepts = [], []
for column in range(y.shape[1]):
valid = np.isfinite(y[:, column])
if not valid.any():
raise ValueError("Training partition has no labels for a prediction head")
model = Ridge(alpha=alpha, solver="cholesky").fit(x[valid], y[valid, column])
coefficients.append(model.coef_)
intercepts.append(model.intercept_)
return np.stack(coefficients), np.asarray(intercepts)
def predict_heads(x, coef, intercept, factors=1.):
return np.maximum(x @ coef.T + intercept, 0) * factors
def choose_factors(y, predictions):
losses = np.stack([np.nanmean(errors(y, predictions * f), axis=0) for f in FACTORS])
factors = FACTORS[np.argmin(losses, axis=0)]
return factors, float(np.nanmean(errors(y, predictions * factors)))
def constant_prediction(y):
candidates = np.arange(87, dtype=float)
return np.asarray([candidates[np.argmin(np.nanmean(
errors(y[:, j, None], candidates[None, :]), axis=0))] for j in range(y.shape[1])])
def save_bundle(path, mode, vocabulary, params, coef, intercept, factors):
path = Path(path)
path.mkdir(parents=True, exist_ok=True)
(path / "model.json").write_text(json.dumps({"mode": mode, "proteins": list(vocabulary),
"history_window": 0, "outputs": [f"updrs_{t}_{h}" for t in range(1, 5)
for h in [0, 6, 12, 24]]}, indent=2) + "\n")
np.savez(path / "parameters.npz", **params, coef=coef, intercept=intercept, factors=factors)
class CurrentProteinPredictor:
"""Stateless inference. Patient IDs, labels and history are not accepted."""
def __init__(self, bundle):
bundle = Path(bundle)
self.spec = json.loads((bundle / "model.json").read_text())
with np.load(bundle / "parameters.npz", allow_pickle=False) as archive:
self.params = dict(archive)
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 vector does not match training assay vocabulary")
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(months, proteins, self.spec["mode"], self.params)
return predict_heads(x, self.params["coef"], self.params["intercept"], self.params["factors"])
|