source: interfaces/python/test_aubio.py @ cc9d0cc

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

interfaces/python: add cvec and filter types

  • Property mode set to 100644
File size: 4.0 KB
Line 
1from numpy.testing import TestCase, run_module_suite
2from numpy.testing import assert_equal
3from _aubio import *
4from numpy import array
5
6AUBIO_DO_CASTING = 0
7
8class aubiomodule_test_case(TestCase):
9
10  def setUp(self):
11    """ try importing aubio """
12
13  def test_vector(self):
14    a = fvec()
15    a.length, a.channels
16    a[0]
17    array(a)
18    a = fvec(10)
19    a = fvec(1, 2)
20    array(a).T
21    a[0] = range(a.length)
22    a[1][0] = 2
23
24  def test_wrong_values(self):
25    self.assertRaises (ValueError, fvec, -10)
26    self.assertRaises (ValueError, fvec, 1, -1)
27 
28    a = fvec(2, 3)
29    self.assertRaises (IndexError, a.__getitem__, 3)
30    self.assertRaises (IndexError, a[0].__getitem__, 2)
31
32  def test_alpha_norm_of_fvec(self):
33    a = fvec(2, 2)
34    self.assertEquals (alpha_norm(a, 1), 0)
35    a[0] = [1, 2] 
36    self.assertEquals (alpha_norm(a, 1), 1.5)
37    a[1] = [1, 2] 
38    self.assertEquals (alpha_norm(a, 1), 3)
39    a[0] = [0, 1]; a[1] = [1, 0]
40    self.assertEquals (alpha_norm(a, 2), 1)
41
42  def test_alpha_norm_of_array_of_float32(self):
43    # check scalar fails
44    a = array(1, dtype = 'float32')
45    self.assertRaises (ValueError, alpha_norm, a, 1)
46    # check 3d array fails
47    a = array([[[1,2],[3,4]]], dtype = 'float32')
48    self.assertRaises (ValueError, alpha_norm, a, 1)
49    # check 1d array
50    a = array(range(10), dtype = 'float32')
51    self.assertEquals (alpha_norm(a, 1), 4.5)
52    # check 2d array
53    a = array([range(10), range(10)], dtype = 'float32')
54    self.assertEquals (alpha_norm(a, 1), 9)
55
56  def test_alpha_norm_of_array_of_int(self):
57    a = array(1, dtype = 'int')
58    self.assertRaises (ValueError, alpha_norm, a, 1)
59    a = array([[[1,2],[3,4]]], dtype = 'int')
60    self.assertRaises (ValueError, alpha_norm, a, 1)
61    a = array(range(10), dtype = 'int')
62    self.assertRaises (ValueError, alpha_norm, a, 1)
63
64  def test_alpha_norm_of_array_of_string (self):
65    a = "hello"
66    self.assertRaises (ValueError, alpha_norm, a, 1)
67
68  def test_zero_crossing_rate(self):
69    a = array([0,1,-1], dtype='float32')
70    self.assertEquals (zero_crossing_rate(a), 1./3 )
71    a = array([0.]*100, dtype='float32')
72    self.assertEquals (zero_crossing_rate(a), 0 )
73    a = array([-1.]*100, dtype='float32')
74    self.assertEquals (zero_crossing_rate(a), 0 )
75    a = array([1.]*100, dtype='float32')
76    self.assertEquals (zero_crossing_rate(a), 0 )
77
78  def test_alpha_norm_of_array_of_float64(self):
79    # check scalar fail
80    a = array(1, dtype = 'float64')
81    self.assertRaises (ValueError, alpha_norm, a, 1)
82    # check 3d array fail
83    a = array([[[1,2],[3,4]]], dtype = 'float64')
84    self.assertRaises (ValueError, alpha_norm, a, 1)
85    if AUBIO_DO_CASTING:
86      # check float64 1d array fail
87      a = array(range(10), dtype = 'float64')
88      self.assertEquals (alpha_norm(a, 1), 4.5)
89      # check float64 2d array fail
90      a = array([range(10), range(10)], dtype = 'float64')
91      self.assertEquals (alpha_norm(a, 1), 9)
92    else:
93      # check float64 1d array fail
94      a = array(range(10), dtype = 'float64')
95      self.assertRaises (ValueError, alpha_norm, a, 1)
96      # check float64 2d array fail
97      a = array([range(10), range(10)], dtype = 'float64')
98      self.assertRaises (ValueError, alpha_norm, a, 1)
99
100  def test_fvec_min_removal_of_array(self):
101    a = array([20,1,19], dtype='float32')
102    b = min_removal(a)
103    assert_equal (array(b), [19, 0, 18])
104    assert_equal (b, [19, 0, 18])
105    assert_equal (a, b)
106    a[0] = 0
107    assert_equal (a, b)
108
109  def test_fvec_min_removal_of_array_float64(self):
110    a = array([20,1,19], dtype='float64')
111    if AUBIO_DO_CASTING:
112      b = min_removal(a)
113      assert_equal (array(b), [19, 0, 18])
114      assert_equal (b, [19, 0, 18])
115      #assert_equal (a, b)
116    else:
117      self.assertRaises (ValueError, min_removal, a)
118     
119  def test_fvec_min_removal_of_fvec(self):
120    a = fvec(3, 1)
121    a[0] = [20, 1, 19]
122    b = min_removal(a)
123    assert_equal (array(b), [19, 0, 18])
124    assert_equal (b, [19, 0, 18])
125    assert_equal (a, b)
126
127if __name__ == '__main__':
128  from unittest import main
129  main()
130
Note: See TracBrowser for help on using the repository browser.