File size: 6,447 Bytes
2a1b4e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03a62ca
 
2a1b4e2
 
 
 
 
 
 
 
 
 
 
 
 
c882093
 
7a51ffd
7da7188
588397f
f10f46d
7da7188
c882093
 
8585b9c
2a1b4e2
 
 
 
f10f46d
2a1b4e2
 
 
 
 
 
88028db
2a1b4e2
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from typing import List

import datasets

import pandas


VERSION = datasets.Version("1.0.0")
_BASE_FEATURE_NAMES = [
    "family",
    "product_type",
    "steel_type",
    "carbon",
    "hardness",
    "temper_rolling",
    "condition",
    "formability",
    "strength",
    "non_ageing",
    "surface_finish",
    "surface_quality",
    "enamelability",
    "bc",
    "bf",
    "bt",
    "bw_time",
    "bl",
    "m",
    "chrom",
    "phos",
    "cbond",
    "marvi",
    "exptl",
    "ferro",
    "corr",
    "blue",
    "lustre",
    "jurofm",
    "s",
    "p",
    "is_coil",
    "thick",
    "width",
    "len",
    "oil",
    "bore",
    "packing",
    "class"
]


DESCRIPTION = "Anneal dataset from the UCI ML repository."
_HOMEPAGE = "https://archive-beta.ics.uci.edu/dataset/3/annealing"
_URLS = ("https://archive-beta.ics.uci.edu/dataset/3/annealing")
_CITATION = """"""

# Dataset info
urls_per_split = {
    "train": "https://huggingface.co/datasets/mstz/anneal/raw/main/anneal.data",
    "test": "https://huggingface.co/datasets/mstz/anneal/raw/main/anneal.test"
}
features_types_per_config = {
    "annealing": {
        "family": datasets.Value("string"),
        "steel_type": datasets.Value("string"),
        "carbon": datasets.Value("float64"),
        "hardness": datasets.Value("float64"),
        "temper_rolling": datasets.Value("bool"),
        "condition": datasets.Value("string"),
        "formability": datasets.Value("int8"),
        "strength": datasets.Value("float64"),
        "surface_finish": datasets.Value("string"),
        "surface_quality": datasets.Value("string"),
        "enamelability": datasets.Value("int8"),
        "bc": datasets.Value("bool"),
        "bf": datasets.Value("bool"),
        "bt": datasets.Value("bool"),
        "bw_time": datasets.Value("string"),
        "bl": datasets.Value("bool"),
        "chrom": datasets.Value("bool"),
        "phos": datasets.Value("bool"),
        "cbond": datasets.Value("bool"),
        "marvi": datasets.Value("bool"),
        "exptl": datasets.Value("bool"),
        "ferro": datasets.Value("bool"),
        "corr": datasets.Value("bool"),
        "blue": datasets.Value("string"),
        "lustre": datasets.Value("bool"),
        "jurofm": datasets.Value("bool"),
        "s": datasets.Value("bool"),
        "p": datasets.Value("bool"),
        "is_coil": datasets.Value("bool"),
        "thick": datasets.Value("float64"),
        "width": datasets.Value("float64"),
        "len": datasets.Value("float64"),
        "oil": datasets.Value("string"),
        "bore": datasets.Value("string"),
        "packing": datasets.Value("string"),
        "class": datasets.ClassLabel(num_classes=6, names=["1", "2", "3", "4", "5", "U"])
    }
}
features_per_config = {k: datasets.Features(features_types_per_config[k]) for k in features_types_per_config}


class AnnealConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(AnnealConfig, self).__init__(version=VERSION, **kwargs)
        self.features = features_per_config[kwargs["name"]]


class Anneal(datasets.GeneratorBasedBuilder):
    # dataset versions
    DEFAULT_CONFIG = "annealing"
    BUILDER_CONFIGS = [
        AnnealConfig(name="annealing",
                    description="Anneal for multiclass 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"]}),
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloads["test"]})
        ]
    
    def _generate_examples(self, filepath: str):
            data = pandas.read_csv(filepath)
            data = self.preprocess(data, config=self.config.name)

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

                yield row_id, data_row

    def preprocess(self, data: pandas.DataFrame, config: str = DEFAULT_CONFIG) -> pandas.DataFrame:
        data.columns = _BASE_FEATURE_NAMES
        # data = data[data.formability != "?"]
        # data = data[data.enamelability != "?"]

        data.drop("product_type", axis="columns", inplace=True)
        data.drop("non_ageing", axis="columns", inplace=True)
        data.drop("m", axis="columns", inplace=True)
        
        data.loc[:, "formability"] = data.formability.apply(lambda x: int(x) if x not in ("?", "-") else 0)
        data.loc[:, "enamelability"] = data.enamelability.apply(lambda x: int(x) if x not in ("?", "-") else 0)
        # data.loc[:, "non_ageing"] = data.formability.apply(lambda x: True if x == "-" else False)
        data.loc[:, "bc"] = data.bc.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "bf"] = data.bf.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "bt"] = data.bt.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "bl"] = data.bl.apply(lambda x: True if x == "Y" else False)
        # data.loc[:, "m"] = data.m.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "chrom"] = data.chrom.apply(lambda x: True if x == "C" else False)
        data.loc[:, "phos"] = data.phos.apply(lambda x: True if x == "P" else False)
        data.loc[:, "cbond"] = data.cbond.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "marvi"] = data.marvi.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "exptl"] = data.exptl.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "ferro"] = data.ferro.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "corr"] = data["corr"].apply(lambda x: True if x == "Y" else False)
        data.loc[:, "lustre"] = data.lustre.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "jurofm"] = data.jurofm.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "s"] = data.s.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "p"] = data.p.apply(lambda x: True if x == "Y" else False)
        data.loc[:, "class"] = data.p.apply(lambda x: int(x) if x != "U" else 0)

        return data