from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") DESCRIPTION = "Arhythmia dataset from the UCI ML repository." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/5/arrhythmia" _URLS = ("https://archive-beta.ics.uci.edu/dataset/5/arrhythmia") _CITATION = """ @misc{misc_arrhythmia_5, author = {Guvenir,H., Acar,Burak & Muderrisoglu,Haldun}, title = {{Arrhythmia}}, year = {1998}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C5BS32}} }""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/arhythmia/raw/main/arrhythmia.data" } features_types_per_config = { "arhythmia": { "age": datasets.Value("int8"), "is_female": datasets.Value("bool"), "height": datasets.Value("float64"), "weight": datasets.Value("float64"), "qrs_duration": datasets.Value("float64"), "p_r_interval": datasets.Value("float64"), "q_t_interval": datasets.Value("float64"), "t_interval": datasets.Value("float64"), "p_interval": datasets.Value("float64"), "qrs": datasets.Value("float64"), "t": datasets.Value("float64"), "p": datasets.Value("float64"), "qrst": datasets.Value("float64"), "j": datasets.Value("float64"), "heart_rate": datasets.Value("float64"), "average_witdth_of_wave_q_on_DI": datasets.Value("float64"), "average_witdth_of_wave_r_on_DI": datasets.Value("float64"), "average_witdth_of_wave_s_on_DI": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_DI": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_DI": datasets.Value("float64"), "number_of_deflections_on_DI": datasets.Value("float64"), "type_of_ragged_r_wave_on_DI": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_DI": datasets.Value("float64"), "type_of_ragged_p_wave_on_DI": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_DI": datasets.Value("float64"), "type_of_ragged_t_wave_on_DI": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_DI": datasets.Value("float64"), "average_witdth_of_wave_q_on_DII": datasets.Value("float64"), "average_witdth_of_wave_r_on_DII": datasets.Value("float64"), "average_witdth_of_wave_s_on_DII": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_DII": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_DII": datasets.Value("float64"), "number_of_deflections_on_DII": datasets.Value("float64"), "type_of_ragged_r_wave_on_DII": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_DII": datasets.Value("float64"), "type_of_ragged_p_wave_on_DII": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_DII": datasets.Value("float64"), "type_of_ragged_t_wave_on_DII": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_DII": datasets.Value("float64"), "average_witdth_of_wave_q_on_DIII": datasets.Value("float64"), "average_witdth_of_wave_r_on_DIII": datasets.Value("float64"), "average_witdth_of_wave_s_on_DIII": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_DIII": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_DIII": datasets.Value("float64"), "number_of_deflections_on_DIII": datasets.Value("float64"), "type_of_ragged_r_wave_on_DIII": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_DIII": datasets.Value("float64"), "type_of_ragged_p_wave_on_DIII": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_DIII": datasets.Value("float64"), "type_of_ragged_t_wave_on_DIII": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_DIII": datasets.Value("float64"), "average_witdth_of_wave_q_on_AVR": datasets.Value("float64"), "average_witdth_of_wave_r_on_AVR": datasets.Value("float64"), "average_witdth_of_wave_s_on_AVR": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_AVR": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_AVR": datasets.Value("float64"), "number_of_deflections_on_AVR": datasets.Value("float64"), "type_of_ragged_r_wave_on_AVR": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_AVR": datasets.Value("float64"), "type_of_ragged_p_wave_on_AVR": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_AVR": datasets.Value("float64"), "type_of_ragged_t_wave_on_AVR": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_AVR": datasets.Value("float64"), "average_witdth_of_wave_q_on_AVL": datasets.Value("float64"), "average_witdth_of_wave_r_on_AVL": datasets.Value("float64"), "average_witdth_of_wave_s_on_AVL": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_AVL": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_AVL": datasets.Value("float64"), "number_of_deflections_on_AVL": datasets.Value("float64"), "type_of_ragged_r_wave_on_AVL": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_AVL": datasets.Value("float64"), "type_of_ragged_p_wave_on_AVL": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_AVL": datasets.Value("float64"), "type_of_ragged_t_wave_on_AVL": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_AVL": datasets.Value("float64"), "average_witdth_of_wave_q_on_AVF": datasets.Value("float64"), "average_witdth_of_wave_r_on_AVF": datasets.Value("float64"), "average_witdth_of_wave_s_on_AVF": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_AVF": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_AVF": datasets.Value("float64"), "number_of_deflections_on_AVF": datasets.Value("float64"), "type_of_ragged_r_wave_on_AVF": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_AVF": datasets.Value("float64"), "type_of_ragged_p_wave_on_AVF": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_AVF": datasets.Value("float64"), "type_of_ragged_t_wave_on_AVF": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_AVF": datasets.Value("float64"), "average_witdth_of_wave_q_on_V1": datasets.Value("float64"), "average_witdth_of_wave_r_on_V1": datasets.Value("float64"), "average_witdth_of_wave_s_on_V1": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_V1": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_V1": datasets.Value("float64"), "number_of_deflections_on_V1": datasets.Value("float64"), "type_of_ragged_r_wave_on_V1": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_V1": datasets.Value("float64"), "type_of_ragged_p_wave_on_V1": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_V1": datasets.Value("float64"), "type_of_ragged_t_wave_on_V1": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_V1": datasets.Value("float64"), "average_witdth_of_wave_q_on_V2": datasets.Value("float64"), "average_witdth_of_wave_r_on_V2": datasets.Value("float64"), "average_witdth_of_wave_s_on_V2": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_V2": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_V2": datasets.Value("float64"), "number_of_deflections_on_V2": datasets.Value("float64"), "type_of_ragged_r_wave_on_V2": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_V2": datasets.Value("float64"), "type_of_ragged_p_wave_on_V2": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_V2": datasets.Value("float64"), "type_of_ragged_t_wave_on_V2": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_V2": datasets.Value("float64"), "average_witdth_of_wave_q_on_V3": datasets.Value("float64"), "average_witdth_of_wave_r_on_V3": datasets.Value("float64"), "average_witdth_of_wave_s_on_V3": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_V3": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_V3": datasets.Value("float64"), "number_of_deflections_on_V3": datasets.Value("float64"), "type_of_ragged_r_wave_on_V3": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_V3": datasets.Value("float64"), "type_of_ragged_p_wave_on_V3": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_V3": datasets.Value("float64"), "type_of_ragged_t_wave_on_V3": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_V3": datasets.Value("float64"), "average_witdth_of_wave_q_on_V4": datasets.Value("float64"), "average_witdth_of_wave_r_on_V4": datasets.Value("float64"), "average_witdth_of_wave_s_on_V4": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_V4": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_V4": datasets.Value("float64"), "number_of_deflections_on_V4": datasets.Value("float64"), "type_of_ragged_r_wave_on_V4": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_V4": datasets.Value("float64"), "type_of_ragged_p_wave_on_V4": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_V4": datasets.Value("float64"), "type_of_ragged_t_wave_on_V4": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_V4": datasets.Value("float64"), "average_witdth_of_wave_q_on_V5": datasets.Value("float64"), "average_witdth_of_wave_r_on_V5": datasets.Value("float64"), "average_witdth_of_wave_s_on_V5": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_V5": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_V5": datasets.Value("float64"), "number_of_deflections_on_V5": datasets.Value("float64"), "type_of_ragged_r_wave_on_V5": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_V5": datasets.Value("float64"), "type_of_ragged_p_wave_on_V5": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_V5": datasets.Value("float64"), "type_of_ragged_t_wave_on_V5": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_V5": datasets.Value("float64"), "average_witdth_of_wave_q_on_V6": datasets.Value("float64"), "average_witdth_of_wave_r_on_V6": datasets.Value("float64"), "average_witdth_of_wave_s_on_V6": datasets.Value("float64"), "average_witdth_of_wave_rprime_on_V6": datasets.Value("float64"), "average_witdth_of_wave_sprime_on_V6": datasets.Value("float64"), "number_of_deflections_on_V6": datasets.Value("float64"), "type_of_ragged_r_wave_on_V6": datasets.Value("float64"), "type_of_diphasic_derivation_of_r_wave_on_V6": datasets.Value("float64"), "type_of_ragged_p_wave_on_V6": datasets.Value("float64"), "type_of_diphasic_derivation_of_p_wave_on_V6": datasets.Value("float64"), "type_of_ragged_t_wave_on_V6": datasets.Value("float64"), "type_of_diphasic_derivation_of_t_wave_on_V6": datasets.Value("float64"), "amplitude_of_jj_wave_on_DI": datasets.Value("float64"), "amplitude_of_q_wave_on_DI": datasets.Value("float64"), "amplitude_of_r_wave_on_DI": datasets.Value("float64"), "amplitude_of_s_wave_on_DI": datasets.Value("float64"), "amplitude_of_rprime_wave_on_DI": datasets.Value("float64"), "amplitude_of_sprime_wave_on_DI": datasets.Value("float64"), "amplitude_of_p_wave_on_DI": datasets.Value("float64"), "amplitude_of_t_wave_on_DI": datasets.Value("float64"), "qrsa_on_DI": datasets.Value("float64"), "qrsta_on_DI": datasets.Value("float64"), "amplitude_of_jj_wave_on_DII": datasets.Value("float64"), "amplitude_of_q_wave_on_DII": datasets.Value("float64"), "amplitude_of_r_wave_on_DII": datasets.Value("float64"), "amplitude_of_s_wave_on_DII": datasets.Value("float64"), "amplitude_of_rprime_wave_on_DII": datasets.Value("float64"), "amplitude_of_sprime_wave_on_DII": datasets.Value("float64"), "amplitude_of_p_wave_on_DII": datasets.Value("float64"), "amplitude_of_t_wave_on_DII": datasets.Value("float64"), "qrsa_on_DII": datasets.Value("float64"), "qrsta_on_DII": datasets.Value("float64"), "amplitude_of_jj_wave_on_DIII": datasets.Value("float64"), "amplitude_of_q_wave_on_DIII": datasets.Value("float64"), "amplitude_of_r_wave_on_DIII": datasets.Value("float64"), "amplitude_of_s_wave_on_DIII": datasets.Value("float64"), "amplitude_of_rprime_wave_on_DIII": datasets.Value("float64"), "amplitude_of_sprime_wave_on_DIII": datasets.Value("float64"), "amplitude_of_p_wave_on_DIII": datasets.Value("float64"), "amplitude_of_t_wave_on_DIII": datasets.Value("float64"), "qrsa_on_DIII": datasets.Value("float64"), "qrsta_on_DIII": datasets.Value("float64"), "amplitude_of_jj_wave_on_AVR": datasets.Value("float64"), "amplitude_of_q_wave_on_AVR": datasets.Value("float64"), "amplitude_of_r_wave_on_AVR": datasets.Value("float64"), "amplitude_of_s_wave_on_AVR": datasets.Value("float64"), "amplitude_of_rprime_wave_on_AVR": datasets.Value("float64"), "amplitude_of_sprime_wave_on_AVR": datasets.Value("float64"), "amplitude_of_p_wave_on_AVR": datasets.Value("float64"), "amplitude_of_t_wave_on_AVR": datasets.Value("float64"), "qrsa_on_AVR": datasets.Value("float64"), "qrsta_on_AVR": datasets.Value("float64"), "amplitude_of_jj_wave_on_AVL": datasets.Value("float64"), "amplitude_of_q_wave_on_AVL": datasets.Value("float64"), "amplitude_of_r_wave_on_AVL": datasets.Value("float64"), "amplitude_of_s_wave_on_AVL": datasets.Value("float64"), "amplitude_of_rprime_wave_on_AVL": datasets.Value("float64"), "amplitude_of_sprime_wave_on_AVL": datasets.Value("float64"), "amplitude_of_p_wave_on_AVL": datasets.Value("float64"), "amplitude_of_t_wave_on_AVL": datasets.Value("float64"), "qrsa_on_AVL": datasets.Value("float64"), "qrsta_on_AVL": datasets.Value("float64"), "amplitude_of_jj_wave_on_AVF": datasets.Value("float64"), "amplitude_of_q_wave_on_AVF": datasets.Value("float64"), "amplitude_of_r_wave_on_AVF": datasets.Value("float64"), "amplitude_of_s_wave_on_AVF": datasets.Value("float64"), "amplitude_of_rprime_wave_on_AVF": datasets.Value("float64"), "amplitude_of_sprime_wave_on_AVF": datasets.Value("float64"), "amplitude_of_p_wave_on_AVF": datasets.Value("float64"), "amplitude_of_t_wave_on_AVF": datasets.Value("float64"), "qrsa_on_AVF": datasets.Value("float64"), "qrsta_on_AVF": datasets.Value("float64"), "amplitude_of_jj_wave_on_V1": datasets.Value("float64"), "amplitude_of_q_wave_on_V1": datasets.Value("float64"), "amplitude_of_r_wave_on_V1": datasets.Value("float64"), "amplitude_of_s_wave_on_V1": datasets.Value("float64"), "amplitude_of_rprime_wave_on_V1": datasets.Value("float64"), "amplitude_of_sprime_wave_on_V1": datasets.Value("float64"), "amplitude_of_p_wave_on_V1": datasets.Value("float64"), "amplitude_of_t_wave_on_V1": datasets.Value("float64"), "qrsa_on_V1": datasets.Value("float64"), "qrsta_on_V1": datasets.Value("float64"), "amplitude_of_jj_wave_on_V2": datasets.Value("float64"), "amplitude_of_q_wave_on_V2": datasets.Value("float64"), "amplitude_of_r_wave_on_V2": datasets.Value("float64"), "amplitude_of_s_wave_on_V2": datasets.Value("float64"), "amplitude_of_rprime_wave_on_V2": datasets.Value("float64"), "amplitude_of_sprime_wave_on_V2": datasets.Value("float64"), "amplitude_of_p_wave_on_V2": datasets.Value("float64"), "amplitude_of_t_wave_on_V2": datasets.Value("float64"), "qrsa_on_V2": datasets.Value("float64"), "qrsta_on_V2": datasets.Value("float64"), "amplitude_of_jj_wave_on_V3": datasets.Value("float64"), "amplitude_of_q_wave_on_V3": datasets.Value("float64"), "amplitude_of_r_wave_on_V3": datasets.Value("float64"), "amplitude_of_s_wave_on_V3": datasets.Value("float64"), "amplitude_of_rprime_wave_on_V3": datasets.Value("float64"), "amplitude_of_sprime_wave_on_V3": datasets.Value("float64"), "amplitude_of_p_wave_on_V3": datasets.Value("float64"), "amplitude_of_t_wave_on_V3": datasets.Value("float64"), "qrsa_on_V3": datasets.Value("float64"), "qrsta_on_V3": datasets.Value("float64"), "amplitude_of_jj_wave_on_V4": datasets.Value("float64"), "amplitude_of_q_wave_on_V4": datasets.Value("float64"), "amplitude_of_r_wave_on_V4": datasets.Value("float64"), "amplitude_of_s_wave_on_V4": datasets.Value("float64"), "amplitude_of_rprime_wave_on_V4": datasets.Value("float64"), "amplitude_of_sprime_wave_on_V4": datasets.Value("float64"), "amplitude_of_p_wave_on_V4": datasets.Value("float64"), "amplitude_of_t_wave_on_V4": datasets.Value("float64"), "qrsa_on_V4": datasets.Value("float64"), "qrsta_on_V4": datasets.Value("float64"), "amplitude_of_jj_wave_on_V5": datasets.Value("float64"), "amplitude_of_q_wave_on_V5": datasets.Value("float64"), "amplitude_of_r_wave_on_V5": datasets.Value("float64"), "amplitude_of_s_wave_on_V5": datasets.Value("float64"), "amplitude_of_rprime_wave_on_V5": datasets.Value("float64"), "amplitude_of_sprime_wave_on_V5": datasets.Value("float64"), "amplitude_of_p_wave_on_V5": datasets.Value("float64"), "amplitude_of_t_wave_on_V5": datasets.Value("float64"), "qrsa_on_V5": datasets.Value("float64"), "qrsta_on_V5": datasets.Value("float64"), "amplitude_of_jj_wave_on_V6": datasets.Value("float64"), "amplitude_of_q_wave_on_V6": datasets.Value("float64"), "amplitude_of_r_wave_on_V6": datasets.Value("float64"), "amplitude_of_s_wave_on_V6": datasets.Value("float64"), "amplitude_of_rprime_wave_on_V6": datasets.Value("float64"), "amplitude_of_sprime_wave_on_V6": datasets.Value("float64"), "amplitude_of_p_wave_on_V6": datasets.Value("float64"), "amplitude_of_t_wave_on_V6": datasets.Value("float64"), "qrsa_on_V6": datasets.Value("float64"), "qrsta_on_V6": datasets.Value("float64"), "type_of_arhythmia": datasets.ClassLabel(num_classes=13, names=["no_arhythmia"] + [f"arhythmia_of_type_{f}" for f in range(11)] + ["unknown_arhythmia"]) } } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class ArrhythmiaConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(ArrhythmiaConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Arrhythmia(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "arhythmia" BUILDER_CONFIGS = [ ArrhythmiaConfig(name="arhythmia", description="Multiclass classification of arhythmia type."), ] 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["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 = DEFAULT_CONFIG) -> pandas.DataFrame: data = data[(data.t != "?") & (data.qrst != "?") & (data.p != "?") & (data.j != "?") & (data.heart_rate != "?")] data = data.astype({"t": int, "qrst": int, "p": int, "j": int, "heart_rate": int, "is_female": bool}) if "type_of_arrhythmia" in data.columns: data = data.rename(columns={"type_of_arrhythmia": "type_of_arhythmia"}) label_encoding = { 1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, 9: 8, 10: 9, 14: 10, 15: 11, 16: 12, } data.loc[:, "type_of_arhythmia"] = data["type_of_arhythmia"].apply(lambda x: label_encoding[x]) return data[list(features_types_per_config[config].keys())]