Changeset 88554b9


Ignore:
Timestamp:
May 11, 2016, 9:40:50 AM (3 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/constantq, feature/pitchshift, feature/pydocstrings, feature/timestretch, master, pitchshift, sampler, timestretch, yinfft+
Children:
44312de
Parents:
b96a7b8
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_fvec.py

    rb96a7b8 r88554b9  
    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
     3import numpy as np
     4from numpy.testing import TestCase, assert_equal, assert_almost_equal
    55from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
    66from aubio import float_type
    7 from numpy import array, shape
    87
    98wrong_type = 'float32' if float_type == 'float64' else 'float64'
     
    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):
     
    3837    def test_vector(self):
    3938        a = fvec()
    40         a, len(a) #a.length
    41         a[0]
    42         array(a)
     39        len(a)
     40        _ = a[0]
     41        np.array(a)
     42        a = fvec(1)
    4343        a = fvec(10)
    44         a = fvec(1)
    45         a.T
    46         array(a).T
    47         a = list(range(len(a)))
     44        _ = a.T
    4845
    49     def test_wrong_values(self):
    50         self.assertRaises (ValueError, fvec, -10)
    51  
     46class aubio_fvec_wrong_values(TestCase):
     47
     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) """
    5258        a = fvec(2)
    53         self.assertRaises (IndexError, a.__getitem__, 3)
    54         self.assertRaises (IndexError, a.__getitem__, 2)
     59        self.assertRaises(IndexError, a.__getitem__, 3)
     60        self.assertRaises(IndexError, a.__getitem__, 2)
    5561
    56     def test_alpha_norm_of_fvec(self):
    57         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)
     62class aubio_wrong_fvec_input(TestCase):
     63    """ uses min_removal to test PyAubio_IsValidVector """
    6564
    66     def test_alpha_norm_of_none(self):
    67         self.assertRaises (ValueError, alpha_norm, None, 1)
     65    def test_no_input(self):
     66        self.assertRaises(TypeError, min_removal)
    6867
    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)
     68    def test_none(self):
     69        self.assertRaises(ValueError, min_removal, None)
    7970
    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)
     71    def test_wrong_scalar(self):
     72        a = np.array(10, dtype=float_type)
     73        self.assertRaises(ValueError, min_removal, a)
    8774
    88     def test_alpha_norm_of_array_of_string (self):
    89         a = "hello"
    90         self.assertRaises (ValueError, alpha_norm, a, 1)
     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
     94class 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
     102class 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 )
     105        a = np.array([0, 1, -1], dtype=float_type)
     106        assert_almost_equal(zero_crossing_rate(a), 1./3.)
    101107
    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)
     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
     120class aubio_fvec_min_removal(TestCase):
    115121
    116122    def test_fvec_min_removal_of_array(self):
    117         a = array([20,1,19], dtype=float_type)
     123        a = np.array([20, 1, 19], dtype=float_type)
    118124        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)
     125        assert_equal(b, [19, 0, 18])
    124126
    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)
    132         b = min_removal(a)
    133         assert_equal (array(b), [19, 0, 18])
    134         assert_equal (b, [19, 0, 18])
    135         assert_equal (a, b)
     127class aubio_fvec_test_memory(TestCase):
    136128
    137129    def test_pass_to_numpy(self):
     
    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
Note: See TracChangeset for help on using the changeset viewer.