generated from institut-galilee/2021-ml-iot-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
439 lines (373 loc) · 13.4 KB
/
dataset.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import os
# import subprocess
import numpy as np
import pandas as pd
import matplotlib
from matplotlib import pyplot as plt
from cycler import cycler
from config import Configuration as config
class DataReader(object):
"""
Example:
```python
train = DataReader(what='train')
validation = DataReader(what='validation')
test = DataReader(what='test')
print('Shape of train.X[''Torso''][''Acc_x'']')
print(train.X['Torso']['Acc_x'].shape)
print('Shape of train.y')
print(train.y.shape)
print('Shape of validation.X[''Torso''][''Acc_x'']')
print(validation.X['Torso']['Acc_x'].shape)
print('Shape of validation.y')
print(validation.y.shape)
print('Shape of test.X[''Torso''][''Acc_x'']')
print(test.X['Torso']['Acc_x'].shape)
# no labels for the test set!
```
"""
def __init__(self, what='train', train_frames=196072):
self.what = what
self.train_frames = train_frames
# before starting anything, check if the right folder where we will
# store data exists, otherwise create it
# e.g. generated/0.1/train/ or generation/0.1/validation
path = os.path.join(
config.experimentsfolder,
what)
if not os.path.exists(path):
os.makedirs(path)
if what == 'train' or what == 'validation':
self._data = self._load_data(what)
self._labels = self._load_labels(what)
else:
self._data = self._load_data(what)
@property
def X(self):
return self._data
@property
def y(self):
return self._labels
# channels corresponding to the columns of <position>_motion.txt files
# ordered according to the SHL dataset documentation.
channels = {
# [...]
2: 'Acc_x',
3: 'Acc_y',
4: 'Acc_z',
5: 'Gyr_x',
6: 'Gyr_y',
7: 'Gyr_z',
8: 'Mag_x',
9: 'Mag_y',
10: 'Mag_z',
# 11: 'Ori_w',
# 12: 'Ori_x',
# 13: 'Ori_y',
# 14: 'Ori_z',
# 15: 'Gra_x',
# 16: 'Gra_y',
# 17: 'Gra_z',
# 18: 'LAcc_x',
# 19: 'LAcc_y',
# 20: 'LAcc_z',
# 21: 'Pressure'
# [...]
}
modalities = [
'Acc',
'Gyr',
'Mag',
# 'LAc',
# 'Gra',
# 'Ori',
# 'Pre'
]
def channel_to_modality(channel):
return channel[:3] # haha
coarselabel_map = {
# 0: 'null',
1: 'still',
2: 'walk',
3: 'run',
4: 'bike',
5: 'car',
6: 'bus',
7: 'train',
8: 'subway',
}
# finelabel_map = {
# 1: 'till;Stand;Outside',
# 2: 'Still;Stand;Inside',
# 3: 'Still;Sit;Outside',
# 4: 'Still;Sit;Inside',
# 5: 'Walking;Outside',
# 6: 'Walking;Inside',
# 7: 'Run',
# 8: 'Bike',
# 9: 'Car;Driver',
# 10: 'Car;Passenger',
# 11: 'Bus;Stand',
# 12: 'Bus;Sit',
# 13: 'Bus;Up;Stand',
# 14: 'Bus;Up;Sit',
# 15: 'Train;Stand',
# 16: 'Train;Sit',
# 17: 'Subway;Stand',
# 18: 'Subway;Sit',
# }
smartphone_positions = [
'Torso',
# 'Hips',
# 'Bag',
# 'Hand'
]
# files = [
# 'Acc_x.txt', 'Acc_y.txt', 'Acc_z.txt',
# 'Gyr_x.txt', 'Gyr_y.txt', 'Gyr_z.txt',
# 'Mag_x.txt', 'Mag_y.txt', 'Mag_z.txt',
# 'LAcc_x.txt', 'LAcc_y.txt', 'LAcc_z.txt',
# 'Gra_x.txt', 'Gra_y.txt', 'Gra_z.txt',
# 'Ori_x.txt', 'Ori_y.txt', 'Ori_z.txt', 'Ori_w.txt',
# 'Pressure.txt'
# ]
# trainfiles = {
# 'User1': ['220617', '260617', '270617'],
# 'User2': ['140617', '140717', '180717'],
# 'User3': ['030717', '070717', '140617'],
# }
# testfiles = {
# }
num_channels = len(channels) # 20
num_modalities = len(modalities) # 7
num_coarselabels = len(coarselabel_map)
# num_finelabels = len(finelabel_map)
samples = 500
# train_frames = 196072
validation_frames = 28789
test_frames = 57573
def _load_data(self, what='train'):
"""
Synopsis
Returns
"""
data = {}
# test data
if what == 'test':
for _, channel in self.channels.items():
# e.g. data/Gra_x.txt
src = os.path.join(
config.datafolder,
channel + '.txt')
key = \
what + '_' +\
channel
dest = os.path.join(
config.experimentsfolder, key + '.mmap')
data[channel] = self._mmap_file(
src,
dest,
dtype=np.double,
shape=(self.test_frames, self.samples))
return data
# common for train and validation
for position in self.smartphone_positions:
data[position] = {}
for _, channel in self.channels.items():
# e.g. data/train/Torso/Gra_x.txt
src = os.path.join(
config.datafolder,
what,
position,
channel + '.txt')
key = \
what + '_' +\
position + '_' +\
channel
dest = os.path.join(
config.experimentsfolder,
what,
key + '.mmap')
data[position][channel] = self._mmap_file(
src,
dest,
dtype=np.double,
shape=(self.train_frames if what == 'train' else self.validation_frames,
self.samples))
return data
def _load_labels(self, what='train', position='Torso'):
"""
Synopsis
Returns
NB. Each sub-directory (Bag, Hips, Torso, Hand) has a Label.txt file,
however, they are the same. Indeed, the data collection from the
different positions is synchronized. Therefore, this function loads only
one Label.txt among those available at each position.
By default, this function loads the training Label.txt contained in Torso
sub-folder (i.e., '/train/Torso/Label.txt') as well as the validation
Label.txt contained, also, in Torso sub-folder.
"""
filename = 'Label.txt'
src = os.path.join(
config.datafolder,
what,
position,
filename)
dest = os.path.join(
config.experimentsfolder,
what,
what + '_Label.mmap')
labels = self._mmap_file(
src,
dest,
dtype=np.integer,
shape=(self.train_frames if what == 'train' else self.validation_frames,
self.samples))
return labels
def _mmap_file(self, src, dest, dtype, shape):
if os.path.exists(dest):
# just load mmap file contents
print('%s exists, loading ...' % dest)
mmap = np.memmap(
dest,
mode='r+', # originally, mode was set to r+, i.e. Open existing file for reading and writing.
dtype=dtype,
shape=shape)
return mmap
else:
# build mmap file from scratch
print('Building from scratch %s ...' % dest)
print(shape)
mmap = np.memmap(
dest,
mode='w+',
dtype=dtype,
shape=shape)
chunksize = 5000
offset = 0
for chunk in pd.read_csv(src, delimiter=' ', chunksize=chunksize, header=None):
mmap[offset:offset+chunk.shape[0]] = chunk.values
offset += chunk.shape[0]
return mmap
def CHECK_transition_frames(self):
"""
This function checks for frames that contain a transition between two activities.
Returns
a list containing the index of the frames that contain a transition between two activities
"""
tr_frames = []
for i, frame in enumerate(self.y):
if not np.all(frame == frame[0]):
tr_frames.append(frame)
print('there are ', len(tr_frames), ' frames containing a transition')
return tr_frames
def CHECK_nans(self):
# checking for nan's
nans = {}
for position in self.smartphone_positions:
for _, channel in self.channels.items():
for i, a in enumerate(self.X[position][channel]):
if np.isnan(a).any():
if position not in nans:
nans[position] = {}
if channel not in nans[position]:
nans[position][channel] = []
nans[position][channel].append(i)
print('there are ', len(nans), ' frames containing NaNs')
return nans
def replace_nans(self, index=0):
"""
First, checks if the frame of index `index` actually contains NaNs;
Second, infere an interpolation function on the values from 0 to 450;
Lastly, replaces the NaNs using the infered interpolation function;
NB. this function supposes that the index of the frame that contains NaNs
is known beforehand by the user.
"""
from scipy.interpolate import interp1d
for position in self.smartphone_positions:
for _, channel in self.channels.items():
a = self.X[position][channel][index]
if np.isnan(a).any():
print('Imputing NaN in frame ', index,\
' of channel ', channel,\
' located on ', position)
interp = interp1d(range(450), a[:450], fill_value='extrapolate')
for j in np.where(np.isnan(a))[0]:
print('j ', j, ' interp ', interp(j))
self.X[position][channel][index, j] = interp(j)
else:
print('No missing value was found. No imputation performed')
def plot(self, sample_idx, modality, position, save=False):
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.plot(self.X[position][modality][sample_idx], 'b-')
title = 'sample_idx_'\
+str(sample_idx)+'_'\
+modality+'_'\
+position+'__'\
+self.coarselabel_map[self.y[sample_idx][0]]
ax.set_title(title)
if save:
fig.savefig(title)
return ax
def plot_activites(self, portion=None, save=False):
"""
plot transitions between activities over time
"""
y = self.y[:, 0] # FIXME: we are considering the label of the first sample of a given example
start = 0
end = 0
for portion in np.array_split(y, 10):
print('portion.shape ', portion.shape)
index = np.arange(portion.shape[0])
print('index.shape ', index.shape)
fulfill = np.zeros_like(index) + 1
start = end + 1
end += portion.shape[0]
n = 8 # number of classes
# color = plt.cm.YlGnBu(np.linspace(0, 1,n))
color = plt.cm.Paired(np.linspace(0, 1,n))
plt.rcParams['axes.prop_cycle'] = cycler('color', color)
fig, ax = plt.subplots(figsize=(15,1))
for i in range(1, n+1):
ax.bar(index[portion==i],
fulfill[portion==i],
label=self.coarselabel_map[i])
print('start ', str(start))
print('end ', str(end))
#fig.get_axes()[0].set_xlabel('Recognition performances')
plt.yticks([])
plt.xticks(rotation=70)
ax.set_xticks([i for i in np.arange(1, index.shape[0], 500)])
ax.set_xticklabels([i for i in np.arange(start, end, 500)])
ax.set_xlabel('example index')
# plt.legend(['still', 'walk', 'run', 'bike', 'car', 'subway', 'train', 'bus'])
fig.suptitle('Transitions between activities --- examples from '\
+ str(start) +' to '+ str(end))
fig.savefig('misc/'+self.what+'_transitions_between_activities_'\
+str(start)+'_'+str(end)+'.svg',
format='svg',
bbox_inches='tight')
plt.close(fig)
if __name__ == '__main__':
train = DataReader(what='train')
validation = DataReader(what='validation')
test = DataReader(what='test')
print('Shape of train.X[''Torso''][''Acc_x'']')
print(train.X['Torso']['Acc_x'].shape)
print('Shape of train.y')
print(train.y.shape)
print('unique labels in train: ', np.unique(train.y))
train.CHECK_transition_frames()
train.CHECK_nans()
# train.replace_nans(index=121217)
print('Shape of validation.X[''Torso''][''Acc_x'']')
print(validation.X['Torso']['Acc_x'].shape)
print('Shape of validation.y')
print(validation.y.shape)
print('unique labels in validation: ', np.unique(validation.y))
validation.CHECK_transition_frames()
validation.CHECK_nans()
print('Shape of test.X[''Torso''][''Acc_x'']')
print(test.X['Acc_x'].shape)
validation.CHECK_nans()
# no labels for the test set!