1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
# Copyright 2012 Knowledge Economy Developments Ltd
#
# Henry Gomersall
# heng@kedevelopments.co.uk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from pyfftw import interfaces, builders
import numpy
import unittest
from .test_pyfftw_base import run_test_suites
from .test_pyfftw_numpy_interface import InterfacesNumpyFFTTestFFT
import threading
import time
'''Test the caching functionality of the interfaces package.
'''
class InterfacesNumpyFFTCacheTestFFT(InterfacesNumpyFFTTestFFT):
test_shapes = (
((100,), {}),
((128, 64), {'axis': 0}),
((128, 32), {'axis': -1}),
((32, 64), {}),
)
def validate(self, array_type, test_shape, dtype,
s, kwargs):
# Do it with the cache
interfaces.cache.enable()
output = self._validate(array_type, test_shape, dtype, s, kwargs)
output2 = self._validate(array_type, test_shape, dtype, s, kwargs)
self.assertIsNot(output, output2)
# Turn it off to finish
interfaces.cache.disable()
class CacheSpecificInterfacesUtils(unittest.TestCase):
def test_slow_lookup_no_race_condition(self):
'''Checks that lookups in _utils longer than the keepalive time are ok.
'''
# Any old size, it doesn't matter
data_shape = (128,)
# Monkey patch the module with a custom _Cache object
_Cache_class = interfaces.cache._Cache
class _SlowLookupCache(_Cache_class):
def _lookup(self, key):
return _Cache_class.lookup(self, key)
def lookup(self, key):
time.sleep(0.1)
return self._lookup(key)
try:
interfaces.cache._Cache = _SlowLookupCache
interfaces.cache.enable()
# something shortish
interfaces.cache.set_keepalive_time(0.001)
ar, ai = numpy.random.randn(*(2,) + data_shape)
a = ar + 1j*ai
# Both the following should work without exception
# (even if it fails to get from the cache)
interfaces.numpy_fft.fft(a)
interfaces.numpy_fft.fft(a)
interfaces.cache.disable()
finally:
# Revert the monkey patching
interfaces.cache._Cache = _Cache_class
class InterfacesCacheTest(unittest.TestCase):
def test_missing_threading(self):
self.assertIs(interfaces.cache._fftw_cache, None)
mod_threading = interfaces.cache._threading
interfaces.cache._threading = None
with self.assertRaises(ImportError):
interfaces.cache.enable()
interfaces.cache._threading = mod_threading
def test_is_enabled(self):
self.assertIs(interfaces.cache._fftw_cache, None)
interfaces.cache.enable()
self.assertTrue(interfaces.cache.is_enabled())
interfaces.cache.disable()
self.assertFalse(interfaces.cache.is_enabled())
def test_cache_enable_disable(self):
self.assertIs(interfaces.cache._fftw_cache, None)
interfaces.cache.enable()
self.assertIsInstance(
interfaces.cache._fftw_cache, interfaces.cache._Cache)
interfaces.cache.disable()
self.assertIs(interfaces.cache._fftw_cache, None)
def test_set_keepalive_time(self):
with self.assertRaises(interfaces.cache.CacheError):
interfaces.cache.set_keepalive_time(10)
interfaces.cache.enable()
interfaces.cache.set_keepalive_time(10)
self.assertTrue(
interfaces.cache._fftw_cache.keepalive_time == 10.0)
interfaces.cache.disable()
class CacheTest(unittest.TestCase):
def test_cache_parent_thread_ended(self):
'''Test ending cache parent thread ends cache thread.
'''
self.assertTrue(threading.active_count() == 1)
def cache_parent_thread():
cache = interfaces.cache._Cache()
time.sleep(0.2)
parent_t = threading.Thread(target=cache_parent_thread)
parent_t.start()
time.sleep(0.1)
# Check it's running
self.assertTrue(threading.active_count() == 3)
parent_t.join()
time.sleep(0.1)
# Check both threads have exited properly
self.assertTrue(threading.active_count() == 1)
def test_delete_cache_object(self):
'''Test deleting a cache object ends cache thread.
'''
self.assertTrue(threading.active_count() == 1)
_cache = interfaces.cache._Cache()
time.sleep(0.1)
self.assertTrue(threading.active_count() == 2)
del _cache
time.sleep(0.1)
self.assertTrue(threading.active_count() == 1)
def test_insert_and_lookup_item(self):
_cache = interfaces.cache._Cache()
key = 'the key'
test_array = numpy.random.randn(16)
obj = builders.fft(test_array)
_cache.insert(obj, key)
self.assertIs(_cache.lookup(key), obj)
def test_invalid_lookup(self):
_cache = interfaces.cache._Cache()
key = 'the key'
test_array = numpy.random.randn(16)
obj = builders.fft(test_array)
_cache.insert(obj, key)
self.assertRaises(KeyError, _cache.lookup, 'wrong_key')
def test_keepalive_time_update(self):
_cache = interfaces.cache._Cache()
# The default
self.assertEqual(_cache.keepalive_time, 0.1)
_cache.set_keepalive_time(0.3)
self.assertEqual(_cache.keepalive_time, 0.3)
_cache.set_keepalive_time(10.0)
self.assertEqual(_cache.keepalive_time, 10.0)
_cache.set_keepalive_time('0.2')
self.assertEqual(_cache.keepalive_time, 0.2)
with self.assertRaises(ValueError):
_cache.set_keepalive_time('foo')
with self.assertRaises(TypeError):
_cache.set_keepalive_time([])
def test_contains(self):
_cache = interfaces.cache._Cache()
key = 'the key'
test_array = numpy.random.randn(16)
obj = builders.fft(test_array)
_cache.insert(obj, key)
self.assertTrue(key in _cache)
self.assertFalse('Not a key' in _cache)
def test_objects_removed_after_keepalive(self):
_cache = interfaces.cache._Cache()
key = 'the key'
test_array = numpy.random.randn(16)
obj = builders.fft(test_array)
_cache.insert(obj, key)
self.assertIs(_cache.lookup(key), obj)
keepalive_time = _cache.keepalive_time
time.sleep(_cache.keepalive_time*3)
self.assertRaises(KeyError, _cache.lookup, key)
_cache.insert(obj, key)
old_keepalive_time = _cache.keepalive_time
_cache.set_keepalive_time(old_keepalive_time * 4)
self.assertIs(_cache.lookup(key), obj)
time.sleep(old_keepalive_time * 3)
self.assertIs(_cache.lookup(key), obj)
time.sleep(old_keepalive_time * 8)
self.assertRaises(KeyError, _cache.lookup, key)
class InterfacesNumpyFFTCacheTestIFFT(InterfacesNumpyFFTCacheTestFFT):
func = 'ifft'
class InterfacesNumpyFFTCacheTestRFFT(InterfacesNumpyFFTCacheTestFFT):
func = 'rfft'
class InterfacesNumpyFFTCacheTestIRFFT(InterfacesNumpyFFTCacheTestFFT):
func = 'irfft'
realinv = True
class InterfacesNumpyFFTCacheTestFFT2(InterfacesNumpyFFTCacheTestFFT):
axes_kw = 'axes'
func = 'ifft2'
test_shapes = (
((128, 64), {'axes': None}),
((128, 32), {'axes': None}),
((32, 64), {'axes': (-2, -1)}),
((4, 6, 8, 4), {'axes': (0, 3)}),
)
invalid_args = (
((100,), ((100, 200),), ValueError, 'Shape error'),
((100, 200), ((100, 200, 100),), ValueError, 'Shape error'),
((100,), ((100, 200), (-3, -2, -1)), ValueError, 'Shape error'),
((100, 200), (100, -1), TypeError, ''),
((100, 200), ((100, 200), (-3, -2)), IndexError, 'Invalid axes'),
((100, 200), ((100,), (-3,)), IndexError, 'Invalid axes'))
class InterfacesNumpyFFTCacheTestIFFT2(InterfacesNumpyFFTCacheTestFFT2):
func = 'ifft2'
class InterfacesNumpyFFTCacheTestRFFT2(InterfacesNumpyFFTCacheTestFFT2):
func = 'rfft2'
class InterfacesNumpyFFTCacheTestIRFFT2(InterfacesNumpyFFTCacheTestFFT2):
func = 'irfft2'
realinv = True
class InterfacesNumpyFFTCacheTestFFTN(InterfacesNumpyFFTCacheTestFFT2):
func = 'ifftn'
test_shapes = (
((128, 32, 4), {'axes': None}),
((64, 128, 16), {'axes': (0, 1, 2)}),
((4, 6, 8, 4), {'axes': (0, 3, 1)}),
((4, 6, 8, 4), {'axes': (0, 3, 1, 2)}),
)
class InterfacesNumpyFFTCacheTestIFFTN(InterfacesNumpyFFTCacheTestFFTN):
func = 'ifftn'
class InterfacesNumpyFFTCacheTestRFFTN(InterfacesNumpyFFTCacheTestFFTN):
func = 'rfftn'
class InterfacesNumpyFFTCacheTestIRFFTN(InterfacesNumpyFFTCacheTestFFTN):
func = 'irfftn'
realinv = True
test_cases = (
CacheTest,
InterfacesCacheTest,
CacheSpecificInterfacesUtils,
InterfacesNumpyFFTCacheTestFFT,
InterfacesNumpyFFTCacheTestIFFT,
InterfacesNumpyFFTCacheTestRFFT,
InterfacesNumpyFFTCacheTestIRFFT,
InterfacesNumpyFFTCacheTestFFT2,
InterfacesNumpyFFTCacheTestIFFT2,
InterfacesNumpyFFTCacheTestRFFT2,
InterfacesNumpyFFTCacheTestIRFFT2,
InterfacesNumpyFFTCacheTestFFTN,
InterfacesNumpyFFTCacheTestIFFTN,
InterfacesNumpyFFTCacheTestRFFTN,
InterfacesNumpyFFTCacheTestIRFFTN,)
test_set = None
if __name__ == '__main__':
run_test_suites(test_cases, test_set)
|