File size: 6,825 Bytes
761febb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4303419
 
 
761febb
 
 
 
ef24fda
 
 
 
 
 
 
 
 
 
 
 
 
761febb
 
 
c4bda92
 
 
 
 
 
 
 
 
 
 
 
 
761febb
 
 
c4bda92
 
 
 
 
 
 
 
 
 
 
 
 
761febb
 
 
c4bda92
 
 
 
 
 
 
 
 
 
 
 
 
761febb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f6660f
761febb
 
8d0a9fe
761febb
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""Heart"""

from typing import List
from functools import partial

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_BASE_FEATURE_NAMES = [
    "age"
    "is_male"
    "type_of_chest_pain"
    "resting_blood_pressure"
    "serum_cholesterol"
    "fasting_blood_sugar"
    "rest_electrocardiographic_type"
    "maximum_heart_rate"
    "has_exercise_induced_angina"
    "depression_induced_by_exercise"
    "slope_of_peak_exercise"
    "number_of_major_vessels_colored_by_flourosopy"
    "thal"
    "has_hearth_disease"
]

DESCRIPTION = "Heart dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Heart"
_URLS = ("https://huggingface.co/datasets/mstz/heart/raw/heart.csv")
_CITATION = """
@misc{misc_heart_disease_45,
  author       = {Janosi,Andras, Steinbrunn,William, Pfisterer,Matthias, Detrano,Robert & M.D.,M.D.},
  title        = {{Heart Disease}},
  year         = {1988},
  howpublished = {UCI Machine Learning Repository},
  note         = {{DOI}: \\url{10.24432/C52P4X}}
}"""

# Dataset info
urls_per_split = {
    "cleveland": {"train": "https://huggingface.co/datasets/mstz/heart/raw/main/processed.cleveland.data"},
    "hungary": {"train": "https://huggingface.co/datasets/mstz/heart/raw/main/processed.hungarian.data"},
    "switzerland": {"train": "https://huggingface.co/datasets/mstz/heart/raw/main/processed.switzerland.data"},
    "va": {"train": "https://huggingface.co/datasets/mstz/heart/raw/main/processed.va.data"}
}
features_types_per_config = {
    "cleveland": {
        "age": datasets.Value("int8"),
        "is_male": datasets.Value("bool"),
        "type_of_chest_pain": datasets.Value("string"),
        "resting_blood_pressure": datasets.Value("float32"),
        "serum_cholesterol": datasets.Value("float32"),
        "fasting_blood_sugar": datasets.Value("float32"),
        "rest_electrocardiographic_type": datasets.Value("string"),
        "maximum_heart_rate": datasets.Value("float32"),
        "has_exercise_induced_angina": datasets.Value("bool"),
        "depression_induced_by_exercise": datasets.Value("float32"),
        "slope_of_peak_exercise": datasets.Value("float32"),
        "number_of_major_vessels_colored_by_flourosopy": datasets.Value("int16"),
        "thal": datasets.Value("float32"),
        "has_hearth_disease": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
    },
    "va": {
        "age": datasets.Value("int8"),
        "is_male": datasets.Value("bool"),
        "type_of_chest_pain": datasets.Value("string"),
        "resting_blood_pressure": datasets.Value("float32"),
        "serum_cholesterol": datasets.Value("float32"),
        "fasting_blood_sugar": datasets.Value("float32"),
        "rest_electrocardiographic_type": datasets.Value("string"),
        "maximum_heart_rate": datasets.Value("float32"),
        "has_exercise_induced_angina": datasets.Value("bool"),
        "depression_induced_by_exercise": datasets.Value("float32"),
        "slope_of_peak_exercise": datasets.Value("float32"),
        "number_of_major_vessels_colored_by_flourosopy": datasets.Value("int16"),
        "thal": datasets.Value("float32"),
        "has_hearth_disease": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
    },
    "switzerland": {
        "age": datasets.Value("int8"),
        "is_male": datasets.Value("bool"),
        "type_of_chest_pain": datasets.Value("string"),
        "resting_blood_pressure": datasets.Value("float32"),
        "serum_cholesterol": datasets.Value("float32"),
        "fasting_blood_sugar": datasets.Value("float32"),
        "rest_electrocardiographic_type": datasets.Value("string"),
        "maximum_heart_rate": datasets.Value("float32"),
        "has_exercise_induced_angina": datasets.Value("bool"),
        "depression_induced_by_exercise": datasets.Value("float32"),
        "slope_of_peak_exercise": datasets.Value("float32"),
        "number_of_major_vessels_colored_by_flourosopy": datasets.Value("int16"),
        "thal": datasets.Value("float32"),
        "has_hearth_disease": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
    },
    "hungary": {
        "age": datasets.Value("int8"),
        "is_male": datasets.Value("bool"),
        "type_of_chest_pain": datasets.Value("string"),
        "resting_blood_pressure": datasets.Value("float32"),
        "serum_cholesterol": datasets.Value("float32"),
        "fasting_blood_sugar": datasets.Value("float32"),
        "rest_electrocardiographic_type": datasets.Value("string"),
        "maximum_heart_rate": datasets.Value("float32"),
        "has_exercise_induced_angina": datasets.Value("bool"),
        "depression_induced_by_exercise": datasets.Value("float32"),
        "slope_of_peak_exercise": datasets.Value("float32"),
        "number_of_major_vessels_colored_by_flourosopy": datasets.Value("int16"),
        "thal": datasets.Value("float32"),
        "has_hearth_disease": 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 HeartConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(HeartConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Heart(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "cleveland"
    BUILDER_CONFIGS = [
        HeartConfig(name="cleveland",
                    description="Heart for binary classification, dataset."),
        HeartConfig(name="va",
                    description="Heart for binary classification, va dataset."),
        HeartConfig(name="switzerland",
                    description="Heart for binary classification, switzerland dataset."),
        HeartConfig(name="hungary",
                    description="Heart for binary classification, hungary dataset.")
    ]


    def _info(self):
        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)
        print(downloads)

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

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

            yield row_id, data_row