|
"""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}} |
|
}""" |
|
|
|
|
|
urls_per_split = { |
|
"hungary": {"train": "https://huggingface.co/datasets/mstz/heart/raw/main/processed.hungarian.data"}, |
|
} |
|
features_types_per_config = { |
|
"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"), |
|
"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} |
|
|
|
_ENCODING_DICS = { |
|
"type_of_chest_pain": { |
|
1: "typical angina", |
|
2: "atypical angina", |
|
3: "non-anginal pain", |
|
4: "asymptomatic" |
|
} |
|
} |
|
|
|
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): |
|
|
|
DEFAULT_CONFIG = "hungary" |
|
BUILDER_CONFIGS = [ |
|
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) |
|
|
|
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, header=None) |
|
data.columns = _BASE_FEATURE_NAMES |
|
data = self.preprocess(data, self.config.name) |
|
|
|
for row_id, row in data.iterrows(): |
|
data_row = dict(row) |
|
|
|
yield row_id, data_row |
|
|
|
def preprocess(self, data, config): |
|
for feature in _ENCODING_DICS: |
|
encoding_function = partial(self.encode, feature) |
|
data.loc[:, feature] = data[feature].apply(encoding_function) |
|
|
|
data[["age"]].applymap(int) |
|
|
|
data.drop("slope_of_peak_exercise", axis="columns", inplace=True) |
|
data.drop("number_of_major_vessels_colored_by_flourosopy", axis="columns", inplace=True) |
|
data.drop("thal", axis="columns", inplace=True) |
|
data = data[data.serum_cholesterol != "?"] |
|
|
|
data = data.infer_objects() |
|
|
|
data = data[data.resting_blood_pressure != "?"] |
|
data = data[data.fasting_blood_sugar != "?"] |
|
data = data[data.rest_electrocardiographic_type != "?"] |
|
data = data[data.maximum_heart_rate != "?"] |
|
data = data[data.has_exercise_induced_angina != "?"] |
|
|
|
data = data.astype({"is_male": bool, "has_exercise_induced_angina": bool, |
|
"serum_cholesterol": float, "maximum_heart_rate": float, |
|
"resting_blood_pressure": float, "fasting_blood_sugar": float}) |
|
|
|
return data |
|
|
|
def encode(self, feature, value): |
|
if feature in _ENCODING_DICS: |
|
return _ENCODING_DICS[feature][value] |
|
raise ValueError(f"Unknown feature: {feature}") |
|
|