source: interfaces/python/test_fvec.py @ 0178c65

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 0178c65 was b7f3aaf, checked in by Paul Brossier <piem@piem.org>, 15 years ago

interfaces/python: start updating tests for mono

  • Property mode set to 100644
File size: 4.0 KB
Line 
1from numpy.testing import TestCase, run_module_suite
2from numpy.testing import assert_equal, assert_almost_equal
3from _aubio import *
4from numpy import array
5
6class aubio_fvec_test_case(TestCase):
7
8
9    def test_vector_created_with_zeroes(self):
10        a = fvec()
11        assert_equal(array(a), 0.)
12
13    def test_vector_assign_element(self):
14        a = fvec()
15        a[0] = 1
16        assert_equal(a[0], 1)
17
18    def test_vector_assign_element_end(self):
19        a = fvec()
20        a[-1] = 1
21        assert_equal(a[-1], 1)
22        assert_equal(a[a.length-1], 1)
23
24    def test_vector(self):
25        a = fvec()
26        a, a.length
27        a[0]
28        array(a)
29        a = fvec(10)
30        a = fvec(1)
31        array(a).T
32        a[0] = range(a.length)
33
34    def test_wrong_values(self):
35        self.assertRaises (ValueError, fvec, -10)
36 
37        a = fvec(2)
38        self.assertRaises (IndexError, a.__getitem__, 3)
39        self.assertRaises (IndexError, a.__getitem__, 2)
40
41    def test_alpha_norm_of_fvec(self):
42        a = fvec(2)
43        self.assertEquals (alpha_norm(a, 1), 0)
44        a[0] = 1
45        self.assertEquals (alpha_norm(a, 1), 0.5)
46        a[1] = 1
47        self.assertEquals (alpha_norm(a, 1), 1)
48        a = array([0, 1], dtype='float32')
49        from math import sqrt
50        assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.)
51
52    def test_alpha_norm_of_array_of_float32(self):
53        # check scalar fails
54        a = array(1, dtype = 'float32')
55        self.assertRaises (ValueError, alpha_norm, a, 1)
56        # check 2d array fails
57        a = array([[2],[4]], dtype = 'float32')
58        self.assertRaises (ValueError, alpha_norm, a, 1)
59        # check 1d array
60        a = array(range(10), dtype = 'float32')
61        self.assertEquals (alpha_norm(a, 1), 4.5)
62
63    def test_alpha_norm_of_array_of_int(self):
64        a = array(1, dtype = 'int')
65        self.assertRaises (ValueError, alpha_norm, a, 1)
66        a = array([[[1,2],[3,4]]], dtype = 'int')
67        self.assertRaises (ValueError, alpha_norm, a, 1)
68        a = array(range(10), dtype = 'int')
69        self.assertRaises (ValueError, alpha_norm, a, 1)
70
71    def test_alpha_norm_of_array_of_string (self):
72        a = "hello"
73        self.assertRaises (ValueError, alpha_norm, a, 1)
74
75    def test_zero_crossing_rate(self):
76        a = array([0,1,-1], dtype='float32')
77        self.assertEquals (zero_crossing_rate(a), 1./3 )
78        a = array([0.]*100, dtype='float32')
79        self.assertEquals (zero_crossing_rate(a), 0 )
80        a = array([-1.]*100, dtype='float32')
81        self.assertEquals (zero_crossing_rate(a), 0 )
82        a = array([1.]*100, dtype='float32')
83        self.assertEquals (zero_crossing_rate(a), 0 )
84
85    def test_alpha_norm_of_array_of_float64(self):
86        # check scalar fail
87        a = array(1, dtype = 'float64')
88        self.assertRaises (ValueError, alpha_norm, a, 1)
89        # check 3d array fail
90        a = array([[[1,2],[3,4]]], dtype = 'float64')
91        self.assertRaises (ValueError, alpha_norm, a, 1)
92        # check float64 1d array fail
93        a = array(range(10), dtype = 'float64')
94        self.assertRaises (ValueError, alpha_norm, a, 1)
95        # check float64 2d array fail
96        a = array([range(10), range(10)], dtype = 'float64')
97        self.assertRaises (ValueError, alpha_norm, a, 1)
98
99    def test_fvec_min_removal_of_array(self):
100        a = array([20,1,19], dtype='float32')
101        b = min_removal(a)
102        assert_equal (array(b), [19, 0, 18])
103        assert_equal (b, [19, 0, 18])
104        assert_equal (a, b)
105        a[0] = 0
106        assert_equal (a, b)
107
108    def test_fvec_min_removal_of_array_float64(self):
109        a = array([20,1,19], dtype='float64')
110        self.assertRaises (ValueError, min_removal, a)
111
112    def test_fvec_min_removal_of_fvec(self):
113        a = fvec(3)
114        a = array([20, 1, 19], dtype = 'float32')
115        b = min_removal(a)
116        assert_equal (array(b), [19, 0, 18])
117        assert_equal (b, [19, 0, 18])
118        assert_equal (a, b)
119
120if __name__ == '__main__':
121    from unittest import main
122    main()
Note: See TracBrowser for help on using the repository browser.