-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bnn_surv.py
86 lines (62 loc) · 2.73 KB
/
test_bnn_surv.py
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
from bnnsurv import models
import pandas as pd
import numpy as np
from sksurv.datasets import load_whas500
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
def make_time_event_split(y):
y_t = np.array(y['time'])
y_e = np.array(y['event'])
return (y_t, y_e)
def convert_to_structured(T, E):
default_dtypes = {"names": ("event", "time"), "formats": ("bool", "f8")}
concat = list(zip(E, T))
return np.array(concat, dtype=default_dtypes)
def test_mlp():
X, y = load_whas500()
y = convert_to_structured(y['lenfol'], y['fstat'])
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=0)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
t_train, e_train = make_time_event_split(y_train)
lower, upper = np.percentile(t_train[t_train.dtype.names], [10, 90])
event_times = np.arange(lower, upper+1)
model = models.MLP(layers=[32, 32], num_epochs=1)
model.fit(X_train, t_train, e_train)
preds_risk = model.predict_risk(X_test)
preds_surv = model.predict_survival(X_test, event_times)
assert len(preds_risk) != 0
assert len(preds_surv) != 0
def test_vi():
X, y = load_whas500()
y = convert_to_structured(y['lenfol'], y['fstat'])
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=0)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
t_train, e_train = make_time_event_split(y_train)
lower, upper = np.percentile(t_train[t_train.dtype.names], [10, 90])
event_times = np.arange(lower, upper+1)
model = models.VI(layers=[32, 32], num_epochs=1)
model.fit(X_train, t_train, e_train)
preds_risk = model.predict_risk(X_test)
preds_surv = model.predict_survival(X_test, event_times)
assert len(preds_risk) != 0
assert len(preds_surv) != 0
def test_mcd():
X, y = load_whas500()
y = convert_to_structured(y['lenfol'], y['fstat'])
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, random_state=0)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
t_train, e_train = make_time_event_split(y_train)
lower, upper = np.percentile(t_train[t_train.dtype.names], [10, 90])
event_times = np.arange(lower, upper+1)
model = models.MCD(layers=[32, 32], num_epochs=1)
model.fit(X_train, t_train, e_train)
preds_risk = model.predict_risk(X_test)
preds_surv = model.predict_survival(X_test, event_times)
assert len(preds_risk) != 0
assert len(preds_surv) != 0