History

python/tests: use aubio.float_type

Paul Brossier committed 10 years ago

Commit c4b21831 parent fbcee4f6 files · +61 −58Browse files

Showing the changes to python (6 of 6 files). Show all

Unified · Split

6 files
  1. python/tests/test_filterbank.py +6 −6
  2. python/tests/test_filterbank_mel.py +3 −3
  3. python/tests/test_fvec.py +20 −17
  4. python/tests/test_musicutils.py +5 −5
  5. python/tests/test_pitch.py +2 −2
  6. python/tests/test_specdesc.py +25 −25
python/tests/test_filterbank.py+6−6
@@ -5,7 +5,7 @@ from numpy.testing import assert_equal, assert_almost_equal · 4 unmodified lines
55 from numpy import random
66 from math import pi
77 from numpy import array
8−from aubio import cvec, filterbank
8+from aubio import cvec, filterbank, float_type
99 from utils import array_from_text_file
1010
1111 class aubio_filterbank_test_case(TestCase):
@@ -16,7 +16,7 @@ class aubio_filterbank_test_case(TestCase): · 4 unmodified lines
1616
1717 def test_set_coeffs(self):
1818 f = filterbank(40, 512)
19− r = random.random([40, int(512 / 2) + 1]).astype('float32')
19+ r = random.random([40, int(512 / 2) + 1]).astype(float_type)
2020 f.set_coeffs(r)
2121 assert_equal (r, f.get_coeffs())
2222
@@ -35,16 +35,16 @@ class aubio_filterbank_test_case(TestCase): · 12 unmodified lines
3535 def test_random_norm(self):
3636 f = filterbank(40, 512)
3737 c = cvec(512)
38− c.norm[:] = random.random((int(512 / 2) + 1,)).astype('float32')
38+ c.norm[:] = random.random((int(512 / 2) + 1,)).astype(float_type)
3939 assert_equal( f(c), 0)
4040
4141 def test_random_coeffs(self):
4242 f = filterbank(40, 512)
4343 c = cvec(512)
44− r = random.random([40, int(512 / 2) + 1]).astype('float32')
44+ r = random.random([40, int(512 / 2) + 1]).astype(float_type)
4545 r /= r.sum()
4646 f.set_coeffs(r)
47− c.norm[:] = random.random((int(512 / 2) + 1,)).astype('float32')
47+ c.norm[:] = random.random((int(512 / 2) + 1,)).astype(float_type)
4848 assert_equal ( f(c) < 1., True )
4949 assert_equal ( f(c) > 0., True )
5050
@@ -52,7 +52,7 @@ class aubio_filterbank_test_case(TestCase): · 1 unmodified line
5252 f = filterbank(40, 512)
5353 c = cvec(512)
5454 f.set_mel_coeffs_slaney(44100)
55− c.norm[:] = random.random((int(512 / 2) + 1,)).astype('float32')
55+ c.norm[:] = random.random((int(512 / 2) + 1,)).astype(float_type)
5656 assert_equal ( f(c) < 1., True )
5757 assert_equal ( f(c) > 0., True )
5858
python/tests/test_filterbank_mel.py+3−3
@@ -3,7 +3,7 @@ · 2 unmodified lines
33 from numpy.testing import TestCase, run_module_suite
44 from numpy.testing import assert_equal, assert_almost_equal
55 from numpy import array, shape
6−from aubio import cvec, filterbank
6+from aubio import cvec, filterbank, float_type
77
88 class aubio_filterbank_mel_test_case(TestCase):
99
@@ -27,7 +27,7 @@ class aubio_filterbank_mel_test_case(TestCase): · 17 unmodified lines
2727 def test_triangle_freqs_zeros(self):
2828 f = filterbank(9, 1024)
2929 freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000]
30− freqs = array(freq_list, dtype = 'float32')
30+ freqs = array(freq_list, dtype = float_type)
3131 f.set_triangle_bands(freqs, 48000)
3232 f.get_coeffs().T
3333 assert_equal ( f(cvec(1024)), 0)
@@ -35,7 +35,7 @@ class aubio_filterbank_mel_test_case(TestCase): · 1 unmodified line
3535 def test_triangle_freqs_ones(self):
3636 f = filterbank(9, 1024)
3737 freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000]
38− freqs = array(freq_list, dtype = 'float32')
38+ freqs = array(freq_list, dtype = float_type)
3939 f.set_triangle_bands(freqs, 48000)
4040 f.get_coeffs().T
4141 spec = cvec(1024)
python/tests/test_fvec.py+20−17
@@ -3,21 +3,24 @@ · 2 unmodified lines
33 from numpy.testing import TestCase, run_module_suite
44 from numpy.testing import assert_equal, assert_almost_equal
55 from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
6+from aubio import float_type
67 from numpy import array, shape
78
9+wrong_type = 'float32' if float_type == 'float64' else 'float64'
10+
811 default_size = 512
912
1013 class aubio_fvec_test_case(TestCase):
1114
1215 def test_vector_created_with_zeroes(self):
1316 a = fvec(10)
14− assert a.dtype == 'float32'
17+ assert a.dtype == float_type
1518 assert a.shape == (10,)
1619 assert_equal (a, 0)
1720
1821 def test_vector_create_with_list(self):
1922 a = fvec([0,1,2,3])
20− assert a.dtype == 'float32'
23+ assert a.dtype == float_type
2124 assert a.shape == (4,)
2225 assert_equal (list(range(4)), a)
2326
@@ -57,7 +60,7 @@ class aubio_fvec_test_case(TestCase): · 33 unmodified lines
5760 self.assertEqual (alpha_norm(a, 1), 0.5)
5861 a[1] = 1
5962 self.assertEqual (alpha_norm(a, 1), 1)
60− a = array([0, 1], dtype='float32')
63+ a = array([0, 1], dtype=float_type)
6164 from math import sqrt
6265 assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.)
6366
@@ -66,13 +69,13 @@ class aubio_fvec_test_case(TestCase): · 2 unmodified lines
6669
6770 def test_alpha_norm_of_array_of_float32(self):
6871 # check scalar fails
69− a = array(1, dtype = 'float32')
72+ a = array(1, dtype = float_type)
7073 self.assertRaises (ValueError, alpha_norm, a, 1)
7174 # check 2d array fails
72− a = array([[2],[4]], dtype = 'float32')
75+ a = array([[2],[4]], dtype = float_type)
7376 self.assertRaises (ValueError, alpha_norm, a, 1)
7477 # check 1d array
75− a = array(range(10), dtype = 'float32')
78+ a = array(range(10), dtype = float_type)
7679 self.assertEqual (alpha_norm(a, 1), 4.5)
7780
7881 def test_alpha_norm_of_array_of_int(self):
@@ -88,31 +91,31 @@ class aubio_fvec_test_case(TestCase): · 9 unmodified lines
8891 self.assertRaises (ValueError, alpha_norm, a, 1)
8992
9093 def test_zero_crossing_rate(self):
91− a = array([0,1,-1], dtype='float32')
94+ a = array([0,1,-1], dtype=float_type)
9295 assert_almost_equal (zero_crossing_rate(a), 1./3. )
93− a = array([0.]*100, dtype='float32')
96+ a = array([0.]*100, dtype=float_type)
9497 self.assertEqual (zero_crossing_rate(a), 0 )
95− a = array([-1.]*100, dtype='float32')
98+ a = array([-1.]*100, dtype=float_type)
9699 self.assertEqual (zero_crossing_rate(a), 0 )
97− a = array([1.]*100, dtype='float32')
100+ a = array([1.]*100, dtype=float_type)
98101 self.assertEqual (zero_crossing_rate(a), 0 )
99102
100103 def test_alpha_norm_of_array_of_float64(self):
101104 # check scalar fail
102− a = array(1, dtype = 'float64')
105+ a = array(1, dtype = wrong_type)
103106 self.assertRaises (ValueError, alpha_norm, a, 1)
104107 # check 3d array fail
105− a = array([[[1,2],[3,4]]], dtype = 'float64')
108+ a = array([[[1,2],[3,4]]], dtype = wrong_type)
106109 self.assertRaises (ValueError, alpha_norm, a, 1)
107110 # check float64 1d array fail
108− a = array(list(range(10)), dtype = 'float64')
111+ a = array(list(range(10)), dtype = wrong_type)
109112 self.assertRaises (ValueError, alpha_norm, a, 1)
110113 # check float64 2d array fail
111− a = array([list(range(10)), list(range(10))], dtype = 'float64')
114+ a = array([list(range(10)), list(range(10))], dtype = wrong_type)
112115 self.assertRaises (ValueError, alpha_norm, a, 1)
113116
114117 def test_fvec_min_removal_of_array(self):
115− a = array([20,1,19], dtype='float32')
118+ a = array([20,1,19], dtype=float_type)
116119 b = min_removal(a)
117120 assert_equal (array(b), [19, 0, 18])
118121 assert_equal (b, [19, 0, 18])
@@ -121,12 +124,12 @@ class aubio_fvec_test_case(TestCase): · 2 unmodified lines
121124 assert_equal (a, b)
122125
123126 def test_fvec_min_removal_of_array_float64(self):
124− a = array([20,1,19], dtype='float64')
127+ a = array([20,1,19], dtype=wrong_type)
125128 self.assertRaises (ValueError, min_removal, a)
126129
127130 def test_fvec_min_removal_of_fvec(self):
128131 a = fvec(3)
129− a = array([20, 1, 19], dtype = 'float32')
132+ a = array([20, 1, 19], dtype = float_type)
130133 b = min_removal(a)
131134 assert_equal (array(b), [19, 0, 18])
132135 assert_equal (b, [19, 0, 18])
python/tests/test_musicutils.py+5−5
@@ -7,7 +7,7 @@ from math import pi · 6 unmodified lines
77
88 from aubio import window, level_lin, db_spl, silence_detection, level_detection
99
10−from aubio import fvec
10+from aubio import fvec, float_type
1111
1212 class aubio_window(TestCase):
1313
@@ -53,7 +53,7 @@ class aubio_level_lin(TestCase): · 39 unmodified lines
5353
5454 def test_minus_ones_is_one(self):
5555 from numpy import ones
56− assert_equal(level_lin(-ones(1024, dtype="float32")), 1.)
56+ assert_equal(level_lin(-ones(1024, dtype = float_type)), 1.)
5757
5858 class aubio_db_spl(TestCase):
5959 def test_accept_fvec(self):
@@ -73,7 +73,7 @@ class aubio_db_spl(TestCase): · 13 unmodified lines
7373
7474 def test_minus_ones_is_zero(self):
7575 from numpy import ones
76− assert_equal(db_spl(-ones(1024, dtype="float32")), 0.)
76+ assert_equal(db_spl(-ones(1024, dtype = float_type)), 0.)
7777
7878 class aubio_silence_detection(TestCase):
7979 def test_accept_fvec(self):
@@ -93,7 +93,7 @@ class aubio_silence_detection(TestCase): · 13 unmodified lines
9393
9494 def test_minus_ones_is_zero(self):
9595 from numpy import ones
96− assert silence_detection(ones(1024, dtype="float32"), -70) == 0
96+ assert silence_detection(ones(1024, dtype = float_type), -70) == 0
9797
9898 class aubio_level_detection(TestCase):
9999 def test_accept_fvec(self):
@@ -113,7 +113,7 @@ class aubio_level_detection(TestCase): · 13 unmodified lines
113113
114114 def test_minus_ones_is_zero(self):
115115 from numpy import ones
116− assert level_detection(ones(1024, dtype="float32"), -70) == 0
116+ assert level_detection(ones(1024, dtype = float_type), -70) == 0
117117
118118 if __name__ == '__main__':
119119 from unittest import main
python/tests/test_pitch.py+2−2
@@ -4,7 +4,7 @@ from unittest import TestCase · 3 unmodified lines
44 from numpy.testing import assert_equal, assert_almost_equal
55 from numpy import random, sin, arange, mean, median, isnan
66 from math import pi
7−from aubio import fvec, pitch, freqtomidi
7+from aubio import fvec, pitch, freqtomidi, float_type
88
99 class aubio_pitch_Good_Values(TestCase):
1010
@@ -50,7 +50,7 @@ class aubio_pitch_Sinusoid(TestCase): · 39 unmodified lines
5050 self.run_pitch(p, sinvec, freq)
5151
5252 def build_sinusoid(self, length, freq, samplerate):
53− return sin( 2. * pi * arange(length).astype('float32') * freq / samplerate)
53+ return sin( 2. * pi * arange(length).astype(float_type) * freq / samplerate)
5454
5555 def run_pitch(self, p, input_vec, freq):
5656 count = 0
python/tests/test_specdesc.py+25−25
@@ -2,7 +2,7 @@ · 1 unmodified line
22
33 from numpy.testing import TestCase, assert_equal, assert_almost_equal
44 from numpy import random, arange, log, zeros
5−from aubio import specdesc, cvec
5+from aubio import specdesc, cvec, float_type
66 from math import pi
77
88 methods = ["default",
@@ -37,8 +37,8 @@ class aubio_specdesc(TestCase): · 28 unmodified lines
3737 spec.norm[1] = 1./2.
3838 #print "%20s" % method, str(o(spec))
3939 o(spec)
40− spec.norm = random.random_sample((len(spec.norm),)).astype('float32')
41− spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
40+ spec.norm = random.random_sample((len(spec.norm),)).astype(float_type)
41+ spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
4242 #print "%20s" % method, str(o(spec))
4343 assert (o(spec) != 0.)
4444
@@ -60,7 +60,7 @@ class aubio_specdesc(TestCase): · 15 unmodified lines
6060 spec = cvec(buf_size)
6161 # phase of zeros is zero
6262 assert_equal (o(spec), 0.)
63− spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
63+ spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
6464 # phase of random is not zero
6565 spec.norm[:] = 1
6666 assert (o(spec) != 0.)
@@ -70,7 +70,7 @@ class aubio_specdesc(TestCase): · 3 unmodified lines
7070 spec = cvec(buf_size)
7171 # specdiff of zeros is zero
7272 assert_equal (o(spec), 0.)
73− spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
73+ spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
7474 # phase of random is not zero
7575 spec.norm[:] = 1
7676 assert (o(spec) != 0.)
@@ -79,7 +79,7 @@ class aubio_specdesc(TestCase): · 2 unmodified lines
7979 o = specdesc("hfc")
8080 c = cvec()
8181 assert_equal( 0., o(c))
82− a = arange(c.length, dtype='float32')
82+ a = arange(c.length, dtype=float_type)
8383 c.norm = a
8484 assert_equal (a, c.norm)
8585 assert_equal ( sum(a*(a+1)), o(c))
@@ -88,7 +88,7 @@ class aubio_specdesc(TestCase): · 2 unmodified lines
8888 o = specdesc("complex")
8989 c = cvec()
9090 assert_equal( 0., o(c))
91− a = arange(c.length, dtype='float32')
91+ a = arange(c.length, dtype=float_type)
9292 c.norm = a
9393 assert_equal (a, c.norm)
9494 # the previous run was on zeros, so previous frames are still 0
@@ -101,7 +101,7 @@ class aubio_specdesc(TestCase): · 6 unmodified lines
101101 o = specdesc("kl")
102102 c = cvec()
103103 assert_equal( 0., o(c))
104− a = arange(c.length, dtype='float32')
104+ a = arange(c.length, dtype=float_type)
105105 c.norm = a
106106 assert_almost_equal( sum(a * log(1.+ a/1.e-1 ) ) / o(c), 1., decimal=6)
107107
@@ -109,7 +109,7 @@ class aubio_specdesc(TestCase): · 1 unmodified line
109109 o = specdesc("mkl")
110110 c = cvec()
111111 assert_equal( 0., o(c))
112− a = arange(c.length, dtype='float32')
112+ a = arange(c.length, dtype=float_type)
113113 c.norm = a
114114 assert_almost_equal( sum(log(1.+ a/1.e-1 ) ) / o(c), 1, decimal=6)
115115
@@ -117,11 +117,11 @@ class aubio_specdesc(TestCase): · 1 unmodified line
117117 o = specdesc("specflux")
118118 c = cvec()
119119 assert_equal( 0., o(c))
120− a = arange(c.length, dtype='float32')
120+ a = arange(c.length, dtype=float_type)
121121 c.norm = a
122122 assert_equal( sum(a), o(c))
123123 assert_equal( 0, o(c))
124− c.norm = zeros(c.length, dtype='float32')
124+ c.norm = zeros(c.length, dtype=float_type)
125125 assert_equal( 0, o(c))
126126
127127 def test_centroid(self):
@@ -129,7 +129,7 @@ class aubio_specdesc(TestCase): · 1 unmodified line
129129 c = cvec()
130130 # make sure centroid of zeros is zero
131131 assert_equal( 0., o(c))
132− a = arange(c.length, dtype='float32')
132+ a = arange(c.length, dtype=float_type)
133133 c.norm = a
134134 centroid = sum(a*a) / sum(a)
135135 assert_almost_equal (centroid, o(c), decimal = 2)
@@ -140,7 +140,7 @@ class aubio_specdesc(TestCase): · 4 unmodified lines
140140 def test_spread(self):
141141 o = specdesc("spread")
142142 c = cvec(2048)
143− ramp = arange(c.length, dtype='float32')
143+ ramp = arange(c.length, dtype=float_type)
144144 assert_equal( 0., o(c))
145145
146146 a = ramp
@@ -153,7 +153,7 @@ class aubio_specdesc(TestCase): · 6 unmodified lines
153153 o = specdesc("skewness")
154154 c = cvec()
155155 assert_equal( 0., o(c))
156− a = arange(c.length, dtype='float32')
156+ a = arange(c.length, dtype=float_type)
157157 c.norm = a
158158 centroid = sum(a*a) / sum(a)
159159 spread = sum( (a - centroid)**2 *a) / sum(a)
@@ -167,7 +167,7 @@ class aubio_specdesc(TestCase): · 7 unmodified lines
167167 o = specdesc("kurtosis")
168168 c = cvec()
169169 assert_equal( 0., o(c))
170− a = arange(c.length, dtype='float32')
170+ a = arange(c.length, dtype=float_type)
171171 c.norm = a
172172 centroid = sum(a*a) / sum(a)
173173 spread = sum( (a - centroid)**2 *a) / sum(a)
@@ -178,22 +178,22 @@ class aubio_specdesc(TestCase): · 4 unmodified lines
178178 o = specdesc("slope")
179179 c = cvec()
180180 assert_equal( 0., o(c))
181− a = arange(c.length * 2, 0, -2, dtype='float32')
182− k = arange(c.length, dtype='float32')
181+ a = arange(c.length * 2, 0, -2, dtype=float_type)
182+ k = arange(c.length, dtype=float_type)
183183 c.norm = a
184184 num = len(a) * sum(k*a) - sum(k)*sum(a)
185185 den = (len(a) * sum(k**2) - sum(k)**2)
186186 slope = num/den/sum(a)
187187 assert_almost_equal (slope, o(c), decimal = 5)
188188
189− a = arange(0, c.length * 2, +2, dtype='float32')
189+ a = arange(0, c.length * 2, +2, dtype=float_type)
190190 c.norm = a
191191 num = len(a) * sum(k*a) - sum(k)*sum(a)
192192 den = (len(a) * sum(k**2) - sum(k)**2)
193193 slope = num/den/sum(a)
194194 assert_almost_equal (slope, o(c), decimal = 5)
195195
196− a = arange(0, c.length * 2, +2, dtype='float32')
196+ a = arange(0, c.length * 2, +2, dtype=float_type)
197197 c.norm = a * 2
198198 assert_almost_equal (slope, o(c), decimal = 5)
199199
@@ -201,18 +201,18 @@ class aubio_specdesc(TestCase): · 1 unmodified line
201201 o = specdesc("decrease")
202202 c = cvec()
203203 assert_equal( 0., o(c))
204− a = arange(c.length * 2, 0, -2, dtype='float32')
205− k = arange(c.length, dtype='float32')
204+ a = arange(c.length * 2, 0, -2, dtype=float_type)
205+ k = arange(c.length, dtype=float_type)
206206 c.norm = a
207207 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
208208 assert_almost_equal (decrease, o(c), decimal = 5)
209209
210− a = arange(0, c.length * 2, +2, dtype='float32')
210+ a = arange(0, c.length * 2, +2, dtype=float_type)
211211 c.norm = a
212212 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
213213 assert_almost_equal (decrease, o(c), decimal = 5)
214214
215− a = arange(0, c.length * 2, +2, dtype='float32')
215+ a = arange(0, c.length * 2, +2, dtype=float_type)
216216 c.norm = a * 2
217217 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:])
218218 assert_almost_equal (decrease, o(c), decimal = 5)
@@ -221,8 +221,8 @@ class aubio_specdesc(TestCase): · 2 unmodified lines
221221 o = specdesc("rolloff")
222222 c = cvec()
223223 assert_equal( 0., o(c))
224− a = arange(c.length * 2, 0, -2, dtype='float32')
225− k = arange(c.length, dtype='float32')
224+ a = arange(c.length * 2, 0, -2, dtype=float_type)
225+ k = arange(c.length, dtype=float_type)
226226 c.norm = a
227227 cumsum = .95*sum(a*a)
228228 i = 0; rollsum = 0

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