Add tests for window manager.

This commit is contained in:
Stefan van der Walt
2009-11-02 21:57:31 +02:00
parent c0ad658822
commit bcea97c5d6
4 changed files with 40 additions and 11 deletions
+1 -1
View File
@@ -48,7 +48,7 @@ else:
iw.show()
def gtk_show():
if window_manager.has_images():
if window_manager.has_windows():
window_manager.register_callback(gtk.main_quit)
gtk.main()
else:
+1 -1
View File
@@ -60,7 +60,7 @@ else:
def qt_show():
global app
if app and window_manager.has_images():
if app and window_manager.has_windows():
app.exec_()
else:
print 'No images to show. See `imshow`.'
+9 -9
View File
@@ -42,7 +42,7 @@ class WindowManager(object):
def _release(self, kit):
# releaseing the lock will lose all references to currently
# track images and callback.
# tracked images and the callback.
# this function is private for reason!
self._check_locked()
if str(kit) == self._guikit:
@@ -76,7 +76,7 @@ class WindowManager(object):
self._callback_args = cbargs
self._callback_kwargs = cbkwargs
def has_images(self):
def has_windows(self):
if len(self._windows) > 0:
return True
else:
@@ -117,7 +117,7 @@ def prepare_for_display(npy_img):
ignored.
'''
if len(npy_img.shape) < 2:
if npy_img.ndim < 2:
raise ValueError('Image must be 2D or 3D array')
height = npy_img.shape[0]
@@ -125,9 +125,9 @@ def prepare_for_display(npy_img):
out = np.empty((height, width, 3), dtype=np.uint8)
if len(npy_img.shape) == 2 or \
(len(npy_img.shape) == 3 and npy_img.shape[2] == 1):
if npy_img.dtype in [np.float32, np.float64]:
if npy_img.ndim == 2 or \
(npy_img.ndim == 3 and npy_img.shape[2] == 1):
if np.issubdtype(npy_img.dtype, float):
out[:,:,0] = npy_img*255
out[:,:,1] = out[:,:,0]
out[:,:,2] = out[:,:,0]
@@ -136,9 +136,9 @@ def prepare_for_display(npy_img):
out[:,:,1] = npy_img
out[:,:,2] = npy_img
elif len(npy_img.shape) == 3:
elif npy_img.ndim == 3:
if npy_img.shape[2] == 3 or npy_img.shape[2] == 4:
if npy_img.dtype in [np.float32, np.float64]:
if np.issubdtype(npy_img.dtype, float):
out[:,:,:3] = (npy_img[:,:,:3])*255
else:
out[:,:,:3] = npy_img[:,:,:3]
@@ -148,4 +148,4 @@ def prepare_for_display(npy_img):
else:
raise ValueError('Image must have 2 or 3 dimensions')
return out
return out
@@ -31,5 +31,34 @@ class TestPrepareForDisplay:
def test_wrong_depth(self):
x = prepare_for_display(np.random.random((10, 10, 5)))
class TestWindowManager:
callback_called = False
def setup(self):
self.wm = WindowManager()
self.wm.acquire('test')
def test_add_window(self):
self.wm.add_window('window1')
self.wm.remove_window('window1')
def callback(self):
self.callback_called = True
def test_callback(self):
cb = lambda x: x
self.wm.register_callback(self.callback)
self.wm.add_window('window')
self.wm.remove_window('window')
assert self.callback_called
def test_has_images(self):
assert not self.wm.has_windows()
self.wm.add_window('window')
assert self.wm.has_windows()
def teardown(self):
self.wm._release('test')
if __name__ == "__main__":
run_module_suite()