Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_trim_zeros_frames():
arr = np.array(((0, 0), (0, 0), (1, 1), (2, 2), (0, 0)))
desired_default = np.array(((0, 0), (0, 0), (1, 1), (2, 2)))
actual_default = trim_zeros_frames(arr)
assert desired_default.shape[1] == actual_default.shape[1]
np.testing.assert_array_equal(actual_default, desired_default)
desired_b = np.array(((0, 0), (0, 0), (1, 1), (2, 2)))
actual_b = trim_zeros_frames(arr, trim='b')
assert desired_b.shape[1] == actual_b.shape[1]
np.testing.assert_array_equal(actual_b, desired_b)
desired_f = np.array(((1, 1), (2, 2), (0, 0)))
actual_f = trim_zeros_frames(arr, trim='f')
assert desired_f.shape[1] == actual_f.shape[1]
np.testing.assert_array_equal(actual_f, desired_f)
desired_fb = np.array(((1, 1), (2, 2)))
actual_fb = trim_zeros_frames(arr, trim='fb')
assert desired_fb.shape[1] == actual_fb.shape[1]
np.testing.assert_array_equal(actual_fb, desired_fb)
non_zeros = np.array(((1, 1), (2, 2), (3, 3), (4, 4), (5, 5)))
desired_b_or_fb_non_zeros = np.array(((1, 1), (2, 2), (3, 3), (4, 4), (5, 5)))
actual_b = trim_zeros_frames(non_zeros, trim='b')
np.testing.assert_array_equal(actual_b, desired_b_or_fb_non_zeros)
actual_fb = trim_zeros_frames(non_zeros, trim='fb')
np.testing.assert_array_equal(actual_default, desired_default)
desired_b = np.array(((0, 0), (0, 0), (1, 1), (2, 2)))
actual_b = trim_zeros_frames(arr, trim='b')
assert desired_b.shape[1] == actual_b.shape[1]
np.testing.assert_array_equal(actual_b, desired_b)
desired_f = np.array(((1, 1), (2, 2), (0, 0)))
actual_f = trim_zeros_frames(arr, trim='f')
assert desired_f.shape[1] == actual_f.shape[1]
np.testing.assert_array_equal(actual_f, desired_f)
desired_fb = np.array(((1, 1), (2, 2)))
actual_fb = trim_zeros_frames(arr, trim='fb')
assert desired_fb.shape[1] == actual_fb.shape[1]
np.testing.assert_array_equal(actual_fb, desired_fb)
non_zeros = np.array(((1, 1), (2, 2), (3, 3), (4, 4), (5, 5)))
desired_b_or_fb_non_zeros = np.array(((1, 1), (2, 2), (3, 3), (4, 4), (5, 5)))
actual_b = trim_zeros_frames(non_zeros, trim='b')
np.testing.assert_array_equal(actual_b, desired_b_or_fb_non_zeros)
actual_fb = trim_zeros_frames(non_zeros, trim='fb')
np.testing.assert_array_equal(actual_fb, desired_b_or_fb_non_zeros)
def collect_features(self, path):
fs, x = wavfile.read(path)
x = x.astype(np.float64)
f0, timeaxis = pyworld.dio(x, fs, frame_period=5)
f0 = pyworld.stonemask(x, f0, timeaxis, fs)
spectrogram = pyworld.cheaptrick(x, f0, timeaxis, fs)
spectrogram = trim_zeros_frames(spectrogram)
mc = pysptk.sp2mc(spectrogram, order=24, alpha=self.alpha)
return mc.astype(np.float32)
def test_trim_remove_zeros_frames():
fs, x = wavfile.read(example_audio_file())
frame_period = 5
x = x.astype(np.float64)
f0, timeaxis = pyworld.dio(x, fs, frame_period=frame_period)
spectrogram = pyworld.cheaptrick(x, f0, timeaxis, fs)
aperiodicity = pyworld.d4c(x, f0, timeaxis, fs)
for mat in [spectrogram, aperiodicity]:
trimmed = trim_zeros_frames(mat)
assert trimmed.shape[1] == mat.shape[1]
for mat in [spectrogram, aperiodicity]:
trimmed = remove_zeros_frames(mat)
assert trimmed.shape[1] == mat.shape[1]
def collect_features(self, path):
fs, x = wavfile.read(path)
assert fs == 48000
x = x.astype(np.float64)
f0, timeaxis = pyworld.dio(x, fs, frame_period=5)
f0 = pyworld.stonemask(x, f0, timeaxis, fs)
spectrogram = pyworld.cheaptrick(x, f0, timeaxis, fs)
spectrogram = trim_zeros_frames(spectrogram)
mc = pysptk.sp2mc(spectrogram, order=24, alpha=self.alpha)
return mc.astype(np.float32)
def test_trim_zeros_frames():
arr = np.array(((0, 0), (0, 0), (1, 1), (2, 2), (0, 0)))
desired_default = np.array(((0, 0), (0, 0), (1, 1), (2, 2)))
actual_default = trim_zeros_frames(arr)
assert desired_default.shape[1] == actual_default.shape[1]
np.testing.assert_array_equal(actual_default, desired_default)
desired_b = np.array(((0, 0), (0, 0), (1, 1), (2, 2)))
actual_b = trim_zeros_frames(arr, trim='b')
assert desired_b.shape[1] == actual_b.shape[1]
np.testing.assert_array_equal(actual_b, desired_b)
desired_f = np.array(((1, 1), (2, 2), (0, 0)))
actual_f = trim_zeros_frames(arr, trim='f')
assert desired_f.shape[1] == actual_f.shape[1]
np.testing.assert_array_equal(actual_f, desired_f)
def collect_features(self, path):
fs, x = wavfile.read(path)
assert fs == 48000
x = x.astype(np.float64)
f0, timeaxis = pyworld.dio(x, fs, frame_period=5)
f0 = pyworld.stonemask(x, f0, timeaxis, fs)
spectrogram = pyworld.cheaptrick(x, f0, timeaxis, fs)
spectrogram = trim_zeros_frames(spectrogram)
mc = pysptk.sp2mc(spectrogram, order=24, alpha=self.alpha)
return mc.astype(np.float32)
assert desired_f.shape[1] == actual_f.shape[1]
np.testing.assert_array_equal(actual_f, desired_f)
desired_fb = np.array(((1, 1), (2, 2)))
actual_fb = trim_zeros_frames(arr, trim='fb')
assert desired_fb.shape[1] == actual_fb.shape[1]
np.testing.assert_array_equal(actual_fb, desired_fb)
non_zeros = np.array(((1, 1), (2, 2), (3, 3), (4, 4), (5, 5)))
desired_b_or_fb_non_zeros = np.array(((1, 1), (2, 2), (3, 3), (4, 4), (5, 5)))
actual_b = trim_zeros_frames(non_zeros, trim='b')
np.testing.assert_array_equal(actual_b, desired_b_or_fb_non_zeros)
actual_fb = trim_zeros_frames(non_zeros, trim='fb')
np.testing.assert_array_equal(actual_fb, desired_b_or_fb_non_zeros)
X_aligned[idx][:len(x)] = x
Y_aligned[idx][:len(y)] = y
if self.verbose > 0:
print("{}, distance: {}".format(idx, dist))
# Fit
gmm = GaussianMixture(
n_components=self.n_components_gmm,
covariance_type="full", max_iter=self.max_iter_gmm)
XY = np.concatenate((X_aligned, Y_aligned),
axis=-1).reshape(-1, X.shape[-1] * 2)
gmm.fit(XY)
windows = [(0, 0, np.array([1.0]))] # no delta
paramgen = MLPG(gmm, windows=windows)
for idx in range(len(Xc)):
x = trim_zeros_frames(Xc[idx])
Xc[idx][:len(x)] = paramgen.transform(x)
# Finally we can get aligned X
for idx in range(len(X_aligned)):
x = X[idx][refined_paths[idx]]
X_aligned[idx][:len(x)] = x
return X_aligned, Y_aligned
def transform(self, XY):
X, Y = XY
assert X.ndim == 3 and Y.ndim == 3
longer_features = X if X.shape[1] > Y.shape[1] else Y
X_aligned = np.zeros_like(longer_features)
Y_aligned = np.zeros_like(longer_features)
for idx, (x, y) in enumerate(zip(X, Y)):
x, y = trim_zeros_frames(x), trim_zeros_frames(y)
dist, path = fastdtw(x, y, radius=self.radius, dist=self.dist)
dist /= (len(x) + len(y))
pathx = list(map(lambda l: l[0], path))
pathy = list(map(lambda l: l[1], path))
x, y = x[pathx], y[pathy]
max_len = max(len(x), len(y))
if max_len > X_aligned.shape[1] or max_len > Y_aligned.shape[1]:
pad_size = max(max_len - X_aligned.shape[1],
max_len > Y_aligned.shape[1])
X_aligned = np.pad(
X_aligned, [(0, 0), (0, pad_size), (0, 0)],
mode="constant", constant_values=0)
Y_aligned = np.pad(
Y_aligned, [(0, 0), (0, pad_size), (0, 0)],
mode="constant", constant_values=0)
X_aligned[idx][:len(x)] = x