File size: 3,964 Bytes
1db1c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Ionosphere"""

from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")

DESCRIPTION = "Ionosphere dataset from the UCI ML repository."
_HOMEPAGE = "https://archive.ics.uci.edu/ml/datasets/Ionosphere"
_URLS = ("https://huggingface.co/datasets/mstz/ionosphere/raw/ionosphere.data")
_CITATION = """
@misc{misc_ionosphere_52,
  author       = {Sigillito,V., Wing,S., Hutton,L. & Baker,K.},
  title        = {{Ionosphere}},
  year         = {1989},
  howpublished = {UCI Machine Learning Repository},
  note         = {{DOI}: \\url{10.24432/C5W01B}}
}"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/ionosphere/raw/main/ionosphere.data"
}
features_types_per_config = {
    "ionosphere": {
        "signal_0": datasets.Value("float64"),
        "signal_1": datasets.Value("float64"),
        "signal_2": datasets.Value("float64"),
        "signal_3": datasets.Value("float64"),
        "signal_4": datasets.Value("float64"),
        "signal_5": datasets.Value("float64"),
        "signal_6": datasets.Value("float64"),
        "signal_7": datasets.Value("float64"),
        "signal_8": datasets.Value("float64"),
        "signal_9": datasets.Value("float64"),
        "signal_10": datasets.Value("float64"),
        "signal_11": datasets.Value("float64"),
        "signal_12": datasets.Value("float64"),
        "signal_13": datasets.Value("float64"),
        "signal_14": datasets.Value("float64"),
        "signal_15": datasets.Value("float64"),
        "signal_16": datasets.Value("float64"),
        "signal_17": datasets.Value("float64"),
        "signal_18": datasets.Value("float64"),
        "signal_19": datasets.Value("float64"),
        "signal_20": datasets.Value("float64"),
        "signal_21": datasets.Value("float64"),
        "signal_22": datasets.Value("float64"),
        "signal_23": datasets.Value("float64"),
        "signal_24": datasets.Value("float64"),
        "signal_25": datasets.Value("float64"),
        "signal_26": datasets.Value("float64"),
        "signal_27": datasets.Value("float64"),
        "signal_28": datasets.Value("float64"),
        "signal_29": datasets.Value("float64"),
        "signal_30": datasets.Value("float64"),
        "signal_31": datasets.Value("float64"),
        "signal_32": datasets.Value("float64"),
        "signal_33": 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 IonosphereConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(IonosphereConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Ionosphere(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "ionosphere"
    BUILDER_CONFIGS = [
        IonosphereConfig(name="ionosphere",
                    description="Ionosphere 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 = [f"signal_{i}" for i in range(data.shape[1] - 1)] + ["class"]
        data.loc[:, "class"] = data["class"].apply(lambda x: 1 if x == "g" else 0)

        for row_id, row in data.iterrows():
            data_row = dict(row)

            yield row_id, data_row