"""PageBlocks Dataset""" from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") _ENCODING_DICS = { } DESCRIPTION = "PageBlocks dataset." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/78/page+blocks+classification" _URLS = ("https://archive-beta.ics.uci.edu/dataset/78/page+blocks+classification") _CITATION = """ @misc{misc_page_blocks_classification_78, author = {Malerba,Donato}, title = {{Page Blocks Classification}}, year = {1995}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C5J590}} } """ # Dataset info urls_per_split = { "train": "https://huggingface.co./datasets/mstz/page_blocks/raw/main/page_blocks.data" } features_types_per_config = { "page_blocks": { "height": datasets.Value("float64"), "lenght": datasets.Value("float64"), "area": datasets.Value("float64"), "eccentricity": datasets.Value("float64"), "percentage_black_pixels": datasets.Value("float64"), "percentage_black_pixels_after_rlsa_and": datasets.Value("float64"), "mean_numer_of_transitions": datasets.Value("float64"), "number_of_black_pixels": datasets.Value("float64"), "number_of_black_pixels_after_rlsa": datasets.Value("float64"), "number_of_transitions": datasets.Value("int8") }, "page_blocks_binary": { "height": datasets.Value("float64"), "lenght": datasets.Value("float64"), "area": datasets.Value("float64"), "eccentricity": datasets.Value("float64"), "percentage_black_pixels": datasets.Value("float64"), "percentage_black_pixels_after_rlsa_and": datasets.Value("float64"), "mean_numer_of_transitions": datasets.Value("float64"), "number_of_black_pixels": datasets.Value("float64"), "number_of_black_pixels_after_rlsa": datasets.Value("float64"), "has_multiple_transitions": datasets.ClassLabel(num_classes=2) } } features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config} class PageBlocksConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(PageBlocksConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class PageBlocks(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "page_blocks" BUILDER_CONFIGS = [ PageBlocksConfig(name="page_blocks", description="PageBlocks for regression."), PageBlocksConfig(name="page_blocks_binary", description="PageBlocks 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 == "page_blocks_binary": data["number_of_transitions"] = data["number_of_transitions"].apply(lambda x: 1 if x > 1 else 0) data = data.rename(columns={"number_of_transitions": "has_multiple_transitions"}) for feature in _ENCODING_DICS: encoding_function = partial(self.encode, feature) data.loc[:, feature] = data[feature].apply(encoding_function) data = data.reset_index() data.drop("index", axis="columns", inplace=True) 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}")