shithub: aubio

Download patch

ref: c4b2183510f37285509236ed14bdff7f4e3c6957
parent: fbcee4fde322612474a48791b3b7f00dcb6d4888
author: Paul Brossier <piem@piem.org>
date: Sun Apr 24 20:53:03 EDT 2016

python/tests: use aubio.float_type

--- a/python/tests/test_filterbank.py
+++ b/python/tests/test_filterbank.py
@@ -5,7 +5,7 @@
 from numpy import random
 from math import pi
 from numpy import array
-from aubio import cvec, filterbank
+from aubio import cvec, filterbank, float_type
 from utils import array_from_text_file
 
 class aubio_filterbank_test_case(TestCase):
@@ -16,7 +16,7 @@
 
   def test_set_coeffs(self):
     f = filterbank(40, 512)
-    r = random.random([40, int(512 / 2) + 1]).astype('float32')
+    r = random.random([40, int(512 / 2) + 1]).astype(float_type)
     f.set_coeffs(r)
     assert_equal (r, f.get_coeffs())
 
@@ -35,16 +35,16 @@
   def test_random_norm(self):
     f = filterbank(40, 512)
     c = cvec(512)
-    c.norm[:] = random.random((int(512 / 2) + 1,)).astype('float32')
+    c.norm[:] = random.random((int(512 / 2) + 1,)).astype(float_type)
     assert_equal( f(c), 0)
 
   def test_random_coeffs(self):
     f = filterbank(40, 512)
     c = cvec(512)
-    r = random.random([40, int(512 / 2) + 1]).astype('float32')
+    r = random.random([40, int(512 / 2) + 1]).astype(float_type)
     r /= r.sum()
     f.set_coeffs(r)
-    c.norm[:] = random.random((int(512 / 2) + 1,)).astype('float32')
+    c.norm[:] = random.random((int(512 / 2) + 1,)).astype(float_type)
     assert_equal ( f(c) < 1., True )
     assert_equal ( f(c) > 0., True )
 
@@ -52,7 +52,7 @@
     f = filterbank(40, 512)
     c = cvec(512)
     f.set_mel_coeffs_slaney(44100)
-    c.norm[:] = random.random((int(512 / 2) + 1,)).astype('float32')
+    c.norm[:] = random.random((int(512 / 2) + 1,)).astype(float_type)
     assert_equal ( f(c) < 1., True )
     assert_equal ( f(c) > 0., True )
 
--- a/python/tests/test_filterbank_mel.py
+++ b/python/tests/test_filterbank_mel.py
@@ -3,7 +3,7 @@
 from numpy.testing import TestCase, run_module_suite
 from numpy.testing import assert_equal, assert_almost_equal
 from numpy import array, shape
-from aubio import cvec, filterbank
+from aubio import cvec, filterbank, float_type
 
 class aubio_filterbank_mel_test_case(TestCase):
 
@@ -27,7 +27,7 @@
   def test_triangle_freqs_zeros(self):
     f = filterbank(9, 1024)
     freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000]
-    freqs = array(freq_list, dtype = 'float32')
+    freqs = array(freq_list, dtype = float_type)
     f.set_triangle_bands(freqs, 48000)
     f.get_coeffs().T
     assert_equal ( f(cvec(1024)), 0)
@@ -35,7 +35,7 @@
   def test_triangle_freqs_ones(self):
     f = filterbank(9, 1024)
     freq_list = [40, 80, 200, 400, 800, 1600, 3200, 6400, 12800, 15000, 24000]
-    freqs = array(freq_list, dtype = 'float32')
+    freqs = array(freq_list, dtype = float_type)
     f.set_triangle_bands(freqs, 48000)
     f.get_coeffs().T
     spec = cvec(1024)
--- a/python/tests/test_fvec.py
+++ b/python/tests/test_fvec.py
@@ -3,8 +3,11 @@
 from numpy.testing import TestCase, run_module_suite
 from numpy.testing import assert_equal, assert_almost_equal
 from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
+from aubio import float_type
 from numpy import array, shape
 
+wrong_type = 'float32' if float_type == 'float64' else 'float64'
+
 default_size = 512
 
 class aubio_fvec_test_case(TestCase):
@@ -11,13 +14,13 @@
 
     def test_vector_created_with_zeroes(self):
         a = fvec(10)
-        assert a.dtype == 'float32'
+        assert a.dtype == float_type
         assert a.shape == (10,)
         assert_equal (a, 0)
 
     def test_vector_create_with_list(self):
         a = fvec([0,1,2,3])
