File size: 15,167 Bytes
cb07198 bd9cea6 03fa0b9 bd9cea6 cb07198 bd9cea6 56e4abf cb07198 978f7e8 f069a5a cb07198 6bd714d cb07198 978f7e8 cb07198 f069a5a cb07198 6e3b7ae cb07198 f069a5a cb07198 |
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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
import os
import sys
import yaml
import time
import yaml
import sys
def check_config_file(task,config_file):
with open(config_file) as f:
config = yaml.safe_load(f)
if task=='Phenotype':
disease_label = config['disease_label']
else :
disease_label = ""
time = config['timePrediction']
label = task
timeW = config['timeWindow']
include=int(timeW.split()[1])
bucket = config['timebucket']
radimp = config['radimp']
predW = config['predW']
disease_filter = config['disease_filter']
icu_no_icu = config['icu_no_icu']
groupingDiag = config['groupingDiag']
#assert( icu_no_icu in ['ICU','Non-ICU' ], "Chossen data should be one of the following: ICU, Non-ICU")
assert( icu_no_icu =='ICU', "The dataset is only available for ICU data")
data_icu = icu_no_icu=='ICU'
if data_icu:
chart_flag = config['chart']
output_flag = config['output']
select_chart = config['select_chart']
lab_flag = False
select_lab = False
else:
lab_flag =config['lab']
select_lab = config['select_lab']
groupingMed = config['groupingMed']
groupingProc = config['groupingProc']
chart_flag = False
output_flag = False
select_chart = False
diag_flag= config['diagnosis']
proc_flag = config['proc']
meds_flag = config['meds']
select_diag= config['select_diag']
select_med= config['select_med']
select_proc= config['select_proc']
select_out = config['select_out']
outlier_removal=config['outlier_removal']
thresh=config['outlier']
left_thresh=config['left_outlier']
if data_icu:
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")
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")
else:
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")
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")
if task=='Phenotype':
if disease_label=='Heart Failure':
label='Readmission'
time=30
disease_label='I50'
elif disease_label=='CAD':
label='Readmission'
time=30
disease_label='I25'
elif disease_label=='CKD':
label='Readmission'
time=30
disease_label='N18'
elif disease_label=='COPD':
label='Readmission'
time=30
disease_label='J44'
else :
raise ValueError('Disease label not correct provide one in the list: Heart Failure, CAD, CKD, COPD')
predW=0
assert (timeW[0]=='Last' and include<=72 and include>=24, "Time window should be between Last 24 and Last 72")
elif task=='Mortality':
time=0
label= 'Mortality'
assert (predW<=8 and predW>=2, "Prediction window should be between 2 and 8")
assert (timeW[0]=='Fisrt' and include<=72 and include>=24, "Time window should be between First 24 and First 72")
elif task=='Length_of_Stay':
label= 'Length of Stay'
assert (timeW[0]=='Fisrt' and include<=72 and include>=24, "Time window should be between Fisrt 24 and Fisrt 72")
assert (time<=10 and time>=1, "Length of stay should be between 1 and 10")
predW=0
elif task=='Readmission':
label= 'Readmission'
assert (timeW[0]=='Last' and include<=72 and include>=24, "Time window should be between Last 24 and Last 72")
assert (time<=150 and time>=10 and time%10==0, "Readmission window should be between 10 and 150 with a step of 10")
predW=0
else:
raise ValueError('Task not correct')
assert( disease_filter in ['Heart Failure','COPD','CKD','CAD',""], "Disease filter should be one of the following: Heart Failure, COPD, CKD, CAD or empty")
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")
assert (bucket<=6 and bucket>=1 and isinstance(bucket, int), "Time bucket should be between 1 and 6 and an integer")
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")
if chart_flag:
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")
assert (thresh>=90 and thresh<=99 and isinstance(thresh, int), "Outlier threshold should be between 90 and 99 and an integer")
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)")
if lab_flag:
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")
assert (thresh>=90 and thresh<=99 and isinstance(thresh, int), "Outlier threshold should be between 90 and 99 and an integer")
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)")
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")
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")
return label, time, disease_label, predW
def create_cohort(task, mimic_path, config_path):
sys.path.append('./preprocessing/day_intervals_preproc')
sys.path.append('./utils')
sys.path.append('./preprocessing/hosp_module_preproc')
sys.path.append('./model')
import day_intervals_cohort
import feature_selection_icu
import feature_selection_hosp
import day_intervals_cohort_v22
import data_generation_icu_modify
import data_generation_modify
root_dir = os.path.dirname(os.path.abspath('UserInterface.ipynb'))
config_path='./config/'+config_path
with open(config_path) as f:
config = yaml.safe_load(f)
version_path = mimic_path+'/'
print(version_path)
version = mimic_path.split('/')[-1][0]
start = time.time()
#----------------------------------------------config----------------------------------------------------
label, tim, disease_label, predW = check_config_file(task,config_path)
icu_no_icu = config['icu_no_icu']
timeW = config['timeWindow']
include=int(timeW.split()[1])
bucket = config['timebucket']
radimp = config['radimp']
diag_flag = config['diagnosis']
proc_flag= config['proc']
med_flag = config['meds']
disease_filter = config['disease_filter']
groupingDiag = config['groupingDiag']
select_diag= config['select_diag']
select_med= config['select_med']
select_proc= config['select_proc']
if icu_no_icu=='ICU':
out_flag = config['output']
chart_flag = config['chart']
select_out= config['select_out']
select_chart= config['select_chart']
lab_flag = False
select_lab = False
else:
lab_flag = config['lab']
groupingMed = config['groupingMed']
groupingProc = config['groupingProc']
select_lab= config['select_lab']
out_flag = False
chart_flag = False
select_out= False
select_chart= False
# -------------------------------------------------------------------------------------------------------------
data_icu=icu_no_icu=="ICU"
data_mort=label=="Mortality"
data_admn=label=='Readmission'
data_los=label=='Length of Stay'
if (disease_filter=="Heart Failure"):
icd_code='I50'
elif (disease_filter=="CKD"):
icd_code='N18'
elif (disease_filter=="COPD"):
icd_code='J44'
elif (disease_filter=="CAD"):
icd_code='I25'
else:
icd_code='No Disease Filter'
#-----------------------------------------------EXTRACT MIMIC-----------------------------------------------------
if version == '2':
cohort_output = day_intervals_cohort_v22.extract_data(icu_no_icu,label,tim,icd_code, root_dir,version_path,disease_label)
elif version == '1':
cohort_output = day_intervals_cohort.extract_data(icu_no_icu,label,tim,icd_code, root_dir,version_path,disease_label)
#----------------------------------------------FEATURES-------------------------------------------------------
if data_icu :
feature_selection_icu.feature_icu(cohort_output, version_path,diag_flag,out_flag,chart_flag,proc_flag,med_flag)
else:
feature_selection_hosp.feature_nonicu(cohort_output, version_path,diag_flag,lab_flag,proc_flag,med_flag)
#----------------------------------------------GROUPING-------------------------------------------------------
if data_icu:
if diag_flag:
group_diag=groupingDiag
feature_selection_icu.preprocess_features_icu(cohort_output, diag_flag, group_diag,False,False,False,0,0)
else:
if diag_flag:
group_diag=groupingDiag
if med_flag:
group_med=groupingMed
if proc_flag:
group_proc=groupingProc
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)
#----------------------------------------------SUMMARY-------------------------------------------------------
if data_icu:
feature_selection_icu.generate_summary_icu(diag_flag,proc_flag,med_flag,out_flag,chart_flag)
else:
feature_selection_hosp.generate_summary_hosp(diag_flag,proc_flag,med_flag,lab_flag)
#----------------------------------------------FEATURE SELECTION---------------------------------------------
#----------------------------------------------FEATURE SELECTION---------------------------------------------
if data_icu:
if select_chart or select_out or select_diag or select_med or select_proc:
if select_chart:
input('Please edit list of codes in ./data/summary/chart_features.csv to select the chart items to keep and press enter to continue')
if select_out:
input('Please edit list of codes in ./data/summary/out_features.csv to select the output items to keep and press enter to continue')
if select_diag:
input('Please edit list of codes in ./data/summary/diag_features.csv to select the diagnosis ids to keep and press enter to continue')
if select_med:
input('Please edit list of codes in ./data/summary/med_features.csv to select the meds items to keep and press enter to continue')
if select_proc:
input('Please edit list of codes in ./data/summary/proc_features.csv to select the procedures ids to keep and press enter to continue')
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)
else:
if select_diag or select_med or select_proc or select_lab:
if select_diag:
input('Please edit list of codes in ./data/summary/diag_features.csv to select the diagnosis ids to keep and press enter to continue')
if select_med:
input('Please edit list of codes in ./data/summary/med_features.csv to select the meds items to keep and press enter to continue')
if select_proc:
input('Please edit list of codes in ./data/summary/proc_features.csv to select the procedures ids to keep and press enter to continue')
if select_lab:
input('Please edit list of codes in ./data/summary/labs_features.csv to select the labs items to keep and press enter to continue')
feature_selection_hosp.features_selection_hosp(cohort_output, diag_flag,proc_flag,med_flag,lab_flag,select_diag,select_med,select_proc,select_lab)
#---------------------------------------CLEANING OF FEATURES-----------------------------------------------
thresh=0
if data_icu:
if chart_flag:
outlier_removal=config['outlier_removal']
clean_chart=outlier_removal!='No outlier detection'
impute_outlier_chart=outlier_removal=='Impute Outlier (default:98)'
thresh=config['outlier']
left_thresh=config['left_outlier']
feature_selection_icu.preprocess_features_icu(cohort_output, False, False,chart_flag,clean_chart,impute_outlier_chart,thresh,left_thresh)
else:
if lab_flag:
outlier_removal=config['outlier_removal']
clean_chart=outlier_removal!='No outlier detection'
impute_outlier_chart=outlier_removal=='Impute Outlier (default:98)'
thresh=config['outlier']
left_thresh=config['left_outlier']
feature_selection_hosp.preprocess_features_hosp(cohort_output, False,False, False,lab_flag,False,False,False,clean_chart,impute_outlier_chart,thresh,left_thresh)
# ---------------------------------------time-Series Representation--------------------------------------------
if radimp == 'forward fill and mean' :
impute='Mean'
elif radimp =='forward fill and median':
impute = 'Median'
else :
impute = False
if data_icu:
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)
else:
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)
end = time.time()
print("Time elapsed : ", round((end - start)/60,2),"mins")
print("[============TASK COHORT SUCCESSFULLY CREATED============]")
|