History

python/tests/test_fvec.py: clean up and simplify

Paul Brossier committed 10 years ago

Commit 88554b91 parent b96a7b81 file · +87 −95Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/tests/test_fvec.py+87−95
@@ -1,10 +1,9 @@
11 #! /usr/bin/env python
22
3−from numpy.testing import TestCase, run_module_suite
4−from numpy.testing import assert_equal, assert_almost_equal
3+import numpy as np
4+from numpy.testing import TestCase, assert_equal, assert_almost_equal
55 from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
66 from aubio import float_type
7−from numpy import array, shape
87
98 wrong_type = 'float32' if float_type == 'float64' else 'float64'
109
@@ -16,13 +15,13 @@ class aubio_fvec_test_case(TestCase): · 5 unmodified lines
1615 a = fvec(10)
1716 assert a.dtype == float_type
1817 assert a.shape == (10,)
19− assert_equal (a, 0)
18+ assert_equal(a, 0)
2019
2120 def test_vector_create_with_list(self):
22− a = fvec([0,1,2,3])
21+ a = fvec([0, 1, 2, 3])
2322 assert a.dtype == float_type
2423 assert a.shape == (4,)
25− assert_equal (list(range(4)), a)
24+ assert_equal(list(range(4)), a)
2625
2726 def test_vector_assign_element(self):
2827 a = fvec(default_size)
@@ -37,113 +36,106 @@ class aubio_fvec_test_case(TestCase): · 8 unmodified lines
3736
3837 def test_vector(self):
3938 a = fvec()
40− a, len(a) #a.length
41− a[0]
42− array(a)
43− a = fvec(10)
39+ len(a)
40+ _ = a[0]
41+ np.array(a)
4442 a = fvec(1)
45− a.T
46− array(a).T
47− a = list(range(len(a)))
43+ a = fvec(10)
44+ _ = a.T
4845
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)
46+class aubio_fvec_wrong_values(TestCase):
5547
56− def test_alpha_norm_of_fvec(self):
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_negative_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) """
5758 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)
59+ self.assertRaises(IndexError, a.__getitem__, 3)
60+ self.assertRaises(IndexError, a.__getitem__, 2)
61+
62+class aubio_wrong_fvec_input(TestCase):
63+ """ uses min_removal to test PyAubio_IsValidVector """
64+
65+ def test_no_input(self):
66+ self.assertRaises(TypeError, min_removal)
67+
68+ def test_none(self):
69+ self.assertRaises(ValueError, min_removal, None)
70+
71+ def test_wrong_scalar(self):
72+ a = np.array(10, dtype=float_type)
73+ self.assertRaises(ValueError, min_removal, a)
74+
75+ def test_wrong_dimensions(self):
76+ a = np.array([[[1, 2], [3, 4]]], dtype=float_type)
77+ self.assertRaises(ValueError, min_removal, a)
78+
79+ def test_wrong_array_size(self):
80+ x = np.array([], dtype=float_type)
81+ self.assertRaises(ValueError, min_removal, x)
82+
83+ def test_wrong_type(self):
84+ a = np.zeros(10, dtype=wrong_type)
85+ self.assertRaises(ValueError, min_removal, a)
86+
87+ def test_wrong_list_input(self):
88+ self.assertRaises(ValueError, min_removal, [0., 1.])
89+
90+ def test_good_input(self):
91+ a = np.zeros(10, dtype=float_type)
92+ assert_equal(np.zeros(10, dtype=float_type), min_removal(a))
93+
94+class aubio_alpha_norm(TestCase):
95+
96+ def test_alpha_norm_of_random(self):
97+ x = np.random.rand(1024).astype(float_type)
98+ alpha = np.random.rand() * 5.
99+ x_alpha_norm = (np.sum(np.abs(x)**alpha)/len(x))**(1/alpha)
100+ assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm)
101+
102+class aubio_zero_crossing_rate_test(TestCase):
91103
92104 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)
105+ a = np.array([0, 1, -1], dtype=float_type)
106+ assert_almost_equal(zero_crossing_rate(a), 1./3.)
107+
108+ def test_zero_crossing_rate_zeros(self):
109+ a = np.zeros(100, dtype=float_type)
110+ self.assertEqual(zero_crossing_rate(a), 0)
111+
112+ def test_zero_crossing_rate_minus_ones(self):
113+ a = np.ones(100, dtype=float_type)
114+ self.assertEqual(zero_crossing_rate(a), 0)
115+
116+ def test_zero_crossing_rate_plus_ones(self):
117+ a = np.ones(100, dtype=float_type)
118+ self.assertEqual(zero_crossing_rate(a), 0)
119+
120+class aubio_fvec_min_removal(TestCase):
115121
116122 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)
123+ a = np.array([20, 1, 19], dtype=float_type)
132124 b = min_removal(a)
133− assert_equal (array(b), [19, 0, 18])
134− assert_equal (b, [19, 0, 18])
135− assert_equal (a, b)
125+ assert_equal(b, [19, 0, 18])
126+
127+class aubio_fvec_test_memory(TestCase):
136128
137129 def test_pass_to_numpy(self):
138130 a = fvec(10)
139131 a = 1.
140132 b = a
141133 del a
142− assert_equal (b, 1.)
134+ assert_equal(b, 1.)
143135 c = fvec(10)
144136 c = b
145137 del b
146− assert_equal (c, 1.)
138+ assert_equal(c, 1.)
147139 del c
148140
149141 if __name__ == '__main__':

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t