thbndi commited on
Commit
8c8e656
1 Parent(s): f765c2a

Update dataset_utils.py

Browse files
Files changed (1) hide show
  1. dataset_utils.py +25 -70
dataset_utils.py CHANGED
@@ -109,19 +109,13 @@ def open_dict(task,cond, proc, out, chart, lab, med):
109
 
110
  return condDict, procDict, outDict, chartDict, medDict
111
 
112
- def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab,condDict, procDict, outDict, chartDict, medDict):
113
  meds=data['Med']
114
  proc = data['Proc']
115
  out = data['Out']
116
- chart = data['Chart']
117
  cond= data['Cond']['fids']
118
 
119
- cond_df=pd.DataFrame()
120
- proc_df=pd.DataFrame()
121
- out_df=pd.DataFrame()
122
- chart_df=pd.DataFrame()
123
- meds_df=pd.DataFrame()
124
-
125
  #demographic
126
  demo=pd.DataFrame(columns=['Age','gender','ethnicity','label','insurance'])
127
  new_row = {'Age': data['age'], 'gender': data['gender'], 'ethnicity': data['ethnicity'], 'label': data['label'], 'insurance': data['insurance']}
@@ -129,102 +123,67 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat
129
 
130
  ##########COND#########
131
  if (feat_cond):
132
- conds=pd.DataFrame(condDict,columns=['COND'])
133
- features=pd.DataFrame(np.zeros([1,len(conds)]),columns=conds['COND'])
134
-
135
- #onehot encode
136
- if(cond ==[]):
137
- cond_df=pd.DataFrame(np.zeros([1,len(features)]),columns=features['COND'])
138
- cond_df=cond_df.fillna(0)
139
- else:
140
- cond_df=pd.DataFrame(cond,columns=['COND'])
141
- cond_df['val']=1
142
- cond_df=(cond_df.drop_duplicates()).pivot(columns='COND',values='val').reset_index(drop=True)
143
- cond_df=cond_df.fillna(0)
144
- oneh = cond_df.sum().to_frame().T
145
- combined_df = pd.concat([features,oneh],ignore_index=True).fillna(0)
146
- combined_oneh=combined_df.sum().to_frame().T
147
- cond_df=combined_oneh
148
- for c in cond_df.columns :
149
- if c not in features:
150
- cond_df=cond_df.drop(columns=[c])
151
 
152
  ##########PROC#########
153
  if (feat_proc):
154
  if proc :
155
  feat=proc.keys()
156
  proc_val=[proc[key] for key in feat]
157
- procedures=pd.DataFrame(procDict,columns=['PROC'])
158
- features=pd.DataFrame(np.zeros([1,len(procedures)]),columns=procedures['PROC'])
159
- procs=pd.DataFrame(columns=feat)
160
  for p,v in zip(feat,proc_val):
161
- procs[p]=v
162
- features=features.drop(columns=procs.columns.to_list())
163
- proc_df = pd.concat([features,procs],axis=1).fillna(0)
164
  proc_df.columns=pd.MultiIndex.from_product([["PROC"], proc_df.columns])
 
165
  else:
166
  procedures=pd.DataFrame(procDict,columns=['PROC'])
167
- features=pd.DataFrame(np.zeros([1,len(procedures)]),columns=procedures['PROC'])
168
  features.columns=pd.MultiIndex.from_product([["PROC"], features.columns])
169
- proc_df=features.fillna(0)
170
 
171
  ##########OUT#########
172
  if (feat_out):
173
  if out :
174
  feat=out.keys()
175
  out_val=[out[key] for key in feat]
176
- outputs=pd.DataFrame(outDict,columns=['OUT'])
177
- features=pd.DataFrame(np.zeros([1,len(outputs)]),columns=outputs['OUT'])
178
- outs=pd.DataFrame(columns=feat)
179
  for o,v in zip(feat,out_val):
180
- outs[o]=v
181
- features=features.drop(columns=outs.columns.to_list())
182
- out_df = pd.concat([features,outs],axis=1).fillna(0)
183
  out_df.columns=pd.MultiIndex.from_product([["OUT"], out_df.columns])
184
  else:
185
  outputs=pd.DataFrame(outDict,columns=['OUT'])
186
- features=pd.DataFrame(np.zeros([1,len(outputs)]),columns=outputs['OUT'])
187
  features.columns=pd.MultiIndex.from_product([["OUT"], features.columns])
188
  out_df=features.fillna(0)
189
 
190
  ##########CHART#########
191
  if (feat_chart):
