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(datasets.Value("int32"), length=None), "MEDS": datasets.Sequence(datasets.Value("int32"), length=None), "PROC": datasets.Sequence(datasets.Value("int32"), length=None), "CHART": datasets.Sequence(datasets.Value("int32"), length=None), "OUT": datasets.Sequence(datasets.Value("int32"), length=None), "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") 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() #get stay_id which is the name of the folder containing the files stay_id = demographic_file.split("/")[-2] #get the label label = int(labels.loc[labels['stay_id'] == stay_id]['label']) yield stay_id, { "gender" : demo_dict['gender'], "ethnicity" : demo_dict['ethnicity'], "insurance" : demo_dict['insurance'], "age" : demo_dict['age'], "MEDS" : meds_dict, "PROC" : proc_dict, "CHART" : chart_dict, "OUT" : out_dict, "COND" : stat_dict, "label" : label }