Update Mimic4Dataset.py
Browse files- Mimic4Dataset.py +118 -7
Mimic4Dataset.py
CHANGED
@@ -14,6 +14,9 @@ import numpy as np
|
|
14 |
from tqdm import tqdm
|
15 |
import yaml
|
16 |
import torch
|
|
|
|
|
|
|
17 |
|
18 |
|
19 |
_DESCRIPTION = """\
|
@@ -370,6 +373,116 @@ def generate_split_deep(path,task,feat_cond,feat_chart,feat_proc, feat_meds, fea
|
|
370 |
|
371 |
return X_dict
|
372 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
373 |
class Mimic4DatasetConfig(datasets.BuilderConfig):
|
374 |
"""BuilderConfig for Mimic4Dataset."""
|
375 |
|
@@ -519,12 +632,14 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
519 |
sys.path.append(path_bench)
|
520 |
config = self.config_path.split('/')[-1]
|
521 |
|
522 |
-
script = 'python cohort.py '+ self.config.name.replace(" ","_") +" "+ self.mimic_path+ " "+
|
523 |
|
524 |
#####################################CHANGE##########
|
525 |
#if not os.path.exists(data_dir) :
|
526 |
-
os.system(script)
|
|
|
527 |
#####################################CHANGE##########
|
|
|
528 |
config_path='./config/'+config
|
529 |
with open(config_path) as f:
|
530 |
config = yaml.safe_load(f)
|
@@ -560,8 +675,6 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
560 |
###########################################################RAW##################################################################
|
561 |
|
562 |
def _info_raw(self):
|
563 |
-
self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out,self.path = self.create_cohort()
|
564 |
-
|
565 |
features = datasets.Features(
|
566 |
{
|
567 |
"label": datasets.ClassLabel(num_classes=2,names=["0", "1"]),
|
@@ -725,7 +838,6 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
725 |
###########################################################ENCODED##################################################################
|
726 |
|
727 |
def _info_encoded(self):
|
728 |
-
self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out,self.path = self.create_cohort()
|
729 |
X_train_encoded=generate_split(self.path+'/train_data.pkl',self.config.name,True,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
730 |
X_test_encoded=generate_split(self.path+'/test_data.pkl',self.config.name,True,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
731 |
X_val_encoded=generate_split(self.path+'/val_data.pkl',self.config.name,True,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
@@ -757,7 +869,6 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
757 |
yield i, row.to_dict()
|
758 |
######################################################DEEP###############################################################
|
759 |
def _info_deep(self):
|
760 |
-
self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out,self.path = self.create_cohort()
|
761 |
X_train_deep = generate_split_deep(self.path+'/train_data.pkl',self.config.name,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
762 |
X_test_deep = generate_split_deep(self.path+'/test_data.pkl',self.config.name,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
763 |
X_val_deep = generate_split_deep(self.path+'/val_data.pkl',self.config.name,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
@@ -823,7 +934,7 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
823 |
|
824 |
#############################################################################################################################
|
825 |
def _info(self):
|
826 |
-
|
827 |
if self.encoding == 'onehot' :
|
828 |
return self._info_encoded()
|
829 |
|
|
|
14 |
from tqdm import tqdm
|
15 |
import yaml
|
16 |
import torch
|
17 |
+
import time
|
18 |
+
from pathlib import Path
|
19 |
+
import importlib
|
20 |
|
21 |
|
22 |
_DESCRIPTION = """\
|
|
|
373 |
|
374 |
return X_dict
|
375 |
|
376 |
+
|
377 |
+
def task_cohort(task, mimic_path, config_path):
|
378 |
+
sys.path.append('./preprocessing/day_intervals_preproc')
|
379 |
+
sys.path.append('./utils')
|
380 |
+
sys.path.append('./preprocessing/hosp_module_preproc')
|
381 |
+
sys.path.append('./model')
|
382 |
+
import day_intervals_cohort_v22
|
383 |
+
import day_intervals_cohort
|
384 |
+
import feature_selection_icu
|
385 |
+
import data_generation_icu_modify
|
386 |
+
|
387 |
+
root_dir = os.path.dirname(os.path.abspath('UserInterface.ipynb'))
|
388 |
+
config_path='./config/'+config_path
|
389 |
+
with open(config_path) as f:
|
390 |
+
config = yaml.safe_load(f)
|
391 |
+
version_path = mimic_path+'/'
|
392 |
+
version = mimic_path.split('/')[-1][0]
|
393 |
+
start = time.time()
|
394 |
+
#----------------------------------------------config----------------------------------------------------
|
395 |
+
disease_label = config['disease_label']
|
396 |
+
tim = config['time']
|
397 |
+
label = config['label']
|
398 |
+
timeW = config['timeW']
|
399 |
+
include=int(timeW.split()[1])
|
400 |
+
bucket = config['bucket']
|
401 |
+
radimp = config['radimp']
|
402 |
+
predW = config['predW']
|
403 |
+
diag_flag = config['diagnosis']
|
404 |
+
out_flag = config['output']
|
405 |
+
chart_flag = config['chart']
|
406 |
+
proc_flag= config['proc']
|
407 |
+
med_flag = config['meds']
|
408 |
+
disease_filter = config['disease_filter']
|
409 |
+
icu_no_icu = config['icu_no_icu']
|
410 |
+
groupingICD = config['groupingICD']
|
411 |
+
# -------------------------------------------------------------------------------------------------------------
|
412 |
+
|
413 |
+
data_icu=icu_no_icu=="ICU"
|
414 |
+
data_mort=label=="Mortality"
|
415 |
+
data_admn=label=='Readmission'
|
416 |
+
data_los=label=='Length of Stay'
|
417 |
+
|
418 |
+
if (disease_filter=="Heart Failure"):
|
419 |
+
icd_code='I50'
|
420 |
+
elif (disease_filter=="CKD"):
|
421 |
+
icd_code='N18'
|
422 |
+
elif (disease_filter=="COPD"):
|
423 |
+
icd_code='J44'
|
424 |
+
elif (disease_filter=="CAD"):
|
425 |
+
icd_code='I25'
|
426 |
+
else:
|
427 |
+
icd_code='No Disease Filter'
|
428 |
+
|
429 |
+
#-----------------------------------------------EXTRACT MIMIC-----------------------------------------------------
|
430 |
+
if version == '2':
|
431 |
+
cohort_output = day_intervals_cohort_v22.extract_data(icu_no_icu,label,tim,icd_code, root_dir,version_path,disease_label)
|
432 |
+
|
433 |
+
elif version == '1':
|
434 |
+
cohort_output = day_intervals_cohort.extract_data(icu_no_icu,label,tim,icd_code, root_dir,version_path,disease_label)
|
435 |
+
#----------------------------------------------FEATURES-------------------------------------------------------
|
436 |
+
print(data_icu)
|
437 |
+
if data_icu :
|
438 |
+
feature_selection_icu.feature_icu(cohort_output, version_path,diag_flag,out_flag,chart_flag,proc_flag,med_flag)
|
439 |
+
#----------------------------------------------GROUPING-------------------------------------------------------
|
440 |
+
group_diag=False
|
441 |
+
group_med=False
|
442 |
+
group_proc=False
|
443 |
+
if data_icu:
|
444 |
+
if diag_flag:
|
445 |
+
group_diag=groupingICD
|
446 |
+
feature_selection_icu.preprocess_features_icu(cohort_output, diag_flag, group_diag,False,False,False,0,0)
|
447 |
+
#----------------------------------------------SUMMARY-------------------------------------------------------
|
448 |
+
if data_icu:
|
449 |
+
feature_selection_icu.generate_summary_icu(diag_flag,proc_flag,med_flag,out_flag,chart_flag)
|
450 |
+
#----------------------------------------------FEATURE SELECTION---------------------------------------------
|
451 |
+
|
452 |
+
select_diag= config['select_diag']
|
453 |
+
select_med= config['select_med']
|
454 |
+
select_proc= config['select_proc']
|
455 |
+
#select_lab= config['select_lab']
|
456 |
+
select_out= config['select_out']
|
457 |
+
select_chart= config['select_chart']
|
458 |
+
|
459 |
+
feature_selection_icu.features_selection_icu(cohort_output, diag_flag,proc_flag,med_flag,out_flag, chart_flag,select_diag,select_med,select_proc,select_out,select_chart)
|
460 |
+
#---------------------------------------CLEANING OF FEATURES-----------------------------------------------
|
461 |
+
thresh=0
|
462 |
+
if data_icu:
|
463 |
+
if chart_flag:
|
464 |
+
outlier_removal=config['outlier_removal']
|
465 |
+
clean_chart=outlier_removal!='No outlier detection'
|
466 |
+
impute_outlier_chart=outlier_removal=='Impute Outlier (default:98)'
|
467 |
+
thresh=config['outlier']
|
468 |
+
left_thresh=config['left_outlier']
|
469 |
+
feature_selection_icu.preprocess_features_icu(cohort_output, False, False,chart_flag,clean_chart,impute_outlier_chart,thresh,left_thresh)
|
470 |
+
# ---------------------------------------tim-Series Representation--------------------------------------------
|
471 |
+
if radimp == 'forward fill and mean' :
|
472 |
+
impute='Mean'
|
473 |
+
elif radimp =='forward fill and median':
|
474 |
+
impute = 'Median'
|
475 |
+
else :
|
476 |
+
impute = False
|
477 |
+
|
478 |
+
if data_icu:
|
479 |
+
gen=data_generation_icu_modify.Generator(task,cohort_output,data_mort,data_admn,data_los,diag_flag,proc_flag,out_flag,chart_flag,med_flag,impute,include,bucket,predW)
|
480 |
+
end = time.time()
|
481 |
+
print("Time elapsed : ", round((end - start)/60,2),"mins")
|
482 |
+
print("[============TASK COHORT SUCCESSFULLY CREATED============]")
|
483 |
+
|
484 |
+
|
485 |
+
#############################################DATASET####################################################################
|
486 |
class Mimic4DatasetConfig(datasets.BuilderConfig):
|
487 |
"""BuilderConfig for Mimic4Dataset."""
|
488 |
|
|
|
632 |
sys.path.append(path_bench)
|
633 |
config = self.config_path.split('/')[-1]
|
634 |
|
635 |
+
script = 'python cohort.py '+ self.config.name.replace(" ","_") +" "+ self.mimic_path+ " "+ " "+config
|
636 |
|
637 |
#####################################CHANGE##########
|
638 |
#if not os.path.exists(data_dir) :
|
639 |
+
#os.system(script)
|
640 |
+
task_cohort(self.config.name.replace(" ","_"),self.mimic_path,config)
|
641 |
#####################################CHANGE##########
|
642 |
+
|
643 |
config_path='./config/'+config
|
644 |
with open(config_path) as f:
|
645 |
config = yaml.safe_load(f)
|
|
|
675 |
###########################################################RAW##################################################################
|
676 |
|
677 |
def _info_raw(self):
|
|
|
|
|
678 |
features = datasets.Features(
|
679 |
{
|
680 |
"label": datasets.ClassLabel(num_classes=2,names=["0", "1"]),
|
|
|
838 |
###########################################################ENCODED##################################################################
|
839 |
|
840 |
def _info_encoded(self):
|
|
|
841 |
X_train_encoded=generate_split(self.path+'/train_data.pkl',self.config.name,True,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
842 |
X_test_encoded=generate_split(self.path+'/test_data.pkl',self.config.name,True,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
843 |
X_val_encoded=generate_split(self.path+'/val_data.pkl',self.config.name,True,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
|
|
869 |
yield i, row.to_dict()
|
870 |
######################################################DEEP###############################################################
|
871 |
def _info_deep(self):
|
|
|
872 |
X_train_deep = generate_split_deep(self.path+'/train_data.pkl',self.config.name,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
873 |
X_test_deep = generate_split_deep(self.path+'/test_data.pkl',self.config.name,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
874 |
X_val_deep = generate_split_deep(self.path+'/val_data.pkl',self.config.name,self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out)
|
|
|
934 |
|
935 |
#############################################################################################################################
|
936 |
def _info(self):
|
937 |
+
self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out,self.path = self.create_cohort()
|
938 |
if self.encoding == 'onehot' :
|
939 |
return self._info_encoded()
|
940 |
|