192
- if chart:
193
- charts=chart['val']
194
  feat=charts.keys()
195
  chart_val=[charts[key] for key in feat]
196
- charts=pd.DataFrame(chartDict,columns=['CHART'])
197
- features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
198
- chart=pd.DataFrame(columns=feat)
199
  for c,v in zip(feat,chart_val):
200
- chart[c]=v
201
- features=features.drop(columns=chart.columns.to_list())
202
- chart_df = pd.concat([features,chart],axis=1).fillna(0)
203
  chart_df.columns=pd.MultiIndex.from_product([["CHART"], chart_df.columns])
204
  else:
205
  charts=pd.DataFrame(chartDict,columns=['CHART'])
206
- features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
207
  features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
208
  chart_df=features.fillna(0)
209
  ##########LAB#########
210
 
211
  if (feat_lab):
212
- if chart:
213
- charts=chart['val']
214
  feat=charts.keys()
215
  chart_val=[charts[key] for key in feat]
216
- charts=pd.DataFrame(chartDict,columns=['LAB'])
217
- features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
218
- chart=pd.DataFrame(columns=feat)
219
  for c,v in zip(feat,chart_val):
220
- chart[c]=v
221
- features=features.drop(columns=chart.columns.to_list())
222
- chart.columns=pd.MultiIndex.from_product([["LAB"], chart.columns])
223
- chart_df = pd.concat([features,chart],axis=1).fillna(0)
224
  chart_df.columns=pd.MultiIndex.from_product([["LAB"], chart_df.columns])
225
  else:
226
  charts=pd.DataFrame(chartDict,columns=['LAB'])
227
- features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
228
  features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
229
  chart_df=features.fillna(0)
230
 
@@ -233,17 +192,13 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat
233
  if meds:
234
  feat=meds['signal'].keys()
235
  med_val=[meds['amount'][key] for key in feat]
236
- meds=pd.DataFrame(medDict,columns=['MEDS'])
237
- features=pd.DataFrame(np.zeros([1,len(meds)]),columns=meds['MEDS'])
238
- med=pd.DataFrame(columns=feat)
239
  for m,v in zip(feat,med_val):
240
- med[m]=v
241
- features=features.drop(columns=med.columns.to_list())
242
- meds_df = pd.concat([features,med],axis=1).fillna(0)
243
  meds_df.columns=pd.MultiIndex.from_product([["MEDS"], meds_df.columns])
244
  else:
245
  meds=pd.DataFrame(medDict,columns=['MEDS'])
246
- features=pd.DataFrame(np.zeros([1,len(meds)]),columns=meds['MEDS'])
247
  features.columns=pd.MultiIndex.from_product([["MEDS"], features.columns])
248
  meds_df=features.fillna(0)
249
 
@@ -252,7 +207,7 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat
252
 
253
 
254
 
255
- def generate_deep(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab,condDict, procDict, outDict, chartDict, medDict):
256
  meds = []
257
  charts = []
258
  proc = []
