File size: 6,003 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 | """Training-only longitudinal teachers; current-sample students at inference."""
from __future__ import annotations
import json
from pathlib import Path
import numpy as np
import pandas as pd
from ampd.omics_boost import fit_pls, transform_pls
from ampd.protein_representation import RepresentationPredictor, current_process, standardize_fit
from ampd.protein_residual import correct, fit_residual
TARGETS = [f"updrs_{i}" for i in range(1, 5)]
HORIZONS = np.array([0., 6., 12., 24.])
DIMS = (1, 2, 4, 8, 16)
ALPHAS = (1., 10., 100., 1000.)
ARMS = {f"{kind}_{teacher}": (kind, teacher)
for kind in ["protein", "missing", "month"] for teacher in ["projection", "level", "trend"]}
ARMS.update({"shuffle_level": ("shuffle", "level"), "shuffle_trend": ("shuffle", "trend")})
ARMS["protein_raw"] = ("protein", "raw")
def build_teacher(clinical, allowed_uids, predict_current):
"""Filter patient scope BEFORE calling the teacher or reading target values.
predict_current receives current visit metadata only. The resulting table
is a training target artifact and must never be placed in a model bundle.
"""
scoped = clinical.loc[clinical.uid.isin(set(allowed_uids))].sort_values(["uid", "visit_month"]).copy()
if scoped.empty:
raise ValueError("Empty teacher training scope")
predicted = np.asarray(predict_current(scoped[["visit_id", "visit_month"]]), dtype=float)
if predicted.shape != (len(scoped), 4):
raise ValueError("Current-score baseline prediction shape mismatch")
residual = scoped[TARGETS].to_numpy(float)-predicted
rows = []
for uid, indices in scoped.reset_index(drop=True).groupby("uid", sort=True).indices.items():
months = scoped.iloc[indices].visit_month.to_numpy(float)
row = {"uid": uid, "clinical_visits": len(indices)}
for target in range(4):
values = residual[indices, target]
observed = np.isfinite(values)
row[f"count_{target}"] = int(observed.sum())
if not observed.any():
row[f"mean_{target}"] = row[f"intercept_{target}"] = row[f"slope_{target}"] = np.nan
continue
row[f"mean_{target}"] = float(values[observed].mean())
# Fixed shrinkage toward zero patient residual; slope is per year.
x = np.c_[np.ones(observed.sum()), (months[observed]-24.)/12.]
coef = np.linalg.solve(x.T @ x + np.diag([2., 20.]), x.T @ values[observed])
row[f"intercept_{target}"], row[f"slope_{target}"] = map(float, coef)
rows.append(row)
return pd.DataFrame(rows).set_index("uid")
def teacher_targets(samples, table):
unknown = set(samples.uid)-set(table.index)
if unknown:
raise ValueError("Teacher targets requested for patients outside its training scope")
values = table.reindex(samples.uid)
means = values[[f"mean_{i}" for i in range(4)]].to_numpy(float)
intercept = values[[f"intercept_{i}" for i in range(4)]].to_numpy(float)
slope = values[[f"slope_{i}" for i in range(4)]].to_numpy(float)
time = (samples.visit_month.to_numpy(float)[:, None]+HORIZONS[None, :]-24.)/12.
trend = intercept[:, :, None]+slope[:, :, None]*time[:, None, :]
return {"mean": np.repeat(means, 4, axis=1), "level": means, "trend": trend.reshape(len(samples), 16)}
def fit_student_features(months, proteins, teacher, input_kind, components):
if input_kind == "month":
mean, scale = standardize_fit(current_process(months))
return {"month_mean": mean, "month_scale": scale}
values = (~np.isfinite(proteins)).astype(float) if input_kind == "missing" else proteins
return fit_pls(values, teacher, components)
def student_features(months, proteins, input_kind, params):
if input_kind == "month":
return (current_process(months)-params["month_mean"])/params["month_scale"]
values = (~np.isfinite(proteins)).astype(float) if input_kind == "missing" else proteins
return transform_pls(values, params)
def fit_student(x, raw_y, baseline, targets, teacher_kind, alpha):
if teacher_kind in {"projection", "raw"}:
return fit_residual(x, raw_y, baseline, alpha)
if teacher_kind == "level":
y = targets["level"]
else:
# Smooth training targets only at actually observed original label rows.
y = np.where(np.isfinite(raw_y), targets["trend"], np.nan)
return fit_residual(x, y, np.zeros_like(y), alpha)
def student_delta(x, coef, teacher_kind):
delta = x @ coef.T
return np.repeat(delta, 4, axis=1) if teacher_kind == "level" else delta
class SmoothSupervisionPredictor:
def __init__(self, bundle):
bundle = Path(bundle)
self.spec = json.loads((bundle / "model.json").read_text())
self.baseline = RepresentationPredictor(bundle / "baseline")
self.models = {}
for name in self.spec["selections"]:
with np.load(bundle / f"{name}.npz", allow_pickle=False) as archive:
self.models[name] = dict(archive)
with np.load(bundle / "shared_pls.npz", allow_pickle=False) as archive:
self.shared = dict(archive)
def predict(self, events):
baseline = self.baseline.predict(events) # Rejects labels/history/ID fields.
months = np.asarray([e["visit_month"] for e in events], dtype=float)
proteins = np.asarray([e["proteins"] for e in events], dtype=float)
result = {"none": baseline, "shared_pls": correct(baseline,
transform_pls(proteins, self.shared) @ self.shared["coef"].T, self.shared["weights"])}
for name, params in self.models.items():
selection = self.spec["selections"][name]
x = student_features(months, proteins, selection["input_kind"], params)
result[name] = correct(baseline, student_delta(x, params["coef"], selection["teacher_kind"]), params["weights"])
result["selected"] = result[self.spec["selected"]].copy()
return result
|