-        assert a.dtype == 'float32'
+        assert a.dtype == float_type
         assert a.shape == (4,)
         assert_equal (list(range(4)), a)
 
@@ -57,7 +60,7 @@
         self.assertEqual (alpha_norm(a, 1), 0.5)
         a[1] = 1
         self.assertEqual (alpha_norm(a, 1), 1)
-        a = array([0, 1], dtype='float32')
+        a = array([0, 1], dtype=float_type)
         from math import sqrt
         assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.)
 
@@ -66,13 +69,13 @@
 
     def test_alpha_norm_of_array_of_float32(self):
         # check scalar fails
-        a = array(1, dtype = 'float32')
+        a = array(1, dtype = float_type)
         self.assertRaises (ValueError, alpha_norm, a, 1)
         # check 2d array fails
-        a = array([[2],[4]], dtype = 'float32')
+        a = array([[2],[4]], dtype = float_type)
         self.assertRaises (ValueError, alpha_norm, a, 1)
         # check 1d array
-        a = array(range(10), dtype = 'float32')
+        a = array(range(10), dtype = float_type)
         self.assertEqual (alpha_norm(a, 1), 4.5)
 
     def test_alpha_norm_of_array_of_int(self):
@@ -88,31 +91,31 @@
         self.assertRaises (ValueError, alpha_norm, a, 1)
 
     def test_zero_crossing_rate(self):
-        a = array([0,1,-1], dtype='float32')
+        a = array([0,1,-1], dtype=float_type)
         assert_almost_equal (zero_crossing_rate(a), 1./3. )
-        a = array([0.]*100, dtype='float32')
+        a = array([0.]*100, dtype=float_type)
         self.assertEqual (zero_crossing_rate(a), 0 )
-        a = array([-1.]*100, dtype='float32')
+        a = array([-1.]*100, dtype=float_type)
         self.assertEqual (zero_crossing_rate(a), 0 )
-        a = array([1.]*100, dtype='float32')
+        a = array([1.]*100, dtype=float_type)
         self.assertEqual (zero_crossing_rate(a), 0 )
 
     def test_alpha_norm_of_array_of_float64(self):
         # check scalar fail
-        a = array(1, dtype = 'float64')
+        a = array(1, dtype = wrong_type)
         self.assertRaises (ValueError, alpha_norm, a, 1)
         # check 3d array fail
-        a = array([[[1,2],[3,4]]], dtype = 'float64')
+        a = array([[[1,2],[3,4]]], dtype = wrong_type)
         self.assertRaises (ValueError, alpha_norm, a, 1)
         # check float64 1d array fail
-        a = array(list(range(10)), dtype = 'float64')
+        a = array(list(range(10)), dtype = wrong_type)
         self.assertRaises (ValueError, alpha_norm, a, 1)
         # check float64 2d array fail
-        a = array([list(range(10)), list(range(10))], dtype = 'float64')
+        a = array([list(range(10)), list(range(10))], dtype = wrong_type)
         self.assertRaises (ValueError, alpha_norm, a, 1)
 
     def test_fvec_min_removal_of_array(self):
-        a = array([20,1,19], dtype='float32')
+        a = array([20,1,19], dtype=float_type)
         b = min_removal(a)
         assert_equal (array(b), [19, 0, 18])
         assert_equal (b, [19, 0, 18])
@@ -121,12 +124,12 @@
         assert_equal (a, b)
 
     def test_fvec_min_removal_of_array_float64(self):
-        a = array([20,1,19], dtype='float64')
+        a = array([20,1,19], dtype=wrong_type)
         self.assertRaises (ValueError, min_removal, a)
 
     def test_fvec_min_removal_of_fvec(self):
         a = fvec(3)
-        a = array([20, 1, 19], dtype = 'float32')
+        a = array([20, 1, 19], dtype = float_type)
         b = min_removal(a)
         assert_equal (array(b), [19, 0, 18])
         assert_equal (b, [19, 0, 18])
--- a/python/tests/test_musicutils.py
+++ b/python/tests/test_musicutils.py
@@ -7,7 +7,7 @@
 
 from aubio import window, level_lin, db_spl, silence_detection, level_detection
 
-from aubio import fvec
+from aubio import fvec, float_type
 
 class aubio_window(TestCase):
 
@@ -53,7 +53,7 @@
 
     def test_minus_ones_is_one(self):
         from numpy import ones
