source: python/tests/test_fvec.py @ ad9f999

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since ad9f999 was fb3f62e, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/tests/test_fvec.py: add test_pass_to_numpy

  • Property mode set to 100755
File size: 4.8 KB
RevLine 
[75e715f]1#! /usr/bin/env python
[1ebf8770]2
[b7f3aaf]3from numpy.testing import TestCase, run_module_suite
4from numpy.testing import assert_equal, assert_almost_equal
[4c01c0f]5from aubio import fvec, zero_crossing_rate, alpha_norm, min_removal
[c4b2183]6from aubio import float_type
[1a6ef2c]7from numpy import array, shape
[b7f3aaf]8
[c4b2183]9wrong_type = 'float32' if float_type == 'float64' else 'float64'
10
[973206a]11default_size = 512
12
[b7f3aaf]13class aubio_fvec_test_case(TestCase):
14
15    def test_vector_created_with_zeroes(self):
[1a6ef2c]16        a = fvec(10)
[c4b2183]17        assert a.dtype == float_type
[973206a]18        assert a.shape == (10,)
19        assert_equal (a, 0)
[b7f3aaf]20
[493e6f7]21    def test_vector_create_with_list(self):
22        a = fvec([0,1,2,3])
[c4b2183]23        assert a.dtype == float_type
[973206a]24        assert a.shape == (4,)
[689106e]25        assert_equal (list(range(4)), a)
[493e6f7]26
[b7f3aaf]27    def test_vector_assign_element(self):
[973206a]28        a = fvec(default_size)
[b7f3aaf]29        a[0] = 1
30        assert_equal(a[0], 1)
31
32    def test_vector_assign_element_end(self):
[973206a]33        a = fvec(default_size)
[b7f3aaf]34        a[-1] = 1
35        assert_equal(a[-1], 1)
[1a6ef2c]36        assert_equal(a[len(a)-1], 1)
[b7f3aaf]37
38    def test_vector(self):
39        a = fvec()
[1a6ef2c]40        a, len(a) #a.length
[b7f3aaf]41        a[0]
42        array(a)
43        a = fvec(10)
44        a = fvec(1)
[1a6ef2c]45        a.T
[b7f3aaf]46        array(a).T
[689106e]47        a = list(range(len(a)))
[b7f3aaf]48
49    def test_wrong_values(self):
50        self.assertRaises (ValueError, fvec, -10)
51 
52        a = fvec(2)
53        self.assertRaises (IndexError, a.__getitem__, 3)
54        self.assertRaises (IndexError, a.__getitem__, 2)
55
56    def test_alpha_norm_of_fvec(self):
57        a = fvec(2)
[689106e]58        self.assertEqual (alpha_norm(a, 1), 0)
[b7f3aaf]59        a[0] = 1
[689106e]60        self.assertEqual (alpha_norm(a, 1), 0.5)
[b7f3aaf]61        a[1] = 1
[689106e]62        self.assertEqual (alpha_norm(a, 1), 1)
[c4b2183]63        a = array([0, 1], dtype=float_type)
[b7f3aaf]64        from math import sqrt
65        assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.)
66
[493e6f7]67    def test_alpha_norm_of_none(self):
68        self.assertRaises (ValueError, alpha_norm, None, 1)
69
[b7f3aaf]70    def test_alpha_norm_of_array_of_float32(self):
71        # check scalar fails
[c4b2183]72        a = array(1, dtype = float_type)
[b7f3aaf]73        self.assertRaises (ValueError, alpha_norm, a, 1)
74        # check 2d array fails
[c4b2183]75        a = array([[2],[4]], dtype = float_type)
[b7f3aaf]76        self.assertRaises (ValueError, alpha_norm, a, 1)
77        # check 1d array
[c4b2183]78        a = array(range(10), dtype = float_type)
[689106e]79        self.assertEqual (alpha_norm(a, 1), 4.5)
[b7f3aaf]80
81    def test_alpha_norm_of_array_of_int(self):
82        a = array(1, dtype = 'int')
83        self.assertRaises (ValueError, alpha_norm, a, 1)
84        a = array([[[1,2],[3,4]]], dtype = 'int')
85        self.assertRaises (ValueError, alpha_norm, a, 1)
86        a = array(range(10), dtype = 'int')
87        self.assertRaises (ValueError, alpha_norm, a, 1)
88
89    def test_alpha_norm_of_array_of_string (self):
90        a = "hello"
91        self.assertRaises (ValueError, alpha_norm, a, 1)
92
93    def test_zero_crossing_rate(self):
[c4b2183]94        a = array([0,1,-1], dtype=float_type)
[4afa448e]95        assert_almost_equal (zero_crossing_rate(a), 1./3. )
[c4b2183]96        a = array([0.]*100, dtype=float_type)
[689106e]97        self.assertEqual (zero_crossing_rate(a), 0 )
[c4b2183]98        a = array([-1.]*100, dtype=float_type)
[689106e]99        self.assertEqual (zero_crossing_rate(a), 0 )
[c4b2183]100        a = array([1.]*100, dtype=float_type)
[689106e]101        self.assertEqual (zero_crossing_rate(a), 0 )
[b7f3aaf]102
103    def test_alpha_norm_of_array_of_float64(self):
104        # check scalar fail
[c4b2183]105        a = array(1, dtype = wrong_type)
[b7f3aaf]106        self.assertRaises (ValueError, alpha_norm, a, 1)
107        # check 3d array fail
[c4b2183]108        a = array([[[1,2],[3,4]]], dtype = wrong_type)
[b7f3aaf]109        self.assertRaises (ValueError, alpha_norm, a, 1)
110        # check float64 1d array fail
[c4b2183]111        a = array(list(range(10)), dtype = wrong_type)
[b7f3aaf]112        self.assertRaises (ValueError, alpha_norm, a, 1)
113        # check float64 2d array fail
[c4b2183]114        a = array([list(range(10)), list(range(10))], dtype = wrong_type)
[b7f3aaf]115        self.assertRaises (ValueError, alpha_norm, a, 1)
116
117    def test_fvec_min_removal_of_array(self):
[c4b2183]118        a = array([20,1,19], dtype=float_type)
[b7f3aaf]119        b = min_removal(a)
120        assert_equal (array(b), [19, 0, 18])
121        assert_equal (b, [19, 0, 18])
122        assert_equal (a, b)
123        a[0] = 0
124        assert_equal (a, b)
125
126    def test_fvec_min_removal_of_array_float64(self):
[c4b2183]127        a = array([20,1,19], dtype=wrong_type)
[b7f3aaf]128        self.assertRaises (ValueError, min_removal, a)
129
130    def test_fvec_min_removal_of_fvec(self):
131        a = fvec(3)
[c4b2183]132        a = array([20, 1, 19], dtype = float_type)
[b7f3aaf]133        b = min_removal(a)
134        assert_equal (array(b), [19, 0, 18])
135        assert_equal (b, [19, 0, 18])
136        assert_equal (a, b)
137
[fb3f62e]138    def test_pass_to_numpy(self):
139        a = fvec(10)
140        a = 1.
141        b = a
142        del a
143        assert_equal (b, 1.)
144        c = fvec(10)
145        c = b
146        del b
147        assert_equal (c, 1.)
148        del c
149
[b7f3aaf]150if __name__ == '__main__':
151    from unittest import main
152    main()
Note: See TracBrowser for help on using the repository browser.