from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ORIGINAL_FEATURE_NAMES = [ "rads", "age", "lesion_shape", "margin", "density", "is_severe" ] _BASE_FEATURE_NAMES = [ "age", "lesion_shape", "margin", "density", "is_severe" ] _ENCODING_DICS = { "lesion_shape": { "1": "round", "2": "oval", "3": "lobular", "4": "irregular", }, "margin": { "1": "circumbscribed", "2": "microlobulated", "3": "obscured", "4": "ill-defined", "5": "spiculated", }, "density": { "1": "high", "2": "iso", "3": "low", "4": "fat-containing", "5": "spiculated", } } DESCRIPTION = "Mammography dataset from the UCI ML repository." _HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Mammography" _URLS = ("https://huggingface.co/datasets/mstz/mammography/raw/mammography_masses.data") _CITATION = """ @misc{misc_mammographic_mass_161, author = {Elter,Matthias}, title = {{Mammographic Mass}}, year = {2007}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C53K6Z}} }""" # Dataset info urls_per_split = { "train": "https://huggingface.co/datasets/mstz/mammography/raw/main/mammographic_masses.data" } features_types_per_config = { "mammography": { "age": datasets.Value("int8"), "lesion_shape": datasets.Value("string"), "margin": datasets.Value("string"), "density": datasets.Value("string"), "is_severe": 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 MammographyConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(MammographyConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Mammography(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "mammography" BUILDER_CONFIGS = [ MammographyConfig(name="mammography", description="Mammography 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) data.columns = _ORIGINAL_FEATURE_NAMES data.drop("rads", axis="columns", inplace=True) data = data[data.age != "?"] data = data[data.lesion_shape != "?"] data = data[data.margin != "?"] data = data[data.density != "?"] data = data.infer_objects() for feature in _ENCODING_DICS: encoding_function = partial(self.encode, feature) data.loc[:, feature] = data[feature].apply(encoding_function) for row_id, row in data.iterrows(): data_row = dict(row) yield row_id, data_row def encode(self, feature, value): if feature in _ENCODING_DICS: return _ENCODING_DICS[feature][value] raise ValueError(f"Unknown feature: {feature}")