source: python/tests/test_cvec.py @ cb76f5d

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

python/tests/test_cvec.py: simplify

  • Property mode set to 100755
File size: 4.3 KB
Line 
1#! /usr/bin/env python
2
3from unittest import main
4import numpy as np
5from numpy.testing import TestCase, assert_equal
6from aubio import cvec, fvec, float_type
7
8wrong_type = 'float32' if float_type == 'float64' else 'float64'
9
10class aubio_cvec_test_case(TestCase):
11
12    def test_vector_created_with_zeroes(self):
13        a = cvec(10)
14        assert_equal(a.norm.shape[0], 10 // 2 + 1)
15        assert_equal(a.phas.shape[0], 10 // 2 + 1)
16        assert_equal(a.norm, 0.)
17        assert_equal(a.phas, 0.)
18
19    def test_vector_assign_element(self):
20        a = cvec()
21        a.norm[0] = 1
22        assert_equal(a.norm[0], 1)
23        a.phas[0] = 1
24        assert_equal(a.phas[0], 1)
25
26    def test_vector_assign_element_end(self):
27        a = cvec()
28        a.norm[-1] = 1
29        assert_equal(a.norm[-1], 1)
30        assert_equal(a.norm[len(a.norm)-1], 1)
31        a.phas[-1] = 1
32        assert_equal(a.phas[-1], 1)
33        assert_equal(a.phas[len(a.phas)-1], 1)
34
35    def test_assign_cvec_norm_slice(self):
36        spec = cvec(1024)
37        spec.norm[40:100] = 100
38        assert_equal(spec.norm[0:40], 0)
39        assert_equal(spec.norm[40:100], 100)
40        assert_equal(spec.norm[100:-1], 0)
41        assert_equal(spec.phas, 0)
42
43    def test_assign_cvec_phas_slice(self):
44        spec = cvec(1024)
45        spec.phas[39:-1] = -np.pi
46        assert_equal(spec.phas[0:39], 0)
47        assert_equal(spec.phas[39:-1], -np.pi)
48        assert_equal(spec.norm, 0)
49
50    def test_assign_cvec_with_other_cvec(self):
51        """ check dest cvec is still reachable after source was deleted """
52        spec = cvec(1024)
53        a = np.random.rand(1024//2+1).astype(float_type)
54        b = np.random.rand(1024//2+1).astype(float_type)
55        spec.norm = a
56        spec.phas = b
57        new_spec = spec
58        del spec
59        assert_equal(a, new_spec.norm)
60        assert_equal(b, new_spec.phas)
61        assert_equal(id(a), id(new_spec.norm))
62        assert_equal(id(b), id(new_spec.phas))
63
64    def test_pass_to_numpy(self):
65        spec = cvec(1024)
66        norm = spec.norm
67        phas = spec.phas
68        del spec
69        new_spec = cvec(1024)
70        new_spec.norm = norm
71        new_spec.phas = phas
72        assert_equal(norm, new_spec.norm)
73        assert_equal(phas, new_spec.phas)
74        assert_equal(id(norm), id(new_spec.norm))
75        assert_equal(id(phas), id(new_spec.phas))
76        del norm
77        del phas
78        assert_equal(new_spec.norm, 0.)
79        assert_equal(new_spec.phas, 0.)
80        del new_spec
81
82    def test_assign_norm_too_large(self):
83        a = cvec(512)
84        b = fvec(512//2+1 + 4)
85        with self.assertRaises(ValueError):
86            a.norm = b
87
88    def test_assign_norm_too_small(self):
89        a = cvec(512)
90        b = fvec(512//2+1 - 4)
91        with self.assertRaises(ValueError):
92            a.norm = b
93
94    def test_assign_phas_too_large(self):
95        a = cvec(512)
96        b = fvec(512//2+1 + 4)
97        with self.assertRaises(ValueError):
98            a.phas = b
99
100    def test_assign_phas_too_small(self):
101        a = cvec(512)
102        b = fvec(512//2+1 - 4)
103        with self.assertRaises(ValueError):
104            a.phas = b
105
106    def test_cvec_repr(self):
107        win_s = 512
108        c = cvec(win_s)
109        expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1)
110        self.assertEqual(repr(c), expected_repr)
111
112class aubio_cvec_wrong_norm_input(TestCase):
113
114    def test_wrong_length(self):
115        with self.assertRaises(ValueError):
116            cvec(-1)
117
118    def test_set_norm_with_scalar(self):
119        a = cvec(512)
120        with self.assertRaises(ValueError):
121            a.norm = 1
122
123    def test_set_norm_with_scalar_array(self):
124        a = cvec(512)
125        with self.assertRaises(ValueError):
126            a.norm = np.ndarray(1, dtype = 'int')
127
128    def test_set_norm_with_int_array(self):
129        a = cvec(512)
130        with self.assertRaises(ValueError):
131            a.norm = np.zeros(512//2+1, dtype = 'int')
132
133    def test_set_norm_with_wrong_float_array(self):
134        a = cvec(512)
135        with self.assertRaises(ValueError):
136            a.norm = np.zeros(512//2+1, dtype = wrong_type)
137
138    def test_set_norm_with_wrong_2d_array(self):
139        a = cvec(512)
140        with self.assertRaises(ValueError):
141            a.norm = np.zeros((512//2+1, 2), dtype = float_type)
142
143if __name__ == '__main__':
144    main()
Note: See TracBrowser for help on using the repository browser.