[146280a] | 1 | from numpy.testing import TestCase, run_module_suite |
---|
| 2 | from numpy.testing import assert_equal as numpy_assert_equal |
---|
[ae6e15c] | 3 | from _aubio import * |
---|
| 4 | from numpy import array |
---|
| 5 | |
---|
[146280a] | 6 | AUBIO_DO_CASTING = 0 |
---|
| 7 | |
---|
| 8 | def assert_equal(a, b): |
---|
| 9 | numpy_assert_equal(array(a),array(b)) |
---|
| 10 | |
---|
| 11 | class aubiomodule_test_case(TestCase): |
---|
[ae6e15c] | 12 | |
---|
| 13 | def setUp(self): |
---|
| 14 | """ try importing aubio """ |
---|
| 15 | |
---|
| 16 | def test_vector(self): |
---|
| 17 | a = fvec() |
---|
| 18 | a.length, a.channels |
---|
| 19 | a[0] |
---|
| 20 | array(a) |
---|
| 21 | a = fvec(10) |
---|
| 22 | a = fvec(1, 2) |
---|
| 23 | array(a).T |
---|
| 24 | a[0] = range(a.length) |
---|
| 25 | a[1][0] = 2 |
---|
| 26 | |
---|
| 27 | def test_wrong_values(self): |
---|
| 28 | self.assertRaises (ValueError, fvec, -10) |
---|
| 29 | self.assertRaises (ValueError, fvec, 1, -1) |
---|
| 30 | |
---|
| 31 | a = fvec(2, 3) |
---|
| 32 | self.assertRaises (IndexError, a.__getitem__, 3) |
---|
| 33 | self.assertRaises (IndexError, a[0].__getitem__, 2) |
---|
| 34 | |
---|
| 35 | def test_alpha_norm_of_fvec(self): |
---|
| 36 | a = fvec(2, 2) |
---|
| 37 | self.assertEquals (alpha_norm(a, 1), 0) |
---|
| 38 | a[0] = [1, 2] |
---|
| 39 | self.assertEquals (alpha_norm(a, 1), 1.5) |
---|
| 40 | a[1] = [1, 2] |
---|
| 41 | self.assertEquals (alpha_norm(a, 1), 3) |
---|
| 42 | a[0] = [0, 1]; a[1] = [1, 0] |
---|
| 43 | self.assertEquals (alpha_norm(a, 2), 1) |
---|
| 44 | |
---|
| 45 | def test_alpha_norm_of_array_of_float32(self): |
---|
[146280a] | 46 | # check scalar fails |
---|
[ae6e15c] | 47 | a = array(1, dtype = 'float32') |
---|
| 48 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
[146280a] | 49 | # check 3d array fails |
---|
[ae6e15c] | 50 | a = array([[[1,2],[3,4]]], dtype = 'float32') |
---|
| 51 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
[146280a] | 52 | # check 1d array |
---|
[ae6e15c] | 53 | a = array(range(10), dtype = 'float32') |
---|
| 54 | self.assertEquals (alpha_norm(a, 1), 4.5) |
---|
[146280a] | 55 | # check 2d array |
---|
[ae6e15c] | 56 | a = array([range(10), range(10)], dtype = 'float32') |
---|
| 57 | self.assertEquals (alpha_norm(a, 1), 9) |
---|
| 58 | |
---|
| 59 | def test_alpha_norm_of_array_of_int(self): |
---|
| 60 | a = array(1, dtype = 'int') |
---|
| 61 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
| 62 | a = array([[[1,2],[3,4]]], dtype = 'int') |
---|
| 63 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
| 64 | a = array(range(10), dtype = 'int') |
---|
| 65 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
| 66 | |
---|
| 67 | def test_alpha_norm_of_array_of_string (self): |
---|
| 68 | a = "hello" |
---|
| 69 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
| 70 | |
---|
[146280a] | 71 | def test_zero_crossing_rate(self): |
---|
| 72 | a = array([0,1,-1], dtype='float32') |
---|
| 73 | self.assertEquals (zero_crossing_rate(a), 1./3 ) |
---|
| 74 | a = array([0.]*100, dtype='float32') |
---|
| 75 | self.assertEquals (zero_crossing_rate(a), 0 ) |
---|
| 76 | a = array([-1.]*100, dtype='float32') |
---|
| 77 | self.assertEquals (zero_crossing_rate(a), 0 ) |
---|
| 78 | a = array([1.]*100, dtype='float32') |
---|
| 79 | self.assertEquals (zero_crossing_rate(a), 0 ) |
---|
| 80 | |
---|
| 81 | def test_alpha_norm_of_array_of_float64(self): |
---|
| 82 | # check scalar fail |
---|
| 83 | a = array(1, dtype = 'float64') |
---|
| 84 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
| 85 | # check 3d array fail |
---|
| 86 | a = array([[[1,2],[3,4]]], dtype = 'float64') |
---|
| 87 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
| 88 | if AUBIO_DO_CASTING: |
---|
| 89 | # check float64 1d array fail |
---|
| 90 | a = array(range(10), dtype = 'float64') |
---|
| 91 | self.assertEquals (alpha_norm(a, 1), 4.5) |
---|
| 92 | # check float64 2d array fail |
---|
| 93 | a = array([range(10), range(10)], dtype = 'float64') |
---|
| 94 | self.assertEquals (alpha_norm(a, 1), 9) |
---|
| 95 | else: |
---|
| 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 | if AUBIO_DO_CASTING: |
---|
| 115 | b = min_removal(a) |
---|
| 116 | assert_equal (array(b), [19, 0, 18]) |
---|
| 117 | assert_equal (b, [19, 0, 18]) |
---|
| 118 | #assert_equal (a, b) |
---|
| 119 | else: |
---|
| 120 | self.assertRaises (ValueError, min_removal, a) |
---|
| 121 | |
---|
| 122 | def test_fvec_min_removal_of_fvec(self): |
---|
| 123 | a = fvec(3, 1) |
---|
| 124 | a[0] = [20, 1, 19] |
---|
| 125 | b = min_removal(a) |
---|
| 126 | assert_equal (array(b), [19, 0, 18]) |
---|
| 127 | assert_equal (b, [19, 0, 18]) |
---|
| 128 | assert_equal (a, b) |
---|
| 129 | |
---|
[ae6e15c] | 130 | if __name__ == '__main__': |
---|
[146280a] | 131 | from unittest import main |
---|
| 132 | main() |
---|
| 133 | |
---|