Update Mimic4Dataset.py
Browse files- Mimic4Dataset.py +147 -34
Mimic4Dataset.py
CHANGED
@@ -27,6 +27,7 @@ _HOMEPAGE = "https://huggingface.co/datasets/thbndi/Mimic4Dataset"
|
|
27 |
_CITATION = "https://proceedings.mlr.press/v193/gupta22a.html"
|
28 |
_URL = "https://github.com/healthylaife/MIMIC-IV-Data-Pipeline"
|
29 |
_DATA_GEN = 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/data_generation_icu_modify.py'
|
|
|
30 |
_DAY_INT= 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/day_intervals_cohort_v22.py'
|
31 |
_CONFIG_URLS = {'los' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/los.config',
|
32 |
'mortality' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/mortality.config',
|
@@ -53,26 +54,42 @@ def check_config(task,config_file):
|
|
53 |
predW = config['predW']
|
54 |
disease_filter = config['disease_filter']
|
55 |
icu_no_icu = config['icu_no_icu']
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
chart_flag = config['chart']
|
59 |
-
output_flag = config['output']
|
60 |
diag_flag= config['diagnosis']
|
61 |
proc_flag = config['proc']
|
62 |
meds_flag = config['meds']
|
63 |
-
|
64 |
select_diag= config['select_diag']
|
65 |
select_med= config['select_med']
|
66 |
select_proc= config['select_proc']
|
67 |
select_out = config['select_out']
|
68 |
-
select_chart = config['select_chart']
|
69 |
|
70 |
outlier_removal=config['outlier_removal']
|
71 |
thresh=config['outlier']
|
72 |
left_thresh=config['left_outlier']
|
73 |
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
if task=='Phenotype':
|
77 |
if disease_label=='Heart Failure':
|
78 |
label='Readmission'
|
@@ -117,14 +134,19 @@ def check_config(task,config_file):
|
|
117 |
raise ValueError('Task not correct')
|
118 |
|
119 |
assert( disease_filter in ['Heart Failure','COPD','CKD','CAD',""], "Disease filter should be one of the following: Heart Failure, COPD, CKD, CAD or empty")
|
120 |
-
assert(
|
121 |
-
assert( groupingICD in ['Convert ICD-9 to ICD-10 and group ICD-10 codes','Keep both ICD-9 and ICD-10 codes','Convert ICD-9 to ICD-10 codes'], "Grouping ICD should be one of the following: Convert ICD-9 to ICD-10 and group ICD-10 codes, Keep both ICD-9 and ICD-10 codes, Convert ICD-9 to ICD-10 codes")
|
122 |
assert (bucket<=6 and bucket>=1 and isinstance(bucket, int), "Time bucket should be between 1 and 6 and an integer")
|
123 |
assert (radimp in ['No Imputation', 'forward fill and mean','forward fill and median'], "imputation should be one of the following: No Imputation, forward fill and mean, forward fill and median")
|
124 |
if chart_flag:
|
125 |
assert (left_thresh>=0 and left_thresh<=10 and isinstance(left_thresh, int), "Left outlier threshold should be between 0 and 10 and an integer")
|
126 |
assert (thresh>=90 and thresh<=99 and isinstance(thresh, int), "Outlier threshold should be between 90 and 99 and an integer")
|
127 |
assert (outlier_removal in ['No outlier detection','Impute Outlier (default:98)','Remove outliers (default:98)'], "Outlier removal should be one of the following: No outlier detection, Impute Outlier (default:98), Remove outliers (default:98)")
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
return label, time, disease_label, predW
|
130 |
|
@@ -201,7 +223,7 @@ def vocab(task,diag_flag,proc_flag,out_flag,chart_flag,med_flag,lab_flag):
|
|
201 |
|
202 |
return len(condVocabDict),len(procVocabDict),len(medVocabDict),len(outVocabDict),len(chartVocabDict),len(labVocabDict),ethVocabDict,genderVocabDict,ageVocabDict,insVocabDict
|
203 |
|
204 |
-
def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds):
|
205 |
meds=data['Med']
|
206 |
proc = data['Proc']
|
207 |
out = data['Out']
|
@@ -308,6 +330,30 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds):
|
|
308 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
|
309 |
features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
|
310 |
chart_df=features.fillna(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
|
312 |
###MEDS
|
313 |
if (feat_meds):
|
@@ -334,7 +380,9 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds):
|
|
334 |
|
335 |
dyn_df = pd.concat([meds_df,proc_df,out_df,chart_df], axis=1)
|
336 |
return dyn_df,cond_df,demo
|
337 |
-
|
|
|
|
|
338 |
stat_df = torch.zeros(size=(1,0))
|
339 |
demo_df = torch.zeros(size=(1,0))
|
340 |
meds = torch.zeros(size=(0,0))
|
@@ -344,10 +392,9 @@ def getXY_deep(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds):
|
|
344 |
lab = torch.zeros(size=(0,0))
|
345 |
stat_df = torch.zeros(size=(1,0))
|
346 |
demo_df = torch.zeros(size=(1,0))
|
347 |
-
feat_lab = False
|
348 |
|
349 |
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)
|
350 |
-
dyn,cond_df,demo=concat_data(data,task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds)
|
351 |
|
352 |
###########""
|
353 |
if feat_chart:
|
@@ -462,6 +509,9 @@ def getXY(dyn,stat,demo,concat_cols,concat):
|
|
462 |
|
463 |
|
464 |
|
|
|
|
|
|
|
465 |
def task_cohort(task, mimic_path, config_path):
|
466 |
sys.path.append('./preprocessing/day_intervals_preproc')
|
467 |
sys.path.append('./utils')
|
@@ -471,6 +521,9 @@ def task_cohort(task, mimic_path, config_path):
|
|
471 |
import day_intervals_cohort
|
472 |
import feature_selection_icu
|
473 |
import data_generation_icu_modify
|
|
|
|
|
|
|
474 |
|
475 |
root_dir = os.path.dirname(os.path.abspath('UserInterface.ipynb'))
|
476 |
config_path='./config/'+config_path
|
@@ -492,15 +545,18 @@ def task_cohort(task, mimic_path, config_path):
|
|
492 |
chart_flag = config['chart']
|
493 |
proc_flag= config['proc']
|
494 |
med_flag = config['meds']
|
|
|
495 |
|
496 |
disease_filter = config['disease_filter']
|
497 |
icu_no_icu = config['icu_no_icu']
|
498 |
-
|
|
|
|
|
499 |
|
500 |
select_diag= config['select_diag']
|
501 |
select_med= config['select_med']
|
502 |
select_proc= config['select_proc']
|
503 |
-
|
504 |
select_out= config['select_out']
|
505 |
select_chart= config['select_chart']
|
506 |
|
@@ -532,17 +588,34 @@ def task_cohort(task, mimic_path, config_path):
|
|
532 |
print(data_icu)
|
533 |
if data_icu :
|
534 |
feature_selection_icu.feature_icu(cohort_output, version_path,diag_flag,out_flag,chart_flag,proc_flag,med_flag)
|
|
|
|
|
535 |
#----------------------------------------------GROUPING-------------------------------------------------------
|
536 |
if data_icu:
|
537 |
if diag_flag:
|
538 |
-
group_diag=
|
539 |
feature_selection_icu.preprocess_features_icu(cohort_output, diag_flag, group_diag,False,False,False,0,0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
540 |
#----------------------------------------------SUMMARY-------------------------------------------------------
|
541 |
if data_icu:
|
542 |
feature_selection_icu.generate_summary_icu(diag_flag,proc_flag,med_flag,out_flag,chart_flag)
|
|
|
|
|
543 |
#----------------------------------------------FEATURE SELECTION---------------------------------------------
|
544 |
|
545 |
-
|
|
|
|
|
|
|
|
|
546 |
#---------------------------------------CLEANING OF FEATURES-----------------------------------------------
|
547 |
thresh=0
|
548 |
if data_icu:
|
@@ -553,6 +626,14 @@ def task_cohort(task, mimic_path, config_path):
|
|
553 |
thresh=config['outlier']
|
554 |
left_thresh=config['left_outlier']
|
555 |
feature_selection_icu.preprocess_features_icu(cohort_output, False, False,chart_flag,clean_chart,impute_outlier_chart,thresh,left_thresh)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
556 |
# ---------------------------------------tim-Series Representation--------------------------------------------
|
557 |
if radimp == 'forward fill and mean' :
|
558 |
impute='Mean'
|
@@ -563,11 +644,16 @@ def task_cohort(task, mimic_path, config_path):
|
|
563 |
|
564 |
if data_icu:
|
565 |
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)
|
|
|
|
|
|
|
566 |
end = time.time()
|
567 |
print("Time elapsed : ", round((end - start)/60,2),"mins")
|
568 |
print("[============TASK COHORT SUCCESSFULLY CREATED============]")
|
569 |
|
570 |
|
|
|
|
|
571 |
#############################################DATASET####################################################################
|
572 |
class Mimic4DatasetConfig(datasets.BuilderConfig):
|
573 |
"""BuilderConfig for Mimic4Dataset."""
|
@@ -676,13 +762,22 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
676 |
shutil.move(file_path,'./config')
|
677 |
with open(self.conf) as f:
|
678 |
config = yaml.safe_load(f)
|
679 |
-
|
|
|
|
|
|
|
|
|
|
|
680 |
|
681 |
#####################downloads modules from hub
|
682 |
if not os.path.exists('./model/data_generation_icu_modify.py'):
|
683 |
file_path, head = urlretrieve(_DATA_GEN, "data_generation_icu_modify.py")
|
684 |
shutil.move(file_path, './model')
|
685 |
|
|
|
|
|
|
|
|
|
686 |
if not os.path.exists('./preprocessing/day_intervals_preproc/day_intervals_cohort_v22.py'):
|
687 |
file_path, head = urlretrieve(_DAY_INT, "day_intervals_cohort_v22.py")
|
688 |
shutil.move(file_path, './preprocessing/day_intervals_preproc')
|
@@ -757,7 +852,7 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
757 |
"id": datasets.Sequence(datasets.Value("int32")),
|
758 |
"value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
|
759 |
},
|
760 |
-
"CHART":
|
761 |
{
|
762 |
"signal" : {
|
763 |
"id": datasets.Sequence(datasets.Value("int32")),
|
@@ -787,7 +882,6 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
787 |
dataDic = pickle.load(fp)
|
788 |
for hid, data in dataDic.items():
|
789 |
proc_features = data['Proc']
|
790 |
-
chart_features = data['Chart']
|
791 |
meds_features = data['Med']
|
792 |
out_features = data['Out']
|
793 |
cond_features = data['Cond']['fids']
|
@@ -807,6 +901,11 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
807 |
outs = {"id" : items_outs,
|
808 |
"value": values_outs}
|
809 |
|
|
|
|
|
|
|
|
|
|
|
810 |
#chart signal
|
811 |
if ('signal' in chart_features):
|
812 |
items_chart_sig = list(chart_features['signal'].keys())
|
@@ -861,6 +960,7 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
861 |
"rate" : meds_rate,
|
862 |
"amount" : meds_amount}
|
863 |
|
|
|
864 |
yield int(hid), {
|
865 |
"label" : label,
|
866 |
"gender" : gender,
|
@@ -869,12 +969,13 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
869 |
"age" : age,
|
870 |
"COND" : cond_features,
|
871 |
"PROC" : procs,
|
872 |
-
"CHART" : charts,
|
873 |
"OUT" : outs,
|
874 |
"MEDS" : meds
|
875 |
}
|
876 |
|
877 |
|
|
|
878 |
###########################################################ENCODED##################################################################
|
879 |
|
880 |
def _info_encoded(self):
|
@@ -915,7 +1016,7 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
915 |
|
916 |
for i, data in df.iterrows():
|
917 |
concat_cols=[]
|
918 |
-
dyn_df,cond_df,demo=concat_data(data,task,self.feat_cond,self.feat_proc,self.feat_out, self.feat_chart, self.feat_meds)
|
919 |
dyn=dyn_df.copy()
|
920 |
dyn.columns=dyn.columns.droplevel(0)
|
921 |
cols=dyn.columns
|
@@ -943,7 +1044,7 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
943 |
"COND" : datasets.Array2D(shape=(None, 1025), dtype='int64') ,
|
944 |
"MEDS" : datasets.Array2D(shape=(None, self.size_meds), dtype='int64') ,
|
945 |
"PROC" : datasets.Array2D(shape=(None, self.size_proc), dtype='int64') ,
|
946 |
-
"CHART" : datasets.Array2D(shape=(None, self.size_chart), dtype='int64') ,
|
947 |
"OUT" : datasets.Array2D(shape=(None, self.size_out), dtype='int64') ,
|
948 |
|
949 |
}
|
@@ -961,17 +1062,29 @@ class Mimic4Dataset(datasets.GeneratorBasedBuilder):
|
|
961 |
dico = pickle.load(fp)
|
962 |
task=self.config.name.replace(" ","_")
|
963 |
for key, data in dico.items():
|
964 |
-
stat, demo, meds, chart, out, proc, lab, y = getXY_deep(data, task, self.feat_cond, self.feat_proc, self.feat_out, self.feat_chart, self.feat_meds)
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
972 |
-
|
973 |
-
|
974 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
975 |
|
976 |
#############################################################################################################################
|
977 |
def _info(self):
|
|
|
27 |
_CITATION = "https://proceedings.mlr.press/v193/gupta22a.html"
|
28 |
_URL = "https://github.com/healthylaife/MIMIC-IV-Data-Pipeline"
|
29 |
_DATA_GEN = 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/data_generation_icu_modify.py'
|
30 |
+
_DATA_GEN_HOSP= 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/data_generation_modify.py'
|
31 |
_DAY_INT= 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/day_intervals_cohort_v22.py'
|
32 |
_CONFIG_URLS = {'los' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/los.config',
|
33 |
'mortality' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/mortality.config',
|
|
|
54 |
predW = config['predW']
|
55 |
disease_filter = config['disease_filter']
|
56 |
icu_no_icu = config['icu_no_icu']
|
57 |
+
groupingDiag = config['groupingDiag']
|
58 |
+
|
59 |
+
assert( icu_no_icu in ['ICU','Non-ICU' ], "Chossen data should be one of the following: ICU, Non-ICU")
|
60 |
+
data_icu = icu_no_icu=='ICU'
|
61 |
+
|
62 |
+
if data_icu:
|
63 |
+
chart_flag = config['chart']
|
64 |
+
output_flag = config['output']
|
65 |
+
select_chart = config['select_chart']
|
66 |
+
else:
|
67 |
+
lab_flag =config['lab']
|
68 |
+
select_lab = config['select_lab']
|
69 |
+
groupingMed = config['groupingMed']
|
70 |
+
groupingProc = config['groupingProc']
|
71 |
+
|
72 |
|
|
|
|
|
73 |
diag_flag= config['diagnosis']
|
74 |
proc_flag = config['proc']
|
75 |
meds_flag = config['meds']
|
|
|
76 |
select_diag= config['select_diag']
|
77 |
select_med= config['select_med']
|
78 |
select_proc= config['select_proc']
|
79 |
select_out = config['select_out']
|
|
|
80 |
|
81 |
outlier_removal=config['outlier_removal']
|
82 |
thresh=config['outlier']
|
83 |
left_thresh=config['left_outlier']
|
84 |
|
85 |
+
if data_icu:
|
86 |
+
assert (isinstance(select_diag,bool) and isinstance(select_med,bool) and isinstance(select_proc,bool) and isinstance(select_out,bool) and isinstance(select_chart,bool), " select_diag, select_chart, select_med, select_proc, select_out should be boolean")
|
87 |
+
assert (isinstance(chart_flag,bool) and isinstance(output_flag,bool) and isinstance(diag_flag,bool) and isinstance(proc_flag,bool) and isinstance(meds_flag,bool), "chart_flag, output_flag, diag_flag, proc_flag, meds_flag should be boolean")
|
88 |
+
|
89 |
+
else:
|
90 |
+
assert (isinstance(select_diag,bool) and isinstance(select_med,bool) and isinstance(select_proc,bool) and isinstance(select_out,bool) and isinstance(select_lab,bool), " select_diag, select_lab, select_med, select_proc, select_out should be boolean")
|
91 |
+
assert (isinstance(lab_flag,bool) and isinstance(diag_flag,bool) and isinstance(proc_flag,bool) and isinstance(meds_flag,bool), "lab_flag, diag_flag, proc_flag, meds_flag should be boolean")
|
92 |
+
|
93 |
if task=='Phenotype':
|
94 |
if disease_label=='Heart Failure':
|
95 |
label='Readmission'
|
|
|
134 |
raise ValueError('Task not correct')
|
135 |
|
136 |
assert( disease_filter in ['Heart Failure','COPD','CKD','CAD',""], "Disease filter should be one of the following: Heart Failure, COPD, CKD, CAD or empty")
|
137 |
+
assert( groupingDiag in ['Convert ICD-9 to ICD-10 and group ICD-10 codes','Keep both ICD-9 and ICD-10 codes','Convert ICD-9 to ICD-10 codes'], "Grouping ICD should be one of the following: Convert ICD-9 to ICD-10 and group ICD-10 codes, Keep both ICD-9 and ICD-10 codes, Convert ICD-9 to ICD-10 codes")
|
|
|
138 |
assert (bucket<=6 and bucket>=1 and isinstance(bucket, int), "Time bucket should be between 1 and 6 and an integer")
|
139 |
assert (radimp in ['No Imputation', 'forward fill and mean','forward fill and median'], "imputation should be one of the following: No Imputation, forward fill and mean, forward fill and median")
|
140 |
if chart_flag:
|
141 |
assert (left_thresh>=0 and left_thresh<=10 and isinstance(left_thresh, int), "Left outlier threshold should be between 0 and 10 and an integer")
|
142 |
assert (thresh>=90 and thresh<=99 and isinstance(thresh, int), "Outlier threshold should be between 90 and 99 and an integer")
|
143 |
assert (outlier_removal in ['No outlier detection','Impute Outlier (default:98)','Remove outliers (default:98)'], "Outlier removal should be one of the following: No outlier detection, Impute Outlier (default:98), Remove outliers (default:98)")
|
144 |
+
if lab_flag:
|
145 |
+
assert (left_thresh>=0 and left_thresh<=10 and isinstance(left_thresh, int), "Left outlier threshold should be between 0 and 10 and an integer")
|
146 |
+
assert (thresh>=90 and thresh<=99 and isinstance(thresh, int), "Outlier threshold should be between 90 and 99 and an integer")
|
147 |
+
assert (outlier_removal in ['No outlier detection','Impute Outlier (default:98)','Remove outliers (default:98)'], "Outlier removal should be one of the following: No outlier detection, Impute Outlier (default:98), Remove outliers (default:98)")
|
148 |
+
assert (groupingProc in ['ICD-9 and ICD-10','ICD-10'], "Grouping procedure should be one of the following: ICD-9 and ICD-10, ICD-10")
|
149 |
+
assert (groupingMed in ['Yes','No'], "Do you want to group Medication codes to use Non propietary names? : Grouping medication should be one of the following: Yes, No")
|
150 |
|
151 |
return label, time, disease_label, predW
|
152 |
|
|
|
223 |
|
224 |
return len(condVocabDict),len(procVocabDict),len(medVocabDict),len(outVocabDict),len(chartVocabDict),len(labVocabDict),ethVocabDict,genderVocabDict,ageVocabDict,insVocabDict
|
225 |
|
226 |
+
def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab):
|
227 |
meds=data['Med']
|
228 |
proc = data['Proc']
|
229 |
out = data['Out']
|
|
|
330 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
|
331 |
features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
|
332 |
chart_df=features.fillna(0)
|
333 |
+
|
334 |
+
##########LAB#########
|
335 |
+
if (feat_lab):
|
336 |
+
with open("./data/dict/"+task+"/labsVocab", 'rb') as fp:
|
337 |
+
chartDic = pickle.load(fp)
|
338 |
+
|
339 |
+
if chart:
|
340 |
+
charts=chart['val']
|
341 |
+
feat=charts.keys()
|
342 |
+
chart_val=[charts[key] for key in feat]
|
343 |
+
charts=pd.DataFrame(chartDic,columns=['LAB'])
|
344 |
+
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
|
345 |
+
features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
|
346 |
+
|
347 |
+
chart=pd.DataFrame(columns=feat)
|
348 |
+
for c,v in zip(feat,chart_val):
|
349 |
+
chart[c]=v
|
350 |
+
chart.columns=pd.MultiIndex.from_product([["LAB"], chart.columns])
|
351 |
+
chart_df = pd.concat([features,chart],ignore_index=True).fillna(0)
|
352 |
+
else:
|
353 |
+
charts=pd.DataFrame(chartDic,columns=['LAB'])
|
354 |
+
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
|
355 |
+
features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
|
356 |
+
chart_df=features.fillna(0)
|
357 |
|
358 |
###MEDS
|
359 |
if (feat_meds):
|
|
|
380 |
|
381 |
dyn_df = pd.concat([meds_df,proc_df,out_df,chart_df], axis=1)
|
382 |
return dyn_df,cond_df,demo
|
383 |
+
|
384 |
+
|
385 |
+
def getXY_deep(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab):
|
386 |
stat_df = torch.zeros(size=(1,0))
|
387 |
demo_df = torch.zeros(size=(1,0))
|
388 |
meds = torch.zeros(size=(0,0))
|
|
|
392 |
lab = torch.zeros(size=(0,0))
|
393 |
stat_df = torch.zeros(size=(1,0))
|
394 |
demo_df = torch.zeros(size=(1,0))
|
|
|
395 |
|
396 |
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)
|
397 |
+
dyn,cond_df,demo=concat_data(data,task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab)
|
398 |
|
399 |
###########""
|
400 |
if feat_chart:
|
|
|
509 |
|
510 |
|
511 |
|
512 |
+
|
513 |
+
|
514 |
+
|
515 |
def task_cohort(task, mimic_path, config_path):
|
516 |
sys.path.append('./preprocessing/day_intervals_preproc')
|
517 |
sys.path.append('./utils')
|
|
|
521 |
import day_intervals_cohort
|
522 |
import feature_selection_icu
|
523 |
import data_generation_icu_modify
|
524 |
+
import data_generation_modify
|
525 |
+
import feature_selection_hosp
|
526 |
+
|
527 |
|
528 |
root_dir = os.path.dirname(os.path.abspath('UserInterface.ipynb'))
|
529 |
config_path='./config/'+config_path
|
|
|
545 |
chart_flag = config['chart']
|
546 |
proc_flag= config['proc']
|
547 |
med_flag = config['meds']
|
548 |
+
lab_flag = config['lab']
|
549 |
|
550 |
disease_filter = config['disease_filter']
|
551 |
icu_no_icu = config['icu_no_icu']
|
552 |
+
groupingDiag = config['groupingDiag']
|
553 |
+
groupingMed = config['groupingMed']
|
554 |
+
groupingProc = config['groupingProc']
|
555 |
|
556 |
select_diag= config['select_diag']
|
557 |
select_med= config['select_med']
|
558 |
select_proc= config['select_proc']
|
559 |
+
select_lab= config['select_lab']
|
560 |
select_out= config['select_out']
|
561 |
select_chart= config['select_chart']
|
562 |
|
|
|
588 |
print(data_icu)
|
589 |
if data_icu :
|
590 |
feature_selection_icu.feature_icu(cohort_output, version_path,diag_flag,out_flag,chart_flag,proc_flag,med_flag)
|
591 |
+
else:
|
592 |
+
feature_selection_hosp.feature_nonicu(cohort_output, version_path,diag_flag,lab_flag,proc_flag,med_flag)
|
593 |
#----------------------------------------------GROUPING-------------------------------------------------------
|
594 |
if data_icu:
|
595 |
if diag_flag:
|
596 |
+
group_diag=groupingDiag
|
597 |
feature_selection_icu.preprocess_features_icu(cohort_output, diag_flag, group_diag,False,False,False,0,0)
|
598 |
+
|
599 |
+
else:
|
600 |
+
if diag_flag:
|
601 |
+
group_diag=groupingDiag
|
602 |
+
if med_flag:
|
603 |
+
group_med=groupingMed
|
604 |
+
if proc_flag:
|
605 |
+
group_proc=groupingProc
|
606 |
+
feature_selection_hosp.preprocess_features_hosp(cohort_output, diag_flag,proc_flag,med_flag,False,group_diag,group_med,group_proc,False,False,0,0)
|
607 |
#----------------------------------------------SUMMARY-------------------------------------------------------
|
608 |
if data_icu:
|
609 |
feature_selection_icu.generate_summary_icu(diag_flag,proc_flag,med_flag,out_flag,chart_flag)
|
610 |
+
else:
|
611 |
+
feature_selection_hosp.generate_summary_hosp(diag_flag,proc_flag,med_flag,lab_flag)
|
612 |
#----------------------------------------------FEATURE SELECTION---------------------------------------------
|
613 |
|
614 |
+
if data_icu:
|
615 |
+
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)
|
616 |
+
else:
|
617 |
+
feature_selection_hosp.features_selection_hosp(cohort_output, diag_flag,proc_flag,med_flag,lab_flag,select_diag,select_med,select_proc,select_lab)
|
618 |
+
|
619 |
#---------------------------------------CLEANING OF FEATURES-----------------------------------------------
|
620 |
thresh=0
|
621 |
if data_icu:
|
|
|
626 |
thresh=config['outlier']
|
627 |
left_thresh=config['left_outlier']
|
628 |
feature_selection_icu.preprocess_features_icu(cohort_output, False, False,chart_flag,clean_chart,impute_outlier_chart,thresh,left_thresh)
|
629 |
+
else:
|
630 |
+
if lab_flag:
|
631 |
+
outlier_removal=config['outlier_removal']
|
632 |
+
clean_chart=outlier_removal!='No outlier detection'
|
633 |
+
impute_outlier_chart=outlier_removal=='Impute Outlier (default:98)'
|
634 |
+
thresh=config['outlier']
|
635 |
+
left_thresh=config['left_outlier']
|
636 |
+
feature_selection_hosp.preprocess_features_hosp(cohort_output, False,False, False,lab_flag,False,False,False,clean_chart,impute_outlier_chart,thresh,left_thresh)
|
637 |
# ---------------------------------------tim-Series Representation--------------------------------------------
|
638 |
if radimp == 'forward fill and mean' :
|
639 |
impute='Mean'
|
|
|
644 |
|
645 |
if data_icu:
|
646 |
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)
|
647 |
+
else:
|
648 |
+
gen=data_generation_modify.Generator(cohort_output,data_mort,data_admn,data_los,diag_flag,lab_flag,proc_flag,med_flag,impute,include,bucket,predW)
|
649 |
+
|
650 |
end = time.time()
|
651 |
print("Time elapsed : ", round((end - start)/60,2),"mins")
|
652 |
print("[============TASK COHORT SUCCESSFULLY CREATED============]")
|
653 |
|
654 |
|
655 |
+
|
656 |
+
|
657 |
#############################################DATASET####################################################################
|
658 |
class Mimic4DatasetConfig(datasets.BuilderConfig):
|
659 |
"""BuilderConfig for Mimic4Dataset."""
|
|
|
762 |
shutil.move(file_path,'./config')
|
763 |
with open(self.conf) as f:
|
764 |
config = yaml.safe_load(f)
|
765 |
+
|
766 |
+
self.data_icu = config['icu_no_icu']=='ICU'
|
767 |
+
if self.data_icu:
|
768 |
+
self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out, self.lab = config['diagnosis'], config['chart'], config['proc'], config['meds'], config['output'], False
|
769 |
+
else:
|
770 |
+
self.feat_cond, self.feat_lab, self.feat_proc, self.feat_meds, self.feat_chart, self.out = config['diagnosis'], config['lab'], config['proc'], config['meds'], False, False
|
771 |
|
772 |
#####################downloads modules from hub
|
773 |
if not os.path.exists('./model/data_generation_icu_modify.py'):
|
774 |
file_path, head = urlretrieve(_DATA_GEN, "data_generation_icu_modify.py")
|
775 |
shutil.move(file_path, './model')
|
776 |
|
777 |
+
if not os.path.exists('./model/data_generation_modify.py'):
|
778 |
+
file_path, head = urlretrieve(_DATA_GEN_HOSP, "data_generation_modify.py")
|
779 |
+
shutil.move(file_path, './model')
|
780 |
+
|
781 |
if not os.path.exists('./preprocessing/day_intervals_preproc/day_intervals_cohort_v22.py'):
|
782 |
file_path, head = urlretrieve(_DAY_INT, "day_intervals_cohort_v22.py")
|
783 |
shutil.move(file_path, './preprocessing/day_intervals_preproc')
|
|
|
852 |
"id": datasets.Sequence(datasets.Value("int32")),
|
853 |
"value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
|
854 |
},
|
855 |
+
"CHART/LAB":
|
856 |
{
|
857 |
"signal" : {
|
858 |
"id": datasets.Sequence(datasets.Value("int32")),
|
|
|
882 |
dataDic = pickle.load(fp)
|
883 |
for hid, data in dataDic.items():
|
884 |
proc_features = data['Proc']
|
|
|
885 |
meds_features = data['Med']
|
886 |
out_features = data['Out']
|
887 |
cond_features = data['Cond']['fids']
|
|
|
901 |
outs = {"id" : items_outs,
|
902 |
"value": values_outs}
|
903 |
|
904 |
+
if self.data_icu:
|
905 |
+
chart_features = data['Chart']
|
906 |
+
else:
|
907 |
+
chart_features = data['Lab']
|
908 |
+
|
909 |
#chart signal
|
910 |
if ('signal' in chart_features):
|
911 |
items_chart_sig = list(chart_features['signal'].keys())
|
|
|
960 |
"rate" : meds_rate,
|
961 |
"amount" : meds_amount}
|
962 |
|
963 |
+
|
964 |
yield int(hid), {
|
965 |
"label" : label,
|
966 |
"gender" : gender,
|
|
|
969 |
"age" : age,
|
970 |
"COND" : cond_features,
|
971 |
"PROC" : procs,
|
972 |
+
"CHART/LAB" : charts,
|
973 |
"OUT" : outs,
|
974 |
"MEDS" : meds
|
975 |
}
|
976 |
|
977 |
|
978 |
+
|
979 |
###########################################################ENCODED##################################################################
|
980 |
|
981 |
def _info_encoded(self):
|
|
|
1016 |
|
1017 |
for i, data in df.iterrows():
|
1018 |
concat_cols=[]
|
1019 |
+
dyn_df,cond_df,demo=concat_data(data,task,self.feat_cond,self.feat_proc,self.feat_out, self.feat_chart, self.feat_meds,self.feat_lab)
|
1020 |
dyn=dyn_df.copy()
|
1021 |
dyn.columns=dyn.columns.droplevel(0)
|
1022 |
cols=dyn.columns
|
|
|
1044 |
"COND" : datasets.Array2D(shape=(None, 1025), dtype='int64') ,
|
1045 |
"MEDS" : datasets.Array2D(shape=(None, self.size_meds), dtype='int64') ,
|
1046 |
"PROC" : datasets.Array2D(shape=(None, self.size_proc), dtype='int64') ,
|
1047 |
+
"CHART/LAB" : datasets.Array2D(shape=(None, self.size_chart), dtype='int64') ,
|
1048 |
"OUT" : datasets.Array2D(shape=(None, self.size_out), dtype='int64') ,
|
1049 |
|
1050 |
}
|
|
|
1062 |
dico = pickle.load(fp)
|
1063 |
task=self.config.name.replace(" ","_")
|
1064 |
for key, data in dico.items():
|
1065 |
+
stat, demo, meds, chart, out, proc, lab, y = getXY_deep(data, task, self.feat_cond, self.feat_proc, self.feat_out, self.feat_chart, self.feat_meds,self.feat_lab)
|
1066 |
+
|
1067 |
+
if self.data_icu:
|
1068 |
+
yield int(key), {
|
1069 |
+
'label': y,
|
1070 |
+
'DEMO': demo,
|
1071 |
+
'COND': stat,
|
1072 |
+
'MEDS': meds,
|
1073 |
+
'PROC': proc,
|
1074 |
+
'CHART/LAB': chart,
|
1075 |
+
'OUT': out,
|
1076 |
+
}
|
1077 |
+
else:
|
1078 |
+
yield int(key), {
|
1079 |
+
'label': y,
|
1080 |
+
'DEMO': demo,
|
1081 |
+
'COND': stat,
|
1082 |
+
'MEDS': meds,
|
1083 |
+
'PROC': proc,
|
1084 |
+
'CHART/LAB': lab,
|
1085 |
+
'OUT': out,
|
1086 |
+
}
|
1087 |
+
|
1088 |
|
1089 |
#############################################################################################################################
|
1090 |
def _info(self):
|