Changes in python/tests/test_specdesc.py [0b6d23d:8e9cb57]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/tests/test_specdesc.py
r0b6d23d r8e9cb57 1 1 #! /usr/bin/env python 2 2 3 from unittest import main4 3 from numpy.testing import TestCase, assert_equal, assert_almost_equal 5 4 from numpy import random, arange, log, zeros 6 from aubio import specdesc, cvec, float_type 5 from aubio import specdesc, cvec 6 from math import pi 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(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.) 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))) 44 57 45 58 def test_phase(self): … … 48 61 # phase of zeros is zero 49 62 assert_equal (o(spec), 0.) 50 spec.phas = random.random_sample((len(spec.phas),)).astype( float_type)63 spec.phas = random.random_sample((len(spec.phas),)).astype('float32') 51 64 # phase of random is not zero 52 65 spec.norm[:] = 1 … … 58 71 # specdiff of zeros is zero 59 72 assert_equal (o(spec), 0.) 60 spec.phas = random.random_sample((len(spec.phas),)).astype( float_type)73 spec.phas = random.random_sample((len(spec.phas),)).astype('float32') 61 74 # phase of random is not zero 62 75 spec.norm[:] = 1 … … 67 80 c = cvec() 68 81 assert_equal( 0., o(c)) 69 a = arange(c.length, dtype= float_type)82 a = arange(c.length, dtype='float32') 70 83 c.norm = a 71 84 assert_equal (a, c.norm) … … 76 89 c = cvec() 77 90 assert_equal( 0., o(c)) 78 a = arange(c.length, dtype= float_type)91 a = arange(c.length, dtype='float32') 79 92 c.norm = a 80 93 assert_equal (a, c.norm) … … 89 102 c = cvec() 90 103 assert_equal( 0., o(c)) 91 a = arange(c.length, dtype= float_type)104 a = arange(c.length, dtype='float32') 92 105 c.norm = a 93 106 assert_almost_equal( sum(a * log(1.+ a/1.e-1 ) ) / o(c), 1., decimal=6) … … 97 110 c = cvec() 98 111 assert_equal( 0., o(c)) 99 a = arange(c.length, dtype= float_type)112 a = arange(c.length, dtype='float32') 100 113 c.norm = a 101 114 assert_almost_equal( sum(log(1.+ a/1.e-1 ) ) / o(c), 1, decimal=6) … … 105 118 c = cvec() 106 119 assert_equal( 0., o(c)) 107 a = arange(c.length, dtype= float_type)120 a = arange(c.length, dtype='float32') 108 121 c.norm = a 109 122 assert_equal( sum(a), o(c)) 110 123 assert_equal( 0, o(c)) 111 c.norm = zeros(c.length, dtype= float_type)124 c.norm = zeros(c.length, dtype='float32') 112 125 assert_equal( 0, o(c)) 113 126 … … 117 130 # make sure centroid of zeros is zero 118 131 assert_equal( 0., o(c)) 119 a = arange(c.length, dtype= float_type)132 a = arange(c.length, dtype='float32') 120 133 c.norm = a 121 134 centroid = sum(a*a) / sum(a) … … 128 141 o = specdesc("spread") 129 142 c = cvec(2048) 130 ramp = arange(c.length, dtype= float_type)143 ramp = arange(c.length, dtype='float32') 131 144 assert_equal( 0., o(c)) 132 145 … … 141 154 c = cvec() 142 155 assert_equal( 0., o(c)) 143 a = arange(c.length, dtype= float_type)156 a = arange(c.length, dtype='float32') 144 157 c.norm = a 145 158 centroid = sum(a*a) / sum(a) … … 155 168 c = cvec() 156 169 assert_equal( 0., o(c)) 157 a = arange(c.length, dtype= float_type)170 a = arange(c.length, dtype='float32') 158 171 c.norm = a 159 172 centroid = sum(a*a) / sum(a) … … 166 179 c = cvec() 167 180 assert_equal( 0., o(c)) 168 a = arange(c.length * 2, 0, -2, dtype= float_type)169 k = arange(c.length, dtype= float_type)181 a = arange(c.length * 2, 0, -2, dtype='float32') 182 k = arange(c.length, dtype='float32') 170 183 c.norm = a 171 184 num = len(a) * sum(k*a) - sum(k)*sum(a) … … 174 187 assert_almost_equal (slope, o(c), decimal = 5) 175 188 176 a = arange(0, c.length * 2, +2, dtype= float_type)189 a = arange(0, c.length * 2, +2, dtype='float32') 177 190 c.norm = a 178 191 num = len(a) * sum(k*a) - sum(k)*sum(a) … … 181 194 assert_almost_equal (slope, o(c), decimal = 5) 182 195 183 a = arange(0, c.length * 2, +2, dtype= float_type)196 a = arange(0, c.length * 2, +2, dtype='float32') 184 197 c.norm = a * 2 185 198 assert_almost_equal (slope, o(c), decimal = 5) … … 189 202 c = cvec() 190 203 assert_equal( 0., o(c)) 191 a = arange(c.length * 2, 0, -2, dtype= float_type)192 k = arange(c.length, dtype= float_type)204 a = arange(c.length * 2, 0, -2, dtype='float32') 205 k = arange(c.length, dtype='float32') 193 206 c.norm = a 194 207 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 195 208 assert_almost_equal (decrease, o(c), decimal = 5) 196 209 197 a = arange(0, c.length * 2, +2, dtype= float_type)210 a = arange(0, c.length * 2, +2, dtype='float32') 198 211 c.norm = a 199 212 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) 200 213 assert_almost_equal (decrease, o(c), decimal = 5) 201 214 202 a = arange(0, c.length * 2, +2, dtype= float_type)215 a = arange(0, c.length * 2, +2, dtype='float32') 203 216 c.norm = a * 2 204 217 decrease = sum((a[1:] - a [0]) / k[1:]) / sum(a[1:]) … … 209 222 c = cvec() 210 223 assert_equal( 0., o(c)) 211 a = arange(c.length * 2, 0, -2, dtype=float_type) 224 a = arange(c.length * 2, 0, -2, dtype='float32') 225 k = arange(c.length, dtype='float32') 212 226 c.norm = a 213 227 cumsum = .95*sum(a*a) 214 228 i = 0; rollsum = 0 215 229 while rollsum < cumsum: 216 217 230 rollsum += a[i]*a[i] 231 i+=1 218 232 rolloff = i 219 233 assert_equal (rolloff, o(c)) 220 234 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')232 235 233 236 if __name__ == '__main__': 237 from unittest import main 234 238 main()
Note: See TracChangeset
for help on using the changeset viewer.