File size: 10,868 Bytes
8b8ed7d
 
 
 
 
2905dfe
48c5fb0
3dc5893
60f8433
02342db
8b8ed7d
 
 
 
 
3a44a8f
8b8ed7d
 
 
 
3dc5893
02342db
65408f0
 
 
 
 
08904b5
 
 
2905dfe
 
 
 
f0830d9
2905dfe
08904b5
 
a6ab2dc
 
 
 
 
 
 
 
37f1ab4
496e800
4578bb4
a6ab2dc
8b8ed7d
08904b5
8b8ed7d
 
719f4db
8b8ed7d
08904b5
8b8ed7d
 
719f4db
8b8ed7d
08904b5
8b8ed7d
 
719f4db
8b8ed7d
08904b5
8b8ed7d
 
719f4db
8b8ed7d
 
 
 
 
 
 
 
 
2bca97a
8b8ed7d
 
 
2905dfe
1410ca4
1f87786
1410ca4
 
3fd11f7
1410ca4
1f87786
 
1410ca4
73e2a36
3fd11f7
1410ca4
1f87786
 
1410ca4
73e2a36
3fd11f7
1410ca4
1f87786
1410ca4
7e535bc
 
f982edd
7f7a29a
3838254
1189a30
1f55b95
 
 
 
 
 
 
 
3838254
a456741
 
 
 
2bca97a
8b8ed7d
 
 
 
 
 
 
 
 
9ce76f5
422f6f1
 
 
 
 
3dc5893
3a44a8f
 
 
 
 
 
65408f0
 
 
1d2022c
 
 
 
 
65408f0
a96906c
f042773
02342db
417795a
65408f0
8b8ed7d
3a44a8f
8b8ed7d
 
 
3a44a8f
8a08aed
 
 
 
 
 
 
 
 
 
 
 
a456741
7e535bc
 
eb8a83e
4ad564e
a456741
 
 
 
 
0767865
1f55b95
daf5242
 
 
 
 
 
 
 
1f55b95
daf5242
 
 
 
 
 
 
 
 
3838254
 
1410ca4
 
daf5242
 
 
 
1410ca4
daf5242
 
 
1410ca4
daf5242
 
 
 
 
 
 
 
1410ca4
daf5242
 
 
 
 
 
 
 
3fd11f7
1410ca4
 
 
3fd11f7
46b051d
1410ca4
8a08aed
 
 
1410ca4
7e535bc
3838254
a456741
1410ca4
8a08aed
1189a30
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
import csv
import json
import os
import pandas as pd
import datasets
import pickle
from .cohort import task_cohort
import subprocess
import shutil
from urllib.request import urlretrieve

_DESCRIPTION = """\
Dataset for mimic4 data, by default for the Mortality task.
Available tasks are: Mortality, Length of Stay, Readmission, Phenotype.
The data is extracted from the mimic4 database using this pipeline: 'https://github.com/healthylaife/MIMIC-IV-Data-Pipeline/tree/main'
mimic path should have this form : "absolute/path/to/mimic4data/mimiciv/2.2"
"""

_HOMEPAGE = "https://huggingface.co/datasets/thbndi/Mimic4Dataset"
_CITATION = "https://proceedings.mlr.press/v193/gupta22a.html"
_URL = "https://github.com/healthylaife/MIMIC-IV-Data-Pipeline"
_DATA_GEN = 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/blob/main/data_generation_icu_modify.py'
_CONFIG_URLS = {'los' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/blob/main/config/los.config',
                'mortality' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/blob/main/config/mortality.config',
                'phenotype' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/blob/main/config/phenotype.config',
                'readmission' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/blob/main/config/readmission.config'
        }
class Mimic4DatasetConfig(datasets.BuilderConfig):
    """BuilderConfig for Mimic4Dataset."""

    def __init__(
        self,
        **kwargs,
    ):
        super().__init__(**kwargs)
        
