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