import os import sys import yaml import time from .check_config import check_config from .day_intervals_cohort_v22 import * from .data_generation_icu_modify import * from .data_generation_modify import * def task_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 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(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 = 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) # ---------------------------------------tim-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=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=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============]")