-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_utils.py
342 lines (304 loc) · 9.39 KB
/
dataset_utils.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# -*- coding: utf-8 -*-
import numpy as np
from aeon.datasets import load_classification
from sklearn.preprocessing import LabelEncoder
from numba import njit, prange
from convst import __USE_NUMBA_CACHE__
@njit(cache=__USE_NUMBA_CACHE__)
def z_norm_3D(X):
"""
Z normalise a time series dataset assumed to be of even length. A small value
is added to the standard deviation for all samples and features to avoid
0 division.
Parameters
----------
X : array, shape=(n_samples, n_features, n_timestamps)
Input numerical array to z-normalise
Returns
-------
X : array, shape=(n_samples, n_features, n_timestamps)
Z-normalised array
"""
for i_x in prange(X.shape[0]):
for i_ft in prange(X.shape[1]):
X[i_x, i_ft] = (X[i_x, i_ft] - X[i_x, i_ft].mean()) / (
X[i_x, i_ft].std() + 1e-8
)
return X
def z_norm_3D_list(X):
"""
Z normalise a time series dataset assumed to be of even length. A small value
is added to the standard deviation for all samples and features to avoid
0 division.
Parameters
----------
X : array, shape=(n_samples, n_features, n_timestamps)
Input numerical array to z-normalise
Returns
-------
X : array, shape=(n_samples, n_features, n_timestamps)
Z-normalised array
"""
for i_x in range(len(X)):
for i_ft in range(len(X[i_x])):
X[i_x][i_ft] = (X[i_x][i_ft] - X[i_x][i_ft].mean()) / (
X[i_x][i_ft].std() + 1e-8
)
return X
def load_UCR_UEA_dataset_split(name, normalize=False):
"""
Load the original train and test splits of a dataset
from the UCR/UEA archive by name using aeon API.
Parameters
----------
name : string
Name of the dataset to download.
normalize : boolean, optional
If True, time series will be z-normalized. The default is True.
Returns
-------
X_train : array, shape=(n_samples_train, n_features, n_timestamps)
Training data from the dataset specified by path.
X_test : array, shape=(n_samples_test, n_features, n_timestamps)
Testing data from the dataset specified by path.
y_train : array, shape=(n_samples_train)
Class of the training data.
y_test : array, shape=(n_samples_test)
Class of the testing data.
le : LabelEncoder
LabelEncoder object used to uniformize the class labels
"""
# Load datasets
X_train, y_train = load_classification(name, return_metadata=False, split="train")
X_test, y_test = load_classification(name, return_metadata=False, split="test")
# Convert class labels to make sure they are between 0,n_classes
le = LabelEncoder().fit(y_train)
y_train = le.transform(y_train)
y_test = le.transform(y_test)
min_len = min(
min([len(X_train[i][0]) for i in range(len(X_train))]),
min([len(X_test[i][0]) for i in range(len(X_test))]),
)
# Z-Normalize the data
if normalize and not isinstance(X_train, list):
X_train = z_norm_3D(X_train)
X_test = z_norm_3D(X_test)
if normalize and isinstance(X_train, list):
X_train = z_norm_3D_list(X_train)
X_test = z_norm_3D_list(X_test)
return X_train, X_test, y_train, y_test, min_len
def load_UCR_UEA_dataset(name, normalize=False):
"""
Load a dataset from the UCR/UEA archive by name using aeon API
Parameters
----------
name : string
Name of the dataset to download.
normalize : boolean, optional
If True, time series will be z-normalized. The default is True.
Returns
-------
X : array, shape=(n_samples, n_features, n_timestamps)
Time series data from the dataset specified by name.
y : array, shape=(n_samples)
Class of the time series
"""
# Load datasets
X_train, X_test, y_train, y_test, _ = load_UCR_UEA_dataset_split(
name, normalize=normalize
)
return np.concatenate((X_train, X_test), axis=0), np.concatenate(
(y_train, y_test), axis=0
)
def return_all_dataset_names():
return np.concatenate(
(
return_all_univariate_dataset_names(),
return_all_multivariate_dataset_names(),
return_all_variable_univariate_dataset_names(),
return_all_variable_multivariate_dataset_names(),
)
)
def return_all_multivariate_dataset_names():
return np.asarray(
[
"ArticularyWordRecognition",
"AtrialFibrillation",
"BasicMotions",
"Cricket",
"DuckDuckGeese",
"EigenWorms",
"Epilepsy",
"EthanolConcentration",
"ERing",
"FaceDetection",
"FingerMovements",
"HandMovementDirection",
"Handwriting",
"Heartbeat",
"Libras",
"LSST",
"MotorImagery",
"NATOPS",
"PenDigits",
"PEMS-SF",
"PhonemeSpectra",
"RacketSports",
"SelfRegulationSCP1",
"SelfRegulationSCP2",
"StandWalkJump",
"UWaveGestureLibrary",
]
)
def return_all_variable_multivariate_dataset_names():
return np.asarray(
[
"AsphaltObstaclesCoordinates",
"AsphaltPavementTypeCoordinates",
"AsphaltRegularityCoordinates",
"CharacterTrajectories",
"InsectWingbeat",
"JapaneseVowels",
"SpokenArabicDigits",
]
)
def return_all_variable_univariate_dataset_names():
return np.asarray(
[
"AllGestureWiimoteX",
"AllGestureWiimoteY",
"AllGestureWiimoteZ",
"GestureMidAirD1",
"GestureMidAirD2",
"GestureMidAirD3",
"GesturePebbleZ1",
"GesturePebbleZ2",
"PickupGestureWiimoteZ",
"PLAID",
"ShakeGestureWiimoteZ",
]
)
def return_all_univariate_dataset_names():
"""
Return the names of the 112 univariate datasets of the UCR archive.
Returns
-------
array, shape=(112)
Names of the univariate UCR datasets.
"""
return np.asarray(
[
"ACSF1",
"Adiac",
"ArrowHead",
"Beef",
"BeetleFly",
"BirdChicken",
"BME",
"Car",
"CBF",
"Chinatown",
"ChlorineConcentration",
"Coffee",
"Computers",
"CricketX",
"CricketY",
"CricketZ",
"DiatomSizeReduction",
"DistalPhalanxOutlineAgeGroup",
"DistalPhalanxOutlineCorrect",
"DistalPhalanxTW",
"Earthquakes",
"ECG200",
"ECG5000",
"ECGFiveDays",
"ElectricDevices",
"EOGHorizontalSignal",
"EOGVerticalSignal",
"FaceAll",
"FaceFour",
"FacesUCR",
"FiftyWords",
"Fish",
"FordA",
"FordB",
"FreezerRegularTrain",
"FreezerSmallTrain",
"GunPoint",
"GunPointAgeSpan",
"GunPointMaleVersusFemale",
"GunPointOldVersusYoung",
"Ham",
"Haptics",
"Herring",
"HouseTwenty",
"InlineSkate",
"InsectEPGRegularTrain",
"InsectEPGSmallTrain",
"InsectWingbeatSound",
"ItalyPowerDemand",
"LargeKitchenAppliances",
"Lightning2",
"Lightning7",
"Mallat",
"Meat",
"MedicalImages",
"MiddlePhalanxOutlineAgeGroup",
"MiddlePhalanxOutlineCorrect",
"MiddlePhalanxTW",
"MixedShapesRegularTrain",
"MixedShapesSmallTrain",
"MoteStrain",
"NonInvasiveFetalECGThorax1",
"NonInvasiveFetalECGThorax2",
"OliveOil",
"OSULeaf",
"PhalangesOutlinesCorrect",
"Plane",
"PowerCons",
"ProximalPhalanxOutlineAgeGroup",
"ProximalPhalanxOutlineCorrect",
"ProximalPhalanxTW",
"RefrigerationDevices",
"ScreenType",
"SemgHandGenderCh2",
"SemgHandMovementCh2",
"SemgHandSubjectCh2",
"ShapeletSim",
"ShapesAll",
"SmallKitchenAppliances",
"SmoothSubspace",
"SonyAIBORobotSurface1",
"SonyAIBORobotSurface2",
"Strawberry",
"SwedishLeaf",
"Symbols",
"SyntheticControl",
"ToeSegmentation1",
"ToeSegmentation2",
"Trace",
"TwoLeadECG",
"TwoPatterns",
"UMD",
"UWaveGestureLibraryAll",
"UWaveGestureLibraryX",
"UWaveGestureLibraryY",
"UWaveGestureLibraryZ",
"Wafer",
"Wine",
"WordSynonyms",
"Worms",
"WormsTwoClass",
"Yoga",
"Phoneme",
"PigAirwayPressure",
"PigArtPressure",
"PigCVP",
"Crop",
"StarLightCurves",
"Rock",
"HandOutlines",
"CinCECGTorso",
"EthanolLevel",
]
)