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