File size: 8,487 Bytes
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
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============]")