-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
318 lines (257 loc) · 11.7 KB
/
datasets.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# coding=utf-8
# MIT License
# Copyright (c) 2020 Carnegie Mellon University, Auton Lab
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Utility functions to load standard datasets to train and evaluate the
Deep Survival Machines models.
"""
import io
import pkgutil
import pandas as pd
import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
import torchvision
def increase_censoring(e, t, p, random_seed=0):
np.random.seed(random_seed)
uncens = np.where(e == 1)[0]
mask = np.random.choice([False, True], len(uncens), p=[1-p, p])
toswitch = uncens[mask]
e[toswitch] = 0
t_ = t[toswitch]
newt = []
for t__ in t_:
newt.append(np.random.uniform(1, t__))
t[toswitch] = newt
return e, t
def _load_framingham_dataset(sequential):
"""Helper function to load and preprocess the Framingham dataset.
The Framingham Dataset is a subset of 4,434 participants of the well known,
ongoing Framingham Heart study [1] for studying epidemiology for
hypertensive and arteriosclerotic cardiovascular disease. It is a popular
dataset for longitudinal survival analysis with time dependent covariates.
Parameters
----------
sequential: bool
If True returns a list of np.arrays for each individual.
else, returns collapsed results for each time step. To train
recurrent neural models you would typically use True.
References
----------
[1] Dawber, Thomas R., Gilcin F. Meadors, and Felix E. Moore Jr.
"Epidemiological approaches to heart disease: the Framingham Study."
American Journal of Public Health and the Nations Health 41.3 (1951).
"""
data = pkgutil.get_data(__name__, 'datasets/framingham.csv')
data = pd.read_csv(io.BytesIO(data))
dat_cat = data[['SEX', 'CURSMOKE', 'DIABETES', 'BPMEDS',
'educ', 'PREVCHD', 'PREVAP', 'PREVMI',
'PREVSTRK', 'PREVHYP']]
dat_num = data[['TOTCHOL', 'AGE', 'SYSBP', 'DIABP',
'CIGPDAY', 'BMI', 'HEARTRTE', 'GLUCOSE']]
x1 = pd.get_dummies(dat_cat).values
x2 = dat_num.values
x = np.hstack([x1, x2])
time = (data['TIMEDTH'] - data['TIME']).values
event = data['DEATH'].values
x = SimpleImputer(missing_values=np.nan, strategy='mean').fit_transform(x)
x_ = StandardScaler().fit_transform(x)
if not sequential:
return x_, time, event
else:
x, t, e = [], [], []
for id_ in sorted(list(set(data['RANDID']))):
x.append(x_[data['RANDID'] == id_])
t.append(time[data['RANDID'] == id_])
e.append(event[data['RANDID'] == id_])
return x, t, e
def _load_pbc_dataset(sequential):
"""Helper function to load and preprocess the PBC dataset
The Primary biliary cirrhosis (PBC) Dataset [1] is well known
dataset for evaluating survival analysis models with time
dependent covariates.
Parameters
----------
sequential: bool
If True returns a list of np.arrays for each individual.
else, returns collapsed results for each time step. To train
recurrent neural models you would typically use True.
References
----------
[1] Fleming, Thomas R., and David P. Harrington. Counting processes and
survival analysis. Vol. 169. John Wiley & Sons, 2011.
"""
data = pkgutil.get_data(__name__, 'datasets/pbc2.csv')
data = pd.read_csv(io.BytesIO(data))
data['histologic'] = data['histologic'].astype(str)
dat_cat = data[['drug', 'sex', 'ascites', 'hepatomegaly',
'spiders', 'edema', 'histologic']]
dat_num = data[['serBilir', 'serChol', 'albumin', 'alkaline',
'SGOT', 'platelets', 'prothrombin']]
age = data['age'] + data['years']
x1 = pd.get_dummies(dat_cat).values
x2 = dat_num.values
x3 = age.values.reshape(-1, 1)
x = np.hstack([x1, x2, x3])
time = (data['years'] - data['year']).values
event = data['status2'].values
x = SimpleImputer(missing_values=np.nan, strategy='mean').fit_transform(x)
x_ = StandardScaler().fit_transform(x)
if not sequential:
return x_, time, event
else:
x, t, e = [], [], []
for id_ in sorted(list(set(data['id']))):
x.append(x_[data['id'] == id_])
t.append(time[data['id'] == id_])
e.append(event[data['id'] == id_])
return x, t, e
def load_support():
"""Helper function to load and preprocess the SUPPORT dataset.
The SUPPORT Dataset comes from the Vanderbilt University study
to estimate survival for seriously ill hospitalized adults [1].
Please refer to http://biostat.mc.vanderbilt.edu/wiki/Main/SupportDesc.
for the original datasource.
References
----------
[1]: Knaus WA, Harrell FE, Lynn J et al. (1995): The SUPPORT prognostic
model: Objective estimates of survival for seriously ill hospitalized
adults. Annals of Internal Medicine 122:191-203.
"""
data = pkgutil.get_data(__name__, 'datasets/support2.csv')
data = pd.read_csv(io.BytesIO(data))
drop_cols = ['death', 'd.time']
outcomes = data.copy()
outcomes['event'] = data['death']
outcomes['time'] = data['d.time']
outcomes = outcomes[['event', 'time']]
cat_feats = ['sex', 'dzgroup', 'dzclass', 'income', 'race', 'ca']
num_feats = ['age', 'num.co', 'meanbp', 'wblc', 'hrt', 'resp',
'temp', 'pafi', 'alb', 'bili', 'crea', 'sod', 'ph',
'glucose', 'bun', 'urine', 'adlp', 'adls']
return outcomes, data[cat_feats+num_feats]
# def _load_support_dataset():
# """Helper function to load and preprocess the SUPPORT dataset.
# The SUPPORT Dataset comes from the Vanderbilt University study
# to estimate survival for seriously ill hospitalized adults [1].
# Please refer to http://biostat.mc.vanderbilt.edu/wiki/Main/SupportDesc.
# for the original datasource.
# References
# ----------
# [1]: Knaus WA, Harrell FE, Lynn J et al. (1995): The SUPPORT prognostic
# model: Objective estimates of survival for seriously ill hospitalized
# adults. Annals of Internal Medicine 122:191-203.
# """
# data = pkgutil.get_data(__name__, 'datasets/support2.csv')
# data = pd.read_csv(io.BytesIO(data))
# x1 = data[['age', 'num.co', 'meanbp', 'wblc', 'hrt', 'resp', 'temp',
# 'pafi', 'alb', 'bili', 'crea', 'sod', 'ph', 'glucose', 'bun',
# 'urine', 'adlp', 'adls']]
# catfeats = ['sex', 'dzgroup', 'dzclass', 'income', 'race', 'ca']
# x2 = pd.get_dummies(data[catfeats])
# x = np.concatenate([x1, x2], axis=1)
# t = data['d.time'].values
# e = data['death'].values
# x = SimpleImputer(missing_values=np.nan, strategy='mean').fit_transform(x)
# x = StandardScaler().fit_transform(x)
# remove = ~np.isnan(t)
# return x[remove], t[remove], e[remove]
def _load_mnist():
"""Helper function to load and preprocess the MNIST dataset.
The MNIST database of handwritten digits, available from this page, has a
training set of 60,000 examples, and a test set of 10,000 examples.
It is a good database for people who want to try learning techniques and
pattern recognition methods on real-world data while spending minimal
efforts on preprocessing and formatting [1].
Please refer to http://yann.lecun.com/exdb/mnist/.
for the original datasource.
References
----------
[1]: LeCun, Y. (1998). The MNIST database of handwritten digits.
http://yann.lecun.com/exdb/mnist/.
"""
train = torchvision.datasets.MNIST(root='datasets/',
train=True, download=True)
x = train.data.numpy()
x = np.expand_dims(x, 1).astype(float)
t = train.targets.numpy().astype(float) + 1
e, t = increase_censoring(np.ones(t.shape), t, p=.5)
return x, t, e
def load_synthetic_cf_phenotyping():
data = pkgutil.get_data(__name__, 'datasets/synthetic_dataset.csv')
data = pd.read_csv(io.BytesIO(data))
outcomes = data[['event', 'time', 'uncensored time treated',
'uncensored time control', 'Z','Zeta']]
features = data[['X1','X2','X3','X4','X5','X6','X7','X8']]
interventions = data['intervention']
return outcomes, features, interventions
def load_dataset(dataset='SUPPORT', **kwargs):
"""Helper function to load datasets to test Survival Analysis models.
Currently implemented datasets include:\n
**SUPPORT**: This dataset comes from the Vanderbilt University study
to estimate survival for seriously ill hospitalized adults [1].
(Refer to http://biostat.mc.vanderbilt.edu/wiki/Main/SupportDesc.
for the original datasource.)\n
**PBC**: The Primary biliary cirrhosis dataset [2] is well known
dataset for evaluating survival analysis models with time
dependent covariates.\n
**FRAMINGHAM**: This dataset is a subset of 4,434 participants of the well
known, ongoing Framingham Heart study [3] for studying epidemiology for
hypertensive and arteriosclerotic cardiovascular disease. It is a popular
dataset for longitudinal survival analysis with time dependent covariates.\n
**SYNTHETIC**: This is a non-linear censored dataset for counterfactual
time-to-event phenotyping. Introduced in [4], the dataset is generated
such that the treatment effect is heterogenous conditioned on the covariates.
References
-----------
[1]: Knaus WA, Harrell FE, Lynn J et al. (1995): The SUPPORT prognostic
model: Objective estimates of survival for seriously ill hospitalized
adults. Annals of Internal Medicine 122:191-203.\n
[2] Fleming, Thomas R., and David P. Harrington. Counting processes and
survival analysis. Vol. 169. John Wiley & Sons, 2011.\n
[3] Dawber, Thomas R., Gilcin F. Meadors, and Felix E. Moore Jr.
"Epidemiological approaches to heart disease: the Framingham Study."
American Journal of Public Health and the Nations Health 41.3 (1951).\n
[4] Nagpal, C., Goswami M., Dufendach K., and Artur Dubrawski.
"Counterfactual phenotyping for censored Time-to-Events" (2022).
Parameters
----------
dataset: str
The choice of dataset to load. Currently implemented is 'SUPPORT',
'PBC' and 'FRAMINGHAM'.
**kwargs: dict
Dataset specific keyword arguments.
Returns
----------
tuple: (np.ndarray, np.ndarray, np.ndarray)
A tuple of the form of \( (x, t, e) \) where \( x \)
are the input covariates, \( t \) the event times and
\( e \) the censoring indicators.
"""
sequential = kwargs.get('sequential', False)
if dataset == 'SUPPORT':
return load_support()
if dataset == 'PBC':
return _load_pbc_dataset(sequential)
if dataset == 'FRAMINGHAM':
return _load_framingham_dataset(sequential)
if dataset == 'MNIST':
return _load_mnist()
if dataset == 'SYNTHETIC':
return load_synthetic_cf_phenotyping()
else:
raise NotImplementedError('Dataset '+dataset+' not implemented.')