source: python/tests/test_fvec.py @ 0df6e9e

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

python/tests/test_fvec.py: avoid import

  • Property mode set to 100755
File size: 4.8 KB
Line 
1#! /usr/bin/env python
2
3from numpy.testing import TestCase, run_module_suite
4from numpy.testing import assert_equal, assert_almost_equal
5from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
6from aubio import float_type
7from numpy import array, shape
8
9wrong_type = 'float32' if float_type == 'float64' else 'float64'
10
11default_size = 512
12
13class aubio_fvec_test_case(TestCase):
14
15    def test_vector_created_with_zeroes(self):
16        a = fvec(10)
17        assert a.dtype == float_type
18        assert a.shape == (10,)
19        assert_equal (a, 0)
20
21    def test_vector_create_with_list(self):
22        a = fvec([0,1,2,3])
23        assert a.dtype == float_type
24        assert a.shape == (4,)
25        assert_equal (list(range(4)), a)
26
27    def test_vector_assign_element(self):
28        a = fvec(default_size)
29        a[0] = 1
30        assert_equal(a[0], 1)
31
32    def test_vector_assign_element_end(self):
33        a = fvec(default_size)
34        a[-1] = 1
35        assert_equal(a[-1], 1)
36        assert_equal(a[len(a)-1], 1)
37
38    def test_vector(self):
39        a = fvec()
40        a, len(a) #a.length
41        a[0]
42        array(a)
43        a = fvec(10)
44        a = fvec(1)
45        a.T
46        array(a).T
47        a = list(range(len(a)))
48
49    def test_wrong_values(self):
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
56    def test_alpha_norm_of_fvec(self):
57        a = fvec(2)
58        self.assertEqual (alpha_norm(a, 1), 0)
59        a[0] = 1
60        self.assertEqual (alpha_norm(a, 1), 0.5)
61        a[1] = 1
62        self.assertEqual (alpha_norm(a, 1), 1)
63        a = array([0, 1], dtype=float_type)
64        assert_almost_equal (alpha_norm(a, 2), .5*2.**.5)
65
66    def test_alpha_norm_of_none(self):
67        self.assertRaises (ValueError, alpha_norm, None, 1)
68
69    def test_alpha_norm_of_array_of_float32(self):
70        # check scalar fails
71        a = array(1, dtype = float_type)
72        self.assertRaises (ValueError, alpha_norm, a, 1)
73        # check 2d array fails
74        a = array([[2],[4]], dtype = float_type)
75        self.assertRaises (ValueError, alpha_norm, a, 1)
76        # check 1d array
77        a = array(range(10), dtype = float_type)
78        self.assertEqual (alpha_norm(a, 1), 4.5)
79
80    def test_alpha_norm_of_array_of_int(self):
81        a = array(1, dtype = 'int')
82        self.assertRaises (ValueError, alpha_norm, a, 1)
83        a = array([[[1,2],[3,4]]], dtype = 'int')
84        self.assertRaises (ValueError, alpha_norm, a, 1)
85        a = array(range(10), dtype = 'int')
86        self.assertRaises (ValueError, alpha_norm, a, 1)
87
88    def test_alpha_norm_of_array_of_string (self):
89        a = "hello"
90        self.assertRaises (ValueError, alpha_norm, a, 1)
91
92    def test_zero_crossing_rate(self):
93        a = array([0,1,-1], dtype=float_type)
94        assert_almost_equal (zero_crossing_rate(a), 1./3. )
95        a = array([0.]*100, dtype=float_type)
96        self.assertEqual (zero_crossing_rate(a), 0 )
97        a = array([-1.]*100, dtype=float_type)
98        self.assertEqual (zero_crossing_rate(a), 0 )
99        a = array([1.]*100, dtype=float_type)
100        self.assertEqual (zero_crossing_rate(a), 0 )
101
102    def test_alpha_norm_of_array_of_float64(self):
103        # check scalar fail
104        a = array(1, dtype = wrong_type)
105        self.assertRaises (ValueError, alpha_norm, a, 1)
106        # check 3d array fail
107        a = array([[[1,2],[3,4]]], dtype = wrong_type)
108        self.assertRaises (ValueError, alpha_norm, a, 1)
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
116    def test_fvec_min_removal_of_array(self):
117        a = 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)
133        assert_equal (array(b), [19, 0, 18])
134        assert_equal (b, [19, 0, 18])
135        assert_equal (a, b)
136
137    def test_pass_to_numpy(self):
138        a = fvec(10)
139        a = 1.
140        b = a
141        del a
142        assert_equal (b, 1.)
143        c = fvec(10)
144        c = b
145        del b
146        assert_equal (c, 1.)
147        del c
148
149if __name__ == '__main__':
150    from unittest import main
151    main()
Note: See TracBrowser for help on using the repository browser.