From 9512678e759fc6d13f14e3c4262bc84e0be8cd2f Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Tue, 21 Feb 2012 17:53:16 -0500 Subject: [PATCH 1/5] ENH: Add tifffile IO plugin --- skimage/io/_plugins/tifffile_plugin.ini | 3 +++ skimage/io/_plugins/tifffile_plugin.py | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 skimage/io/_plugins/tifffile_plugin.ini create mode 100644 skimage/io/_plugins/tifffile_plugin.py diff --git a/skimage/io/_plugins/tifffile_plugin.ini b/skimage/io/_plugins/tifffile_plugin.ini new file mode 100644 index 00000000..041e79a5 --- /dev/null +++ b/skimage/io/_plugins/tifffile_plugin.ini @@ -0,0 +1,3 @@ +[tifffile] +description = Open, save, or display images using tifffile.py +provides = imread, imsave diff --git a/skimage/io/_plugins/tifffile_plugin.py b/skimage/io/_plugins/tifffile_plugin.py new file mode 100644 index 00000000..a5688bd4 --- /dev/null +++ b/skimage/io/_plugins/tifffile_plugin.py @@ -0,0 +1,6 @@ +try: + from tifffile import imread, imsave +except ImportError: + raise ImportError("The tifffile module could not be found.\n" + "It can be obtained at " + "\n") From a6b154f5b56581ff6a0d7501fedfb453cb7f18b8 Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Tue, 21 Feb 2012 17:55:32 -0500 Subject: [PATCH 2/5] Fix description of tifffile plugin --- skimage/io/_plugins/tifffile_plugin.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/io/_plugins/tifffile_plugin.ini b/skimage/io/_plugins/tifffile_plugin.ini index 041e79a5..4666e503 100644 --- a/skimage/io/_plugins/tifffile_plugin.ini +++ b/skimage/io/_plugins/tifffile_plugin.ini @@ -1,3 +1,3 @@ [tifffile] -description = Open, save, or display images using tifffile.py +description = Open and save TIFF and TIFF-based (LSM, STK, etc.) images using tifffile.py provides = imread, imsave From e2d4261c6dbe24962bd51fe1156860a40fa824b8 Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Tue, 21 Feb 2012 18:14:59 -0500 Subject: [PATCH 3/5] ENH: Add tifffile tests and improve freeimage tests --- skimage/io/tests/test_freeimage.py | 44 ++++++++++++++++++++++-- skimage/io/tests/test_tifffile.py | 54 ++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 skimage/io/tests/test_tifffile.py diff --git a/skimage/io/tests/test_freeimage.py b/skimage/io/tests/test_freeimage.py index 4cbf9830..e5faa9be 100644 --- a/skimage/io/tests/test_freeimage.py +++ b/skimage/io/tests/test_freeimage.py @@ -6,18 +6,56 @@ from numpy.testing import * from numpy.testing.decorators import skipif try: - import skimage.io._plugins.freeimage_plugin as fi + import skimage.io._plugins.freeimage as fi FI_available = True + sio.use_plugin('freeimage') except OSError: FI_available = False @skipif(not FI_available) -def test_read(): - sio.use_plugin('freeimage', 'imread') +def test_imread(): img = sio.imread(os.path.join(si.data_dir, 'color.png')) assert img.shape == (370, 371, 3) assert all(img[274,135] == [0, 130, 253]) +@skipif(not FI_available) +def test_imread_uint16(): + expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(data_dir, 'chessboard_GRAY_U16.tif')) + assert img.dtype == np.uint16 + assert_array_almost_equal(img, expected) + +@skipif(not FI_available) +def test_imread_uint16_big_endian(): + expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(data_dir, 'chessboard_GRAY_U16B.tif')) + assert img.dtype == np.dtype('>u2') + assert_array_almost_equal(img, expected) + + +class TestSave: + def roundtrip(self, dtype, x, scaling=1): + f = NamedTemporaryFile(suffix='.tif') + fname = f.name + f.close() + sio.imsave(fname, x) + y = sio.imread(fname) + + assert_array_almost_equal((x * scaling).astype(np.int32), y) + + @skipif(not FI_available) + 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.float64): + x = np.ones(shape, dtype=dtype) * np.random.random(shape) + + if np.issubdtype(dtype, float): + yield self.roundtrip, dtype, x, 255 + else: + x = (x * 255).astype(dtype) + yield self.roundtrip, dtype, x + + @skipif(not FI_available) def test_metadata(): meta = fi.read_metadata(os.path.join(si.data_dir, 'multipage.tif')) diff --git a/skimage/io/tests/test_tifffile.py b/skimage/io/tests/test_tifffile.py new file mode 100644 index 00000000..ccc2b063 --- /dev/null +++ b/skimage/io/tests/test_tifffile.py @@ -0,0 +1,54 @@ +import os +import skimage as si +import skimage.io as sio + +from numpy.testing import * +from numpy.testing.decorators import skipif + +try: + import skimage.io._plugins.tifffile as tf + TF_available = True + sio.use_plugin('tifffile') +except OSError: + TF_available = False + +@skipif(not TF_available) +def test_imread_uint16(): + expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(data_dir, 'chessboard_GRAY_U16.tif')) + assert img.dtype == np.uint16 + assert_array_almost_equal(img, expected) + +@skipif(not TF_available) +def test_imread_uint16_big_endian(): + expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(data_dir, 'chessboard_GRAY_U16B.tif')) + assert img.dtype == np.dtype('>u2') + assert_array_almost_equal(img, expected) + + +class TestSave: + def roundtrip(self, dtype, x, scaling=1): + f = NamedTemporaryFile(suffix='.tif') + fname = f.name + f.close() + sio.imsave(fname, x) + y = sio.imread(fname) + + assert_array_almost_equal((x * scaling).astype(np.int32), y) + + @skipif(not TF_available) + 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.float64): + x = np.ones(shape, dtype=dtype) * np.random.random(shape) + + if np.issubdtype(dtype, float): + yield self.roundtrip, dtype, x, 255 + else: + x = (x * 255).astype(dtype) + yield self.roundtrip, dtype, x + + +if __name__ == "__main__": + run_module_suite() From b80ef2a66037a4c2d2f5f57061925e263ac10f90 Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Tue, 21 Feb 2012 18:17:34 -0500 Subject: [PATCH 4/5] Fix copy/paste error --- skimage/io/tests/test_freeimage.py | 2 +- skimage/io/tests/test_tifffile.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/io/tests/test_freeimage.py b/skimage/io/tests/test_freeimage.py index e5faa9be..ba75fc91 100644 --- a/skimage/io/tests/test_freeimage.py +++ b/skimage/io/tests/test_freeimage.py @@ -6,7 +6,7 @@ from numpy.testing import * from numpy.testing.decorators import skipif try: - import skimage.io._plugins.freeimage as fi + import skimage.io._plugins.freeimage_plugin as fi FI_available = True sio.use_plugin('freeimage') except OSError: diff --git a/skimage/io/tests/test_tifffile.py b/skimage/io/tests/test_tifffile.py index ccc2b063..454c2c1f 100644 --- a/skimage/io/tests/test_tifffile.py +++ b/skimage/io/tests/test_tifffile.py @@ -6,7 +6,7 @@ from numpy.testing import * from numpy.testing.decorators import skipif try: - import skimage.io._plugins.tifffile as tf + import skimage.io._plugins.tifffile_plugin as tf TF_available = True sio.use_plugin('tifffile') except OSError: From 026fae0e977cbc8e9916ef7fa88d16f746527c54 Mon Sep 17 00:00:00 2001 From: Zach Pincus Date: Tue, 21 Feb 2012 19:17:55 -0500 Subject: [PATCH 5/5] BUG: Fix tests --- skimage/io/tests/test_freeimage.py | 41 +++++++++++++++++------------- skimage/io/tests/test_tifffile.py | 23 ++++++++--------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/skimage/io/tests/test_freeimage.py b/skimage/io/tests/test_freeimage.py index ba75fc91..05ce5258 100644 --- a/skimage/io/tests/test_freeimage.py +++ b/skimage/io/tests/test_freeimage.py @@ -1,9 +1,11 @@ import os import skimage as si import skimage.io as sio +import numpy as np from numpy.testing import * from numpy.testing.decorators import skipif +from tempfile import NamedTemporaryFile try: import skimage.io._plugins.freeimage_plugin as fi @@ -20,40 +22,43 @@ def test_imread(): @skipif(not FI_available) def test_imread_uint16(): - expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) - img = sio.imread(os.path.join(data_dir, 'chessboard_GRAY_U16.tif')) + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16.tif')) assert img.dtype == np.uint16 assert_array_almost_equal(img, expected) @skipif(not FI_available) def test_imread_uint16_big_endian(): - expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) - img = sio.imread(os.path.join(data_dir, 'chessboard_GRAY_U16B.tif')) - assert img.dtype == np.dtype('>u2') + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16B.tif')) + assert img.dtype == np.uint16 assert_array_almost_equal(img, expected) class TestSave: - def roundtrip(self, dtype, x, scaling=1): - f = NamedTemporaryFile(suffix='.tif') + def roundtrip(self, dtype, x, suffix): + print dtype, x.shape, suffix + f = NamedTemporaryFile(suffix='.'+suffix) fname = f.name f.close() sio.imsave(fname, x) y = sio.imread(fname) - - assert_array_almost_equal((x * scaling).astype(np.int32), y) + assert_array_equal(x, y) @skipif(not FI_available) 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.float64): - x = np.ones(shape, dtype=dtype) * np.random.random(shape) - - if np.issubdtype(dtype, float): - yield self.roundtrip, dtype, x, 255 - else: - x = (x * 255).astype(dtype) - yield self.roundtrip, dtype, x + for shape, dtype, format in [ + [(10, 10), (np.uint8, np.uint16), ('tif', 'png')], + [(10, 10), (np.float32,), ('tif',)], + [(10, 10, 3), (np.uint8,), ('png',)], + [(10, 10, 4), (np.uint8,), ('png',)] + ]: + tests = [(d,f) for d in dtype for f in format] + for d, f in tests: + x = np.ones(shape, dtype=d) * np.random.random(shape) + if not np.issubdtype(d, float): + x = (x * 255).astype(d) + yield self.roundtrip, d, x, f @skipif(not FI_available) diff --git a/skimage/io/tests/test_tifffile.py b/skimage/io/tests/test_tifffile.py index 454c2c1f..e412cd93 100644 --- a/skimage/io/tests/test_tifffile.py +++ b/skimage/io/tests/test_tifffile.py @@ -1,9 +1,11 @@ import os import skimage as si import skimage.io as sio +import numpy as np from numpy.testing import * from numpy.testing.decorators import skipif +from tempfile import NamedTemporaryFile try: import skimage.io._plugins.tifffile_plugin as tf @@ -14,28 +16,27 @@ except OSError: @skipif(not TF_available) def test_imread_uint16(): - expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) - img = sio.imread(os.path.join(data_dir, 'chessboard_GRAY_U16.tif')) + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16.tif')) assert img.dtype == np.uint16 assert_array_almost_equal(img, expected) @skipif(not TF_available) def test_imread_uint16_big_endian(): - expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy')) - img = sio.imread(os.path.join(data_dir, 'chessboard_GRAY_U16B.tif')) - assert img.dtype == np.dtype('>u2') + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16B.tif')) + assert img.dtype == np.uint16 assert_array_almost_equal(img, expected) class TestSave: - def roundtrip(self, dtype, x, scaling=1): + def roundtrip(self, dtype, x): f = NamedTemporaryFile(suffix='.tif') fname = f.name f.close() sio.imsave(fname, x) y = sio.imread(fname) - - assert_array_almost_equal((x * scaling).astype(np.int32), y) + assert_array_equal(x, y) @skipif(not TF_available) def test_imsave_roundtrip(self): @@ -43,11 +44,9 @@ class TestSave: for dtype in (np.uint8, np.uint16, np.float32, np.float64): x = np.ones(shape, dtype=dtype) * np.random.random(shape) - if np.issubdtype(dtype, float): - yield self.roundtrip, dtype, x, 255 - else: + if not np.issubdtype(dtype, float): x = (x * 255).astype(dtype) - yield self.roundtrip, dtype, x + yield self.roundtrip, dtype, x if __name__ == "__main__":