"Fossies" - the Fresh Open Source Software Archive

Member "scikit-image-0.19.3/skimage/io/tests/test_tifffile.py" (12 Jun 2022, 2282 Bytes) of package /linux/misc/scikit-image-0.19.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_tifffile.py": 0.19.2_vs_0.19.3.

    1 import pathlib
    2 from tempfile import NamedTemporaryFile
    3 
    4 import numpy as np
    5 import pytest
    6 from numpy.testing import assert_array_almost_equal, assert_array_equal
    7 
    8 from skimage._shared.testing import fetch
    9 from skimage.io import imread, imsave, reset_plugins, use_plugin
   10 
   11 
   12 def setup():
   13     use_plugin('tifffile')
   14     np.random.seed(0)
   15 
   16 
   17 def teardown():
   18     reset_plugins()
   19 
   20 
   21 def test_imread_uint16():
   22     expected = np.load(fetch('data/chessboard_GRAY_U8.npy'))
   23     img = imread(fetch('data/chessboard_GRAY_U16.tif'))
   24     assert img.dtype == np.uint16
   25     assert_array_almost_equal(img, expected)
   26 
   27 
   28 def test_imread_uint16_big_endian():
   29     expected = np.load(fetch('data/chessboard_GRAY_U8.npy'))
   30     img = imread(fetch('data/chessboard_GRAY_U16B.tif'))
   31     assert img.dtype == np.uint16
   32     assert_array_almost_equal(img, expected)
   33 
   34 
   35 def test_imread_multipage_rgb_tif():
   36     img = imread(fetch('data/multipage_rgb.tif'))
   37     assert img.shape == (2, 10, 10, 3), img.shape
   38 
   39 
   40 def test_tifffile_kwarg_passthrough ():
   41     img = imread(fetch('data/multipage.tif'), key=[1], is_ome=True)
   42     assert img.shape == (15, 10), img.shape
   43 
   44 
   45 def test_imread_handle():
   46     expected = np.load(fetch('data/chessboard_GRAY_U8.npy'))
   47     with open(fetch('data/chessboard_GRAY_U16.tif'), 'rb') as fh:
   48         img = imread(fh)
   49     assert img.dtype == np.uint16
   50     assert_array_almost_equal(img, expected)
   51 
   52 
   53 class TestSave:
   54     def roundtrip(self, dtype, x, use_pathlib=False):
   55         f = NamedTemporaryFile(suffix='.tif')
   56         fname = f.name
   57         f.close()
   58         if use_pathlib:
   59             fname = pathlib.Path(fname)
   60         imsave(fname, x, check_contrast=False)
   61         y = imread(fname)
   62         assert_array_equal(x, y)
   63 
   64     shapes = ((10, 10), (10, 10, 3), (10, 10, 4))
   65     dtypes = (np.uint8, np.uint16, np.float32, np.int16, np.float64)
   66 
   67     @pytest.mark.parametrize("shape", shapes)
   68     @pytest.mark.parametrize("dtype", dtypes)
   69     @pytest.mark.parametrize("use_pathlib", [False, True])
   70     def test_imsave_roundtrip(self, shape, dtype, use_pathlib):
   71         x = np.random.rand(*shape)
   72 
   73         if not np.issubdtype(dtype, np.floating):
   74             x = (x * np.iinfo(dtype).max).astype(dtype)
   75         else:
   76             x = x.astype(dtype)
   77         self.roundtrip(dtype, x, use_pathlib)