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