3 from unittest import main
5 from numpy.testing import TestCase, assert_equal, assert_almost_equal
6 from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
7 from aubio import float_type
9 wrong_type = 'float32' if float_type == 'float64' else 'float64'
13 class aubio_fvec_test_case(TestCase):
15 def test_vector_created_with_zeroes(self):
17 assert a.dtype == float_type
18 assert a.shape == (10,)
21 def test_vector_create_with_list(self):
22 a = fvec([0, 1, 2, 3])
23 assert a.dtype == float_type
24 assert a.shape == (4,)
25 assert_equal(list(range(4)), a)
27 def test_vector_assign_element(self):
28 a = fvec(default_size)
32 def test_vector_assign_element_end(self):
33 a = fvec(default_size)
35 assert_equal(a[-1], 1)
36 assert_equal(a[len(a)-1], 1)
38 def test_vector(self):
47 class aubio_fvec_wrong_values(TestCase):
49 def test_negative_length(self):
50 """ test creating fvec with negative length fails (pure python) """
51 self.assertRaises(ValueError, fvec, -10)
53 def test_zero_length(self):
54 """ test creating fvec with zero length fails (pure python) """
55 self.assertRaises(ValueError, fvec, 0)
57 def test_out_of_bound(self):
58 """ test assiging fvec out of bounds fails (pure python) """
60 self.assertRaises(IndexError, a.__getitem__, 3)
61 self.assertRaises(IndexError, a.__getitem__, 2)
63 def test_wrong_dimensions(self):
64 a = np.array([[[1, 2], [3, 4]]], dtype=float_type)
65 self.assertRaises(ValueError, fvec, a)
67 def test_wrong_size(self):
68 a = np.ndarray([0,], dtype=float_type)
69 self.assertRaises(ValueError, fvec, a)
71 class aubio_wrong_fvec_input(TestCase):
72 """ uses min_removal to test PyAubio_IsValidVector """
74 def test_no_input(self):
75 self.assertRaises(TypeError, min_removal)
78 self.assertRaises(ValueError, min_removal, None)
80 def test_wrong_scalar(self):
81 a = np.array(10, dtype=float_type)
82 self.assertRaises(ValueError, min_removal, a)
84 def test_wrong_dimensions(self):
85 a = np.array([[[1, 2], [3, 4]]], dtype=float_type)
86 self.assertRaises(ValueError, min_removal, a)
88 def test_wrong_array_size(self):
89 x = np.array([], dtype=float_type)
90 self.assertRaises(ValueError, min_removal, x)
92 def test_wrong_type(self):
93 a = np.zeros(10, dtype=wrong_type)
94 self.assertRaises(ValueError, min_removal, a)
96 def test_wrong_list_input(self):
97 self.assertRaises(ValueError, min_removal, [0., 1.])
99 def test_good_input(self):
100 a = np.zeros(10, dtype=float_type)
101 assert_equal(np.zeros(10, dtype=float_type), min_removal(a))
103 class aubio_alpha_norm(TestCase):
105 def test_alpha_norm_of_random(self):
106 x = np.random.rand(1024).astype(float_type)
107 alpha = np.random.rand() * 5.
108 x_alpha_norm = (np.sum(np.abs(x)**alpha)/len(x))**(1/alpha)
109 assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm, decimal = 4)
111 class aubio_zero_crossing_rate_test(TestCase):
113 def test_zero_crossing_rate(self):
114 a = np.array([0, 1, -1], dtype=float_type)
115 assert_almost_equal(zero_crossing_rate(a), 1./3.)
117 def test_zero_crossing_rate_zeros(self):
118 a = np.zeros(100, dtype=float_type)
119 self.assertEqual(zero_crossing_rate(a), 0)
121 def test_zero_crossing_rate_minus_ones(self):
122 a = np.ones(100, dtype=float_type)
123 self.assertEqual(zero_crossing_rate(a), 0)
125 def test_zero_crossing_rate_plus_ones(self):
126 a = np.ones(100, dtype=float_type)
127 self.assertEqual(zero_crossing_rate(a), 0)
129 class aubio_fvec_min_removal(TestCase):
131 def test_fvec_min_removal_of_array(self):
132 a = np.array([20, 1, 19], dtype=float_type)
134 assert_equal(b, [19, 0, 18])
136 class aubio_fvec_test_memory(TestCase):
138 def test_pass_to_numpy(self):
150 if __name__ == '__main__':