-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathExample_DataAugmentation_TimeseriesData.py
383 lines (235 loc) · 9.17 KB
/
Example_DataAugmentation_TimeseriesData.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
# coding: utf-8
# # Data augmentation for time-series data
# #### This is a simple example to apply data augmentation to time-series data (e.g. wearable sensor data). If it helps your research, please cite the below paper.
# T. T. Um et al., “Data augmentation of wearable sensor data for parkinson’s disease monitoring using convolutional neural networks,” in Proceedings of the 19th ACM International Conference on Multimodal Interaction, ser. ICMI 2017. New York, NY, USA: ACM, 2017, pp. 216–220.
# https://dl.acm.org/citation.cfm?id=3136817
#
# https://arxiv.org/abs/1706.00527
# @inproceedings{TerryUm_ICMI2017,
# author = {Um, Terry T. and Pfister, Franz M. J. and Pichler, Daniel and Endo, Satoshi and Lang, Muriel and Hirche, Sandra and Fietzek, Urban and Kuli\'{c}, Dana},
# title = {Data Augmentation of Wearable Sensor Data for Parkinson's Disease Monitoring Using Convolutional Neural Networks},
# booktitle = {Proceedings of the 19th ACM International Conference on Multimodal Interaction},
# series = {ICMI 2017},
# year = {2017},
# isbn = {978-1-4503-5543-8},
# location = {Glasgow, UK},
# pages = {216--220},
# numpages = {5},
# doi = {10.1145/3136755.3136817},
# acmid = {3136817},
# publisher = {ACM},
# address = {New York, NY, USA},
# keywords = {Parkinson\&\#39;s disease, convolutional neural networks, data augmentation, health monitoring, motor state detection, wearable sensor},
# }
# #### You can freely modify this code for your own purpose. However, please leave the above citation information untouched when you redistributed the code to others. Please contact me via email if you have any questions. Your contributions on the code are always welcome. Thank you.
# Terry Taewoong Um ([email protected])
#
# https://twitter.com/TerryUm_ML
#
# https://github.com/terryum/Data-Augmentation-For-Wearable-Sensor-Data
# In[1]:
import numpy as np
import matplotlib.pyplot as plt
# In[2]:
from scipy.interpolate import CubicSpline # for warping
from transforms3d.axangles import axangle2mat # for rotation
# In[3]:
myX = np.load('X_sample.npy')
print(myX.shape) ## 3600 samples (Time) * 3 features (X,Y,Z)
# Make sure that your data are asssume to have (time*feat) dimension.
# In[4]:
plt.plot(myX)
plt.title("An example of 1min acceleration data")
plt.axis([0,3600,-1.5,1.5])
# ## 1. Jittering
# #### Hyperparameters : sigma = standard devitation (STD) of the noise
# In[5]:
sigma = 0.05
# In[6]:
def DA_Jitter(X, sigma=0.05):
myNoise = np.random.normal(loc=0, scale=sigma, size=X.shape)
return X+myNoise
# In[7]:
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DA_Jitter(myX, sigma))
ax.set_xlim([0,3600])
ax.set_ylim([-1.5,1.5])
# ## 2. Scaling
# #### Hyperparameters : sigma = STD of the zoom-in/out factor
# In[8]:
sigma = 0.1
# In[9]:
def DA_Scaling(X, sigma=0.1):
scalingFactor = np.random.normal(loc=1.0, scale=sigma, size=(1,X.shape[1])) # shape=(1,3)
myNoise = np.matmul(np.ones((X.shape[0],1)), scalingFactor)
return X*myNoise
# In[10]:
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DA_Scaling(myX, sigma))
ax.set_xlim([0,3600])
ax.set_ylim([-1.5,1.5])
# ## 3. Magnitude Warping
# #### Hyperparameters : sigma = STD of the random knots for generating curves
#
# #### knot = # of knots for the random curves (complexity of the curves)
# "Scaling" can be considered as "applying constant noise to the entire samples" whereas "Jittering" can be considered as "applying different noise to each sample".
# "Magnitude Warping" can be considered as "applying smoothly-varing noise to the entire samples"
# In[11]:
sigma = 0.2
knot = 4
# In[12]:
## This example using cubic splice is not the best approach to generate random curves.
## You can use other aprroaches, e.g., Gaussian process regression, Bezier curve, etc.
def GenerateRandomCurves(X, sigma=0.2, knot=4):
xx = (np.ones((X.shape[1],1))*(np.arange(0,X.shape[0], (X.shape[0]-1)/(knot+1)))).transpose()
yy = np.random.normal(loc=1.0, scale=sigma, size=(knot+2, X.shape[1]))
x_range = np.arange(X.shape[0])
cs_x = CubicSpline(xx[:,0], yy[:,0])
cs_y = CubicSpline(xx[:,1], yy[:,1])
cs_z = CubicSpline(xx[:,2], yy[:,2])
return np.array([cs_x(x_range),cs_y(x_range),cs_z(x_range)]).transpose()
# In[13]:
## Random curves around 1.0
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(GenerateRandomCurves(myX, sigma))
plt.axis([0,3600,-0.5,2])
# In[14]:
def DA_MagWarp(X, sigma):
return X * GenerateRandomCurves(X, sigma)
# In[15]:
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DA_MagWarp(myX, sigma))
ax.set_xlim([0,3600])
ax.set_ylim([-1.5,1.5])
# ## 4. Time Warping
# #### Hyperparameters : sigma = STD of the random knots for generating curves
#
# #### knot = # of knots for the random curves (complexity of the curves)
# In[16]:
sigma = 0.2
knot = 4
# In[17]:
def DistortTimesteps(X, sigma=0.2):
tt = GenerateRandomCurves(X, sigma) # Regard these samples aroun 1 as time intervals
tt_cum = np.cumsum(tt, axis=0) # Add intervals to make a cumulative graph
# Make the last value to have X.shape[0]
t_scale = [(X.shape[0]-1)/tt_cum[-1,0],(X.shape[0]-1)/tt_cum[-1,1],(X.shape[0]-1)/tt_cum[-1,2]]
tt_cum[:,0] = tt_cum[:,0]*t_scale[0]
tt_cum[:,1] = tt_cum[:,1]*t_scale[1]
tt_cum[:,2] = tt_cum[:,2]*t_scale[2]
return tt_cum
# In[18]:
## Random curves around 1.0
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DistortTimesteps(myX, sigma))
ax.set_xlim([0,3600])
ax.set_ylim([0,3600])
# In[19]:
def DA_TimeWarp(X, sigma=0.2):
tt_new = DistortTimesteps(X, sigma)
X_new = np.zeros(X.shape)
x_range = np.arange(X.shape[0])
X_new[:,0] = np.interp(x_range, tt_new[:,0], X[:,0])
X_new[:,1] = np.interp(x_range, tt_new[:,1], X[:,1])
X_new[:,2] = np.interp(x_range, tt_new[:,2], X[:,2])
return X_new
# In[20]:
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DA_TimeWarp(myX, sigma))
ax.set_xlim([0,3600])
ax.set_ylim([-1.5,1.5])
# ## 5. Rotation
# #### Hyperparameters : N/A
# In[21]:
def DA_Rotation(X):
axis = np.random.uniform(low=-1, high=1, size=X.shape[1])
angle = np.random.uniform(low=-np.pi, high=np.pi)
return np.matmul(X , axangle2mat(axis,angle))
# In[22]:
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DA_Rotation(myX))
ax.set_xlim([0,3600])
ax.set_ylim([-1.5,1.5])
# ## 6. Permutation
# #### Hyperparameters : nPerm = # of segments to permute
# #### minSegLength = allowable minimum length for each segment
# In[23]:
nPerm = 4
minSegLength = 100
# In[24]:
def DA_Permutation(X, nPerm=4, minSegLength=10):
X_new = np.zeros(X.shape)
idx = np.random.permutation(nPerm)
bWhile = True
while bWhile == True:
segs = np.zeros(nPerm+1, dtype=int)
segs[1:-1] = np.sort(np.random.randint(minSegLength, X.shape[0]-minSegLength, nPerm-1))
segs[-1] = X.shape[0]
if np.min(segs[1:]-segs[0:-1]) > minSegLength:
bWhile = False
pp = 0
for ii in range(nPerm):
x_temp = X[segs[idx[ii]]:segs[idx[ii]+1],:]
X_new[pp:pp+len(x_temp),:] = x_temp
pp += len(x_temp)
return(X_new)
# In[25]:
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DA_Permutation(myX))
ax.set_xlim([0,3600])
ax.set_ylim([-1.5,1.5])
# ## 7. Random Sampling
# #### Hyperparameters : nSample = # of subsamples (nSample <= X.shape[0])
# This approach is similar to TimeWarp, but will use only subsamples (not all samples) for interpolation. (Using TimeWarp is more recommended)
# In[26]:
nSample = 1000
# In[27]:
def RandSampleTimesteps(X, nSample=1000):
X_new = np.zeros(X.shape)
tt = np.zeros((nSample,X.shape[1]), dtype=int)
tt[1:-1,0] = np.sort(np.random.randint(1,X.shape[0]-1,nSample-2))
tt[1:-1,1] = np.sort(np.random.randint(1,X.shape[0]-1,nSample-2))
tt[1:-1,2] = np.sort(np.random.randint(1,X.shape[0]-1,nSample-2))
tt[-1,:] = X.shape[0]-1
return tt
# In[28]:
def DA_RandSampling(X, nSample=1000):
tt = RandSampleTimesteps(X, nSample)
X_new = np.zeros(X.shape)
X_new[:,0] = np.interp(np.arange(X.shape[0]), tt[:,0], X[tt[:,0],0])
X_new[:,1] = np.interp(np.arange(X.shape[0]), tt[:,1], X[tt[:,1],1])
X_new[:,2] = np.interp(np.arange(X.shape[0]), tt[:,2], X[tt[:,2],2])
return X_new
# In[29]:
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DA_RandSampling(myX))
ax.set_xlim([0,3600])
ax.set_ylim([-1.5,1.5])
# ## 8. Combinations
# You can combine any of the above data augmentation techiniques. For example,
# In[30]:
## Rotation + Permutation
fig = plt.figure(figsize=(15,4))
for ii in range(8):
ax = fig.add_subplot(2,4,ii+1)
ax.plot(DA_Rotation(DA_Permutation(myX, nPerm=4)))
ax.set_xlim([0,3600])
ax.set_ylim([-1.5,1.5])