File size: 3,685 Bytes
cc4b97d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
818a273
cc4b97d
818a273
cc4b97d
818a273
cc4b97d
 
 
818a273
 
7abcdc1
cc4b97d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
818a273
 
cc4b97d
818a273
 
cc4b97d
818a273
cc4b97d
 
 
9326b2e
818a273
 
4e6c55b
 
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
"""Heart Failure Dataset"""

from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_BASE_FEATURE_NAMES = [
    "age",
    "has_anaemia",
    "creatinine_phosphokinase_concentration_in_blood",
    "has_diabetes",
    "heart_ejection_fraction",
    "has_high_blood_pressure",
    "platelets_concentration_in_blood",
    "serum_creatinine_concentration_in_blood",
    "serum_sodium_concentration_in_blood",
    "sex",
    "is_smoker",
    "days_in_study",
    "is_dead"
]

DESCRIPTION = "Heart Failure dataset."
_HOMEPAGE = "https://www.kaggle.com/datasets/ulrikthygepedersen/heart_failures"
_URLS = ("https://www.kaggle.com/datasets/ulrikthygepedersen/heart_failures")
_CITATION = """"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/heart_failure/raw/main/heart_failure_clinical_records_dataset.csv",
}
features_types_per_config = {
    "death": {
        "age": datasets.Value("int8"),
        "has_anaemia": datasets.Value("bool"),
        "creatinine_phosphokinase_concentration_in_blood": datasets.Value("float64"),
        "has_diabetes": datasets.Value("bool"),
        "heart_ejection_fraction": datasets.Value("float64"),
        "has_high_blood_pressure": datasets.Value("bool"),
        "platelets_concentration_in_blood": datasets.Value("float64"),
        "serum_creatinine_concentration_in_blood": datasets.Value("float64"),
        "serum_sodium_concentration_in_blood": datasets.Value("float64"),
        "is_male": datasets.Value("bool"),
        "is_smoker": datasets.Value("bool"),
        "days_in_study": datasets.Value("int64"),
        "is_dead": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
    }
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}


class HeartFailureConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(HeartFailureConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class HeartFailure(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "death"
    BUILDER_CONFIGS = [
        HeartFailureConfig(name="death",
                           description="Binary classification, predict if the patient dies.")
    ]


    def _info(self):
        if self.config.name not in features_per_config:
            raise ValueError(f"Unknown configuration: {self.config.name}")
        
        info = datasets.DatasetInfo(description=DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE,
                                    features=features_per_config[self.config.name])

        return info
    
    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
        downloads = dl_manager.download_and_extract(urls_per_split)

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads["train"]}),
        ]
    
    def _generate_examples(self, filepath: str):
        data = pandas.read_csv(filepath)
        data = self.preprocess(data, config=self.config.name)

        for row_id, row in data.iterrows():
            data_row = dict(row)

            yield row_id, data_row

    def preprocess(self, data: pandas.DataFrame, config: str = "death") -> pandas.DataFrame:
        data.columns = _BASE_FEATURE_NAMES
        data = data.rename(columns={"sex": "is_male"})
        data = data.astype({"has_anaemia": "bool", "has_diabetes": "bool", "has_high_blood_pressure": "bool", "is_male": "bool",
                            "is_smoker": "bool"})

        return data