-        assert_equal(level_lin(-ones(1024, dtype="float32")), 1.)
+        assert_equal(level_lin(-ones(1024, dtype = float_type)), 1.)
 
 class aubio_db_spl(TestCase):
     def test_accept_fvec(self):
@@ -73,7 +73,7 @@
 
     def test_minus_ones_is_zero(self):
         from numpy import ones
-        assert_equal(db_spl(-ones(1024, dtype="float32")), 0.)
+        assert_equal(db_spl(-ones(1024, dtype = float_type)), 0.)
 
 class aubio_silence_detection(TestCase):
     def test_accept_fvec(self):
@@ -93,7 +93,7 @@
 
     def test_minus_ones_is_zero(self):
         from numpy import ones
-        assert silence_detection(ones(1024, dtype="float32"), -70) == 0
+        assert silence_detection(ones(1024, dtype = float_type), -70) == 0
 
 class aubio_level_detection(TestCase):
     def test_accept_fvec(self):
@@ -113,7 +113,7 @@
 
     def test_minus_ones_is_zero(self):
         from numpy import ones
-        assert level_detection(ones(1024, dtype="float32"), -70) == 0
+        assert level_detection(ones(1024, dtype = float_type), -70) == 0
 
 if __name__ == '__main__':
     from unittest import main
--- a/python/tests/test_pitch.py
+++ b/python/tests/test_pitch.py
@@ -4,7 +4,7 @@
 from numpy.testing import assert_equal, assert_almost_equal
 from numpy import random, sin, arange, mean, median, isnan
 from math import pi
-from aubio import fvec, pitch, freqtomidi
+from aubio import fvec, pitch, freqtomidi, float_type
 
 class aubio_pitch_Good_Values(TestCase):
 
@@ -50,7 +50,7 @@
         self.run_pitch(p, sinvec, freq)
 
     def build_sinusoid(self, length, freq, samplerate):
-        return sin( 2. * pi * arange(length).astype('float32') * freq / samplerate)
+        return sin( 2. * pi * arange(length).astype(float_type) * freq / samplerate)
 
     def run_pitch(self, p, input_vec, freq):
         count = 0
--- a/python/tests/test_specdesc.py
+++ b/python/tests/test_specdesc.py
@@ -2,7 +2,7 @@
 
 from numpy.testing import TestCase, assert_equal, assert_almost_equal
 from numpy import random, arange, log, zeros
-from aubio import specdesc, cvec
+from aubio import specdesc, cvec, float_type
 from math import pi
 
 methods = ["default",
@@ -37,8 +37,8 @@
           spec.norm[1] = 1./2.
           #print "%20s" % method, str(o(spec))
           o(spec)
-          spec.norm = random.random_sample((len(spec.norm),)).astype('float32')
-          spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
+          spec.norm = random.random_sample((len(spec.norm),)).astype(float_type)
+          spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
           #print "%20s" % method, str(o(spec))
           assert (o(spec) != 0.)
 
@@ -60,7 +60,7 @@
         spec = cvec(buf_size)
         # phase of zeros is zero
         assert_equal (o(spec), 0.)
-        spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
+        spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
         # phase of random is not zero
         spec.norm[:] = 1
         assert (o(spec) != 0.)
@@ -70,7 +70,7 @@
         spec = cvec(buf_size)
         # specdiff of zeros is zero
         assert_equal (o(spec), 0.)
-        spec.phas = random.random_sample((len(spec.phas),)).astype('float32')
+        spec.phas = random.random_sample((len(spec.phas),)).astype(float_type)
         # phase of random is not zero
         spec.norm[:] = 1
         assert (o(spec) != 0.)
@@ -79,7 +79,7 @@
         o = specdesc("hfc")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length, dtype='float32')
+        a = arange(c.length, dtype=float_type)
         c.norm = a
         assert_equal (a, c.norm)
         assert_equal ( sum(a*(a+1)), o(c))
@@ -88,7 +88,7 @@
         o = specdesc("complex")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length, dtype='float32')
+        a = arange(c.length, dtype=float_type)
         c.norm = a
         assert_equal (a, c.norm)
         # the previous run was on zeros, so previous frames are still 0
@@ -101,7 +101,7 @@
         o = specdesc("kl")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length, dtype='float32')
+        a = arange(c.length, dtype=float_type)
         c.norm = a
         assert_almost_equal( sum(a * log(1.+ a/1.e-1 ) ) / o(c), 1., decimal=6)
 
@@ -109,7 +109,7 @@
         o = specdesc("mkl")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length, dtype='float32')
