thbndi commited on
Commit
b5dc2d0
·
1 Parent(s): b037ea3

Update Mimic4Dataset.py

Browse files
Files changed (1) hide show
  1. Mimic4Dataset.py +41 -89
Mimic4Dataset.py CHANGED
@@ -110,7 +110,7 @@ def vocab(task,diag_flag,proc_flag,out_flag,chart_flag,med_flag,lab_flag):
110
  return ethVocabDict,genderVocabDict,ageVocabDict,insVocabDict
111
 
112
 
113
- def concat_data(data,task,feat_cond=False,feat_proc=False,feat_out=False,feat_chart=False,feat_meds=False):
114
  meds=data['Med']
115
  proc = data['Proc']
116
  out = data['Out']
@@ -244,86 +244,44 @@ def concat_data(data,task,feat_cond=False,feat_proc=False,feat_out=False,feat_ch
244
  dyn_df = pd.concat([meds_df,proc_df,out_df,chart_df], axis=1)
245
  return dyn_df,cond_df,demo
246
 
247
- def getXY_deep(X_df,task,feat_cond,feat_chart,feat_proc, feat_meds, feat_out):
 
248
  eth_vocab,gender_vocab,age_vocab,ins_vocab=vocab(task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds,False)
249
- dyn_df=[]
250
- meds=torch.zeros(size=(0,0))
251
- chart=torch.zeros(size=(0,0))
252
- proc=torch.zeros(size=(0,0))
253
- out=torch.zeros(size=(0,0))
254
- lab=torch.zeros(size=(0,0))
255
- stat_df=torch.zeros(size=(1,0))
256
- demo_df=torch.zeros(size=(1,0))
257
- y_df=[]
258
-
259
- for index,sample in tqdm(X_df.iterrows(),desc='Encoding Splits Data for '+task+' task'):
260
- dyn,stat,demo=concat_data(sample,task,feat_cond,feat_chart,feat_proc, feat_meds, feat_out)
261
- dyn_k=dyn.copy()
262
- keys=dyn_k.columns.levels[0]
263
- if index==0:
264
- for i in range(len(keys)):
265
- dyn_df.append(torch.zeros(size=(1,0)))
266
- y=demo['label']
267
-
268
- y_df.append(int(y))
269
-
270
- for key in range(len(keys)):
271
- dyn_temp=dyn[keys[key]]
272
- dyn_temp=dyn_temp.to_numpy()
273
- dyn_temp=torch.tensor(dyn_temp)
274
- dyn_temp=dyn_temp.unsqueeze(0)
275
- dyn_temp=torch.tensor(dyn_temp)
276
- dyn_temp=dyn_temp.type(torch.LongTensor)
277
-
278
- if key<len(dyn_df):
279
- if dyn_df[key].nelement():
280
- dyn_df[key]=torch.cat((dyn_df[key],dyn_temp),0)
281
- else:
282
- dyn_df[key]=dyn_temp
283
-
284
- stat=stat.to_numpy()
285
- stat=torch.tensor(stat)
286
- if stat_df[0].nelement():
287
- stat_df=torch.cat((stat_df,stat),0)
288
- else:
289
- stat_df=stat
290
- demo=demo.drop(['label'],axis=1)
291
- demo["gender"].replace(gender_vocab, inplace=True)
292
- demo["ethnicity"].replace(eth_vocab, inplace=True)
293
- demo["insurance"].replace(ins_vocab, inplace=True)
294
- demo["Age"].replace(age_vocab, inplace=True)
295
- demo=demo[["gender","ethnicity","insurance","Age"]]
296
- demo=demo.values
297
- demo=torch.tensor(demo)
298
- if demo_df[0].nelement():
299
- demo_df=torch.cat((demo_df,demo),0)
300
- else:
301
- demo_df=demo
302
 
 
 
 
 
303
 
304
  for k in range(len(keys)):
305
- if key<len(dyn_df):
306
- if keys[k]=='MEDS':
307
- meds=dyn_df[k]
308
- if keys[k]=='CHART':
309
- chart=dyn_df[k]
310
- if keys[k]=='OUT':
311
- out=dyn_df[k]
312
- if keys[k]=='PROC':
313
- proc=dyn_df[k]
314
- if keys[k]=='LAB':
315
- lab=dyn_df[k]
316
-
317
- stat_df=torch.tensor(stat_df)
318
- stat_df=stat_df.type(torch.LongTensor)
319
 
320
- demo_df=torch.tensor(demo_df)
321
- demo_df=demo_df.type(torch.LongTensor)
 
 
 
 
 
 
322
 
323
- y_df=torch.tensor(y_df)
324
- y_df=y_df.type(torch.LongTensor)
325
-
326
- return (meds,chart,out,proc,lab ,stat_df, demo_df, y_df )
327
 
328
  def getXY(dyn,stat,demo,concat_cols,concat):
329
  X_df=pd.DataFrame()
@@ -398,21 +356,14 @@ def generate_split(path,task,concat,feat_cond,feat_chart,feat_proc, feat_meds, f
398
  def generate_split_deep(path,task,feat_cond,feat_chart,feat_proc, feat_meds, feat_out):
399
  with open(path, 'rb') as fp:
400
  dico = pickle.load(fp)
401
- df = pd.DataFrame.from_dict(dico, orient='index')
402
- X_df=pd.DataFrame()
403
  taskf=task.replace(" ","_")
404
- meds,chart,out,proc,lab ,stat_df, demo_df, y_df = getXY_deep(df,taskf,feat_cond,feat_chart,feat_proc, feat_meds, feat_out)
405
- X_df=pd.concat([X_df,meds],axis=1)
406
- X_df=pd.concat([X_df,chart],axis=1)
407
- X_df=pd.concat([X_df,out],axis=1)
408
- X_df=pd.concat([X_df,proc],axis=1)
409
- X_df=pd.concat([X_df,lab],axis=1)
410
- X_df=pd.concat([X_df,stat_df],axis=1)
411
- X_df=pd.concat([X_df,demo_df],axis=1)
412
- X_df=pd.concat([X_df,y_df],axis=1)
413
- X_df=X_df.fillna(0)
414
- X_df = encoding(X_df)
415
- return X_df
416
 
417
  class Mimic4DatasetConfig(datasets.BuilderConfig):
418
  """BuilderConfig for Mimic4Dataset."""
@@ -860,6 +811,7 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
860
  pickle.dump(X_val_deep, f)
861
 
862
  return self._info_deep(X_train_deep)
 
863
  else:
864
  return self._info_raw()
865
 
 
110
  return ethVocabDict,genderVocabDict,ageVocabDict,insVocabDict
111
 
112
 
113
+ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds):
114
  meds=data['Med']
115
  proc = data['Proc']
116
  out = data['Out']
 
244
  dyn_df = pd.concat([meds_df,proc_df,out_df,chart_df], axis=1)
245
  return dyn_df,cond_df,demo
246
 
247
+ def getXY_deep(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds):
248
+ meds, chart, out, proc, lab =[],[],[],[],[]
249
  eth_vocab,gender_vocab,age_vocab,ins_vocab=vocab(task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds,False)
250
+ dyn_df,cond_df,demo=concat_data(data,task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds)
251
+ keys=dyn_df.columns.levels[0]
252
+ dyn = dict.fromkeys(keys)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
254
+ for key in range(len(keys)):
255
+ dyn_temp=dyn_df[keys[key]]
256
+ dyn_temp=dyn_temp.to_numpy()
257
+ dyn[key]=dyn_temp
258
 
259
  for k in range(len(keys)):
260
+ if keys[k]=='MEDS':
261
+ meds=dyn[k]
262
+ if keys[k]=='CHART':
263
+ chart=dyn[k]
264
+ if keys[k]=='OUT':
265
+ out=dyn[k]
266
+ if keys[k]=='PROC':
267
+ proc=dyn[k]
268
+ if keys[k]=='LAB':
269
+ lab=dyn[k]
270
+
271
+ stat=cond_df
272
+ stat=stat.to_numpy()
 
273
 
274
+ y = demo['label']
275
+
276
+ demo["gender"].replace(gender_vocab, inplace=True)
277
+ demo["ethnicity"].replace(eth_vocab, inplace=True)
278
+ demo["insurance"].replace(ins_vocab, inplace=True)
279
+ demo["Age"].replace(age_vocab, inplace=True)
280
+ demo=demo[["gender","ethnicity","insurance","Age"]]
281
+ demo=demo.values
282
 
283
+ return stat, demo, meds, chart, out, proc, lab, y
284
+
 
 
285
 
286
  def getXY(dyn,stat,demo,concat_cols,concat):
287
  X_df=pd.DataFrame()
 
356
  def generate_split_deep(path,task,feat_cond,feat_chart,feat_proc, feat_meds, feat_out):
357
  with open(path, 'rb') as fp:
358
  dico = pickle.load(fp)
359
+ X = pd.DataFrame.from_dict(dico, orient='index')
360
+ X_dict = {}
361
  taskf=task.replace(" ","_")
362
+ for hid, data in tqdm(X.iterrows(),desc='Encoding Splits Data for '+task+' task'):
363
+ stat, demo, meds, chart, out, proc, lab, y = getXY_deep(data, taskf, feat_cond, feat_proc, feat_out, feat_chart,feat_meds)
364
+ X_dict[hid] = {'stat': stat, 'demo': demo, 'meds': meds, 'chart': chart, 'out': out, 'proc': proc, 'lab': lab, 'y': y}
365
+
366
+ return X_dict
 
 
 
 
 
 
 
367
 
368
  class Mimic4DatasetConfig(datasets.BuilderConfig):
369
  """BuilderConfig for Mimic4Dataset."""
 
811
  pickle.dump(X_val_deep, f)
812
 
813
  return self._info_deep(X_train_deep)
814
+
815
  else:
816
  return self._info_raw()
817