Mimic4Dataset / Mimic4Dataset.py
thbndi's picture
Update Mimic4Dataset.py
1189a30
raw
history blame
7.11 kB
import csv
import json
import os
import pandas as pd
import datasets
_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'
#TODO ADD DESCRIPTION COHORTS
"""
_HOMEPAGE = "https://huggingface.co/datasets/thbndi/Mimic4Dataset"
_CITATION = "https://proceedings.mlr.press/v193/gupta22a.html"
class Mimic4Dataset(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="Phenotype",
version=VERSION,
data_dir=os.path.abspath("./data/csv/Phenotype"),
description="Dataset for mimic4 Phenotype task",
),
datasets.BuilderConfig(
name="Readmission",
version=VERSION,
data_dir=os.path.abspath("./data/csv/Readmission"),
description="Dataset for mimic4 Readmission task",
),
datasets.BuilderConfig(
name="Length of Stay",
version=VERSION,
data_dir=os.path.abspath("./data/csv/Lenght_of_Stay"),
description="Dataset for mimic4 Length of Stay task",
),
datasets.BuilderConfig(
name="Mortality",
version=VERSION,
data_dir=os.path.abspath("./data/csv/Mortality"),
description="Dataset for mimic4 Mortality task",
),
]
DEFAULT_CONFIG_NAME = "Mortality"
def _info(self):
features = datasets.Features(
{
"gender": datasets.Value("string"),
"ethnicity": datasets.Value("string"),
"insurance": datasets.Value("string"),
"age": datasets.Value("int32"),
"COND": datasets.Sequence(
{
"cond" : datasets.Value("string"),
"value" : datasets.Value("int32")
}),
"MEDS": datasets.Sequence(
{
"meds" : datasets.Value("string"),
"value" : datasets.Value("int32")
}),
"PROC": datasets.Sequence(
{
"proc" : datasets.Value("string"),
"value" : datasets.Value("int32")
}),
"CHART": datasets.Sequence(
{
"chart" : datasets.Value("string"),
"value" : datasets.Value("int32")
}),
"OUT": datasets.Sequence(
{
"out" : datasets.Value("string"),
"value" : datasets.Value("int32")
}),
"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
# Collect file paths for all CSV files in the subfolders
train_files = []
for split_name in os.listdir(data_dir):
split_dir = os.path.join(data_dir, split_name)
if os.path.isdir(split_dir):
for file_name in os.listdir(split_dir):
if file_name.endswith(".csv"):
file_path = os.path.join(split_dir, file_name)
train_files.append(file_path)
# Return a single SplitGenerator for the train split
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"filepaths": train_files,
"split": datasets.Split.TRAIN,
},
)
]
def _generate_examples(self, filepaths, split):
#each 3 successive files are the same admission (demographic, static, dynamic)
labels = pd.read_csv("./data/csv/"+self.config.name +"labels.csv")
labels_dict = dict(zip(labels['stay_id'], labels['label']))
for i in range(0, len(filepaths), 3):
file1, file2, file3 = filepaths[i:i+3]
static_file = file1 if "static.csv" in file1 else file2 if "static.csv" in file2 else file3
demographic_file = file1 if "demo.csv" in file1 else file2 if "demo.csv" in file2 else file3
dynamic_file = file1 if "dynamic.csv" in file1 else file2 if "dynamic.csv" in file2 else file3
#dynamic
dyn = pd.read_csv(dynamic_file, header=[0, 1])
meds = dyn['MEDS']
proc = dyn['PROC']
chart = dyn['CHART']
out = dyn['OUT']
#static
stat = pd.read_csv(static_file, header=[0, 1])
stat = stat['COND']
#demo
demo = pd.read_csv(demographic_file, header=0)
#dict
stat_dict = stat.iloc[0].to_dict()
demo_dict = demo.iloc[0].to_dict()
meds_dict = meds.iloc[0].to_dict()
proc_dict = proc.iloc[0].to_dict()
chart_dict = chart.iloc[0].to_dict()
out_dict = out.iloc[0].to_dict()
stat2 = {'cond': list(stat_dict.keys()), 'value': list(stat_dict.values())}
meds = {'meds': list(meds_dict.keys()), 'value': list(meds_dict.values())}
proc = {'proc': list(proc_dict.keys()), 'value': list(proc_dict.values())}
chart = {'chart': list(chart_dict.keys()), 'value': list(chart_dict.values())}
out = {'out': list(out_dict.keys()), 'value': list(out_dict.values())}
#get stay_id which is the name of the folder containing the files
stay_id = int(demographic_file.split("/")[-2])
#get the label
label = int(labels_dict[stay_id])
yield stay_id, {
"gender" : demo_dict['gender'],
"ethnicity" : demo_dict['ethnicity'],
"insurance" : demo_dict['insurance'],
"age" : demo_dict['Age'],
"MEDS" : {
"meds" : stat2['cond'],
"value" : stat2['value']
},
"PROC" : {
"proc" : stat2['cond'],
"value" : stat2['value']
},
"CHART" : {
"chart" : stat2['cond'],
"value" : stat2['value']
},
"OUT" : {
"out" : stat2['cond'],
"value" : stat2['value']
},
"COND" : {
"cond" : stat2['cond'],
"value" : stat2['value']
},
"label" : label
}