"""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) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloads[self.config.name][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