File size: 3,578 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
"""Protein corrections selected from training-patient cross-validation."""
from __future__ import annotations

import json
from pathlib import Path
import numpy as np

from ampd.current_protein import errors
from ampd.omics_boost import fit_pls, transform_pls
from ampd.protein_representation import RepresentationPredictor, encode, fit_representation
from ampd.protein_residual import correct


FAMILIES = {"pca": [2, 4, 8, 16], "pls": [1, 2, 4, 8, 16], "missing": [2, 4, 8, 16]}
ALPHAS = (1., 10., 100., 1000.)
BLENDS = sorted([(a, b) for a in [0., .25, .5, .75, 1.] for b in [0., .25, .5, .75, 1.]
                 if a+b <= 1.], key=lambda p: (sum(p), p))


def fit_features(family, dim, month, proteins, residual):
    if family == "pls":
        return fit_pls(proteins, residual, dim)
    params, _ = fit_representation(month, proteins, "missing" if family == "missing" else "pca")
    return params


def features(family, dim, proteins, params):
    if family == "pls":
        return transform_pls(proteins, params)
    raw = encode("missing" if family == "missing" else "pca", proteins, params)
    return ((raw-params["feature_mean"])/params["feature_scale"])[:, :dim].astype(np.float64)


def blend_predictions(base, pca, pls, weights):
    result = base.copy().astype(float)
    for target, (a, b) in enumerate(weights):
        cols = slice(target*4, (target+1)*4)
        result[:, cols] = np.clip(base[:, cols] + a*(pca[:, cols]-base[:, cols]) + b*(pls[:, cols]-base[:, cols]), 0, 86)
    return result


def choose_blend(y, base, pca, pls):
    result, candidates = [], []
    for target in range(4):
        cols = slice(target*4, (target+1)*4)
        scores = []
        for a, b in BLENDS:
            prediction = np.clip(base[:, cols]+a*(pca[:, cols]-base[:, cols])+b*(pls[:, cols]-base[:, cols]), 0, 86)
            scores.append(float(np.nanmean(errors(y[:, cols], prediction))))
        index = next(i for i, score in enumerate(scores) if score <= min(scores)+1e-10)
        result.append(BLENDS[index])
        candidates.append({"target": target+1, "candidates": [{"pca": a, "pls": b, "smape": s}
                          for (a, b), s in zip(BLENDS, scores)]})
    return result, candidates


class CVProteinPredictor:
    def __init__(self, bundle):
        bundle = Path(bundle)
        self.spec = json.loads((bundle / "model.json").read_text())
        self.baseline = RepresentationPredictor(bundle / "baseline")
        self.models = {}
        for family in FAMILIES:
            with np.load(bundle / f"{family}.npz", allow_pickle=False) as archive:
                self.models[family] = dict(archive)
        with np.load(bundle / "shared_pls.npz", allow_pickle=False) as archive:
            self.shared = dict(archive)

    def predict(self, events):
        base = self.baseline.predict(events)
        proteins = np.asarray([e["proteins"] for e in events], dtype=float)
        result = {"none": base, "shared_pls": correct(base, transform_pls(proteins, self.shared) @ self.shared["coef"].T,
                                                       self.shared["weights"])}
        for family, model in self.models.items():
            dim = self.spec["selections"][family]["dim"]
            x = features(family, dim, proteins, model)
            result[f"cv_{family}"] = correct(base, x @ model["coef"].T, model["weights"])
        result["equal_blend"] = .5*result["cv_pca"]+.5*result["cv_pls"]
        result["cv_blend"] = blend_predictions(base, result["cv_pca"], result["cv_pls"], self.spec["blend_weights"])
        return result