File size: 5,442 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
"""Simple current-month/protein MLP baseline with fold-local preprocessing."""
from __future__ import annotations

import json
from pathlib import Path
import warnings

import numpy as np
import pandas as pd
import torch

from ampd.evaluation.metrics import smape_plus_1
from ampd.models import build_model
from ampd.training.losses import masked_mae


FEATURE_SETS = ("month_protein", "month_only", "month_missingness")


def raw_features(months, proteins, feature_set):
    months = np.asarray(months, dtype=float)
    proteins = np.asarray(proteins, dtype=float)
    if np.any(proteins < 0):
        raise ValueError("Protein abundances must be nonnegative")
    if feature_set == "month_only":
        return months[:, None]
    if feature_set == "month_protein":
        log = np.where(np.isfinite(proteins), np.log1p(proteins), np.nan)
        return np.c_[months, log]
    if feature_set == "month_missingness":
        missing = ~np.isfinite(proteins)
        return np.c_[months, missing, (~missing).sum(1)].astype(float)
    raise ValueError(feature_set)


def fit_preprocessor(x):
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", RuntimeWarning)
        impute = np.nan_to_num(np.nanmedian(x, axis=0))
    filled = np.where(np.isfinite(x), x, impute)
    mean, scale = filled.mean(0), filled.std(0)
    return {"impute": impute, "mean": mean, "scale": np.where(scale > 0, scale, 1.)}


def transform(x, pre):
    filled = np.where(np.isfinite(x), x, pre["impute"])
    return ((filled-pre["mean"])/pre["scale"]).astype(np.float32)


def train(x, y, config, epochs, validation=None):
    torch.manual_seed(int(config["seed"]))
    model = build_model(config["model"], x.shape[1], 16)
    optimizer = torch.optim.Adam(model.parameters(), lr=config["optimizer"]["lr"],
                                 weight_decay=config["optimizer"]["weight_decay"])
    xt = torch.tensor(x, dtype=torch.float32)
    yt = torch.tensor(np.where(np.isfinite(y), y, 0.), dtype=torch.float32)
    mask = torch.tensor(np.isfinite(y), dtype=torch.float32)
    rng = np.random.default_rng(config["seed"])
    best, selected, stale, state = float("inf"), epochs, 0, None
    logs = []
    if validation is not None:
        vx, vy = validation
        vt = torch.tensor(vx, dtype=torch.float32)
        vm = np.isfinite(vy)
    for epoch in range(1, epochs+1):
        model.train()
        order = rng.permutation(len(x))
        total, count = 0., 0.
        for start in range(0, len(x), config["trainer"]["batch_size"]):
            rows = order[start:start+config["trainer"]["batch_size"]]
            loss = masked_mae(model(xt[rows]), yt[rows], mask[rows])
            if not torch.isfinite(loss):
                raise ValueError("Nonfinite training loss")
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            n = float(mask[rows].sum())
            total += float(loss.detach())*n
            count += n
        record = {"epoch": epoch, "train_masked_mae": total/count}
        if validation is not None:
            model.eval()
            with torch.no_grad():
                point = model(vt).numpy()
            score = smape_plus_1(vy[vm], point[vm])
            record["inner_smape"] = score
            if score < best-config["trainer"]["min_delta"]:
                best, selected, stale = score, epoch, 0
                state = {k: v.detach().clone() for k, v in model.state_dict().items()}
            else:
                stale += 1
        logs.append(record)
        if validation is not None and stale >= config["trainer"]["early_stopping_patience"]:
            break
    if state is not None:
        model.load_state_dict(state)
    return model.eval(), pd.DataFrame(logs), selected


class SimpleMLPPredictor:
    def __init__(self, bundle, feature_sets=None):
        bundle = Path(bundle)
        self.spec = json.loads((bundle/"model.json").read_text())
        self.models, self.preprocessors = {}, {}
        for feature_set in (self.spec["feature_sets"] if feature_sets is None else feature_sets):
            if feature_set not in self.spec["feature_sets"]:
                raise ValueError(f"Unknown feature set: {feature_set}")
            pre = dict(np.load(bundle/f"{feature_set}_preprocessor.npz", allow_pickle=False))
            model = build_model(self.spec["model"], len(pre["mean"]), 16)
            model.load_state_dict(torch.load(bundle/f"{feature_set}.pt", weights_only=True, map_location="cpu"))
            self.models[feature_set], self.preprocessors[feature_set] = model.eval(), pre

    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 vocabulary mismatch")
        months = np.asarray([e["visit_month"] for e in events], dtype=float)
        proteins = np.asarray([e["proteins"] for e in events], dtype=float)
        result = {}
        for feature_set, model in self.models.items():
            raw = raw_features(months, proteins, feature_set)
            x = transform(raw, self.preprocessors[feature_set])
            with torch.no_grad():
                result[feature_set] = model(torch.tensor(x)).numpy()
        return result