-
Notifications
You must be signed in to change notification settings - Fork 8
/
transforms.py
207 lines (125 loc) · 5.2 KB
/
transforms.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
import numpy as np
import random
class _BaseTransform:
def __init__(self, p):
"""
p: Probability of applying transform.
"""
assert 0 <= p <= 1
self.p = p
def apply(self, signals):
raise NotImplementedError
def __call__(self, signals):
if random.random() < self.p:
signals = self.apply(signals)
return signals
class Compose:
def __init__(self, transforms, p=1):
"""
transforms: List of transforms to apply to signals.
p: Probability of applying the list of transforms.
"""
assert 0 <= p <= 1
self.transforms = transforms
self.p = p
def __call__(self, signals):
if random.random() < self.p:
for transform in self.transforms:
signals = transform(signals)
return signals
class FlipTime(_BaseTransform):
"""Randomly flip signals along temporal dimension"""
def __init__(self, p=0.5):
"""
p: Probability of applying transform.
"""
super().__init__(p)
def apply(self, signals):
if len(signals.shape) > 1:
signals = np.fliplr(signals)
else:
signals = np.flipud(signals)
return signals
class MaskTime(_BaseTransform):
"""Randomly mask signal"""
def __init__(self, min_fraction=0.0, max_fraction=0.5, p=0.5):
"""
min_fraction: Minimum length of the mask as a fraction of the total time series length.
max_fraction: Maximum length of the mask as a fraction of the total time series length.
p: Probability of applying transform.
"""
super().__init__(p)
assert 0 <= min_fraction <= 1
assert 0 <= max_fraction <= 1
assert max_fraction >= min_fraction
self.min_fraction = min_fraction
self.max_fraction = max_fraction
def apply(self, signals):
num_samples = signals.shape[-1]
length = random.randint(int(num_samples * self.min_fraction), int(num_samples * self.max_fraction))
start = random.randint(0, num_samples - length)
mask = np.zeros(length)
masked_signals = signals.copy()
masked_signals[..., start : start + length] *= mask
return masked_signals
class Shift(_BaseTransform):
"""Shift the signals forwards or backwards along the temporal dimension"""
def __init__(self, min_fraction=-0.5, max_fraction=0.5, rollover=True, p=0.5):
"""
min_fraction: Fraction of total timeseries to shift.
max_fraction: Fraction of total timeseries to shift.
rollover: Samples that roll beyond the first or last position are re-introduced at the last or first otherwise set to zero.
p: Probability of applying this transform.
"""
super().__init__(p)
assert min_fraction >= -1
assert max_fraction <= 1
self.min_fraction = min_fraction
self.max_fraction = max_fraction
self.rollover = rollover
def apply(self, signals):
num_samples = signals.shape[-1]
num_shift = int(round(random.uniform(self.min_fraction, self.max_fraction) * num_samples))
signals = np.roll(signals, num_shift, axis=-1)
if not self.rollover:
if num_shift > 0:
signals[..., :num_shift] = 0.0
elif num_shift < 0:
signals[..., num_shift:] = 0.0
return signals
class FlipPolarity(_BaseTransform):
"""Randomly flip sign of signal"""
def __init__(self, p=0.5):
"""
p: Probability of applying transform.
"""
super().__init__(p)
def apply(self, signals):
return -signals
class GuassianNoise(_BaseTransform):
"""Add gaussian noise to the signals"""
def __init__(self, min_amplitude=0.001, max_amplitude=0.015, p=0.5):
"""
min_amplitude: minimum amplitude of noise.
max_amplitude: maximum amplitude of noise.
p: Probability of applying this transform.
"""
super().__init__(p)
assert min_amplitude > 0.0
assert max_amplitude > 0.0
assert max_amplitude >= min_amplitude
self.min_amplitude = min_amplitude
self.max_amplitude = max_amplitude
def apply(self, signals):
amplitude = random.uniform(self.min_amplitude, self.max_amplitude)
noise = np.random.randn(*signals.shape).astype(np.float32)
signals = signals + amplitude * noise
return signals
if __name__ == "__main__":
# generate multivariate time series: 250 features, 1000 length
signals = np.random.uniform(low=-0.2, high=0.2, size=(250, 1000,)).astype(np.float32)
# compose a list of transforms
augment = Compose([FlipTime(p=0.5), FlipPolarity(p=0.5), GuassianNoise(p=0.5), Shift(p=0.5), MaskTime(p=0.5)])
# augment signals
augmented_signals = augment(signals)
assert not np.allclose(signals, augmented_signals)