"""P53 Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ENCODING_DICS = { "class": { "inactive": 0, "active": 1 } } DESCRIPTION = "P53 dataset." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/170/p53" _URLS = ("https://archive-beta.ics.uci.edu/dataset/170/p53") _CITATION = """ @misc{misc_p53_mutants_188, author = {Lathrop,Richard}, title = {{p53 Mutants}}, year = {2010}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C5T89H}} } """ # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/p53/resolve/main/p53.data" } features_types_per_config = { "p53": {f"feature_{i}": datasets.Value("float64") for i in range(5408)} } features_types_per_config["p53"]["class"] = datasets.ClassLabel(num_classes=2) features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class P53Config(datasets.BuilderConfig): def __init__(self, **kwargs): super(P53Config, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class P53(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "p53" BUILDER_CONFIGS = [ P53Config(name="p53", description="P53 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, header=None, nrows=100) data.columns = [f"feature_{i}" for i in range(5408)] + ["class"] print("preprocessing..") data = self.preprocess(data) print("preprocessed!\n\n\n\n") for row_id, row in data.iterrows(): data_row = dict(row) yield row_id, data_row def preprocess(self, data: pandas.DataFrame) -> pandas.DataFrame: for feature in _ENCODING_DICS: print(f"encoding {feature}\n\n\n") encoding_function = partial(self.encode, feature) data.loc[:, feature] = data[feature].apply(encoding_function) for feature in data.columns: if feature == "class": break data.loc[data[feature] == "?", feature] = data[data[feature] != "?"].astype(float).mean() 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}")