1 | #! /usr/bin/env python |
---|
2 | |
---|
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 |
---|
8 | |
---|
9 | wrong_type = 'float32' if float_type == 'float64' else 'float64' |
---|
10 | |
---|
11 | default_size = 512 |
---|
12 | |
---|
13 | class aubio_fvec_test_case(TestCase): |
---|
14 | |
---|
15 | def test_vector_created_with_zeroes(self): |
---|
16 | a = fvec(10) |
---|
17 | assert a.dtype == float_type |
---|
18 | assert a.shape == (10,) |
---|
19 | assert_equal (a, 0) |
---|
20 | |
---|
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) |
---|
26 | |
---|
27 | def test_vector_assign_element(self): |
---|
28 | a = fvec(default_size) |
---|
29 | a[0] = 1 |
---|
30 | assert_equal(a[0], 1) |
---|
31 | |
---|
32 | def test_vector_assign_element_end(self): |
---|
33 | a = fvec(default_size) |
---|
34 | a[-1] = 1 |
---|
35 | assert_equal(a[-1], 1) |
---|
36 | assert_equal(a[len(a)-1], 1) |
---|
37 | |
---|
38 | def test_vector(self): |
---|
39 | a = fvec() |
---|
40 | a, len(a) #a.length |
---|
41 | a[0] |
---|
42 | array(a) |
---|
43 | a = fvec(10) |
---|
44 | a = fvec(1) |
---|
45 | a.T |
---|
46 | array(a).T |
---|
47 | a = list(range(len(a))) |
---|
48 | |
---|
49 | def test_wrong_values(self): |
---|
50 | self.assertRaises (ValueError, fvec, -10) |
---|
51 | |
---|
52 | a = fvec(2) |
---|
53 | self.assertRaises (IndexError, a.__getitem__, 3) |
---|
54 | self.assertRaises (IndexError, a.__getitem__, 2) |
---|
55 | |
---|
56 | def test_alpha_norm_of_fvec(self): |
---|
57 | a = fvec(2) |
---|
58 | self.assertEqual (alpha_norm(a, 1), 0) |
---|
59 | a[0] = 1 |
---|
60 | self.assertEqual (alpha_norm(a, 1), 0.5) |
---|
61 | a[1] = 1 |
---|
62 | self.assertEqual (alpha_norm(a, 1), 1) |
---|
63 | a = array([0, 1], dtype=float_type) |
---|
64 | from math import sqrt |
---|
65 | assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.) |
---|
66 | |
---|
67 | def test_alpha_norm_of_none(self): |
---|
68 | self.assertRaises (ValueError, alpha_norm, None, 1) |
---|
69 | |
---|
70 | def test_alpha_norm_of_array_of_float32(self): |
---|
71 | # check scalar fails |
---|
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) |
---|
77 | # check 1d array |
---|
78 | a = array(range(10), dtype = float_type) |
---|
79 | self.assertEqual (alpha_norm(a, 1), 4.5) |
---|
80 | |
---|
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) |
---|
88 | |
---|
89 | def test_alpha_norm_of_array_of_string (self): |
---|
90 | a = "hello" |
---|
91 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
92 | |
---|
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 ) |
---|
102 | |
---|
103 | def test_alpha_norm_of_array_of_float64(self): |
---|
104 | # check scalar fail |
---|
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) |
---|
116 | |
---|
117 | def test_fvec_min_removal_of_array(self): |
---|
118 | a = array([20,1,19], dtype=float_type) |
---|
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 | a[0] = 0 |
---|
124 | assert_equal (a, b) |
---|
125 | |
---|
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) |
---|
129 | |
---|
130 | def test_fvec_min_removal_of_fvec(self): |
---|
131 | a = fvec(3) |
---|
132 | a = array([20, 1, 19], dtype = float_type) |
---|
133 | b = min_removal(a) |
---|
134 | assert_equal (array(b), [19, 0, 18]) |
---|
135 | assert_equal (b, [19, 0, 18]) |
---|
136 | assert_equal (a, b) |
---|
137 | |
---|
138 | if __name__ == '__main__': |
---|
139 | from unittest import main |
---|
140 | main() |
---|