From 88554b9b6fd8d6283d4947014236fd224519210d Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Wed, 11 May 2016 09:40:50 +0200 Subject: [PATCH] python/tests/test_fvec.py: clean up and simplify --- python/tests/test_fvec.py | 182 ++++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 95 deletions(-) diff --git a/python/tests/test_fvec.py b/python/tests/test_fvec.py index 088fe68e..99984f48 100755 --- a/python/tests/test_fvec.py +++ b/python/tests/test_fvec.py @@ -1,10 +1,9 @@ #! /usr/bin/env python -from numpy.testing import TestCase, run_module_suite -from numpy.testing import assert_equal, assert_almost_equal +import numpy as np +from numpy.testing import TestCase, 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' @@ -16,13 +15,13 @@ class aubio_fvec_test_case(TestCase): a = fvec(10) assert a.dtype == float_type assert a.shape == (10,) - assert_equal (a, 0) + assert_equal(a, 0) def test_vector_create_with_list(self): - a = fvec([0,1,2,3]) + a = fvec([0, 1, 2, 3]) assert a.dtype == float_type assert a.shape == (4,) - assert_equal (list(range(4)), a) + assert_equal(list(range(4)), a) def test_vector_assign_element(self): a = fvec(default_size) @@ -37,113 +36,106 @@ class aubio_fvec_test_case(TestCase): def test_vector(self): a = fvec() - a, len(a) #a.length - a[0] - array(a) - a = fvec(10) + len(a) + _ = a[0] + np.array(a) a = fvec(1) - a.T - array(a).T - a = list(range(len(a))) + a = fvec(10) + _ = a.T - def test_wrong_values(self): - self.assertRaises (ValueError, fvec, -10) - - a = fvec(2) - self.assertRaises (IndexError, a.__getitem__, 3) - self.assertRaises (IndexError, a.__getitem__, 2) +class aubio_fvec_wrong_values(TestCase): - def test_alpha_norm_of_fvec(self): + def test_negative_length(self): + """ test creating fvec with negative length fails (pure python) """ + self.assertRaises(ValueError, fvec, -10) + + def test_negative_length(self): + """ test creating fvec with zero length fails (pure python) """ + self.assertRaises(ValueError, fvec, 0) + + def test_out_of_bound(self): + """ test assiging fvec out of bounds fails (pure python) """ a = fvec(2) - self.assertEqual (alpha_norm(a, 1), 0) - a[0] = 1 - self.assertEqual (alpha_norm(a, 1), 0.5) - a[1] = 1 - self.assertEqual (alpha_norm(a, 1), 1) - a = array([0, 1], dtype=float_type) - assert_almost_equal (alpha_norm(a, 2), .5*2.**.5) - - def test_alpha_norm_of_none(self): - self.assertRaises (ValueError, alpha_norm, None, 1) - - def test_alpha_norm_of_array_of_float32(self): - # check scalar fails - a = array(1, dtype = float_type) - self.assertRaises (ValueError, alpha_norm, a, 1) - # check 2d array fails - a = array([[2],[4]], dtype = float_type) - self.assertRaises (ValueError, alpha_norm, a, 1) - # check 1d array - a = array(range(10), dtype = float_type) - self.assertEqual (alpha_norm(a, 1), 4.5) - - def test_alpha_norm_of_array_of_int(self): - a = array(1, dtype = 'int') - self.assertRaises (ValueError, alpha_norm, a, 1) - a = array([[[1,2],[3,4]]], dtype = 'int') - self.assertRaises (ValueError, alpha_norm, a, 1) - a = array(range(10), dtype = 'int') - self.assertRaises (ValueError, alpha_norm, a, 1) - - def test_alpha_norm_of_array_of_string (self): - a = "hello" - self.assertRaises (ValueError, alpha_norm, a, 1) + self.assertRaises(IndexError, a.__getitem__, 3) + self.assertRaises(IndexError, a.__getitem__, 2) + +class aubio_wrong_fvec_input(TestCase): + """ uses min_removal to test PyAubio_IsValidVector """ + + def test_no_input(self): + self.assertRaises(TypeError, min_removal) + + def test_none(self): + self.assertRaises(ValueError, min_removal, None) + + def test_wrong_scalar(self): + a = np.array(10, dtype=float_type) + self.assertRaises(ValueError, min_removal, a) + + def test_wrong_dimensions(self): + a = np.array([[[1, 2], [3, 4]]], dtype=float_type) + self.assertRaises(ValueError, min_removal, a) + + def test_wrong_array_size(self): + x = np.array([], dtype=float_type) + self.assertRaises(ValueError, min_removal, x) + + def test_wrong_type(self): + a = np.zeros(10, dtype=wrong_type) + self.assertRaises(ValueError, min_removal, a) + + def test_wrong_list_input(self): + self.assertRaises(ValueError, min_removal, [0., 1.]) + + def test_good_input(self): + a = np.zeros(10, dtype=float_type) + assert_equal(np.zeros(10, dtype=float_type), min_removal(a)) + +class aubio_alpha_norm(TestCase): + + def test_alpha_norm_of_random(self): + x = np.random.rand(1024).astype(float_type) + alpha = np.random.rand() * 5. + x_alpha_norm = (np.sum(np.abs(x)**alpha)/len(x))**(1/alpha) + assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm) + +class aubio_zero_crossing_rate_test(TestCase): def test_zero_crossing_rate(self): - a = array([0,1,-1], dtype=float_type) - assert_almost_equal (zero_crossing_rate(a), 1./3. ) - a = array([0.]*100, dtype=float_type) - self.assertEqual (zero_crossing_rate(a), 0 ) - a = array([-1.]*100, dtype=float_type) - self.assertEqual (zero_crossing_rate(a), 0 ) - 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 = wrong_type) - self.assertRaises (ValueError, alpha_norm, a, 1) - # check 3d array fail - 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 = wrong_type) - self.assertRaises (ValueError, alpha_norm, a, 1) - # check float64 2d array fail - a = array([list(range(10)), list(range(10))], dtype = wrong_type) - self.assertRaises (ValueError, alpha_norm, a, 1) + a = np.array([0, 1, -1], dtype=float_type) + assert_almost_equal(zero_crossing_rate(a), 1./3.) + + def test_zero_crossing_rate_zeros(self): + a = np.zeros(100, dtype=float_type) + self.assertEqual(zero_crossing_rate(a), 0) + + def test_zero_crossing_rate_minus_ones(self): + a = np.ones(100, dtype=float_type) + self.assertEqual(zero_crossing_rate(a), 0) + + def test_zero_crossing_rate_plus_ones(self): + a = np.ones(100, dtype=float_type) + self.assertEqual(zero_crossing_rate(a), 0) + +class aubio_fvec_min_removal(TestCase): def test_fvec_min_removal_of_array(self): - 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]) - assert_equal (a, b) - a[0] = 0 - assert_equal (a, b) - - def test_fvec_min_removal_of_array_float64(self): - 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 = float_type) + a = np.array([20, 1, 19], dtype=float_type) b = min_removal(a) - assert_equal (array(b), [19, 0, 18]) - assert_equal (b, [19, 0, 18]) - assert_equal (a, b) + assert_equal(b, [19, 0, 18]) + +class aubio_fvec_test_memory(TestCase): def test_pass_to_numpy(self): a = fvec(10) a = 1. b = a del a - assert_equal (b, 1.) + assert_equal(b, 1.) c = fvec(10) c = b del b - assert_equal (c, 1.) + assert_equal(c, 1.) del c if __name__ == '__main__': -- 2.11.0