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