Download code/ampd/current_protein.py from fryabcfry/cs4486-amp-pd-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 6.28 kB
-
https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/current_protein.py
- Command line
-
hf download hf://fryabcfry/cs4486-amp-pd-checkpoints/code/ampd/current_protein.py
-
curl -L -o current_protein.py https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/current_protein.py
6.28 kB
| """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"]) | |