-
Notifications
You must be signed in to change notification settings - Fork 69
/
uea.py
175 lines (148 loc) · 7.9 KB
/
uea.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
import collections as co
import numpy as np
import os
import pathlib
import sktime.utils.load_data
import torch
import urllib.request
import zipfile
from . import common
here = pathlib.Path(__file__).resolve().parent
def download():
base_base_loc = here / 'data'
base_loc = base_base_loc / 'UEA'
loc = base_loc / 'Multivariate2018_ts.zip'
if os.path.exists(loc):
return
if not os.path.exists(base_base_loc):
os.mkdir(base_base_loc)
if not os.path.exists(base_loc):
os.mkdir(base_loc)
urllib.request.urlretrieve('http://www.timeseriesclassification.com/Downloads/Archives/Multivariate2018_ts.zip',
str(loc))
with zipfile.ZipFile(loc, 'r') as f:
f.extractall(str(base_loc))
# Is this actually necessary?
def _pad(channel, maxlen):
channel = torch.tensor(channel)
out = torch.full((maxlen,), channel[-1])
out[:channel.size(0)] = channel
return out
valid_dataset_names = {'ArticularyWordRecognition',
'FaceDetection',
'NATOPS',
'AtrialFibrillation',
'FingerMovements',
'PEMS - SF',
'BasicMotions',
'HandMovementDirection',
'PenDigits',
'CharacterTrajectories',
'Handwriting',
'PhonemeSpectra',
'Cricket',
'Heartbeat',
'RacketSports',
'DuckDuckGeese',
'InsectWingbeat',
'SelfRegulationSCP1',
'EigenWorms',
'JapaneseVowels',
'SelfRegulationSCP2',
'Epilepsy',
'Libras',
'SpokenArabicDigits',
'ERing',
'LSST',
'StandWalkJump',
'EthanolConcentration',
'MotorImagery',
'UWaveGestureLibrary'}
def _process_data(dataset_name, missing_rate, intensity):
# We begin by loading both the train and test data and using our own train/val/test split.
# The reason for this is that (a) by default there is no val split and (b) the sizes of the train/test splits are
# really janky by default. (e.g. LSST has 2459 training samples and 2466 test samples.)
assert dataset_name in valid_dataset_names, "Must specify a valid dataset name."
base_filename = here / 'data' / 'UEA' / 'Multivariate_ts' / dataset_name / dataset_name
train_X, train_y = sktime.utils.load_data.load_from_tsfile_to_dataframe(str(base_filename) + '_TRAIN.ts')
test_X, test_y = sktime.utils.load_data.load_from_tsfile_to_dataframe(str(base_filename) + '_TEST.ts')
train_X = train_X.to_numpy()
test_X = test_X.to_numpy()
X = np.concatenate((train_X, test_X), axis=0)
y = np.concatenate((train_y, test_y), axis=0)
lengths = torch.tensor([len(Xi[0]) for Xi in X])
final_index = lengths - 1
maxlen = lengths.max()
# X is now a numpy array of shape (batch, channel)
# Each channel is a pandas.core.series.Series object of length corresponding to the length of the time series
X = torch.stack([torch.stack([_pad(channel, maxlen) for channel in batch], dim=0) for batch in X], dim=0)
# X is now a tensor of shape (batch, channel, length)
X = X.transpose(-1, -2)
# X is now a tensor of shape (batch, length, channel)
times = torch.linspace(0, X.size(1) - 1, X.size(1))
generator = torch.Generator().manual_seed(56789)
for Xi in X:
removed_points = torch.randperm(X.size(1), generator=generator)[:int(X.size(1) * missing_rate)].sort().values
Xi[removed_points] = float('nan')
# Now fix the labels to be integers from 0 upwards
targets = co.OrderedDict()
counter = 0
for yi in y:
if yi not in targets:
targets[yi] = counter
counter += 1
y = torch.tensor([targets[yi] for yi in y])
(times, train_coeffs, val_coeffs, test_coeffs, train_y, val_y, test_y, train_final_index, val_final_index,
test_final_index, input_channels) = common.preprocess_data(times, X, y, final_index, append_times=True,
append_intensity=intensity)
num_classes = counter
assert num_classes >= 2, "Have only {} classes.".format(num_classes)
return (times, train_coeffs, val_coeffs, test_coeffs, train_y, val_y, test_y, train_final_index, val_final_index,
test_final_index, num_classes, input_channels)
def get_data(dataset_name, missing_rate, device, intensity, batch_size):
# We begin by loading both the train and test data and using our own train/val/test split.
# The reason for this is that (a) by default there is no val split and (b) the sizes of the train/test splits are
# really janky by default. (e.g. LSST has 2459 training samples and 2466 test samples.)
assert dataset_name in valid_dataset_names, "Must specify a valid dataset name."
base_base_loc = here / 'processed_data'
base_loc = base_base_loc / 'UEA'
loc = base_loc / (dataset_name + str(int(missing_rate * 100)) + ('_intensity' if intensity else ''))
if os.path.exists(loc):
tensors = common.load_data(loc)
times = tensors['times']
train_coeffs = tensors['train_a'], tensors['train_b'], tensors['train_c'], tensors['train_d']
val_coeffs = tensors['val_a'], tensors['val_b'], tensors['val_c'], tensors['val_d']
test_coeffs = tensors['test_a'], tensors['test_b'], tensors['test_c'], tensors['test_d']
train_y = tensors['train_y']
val_y = tensors['val_y']
test_y = tensors['test_y']
train_final_index = tensors['train_final_index']
val_final_index = tensors['val_final_index']
test_final_index = tensors['test_final_index']
num_classes = int(tensors['num_classes'])
input_channels = int(tensors['input_channels'])
else:
download()
if not os.path.exists(base_base_loc):
os.mkdir(base_base_loc)
if not os.path.exists(base_loc):
os.mkdir(base_loc)
if not os.path.exists(loc):
os.mkdir(loc)
(times, train_coeffs, val_coeffs, test_coeffs, train_y, val_y, test_y, train_final_index, val_final_index,
test_final_index, num_classes, input_channels) = _process_data(dataset_name, missing_rate, intensity)
common.save_data(loc,
times=times,
train_a=train_coeffs[0], train_b=train_coeffs[1], train_c=train_coeffs[2],
train_d=train_coeffs[3],
val_a=val_coeffs[0], val_b=val_coeffs[1], val_c=val_coeffs[2], val_d=val_coeffs[3],
test_a=test_coeffs[0], test_b=test_coeffs[1], test_c=test_coeffs[2], test_d=test_coeffs[3],
train_y=train_y, val_y=val_y, test_y=test_y, train_final_index=train_final_index,
val_final_index=val_final_index, test_final_index=test_final_index,
num_classes=torch.as_tensor(num_classes), input_channels=torch.as_tensor(input_channels))
times, train_dataloader, val_dataloader, test_dataloader = common.wrap_data(times, train_coeffs, val_coeffs,
test_coeffs, train_y, val_y, test_y,
train_final_index, val_final_index,
test_final_index, device,
num_workers=0, batch_size=batch_size)
return times, train_dataloader, val_dataloader, test_dataloader, num_classes, input_channels