mirror of
https://github.com/wassname/scikit-image.git
synced 2026-08-08 11:26:12 +08:00
Fix PIL tests, move helpers to _shared, fix widgets test
This commit is contained in:
@@ -2,6 +2,12 @@
|
||||
|
||||
|
||||
import re
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
from skimage import (
|
||||
data, io, img_as_uint, img_as_bool, img_as_float, img_as_int, img_as_ubyte)
|
||||
from numpy import testing
|
||||
import numpy as np
|
||||
|
||||
|
||||
SKIP_RE = re.compile("(\s*>>>.*?)(\s*)#\s*skip\s+if\s+(.*)$")
|
||||
@@ -75,3 +81,79 @@ def doctest_skip_parser(func):
|
||||
new_lines.append(code)
|
||||
func.__doc__ = "\n".join(new_lines)
|
||||
return func
|
||||
|
||||
|
||||
def roundtrip(img, plugin, suffix):
|
||||
"""Save and read an image using a specified plugin"""
|
||||
if not '.' in suffix:
|
||||
suffix = '.' + suffix
|
||||
temp_file = NamedTemporaryFile(suffix=suffix, delete=False)
|
||||
temp_file.close()
|
||||
fname = temp_file.name
|
||||
io.imsave(fname, img, plugin=plugin)
|
||||
return io.imread(fname, plugin=plugin)
|
||||
|
||||
|
||||
def ubyte_check(plugin, fmt='png'):
|
||||
"""Check roundtrip behavior for images that can only be saved as uint8
|
||||
|
||||
All major input types should be handled as ubytes and read
|
||||
back correctly.
|
||||
"""
|
||||
img = img_as_ubyte(data.chelsea())
|
||||
r1 = roundtrip(img, plugin, fmt)
|
||||
testing.assert_allclose(img, r1)
|
||||
|
||||
img2 = img > 128
|
||||
r2 = roundtrip(img2, plugin, fmt)
|
||||
testing.assert_allclose(img2.astype(np.uint8), r2)
|
||||
|
||||
img3 = img_as_float(img)
|
||||
r3 = roundtrip(img3, plugin, fmt)
|
||||
testing.assert_allclose(r3, img)
|
||||
|
||||
img4 = img_as_int(img)
|
||||
r4 = roundtrip(img4, plugin, fmt)
|
||||
testing.assert_allclose(r4, img)
|
||||
|
||||
img5 = img_as_uint(img)
|
||||
r5 = roundtrip(img5, plugin, fmt)
|
||||
testing.assert_allclose(r5, img)
|
||||
|
||||
|
||||
def full_range_check(plugin, fmt='png'):
|
||||
"""Check the roundtrip behavior for images that support most types.
|
||||
|
||||
All major input types should be handled, except bool is treated
|
||||
as ubyte and float can treated as uint16 or float.
|
||||
"""
|
||||
|
||||
img = img_as_ubyte(data.moon())
|
||||
r1 = roundtrip(img, plugin, fmt)
|
||||
testing.assert_allclose(img, r1)
|
||||
|
||||
img2 = img > 128
|
||||
r2 = roundtrip(img2, plugin, fmt)
|
||||
testing.assert_allclose(img2.astype(np.uint8), r2)
|
||||
|
||||
img3 = img_as_float(img)
|
||||
r3 = roundtrip(img3, plugin, fmt)
|
||||
if r3.dtype.kind == 'f':
|
||||
testing.assert_allclose(img3, r3)
|
||||
else:
|
||||
testing.assert_allclose(r3, img_as_uint(img))
|
||||
|
||||
img4 = img_as_int(img)
|
||||
r4 = roundtrip(img4, plugin, fmt)
|
||||
testing.assert_allclose(r4, img4)
|
||||
|
||||
img5 = img_as_uint(img)
|
||||
r5 = roundtrip(img5, plugin, fmt)
|
||||
testing.assert_allclose(r5, img5)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ubyte_check('pil')
|
||||
full_range_check('pil')
|
||||
ubyte_check('pil', 'bmp')
|
||||
full_range_check('pil', 'tiff')
|
||||
|
||||
@@ -8,7 +8,7 @@ from tempfile import NamedTemporaryFile
|
||||
from skimage import data_dir
|
||||
from skimage.io import (imread, imsave, use_plugin, reset_plugins,
|
||||
Image as ioImage)
|
||||
from skimage.io.tests.utils import ubyte_check, full_range_check
|
||||
from skimage._shared.testing import ubyte_check, full_range_check
|
||||
|
||||
from six import BytesIO
|
||||
|
||||
@@ -94,7 +94,7 @@ def test_imread_truncated_jpg():
|
||||
def test_imread_uint16_big_endian():
|
||||
expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy'))
|
||||
img = imread(os.path.join(data_dir, 'chessboard_GRAY_U16B.tif'))
|
||||
assert img.dtype == np.dtype('>u2')
|
||||
assert img.dtype == np.uint16
|
||||
assert_array_almost_equal(img, expected)
|
||||
|
||||
|
||||
@@ -133,7 +133,6 @@ class TestSave:
|
||||
self.verify_imsave_roundtrip(self.roundtrip_pil_image)
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_imsave_filelike():
|
||||
shape = (2, 2)
|
||||
image = np.zeros(shape)
|
||||
@@ -149,7 +148,6 @@ def test_imsave_filelike():
|
||||
assert_allclose(out, image)
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_imexport_imimport():
|
||||
shape = (2, 2)
|
||||
image = np.zeros(shape)
|
||||
@@ -158,13 +156,11 @@ def test_imexport_imimport():
|
||||
assert out.shape == shape
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_all_color():
|
||||
ubyte_check('pil')
|
||||
ubyte_check('pil', 'bmp')
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_all_mono():
|
||||
full_range_check('pil')
|
||||
full_range_check('pil', 'tiff')
|
||||
@@ -175,8 +171,8 @@ class TestSaveTIF:
|
||||
f = NamedTemporaryFile(suffix='.tif')
|
||||
fname = f.name
|
||||
f.close()
|
||||
sio.imsave(fname, x)
|
||||
y = sio.imread(fname)
|
||||
imsave(fname, x)
|
||||
y = imread(fname)
|
||||
assert_array_equal(x, y)
|
||||
|
||||
def test_imsave_roundtrip(self):
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
from skimage import (
|
||||
data, io, img_as_uint, img_as_bool, img_as_float, img_as_int, img_as_ubyte)
|
||||
from numpy import testing
|
||||
import numpy as np
|
||||
|
||||
|
||||
def roundtrip(img, plugin, suffix):
|
||||
"""Save and read an image using a specified plugin"""
|
||||
if not '.' in suffix:
|
||||
suffix = '.' + suffix
|
||||
temp_file = NamedTemporaryFile(suffix=suffix, delete=False)
|
||||
temp_file.close()
|
||||
fname = temp_file.name
|
||||
io.imsave(fname, img, plugin=plugin)
|
||||
return io.imread(fname, plugin=plugin)
|
||||
|
||||
|
||||
def ubyte_check(plugin, fmt='png'):
|
||||
"""Check roundtrip behavior for images that can only be saved as uint8
|
||||
|
||||
All major input types should be handled as ubytes and read
|
||||
back correctly.
|
||||
"""
|
||||
img = img_as_ubyte(data.chelsea())
|
||||
r1 = roundtrip(img, plugin, fmt)
|
||||
testing.assert_allclose(img, r1)
|
||||
|
||||
img2 = img > 128
|
||||
r2 = roundtrip(img2, plugin, fmt)
|
||||
testing.assert_allclose(img2.astype(np.uint8), r2)
|
||||
|
||||
img3 = img_as_float(img)
|
||||
r3 = roundtrip(img3, plugin, fmt)
|
||||
testing.assert_allclose(r3, img)
|
||||
|
||||
img4 = img_as_int(img)
|
||||
r4 = roundtrip(img4, plugin, fmt)
|
||||
testing.assert_allclose(r4, img)
|
||||
|
||||
img5 = img_as_uint(img)
|
||||
r5 = roundtrip(img5, plugin, fmt)
|
||||
testing.assert_allclose(r5, img)
|
||||
|
||||
|
||||
def full_range_check(plugin, fmt='png'):
|
||||
"""Check the roundtrip behavior for images that support most types.
|
||||
|
||||
All major input types should be handled, except bool is treated
|
||||
as ubyte and float can treated as uint16 or float.
|
||||
"""
|
||||
|
||||
img = img_as_ubyte(data.moon())
|
||||
r1 = roundtrip(img, plugin, fmt)
|
||||
testing.assert_allclose(img, r1)
|
||||
|
||||
img2 = img > 128
|
||||
r2 = roundtrip(img2, plugin, fmt)
|
||||
testing.assert_allclose(img2.astype(np.uint8), r2)
|
||||
|
||||
img3 = img_as_float(img)
|
||||
r3 = roundtrip(img3, plugin, fmt)
|
||||
if r3.dtype.kind == 'f':
|
||||
testing.assert_allclose(img3, r3)
|
||||
else:
|
||||
testing.assert_allclose(r3, img_as_uint(img))
|
||||
|
||||
img4 = img_as_int(img)
|
||||
r4 = roundtrip(img4, plugin, fmt)
|
||||
testing.assert_allclose(r4, img4)
|
||||
|
||||
img5 = img_as_uint(img)
|
||||
r5 = roundtrip(img5, plugin, fmt)
|
||||
testing.assert_allclose(r5, img5)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ubyte_check('pil')
|
||||
full_range_check('pil')
|
||||
ubyte_check('pil', 'bmp')
|
||||
full_range_check('pil', 'tiff')
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
import os
|
||||
from skimage import data, img_as_float, io
|
||||
from skimage import data, img_as_float, io, img_as_uint
|
||||
from skimage.viewer import ImageViewer, viewer_available
|
||||
from skimage.viewer.widgets import (
|
||||
Slider, OKCancelButtons, SaveButtons, ComboBox, CheckBox, Text)
|
||||
@@ -27,10 +27,10 @@ def test_check_box():
|
||||
cb.val = False
|
||||
assert_equal(cb.val, False)
|
||||
cb.val = 1
|
||||
assert_equal(cb.val, True)
|
||||
assert_equal(cb.val, True)
|
||||
cb.val = 0
|
||||
assert_equal(cb.val, False)
|
||||
|
||||
|
||||
|
||||
@skipif(not viewer_available)
|
||||
def test_combo_box():
|
||||
@@ -101,8 +101,8 @@ def test_save_buttons():
|
||||
sv.save_to_stack()
|
||||
sv.save_to_file(filename)
|
||||
|
||||
img = img_as_float(data.imread(filename))
|
||||
assert_almost_equal(img, viewer.image)
|
||||
img = data.imread(filename)
|
||||
assert_almost_equal(img, img_as_uint(viewer.image))
|
||||
|
||||
img = io.pop()
|
||||
assert_almost_equal(img, viewer.image)
|
||||
|
||||
Reference in New Issue
Block a user