source: python/tests/test_fvec.py @ 00fcc5d

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 00fcc5d was fc144a5, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[tests] avoid some imports, add missing main

  • Property mode set to 100755
File size: 4.4 KB
Line 
1#! /usr/bin/env python
2
3import numpy as np
4from numpy.testing import TestCase, assert_equal, assert_almost_equal
5from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
6from aubio import float_type
7
8wrong_type = 'float32' if float_type == 'float64' else 'float64'
9
10default_size = 512
11
12class aubio_fvec_test_case(TestCase):
13
14    def test_vector_created_with_zeroes(self):
15        a = fvec(10)
16        assert a.dtype == float_type
17        assert a.shape == (10,)
18        assert_equal(a, 0)
19
20    def test_vector_create_with_list(self):
21        a = fvec([0, 1, 2, 3])
22        assert a.dtype == float_type
23        assert a.shape == (4,)
24        assert_equal(list(range(4)), a)
25
26    def test_vector_assign_element(self):
27        a = fvec(default_size)
28        a[0] = 1
29        assert_equal(a[0], 1)
30
31    def test_vector_assign_element_end(self):
32        a = fvec(default_size)
33        a[-1] = 1
34        assert_equal(a[-1], 1)
35        assert_equal(a[len(a)-1], 1)
36
37    def test_vector(self):
38        a = fvec()
39        len(a)
40        _ = a[0]
41        np.array(a)
42        a = fvec(1)
43        a = fvec(10)
44        _ = a.T
45
46class aubio_fvec_wrong_values(TestCase):
47
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_zero_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) """
58        a = fvec(2)
59        self.assertRaises(IndexError, a.__getitem__, 3)
60        self.assertRaises(IndexError, a.__getitem__, 2)
61
62    def test_wrong_dimensions(self):
63        a = np.array([[[1, 2], [3, 4]]], dtype=float_type)
64        self.assertRaises(ValueError, fvec, a)
65
66    def test_wrong_size(self):
67        a = np.ndarray([0,], dtype=float_type)
68        self.assertRaises(ValueError, fvec, a)
69
70class aubio_wrong_fvec_input(TestCase):
71    """ uses min_removal to test PyAubio_IsValidVector """
72
73    def test_no_input(self):
74        self.assertRaises(TypeError, min_removal)
75
76    def test_none(self):
77        self.assertRaises(ValueError, min_removal, None)
78
79    def test_wrong_scalar(self):
80        a = np.array(10, dtype=float_type)
81        self.assertRaises(ValueError, min_removal, a)
82
83    def test_wrong_dimensions(self):
84        a = np.array([[[1, 2], [3, 4]]], dtype=float_type)
85        self.assertRaises(ValueError, min_removal, a)
86
87    def test_wrong_array_size(self):
88        x = np.array([], dtype=float_type)
89        self.assertRaises(ValueError, min_removal, x)
90
91    def test_wrong_type(self):
92        a = np.zeros(10, dtype=wrong_type)
93        self.assertRaises(ValueError, min_removal, a)
94
95    def test_wrong_list_input(self):
96        self.assertRaises(ValueError, min_removal, [0., 1.])
97
98    def test_good_input(self):
99        a = np.zeros(10, dtype=float_type)
100        assert_equal(np.zeros(10, dtype=float_type), min_removal(a))
101
102class aubio_alpha_norm(TestCase):
103
104    def test_alpha_norm_of_random(self):
105        x = np.random.rand(1024).astype(float_type)
106        alpha = np.random.rand() * 5.
107        x_alpha_norm = (np.sum(np.abs(x)**alpha)/len(x))**(1/alpha)
108        assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm, decimal = 4)
109
110class aubio_zero_crossing_rate_test(TestCase):
111
112    def test_zero_crossing_rate(self):
113        a = np.array([0, 1, -1], dtype=float_type)
114        assert_almost_equal(zero_crossing_rate(a), 1./3.)
115
116    def test_zero_crossing_rate_zeros(self):
117        a = np.zeros(100, dtype=float_type)
118        self.assertEqual(zero_crossing_rate(a), 0)
119
120    def test_zero_crossing_rate_minus_ones(self):
121        a = np.ones(100, dtype=float_type)
122        self.assertEqual(zero_crossing_rate(a), 0)
123
124    def test_zero_crossing_rate_plus_ones(self):
125        a = np.ones(100, dtype=float_type)
126        self.assertEqual(zero_crossing_rate(a), 0)
127
128class aubio_fvec_min_removal(TestCase):
129
130    def test_fvec_min_removal_of_array(self):
131        a = np.array([20, 1, 19], dtype=float_type)
132        b = min_removal(a)
133        assert_equal(b, [19, 0, 18])
134
135class aubio_fvec_test_memory(TestCase):
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.