+        a = arange(c.length, dtype=float_type)
         c.norm = a
         assert_almost_equal( sum(log(1.+ a/1.e-1 ) ) / o(c), 1, decimal=6)
 
@@ -117,11 +117,11 @@
         o = specdesc("specflux")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length, dtype='float32')
+        a = arange(c.length, dtype=float_type)
         c.norm = a
         assert_equal( sum(a), o(c))
         assert_equal( 0, o(c))
-        c.norm = zeros(c.length, dtype='float32')
+        c.norm = zeros(c.length, dtype=float_type)
         assert_equal( 0, o(c))
 
     def test_centroid(self):
@@ -129,7 +129,7 @@
         c = cvec()
         # make sure centroid of zeros is zero
         assert_equal( 0., o(c))
-        a = arange(c.length, dtype='float32')
+        a = arange(c.length, dtype=float_type)
         c.norm = a
         centroid = sum(a*a) / sum(a)
         assert_almost_equal (centroid, o(c), decimal = 2)
@@ -140,7 +140,7 @@
     def test_spread(self):
         o = specdesc("spread")
         c = cvec(2048)
-        ramp = arange(c.length, dtype='float32')
+        ramp = arange(c.length, dtype=float_type)
         assert_equal( 0., o(c))
 
         a = ramp
@@ -153,7 +153,7 @@
         o = specdesc("skewness")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length, dtype='float32')
+        a = arange(c.length, dtype=float_type)
         c.norm = a
         centroid = sum(a*a) / sum(a)
         spread = sum( (a - centroid)**2 *a) / sum(a)
@@ -167,7 +167,7 @@
         o = specdesc("kurtosis")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length, dtype='float32')
+        a = arange(c.length, dtype=float_type)
         c.norm = a
         centroid = sum(a*a) / sum(a)
         spread = sum( (a - centroid)**2 *a) / sum(a)
@@ -178,8 +178,8 @@
         o = specdesc("slope")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length * 2, 0, -2, dtype='float32')
-        k = arange(c.length, dtype='float32')
+        a = arange(c.length * 2, 0, -2, dtype=float_type)
+        k = arange(c.length, dtype=float_type)
         c.norm = a
         num = len(a) * sum(k*a) - sum(k)*sum(a)
         den = (len(a) * sum(k**2) - sum(k)**2)
@@ -186,7 +186,7 @@
         slope = num/den/sum(a)
         assert_almost_equal (slope, o(c), decimal = 5)
 
-        a = arange(0, c.length * 2, +2, dtype='float32')
+        a = arange(0, c.length * 2, +2, dtype=float_type)
         c.norm = a
         num = len(a) * sum(k*a) - sum(k)*sum(a)
         den = (len(a) * sum(k**2) - sum(k)**2)
@@ -193,7 +193,7 @@
         slope = num/den/sum(a)
         assert_almost_equal (slope, o(c), decimal = 5)
 
-        a = arange(0, c.length * 2, +2, dtype='float32')
+        a = arange(0, c.length * 2, +2, dtype=float_type)
         c.norm = a * 2
         assert_almost_equal (slope, o(c), decimal = 5)
 
@@ -201,18 +201,18 @@
         o = specdesc("decrease")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length * 2, 0, -2, dtype='float32')
-        k = arange(c.length, dtype='float32')
+        a = arange(c.length * 2, 0, -2, dtype=float_type)
+        k = arange(c.length, dtype=float_type)
         c.norm = a
         decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
         assert_almost_equal (decrease, o(c), decimal = 5)
 
-        a = arange(0, c.length * 2, +2, dtype='float32')
+        a = arange(0, c.length * 2, +2, dtype=float_type)
         c.norm = a
         decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
         assert_almost_equal (decrease, o(c), decimal = 5)
 
-        a = arange(0, c.length * 2, +2, dtype='float32')
+        a = arange(0, c.length * 2, +2, dtype=float_type)
         c.norm = a * 2
         decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 
         assert_almost_equal (decrease, o(c), decimal = 5)
@@ -221,8 +221,8 @@
         o = specdesc("rolloff")
         c = cvec()
         assert_equal( 0., o(c))
-        a = arange(c.length * 2, 0, -2, dtype='float32')
-        k = arange(c.length, dtype='float32')
+        a = arange(c.length * 2, 0, -2, dtype=float_type)
+        k = arange(c.length, dtype=float_type)
         c.norm = a
         cumsum = .95*sum(a*a)
         i = 0; rollsum = 0