Showing the changes to python/tests/test_fvec.py (1 of 1 file). Show all
Unified · Split
| @@ -1,10 +1,9 @@ | |||
| 1 | #! /usr/bin/env python | 1 | #! /usr/bin/env python |
| 2 | 2 | ||
| 3 | from numpy.testing import TestCase, run_module_suite | 3 | import numpy as np |
| 4 | from numpy.testing import assert_equal, assert_almost_equal | 4 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
| 5 | from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal | 5 | from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal |
| 6 | from aubio import float_type | 6 | from aubio import float_type |
| 7 | from numpy import array, shape | ||
| 8 | 7 | ||
| 9 | wrong_type = 'float32' if float_type == 'float64' else 'float64' | 8 | wrong_type = 'float32' if float_type == 'float64' else 'float64' |
| 10 | 9 | ||
| @@ -16,13 +15,13 @@ class aubio_fvec_test_case(TestCase): · 5 unmodified lines | |||
| 16 | a = fvec(10) | 15 | a = fvec(10) |
| 17 | assert a.dtype == float_type | 16 | assert a.dtype == float_type |
| 18 | assert a.shape == (10,) | 17 | assert a.shape == (10,) |
| 19 | assert_equal (a, 0) | 18 | assert_equal(a, 0) |
| 20 | 19 | ||
| 21 | def test_vector_create_with_list(self): | 20 | def test_vector_create_with_list(self): |
| 22 | a = fvec([0,1,2,3]) | 21 | a = fvec([0, 1, 2, 3]) |
| 23 | assert a.dtype == float_type | 22 | assert a.dtype == float_type |
| 24 | assert a.shape == (4,) | 23 | assert a.shape == (4,) |
| 25 | assert_equal (list(range(4)), a) | 24 | assert_equal(list(range(4)), a) |
| 26 | 25 | ||
| 27 | def test_vector_assign_element(self): | 26 | def test_vector_assign_element(self): |
| 28 | a = fvec(default_size) | 27 | a = fvec(default_size) |
| @@ -37,113 +36,106 @@ class aubio_fvec_test_case(TestCase): · 8 unmodified lines | |||
| 37 | 36 | ||
| 38 | def test_vector(self): | 37 | def test_vector(self): |
| 39 | a = fvec() | 38 | a = fvec() |
| 40 | a, len(a) #a.length | 39 | len(a) |
| 41 | a[0] | 40 | _ = a[0] |
| 42 | array(a) | 41 | np.array(a) |
| 43 | a = fvec(10) | ||
| 44 | a = fvec(1) | 42 | a = fvec(1) |
| 45 | a.T | 43 | a = fvec(10) |
| 46 | array(a).T | 44 | _ = a.T |
| 47 | a = list(range(len(a))) | ||
| 48 | 45 | ||
| 49 | def test_wrong_values(self): | 46 | class aubio_fvec_wrong_values(TestCase): |
| 50 | self.assertRaises (ValueError, fvec, -10) | ||
| 51 | |||
| 52 | a = fvec(2) | ||
| 53 | self.assertRaises (IndexError, a.__getitem__, 3) | ||
| 54 | self.assertRaises (IndexError, a.__getitem__, 2) | ||
| 55 | 47 | ||
| 56 | def test_alpha_norm_of_fvec(self): | 48 | def test_negative_length(self): |
| 49 | """ test creating fvec with negative length fails (pure python) """ | ||
| 50 | self.assertRaises(ValueError, fvec, -10) | ||
| 51 | |||
| 52 | def test_negative_length(self): | ||
| 53 | """ test creating fvec with zero length fails (pure python) """ | ||
| 54 | self.assertRaises(ValueError, fvec, 0) | ||
| 55 | |||
| 56 | def test_out_of_bound(self): | ||
| 57 | """ test assiging fvec out of bounds fails (pure python) """ | ||
| 57 | a = fvec(2) | 58 | a = fvec(2) |
| 58 | self.assertEqual (alpha_norm(a, 1), 0) | 59 | self.assertRaises(IndexError, a.__getitem__, 3) |
| 59 | a[0] = 1 | 60 | self.assertRaises(IndexError, a.__getitem__, 2) |
| 60 | self.assertEqual (alpha_norm(a, 1), 0.5) | 61 | |
| 61 | a[1] = 1 | 62 | class aubio_wrong_fvec_input(TestCase): |
| 62 | self.assertEqual (alpha_norm(a, 1), 1) | 63 | """ uses min_removal to test PyAubio_IsValidVector """ |
| 63 | a = array([0, 1], dtype=float_type) | 64 | |
| 64 | assert_almost_equal (alpha_norm(a, 2), .5*2.**.5) | 65 | def test_no_input(self): |
| 65 | 66 | self.assertRaises(TypeError, min_removal) | |
| 66 | def test_alpha_norm_of_none(self): | 67 | |
| 67 | self.assertRaises (ValueError, alpha_norm, None, 1) | 68 | def test_none(self): |
| 68 | 69 | self.assertRaises(ValueError, min_removal, None) | |
| 69 | def test_alpha_norm_of_array_of_float32(self): | 70 | |
| 70 | # check scalar fails | 71 | def test_wrong_scalar(self): |
| 71 | a = array(1, dtype = float_type) | 72 | a = np.array(10, dtype=float_type) |
| 72 | self.assertRaises (ValueError, alpha_norm, a, 1) | 73 | self.assertRaises(ValueError, min_removal, a) |
| 73 | # check 2d array fails | 74 | |
| 74 | a = array([[2],[4]], dtype = float_type) | 75 | def test_wrong_dimensions(self): |
| 75 | self.assertRaises (ValueError, alpha_norm, a, 1) | 76 | a = np.array([[[1, 2], [3, 4]]], dtype=float_type) |
| 76 | # check 1d array | 77 | self.assertRaises(ValueError, min_removal, a) |
| 77 | a = array(range(10), dtype = float_type) | 78 | |
| 78 | self.assertEqual (alpha_norm(a, 1), 4.5) | 79 | def test_wrong_array_size(self): |
| 79 | 80 | x = np.array([], dtype=float_type) | |
| 80 | def test_alpha_norm_of_array_of_int(self): | 81 | self.assertRaises(ValueError, min_removal, x) |
| 81 | a = array(1, dtype = 'int') | 82 | |
| 82 | self.assertRaises (ValueError, alpha_norm, a, 1) | 83 | def test_wrong_type(self): |
| 83 | a = array([[[1,2],[3,4]]], dtype = 'int') | 84 | a = np.zeros(10, dtype=wrong_type) |
| 84 | self.assertRaises (ValueError, alpha_norm, a, 1) | 85 | self.assertRaises(ValueError, min_removal, a) |
| 85 | a = array(range(10), dtype = 'int') | 86 | |
| 86 | self.assertRaises (ValueError, alpha_norm, a, 1) | 87 | def test_wrong_list_input(self): |
| 87 | 88 | self.assertRaises(ValueError, min_removal, [0., 1.]) | |
| 88 | def test_alpha_norm_of_array_of_string (self): | 89 | |
| 89 | a = "hello" | 90 | def test_good_input(self): |
| 90 | self.assertRaises (ValueError, alpha_norm, a, 1) | 91 | a = np.zeros(10, dtype=float_type) |
| 92 | assert_equal(np.zeros(10, dtype=float_type), min_removal(a)) | ||
| 93 | |||
| 94 | class aubio_alpha_norm(TestCase): | ||
| 95 | |||
| 96 | def test_alpha_norm_of_random(self): | ||
| 97 | x = np.random.rand(1024).astype(float_type) | ||
| 98 | alpha = np.random.rand() * 5. | ||
| 99 | x_alpha_norm = (np.sum(np.abs(x)**alpha)/len(x))**(1/alpha) | ||
| 100 | assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm) | ||
| 101 | |||
| 102 | class aubio_zero_crossing_rate_test(TestCase): | ||
| 91 | 103 | ||
| 92 | def test_zero_crossing_rate(self): | 104 | def test_zero_crossing_rate(self): |
| 93 | a = array([0,1,-1], dtype=float_type) | 105 | a = np.array([0, 1, -1], dtype=float_type) |
| 94 | assert_almost_equal (zero_crossing_rate(a), 1./3. ) | 106 | assert_almost_equal(zero_crossing_rate(a), 1./3.) |
| 95 | a = array([0.]*100, dtype=float_type) | 107 | |
| 96 | self.assertEqual (zero_crossing_rate(a), 0 ) | 108 | def test_zero_crossing_rate_zeros(self): |
| 97 | a = array([-1.]*100, dtype=float_type) | 109 | a = np.zeros(100, dtype=float_type) |
| 98 | self.assertEqual (zero_crossing_rate(a), 0 ) | 110 | self.assertEqual(zero_crossing_rate(a), 0) |
| 99 | a = array([1.]*100, dtype=float_type) | 111 | |
| 100 | self.assertEqual (zero_crossing_rate(a), 0 ) | 112 | def test_zero_crossing_rate_minus_ones(self): |
| 101 | 113 | a = np.ones(100, dtype=float_type) | |
| 102 | def test_alpha_norm_of_array_of_float64(self): | 114 | self.assertEqual(zero_crossing_rate(a), 0) |
| 103 | # check scalar fail | 115 | |
| 104 | a = array(1, dtype = wrong_type) | 116 | def test_zero_crossing_rate_plus_ones(self): |
| 105 | self.assertRaises (ValueError, alpha_norm, a, 1) | 117 | a = np.ones(100, dtype=float_type) |
| 106 | # check 3d array fail | 118 | self.assertEqual(zero_crossing_rate(a), 0) |
| 107 | a = array([[[1,2],[3,4]]], dtype = wrong_type) | 119 | |
| 108 | self.assertRaises (ValueError, alpha_norm, a, 1) | 120 | class aubio_fvec_min_removal(TestCase): |
| 109 | # check float64 1d array fail | ||
| 110 | a = array(list(range(10)), dtype = wrong_type) | ||
| 111 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 112 | # check float64 2d array fail | ||
| 113 | a = array([list(range(10)), list(range(10))], dtype = wrong_type) | ||
| 114 | self.assertRaises (ValueError, alpha_norm, a, 1) | ||
| 115 | 121 | ||
| 116 | def test_fvec_min_removal_of_array(self): | 122 | def test_fvec_min_removal_of_array(self): |
| 117 | a = array([20,1,19], dtype=float_type) | 123 | a = np.array([20, 1, 19], dtype=float_type) |
| 118 | b = min_removal(a) | ||
| 119 | assert_equal (array(b), [19, 0, 18]) | ||
| 120 | assert_equal (b, [19, 0, 18]) | ||
| 121 | assert_equal (a, b) | ||
| 122 | a[0] = 0 | ||
| 123 | assert_equal (a, b) | ||
| 124 | |||
| 125 | def test_fvec_min_removal_of_array_float64(self): | ||
| 126 | a = array([20,1,19], dtype=wrong_type) | ||
| 127 | self.assertRaises (ValueError, min_removal, a) | ||
| 128 | |||
| 129 | def test_fvec_min_removal_of_fvec(self): | ||
| 130 | a = fvec(3) | ||
| 131 | a = array([20, 1, 19], dtype = float_type) | ||
| 132 | b = min_removal(a) | 124 | b = min_removal(a) |
| 133 | assert_equal (array(b), [19, 0, 18]) | 125 | assert_equal(b, [19, 0, 18]) |
| 134 | assert_equal (b, [19, 0, 18]) | 126 | |
| 135 | assert_equal (a, b) | 127 | class aubio_fvec_test_memory(TestCase): |
| 136 | 128 | ||
| 137 | def test_pass_to_numpy(self): | 129 | def test_pass_to_numpy(self): |
| 138 | a = fvec(10) | 130 | a = fvec(10) |
| 139 | a = 1. | 131 | a = 1. |
| 140 | b = a | 132 | b = a |
| 141 | del a | 133 | del a |
| 142 | assert_equal (b, 1.) | 134 | assert_equal(b, 1.) |
| 143 | c = fvec(10) | 135 | c = fvec(10) |
| 144 | c = b | 136 | c = b |
| 145 | del b | 137 | del b |
| 146 | assert_equal (c, 1.) | 138 | assert_equal(c, 1.) |
| 147 | del c | 139 | del c |
| 148 | 140 | ||
| 149 | if __name__ == '__main__': | 141 | if __name__ == '__main__': |