1 | from numpy.testing import TestCase, run_module_suite |
---|
2 | from numpy.testing import assert_equal, assert_almost_equal |
---|
3 | from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal |
---|
4 | from numpy import array, shape |
---|
5 | |
---|
6 | class aubio_fvec_test_case(TestCase): |
---|
7 | |
---|
8 | def test_vector_created_with_zeroes(self): |
---|
9 | a = fvec(10) |
---|
10 | a |
---|
11 | shape(a) |
---|
12 | a[0] |
---|
13 | #del a |
---|
14 | assert_equal(array(a), 0.) |
---|
15 | |
---|
16 | def test_vector_assign_element(self): |
---|
17 | a = fvec() |
---|
18 | a[0] = 1 |
---|
19 | assert_equal(a[0], 1) |
---|
20 | |
---|
21 | def test_vector_assign_element_end(self): |
---|
22 | a = fvec() |
---|
23 | a[-1] = 1 |
---|
24 | assert_equal(a[-1], 1) |
---|
25 | assert_equal(a[len(a)-1], 1) |
---|
26 | |
---|
27 | def test_vector(self): |
---|
28 | a = fvec() |
---|
29 | a, len(a) #a.length |
---|
30 | a[0] |
---|
31 | array(a) |
---|
32 | a = fvec(10) |
---|
33 | a = fvec(1) |
---|
34 | a.T |
---|
35 | array(a).T |
---|
36 | a = range(len(a)) |
---|
37 | |
---|
38 | def test_wrong_values(self): |
---|
39 | self.assertRaises (ValueError, fvec, -10) |
---|
40 | |
---|
41 | a = fvec(2) |
---|
42 | self.assertRaises (IndexError, a.__getitem__, 3) |
---|
43 | self.assertRaises (IndexError, a.__getitem__, 2) |
---|
44 | |
---|
45 | def test_alpha_norm_of_fvec(self): |
---|
46 | a = fvec(2) |
---|
47 | self.assertEquals (alpha_norm(a, 1), 0) |
---|
48 | a[0] = 1 |
---|
49 | self.assertEquals (alpha_norm(a, 1), 0.5) |
---|
50 | a[1] = 1 |
---|
51 | self.assertEquals (alpha_norm(a, 1), 1) |
---|
52 | a = array([0, 1], dtype='float32') |
---|
53 | from math import sqrt |
---|
54 | assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.) |
---|
55 | |
---|
56 | def test_alpha_norm_of_array_of_float32(self): |
---|
57 | # check scalar fails |
---|
58 | a = array(1, dtype = 'float32') |
---|
59 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
60 | # check 2d array fails |
---|
61 | a = array([[2],[4]], dtype = 'float32') |
---|
62 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
63 | # check 1d array |
---|
64 | a = array(range(10), dtype = 'float32') |
---|
65 | self.assertEquals (alpha_norm(a, 1), 4.5) |
---|
66 | |
---|
67 | def test_alpha_norm_of_array_of_int(self): |
---|
68 | a = array(1, dtype = 'int') |
---|
69 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
70 | a = array([[[1,2],[3,4]]], dtype = 'int') |
---|
71 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
72 | a = array(range(10), dtype = 'int') |
---|
73 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
74 | |
---|
75 | def test_alpha_norm_of_array_of_string (self): |
---|
76 | a = "hello" |
---|
77 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
78 | |
---|
79 | def test_zero_crossing_rate(self): |
---|
80 | a = array([0,1,-1], dtype='float32') |
---|
81 | self.assertEquals (zero_crossing_rate(a), 1./3 ) |
---|
82 | a = array([0.]*100, dtype='float32') |
---|
83 | self.assertEquals (zero_crossing_rate(a), 0 ) |
---|
84 | a = array([-1.]*100, dtype='float32') |
---|
85 | self.assertEquals (zero_crossing_rate(a), 0 ) |
---|
86 | a = array([1.]*100, dtype='float32') |
---|
87 | self.assertEquals (zero_crossing_rate(a), 0 ) |
---|
88 | |
---|
89 | def test_alpha_norm_of_array_of_float64(self): |
---|
90 | # check scalar fail |
---|
91 | a = array(1, dtype = 'float64') |
---|
92 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
93 | # check 3d array fail |
---|
94 | a = array([[[1,2],[3,4]]], dtype = 'float64') |
---|
95 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
96 | # check float64 1d array fail |
---|
97 | a = array(range(10), dtype = 'float64') |
---|
98 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
99 | # check float64 2d array fail |
---|
100 | a = array([range(10), range(10)], dtype = 'float64') |
---|
101 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
102 | |
---|
103 | def test_fvec_min_removal_of_array(self): |
---|
104 | a = array([20,1,19], dtype='float32') |
---|
105 | b = min_removal(a) |
---|
106 | assert_equal (array(b), [19, 0, 18]) |
---|
107 | assert_equal (b, [19, 0, 18]) |
---|
108 | assert_equal (a, b) |
---|
109 | a[0] = 0 |
---|
110 | assert_equal (a, b) |
---|
111 | |
---|
112 | def test_fvec_min_removal_of_array_float64(self): |
---|
113 | a = array([20,1,19], dtype='float64') |
---|
114 | self.assertRaises (ValueError, min_removal, a) |
---|
115 | |
---|
116 | def test_fvec_min_removal_of_fvec(self): |
---|
117 | a = fvec(3) |
---|
118 | a = array([20, 1, 19], dtype = 'float32') |
---|
119 | b = min_removal(a) |
---|
120 | assert_equal (array(b), [19, 0, 18]) |
---|
121 | assert_equal (b, [19, 0, 18]) |
---|
122 | assert_equal (a, b) |
---|
123 | |
---|
124 | if __name__ == '__main__': |
---|
125 | from unittest import main |
---|
126 | main() |
---|