Skip to content

Commit

Permalink
Add more test to transforms (#2)
Browse files Browse the repository at this point in the history
* add more test to transforms

* replace torch and numpy in module by direct import

* add tests for transform correctness
  • Loading branch information
lijunzh authored Apr 14, 2019
1 parent 96b3932 commit 38190c4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
65 changes: 53 additions & 12 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,81 @@
import yews.transforms as transforms
import yews.transforms.functional as F

import torch
import numpy as np

class TestIsNumpyWaveform:
def test_single_channel_waveform_vector(self):
wav = F.np.empty(10)
wav = np.empty(10)
assert F._is_numpy_waveform(wav)

def test_single_channel_waveform_matrix(self):
wav = F.np.empty((1, 10))
wav = np.empty((1, 10))
assert F._is_numpy_waveform(wav)

def test_multi_channel_waveform(self):
wav = F.np.empty((3, 10))
wav = np.empty((3, 10))
assert F._is_numpy_waveform(wav)

def test_invalid_waveform_wrong_dimension(self):
wav = F.np.empty((1,1,10))
wav = np.empty((1,1,10))
assert not F._is_numpy_waveform(wav)

def test_invalid_waveform_wrong_type(self):
wav = F.torch.tensor(10)
wav = torch.tensor(10)
assert not F._is_numpy_waveform(wav)

class TestToTensor:
def test_type_exception(self):
wav = F.torch.tensor(10)
wav = torch.tensor(10)
with pytest.raises(TypeError):
F._to_tensor(wav)

def test_single_channel_waveform(self):
wav = F.np.zeros(10)
tensor = F.torch.zeros(1, 10,dtype=F.torch.float)
assert F.torch.allclose(F._to_tensor(wav), tensor)
wav = np.zeros(10)
tensor = torch.zeros(1, 10,dtype=torch.float)
assert torch.allclose(F._to_tensor(wav), tensor)

def test_multi_channel_waveform(self):
wav = F.np.zeros((3, 10))
tensor = F.torch.zeros(3, 10,dtype=F.torch.float)
assert F.torch.allclose(F._to_tensor(wav), tensor)
wav = np.zeros((3, 10))
tensor = torch.zeros(3, 10,dtype=torch.float)
assert torch.allclose(F._to_tensor(wav), tensor)

class TestMandatoryMethods:
def test_call_method(self):
assert all([hasattr(getattr(transforms, t), '__call__') for t in
transforms.transforms.__all__])

def test_repr_method(self):
assert all([hasattr(getattr(transforms, t), '__repr__') for t in
transforms.transforms.__all__])

class TestTransformCorrectness:
def test_compose(self):
wav = np.array([1, 3])
assert torch.allclose(transforms.Compose([
transforms.ZeroMean(),
transforms.ToTensor(),
])(wav), torch.tensor([[-1, 1]], dtype=torch.float))

def test_to_tensor_dtype(self):
wav = np.array([1])
assert torch.allclose(torch.tensor([[1]], dtype=torch.float),
transforms.ToTensor()(wav))
def test_to_tensor_shape(self):
wav = np.array([1])
assert transforms.ToTensor()(wav).shape == (1,1)

def test_zero_mean_channel_mean(self):
wav = np.random.rand(3, 2000)
assert all([np.allclose(ch.mean(), 0) for ch in transforms.ZeroMean()(wav)])

def test_cut_waveform_shape(self):
wav = np.random.randn(3, 2000)
assert transforms.CutWaveform(100, 1900)(wav).shape == (3, 1800)

def test_cut_waveform_value(self):
wav = np.random.randn(3, 2000)
assert np.allclose(transforms.CutWaveform(100, 1900)(wav),
wav[:, 100:1900])

2 changes: 1 addition & 1 deletion yews/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ZeroMean(object):
"""

def __call__(self, wav):
wav = wav.T
wav = wav.astype(float).T
wav -= wav.mean(axis=0)
return wav.T

Expand Down

0 comments on commit 38190c4

Please sign in to comment.