[75e715f] | 1 | #! /usr/bin/env python |
---|
[1ebf8770] | 2 | |
---|
[0b6d23d] | 3 | from unittest import main |
---|
[88554b9] | 4 | import numpy as np |
---|
| 5 | from numpy.testing import TestCase, assert_equal, assert_almost_equal |
---|
[4c01c0f] | 6 | from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal |
---|
[c4b2183] | 7 | from aubio import float_type |
---|
[b7f3aaf] | 8 | |
---|
[c4b2183] | 9 | wrong_type = 'float32' if float_type == 'float64' else 'float64' |
---|
| 10 | |
---|
[973206a] | 11 | default_size = 512 |
---|
| 12 | |
---|
[b7f3aaf] | 13 | class aubio_fvec_test_case(TestCase): |
---|
| 14 | |
---|
| 15 | def test_vector_created_with_zeroes(self): |
---|
[1a6ef2c] | 16 | a = fvec(10) |
---|
[c4b2183] | 17 | assert a.dtype == float_type |
---|
[973206a] | 18 | assert a.shape == (10,) |
---|
[88554b9] | 19 | assert_equal(a, 0) |
---|
[b7f3aaf] | 20 | |
---|
[493e6f7] | 21 | def test_vector_create_with_list(self): |
---|
[88554b9] | 22 | a = fvec([0, 1, 2, 3]) |
---|
[c4b2183] | 23 | assert a.dtype == float_type |
---|
[973206a] | 24 | assert a.shape == (4,) |
---|
[88554b9] | 25 | assert_equal(list(range(4)), a) |
---|
[493e6f7] | 26 | |
---|
[b7f3aaf] | 27 | def test_vector_assign_element(self): |
---|
[973206a] | 28 | a = fvec(default_size) |
---|
[b7f3aaf] | 29 | a[0] = 1 |
---|
| 30 | assert_equal(a[0], 1) |
---|
| 31 | |
---|
| 32 | def test_vector_assign_element_end(self): |
---|
[973206a] | 33 | a = fvec(default_size) |
---|
[b7f3aaf] | 34 | a[-1] = 1 |
---|
| 35 | assert_equal(a[-1], 1) |
---|
[1a6ef2c] | 36 | assert_equal(a[len(a)-1], 1) |
---|
[b7f3aaf] | 37 | |
---|
| 38 | def test_vector(self): |
---|
| 39 | a = fvec() |
---|
[88554b9] | 40 | len(a) |
---|
| 41 | _ = a[0] |
---|
| 42 | np.array(a) |
---|
[b7f3aaf] | 43 | a = fvec(1) |
---|
[88554b9] | 44 | a = fvec(10) |
---|
| 45 | _ = a.T |
---|
[b7f3aaf] | 46 | |
---|
[88554b9] | 47 | class aubio_fvec_wrong_values(TestCase): |
---|
[b7f3aaf] | 48 | |
---|
[88554b9] | 49 | def test_negative_length(self): |
---|
| 50 | """ test creating fvec with negative length fails (pure python) """ |
---|
| 51 | self.assertRaises(ValueError, fvec, -10) |
---|
| 52 | |
---|
[0b6d23d] | 53 | def test_zero_length(self): |
---|
[88554b9] | 54 | """ test creating fvec with zero length fails (pure python) """ |
---|
| 55 | self.assertRaises(ValueError, fvec, 0) |
---|
| 56 | |
---|
| 57 | def test_out_of_bound(self): |
---|
| 58 | """ test assiging fvec out of bounds fails (pure python) """ |
---|
[b7f3aaf] | 59 | a = fvec(2) |
---|
[88554b9] | 60 | self.assertRaises(IndexError, a.__getitem__, 3) |
---|
| 61 | self.assertRaises(IndexError, a.__getitem__, 2) |
---|
| 62 | |
---|
| 63 | class aubio_wrong_fvec_input(TestCase): |
---|
| 64 | """ uses min_removal to test PyAubio_IsValidVector """ |
---|
| 65 | |
---|
| 66 | def test_no_input(self): |
---|
| 67 | self.assertRaises(TypeError, min_removal) |
---|
| 68 | |
---|
| 69 | def test_none(self): |
---|
| 70 | self.assertRaises(ValueError, min_removal, None) |
---|
| 71 | |
---|
| 72 | def test_wrong_scalar(self): |
---|
| 73 | a = np.array(10, dtype=float_type) |
---|
| 74 | self.assertRaises(ValueError, min_removal, a) |
---|
| 75 | |
---|
| 76 | def test_wrong_dimensions(self): |
---|
| 77 | a = np.array([[[1, 2], [3, 4]]], dtype=float_type) |
---|
| 78 | self.assertRaises(ValueError, min_removal, a) |
---|
| 79 | |
---|
| 80 | def test_wrong_array_size(self): |
---|
| 81 | x = np.array([], dtype=float_type) |
---|
| 82 | self.assertRaises(ValueError, min_removal, x) |
---|
| 83 | |
---|
| 84 | def test_wrong_type(self): |
---|
| 85 | a = np.zeros(10, dtype=wrong_type) |
---|
| 86 | self.assertRaises(ValueError, min_removal, a) |
---|
| 87 | |
---|
| 88 | def test_wrong_list_input(self): |
---|
| 89 | self.assertRaises(ValueError, min_removal, [0., 1.]) |
---|
| 90 | |
---|
| 91 | def test_good_input(self): |
---|
| 92 | a = np.zeros(10, dtype=float_type) |
---|
| 93 | assert_equal(np.zeros(10, dtype=float_type), min_removal(a)) |
---|
| 94 | |
---|
| 95 | class aubio_alpha_norm(TestCase): |
---|
| 96 | |
---|
| 97 | def test_alpha_norm_of_random(self): |
---|
| 98 | x = np.random.rand(1024).astype(float_type) |
---|
| 99 | alpha = np.random.rand() * 5. |
---|
| 100 | x_alpha_norm = (np.sum(np.abs(x)**alpha)/len(x))**(1/alpha) |
---|
[88c89e3] | 101 | assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm, decimal = 4) |
---|
[88554b9] | 102 | |
---|
| 103 | class aubio_zero_crossing_rate_test(TestCase): |
---|
[b7f3aaf] | 104 | |
---|
| 105 | def test_zero_crossing_rate(self): |
---|
[88554b9] | 106 | a = np.array([0, 1, -1], dtype=float_type) |
---|
| 107 | assert_almost_equal(zero_crossing_rate(a), 1./3.) |
---|
| 108 | |
---|
| 109 | def test_zero_crossing_rate_zeros(self): |
---|
| 110 | a = np.zeros(100, dtype=float_type) |
---|
| 111 | self.assertEqual(zero_crossing_rate(a), 0) |
---|
| 112 | |
---|
| 113 | def test_zero_crossing_rate_minus_ones(self): |
---|
| 114 | a = np.ones(100, dtype=float_type) |
---|
| 115 | self.assertEqual(zero_crossing_rate(a), 0) |
---|
| 116 | |
---|
| 117 | def test_zero_crossing_rate_plus_ones(self): |
---|
| 118 | a = np.ones(100, dtype=float_type) |
---|
| 119 | self.assertEqual(zero_crossing_rate(a), 0) |
---|
| 120 | |
---|
| 121 | class aubio_fvec_min_removal(TestCase): |
---|
[b7f3aaf] | 122 | |
---|
| 123 | def test_fvec_min_removal_of_array(self): |
---|
[88554b9] | 124 | a = np.array([20, 1, 19], dtype=float_type) |
---|
[b7f3aaf] | 125 | b = min_removal(a) |
---|
[88554b9] | 126 | assert_equal(b, [19, 0, 18]) |
---|
| 127 | |
---|
| 128 | class aubio_fvec_test_memory(TestCase): |
---|
[b7f3aaf] | 129 | |
---|
[fb3f62e] | 130 | def test_pass_to_numpy(self): |
---|
| 131 | a = fvec(10) |
---|
[0b6d23d] | 132 | a[:] = 1. |
---|
[fb3f62e] | 133 | b = a |
---|
| 134 | del a |
---|
[88554b9] | 135 | assert_equal(b, 1.) |
---|
[fb3f62e] | 136 | c = fvec(10) |
---|
| 137 | c = b |
---|
| 138 | del b |
---|
[88554b9] | 139 | assert_equal(c, 1.) |
---|
[fb3f62e] | 140 | del c |
---|
| 141 | |
---|
[b7f3aaf] | 142 | if __name__ == '__main__': |
---|
| 143 | main() |
---|