class Mimic4Dataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    def __init__(self, **kwargs):
        self.mimic_path = kwargs.pop("mimic_path", None)
        if self.mimic_path is None:
            raise ValueError("You must specify the path of the mimic4 data")
        if not os.path.exists(self.mimic_path):
            raise ValueError("The path of the mimic4 data does not exist")
        
        self.config_path = kwargs.pop("config_path",None)
        super().__init__(**kwargs)
        

    BUILDER_CONFIGS = [
        Mimic4DatasetConfig(
            name="Phenotype",
            version=VERSION,
            description="Dataset for mimic4 Phenotype task"
        ),
        Mimic4DatasetConfig(
            name="Readmission",
            version=VERSION,
            description="Dataset for mimic4 Readmission task"
        ),
        Mimic4DatasetConfig(
            name="Length of Stay",
            version=VERSION,
            description="Dataset for mimic4 Length of Stay task"
        ),
        Mimic4DatasetConfig(
            name="Mortality",
            version=VERSION,
            description="Dataset for mimic4 Mortality task"
        ),
    ]

    DEFAULT_CONFIG_NAME = "Mortality"

    def _info(self):

        features = datasets.Features(
            {
                "label": datasets.ClassLabel(names=["0", "1"]),
                "gender": datasets.Value("string"),
                "ethnicity": datasets.Value("string"),
                "age": datasets.Value("int32"),
                "COND": datasets.Sequence(datasets.Value("string")),
                "MEDS": {
                            "signal": 
                                {
                                    "id": datasets.Sequence(datasets.Value("int32")),
                                    "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                }
                            ,
                            "rate": 
                                {
                                    "id": datasets.Sequence(datasets.Value("int32")),
                                    "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                }
                            ,
                            "amount": 
                                {
                                    "id": datasets.Sequence(datasets.Value("int32")),
                                    "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                }
                            
                        },
                "PROC":  {
                            "id": datasets.Sequence(datasets.Value("int32")),
                            "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                },
                "CHART":
                    {
                        "signal" : {
                            "id": datasets.Sequence(datasets.Value("int32")),
                            "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                },
                        "val" : {
                            "id": datasets.Sequence(datasets.Value("int32")),
                            "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                },
                    },
                "OUT":  {
                            "id": datasets.Sequence(datasets.Value("int32")),
                            "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                },
                
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager()):
        if self.config.name == 'Phenotype' and self.config_path is None : self.config_path = _CONFIG_URLS['phenotype'] 
        if self.config.name == 'Readmission' and self.config_path is None : self.config_path = _CONFIG_URLS['readmission'] 
        if self.config.name == 'Length of Stay' and self.config_path is None : self.config_path = _CONFIG_URLS['los'] 
        if self.config.name == 'Mortality' and self.config_path is None : self.config_path = _CONFIG_URLS['mortality']
        print(self.config_path)
        repo_url='https://github.com/healthylaife/MIMIC-IV-Data-Pipeline'
        if os.path.exists(os.path.dirname(os.path.abspath('MIMIC-IV-Data-Pipeline-main'))):
            path_bench = os.path.dirname(os.path.abspath('MIMIC-IV-Data-Pipeline-main'))+'/MIMIC-IV-Data-Pipeline-main'
        else:
            repodir = os.getcwd()
            path_bench = repodir+'/MIMIC-IV-Data-Pipeline-main'
            subprocess.run(["git", "clone", repo_url, path_bench])
        
        if not os.path.exists(path_bench+'/config'):
            os.makedirs(path_bench+'/config')
        if self.config_path[0:4] == 'http':
            file_path, head = urlretrieve(self.config_path)
        else :
            print(self.config_path)
        #shutil.move(file_path, path_bench+'/config')
        
        file_path, head = urlretrieve(_DATA_GEN, "data_generation_icu_modify.py")
        shutil.move(file_path, path_bench+'/model')
        
        data_dir = path_bench + "/data/dataDic"
        task_cohort(self.name,self.mimic_path, path_bench, self.config_path)
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_dir}),
        ]


    def _generate_examples(self, filepath):
        with open(filepath, 'rb') as fp:
            dataDic = pickle.load(fp)
        for hid, data in dataDic.items():
            proc_features = data['Proc']
            chart_features = data['Chart']
            meds_features = data['Med']
            out_features = data['Out']
            cond_features = data['Cond']['fids']
            eth= data['ethnicity']
            age = data['age']
            gender = data['gender']
            label = data['label']
            
            items = list(proc_features.keys())
            values =[proc_features[i] for i in items ]
            procs = {"id" : items,
                  "value": values}
            
            items_outs = list(out_features.keys())
            values_outs =[out_features[i] for i in items_outs ]
            outs = {"id" : items_outs,
                  "value": values_outs}

            #chart signal
            if ('signal' in chart_features):
                items_chart_sig = list(chart_features['signal'].keys())
                values_chart_sig =[chart_features['signal'][i] for i in items_chart_sig ]
                chart_sig = {"id" : items_chart_sig,
                        "value": values_chart_sig}
            else:
                chart_sig = {"id" : [],
                        "value": []}
            #chart val
            if ('val' in chart_features):
                items_chart_val = list(chart_features['val'].keys())
                values_chart_val =[chart_features['val'][i] for i in items_chart_val ]
                chart_val = {"id" : items_chart_val,
                        "value": values_chart_val}
            else:
                chart_val = {"id" : [],
                        "value": []}
                
            charts = {"signal" : chart_sig,
                    "val" : chart_val}

            #meds signal
            if ('signal' in meds_features):
                items_meds_sig = list(meds_features['signal'].keys())
                values_meds_sig =[meds_features['signal'][i] for i in items_meds_sig ]
                meds_sig = {"id" : items_meds_sig,
                    "value": values_meds_sig}
            else:
                meds_sig = {"id" : [],
                    "value": []}
            #meds rate
            if ('rate' in meds_features):
                items_meds_rate = list(meds_features['rate'].keys())
                values_meds_rate =[meds_features['rate'][i] for i in items_meds_rate ]
                meds_rate = {"id" : items_meds_rate,
                        "value": values_meds_rate}
            else:
                meds_rate = {"id" : [],
                        "value": []}
            #meds amount
            if ('amount' in meds_features):
                items_meds_amount = list(meds_features['amount'].keys())
                values_meds_amount =[meds_features['amount'][i] for i in items_meds_amount ]
                meds_amount = {"id" : items_meds_amount,
                        "value": values_meds_amount}
            else:
                meds_amount = {"id" : [],
                        "value": []}
            
            meds = {"signal" : meds_sig,
                    "rate" : meds_rate,
                    "amount" : meds_amount}
            
            yield int(hid), {
                "label" : label,
                "gender" : gender,
                "ethnicity" : eth,
                "age" : age,
                "COND" : cond_features,
                "PROC" : procs,
                "CHART" : charts,
                "OUT" : outs,
                "MEDS" : meds
            }