|
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") |
|
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() |
|
|
|
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' |
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
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) |
|
|
|
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) |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|
|
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_icu(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============]") |
|
|