Ignore:
Timestamp:
Jun 22, 2016, 1:00:10 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
4b9443c4
Parents:
60fc05b (diff), 6769586 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into notes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_fvec.py

    r60fc05b rf264b17  
    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
     3from unittest import main
     4import numpy as np
     5from numpy.testing import TestCase, assert_equal, assert_almost_equal
    56from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
    6 from numpy import array, shape
     7from aubio import float_type
     8
     9wrong_type = 'float32' if float_type == 'float64' else 'float64'
    710
    811default_size = 512
     
    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,)
    16         assert_equal (a, 0)
     19        assert_equal(a, 0)
    1720
    1821    def test_vector_create_with_list(self):
    19         a = fvec([0,1,2,3])
    20         assert a.dtype == 'float32'
     22        a = fvec([0, 1, 2, 3])
     23        assert a.dtype == float_type
    2124        assert a.shape == (4,)
    22         assert_equal (range(4), a)
     25        assert_equal(list(range(4)), a)
    2326
    2427    def test_vector_assign_element(self):
     
    3538    def test_vector(self):
    3639        a = fvec()
    37         a, len(a) #a.length
    38         a[0]
    39         array(a)
     40        len(a)
     41        _ = a[0]
     42        np.array(a)
     43        a = fvec(1)
    4044        a = fvec(10)
    41         a = fvec(1)
    42         a.T
    43         array(a).T
    44         a = range(len(a))
     45        _ = a.T
    4546
    46     def test_wrong_values(self):
    47         self.assertRaises (ValueError, fvec, -10)
    48  
     47class aubio_fvec_wrong_values(TestCase):
     48
     49    def test_negative_length(self):
     50        """ test creating fvec with negative length fails (pure python) """
     51        self.assertRaises(ValueError, fvec, -10)
     52
     53    def test_zero_length(self):
     54        """ test creating fvec with zero length fails (pure python) """
     55        self.assertRaises(ValueError, fvec, 0)
     56
     57    def test_out_of_bound(self):
     58        """ test assiging fvec out of bounds fails (pure python) """
    4959        a = fvec(2)
    50         self.assertRaises (IndexError, a.__getitem__, 3)
    51         self.assertRaises (IndexError, a.__getitem__, 2)
     60        self.assertRaises(IndexError, a.__getitem__, 3)
     61        self.assertRaises(IndexError, a.__getitem__, 2)
    5262
    53     def test_alpha_norm_of_fvec(self):
    54         a = fvec(2)
    55         self.assertEquals (alpha_norm(a, 1), 0)
    56         a[0] = 1
    57         self.assertEquals (alpha_norm(a, 1), 0.5)
    58         a[1] = 1
    59         self.assertEquals (alpha_norm(a, 1), 1)
    60         a = array([0, 1], dtype='float32')
    61         from math import sqrt
    62         assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.)
     63class aubio_wrong_fvec_input(TestCase):
     64    """ uses min_removal to test PyAubio_IsValidVector """
    6365
    64     def test_alpha_norm_of_none(self):
    65         self.assertRaises (ValueError, alpha_norm, None, 1)
     66    def test_no_input(self):
     67        self.assertRaises(TypeError, min_removal)
    6668
    67     def test_alpha_norm_of_array_of_float32(self):
    68         # check scalar fails
    69         a = array(1, dtype = 'float32')
    70         self.assertRaises (ValueError, alpha_norm, a, 1)
    71         # check 2d array fails
    72         a = array([[2],[4]], dtype = 'float32')
    73         self.assertRaises (ValueError, alpha_norm, a, 1)
    74         # check 1d array
    75         a = array(range(10), dtype = 'float32')
    76         self.assertEquals (alpha_norm(a, 1), 4.5)
     69    def test_none(self):
     70        self.assertRaises(ValueError, min_removal, None)
    7771
    78     def test_alpha_norm_of_array_of_int(self):
    79         a = array(1, dtype = 'int')
    80         self.assertRaises (ValueError, alpha_norm, a, 1)
    81         a = array([[[1,2],[3,4]]], dtype = 'int')
    82         self.assertRaises (ValueError, alpha_norm, a, 1)
    83         a = array(range(10), dtype = 'int')
    84         self.assertRaises (ValueError, alpha_norm, a, 1)
     72    def test_wrong_scalar(self):
     73        a = np.array(10, dtype=float_type)
     74        self.assertRaises(ValueError, min_removal, a)
    8575
    86     def test_alpha_norm_of_array_of_string (self):
    87         a = "hello"
    88         self.assertRaises (ValueError, alpha_norm, a, 1)
     76    def test_wrong_dimensions(self):
     77        a = np.array([[[1, 2], [3, 4]]], dtype=float_type)
     78        self.assertRaises(ValueError, min_removal, a)
     79
     80    def test_wrong_array_size(self):
     81        x = np.array([], dtype=float_type)
     82        self.assertRaises(ValueError, min_removal, x)
     83
     84    def test_wrong_type(self):
     85        a = np.zeros(10, dtype=wrong_type)
     86        self.assertRaises(ValueError, min_removal, a)
     87
     88    def test_wrong_list_input(self):
     89        self.assertRaises(ValueError, min_removal, [0., 1.])
     90
     91    def test_good_input(self):
     92        a = np.zeros(10, dtype=float_type)
     93        assert_equal(np.zeros(10, dtype=float_type), min_removal(a))
     94
     95class aubio_alpha_norm(TestCase):
     96
     97    def test_alpha_norm_of_random(self):
     98        x = np.random.rand(1024).astype(float_type)
     99        alpha = np.random.rand() * 5.
     100        x_alpha_norm = (np.sum(np.abs(x)**alpha)/len(x))**(1/alpha)
     101        assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm, decimal = 5)
     102
     103class aubio_zero_crossing_rate_test(TestCase):
    89104
    90105    def test_zero_crossing_rate(self):
    91         a = array([0,1,-1], dtype='float32')
    92         assert_almost_equal (zero_crossing_rate(a), 1./3. )
    93         a = array([0.]*100, dtype='float32')
    94         self.assertEquals (zero_crossing_rate(a), 0 )
    95         a = array([-1.]*100, dtype='float32')
    96         self.assertEquals (zero_crossing_rate(a), 0 )
    97         a = array([1.]*100, dtype='float32')
    98         self.assertEquals (zero_crossing_rate(a), 0 )
     106        a = np.array([0, 1, -1], dtype=float_type)
     107        assert_almost_equal(zero_crossing_rate(a), 1./3.)
    99108
    100     def test_alpha_norm_of_array_of_float64(self):
    101         # check scalar fail
    102         a = array(1, dtype = 'float64')
    103         self.assertRaises (ValueError, alpha_norm, a, 1)
    104         # check 3d array fail
    105         a = array([[[1,2],[3,4]]], dtype = 'float64')
    106         self.assertRaises (ValueError, alpha_norm, a, 1)
    107         # check float64 1d array fail
    108         a = array(range(10), dtype = 'float64')
    109         self.assertRaises (ValueError, alpha_norm, a, 1)
    110         # check float64 2d array fail
    111         a = array([range(10), range(10)], dtype = 'float64')
    112         self.assertRaises (ValueError, alpha_norm, a, 1)
     109    def test_zero_crossing_rate_zeros(self):
     110        a = np.zeros(100, dtype=float_type)
     111        self.assertEqual(zero_crossing_rate(a), 0)
     112
     113    def test_zero_crossing_rate_minus_ones(self):
     114        a = np.ones(100, dtype=float_type)
     115        self.assertEqual(zero_crossing_rate(a), 0)
     116
     117    def test_zero_crossing_rate_plus_ones(self):
     118        a = np.ones(100, dtype=float_type)
     119        self.assertEqual(zero_crossing_rate(a), 0)
     120
     121class aubio_fvec_min_removal(TestCase):
    113122
    114123    def test_fvec_min_removal_of_array(self):
    115         a = array([20,1,19], dtype='float32')
     124        a = np.array([20, 1, 19], dtype=float_type)
    116125        b = min_removal(a)
    117         assert_equal (array(b), [19, 0, 18])
    118         assert_equal (b, [19, 0, 18])
    119         assert_equal (a, b)
    120         a[0] = 0
    121         assert_equal (a, b)
     126        assert_equal(b, [19, 0, 18])
    122127
    123     def test_fvec_min_removal_of_array_float64(self):
    124         a = array([20,1,19], dtype='float64')
    125         self.assertRaises (ValueError, min_removal, a)
     128class aubio_fvec_test_memory(TestCase):
    126129
    127     def test_fvec_min_removal_of_fvec(self):
    128         a = fvec(3)
    129         a = array([20, 1, 19], dtype = 'float32')
    130         b = min_removal(a)
    131         assert_equal (array(b), [19, 0, 18])
    132         assert_equal (b, [19, 0, 18])
    133         assert_equal (a, b)
     130    def test_pass_to_numpy(self):
     131        a = fvec(10)
     132        a[:] = 1.
     133        b = a
     134        del a
     135        assert_equal(b, 1.)
     136        c = fvec(10)
     137        c = b
     138        del b
     139        assert_equal(c, 1.)
     140        del c
    134141
    135142if __name__ == '__main__':
    136     from unittest import main
    137143    main()
Note: See TracChangeset for help on using the changeset viewer.