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