from typing import List from functools import partial import datasets import pandas VERSION = datasets.Version("1.0.0") DESCRIPTION = "Glass efficiency dataset from the UCI repository." _HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/242/glass+efficiency" _URLS = ("https://archive-beta.ics.uci.edu/dataset/30/glass+method+choice") _CITATION = """ @misc{misc_glass_efficiency_242, author = {Tsanas,Athanasios & Xifara,Angeliki}, title = {{Glass efficiency}}, year = {2012}, howpublished = {UCI Machine Learning Repository}, note = {{DOI}: \\url{10.24432/C51307}} }""" # Dataset info _BASE_FEATURE_NAMES = [ "refractive_index", "sodium", "magnesium", "aluminum", "silicon", "potassium", "calcium", "barium", "iron", "glass_type", ] urls_per_split = { "train": "https://huggingface.co/datasets/mstz/glass/raw/main/glass.data" } features_types_per_config = { "glass": { "refractive_index": datasets.Value("float64"), "sodium": datasets.Value("float64"), "magnesium": datasets.Value("float64"), "aluminum": datasets.Value("float64"), "silicon": datasets.Value("float64"), "potassium": datasets.Value("float64"), "calcium": datasets.Value("float64"), "barium": datasets.Value("int8"), "iron": datasets.Value("float64"), "glass_type": datasets.ClassLabel(num_classes=7, names=("windows_1", "windows_2", "vehicle_windows_1", "vehicle_windows_2", "containers", "tableware", "headlamps")) }, "windows": { "refractive_index": datasets.Value("float64"), "sodium": datasets.Value("float64"), "magnesium": datasets.Value("float64"), "aluminum": datasets.Value("float64"), "silicon": datasets.Value("float64"), "potassium": datasets.Value("float64"), "calcium": datasets.Value("float64"), "barium": datasets.Value("int8"), "iron": datasets.Value("float64"), "is_windows_glass": datasets.ClassLabel(num_classes=2, names=("no", "yes")) }, "vehicles": { "refractive_index": datasets.Value("float64"), "sodium": datasets.Value("float64"), "magnesium": datasets.Value("float64"), "aluminum": datasets.Value("float64"), "silicon": datasets.Value("float64"), "potassium": datasets.Value("float64"), "calcium": datasets.Value("float64"), "barium": datasets.Value("int8"), "iron": datasets.Value("float64"), "is_vehicle_glass": datasets.ClassLabel(num_classes=2, names=("no", "yes")) }, "containers": { "refractive_index": datasets.Value("float64"), "sodium": datasets.Value("float64"), "magnesium": datasets.Value("float64"), "aluminum": datasets.Value("float64"), "silicon": datasets.Value("float64"), "potassium": datasets.Value("float64"), "calcium": datasets.Value("float64"), "barium": datasets.Value("int8"), "iron": datasets.Value("float64"), "is_container_glass": datasets.ClassLabel(num_classes=2, names=("no", "yes")) }, "tableware": { "refractive_index": datasets.Value("float64"), "sodium": datasets.Value("float64"), "magnesium": datasets.Value("float64"), "aluminum": datasets.Value("float64"), "silicon": datasets.Value("float64"), "potassium": datasets.Value("float64"), "calcium": datasets.Value("float64"), "barium": datasets.Value("int8"), "iron": datasets.Value("float64"), "is_tableware_glass": datasets.ClassLabel(num_classes=2, names=("no", "yes")) }, "headlamps": { "refractive_index": datasets.Value("float64"), "sodium": datasets.Value("float64"), "magnesium": datasets.Value("float64"), "aluminum": datasets.Value("float64"), "silicon": datasets.Value("float64"), "potassium": datasets.Value("float64"), "calcium": datasets.Value("float64"), "barium": datasets.Value("int8"), "iron": datasets.Value("float64"), "is_headlamp_glass": 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 GlassConfig(datasets.BuilderConfig): def __init__(self, **kwargs): super(GlassConfig, self).__init__(version=VERSION, **kwargs) self.features = features_per_config[kwargs["name"]] class Glass(datasets.GeneratorBasedBuilder): # dataset versions DEFAULT_CONFIG = "glass" BUILDER_CONFIGS = [ GlassConfig(name="glass", description="Glass dataset."), GlassConfig(name="windows", description="Is this windows glass?"), GlassConfig(name="vehicles", description="Is this vehicles glass?"), GlassConfig(name="containers", description="Is this containers glass?"), GlassConfig(name="tableware", description="Is this tableware glass?"), GlassConfig(name="headlamps", description="Is this headlamps glass?") ] 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 = 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: data.columns = _BASE_FEATURE_NAMES if self.config.name == "windows": data = data.rename(columns={"glass_type": "is_windows_glass"}) print("\n\n") print(data.is_windows_glass.unique()) data.loc[:, "is_windows_glass"] = data.is_windows_glass.apply(lambda x: 1 if x == 1 or x == 2 else 0) print("\n\n") print(data.is_windows_glass.unique()) print("\n\n") elif self.config.name == "vehicles": data = data.rename(columns={"glass_type": "is_vehicle_glass"}) data.loc[:, "is_vehicle_glass"] = data.is_vehicle_glass.apply(lambda x: 1 if x in {3, 4} else 0) elif self.config.name == "containers": data = data.rename(columns={"glass_type": "is_container_glass"}) data.loc[:, "is_container_glass"] = data.is_container_glass.apply(lambda x: 1 if x == 5 else 0) elif self.config.name == "tableware": data = data.rename(columns={"glass_type": "is_tableware_glass"}) data.loc[:, "is_tableware_glass"] = data.is_tableware_glass.apply(lambda x: 1 if x == 6 else 0) elif self.config.name == "headlamps": data = data.rename(columns={"glass_type": "is_headlamp_glass"}) data.loc[:, "is_headlamp_glass"] = data.is_headlamp_glass.apply(lambda x: 1 if x == 7 else 0) else: data.loc[:, "glass_type"] = data.glass_type.apply(lambda x: x - 1) print(data.glass_type.unique()) return data