@@ -262,7 +217,7 @@ def generate_deep(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,fe
262
  demo = []
263
 
264
  size_cond, size_proc, size_meds, size_out, size_chart, size_lab, eth_vocab,gender_vocab,age_vocab,ins_vocab=vocab(task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds,False)
265
- dyn,cond_df,demo=concat_data(data,task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab,condDict, procDict, outDict, chartDict, medDict)
266
  if feat_chart:
267
  charts = dyn['CHART'].fillna(0).values
268
  if feat_meds:
 
109
 
110
  return condDict, procDict, outDict, chartDict, medDict
111
 
112
+ def concat_data(data,interval,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab,condDict, procDict, outDict, chartDict, medDict):
113
  meds=data['Med']
114
  proc = data['Proc']
115
  out = data['Out']
116
+ charts = data['Chart']['val']
117
  cond= data['Cond']['fids']
118
 
 
 
 
 
 
 
119
  #demographic
120
  demo=pd.DataFrame(columns=['Age','gender','ethnicity','label','insurance'])
121
  new_row = {'Age': data['age'], 'gender': data['gender'], 'ethnicity': data['ethnicity'], 'label': data['label'], 'insurance': data['insurance']}
 
123
 
124
  ##########COND#########
125
  if (feat_cond):
126
+ cond_df=pd.DataFrame(np.zeros([1,len(condDict)]),columns=condDict)
127
+ if cond:
128
+ for c in cond : cond_df[c]=1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  ##########PROC#########
131
  if (feat_proc):
132
  if proc :
133
  feat=proc.keys()
134
  proc_val=[proc[key] for key in feat]
135
+ proc_df=pd.DataFrame(np.zeros([interval,len(procDict)]),columns=procDict)
 
 
136
  for p,v in zip(feat,proc_val):
137
+ proc_df[p]=v
 
 
138
  proc_df.columns=pd.MultiIndex.from_product([["PROC"], proc_df.columns])
139
+ print(proc_df)
140
  else:
141
  procedures=pd.DataFrame(procDict,columns=['PROC'])
142
+ features=pd.DataFrame(np.zeros([interval,len(procedures)]),columns=procedures['PROC'])
143
  features.columns=pd.MultiIndex.from_product([["PROC"], features.columns])
 
144
 
145
  ##########OUT#########
146
  if (feat_out):
147
  if out :
148
  feat=out.keys()
149
  out_val=[out[key] for key in feat]
150
+ out_df=pd.DataFrame(np.zeros([interval,len(outDict)]),columns=outDict)
 
 
151
  for o,v in zip(feat,out_val):
152
+ out_df[o]=v
 
 
153
  out_df.columns=pd.MultiIndex.from_product([["OUT"], out_df.columns])
154
  else:
155
  outputs=pd.DataFrame(outDict,columns=['OUT'])
156
+ features=pd.DataFrame(np.zeros([interval,len(outputs)]),columns=outputs['OUT'])
157
  features.columns=pd.MultiIndex.from_product([["OUT"], features.columns])
158
  out_df=features.fillna(0)
159
 
160
  ##########CHART#########
161
  if (feat_chart):
162
+ if charts:
 
163
  feat=charts.keys()
164
  chart_val=[charts[key] for key in feat]
165
+ chart_df=pd.DataFrame(np.zeros([interval,len(chartDict)]),columns=chartDict)
 
 
166
  for c,v in zip(feat,chart_val):
167
+ chart_df[c]=v
 
 
168
  chart_df.columns=pd.MultiIndex.from_product([["CHART"], chart_df.columns])
169
  else:
170
  charts=pd.DataFrame(chartDict,columns=['CHART'])
171
+ features=pd.DataFrame(np.zeros([interval,len(charts)]),columns=charts['CHART'])
172
  features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
173
  chart_df=features.fillna(0)
174
  ##########LAB#########
175
 
176
  if (feat_lab):
177
+ if charts:
 
178
  feat=charts.keys()
179
  chart_val=[charts[key] for key in feat]
180
+ chart_df=pd.DataFrame(np.zeros([interval,len(chartDict)]),columns=chartDict)
 
 
181
  for c,v in zip(feat,chart_val):
182
+ chart_df[c]=v
 
 
 
183
  chart_df.columns=pd.MultiIndex.from_product([["LAB"], chart_df.columns])
184
  else:
185
  charts=pd.DataFrame(chartDict,columns=['LAB'])
186
+ features=pd.DataFrame(np.zeros([interval,len(charts)]),columns=charts['LAB'])
187
  features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
188
  chart_df=features.fillna(0)
189
 
 
192
  if meds:
193
  feat=meds['signal'].keys()
194
  med_val=[meds['amount'][key] for key in feat]
195
+ meds_df=pd.DataFrame(np.zeros([interval,len(medDict)]),columns=medDict)
 
 
196
  for m,v in zip(feat,med_val):
197
+ meds_df[m]=v
 
 
198
  meds_df.columns=pd.MultiIndex.from_product([["MEDS"], meds_df.columns])
199
  else:
200
  meds=pd.DataFrame(medDict,columns=['MEDS'])
201
+ features=pd.DataFrame(np.zeros([interval,len(meds)]),columns=meds['MEDS'])
202
  features.columns=pd.MultiIndex.from_product([["MEDS"], features.columns])
203
  meds_df=features.fillna(0)
204
 
 
207
 
208
 
209
 
210
+ def generate_deep(data,interval,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab,condDict, procDict, outDict, chartDict, medDict):
211
  meds = []
212
  charts = []
213
  proc = []
 
217
  demo = []
218
 
219
  size_cond, size_proc, size_meds, size_out, size_chart, size_lab, eth_vocab,gender_vocab,age_vocab,ins_vocab=vocab(task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds,False)
220
+ dyn,cond_df,demo=concat_data(data,interval,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab,condDict, procDict, outDict, chartDict, medDict)
221
  if feat_chart:
222
  charts = dyn['CHART'].fillna(0).values
223
  if feat_meds: