3 from numpy.testing import TestCase, run_module_suite
4 from numpy.testing import assert_equal, assert_almost_equal
5 from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
6 from aubio import float_type
7 from numpy import array, shape
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):
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 a = list(range(len(a)))
49 def test_wrong_values(self):
50 self.assertRaises (ValueError, fvec, -10)
53 self.assertRaises (IndexError, a.__getitem__, 3)
54 self.assertRaises (IndexError, a.__getitem__, 2)
56 def test_alpha_norm_of_fvec(self):
58 self.assertEqual (alpha_norm(a, 1), 0)
60 self.assertEqual (alpha_norm(a, 1), 0.5)
62 self.assertEqual (alpha_norm(a, 1), 1)
63 a = array([0, 1], dtype=float_type)
65 assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.)
67 def test_alpha_norm_of_none(self):
68 self.assertRaises (ValueError, alpha_norm, None, 1)
70 def test_alpha_norm_of_array_of_float32(self):
72 a = array(1, dtype = float_type)
73 self.assertRaises (ValueError, alpha_norm, a, 1)
74 # check 2d array fails
75 a = array([[2],[4]], dtype = float_type)
76 self.assertRaises (ValueError, alpha_norm, a, 1)
78 a = array(range(10), dtype = float_type)
79 self.assertEqual (alpha_norm(a, 1), 4.5)
81 def test_alpha_norm_of_array_of_int(self):
82 a = array(1, dtype = 'int')
83 self.assertRaises (ValueError, alpha_norm, a, 1)
84 a = array([[[1,2],[3,4]]], dtype = 'int')
85 self.assertRaises (ValueError, alpha_norm, a, 1)
86 a = array(range(10), dtype = 'int')
87 self.assertRaises (ValueError, alpha_norm, a, 1)
89 def test_alpha_norm_of_array_of_string (self):
91 self.assertRaises (ValueError, alpha_norm, a, 1)
93 def test_zero_crossing_rate(self):
94 a = array([0,1,-1], dtype=float_type)
95 assert_almost_equal (zero_crossing_rate(a), 1./3. )
96 a = array([0.]*100, dtype=float_type)
97 self.assertEqual (zero_crossing_rate(a), 0 )
98 a = array([-1.]*100, dtype=float_type)
99 self.assertEqual (zero_crossing_rate(a), 0 )
100 a = array([1.]*100, dtype=float_type)
101 self.assertEqual (zero_crossing_rate(a), 0 )
103 def test_alpha_norm_of_array_of_float64(self):
105 a = array(1, dtype = wrong_type)
106 self.assertRaises (ValueError, alpha_norm, a, 1)
107 # check 3d array fail
108 a = array([[[1,2],[3,4]]], dtype = wrong_type)
109 self.assertRaises (ValueError, alpha_norm, a, 1)
110 # check float64 1d array fail
111 a = array(list(range(10)), dtype = wrong_type)
112 self.assertRaises (ValueError, alpha_norm, a, 1)
113 # check float64 2d array fail
114 a = array([list(range(10)), list(range(10))], dtype = wrong_type)
115 self.assertRaises (ValueError, alpha_norm, a, 1)
117 def test_fvec_min_removal_of_array(self):
118 a = array([20,1,19], dtype=float_type)
120 assert_equal (array(b), [19, 0, 18])
121 assert_equal (b, [19, 0, 18])
126 def test_fvec_min_removal_of_array_float64(self):
127 a = array([20,1,19], dtype=wrong_type)
128 self.assertRaises (ValueError, min_removal, a)
130 def test_fvec_min_removal_of_fvec(self):
132 a = array([20, 1, 19], dtype = float_type)
134 assert_equal (array(b), [19, 0, 18])
135 assert_equal (b, [19, 0, 18])
138 def test_pass_to_numpy(self):
150 if __name__ == '__main__':
151 from unittest import main