wall_following / wall_following.py
mstz's picture
Upload 3 files
b8a8ff6
"""WallFollowing Dataset"""
from typing import List
from functools import partial
import datasets
import pandas
VERSION = datasets.Version("1.0.0")
_ENCODING_DICS = {}
DESCRIPTION = "WallFollowing dataset."
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/194/wall+following+robot+navigation+data"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/194/wall+following+robot+navigation+data")
_CITATION = """
@misc{misc_wall-following_robot_navigation_data_194,
author = {Freire,Ananda, Veloso,Marcus & Barreto,Guilherme},
title = {{Wall-Following Robot Navigation Data}},
year = {2010},
howpublished = {UCI Machine Learning Repository},
note = {{DOI}: \\url{10.24432/C57C8W}}
}
"""
# Dataset info
urls_per_split = {
"train": "https://huggingface.co/datasets/mstz/wall_following/raw/main/wall_following.csv"
}
features_types_per_config = {
"wall_following": {
"US1": datasets.Value("float64"),
"US2": datasets.Value("float64"),
"US3": datasets.Value("float64"),
"US4": datasets.Value("float64"),
"US5": datasets.Value("float64"),
"US6": datasets.Value("float64"),
"US7": datasets.Value("float64"),
"US8": datasets.Value("float64"),
"US9": datasets.Value("float64"),
"US10": datasets.Value("float64"),
"US11": datasets.Value("float64"),
"US12": datasets.Value("float64"),
"US13": datasets.Value("float64"),
"US14": datasets.Value("float64"),
"US15": datasets.Value("float64"),
"US16": datasets.Value("float64"),
"US17": datasets.Value("float64"),
"US18": datasets.Value("float64"),
"US19": datasets.Value("float64"),
"US20": datasets.Value("float64"),
"US21": datasets.Value("float64"),
"US22": datasets.Value("float64"),
"US23": datasets.Value("float64"),
"US24": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=4),
},
"wall_following_0": {
"US1": datasets.Value("float64"),
"US2": datasets.Value("float64"),
"US3": datasets.Value("float64"),
"US4": datasets.Value("float64"),
"US5": datasets.Value("float64"),
"US6": datasets.Value("float64"),
"US7": datasets.Value("float64"),
"US8": datasets.Value("float64"),
"US9": datasets.Value("float64"),
"US10": datasets.Value("float64"),
"US11": datasets.Value("float64"),
"US12": datasets.Value("float64"),
"US13": datasets.Value("float64"),
"US14": datasets.Value("float64"),
"US15": datasets.Value("float64"),
"US16": datasets.Value("float64"),
"US17": datasets.Value("float64"),
"US18": datasets.Value("float64"),
"US19": datasets.Value("float64"),
"US20": datasets.Value("float64"),
"US21": datasets.Value("float64"),
"US22": datasets.Value("float64"),
"US23": datasets.Value("float64"),
"US24": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"wall_following_1": {
"US1": datasets.Value("float64"),
"US2": datasets.Value("float64"),
"US3": datasets.Value("float64"),
"US4": datasets.Value("float64"),
"US5": datasets.Value("float64"),
"US6": datasets.Value("float64"),
"US7": datasets.Value("float64"),
"US8": datasets.Value("float64"),
"US9": datasets.Value("float64"),
"US10": datasets.Value("float64"),
"US11": datasets.Value("float64"),
"US12": datasets.Value("float64"),
"US13": datasets.Value("float64"),
"US14": datasets.Value("float64"),
"US15": datasets.Value("float64"),
"US16": datasets.Value("float64"),
"US17": datasets.Value("float64"),
"US18": datasets.Value("float64"),
"US19": datasets.Value("float64"),
"US20": datasets.Value("float64"),
"US21": datasets.Value("float64"),
"US22": datasets.Value("float64"),
"US23": datasets.Value("float64"),
"US24": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"wall_following_2": {
"US1": datasets.Value("float64"),
"US2": datasets.Value("float64"),
"US3": datasets.Value("float64"),
"US4": datasets.Value("float64"),
"US5": datasets.Value("float64"),
"US6": datasets.Value("float64"),
"US7": datasets.Value("float64"),
"US8": datasets.Value("float64"),
"US9": datasets.Value("float64"),
"US10": datasets.Value("float64"),
"US11": datasets.Value("float64"),
"US12": datasets.Value("float64"),
"US13": datasets.Value("float64"),
"US14": datasets.Value("float64"),
"US15": datasets.Value("float64"),
"US16": datasets.Value("float64"),
"US17": datasets.Value("float64"),
"US18": datasets.Value("float64"),
"US19": datasets.Value("float64"),
"US20": datasets.Value("float64"),
"US21": datasets.Value("float64"),
"US22": datasets.Value("float64"),
"US23": datasets.Value("float64"),
"US24": datasets.Value("float64"),
"class": datasets.ClassLabel(num_classes=2),
},
"wall_following_3": {
"US1": datasets.Value("float64"),
"US2": datasets.Value("float64"),
"US3": datasets.Value("float64"),
"US4": datasets.Value("float64"),
"US5": datasets.Value("float64"),
"US6": datasets.Value("float64"),
"US7": datasets.Value("float64"),
"US8": datasets.Value("float64"),
"US9": datasets.Value("float64"),
"US10": datasets.Value("float64"),
"US11": datasets.Value("float64"),
"US12": datasets.Value("float64"),
"US13": datasets.Value("float64"),
"US14": datasets.Value("float64"),
"US15": datasets.Value("float64"),
"US16": datasets.Value("float64"),
"US17": datasets.Value("float64"),
"US18": datasets.Value("float64"),
"US19": datasets.Value("float64"),
"US20": datasets.Value("float64"),
"US21": datasets.Value("float64"),
"US22": datasets.Value("float64"),
"US23": datasets.Value("float64"),
"US24": datasets.Value("float64"),
"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 WallFollowingConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
super(WallFollowingConfig, self).__init__(version=VERSION, **kwargs)
self.features = features_per_config[kwargs["name"]]
class WallFollowing(datasets.GeneratorBasedBuilder):
# dataset versions
DEFAULT_CONFIG = "wall_following"
BUILDER_CONFIGS = [
WallFollowingConfig(name="wall_following", description="WallFollowing for multiclass classification."),
WallFollowingConfig(name="wall_following_0", description="WallFollowing for binary classification."),
WallFollowingConfig(name="wall_following_1", description="WallFollowing for binary classification."),
WallFollowingConfig(name="wall_following_2", description="WallFollowing for binary classification."),
WallFollowingConfig(name="wall_following_3", description="WallFollowing 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 == "wall_following_0":
data["class"] = data["class"].apply(lambda x: 1 if x == 0 else 0)
elif self.config.name == "wall_following_1":
data["class"] = data["class"].apply(lambda x: 1 if x == 1 else 0)
elif self.config.name == "wall_following_2":
data["class"] = data["class"].apply(lambda x: 1 if x == 2 else 0)
elif self.config.name == "wall_following_3":
data["class"] = data["class"].apply(lambda x: 1 if x == 3 else 0)
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}")