diff --git a/skimage/external/test_tifffile.py b/skimage/external/test_tifffile.py new file mode 100644 index 00000000..537b2ea3 --- /dev/null +++ b/skimage/external/test_tifffile.py @@ -0,0 +1,66 @@ +import os +import numpy as np + +try: + import skimage as si +except Exception: + si = None + +from numpy.testing import ( + assert_array_equal, assert_array_almost_equal, run_module_suite) +from numpy.testing.decorators import skipif + +from tempfile import NamedTemporaryFile +from .tifffile import imread, imsave + + +np.random.seed(0) + + +@skipif(si is None) +def test_imread_uint16(): + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16.tif')) + assert img.dtype == np.uint16 + assert_array_almost_equal(img, expected) + + +@skipif(si is None) +def test_imread_uint16_big_endian(): + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16B.tif')) + assert img.dtype == np.uint16 + assert_array_almost_equal(img, expected) + + +def test_extension(): + from .tifffile.tifffile import decodelzw + import types + assert isinstance(decodelzw, types.BuiltinFunctionType), type(decodelzw) + + +class TestSave: + + def roundtrip(self, dtype, x): + f = NamedTemporaryFile(suffix='.tif') + fname = f.name + f.close() + imsave(fname, x) + y = imread(fname) + assert_array_equal(x, y) + + def test_imsave_roundtrip(self): + for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]: + for dtype in (np.uint8, np.uint16, np.float32, np.int16, + np.float64): + x = np.random.rand(*shape) + + if not np.issubdtype(dtype, float): + x = (x * np.iinfo(dtype).max).astype(dtype) + else: + x = x.astype(dtype) + yield self.roundtrip, dtype, x + + +if __name__ == "__main__": + run_module_suite() diff --git a/skimage/external/tifffile/__init__.py b/skimage/external/tifffile/__init__.py index bd426a3b..cb20f71e 100644 --- a/skimage/external/tifffile/__init__.py +++ b/skimage/external/tifffile/__init__.py @@ -1,4 +1,5 @@ -try: - from tifffile import imread, imsave, TiffFile -except ImportError: - from .tifffile_local import imread, imsave, TiffFile +from .tifffile import imsave, imread, imshow, TiffFile, TiffWriter, TiffSequence + +__version__ = '0.6.2' +__all__ = ('imsave', 'imread', 'imshow', 'TiffFile', 'TiffWriter', + 'TiffSequence') diff --git a/skimage/external/tifffile/tifffile_local.py b/skimage/external/tifffile/tifffile.py similarity index 99% rename from skimage/external/tifffile/tifffile_local.py rename to skimage/external/tifffile/tifffile.py index 0e6a839b..817bb44f 100644 --- a/skimage/external/tifffile/tifffile_local.py +++ b/skimage/external/tifffile/tifffile.py @@ -149,9 +149,15 @@ from xml.etree import cElementTree as etree import numpy -from . import _tifffile +try: + from . import _tifffile +except ImportError: + warnings.warn( + "failed to import the optional _tifffile C extension module.\n" + "Loading of some compressed images will be slow.\n" + "Tifffile.c can be obtained at http://www.lfd.uci.edu/~gohlke/") -__version__ = '0.4.1' +__version__ = '2014.08.24' __docformat__ = 'restructuredtext en' __all__ = ('imsave', 'imread', 'imshow', 'TiffFile', 'TiffWriter', 'TiffSequence') @@ -179,8 +185,8 @@ def imsave(filename, data, **kwargs): Examples -------- >>> data = numpy.random.rand(2, 5, 3, 301, 219) - >>> description = '{"shape": %s}' % str(list(data.shape)) - >>> imsave('temp.tif', data, compress=6, + >>> description = u'{"shape": %s}' % str(list(data.shape)) # doctest: +SKIP + >>> imsave('temp.tif', data, compress=6, # doctest: +SKIP ... extratags=[(270, 's', 0, description, True)]) """ @@ -661,12 +667,12 @@ def imread(files, **kwargs): Examples -------- - >>> im = imread('temp.tif', key=0) - >>> im.shape - (3, 301, 219) - >>> ims = imread(['temp.tif', 'temp.tif']) - >>> ims.shape - (2, 10, 3, 301, 219) + >>> im = imread('test.tif', key=0) # doctest: +SKIP + >>> im.shape # doctest: +SKIP + (256, 256, 4) + >>> ims = imread(['test.tif', 'test.tif']) # doctest: +SKIP + >>> ims.shape # doctest: +SKIP + (2, 256, 256, 4) """ kwargs_file = {} @@ -731,10 +737,10 @@ class TiffFile(object): Examples -------- - >>> with TiffFile('temp.tif') as tif: + >>> with TiffFile('test.tif') as tif: # doctest: +SKIP ... data = tif.asarray() ... data.shape - (5, 301, 219) + (256, 256, 4) """ def __init__(self, arg, name=None, offset=None, size=None, @@ -3112,8 +3118,11 @@ def _replace_by(module_function, package=None, warn=False): full_name = modname else: full_name = package + '.' + modname - module = __import__(full_name, fromlist=[modname]) - func, oldfunc = getattr(module, function), func + if modname == '_tifffile': + func = getattr(_tifffile, function) + else: + module = __import__(full_name, fromlist=[modname]) + func, oldfunc = getattr(module, function), func globals()['__old_' + func.__name__] = oldfunc except Exception: if warn: