File size: 5,231 Bytes
8b8ed7d
 
 
 
 
2905dfe
 
8b8ed7d
 
 
 
 
2905dfe
8b8ed7d
 
 
 
2905dfe
8b8ed7d
08904b5
 
 
2905dfe
 
dd1c84c
2905dfe
 
 
f0830d9
dd1c84c
2905dfe
 
08904b5
2905dfe
08904b5
 
8b8ed7d
08904b5
8b8ed7d
 
54e004e
a2455ce
08904b5
8b8ed7d
08904b5
8b8ed7d
 
2905dfe
8b8ed7d
e23ced1
8b8ed7d
08904b5
8b8ed7d
 
2905dfe
8b8ed7d
e23ced1
8b8ed7d
08904b5
8b8ed7d
 
2905dfe
8b8ed7d
e23ced1
8b8ed7d
 
 
 
 
 
 
 
 
 
 
 
 
2905dfe
1189a30
 
2905dfe
 
 
1189a30
 
2905dfe
 
1189a30
 
2905dfe
 
1189a30
 
2905dfe
 
8b8ed7d
 
 
 
 
 
 
 
2905dfe
8b8ed7d
 
 
2905dfe
08904b5
8b8ed7d
2905dfe
8b8ed7d
 
 
2905dfe
 
 
 
 
 
 
 
 
 
 
 
 
1189a30
2905dfe
 
 
 
1189a30
2905dfe
 
 
1189a30
2905dfe
1189a30
2905dfe
 
b4ff457
2905dfe
 
8b8ed7d
 
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
import csv
import json
import os
import pandas as pd
import datasets
import pickle
#import cohort

_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 : 
"""

_HOMEPAGE = "https://huggingface.co/datasets/thbndi/Mimic4Dataset"
_CITATION = "https://proceedings.mlr.press/v193/gupta22a.html"
_GITHUB = "https://github.com/healthylaife/MIMIC-IV-Data-Pipeline/tree/main"

class Mimic4DatasetConfig(datasets.BuilderConfig):
    """BuilderConfig for Mimic4Dataset."""

    def __init__(
        self,
        mimic_path,
        #config,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self.mimic_path =mimic_path
        #self.config = config
        #cohort.task_cohort(self.task,self.mimic_path)

        
class Mimic4Dataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")
    BUILDER_CONFIGS = [
        Mimic4DatasetConfig(
            name="Phenotype",
            version=VERSION,
            data_dir=os.path.abspath("./data/dict/cohort_icu_readmission_30_I50"),
            description="Dataset for mimic4 Phenotype task",
            mimic_path = None
        ),
        Mimic4DatasetConfig(
            name="Readmission",
            version=VERSION,
            data_dir=os.path.abspath("./data/dict"),
            description="Dataset for mimic4 Readmission task",
            mimic_path = None
        ),
        Mimic4DatasetConfig(
            name="Length of Stay",
            version=VERSION,
            data_dir=os.path.abspath("./data/dict"),
            description="Dataset for mimic4 Length of Stay task",
            mimic_path = None
        ),
        Mimic4DatasetConfig(
            name="Mortality",
            version=VERSION,
            data_dir=os.path.abspath("./data/dict"),
            description="Dataset for mimic4 Mortality task",
            mimic_path = None
        ),
    ]

    DEFAULT_CONFIG_NAME = "Mortality"

    def _info(self):
        

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

    def _split_generators(self, dl_manager):
        data_dir = self.config.data_dir + "/dataDic"
        mimic=self.mimic_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']

            yield hid, {
                "gender" : gender,
                "ethnicity" : eth,
                "age" : age,
                "MEDS" : {
                    "signal" : meds_features['signal'],
                    "rate" : meds_features['rate'],
                    "amount" : meds_features['amount']
                },
                "PROC" : proc_features,
                "CHART" : {
                    "signal" : chart_features['signal'],
                    "val" : chart_features['val']
                },
                "OUT" : out_features,
                "COND" : cond_features,
                "label" : label
            }