Mimic4Dataset / Mimic4Dataset.py
thbndi's picture
Update Mimic4Dataset.py
1410ca4
raw
history blame
7.88 kB
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": {
"signal": datasets.Sequence(
{
"id": datasets.Sequence(datasets.Value("int32")),
"value": datasets.Sequence(datasets.Value("float32"))
}
),
"rate": datasets.Sequence(
{
"id": datasets.Value("int32"),
"value": datasets.Sequence(datasets.Value("float32"))
}
),
"amount": datasets.Sequence(
{
"id": datasets.Value("int32"),
"value": 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")))
},
"label": datasets.ClassLabel(names=["0", "1"]),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
citation=_CITATION,
)
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']
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
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}
#chart val
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}
charts = {"signal" : chart_sig,
"val" : chart_val}
#meds signal
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}
#meds rate
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}
#meds amount
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}
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
}