File size: 6,639 Bytes
558be89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f8fe8f
 
 
 
 
 
 
 
 
 
558be89
 
 
 
 
 
58df4ea
 
 
 
 
 
 
 
 
d565e38
58df4ea
 
558be89
58df4ea
 
 
 
 
 
 
 
 
 
 
558be89
58df4ea
 
 
 
 
 
 
 
 
 
 
558be89
58df4ea
 
 
 
 
 
 
 
 
 
 
558be89
58df4ea
 
 
 
 
 
 
 
 
 
 
558be89
58df4ea
 
 
 
 
 
 
 
 
 
 
558be89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58df4ea
 
 
 
 
 
558be89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58df4ea
a06638a
bacaae8
b74785a
58df4ea
 
3bee266
bacaae8
58df4ea
 
3bee266
bacaae8
58df4ea
 
a06638a
bacaae8
58df4ea
 
3bee266
bacaae8
58df4ea
 
 
2f8fe8f
558be89
 
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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"})
			data.loc[:, "is_windows_glass"] = data.is_windows_glass.apply(lambda x: 1 if x == 1 or x == 2 else 0)

		
		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