-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.py
53 lines (43 loc) · 1.74 KB
/
transform.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
# The Tonnetz-Cad dataset | 2021
# Emmanouil Karystinaios
import numpy as np
import scipy
from scipy.ndimage.filters import gaussian_filter
from scipy.interpolate import interp1d
# transformations of the templates which will make them harder to classify
def pad(x, padding):
low, high = padding
p = low + int(np.random.rand()*(high-low+1))
return np.concatenate([x, np.zeros((p))])
def shear(x, scale=10):
coeff = scale*(np.random.rand() - 0.5)
return x - coeff*np.linspace(-0.5,.5,len(x))
def translate(x, max_translation):
k = np.random.choice(max_translation)
return np.concatenate([x[-k:], x[:-k]])
def corr_noise_like(x, scale):
noise = scale * np.random.randn(*x.shape)
return gaussian_filter(noise, 2)
def iid_noise_like(x, scale):
noise = scale * np.random.randn(*x.shape)
return noise
def interpolate(x, N):
scale = np.linspace(0,1,len(x))
new_scale = np.linspace(0,1,N)
new_x = interp1d(scale, x, axis=0, kind='linear')(new_scale)
return new_x
def transform(x, y, args, eps=1e-8):
new_x = pad(x+eps, args.padding) # pad
new_x = interpolate(new_x, args.template_len + args.padding[-1]) # dilate
new_y = interpolate(y, args.template_len + args.padding[-1])
new_x *= (1 + args.scale_coeff*(np.random.rand() - 0.5)) # scale
new_x = translate(new_x, args.max_translation) #translate
# add noise
mask = new_x != 0
new_x = mask*new_x + (1-mask)*corr_noise_like(new_x, args.corr_noise_scale)
new_x = new_x + iid_noise_like(new_x, args.iid_noise_scale)
# shear and interpolate
new_x = shear(new_x, args.shear_scale)
new_x = interpolate(new_x, args.final_seq_length) # subsample
new_y = interpolate(new_y, args.final_seq_length)
return new_x, new_y