forked from mkachuee/Opportunistic
-
Notifications
You must be signed in to change notification settings - Fork 11
/
nhanes.py
247 lines (229 loc) · 8.99 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
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
# Preprocessing functions
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
#### Add your own preprocessing functions ####
# Dataset loader
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_arthritis(self, opts=None):
columns = [
# TARGET: systolic BP average
FeatureColumn('Questionnaire', 'MCQ220',
None, None),
# Gender
FeatureColumn('Demographics', 'RIAGENDR',
preproc_real, None),
# Age at time of screening
FeatureColumn('Demographics', 'RIDAGEYR',
preproc_real, None),
FeatureColumn('Demographics', 'RIDRETH3',
preproc_onehot, None),
# Race/ethnicity
FeatureColumn('Demographics', 'RIDRETH1',
preproc_onehot, None),
# Annual household income
FeatureColumn('Demographics', 'INDHHINC',
preproc_real, {'cutoff':11}),
# Education level
FeatureColumn('Demographics', 'DMDEDUC2',
preproc_real, {'cutoff':5}),
# BMI
FeatureColumn('Examination', 'BMXBMI',
preproc_real, None),
# Waist
FeatureColumn('Examination', 'BMXWAIST',
preproc_real, None),
# Height
FeatureColumn('Examination', 'BMXHT',
preproc_real, None),
# Upper Leg Length
FeatureColumn('Examination', 'BMXLEG',
preproc_real, None),
# Weight
FeatureColumn('Examination', 'BMXWT',
preproc_real, None),
# Total Cholesterol
FeatureColumn('Laboratory', 'LBXTC',
preproc_real, None),
# Alcohol consumption
FeatureColumn('Questionnaire', 'ALQ101',
preproc_real, {'cutoff':2}),
FeatureColumn('Questionnaire', 'ALQ120Q',
preproc_real, {'cutoff':365}),
# Vigorous work activity
FeatureColumn('Questionnaire', 'PAQ605',
preproc_real, {'cutoff':2}),
FeatureColumn('Questionnaire', 'PAQ620',
preproc_real, {'cutoff':2}),
FeatureColumn('Questionnaire', 'PAQ180',
preproc_real, {'cutoff':4}),
FeatureColumn('Questionnaire', 'PAD615',
preproc_real, {'cutoff':780}),
# Doctor told overweight (risk factor)
FeatureColumn('Questionnaire', 'MCQ160J',
preproc_onehot, {'cutoff':2}),
# Sleep
FeatureColumn('Questionnaire', 'SLD010H',
preproc_real, {'cutoff':12}),
# Smoking
FeatureColumn('Questionnaire', 'SMQ020',
preproc_onehot, None),
FeatureColumn('Questionnaire', 'SMD030',
preproc_real, {'cutoff':72}),
# Blood relatives with arthritis
FeatureColumn('Questionnaire', 'MCQ250D',
preproc_onehot, {'cutoff':2}),
# joint pain/aching/stiffness in past year
FeatureColumn('Questionnaire', 'MPQ010',
preproc_onehot, {'cutoff':2}),
# symptoms began only because of injury
FeatureColumn('Questionnaire', 'MPQ030',
preproc_onehot, {'cutoff':2}),
# how long experiencing pain
FeatureColumn('Questionnaire', 'MPQ110',
preproc_real, {'cutoff':4}),
]
nhanes_dataset = NHANES(self.data_path, columns)
df = nhanes_dataset.process()
fe_cols = df.drop(['MCQ220'], axis=1)
features = fe_cols.values
target = df['MCQ220'].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.full((target.shape[0]), 3)
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])
#### Add your own dataset loader ####