Delete Mimic4Dataset.py

#1
by thbndi - opened
Files changed (1) hide show
  1. Mimic4Dataset.py +0 -270
Mimic4Dataset.py DELETED
@@ -1,270 +0,0 @@
1
- import csv
2
- import json
3
- import os
4
- import pandas as pd
5
- import datasets
6
- import sys
7
- import pickle
8
- import subprocess
9
- import shutil
10
- from urllib.request import urlretrieve
11
-
12
- _DESCRIPTION = """\
13
- Dataset for mimic4 data, by default for the Mortality task.
14
- Available tasks are: Mortality, Length of Stay, Readmission, Phenotype.
15
- The data is extracted from the mimic4 database using this pipeline: 'https://github.com/healthylaife/MIMIC-IV-Data-Pipeline/tree/main'
16
- mimic path should have this form : "absolute/path/to/mimic4data/mimiciv/2.2"
17
- """
18
-
19
- _HOMEPAGE = "https://huggingface.co/datasets/thbndi/Mimic4Dataset"
20
- _CITATION = "https://proceedings.mlr.press/v193/gupta22a.html"
21
- _URL = "https://github.com/healthylaife/MIMIC-IV-Data-Pipeline"
22
- _DATA_GEN = 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/blob/main/data_generation_icu_modify.py'
23
- _COHORT = 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/blob/main/cohort.py'
24
- _CONFIG_URLS = {'los' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/blob/main/config/los.config',
25
- 'mortality' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/los.config',
26
- 'phenotype' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/phenotype.config',
27
- 'readmission' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/readmission.config'
28
- }
29
- class Mimic4DatasetConfig(datasets.BuilderConfig):
30
- """BuilderConfig for Mimic4Dataset."""
31
-
32
- def __init__(
33
- self,
34
- **kwargs,
35
- ):
36
- super().__init__(**kwargs)
37
-
38
- class Mimic4Dataset(datasets.GeneratorBasedBuilder):
39
- VERSION = datasets.Version("1.0.0")
40
-
41
- def __init__(self, **kwargs):
42
- self.mimic_path = kwargs.pop("mimic_path", None)
43
- if self.mimic_path is None:
44
- raise ValueError("You must specify the path of the mimic4 data")
45
- if not os.path.exists(self.mimic_path):
46
- raise ValueError("The path of the mimic4 data does not exist")
47
-
48
- self.config_path = kwargs.pop("config_path",None)
49
- super().__init__(**kwargs)
50
-
51
-
52
- BUILDER_CONFIGS = [
53
- Mimic4DatasetConfig(
54
- name="Phenotype",
55
- version=VERSION,
56
- description="Dataset for mimic4 Phenotype task"
57
- ),
58
- Mimic4DatasetConfig(
59
- name="Readmission",
60
- version=VERSION,
61
- description="Dataset for mimic4 Readmission task"
62
- ),
63
- Mimic4DatasetConfig(
64
- name="Length of Stay",
65
- version=VERSION,
66
- description="Dataset for mimic4 Length of Stay task"
67
- ),
68
- Mimic4DatasetConfig(
69
- name="Mortality",
70
- version=VERSION,
71
- description="Dataset for mimic4 Mortality task"
72
- ),
73
- ]
74
-
75
- DEFAULT_CONFIG_NAME = "Mortality"
76
-
77
- def _info(self):
78
-
79
- features = datasets.Features(
80
- {
81
- "label": datasets.ClassLabel(names=["0", "1"]),
82
- "gender": datasets.Value("string"),
83
- "ethnicity": datasets.Value("string"),
84
- "age": datasets.Value("int32"),
85
- "COND": datasets.Sequence(datasets.Value("string")),
86
- "MEDS": {
87
- "signal":
88
- {
89
- "id": datasets.Sequence(datasets.Value("int32")),
90
- "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
91
- }
92
- ,
93
- "rate":
94
- {
95
- "id": datasets.Sequence(datasets.Value("int32")),
96
- "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
97
- }
98
- ,
99
- "amount":
100
- {
101
- "id": datasets.Sequence(datasets.Value("int32")),
102
- "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
103
- }
104
-
105
- },
106
- "PROC": {
107
- "id": datasets.Sequence(datasets.Value("int32")),
108
- "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
109
- },
110
- "CHART":
111
- {
112
- "signal" : {
113
- "id": datasets.Sequence(datasets.Value("int32")),
114
- "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
115
- },
116
- "val" : {
117
- "id": datasets.Sequence(datasets.Value("int32")),
118
- "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
119
- },
120
- },
121
- "OUT": {
122
- "id": datasets.Sequence(datasets.Value("int32")),
123
- "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
124
- },
125
-
126
- }
127
- )
128
- return datasets.DatasetInfo(
129
- description=_DESCRIPTION,
130
- features=features,
131
- homepage=_HOMEPAGE,
132
- citation=_CITATION,
133
- )
134
-
135
- def _split_generators(self, dl_manager: datasets.DownloadManager()):
136
- if self.config.name == 'Phenotype' and self.config_path is None : self.config_path = _CONFIG_URLS['phenotype']
137
- if self.config.name == 'Readmission' and self.config_path is None : self.config_path = _CONFIG_URLS['readmission']
138
- if self.config.name == 'Length of Stay' and self.config_path is None : self.config_path = _CONFIG_URLS['los']
139
- if self.config.name == 'Mortality' and self.config_path is None : self.config_path = _CONFIG_URLS['mortality']
140
-
141
- repo_url='https://github.com/healthylaife/MIMIC-IV-Data-Pipeline'
142
- if os.path.exists(os.path.dirname(os.path.abspath('MIMIC-IV-Data-Pipeline-main'))):
143
- path_bench = os.path.dirname(os.path.abspath('MIMIC-IV-Data-Pipeline-main'))+'/MIMIC-IV-Data-Pipeline-main'
144
- else:
145
- repodir = os.getcwd()
146
- path_bench = repodir+'/MIMIC-IV-Data-Pipeline-main'
147
- subprocess.run(["git", "clone", repo_url, path_bench])
148
-
149
-
150
- if self.config_path[0:4] == 'http':
151
- file_path, head = urlretrieve(self.config_path)
152
- else :
153
- file_path = self.config_path
154
-
155
- conf=path_bench+'/config/'+file_path.split('/')[-1]
156
- print(conf)
157
-
158
- if not os.path.exists(conf):
159
- shutil.move(file_path, path_bench+'/config')
160
-
161
-
162
- if not os.path.exists(path_bench+'/model/data_generation_icu_modify.py'):
163
- file_path, head = urlretrieve(_DATA_GEN, "data_generation_icu_modify.py")
164
- shutil.move(file_path, path_bench+'/model')
165
-
166
- if not os.path.exists(path_bench+'/cohort.py'):
167
- file_path, head = urlretrieve(_COHORT, "cohort.py")
168
- shutil.move(file_path, path_bench)
169
-
170
- data_dir = path_bench + "/data/dataDic"
171
- sys.path.append(path_bench)
172
- config = self.config_path.split('/')[-1]
173
- script = 'python cohort.py '+ self.config.name +" "+ self.mimic_path+ " "+path_bench+ " "+config
174
- #print(script)
175
- #os.system(script)
176
- return [
177
- datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_dir}),
178
- ]
179
-
180
-
181
- def _generate_examples(self, filepath):
182
- with open(filepath, 'rb') as fp:
183
- dataDic = pickle.load(fp)
184
- for hid, data in dataDic.items():
185
- proc_features = data['Proc']
186
- chart_features = data['Chart']
187
- meds_features = data['Med']
188
- out_features = data['Out']
189
- cond_features = data['Cond']['fids']
190
- eth= data['ethnicity']
191
- age = data['age']
192
- gender = data['gender']
193
- label = data['label']
194
-
195
- items = list(proc_features.keys())
196
- values =[proc_features[i] for i in items ]
197
- procs = {"id" : items,
198
- "value": values}
199
-
200
- items_outs = list(out_features.keys())
201
- values_outs =[out_features[i] for i in items_outs ]
202
- outs = {"id" : items_outs,
203
- "value": values_outs}
204
-
205
- #chart signal
206
- if ('signal' in chart_features):
207
- items_chart_sig = list(chart_features['signal'].keys())
208
- values_chart_sig =[chart_features['signal'][i] for i in items_chart_sig ]
209
- chart_sig = {"id" : items_chart_sig,
210
- "value": values_chart_sig}
211
- else:
212
- chart_sig = {"id" : [],
213
- "value": []}
214
- #chart val
215
- if ('val' in chart_features):
216
- items_chart_val = list(chart_features['val'].keys())
217
- values_chart_val =[chart_features['val'][i] for i in items_chart_val ]
218
- chart_val = {"id" : items_chart_val,
219
- "value": values_chart_val}
220
- else:
221
- chart_val = {"id" : [],
222
- "value": []}
223
-
224
- charts = {"signal" : chart_sig,
225
- "val" : chart_val}
226
-
227
- #meds signal
228
- if ('signal' in meds_features):
229
- items_meds_sig = list(meds_features['signal'].keys())
230
- values_meds_sig =[meds_features['signal'][i] for i in items_meds_sig ]
231
- meds_sig = {"id" : items_meds_sig,
232
- "value": values_meds_sig}
233
- else:
234
- meds_sig = {"id" : [],
235
- "value": []}
236
- #meds rate
237
- if ('rate' in meds_features):
238
- items_meds_rate = list(meds_features['rate'].keys())
239
- values_meds_rate =[meds_features['rate'][i] for i in items_meds_rate ]
240
- meds_rate = {"id" : items_meds_rate,
241
- "value": values_meds_rate}
242
- else:
243
- meds_rate = {"id" : [],
244
- "value": []}
245
- #meds amount
246
- if ('amount' in meds_features):
247
- items_meds_amount = list(meds_features['amount'].keys())
248
- values_meds_amount =[meds_features['amount'][i] for i in items_meds_amount ]
249
- meds_amount = {"id" : items_meds_amount,
250
- "value": values_meds_amount}
251
- else:
252
- meds_amount = {"id" : [],
253
- "value": []}
254
-
255
- meds = {"signal" : meds_sig,
256
- "rate" : meds_rate,
257
- "amount" : meds_amount}
258
-
259
- yield int(hid), {
260
- "label" : label,
261
- "gender" : gender,
262
- "ethnicity" : eth,
263
- "age" : age,
264
- "COND" : cond_features,
265
- "PROC" : procs,
266
- "CHART" : charts,
267
- "OUT" : outs,
268
- "MEDS" : meds
269
- }
270
-