-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnhanes.py
754 lines (719 loc) · 31.7 KB
/
nhanes.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
import pdb
import glob
import copy
import os
import pickle
import joblib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats
import sklearn.feature_selection
class FeatureColumn:
def __init__(self, category, field, preprocessor, args=None, cost=None):
self.category = category
self.field = field
self.preprocessor = preprocessor
self.args = args
self.data = None
self.cost = cost
class NHANES:
def __init__(self, db_path=None, columns=None):
self.db_path = db_path
self.columns = columns # Depricated
self.dataset = None # Depricated
self.column_data = None
self.column_info = None
self.df_features = None
self.df_targets = None
self.costs = None
def process(self):
df = None
cache = {}
# collect relevant data
df = []
for fe_col in self.columns:
sheet = fe_col.category
field = fe_col.field
data_files = glob.glob(self.db_path+sheet+'/*.XPT')
df_col = []
for dfile in data_files:
print(80*' ', end='\r')
print('\rProcessing: ' + dfile.split('/')[-1], end='')
# read the file
if dfile in cache:
df_tmp = cache[dfile]
else:
df_tmp = pd.read_sas(dfile)
cache[dfile] = df_tmp
# skip of there is no SEQN
if 'SEQN' not in df_tmp.columns:
continue
#df_tmp.set_index('SEQN')
# skip if there is nothing interseting there
sel_cols = set(df_tmp.columns).intersection([field])
if not sel_cols:
continue
else:
df_tmp = df_tmp[['SEQN'] + list(sel_cols)]
df_tmp.set_index('SEQN', inplace=True)
df_col.append(df_tmp)
try:
df_col = pd.concat(df_col)
except:
#raise Error('Failed to process' + field)
raise Exception('Failed to process' + field)
df.append(df_col)
df = pd.concat(df, axis=1)
#df = pd.merge(df, df_sel, how='outer')
# do preprocessing steps
df_proc = []#[df['SEQN']]
for fe_col in self.columns:
field = fe_col.field
fe_col.data = df[field].copy()
# do preprocessing
if fe_col.preprocessor is not None:
prepr_col = fe_col.preprocessor(df[field], fe_col.args)
else:
prepr_col = df[field]
# handle the 1 to many
if (len(prepr_col.shape) > 1):
fe_col.cost = [fe_col.cost] * prepr_col.shape[1]
else:
fe_col.cost = [fe_col.cost]
df_proc.append(prepr_col)
self.dataset = pd.concat(df_proc, axis=1)
return self.dataset
def index(self, renew_cache=False):
# check if we don't have to renew cache
cache_path = self.db_path + 'cache/index_cache.pkl'
if (not renew_cache) and (os.path.exists(cache_path)):
print('Loading from cache:', cache_path)
try:
with open(cache_path, 'rb') as f:
self.column_data, self.column_info = pickle.load(f)
except:
self.column_data, self.column_info = joblib.load(cache_path)
return
# indexed cache file
self.column_data = {}
self.column_info = {}
# get the list of all sheets
sheets = [p.split('/')[-1] for p in glob.glob(self.db_path+'/*')]
# for each sheet read and index each data-file
for sheet in sheets:
dfiles = glob.glob(self.db_path+sheet+'/*.XPT')
for dfile in dfiles:
print('\rProcessing:', dfile, end='')
df = pd.read_sas(dfile)
if 'SEQN' not in df.columns:
continue
# read file columns and index them
df.set_index('SEQN', drop=True, inplace=True)
if not df.index.is_unique:
continue
for col in df.columns:
# if the column is not cached ever
if col not in self.column_data:
self.column_data[col] = df.loc[:,[col]]
# ignore duplicates
self.column_data[col] = self.column_data[col].groupby(level=0).last()
# get column info
self.update_column_info(col, dfile)
# else, we have cached info for this column
else:
# merge them
self.column_data[col] = pd.concat(
[self.column_data[col], df.loc[:,[col]]],
axis=0, verify_integrity=False)
# ignore duplicates
#if not self.column_data[col].index.is_unique:
# pdb.set_trace()
#self.column_data[col] = self.column_data[col].groupby(level=0).last()
self.column_data[col] = self.column_data[col][~self.column_data[col].index.duplicated(keep=False)]
# store/update cache file
os.makedirs(os.path.dirname(cache_path), exist_ok=True)
print('\rStoring to cache:', cache_path)
try:
with open(cache_path, 'wb+') as f:
pickle.dump((self.column_data, self.column_info), f)
except:
joblib.dump((self.column_data, self.column_info), cache_path, compress=9)
return
def process_supervised(self, target_col, exclude_cols, include_cols=None,
preproc_target=None, preproc_target_args=None,
missing_threshold=1.0, muinfo_threshold=0.0):
# remove missing target values
if type(target_col) is list:
self.df_targets = self.column_data[target_col[0]]
for t_col in target_col[1:]:
self.df_targets = pd.concat([self.df_targets, self.column_data[t_col]],
axis=1, join='inner')
else:
self.df_targets = self.column_data[target_col].copy()
self.df_targets.dropna(axis=0, how='any', inplace=True)
# get target dataframe
#if type(preproc_target) is tuple:
if preproc_target != None:
self.df_targets = preproc_target(self.df_targets, preproc_target_args)
self.df_targets.dropna(axis=0, how='any', inplace=True)
# process features
self.df_features = pd.DataFrame()
for col in self.column_data.keys():
print(80*' ', end='\r')
print('Processing:', col, end='\r')
# check if we should skip this column
if col in exclude_cols or col == target_col:
continue
if self.column_data[col].dtypes[0] == np.dtype('O'):
continue
if include_cols != None:
if col not in include_cols:
continue
# find the intersection of the two
df_valid = pd.concat([self.df_targets, self.column_data[col]],
axis=1, join='inner')
df_col_valid = df_valid.loc[:,[col]]
# if too many nans, skip the column
if df_col_valid.isna().mean()[0]>missing_threshold or df_valid.shape[0]==0:
continue
# low r-value, skip the column
xx = df_valid.values
np.nan_to_num(xx, copy=False)
try:
mu_info = sklearn.feature_selection.mutual_info_classif(
xx[:,1].reshape(-1, 1), xx[:,0].ravel().astype(np.int))
#mu_info = np.abs(scipy.stats.pearsonr(xx[:,0], xx[:,1])[0])
#print(mu_info)
except:
continue
if mu_info < muinfo_threshold:
continue
# do outer join
self.df_features = pd.concat([self.df_features, df_col_valid], axis=1, join='outer')
count_thresh = self.df_features.shape[0]*(1-missing_threshold)
self.df_features.dropna(axis=1, thresh=count_thresh, inplace=True)
# preprocess all features
self.costs = []
prep_features = []
for col in self.df_features.columns:
prepp_col = preprocess(self.df_features[col], self.column_info[col], None)
if type(prepp_col) == type(None):
continue
prep_features.append(prepp_col)
prep_len = 1
if len(prepp_col.shape) == 2:
prep_len = prepp_col.shape[1]
self.costs.extend([self.column_info[col]['cost']] * prep_len)
self.df_features = pd.concat(prep_features, axis=1)
self.costs = np.array(self.costs)
# shuffle them
inds_perm = copy.deepcopy(self.df_features.index.values)
np.random.shuffle(inds_perm)
self.df_features = self.df_features.loc[inds_perm]
self.df_targets = self.df_targets.loc[inds_perm]
return (self.df_features, self.df_targets)
def update_column_info(self, col, dfile):
self.column_info[col] = {}
# get column values
vals = self.column_data[col][col]
vals_unique = len(vals.unique())
if vals_unique < 20:
self.column_info[col]['type'] = 'categorical'
else:
self.column_info[col]['type'] = 'real'
# set feature costs
sheet = dfile.split('/')[-2]
if sheet == 'Demographics':
self.column_info[col]['cost'] = 2.0
elif sheet == 'Dietary':
self.column_info[col]['cost'] = 4.0
elif sheet == 'Examination':
self.column_info[col]['cost'] = 5.0
elif sheet == 'Questionnaire':
self.column_info[col]['cost'] = 4.0
elif sheet == 'Laboratory':
self.column_info[col]['cost'] = 9.0
else:
raise NotImplementedError
return
def save_supervised(self, filename):
save_dict = {'df_features':self.df_features,
'df_targets':self.df_targets,
'costs':self.costs}
with open(filename, 'wb+') as f:
pickle.dump(save_dict, f)
def load_supervised(self, filename):
with open(filename, 'rb') as f:
load_dict = pickle.load(f)
self.df_features = load_dict['df_features']
self.df_targets = load_dict['df_targets']
self.costs = load_dict['costs']
def get_distribution(self, phase, balanced=True):
features = self.df_features.values
targets = self.df_targets.values
# check the phase
inds_tst = np.arange(1,features.shape[0]*0.15, dtype=np.int)
inds_val = np.arange(features.shape[0]*0.15,
features.shape[0]*0.30, dtype=np.int)
inds_trn = np.arange(features.shape[0]*0.30,
features.shape[0]*1.00, dtype=np.int)
# if the phase is validation
if phase == 'validation':
phase_features = features[inds_val,:]
phase_targets = targets[inds_val]
# if the phase is test
elif phase == 'test':
phase_features = features[inds_tst,:]
phase_targets = targets[inds_tst]
# if the phase is train
elif phase == 'train':
phase_features = features[inds_trn,:]
phase_targets = targets[inds_trn]
elif phase == 'all':
phase_features = features[:,:]
phase_targets = targets[:]
else:
raise NotImplementedError('phase not found.')
# sampling
i = 0
y = 0
n_cls = np.max(phase_targets) + 1
while True:
i += 1
ind = i % phase_features.shape[0]
# balance dataset
if balanced:
while phase_targets[ind] != y:
i += 1
ind = i % phase_features.shape[0]
y += 1
if y >= n_cls:
y = 0
# yield the sample
yield (ind, phase_features[ind], phase_targets[ind], self.costs, 1.0)
def get_batch(self, n_size, phase, balanced=True):
dataset_features = self.df_features.values
dataset_targets = self.df_targets.values
# select indices
n_samples = dataset_features.shape[0]
n_classes = int(dataset_targets.max() + 1)
if phase == 'test':
inds_sel = np.arange(0, int(n_samples*0.15), 1)
elif phase == 'validation':
n_samples = dataset_features.shape[0]
inds_sel = np.arange(int(n_samples*0.15), int(n_samples*0.30), 1)
elif phase == 'train':
n_samples = dataset_features.shape[0]
inds_sel = np.arange(int(n_samples*0.30), n_samples, 1)
else:
raise NotImplementedError
inds_sel = np.random.permutation(inds_sel)
batch_inds = []
# if we should balance the data
if balanced:
for cl in range(n_classes):
inds_cl = inds_sel[dataset_targets[inds_sel] == cl]
batch_inds.extend(inds_cl[:n_size//n_classes])
else:
batch_inds = inds_sel[:n_size]
batch_inds = np.random.permutation(batch_inds)
return dataset_features[batch_inds], dataset_targets[batch_inds]
def preprocess(df_col, info_col, preprocessor=None):
if df_col.dtypes == np.dtype('O'):
return None#pd.DataFrame()
#df_col[pd.isna(df_col)] = df_col.mean()
if info_col['type'] == 'categorical':
df_col = preproc_onehot(df_col)
#df_col[pd.isna(df_col)] = df_col.mean()
elif info_col['type'] == 'real':
df_col = preproc_real(df_col)
else:
raise NotImplementedError
return df_col
def preproc_onehot(df_col, args=None):
return pd.get_dummies(df_col, prefix=df_col.name, prefix_sep='#')
def preproc_real(df_col, args=None):
if args is None:
args={'cutoff':np.inf}
# other answers as nan
df_col[df_col > args['cutoff']] = np.nan
# nan replaced by mean
df_col[pd.isna(df_col)] = df_col.mean()
# statistical normalization
df_col = (df_col-df_col.mean()) / df_col.std()
return df_col
def preproc_impute(df_col, args=None):
# nan replaced by mean
df_col[pd.isna(df_col)] = df_col.mean()
return df_col
def preproc_cut(df_col, bins):
# limit values to the bins range
df_col = df_col[df_col >= bins[0]]
df_col = df_col[df_col <= bins[-1]]
return pd.cut(df_col.iloc[:,0], bins, labels=False)
def preproc_dropna(df_col, args=None):
df_col.dropna(axis=0, how='any', inplace=True)
return df_col
class Dataset():
"""
Dataset manager class
"""
def __init__(self, data_path=None):
"""
Class intitializer.
"""
# set database path
if data_path == None:
self.data_path = './run_data/'
else:
self.data_path = data_path
# feature and target vecotrs
self.features = None
self.targets = None
self.costs = None
def load_diabetes(self, opts=None):
columns = [
# TARGET: Fasting Glucose
FeatureColumn('Laboratory', 'LBXGLU',
#preproc_dropna, None),
preproc_impute, None),
# Gender
FeatureColumn('Demographics', 'RIAGENDR',
preproc_real, None, cost=2),
# Age at time of screening
FeatureColumn('Demographics', 'RIDAGEYR',
preproc_real, None, cost=2),
FeatureColumn('Demographics', 'RIDRETH3',
preproc_onehot, None, cost=2),
# Race/ethnicity
FeatureColumn('Demographics', 'RIDRETH1',
preproc_onehot, None, cost=2),
# Annual household income
FeatureColumn('Demographics', 'INDHHINC',
preproc_real, {'cutoff':11}, cost=4),
# Education level
FeatureColumn('Demographics', 'DMDEDUC2',
preproc_real, {'cutoff':5}, cost=2),
# Blood pressure
FeatureColumn('Examination', 'BPXSY1',
preproc_real, None, cost=5),
FeatureColumn('Examination', 'BPXDI1',
preproc_real, None, cost=5),
FeatureColumn('Examination', 'BPXSY2',
preproc_real, None, cost=5),
FeatureColumn('Examination', 'BPXDI2',
preproc_real, None, cost=5),
FeatureColumn('Examination', 'BPXSY3',
preproc_real, None, cost=5),
FeatureColumn('Examination', 'BPXDI3',
preproc_real, None, cost=5),
FeatureColumn('Examination', 'BPXSY4',
preproc_real, None, cost=5),
FeatureColumn('Examination', 'BPXDI4',
preproc_real, None, cost=5),
# BMI
FeatureColumn('Examination', 'BMXBMI',
preproc_real, None, cost=5),
# Waist
FeatureColumn('Examination', 'BMXWAIST',
preproc_real, None, cost=5),
# Height
FeatureColumn('Examination', 'BMXHT',
preproc_real, None, cost=5),
# Upper Leg Length
FeatureColumn('Examination', 'BMXLEG',
preproc_real, None, cost=5),
# Weight
FeatureColumn('Examination', 'BMXWT',
preproc_real, None, cost=5),
# Total Cholesterol
FeatureColumn('Laboratory', 'LBXTC',
preproc_real, None, cost=9),
# Triglyceride
FeatureColumn('Laboratory', 'LBXTR',
preproc_real, None, cost=9),
# fibrinogen
FeatureColumn('Laboratory', 'LBXFB',
preproc_real, None, cost=9),
# LDL-cholesterol
FeatureColumn('Laboratory', 'LBDLDL',
preproc_real, None, cost=9),
# Alcohol consumption
FeatureColumn('Questionnaire', 'ALQ101',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'ALQ120Q',
preproc_real, {'cutoff':365}, cost=4),
# Vigorous work activity
FeatureColumn('Questionnaire', 'PAQ605',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'PAQ620',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'PAQ180',
preproc_real, {'cutoff':4}, cost=4),
# Sleep
FeatureColumn('Questionnaire', 'SLD010H',
preproc_real, {'cutoff':12}, cost=4),
# Smoking
FeatureColumn('Questionnaire', 'SMQ020',
preproc_onehot, None, cost=4),
FeatureColumn('Questionnaire', 'SMD030',
preproc_real, {'cutoff':72}, cost=4),
# Blood relatives have diabetes
FeatureColumn('Questionnaire', 'MCQ250A',
preproc_real, {'cutoff':2}, cost=4),
# Blood pressure history
FeatureColumn('Questionnaire', 'BPQ020',
preproc_real, {'cutoff':2}, cost=4),
]
nhanes_dataset = NHANES(self.data_path, columns)
df = nhanes_dataset.process()
# extract feature and target
features = df.loc[:, df.columns != 'LBXGLU'].values
targets_LBXGLU = df['LBXGLU'].values
targets = np.zeros(targets_LBXGLU.shape[0])
targets[targets_LBXGLU <= 100] = 0
targets[np.logical_and(targets_LBXGLU<125,targets_LBXGLU>100)] = 1
targets[targets_LBXGLU >= 125] = 2
# random permutation
perm = np.random.permutation(targets.shape[0])
self.features = features[perm]
self.targets = targets[perm]
self.costs = [c.cost for c in columns[1:]]
self.costs = np.array(
[item for sublist in self.costs for item in sublist])
def load_hypertension(self, opts=None):
columns = [
# TARGET: systolic BP average
FeatureColumn('Examination', 'BPXSAR',
preproc_dropna, None, cost=5),
# Gender
FeatureColumn('Demographics', 'RIAGENDR',
preproc_real, None, cost=2),
# Age at time of screening
FeatureColumn('Demographics', 'RIDAGEYR',
preproc_real, None, cost=2),
# Race/ethnicity
FeatureColumn('Demographics', 'RIDRETH1',
preproc_onehot, None, cost=2),
# Annual household income
FeatureColumn('Demographics', 'INDHHINC',
preproc_real, {'cutoff':11}, cost=4),
# Education level
FeatureColumn('Demographics', 'DMDEDUC2',
preproc_real, {'cutoff':5}, cost=2),
# Sodium eaten day before
FeatureColumn('Dietary', 'DR2TSODI',
preproc_real, {'cutoff':20683}, cost=4),
# BMI
FeatureColumn('Examination', 'BMXBMI',
preproc_real, None, cost=5),
# Waist
FeatureColumn('Examination', 'BMXWAIST',
preproc_real, None, cost=5),
# Height
FeatureColumn('Examination', 'BMXHT',
preproc_real, None, cost=5),
# Upper Leg Length
FeatureColumn('Examination', 'BMXLEG',
preproc_real, None, cost=5),
# Weight
FeatureColumn('Examination', 'BMXWT',
preproc_real, None, cost=5),
# Total Cholesterol
FeatureColumn('Laboratory', 'LBXTC',
preproc_real, None, cost=9),
# Triglyceride
FeatureColumn('Laboratory', 'LBXTR',
preproc_real, None, cost=9),
# fibrinogen
FeatureColumn('Laboratory', 'LBXFB',
preproc_real, None, cost=9),
# LDL-cholesterol
FeatureColumn('Laboratory', 'LBDLDL',
preproc_real, None, cost=9),
# Alcohol consumption
FeatureColumn('Questionnaire', 'ALQ101',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'ALQ120Q',
preproc_real, {'cutoff':365}, cost=4),
# Vigorous work activity
FeatureColumn('Questionnaire', 'PAQ605',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'PAQ620',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'PAQ180',
preproc_real, {'cutoff':4}, cost=4),
# Sleep
FeatureColumn('Questionnaire', 'SLD010H',
preproc_real, {'cutoff':12}, cost=4),
# Smoking
FeatureColumn('Questionnaire', 'SMQ020',
preproc_onehot, None, cost=4),
FeatureColumn('Questionnaire', 'SMD030',
preproc_real, {'cutoff':72}, cost=4),
# Blood relatives have hypertension/stroke
FeatureColumn('Questionnaire', 'MCQ250F',
preproc_real, {'cutoff':2}, cost=4),
]
nhanes_dataset = NHANES(self.data_path, columns)
df = nhanes_dataset.process()
# extract feature and target
# below 90/60 is hypotension, in between is normal, above 120/80 is prehypertension,
# above 140/90 is hypertension
fe_cols = df.drop(['BPXSAR'], axis=1)
features = fe_cols.values
target = df['BPXSAR'].values
# remove nan labeled samples
inds_valid = ~ np.isnan(target)
features = features[inds_valid]
target = target[inds_valid]
# Put each person in the corresponding bin
targets = np.zeros(target.shape[0])
targets[target < 140] = 0 # Rest (hypotsn, normal, prehyptsn)
targets[target >= 140] = 1 # hypertension
# random permutation
perm = np.random.permutation(targets.shape[0])
self.features = features[perm]
self.targets = targets[perm]
self.costs = [c.cost for c in columns[1:]]
self.costs = np.array(
[item for sublist in self.costs for item in sublist])
def load_arthritis(self, opts=None):
columns = [
# TARGET: systolic BP average
FeatureColumn('Questionnaire', 'MCQ160A',
None, None, cost=4),
# Gender
FeatureColumn('Demographics', 'RIAGENDR',
preproc_real, None, cost=2),
# Age at time of screening
FeatureColumn('Demographics', 'RIDAGEYR',
preproc_real, None, cost=2),
FeatureColumn('Demographics', 'RIDRETH3',
preproc_onehot, None, cost=2),
# Race/ethnicity
FeatureColumn('Demographics', 'RIDRETH1',
preproc_onehot, None, cost=2),
# Annual household income
FeatureColumn('Demographics', 'INDHHINC',
preproc_real, {'cutoff':11}, cost=4),
# Education level
FeatureColumn('Demographics', 'DMDEDUC2',
preproc_real, {'cutoff':5}, cost=2),
# BMI
FeatureColumn('Examination', 'BMXBMI',
preproc_real, None, cost=5),
# Waist
FeatureColumn('Examination', 'BMXWAIST',
preproc_real, None, cost=5),
# Height
FeatureColumn('Examination', 'BMXHT',
preproc_real, None, cost=5),
# Upper Leg Length
FeatureColumn('Examination', 'BMXLEG',
preproc_real, None, cost=5),
# Weight
FeatureColumn('Examination', 'BMXWT',
preproc_real, None, cost=5),
# Total Cholesterol
FeatureColumn('Laboratory', 'LBXTC',
preproc_real, None, cost=9),
# Alcohol consumption
FeatureColumn('Questionnaire', 'ALQ101',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'ALQ120Q',
preproc_real, {'cutoff':365}, cost=4),
# Vigorous work activity
FeatureColumn('Questionnaire', 'PAQ605',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'PAQ620',
preproc_real, {'cutoff':2}, cost=4),
FeatureColumn('Questionnaire', 'PAQ180',
preproc_real, {'cutoff':4}, cost=4),
FeatureColumn('Questionnaire', 'PAD615',
preproc_real, {'cutoff':780}, cost=4),
# Doctor told overweight (risk factor)
FeatureColumn('Questionnaire', 'MCQ160J',
preproc_onehot, {'cutoff':2}, cost=4),
# Sleep
FeatureColumn('Questionnaire', 'SLD010H',
preproc_real, {'cutoff':12}, cost=4),
# Smoking
FeatureColumn('Questionnaire', 'SMQ020',
preproc_onehot, None, cost=4),
FeatureColumn('Questionnaire', 'SMD030',
preproc_real, {'cutoff':72}, cost=4),
# Blood relatives with arthritis
FeatureColumn('Questionnaire', 'MCQ250D',
preproc_onehot, {'cutoff':2}, cost=4),
# joint pain/aching/stiffness in past year
FeatureColumn('Questionnaire', 'MPQ010',
preproc_onehot, {'cutoff':2}, cost=4),
# symptoms began only because of injury
FeatureColumn('Questionnaire', 'MPQ030',
preproc_onehot, {'cutoff':2}, cost=4),
# how long experiencing pain
FeatureColumn('Questionnaire', 'MPQ110',
preproc_real, {'cutoff':4}, cost=4),
]
nhanes_dataset = NHANES(self.data_path, columns)
df = nhanes_dataset.process()
fe_cols = df.drop(['MCQ160A'], axis=1)
features = fe_cols.values
target = df['MCQ160A'].values
# remove nan labeled samples
inds_valid = ~ np.isnan(target)
features = features[inds_valid]
target = target[inds_valid]
# Put each person in the corresponding bin
targets = np.zeros(target.shape[0])
targets[target == 1] = 0 # yes arthritis
targets[target == 2] = 1 # no arthritis
# random permutation
perm = np.random.permutation(targets.shape[0])
self.features = features[perm]
self.targets = targets[perm]
self.costs = [c.cost for c in columns[1:]]
self.costs = np.array(
[item for sublist in self.costs for item in sublist])
def get_dataset(self, phase):
# check the phase
inds_tst = np.arange(1,self.features.shape[0]*0.15, dtype=np.int)
inds_val = np.arange(self.features.shape[0]*0.15,
self.features.shape[0]*0.30, dtype=np.int)
inds_trn = np.arange(self.features.shape[0]*0.30,
self.features.shape[0]*1.00, dtype=np.int)
# if the phase is validation
if phase == 'validation':
phase_features = self.features[inds_val,:]
phase_targets = self.targets[inds_val]
# if the phase is test
elif phase == 'test':
phase_features = self.features[inds_tst,:]
phase_targets = self.targets[inds_tst]
# if the phase is train
elif phase == 'train':
phase_features = self.features[inds_trn,:]
phase_targets = self.targets[inds_trn]
elif phase == 'all':
phase_features = self.features[:,:]
phase_targets = self.targets[:]
else:
raise NotImplementedError('phase not found.')
# sampling
i = 0
y = 0
n_cls = np.max(phase_targets) + 1
while True:
i += 1
ind = i % phase_features.shape[0]
# balance dataset
while phase_targets[ind] != y:
i += 1
ind = i % phase_features.shape[0]
y += 1
if y >= n_cls:
y = 0
# yield the sample
yield (ind, phase_features[ind], phase_targets[ind], self.costs, 1.0)