splice / splice.py
mstz's picture
Upload splice.py
fa7260e
"""Splice Dataset"""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_ENCODING_DICS = {}
DESCRIPTION = "Splice dataset."
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/69/molecular+biology+splice+junction+gene+sequences"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/69/molecular+biology+splice+junction+gene+sequences")
_CITATION = """
@misc{misc_molecular_biology_(splice-junction_gene_sequences)_69,
title = {{Molecular Biology (Splice-junction Gene Sequences)}},
year = {1992},
howpublished = {UCI Machine Learning Repository},
note = {{DOI}: \\url{10.24432/C5M888}}
}
"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/splice/raw/main/splice.data"
}
features_types_per_config = {
"splice": {
"position_0": datasets.Value("string"),
"position_1": datasets.Value("string"),
"position_2": datasets.Value("string"),
"position_3": datasets.Value("string"),
"position_4": datasets.Value("string"),
"position_5": datasets.Value("string"),
"position_6": datasets.Value("string"),
"position_7": datasets.Value("string"),
"position_8": datasets.Value("string"),
"position_9": datasets.Value("string"),
"position_10": datasets.Value("string"),
"position_11": datasets.Value("string"),
"position_12": datasets.Value("string"),
"position_13": datasets.Value("string"),
"position_14": datasets.Value("string"),
"position_15": datasets.Value("string"),
"position_16": datasets.Value("string"),
"position_17": datasets.Value("string"),
"position_18": datasets.Value("string"),
"position_19": datasets.Value("string"),
"position_20": datasets.Value("string"),
"position_21": datasets.Value("string"),
"position_22": datasets.Value("string"),
"position_23": datasets.Value("string"),
"position_24": datasets.Value("string"),
"position_25": datasets.Value("string"),
"position_26": datasets.Value("string"),
"position_27": datasets.Value("string"),
"position_28": datasets.Value("string"),
"position_29": datasets.Value("string"),
"position_30": datasets.Value("string"),
"position_31": datasets.Value("string"),
"position_32": datasets.Value("string"),
"position_33": datasets.Value("string"),
"position_34": datasets.Value("string"),
"position_35": datasets.Value("string"),
"position_36": datasets.Value("string"),
"position_37": datasets.Value("string"),
"position_38": datasets.Value("string"),
"position_39": datasets.Value("string"),
"position_40": datasets.Value("string"),
"position_41": datasets.Value("string"),
"position_42": datasets.Value("string"),
"position_43": datasets.Value("string"),
"position_44": datasets.Value("string"),
"position_45": datasets.Value("string"),
"position_46": datasets.Value("string"),
"position_47": datasets.Value("string"),
"position_48": datasets.Value("string"),
"position_49": datasets.Value("string"),
"position_50": datasets.Value("string"),
"position_51": datasets.Value("string"),
"position_52": datasets.Value("string"),
"position_53": datasets.Value("string"),
"position_54": datasets.Value("string"),
"position_55": datasets.Value("string"),
"position_56": datasets.Value("string"),
"position_57": datasets.Value("string"),
"position_58": datasets.Value("string"),
"position_59": datasets.Value("string"),
"class": datasets.ClassLabel(num_classes=3, names=("EI", "IE", "N"))
},
"splice_EI": {
"position_0": datasets.Value("string"),
"position_1": datasets.Value("string"),
"position_2": datasets.Value("string"),
"position_3": datasets.Value("string"),
"position_4": datasets.Value("string"),
"position_5": datasets.Value("string"),
"position_6": datasets.Value("string"),
"position_7": datasets.Value("string"),
"position_8": datasets.Value("string"),
"position_9": datasets.Value("string"),
"position_10": datasets.Value("string"),
"position_11": datasets.Value("string"),
"position_12": datasets.Value("string"),
"position_13": datasets.Value("string"),
"position_14": datasets.Value("string"),
"position_15": datasets.Value("string"),
"position_16": datasets.Value("string"),
"position_17": datasets.Value("string"),
"position_18": datasets.Value("string"),
"position_19": datasets.Value("string"),
"position_20": datasets.Value("string"),
"position_21": datasets.Value("string"),
"position_22": datasets.Value("string"),
"position_23": datasets.Value("string"),
"position_24": datasets.Value("string"),
"position_25": datasets.Value("string"),
"position_26": datasets.Value("string"),
"position_27": datasets.Value("string"),
"position_28": datasets.Value("string"),
"position_29": datasets.Value("string"),
"position_30": datasets.Value("string"),
"position_31": datasets.Value("string"),
"position_32": datasets.Value("string"),
"position_33": datasets.Value("string"),
"position_34": datasets.Value("string"),
"position_35": datasets.Value("string"),
"position_36": datasets.Value("string"),
"position_37": datasets.Value("string"),
"position_38": datasets.Value("string"),
"position_39": datasets.Value("string"),
"position_40": datasets.Value("string"),
"position_41": datasets.Value("string"),
"position_42": datasets.Value("string"),
"position_43": datasets.Value("string"),
"position_44": datasets.Value("string"),
"position_45": datasets.Value("string"),
"position_46": datasets.Value("string"),
"position_47": datasets.Value("string"),
"position_48": datasets.Value("string"),
"position_49": datasets.Value("string"),
"position_50": datasets.Value("string"),
"position_51": datasets.Value("string"),
"position_52": datasets.Value("string"),
"position_53": datasets.Value("string"),
"position_54": datasets.Value("string"),
"position_55": datasets.Value("string"),
"position_56": datasets.Value("string"),
"position_57": datasets.Value("string"),
"position_58": datasets.Value("string"),
"position_59": datasets.Value("string"),
"is_ei": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
},
"splice_IE": {
"position_0": datasets.Value("string"),
"position_1": datasets.Value("string"),
"position_2": datasets.Value("string"),
"position_3": datasets.Value("string"),
"position_4": datasets.Value("string"),
"position_5": datasets.Value("string"),
"position_6": datasets.Value("string"),
"position_7": datasets.Value("string"),
"position_8": datasets.Value("string"),
"position_9": datasets.Value("string"),
"position_10": datasets.Value("string"),
"position_11": datasets.Value("string"),
"position_12": datasets.Value("string"),
"position_13": datasets.Value("string"),
"position_14": datasets.Value("string"),
"position_15": datasets.Value("string"),
"position_16": datasets.Value("string"),
"position_17": datasets.Value("string"),
"position_18": datasets.Value("string"),
"position_19": datasets.Value("string"),
"position_20": datasets.Value("string"),
"position_21": datasets.Value("string"),
"position_22": datasets.Value("string"),
"position_23": datasets.Value("string"),
"position_24": datasets.Value("string"),
"position_25": datasets.Value("string"),
"position_26": datasets.Value("string"),
"position_27": datasets.Value("string"),
"position_28": datasets.Value("string"),
"position_29": datasets.Value("string"),
"position_30": datasets.Value("string"),
"position_31": datasets.Value("string"),
"position_32": datasets.Value("string"),
"position_33": datasets.Value("string"),
"position_34": datasets.Value("string"),
"position_35": datasets.Value("string"),
"position_36": datasets.Value("string"),
"position_37": datasets.Value("string"),
"position_38": datasets.Value("string"),
"position_39": datasets.Value("string"),
"position_40": datasets.Value("string"),
"position_41": datasets.Value("string"),
"position_42": datasets.Value("string"),
"position_43": datasets.Value("string"),
"position_44": datasets.Value("string"),
"position_45": datasets.Value("string"),
"position_46": datasets.Value("string"),
"position_47": datasets.Value("string"),
"position_48": datasets.Value("string"),
"position_49": datasets.Value("string"),
"position_50": datasets.Value("string"),
"position_51": datasets.Value("string"),
"position_52": datasets.Value("string"),
"position_53": datasets.Value("string"),
"position_54": datasets.Value("string"),
"position_55": datasets.Value("string"),
"position_56": datasets.Value("string"),
"position_57": datasets.Value("string"),
"position_58": datasets.Value("string"),
"position_59": datasets.Value("string"),
"is_ie": datasets.ClassLabel(num_classes=2, names=("no", "yes"))
},
"splice_N": {
"position_0": datasets.Value("string"),
"position_1": datasets.Value("string"),
"position_2": datasets.Value("string"),
"position_3": datasets.Value("string"),
"position_4": datasets.Value("string"),
"position_5": datasets.Value("string"),
"position_6": datasets.Value("string"),
"position_7": datasets.Value("string"),
"position_8": datasets.Value("string"),
"position_9": datasets.Value("string"),
"position_10": datasets.Value("string"),
"position_11": datasets.Value("string"),
"position_12": datasets.Value("string"),
"position_13": datasets.Value("string"),
"position_14": datasets.Value("string"),
"position_15": datasets.Value("string"),
"position_16": datasets.Value("string"),
"position_17": datasets.Value("string"),
"position_18": datasets.Value("string"),
"position_19": datasets.Value("string"),
"position_20": datasets.Value("string"),
"position_21": datasets.Value("string"),
"position_22": datasets.Value("string"),
"position_23": datasets.Value("string"),
"position_24": datasets.Value("string"),
"position_25": datasets.Value("string"),
"position_26": datasets.Value("string"),
"position_27": datasets.Value("string"),
"position_28": datasets.Value("string"),
"position_29": datasets.Value("string"),
"position_30": datasets.Value("string"),
"position_31": datasets.Value("string"),
"position_32": datasets.Value("string"),
"position_33": datasets.Value("string"),
"position_34": datasets.Value("string"),
"position_35": datasets.Value("string"),
"position_36": datasets.Value("string"),
"position_37": datasets.Value("string"),
"position_38": datasets.Value("string"),
"position_39": datasets.Value("string"),
"position_40": datasets.Value("string"),
"position_41": datasets.Value("string"),
"position_42": datasets.Value("string"),
"position_43": datasets.Value("string"),
"position_44": datasets.Value("string"),
"position_45": datasets.Value("string"),
"position_46": datasets.Value("string"),
"position_47": datasets.Value("string"),
"position_48": datasets.Value("string"),
"position_49": datasets.Value("string"),
"position_50": datasets.Value("string"),
"position_51": datasets.Value("string"),
"position_52": datasets.Value("string"),
"position_53": datasets.Value("string"),
"position_54": datasets.Value("string"),
"position_55": datasets.Value("string"),
"position_56": datasets.Value("string"),
"position_57": datasets.Value("string"),
"position_58": datasets.Value("string"),
"position_59": datasets.Value("string"),
"is_n": 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 SpliceConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(SpliceConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class Splice(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "splice1"
BUILDER_CONFIGS = [
SpliceConfig(name="splice",
description="Splice for multiclass classification."),
SpliceConfig(name="splice_IE",
description="Splice for binary classification."),
SpliceConfig(name="splice_EI",
description="Splice for binary classification."),
SpliceConfig(name="splice_N",
description="Splice for binary classification."),
]
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)
for row_id, row in data.iterrows():
data_row = dict(row)
yield row_id, data_row
def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame:
if self.config.name == "splice_IE":
data["class"] = data["class"].apply(lambda x: 1 if x == "IE" else 0)
data = data.rename(columns={"class": "is_ie"})
elif self.config.name == "splice_EI":
data["class"] = data["class"].apply(lambda x: 1 if x == "EI" else 0)
data = data.rename(columns={"class": "is_ei"})
elif self.config.name == "splice_N":
data["class"] = data["class"].apply(lambda x: 1 if x == "N" else 0)
data = data.rename(columns={"class": "is_n"})
else:
data["class"] = data["class"].apply(lambda x: {
"EI": 0,
"IE": 1,
"N": 2,
}[x])
for feature in _ENCODING_DICS:
encoding_function = partial(self.encode, feature)
data.loc[:, feature] = data[feature].apply(encoding_function)
return data[list(features_types_per_config[self.config.name].keys())]
def encode(self, feature, value):
if feature in _ENCODING_DICS:
return _ENCODING_DICS[feature][value]
raise ValueError(f"Unknown feature: {feature}")