-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_baselines.py
157 lines (132 loc) · 4.76 KB
/
train_baselines.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import argparse
import collections
import csv
import os
import pickle
from collections import Counter
from typing import List, Optional, Tuple
import femr
import femr.datasets
import lightgbm as ltb
import numpy as np
import scipy
import sklearn.linear_model
from scipy.sparse import issparse
from sklearn import metrics
from sklearn.model_selection import (GridSearchCV, ParameterGrid,
PredefinedSplit)
XGB_PARAMS = {
"max_depth": [3, 6, -1],
"learning_rate": [0.02, 0.1, 0.5],
"num_leaves": [10, 25, 100],
"force_row_wise": [True],
"num_threads": [4],
}
LR_PARAMS = {
"C": [1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 1e2, 1e3, 1e4, 1e5, 1e6],
"penalty": ["l2"],
}
def tune_hyperparams(
X_train, y_train, X_val, y_val, model, params, num_threads: int = 1
):
# In `test_fold`, -1 indicates that the corresponding sample is used for training, and a value >=0 indicates the test set.
# We use `PredefinedSplit` to specify our custom validation split
X = scipy.sparse.vstack([X_train, X_val])
y = np.concatenate((y_train, y_val), axis=0)
test_fold = -np.ones(X.shape[0])
test_fold[X_train.shape[0] :] = 1
clf = GridSearchCV(
model,
params,
n_jobs=27,
verbose=1,
cv=PredefinedSplit(test_fold=test_fold),
refit=True,
scoring="roc_auc",
)
clf.fit(X, y)
return clf
def main(args):
PATH_TO_PATIENT_DATABASE = args.path_to_database
database = femr.datasets.PatientDatabase(PATH_TO_PATIENT_DATABASE)
print(f"Loading patient database from: {PATH_TO_PATIENT_DATABASE}")
print(f"Saving output to: {args.path_to_output_dir}")
with open(os.path.join(args.path_to_output_dir, "features.pkl"), "rb") as f:
features, patient_ids, label_values, label_times = pickle.load(f)
train_patients = np.load(os.path.join(args.path_to_output_dir, 'train_patients.npy'))
valid_patients = np.load(os.path.join(args.path_to_output_dir, 'valid_patients.npy'))
test_patients = np.load(os.path.join(args.path_to_output_dir, 'test_patients.npy'))
train_mask = np.isin(patient_ids, train_patients)
valid_mask = np.isin(patient_ids, valid_patients)
test_mask = np.isin(patient_ids, test_patients)
print(f"Num train: {sum(train_mask)}")
print(f"Num valid: {sum(valid_mask)}")
print(f"Num test: {sum(test_mask)}")
X_train = features[train_mask, :]
X_valid = features[valid_mask, :]
X_test = features[test_mask, :]
y_train = label_values[train_mask]
y_valid = label_values[valid_mask]
y_test = label_values[test_mask]
os.makedirs(args.path_to_output_dir, exist_ok=True)
for model_name in ["logistic", "gbm"]:
print(f"Working on {model_name}")
if model_name == "gbm":
model = tune_hyperparams(
X_train,
y_train,
X_valid,
y_valid,
ltb.LGBMClassifier(),
XGB_PARAMS,
num_threads=args.num_threads,
)
elif model_name == "logistic":
model = tune_hyperparams(
X_train,
y_train,
X_valid,
y_valid,
sklearn.linear_model.LogisticRegression(),
LR_PARAMS,
num_threads=args.num_threads,
)
with open(
os.path.join(args.path_to_output_dir, f"{model_name}_model.pkl"), "wb"
) as f:
pickle.dump(model, f)
proba = model.predict_proba(features)[:, 1]
with open(
os.path.join(args.path_to_output_dir, f"{model_name}_predictions.pkl"), "wb"
) as f:
pickle.dump([proba, patient_ids, label_values, label_times], f)
y_train_proba = proba[train_mask]
y_valid_proba = proba[valid_mask]
y_test_proba = proba[test_mask]
train_auroc = metrics.roc_auc_score(y_train, y_train_proba)
val_auroc = metrics.roc_auc_score(y_valid, y_valid_proba)
test_auroc = metrics.roc_auc_score(y_test, y_test_proba)
print(f"Train AUROC: {train_auroc}")
print(f"Val AUROC: {val_auroc}")
print(f"Test AUROC: {test_auroc}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Train baselines logistic regression and lightgbm models"
)
parser.add_argument(
"--path_to_database", required=True, type=str, help="Path to femr database"
)
parser.add_argument(
"--path_to_output_dir",
required=True,
type=str,
help="Path to save labeles and featurizers",
)
parser.add_argument(
"--num_threads",
type=int,
help="The number of threads to use",
default=1,
)
args = parser.parse_args()
main(args)