[75e715f] | 1 | #! /usr/bin/env python |
---|
[1ebf8770] | 2 | |
---|
[b7f3aaf] | 3 | from numpy.testing import TestCase, run_module_suite |
---|
| 4 | from numpy.testing import assert_equal, assert_almost_equal |
---|
[4c01c0f] | 5 | from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal |
---|
[1a6ef2c] | 6 | from numpy import array, shape |
---|
[b7f3aaf] | 7 | |
---|
[973206a] | 8 | default_size = 512 |
---|
| 9 | |
---|
[b7f3aaf] | 10 | class aubio_fvec_test_case(TestCase): |
---|
| 11 | |
---|
| 12 | def test_vector_created_with_zeroes(self): |
---|
[1a6ef2c] | 13 | a = fvec(10) |
---|
[973206a] | 14 | assert a.dtype == 'float32' |
---|
| 15 | assert a.shape == (10,) |
---|
| 16 | assert_equal (a, 0) |
---|
[b7f3aaf] | 17 | |
---|
[493e6f7] | 18 | def test_vector_create_with_list(self): |
---|
| 19 | a = fvec([0,1,2,3]) |
---|
[973206a] | 20 | assert a.dtype == 'float32' |
---|
| 21 | assert a.shape == (4,) |
---|
[689106e] | 22 | assert_equal (list(range(4)), a) |
---|
[493e6f7] | 23 | |
---|
[b7f3aaf] | 24 | def test_vector_assign_element(self): |
---|
[973206a] | 25 | a = fvec(default_size) |
---|
[b7f3aaf] | 26 | a[0] = 1 |
---|
| 27 | assert_equal(a[0], 1) |
---|
| 28 | |
---|
| 29 | def test_vector_assign_element_end(self): |
---|
[973206a] | 30 | a = fvec(default_size) |
---|
[b7f3aaf] | 31 | a[-1] = 1 |
---|
| 32 | assert_equal(a[-1], 1) |
---|
[1a6ef2c] | 33 | assert_equal(a[len(a)-1], 1) |
---|
[b7f3aaf] | 34 | |
---|
| 35 | def test_vector(self): |
---|
| 36 | a = fvec() |
---|
[1a6ef2c] | 37 | a, len(a) #a.length |
---|
[b7f3aaf] | 38 | a[0] |
---|
| 39 | array(a) |
---|
| 40 | a = fvec(10) |
---|
| 41 | a = fvec(1) |
---|
[1a6ef2c] | 42 | a.T |
---|
[b7f3aaf] | 43 | array(a).T |
---|
[689106e] | 44 | a = list(range(len(a))) |
---|
[b7f3aaf] | 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) |
---|
[689106e] | 55 | self.assertEqual (alpha_norm(a, 1), 0) |
---|
[b7f3aaf] | 56 | a[0] = 1 |
---|
[689106e] | 57 | self.assertEqual (alpha_norm(a, 1), 0.5) |
---|
[b7f3aaf] | 58 | a[1] = 1 |
---|
[689106e] | 59 | self.assertEqual (alpha_norm(a, 1), 1) |
---|
[b7f3aaf] | 60 | a = array([0, 1], dtype='float32') |
---|
| 61 | from math import sqrt |
---|
| 62 | assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.) |
---|
| 63 | |
---|
[493e6f7] | 64 | def test_alpha_norm_of_none(self): |
---|
| 65 | self.assertRaises (ValueError, alpha_norm, None, 1) |
---|
| 66 | |
---|
[b7f3aaf] | 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') |
---|
[689106e] | 76 | self.assertEqual (alpha_norm(a, 1), 4.5) |
---|
[b7f3aaf] | 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') |
---|
[4afa448e] | 92 | assert_almost_equal (zero_crossing_rate(a), 1./3. ) |
---|
[b7f3aaf] | 93 | a = array([0.]*100, dtype='float32') |
---|
[689106e] | 94 | self.assertEqual (zero_crossing_rate(a), 0 ) |
---|
[b7f3aaf] | 95 | a = array([-1.]*100, dtype='float32') |
---|
[689106e] | 96 | self.assertEqual (zero_crossing_rate(a), 0 ) |
---|
[b7f3aaf] | 97 | a = array([1.]*100, dtype='float32') |
---|
[689106e] | 98 | self.assertEqual (zero_crossing_rate(a), 0 ) |
---|
[b7f3aaf] | 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 |
---|
[689106e] | 108 | a = array(list(range(10)), dtype = 'float64') |
---|
[b7f3aaf] | 109 | self.assertRaises (ValueError, alpha_norm, a, 1) |
---|
| 110 | # check float64 2d array fail |
---|
[689106e] | 111 | a = array([list(range(10)), list(range(10))], dtype = 'float64') |
---|
[b7f3aaf] | 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() |
---|