Changes in python/tests/test_specdesc.py [8e9cb57:0b6d23d]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/tests/test_specdesc.py
r8e9cb57 r0b6d23d 1 1 #! /usr/bin/env python 2 2 3 from unittest import main 3 4 from numpy.testing import TestCase, assert_equal, assert_almost_equal 4 5 from numpy import random, arange, log, zeros 5 from aubio import specdesc, cvec 6 from math import pi 6 from aubio import specdesc, cvec, float_type 7 7 8 8 methods = ["default", … … 30 30 31 31 for method in methods: 32 o = specdesc(method, buf_size) 33 assert_equal ([o.buf_size, o.method], [buf_size, method]) 34 35 spec = cvec(buf_size) 36 spec.norm[0] = 1 37 spec.norm[1] = 1./2. 38 #print "%20s" % method, str(o(spec)) 39 o(spec) 40 spec.norm = random.random_sample((len(spec.norm),)).astype('float32') 41 spec.phas = random.random_sample((len(spec.phas),)).astype('float32') 42 #print "%20s" % method, str(o(spec)) 43 assert (o(spec) != 0.) 44 45 def test_hfc(self): 46 o = specdesc("hfc", buf_size) 47 spec = cvec(buf_size) 48 # hfc of zeros is zero 49 assert_equal (o(spec), 0.) 50 # hfc of ones is sum of all bin numbers 51 spec.norm[:] = 1 52 expected = sum(range(buf_size/2 + 2)) 53 assert_equal (o(spec), expected) 54 # changing phase doesn't change anything 55 spec.phas[:] = 1 56 assert_equal (o(spec), sum(range(buf_size/2 + 2))) 32 o = specdesc(method, buf_size) 33 assert_equal ([o.buf_size, o.method], [buf_size, method]) 34 35 spec = cvec(buf_size) 36 spec.norm[0] = 1 37 spec.norm[1] = 1./2. 38 #print "%20s" % method, str(o(spec)) 39 o(spec) 40 spec.norm = random.random_sample((len(spec.norm),)).astype(float_type) 41 spec.phas = random.random_sample((len(spec.phas),)).astype(float_type) 42 #print "%20s" % method, str(o(spec)) 43 assert (o(spec) != 0.) 57 44 58 45 def test_phase(self): … … 61 48 # phase of zeros is zero 62 49 assert_equal (o(spec), 0.) 63 spec.phas = random.random_sample((len(spec.phas),)).astype( 'float32')50 spec.phas = random.random_sample((len(spec.phas),)).astype(float_type) 64 51 # phase of random is not zero 65 52 spec.norm[:] = 1 … … 71 58 # specdiff of zeros is zero 72 59 assert_equal (o(spec), 0.) 73 spec.phas = random.random_sample((len(spec.phas),)).astype( 'float32')60 spec.phas = random.random_sample((len(spec.phas),)).astype(float_type) 74 61 # phase of random is not zero 75 62 spec.norm[:] = 1 … … 80 67 c = cvec() 81 68 assert_equal( 0., o(c)) 82 a = arange(c.length, dtype= 'float32')69 a = arange(c.length, dtype=float_type) 83 70 c.norm = a 84 71 assert_equal (a, c.norm) … … 89 76 c = cvec() 90 77 assert_equal( 0., o(c)) 91 a = arange(c.length, dtype= 'float32')78 a = arange(c.length, dtype=float_type) 92 79 c.norm = a 93 80 assert_equal (a, c.norm) … … 102 89 c = cvec() 103 90 assert_equal( 0., o(c)) 104 a = arange(c.length, dtype= 'float32')91 a = arange(c.length, dtype=float_type) 105 92 c.norm = a 106 93 assert_almost_equal( sum(a * log(1.+ a/1.e-1 ) ) / o(c), 1., decimal=6) … … 110 97 c = cvec() 111 98 assert_equal( 0., o(c)) 112 a = arange(c.length, dtype= 'float32')99 a = arange(c.length, dtype=float_type) 113 100 c.norm = a 114 101 assert_almost_equal( sum(log(1.+ a/1.e-1 ) ) / o(c), 1, decimal=6) … … 118 105 c = cvec() 119 106 assert_equal( 0., o(c)) 120 a = arange(c.length, dtype= 'float32')107 a = arange(c.length, dtype=float_type) 121 108 c.norm = a 122 109 assert_equal( sum(a), o(c)) 123 110 assert_equal( 0, o(c)) 124 c.norm = zeros(c.length, dtype= 'float32')111 c.norm = zeros(c.length, dtype=float_type) 125 112 assert_equal( 0, o(c)) 126 113 … … 130 117 # make sure centroid of zeros is zero 131 118 assert_equal( 0., o(c)) 132 a = arange(c.length, dtype= 'float32')119 a = arange(c.length, dtype=float_type) 133 120 c.norm = a 134 121 centroid = sum(a*a) / sum(a) … … 141 128 o = specdesc("spread") 142 129 c = cvec(2048) 143 ramp = arange(c.length, dtype= 'float32')130 ramp = arange(c.length, dtype=float_type) 144 131 assert_equal( 0., o(c)) 145 132 … … 154 141 c = cvec() 155 142 assert_equal( 0., o(c)) 156 a = arange(c.length, dtype= 'float32')143 a = arange(c.length, dtype=float_type) 157 144 c.norm = a 158 145 centroid = sum(a*a) / sum(a) … … 168 155 c = cvec() 169 156 assert_equal( 0., o(c)) 170 a = arange(c.length, dtype= 'float32')157 a = arange(c.length, dtype=float_type) 171 158 c.norm = a 172 159 centroid = sum(a*a) / sum(a) … … 179 166 c = cvec() 180 167 assert_equal( 0., o(c)) 181 a = arange(c.length * 2, 0, -2, dtype= 'float32')182 k = arange(c.length, dtype= 'float32')168 a = arange(c.length * 2, 0, -2, dtype=float_type) 169 k = arange(c.length, dtype=float_type) 183 170 c.norm = a 184 171 num = len(a) * sum(k*a) - sum(k)*sum(a) … … 187 174 assert_almost_equal (slope, o(c), decimal = 5) 188 175 189 a = arange(0, c.length * 2, +2, dtype= 'float32')176 a = arange(0, c.length * 2, +2, dtype=float_type) 190 177 c.norm = a 191 178 num = len(a) * sum(k*a) - sum(k)*sum(a) … … 194 181 assert_almost_equal (slope, o(c), decimal = 5) 195 182 196 a = arange(0, c.length * 2, +2, dtype= 'float32')183 a = arange(0, c.length * 2, +2, dtype=float_type) 197 184 c.norm = a * 2 198 185 assert_almost_equal (slope, o(c), decimal = 5) … … 202 189 c = cvec() 203 190 assert_equal( 0., o(c)) 204 a = arange(c.length * 2, 0, -2, dtype= 'float32')205 k = arange(c.length, dtype= 'float32')191 a = arange(c.length * 2, 0, -2, dtype=float_type) 192 k = arange(c.length, dtype=float_type) 206 193 c.norm = a 207 194 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 208 195 assert_almost_equal (decrease, o(c), decimal = 5) 209 196 210 a = arange(0, c.length * 2, +2, dtype= 'float32')197 a = arange(0, c.length * 2, +2, dtype=float_type) 211 198 c.norm = a 212 199 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 213 200 assert_almost_equal (decrease, o(c), decimal = 5) 214 201 215 a = arange(0, c.length * 2, +2, dtype= 'float32')202 a = arange(0, c.length * 2, +2, dtype=float_type) 216 203 c.norm = a * 2 217 204 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) … … 222 209 c = cvec() 223 210 assert_equal( 0., o(c)) 224 a = arange(c.length * 2, 0, -2, dtype='float32') 225 k = arange(c.length, dtype='float32') 211 a = arange(c.length * 2, 0, -2, dtype=float_type) 226 212 c.norm = a 227 213 cumsum = .95*sum(a*a) 228 214 i = 0; rollsum = 0 229 215 while rollsum < cumsum: 230 rollsum += a[i]*a[i]231 i+=1216 rollsum += a[i]*a[i] 217 i+=1 232 218 rolloff = i 233 219 assert_equal (rolloff, o(c)) 234 220 221 class aubio_specdesc_wrong(TestCase): 222 223 def test_negative(self): 224 with self.assertRaises(ValueError): 225 specdesc("default", -10) 226 227 def test_unknown(self): 228 # FIXME should fail? 229 with self.assertRaises(ValueError): 230 specdesc("unknown", 512) 231 self.skipTest('todo: new_specdesc should fail on wrong method') 235 232 236 233 if __name__ == '__main__': 237 from unittest import main238 234 main()
Note: See TracChangeset
for help on using the changeset viewer.