"Fossies" - the Fresh Open Source Software Archive

Member "scikit-image-0.19.3/skimage/transform/tests/test_warps.py" (12 Jun 2022, 33801 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_warps.py": 0.19.2_vs_0.19.3.

    1 import numpy as np
    2 import pytest
    3 from numpy.testing import (assert_allclose, assert_array_almost_equal,
    4                            assert_array_equal)
    5 from scipy.ndimage import map_coordinates
    6 
    7 from skimage._shared.testing import expected_warnings, test_parallel
    8 from skimage._shared.utils import _supported_float_type
    9 from skimage.color.colorconv import rgb2gray
   10 from skimage.data import checkerboard, astronaut
   11 from skimage.draw.draw import circle_perimeter_aa
   12 from skimage.feature.peak import peak_local_max
   13 from skimage.transform._warps import (_stackcopy,
   14                                       _linear_polar_mapping,
   15                                       _log_polar_mapping, warp,
   16                                       warp_coords, rotate, resize,
   17                                       rescale, warp_polar, swirl,
   18                                       downscale_local_mean,
   19                                       resize_local_mean)
   20 from skimage.transform._geometric import (AffineTransform,
   21                                           ProjectiveTransform,
   22                                           SimilarityTransform)
   23 from skimage.util.dtype import img_as_float, _convert
   24 
   25 
   26 np.random.seed(0)
   27 
   28 
   29 def test_stackcopy():
   30     layers = 4
   31     x = np.empty((3, 3, layers))
   32     y = np.eye(3, 3)
   33     _stackcopy(x, y)
   34     for i in range(layers):
   35         assert_array_almost_equal(x[..., i], y)
   36 
   37 
   38 def test_warp_tform():
   39     x = np.zeros((5, 5), dtype=np.double)
   40     x[2, 2] = 1
   41     theta = - np.pi / 2
   42     tform = SimilarityTransform(scale=1, rotation=theta, translation=(0, 4))
   43 
   44     x90 = warp(x, tform, order=1)
   45     assert_array_almost_equal(x90, np.rot90(x))
   46 
   47     x90 = warp(x, tform.inverse, order=1)
   48     assert_array_almost_equal(x90, np.rot90(x))
   49 
   50 
   51 def test_warp_callable():
   52     x = np.zeros((5, 5), dtype=np.double)
   53     x[2, 2] = 1
   54     refx = np.zeros((5, 5), dtype=np.double)
   55     refx[1, 1] = 1
   56 
   57     def shift(xy):
   58         return xy + 1
   59 
   60     outx = warp(x, shift, order=1)
   61     assert_array_almost_equal(outx, refx)
   62 
   63 
   64 @test_parallel()
   65 def test_warp_matrix():
   66     x = np.zeros((5, 5), dtype=np.double)
   67     x[2, 2] = 1
   68     refx = np.zeros((5, 5), dtype=np.double)
   69     refx[1, 1] = 1
   70 
   71     matrix = np.array([[1, 0, 1], [0, 1, 1], [0, 0, 1]])
   72 
   73     # _warp_fast
   74     outx = warp(x, matrix, order=1)
   75     assert_array_almost_equal(outx, refx)
   76     # check for ndimage.map_coordinates
   77     outx = warp(x, matrix, order=5)
   78 
   79 
   80 def test_warp_nd():
   81     for dim in range(2, 8):
   82         shape = dim * (5,)
   83 
   84         x = np.zeros(shape, dtype=np.double)
   85         x_c = dim * (2,)
   86         x[x_c] = 1
   87         refx = np.zeros(shape, dtype=np.double)
   88         refx_c = dim * (1,)
   89         refx[refx_c] = 1
   90 
   91         coord_grid = dim * (slice(0, 5, 1),)
   92         coords = np.array(np.mgrid[coord_grid]) + 1
   93 
   94         outx = warp(x, coords, order=0, cval=0)
   95 
   96         assert_array_almost_equal(outx, refx)
   97 
   98 
   99 def test_warp_clip():
  100     x = np.zeros((5, 5), dtype=np.double)
  101     x[2, 2] = 1
  102 
  103     outx = rescale(x, 3, order=3, clip=False, anti_aliasing=False,
  104                    mode='constant')
  105     assert outx.min() < 0
  106 
  107     outx = rescale(x, 3, order=3, clip=True, anti_aliasing=False,
  108                    mode='constant')
  109     assert_array_almost_equal(outx.min(), 0)
  110     assert_array_almost_equal(outx.max(), 1)
  111 
  112 
  113 @pytest.mark.parametrize('order', [0, 1, 3])
  114 def test_warp_clip_image_containing_nans(order):
  115     # Test that clipping works as intended on an image with NaNs
  116     # Orders 2, 4, and 5 do not produce good output when the input image has
  117     # NaNs, so those orders are not tested
  118 
  119     x = np.ones((15, 15), dtype=np.float64)
  120     x[7, 7] = np.nan
  121 
  122     outx = rotate(x, 45, order=order, cval=2, resize=True, clip=True)
  123 
  124     assert_array_almost_equal(np.nanmin(outx), 1)
  125     assert_array_almost_equal(np.nanmax(outx), 2)
  126 
  127 
  128 @pytest.mark.parametrize('order', [0, 1, 3])
  129 def test_warp_clip_cval_is_nan(order):
  130     # Test that clipping works as intended when cval is NaN
  131     # Orders 2, 4, and 5 do not produce good output when cval is NaN, so those
  132     # orders are not tested
  133 
  134     x = np.ones((15, 15), dtype=np.float64)
  135     x[5:-5, 5:-5] = 2
  136 
  137     outx = rotate(x, 45, order=order, cval=np.nan, resize=True, clip=True)
  138 
  139     assert_array_almost_equal(np.nanmin(outx), 1)
  140     assert_array_almost_equal(np.nanmax(outx), 2)
  141 
  142 
  143 @pytest.mark.parametrize('order', range(6))
  144 def test_warp_clip_cval_outside_input_range(order):
  145     # Test that clipping behavior considers cval part of the input range
  146 
  147     x = np.ones((15, 15), dtype=np.float64)
  148 
  149     # Specify a cval that is outside the input range to check clipping
  150     with expected_warnings(['Bi-quadratic.*bug'] if order == 2 else None):
  151         outx = rotate(x, 45, order=order, cval=2, resize=True, clip=True)
  152 
  153     # The corners should be cval for all interpolation orders
  154     assert_array_almost_equal([outx[0, 0], outx[0, -1],
  155                                outx[-1, 0], outx[-1, -1]], 2)
  156 
  157     # For all interpolation orders other than nearest-neighbor, the clipped
  158     # output should have some pixels with values between the input (1) and
  159     # cval (2) (i.e., clipping should not set them to 1)
  160     if order > 0:
  161         assert np.sum(np.less(1, outx) * np.less(outx, 2)) > 0
  162 
  163 
  164 @pytest.mark.parametrize('order', range(6))
  165 def test_warp_clip_cval_not_used(order):
  166     # Test that clipping does not consider cval part of the input range if it
  167     # is not used in the output image
  168 
  169     x = np.ones((15, 15), dtype=np.float64)
  170     x[5:-5, 5:-5] = 2
  171 
  172     # Transform the image by stretching it out by one pixel on each side so
  173     # that cval will not actually be used
  174     transform = AffineTransform(scale=15/(15+2), translation=(1, 1))
  175     with expected_warnings(['Bi-quadratic.*bug'] if order == 2 else None):
  176         outx = warp(x, transform, mode='constant', order=order, cval=0,
  177                     clip=True)
  178 
  179     # At higher orders of interpolation, the transformed image has overshoots
  180     # beyond the input range that should be clipped to the range 1 to 2.  Even
  181     # though cval=0, the minimum value of the clipped output image should be
  182     # 1 and not affected by the unused cval.
  183     assert_array_almost_equal(outx.min(), 1)
  184 
  185 
  186 def test_homography():
  187     x = np.zeros((5, 5), dtype=np.double)
  188     x[1, 1] = 1
  189     theta = -np.pi / 2
  190     M = np.array([[np.cos(theta), - np.sin(theta), 0],
  191                   [np.sin(theta),   np.cos(theta), 4],
  192                   [0,               0,             1]])
  193 
  194     x90 = warp(x,
  195                inverse_map=ProjectiveTransform(M).inverse,
  196                order=1)
  197     assert_array_almost_equal(x90, np.rot90(x))
  198 
  199 
  200 @pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
  201 def test_rotate(dtype):
  202     x = np.zeros((5, 5), dtype=dtype)
  203     x[1, 1] = 1
  204     x90 = rotate(x, 90)
  205     assert x90.dtype == _supported_float_type(dtype)
  206     assert_array_almost_equal(x90, np.rot90(x))
  207 
  208 
  209 def test_rotate_resize():
  210     x = np.zeros((10, 10), dtype=np.double)
  211 
  212     x45 = rotate(x, 45, resize=False)
  213     assert x45.shape == (10, 10)
  214 
  215     x45 = rotate(x, 45, resize=True)
  216     # new dimension should be d = sqrt(2 * (10/2)^2)
  217     assert x45.shape == (14, 14)
  218 
  219 
  220 def test_rotate_center():
  221     x = np.zeros((10, 10), dtype=np.double)
  222     x[4, 4] = 1
  223     refx = np.zeros((10, 10), dtype=np.double)
  224     refx[2, 5] = 1
  225     x20 = rotate(x, 20, order=0, center=(0, 0))
  226     assert_array_almost_equal(x20, refx)
  227     x0 = rotate(x20, -20, order=0, center=(0, 0))
  228     assert_array_almost_equal(x0, x)
  229 
  230 
  231 def test_rotate_resize_center():
  232     x = np.zeros((10, 10), dtype=np.double)
  233     x[0, 0] = 1
  234 
  235     ref_x45 = np.zeros((14, 14), dtype=np.double)
  236     ref_x45[6, 0] = 1
  237     ref_x45[7, 0] = 1
  238 
  239     x45 = rotate(x, 45, resize=True, center=(3, 3), order=0,
  240                  mode='reflect')
  241     # new dimension should be d = sqrt(2 * (10/2)^2)
  242     assert x45.shape == (14, 14)
  243     assert_array_equal(x45, ref_x45)
  244 
  245 
  246 def test_rotate_resize_90():
  247     x90 = rotate(np.zeros((470, 230), dtype=np.double), 90, resize=True)
  248     assert x90.shape == (230, 470)
  249 
  250 
  251 def test_rescale():
  252     # same scale factor
  253     x = np.zeros((5, 5), dtype=np.double)
  254     x[1, 1] = 1
  255     scaled = rescale(x, 2, order=0, anti_aliasing=False, mode='constant')
  256     ref = np.zeros((10, 10))
  257     ref[2:4, 2:4] = 1
  258     assert_array_almost_equal(scaled, ref)
  259 
  260     # different scale factors
  261     x = np.zeros((5, 5), dtype=np.double)
  262     x[1, 1] = 1
  263 
  264     scaled = rescale(x, (2, 1), order=0, anti_aliasing=False, mode='constant')
  265     ref = np.zeros((10, 5))
  266     ref[2:4, 1] = 1
  267     assert_array_almost_equal(scaled, ref)
  268 
  269 
  270 def test_rescale_invalid_scale():
  271     x = np.zeros((10, 10, 3))
  272     with pytest.raises(ValueError):
  273         rescale(x, (2, 2),
  274                 channel_axis=None, anti_aliasing=False, mode='constant')
  275     with pytest.raises(ValueError):
  276         rescale(x, (2, 2, 2),
  277                 channel_axis=-1, anti_aliasing=False, mode='constant')
  278 
  279 
  280 def test_rescale_multichannel():
  281     # 1D + channels
  282     x = np.zeros((8, 3), dtype=np.double)
  283     scaled = rescale(x, 2, order=0, channel_axis=-1, anti_aliasing=False,
  284                      mode='constant')
  285     assert scaled.shape == (16, 3)
  286     # 2D
  287     scaled = rescale(x, 2, order=0, channel_axis=None, anti_aliasing=False,
  288                      mode='constant')
  289     assert scaled.shape == (16, 6)
  290 
  291     # 2D + channels
  292     x = np.zeros((8, 8, 3), dtype=np.double)
  293     scaled = rescale(x, 2, order=0, channel_axis=-1, anti_aliasing=False,
  294                      mode='constant')
  295     assert scaled.shape == (16, 16, 3)
  296     # 3D
  297     scaled = rescale(x, 2, order=0, channel_axis=None, anti_aliasing=False,
  298                      mode='constant')
  299     assert scaled.shape == (16, 16, 6)
  300 
  301     # 3D + channels
  302     x = np.zeros((8, 8, 8, 3), dtype=np.double)
  303     scaled = rescale(x, 2, order=0, channel_axis=-1, anti_aliasing=False,
  304                      mode='constant')
  305     assert scaled.shape == (16, 16, 16, 3)
  306     # 4D
  307     scaled = rescale(x, 2, order=0, channel_axis=None, anti_aliasing=False,
  308                      mode='constant')
  309     assert scaled.shape == (16, 16, 16, 6)
  310 
  311 
  312 def test_rescale_multichannel_deprecated_multiscale():
  313     x = np.zeros((5, 5, 3), dtype=np.double)
  314     with expected_warnings(["`multichannel` is a deprecated argument"]):
  315         scaled = rescale(x, (2, 1), order=0, multichannel=True,
  316                          anti_aliasing=False, mode='constant')
  317     assert scaled.shape == (10, 5, 3)
  318 
  319     # repeat prior test, but check for positional multichannel _warnings
  320     with expected_warnings(["Providing the `multichannel` argument"]):
  321         scaled = rescale(x, (2, 1), 0, 'constant', 0, True, False, True,
  322                          anti_aliasing=False)
  323     assert scaled.shape == (10, 5, 3)
  324 
  325 
  326 @pytest.mark.parametrize('channel_axis', [0, 1, 2, -1])
  327 def test_rescale_channel_axis_multiscale(channel_axis):
  328     x = np.zeros((5, 5, 3), dtype=np.double)
  329     x = np.moveaxis(x, -1, channel_axis)
  330     scaled = rescale(x, scale=(2, 1), order=0, channel_axis=channel_axis,
  331                      anti_aliasing=False, mode='constant')
  332     scaled = np.moveaxis(scaled, channel_axis, -1)
  333     assert scaled.shape == (10, 5, 3)
  334 
  335 
  336 def test_rescale_multichannel_defaults():
  337     x = np.zeros((8, 3), dtype=np.double)
  338     scaled = rescale(x, 2, order=0, anti_aliasing=False, mode='constant')
  339     assert scaled.shape == (16, 6)
  340 
  341     x = np.zeros((8, 8, 3), dtype=np.double)
  342     scaled = rescale(x, 2, order=0, anti_aliasing=False, mode='constant')
  343     assert scaled.shape == (16, 16, 6)
  344 
  345 
  346 def test_resize2d():
  347     x = np.zeros((5, 5), dtype=np.double)
  348     x[1, 1] = 1
  349     resized = resize(x, (10, 10), order=0, anti_aliasing=False,
  350                      mode='constant')
  351     ref = np.zeros((10, 10))
  352     ref[2:4, 2:4] = 1
  353     assert_array_almost_equal(resized, ref)
  354 
  355 
  356 def test_resize3d_keep():
  357     # keep 3rd dimension
  358     x = np.zeros((5, 5, 3), dtype=np.double)
  359     x[1, 1, :] = 1
  360     resized = resize(x, (10, 10), order=0, anti_aliasing=False,
  361                      mode='constant')
  362     with pytest.raises(ValueError):
  363         # output_shape too short
  364         resize(x, (10, ), order=0, anti_aliasing=False, mode='constant')
  365     ref = np.zeros((10, 10, 3))
  366     ref[2:4, 2:4, :] = 1
  367     assert_array_almost_equal(resized, ref)
  368     resized = resize(x, (10, 10, 3), order=0, anti_aliasing=False,
  369                      mode='constant')
  370     assert_array_almost_equal(resized, ref)
  371 
  372 
  373 def test_resize3d_resize():
  374     # resize 3rd dimension
  375     x = np.zeros((5, 5, 3), dtype=np.double)
  376     x[1, 1, :] = 1
  377     resized = resize(x, (10, 10, 1), order=0, anti_aliasing=False,
  378                      mode='constant')
  379     ref = np.zeros((10, 10, 1))
  380     ref[2:4, 2:4] = 1
  381     assert_array_almost_equal(resized, ref)
  382 
  383 
  384 def test_resize3d_2din_3dout():
  385     # 3D output with 2D input
  386     x = np.zeros((5, 5), dtype=np.double)
  387     x[1, 1] = 1
  388     resized = resize(x, (10, 10, 1), order=0, anti_aliasing=False,
  389                      mode='constant')
  390     ref = np.zeros((10, 10, 1))
  391     ref[2:4, 2:4] = 1
  392     assert_array_almost_equal(resized, ref)
  393 
  394 
  395 def test_resize2d_4d():
  396     # resize with extra output dimensions
  397     x = np.zeros((5, 5), dtype=np.double)
  398     x[1, 1] = 1
  399     out_shape = (10, 10, 1, 1)
  400     resized = resize(x, out_shape, order=0, anti_aliasing=False,
  401                      mode='constant')
  402     ref = np.zeros(out_shape)
  403     ref[2:4, 2:4, ...] = 1
  404     assert_array_almost_equal(resized, ref)
  405 
  406 
  407 def test_resize_nd():
  408     for dim in range(1, 6):
  409         shape = 2 + np.arange(dim) * 2
  410         x = np.ones(shape)
  411         out_shape = np.asarray(shape) * 1.5
  412         resized = resize(x, out_shape, order=0, mode='reflect',
  413                          anti_aliasing=False)
  414         expected_shape = 1.5 * shape
  415         assert_array_equal(resized.shape, expected_shape)
  416         assert np.all(resized == 1)
  417 
  418 
  419 def test_resize3d_bilinear():
  420     # bilinear 3rd dimension
  421     x = np.zeros((5, 5, 2), dtype=np.double)
  422     x[1, 1, 0] = 0
  423     x[1, 1, 1] = 1
  424     resized = resize(x, (10, 10, 1), order=1, mode='constant',
  425                      anti_aliasing=False)
  426     ref = np.zeros((10, 10, 1))
  427     ref[1:5, 1:5, :] = 0.03125
  428     ref[1:5, 2:4, :] = 0.09375
  429     ref[2:4, 1:5, :] = 0.09375
  430     ref[2:4, 2:4, :] = 0.28125
  431     assert_array_almost_equal(resized, ref)
  432 
  433 
  434 def test_resize_dtype():
  435     x = np.zeros((5, 5))
  436     x_f32 = x.astype(np.float32)
  437     x_u8 = x.astype(np.uint8)
  438     x_b = x.astype(bool)
  439 
  440     assert resize(x, (10, 10), preserve_range=False).dtype == x.dtype
  441     assert resize(x, (10, 10), preserve_range=True).dtype == x.dtype
  442     assert resize(x_u8, (10, 10), preserve_range=False).dtype == np.double
  443     assert resize(x_u8, (10, 10), preserve_range=True).dtype == np.double
  444     assert resize(x_b, (10, 10), preserve_range=False).dtype == bool
  445     assert resize(x_b, (10, 10), preserve_range=True).dtype == bool
  446     assert resize(x_f32, (10, 10), preserve_range=False).dtype == x_f32.dtype
  447     assert resize(x_f32, (10, 10), preserve_range=True).dtype == x_f32.dtype
  448 
  449 
  450 @pytest.mark.parametrize('order', [0, 1])
  451 @pytest.mark.parametrize('preserve_range', [True, False])
  452 @pytest.mark.parametrize('anti_aliasing', [True, False])
  453 @pytest.mark.parametrize('dtype', [np.float64, np.uint8])
  454 def test_resize_clip(order, preserve_range, anti_aliasing, dtype):
  455     # test if clip as expected
  456     if dtype == np.uint8 and (preserve_range or order == 0):
  457         expected_max = 255
  458     else:
  459         expected_max = 1.0
  460     x = np.ones((5, 5), dtype=dtype)
  461     if dtype == np.uint8:
  462         x *= 255
  463     resized = resize(x, (3, 3), order=order, preserve_range=preserve_range,
  464                      anti_aliasing=anti_aliasing)
  465 
  466     assert resized.max() == expected_max
  467 
  468 
  469 @pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
  470 def test_swirl(dtype):
  471     image = img_as_float(checkerboard()).astype(dtype, copy=False)
  472     float_dtype = _supported_float_type(dtype)
  473 
  474     swirl_params = {'radius': 80, 'rotation': 0, 'order': 2, 'mode': 'reflect'}
  475 
  476     with expected_warnings(['Bi-quadratic.*bug']):
  477         swirled = swirl(image, strength=10, **swirl_params)
  478         unswirled = swirl(swirled, strength=-10, **swirl_params)
  479         assert swirled.dtype == unswirled.dtype == float_dtype
  480 
  481     assert np.mean(np.abs(image - unswirled)) < 0.01
  482 
  483     swirl_params.pop('mode')
  484 
  485     with expected_warnings(['Bi-quadratic.*bug']):
  486         swirled = swirl(image, strength=10, **swirl_params)
  487         unswirled = swirl(swirled, strength=-10, **swirl_params)
  488         assert swirled.dtype == unswirled.dtype == float_dtype
  489 
  490     assert np.mean(np.abs(image[1:-1, 1:-1] - unswirled[1:-1, 1:-1])) < 0.01
  491 
  492 
  493 def test_const_cval_out_of_range():
  494     img = np.random.randn(100, 100)
  495     cval = - 10
  496     warped = warp(img, AffineTransform(translation=(10, 10)), cval=cval)
  497     assert np.sum(warped == cval) == (2 * 100 * 10 - 10 * 10)
  498 
  499 
  500 def test_warp_identity():
  501     img = img_as_float(rgb2gray(astronaut()))
  502     assert len(img.shape) == 2
  503     assert np.allclose(img, warp(img, AffineTransform(rotation=0)))
  504     assert not np.allclose(img, warp(img, AffineTransform(rotation=0.1)))
  505     rgb_img = np.transpose(np.asarray([img, np.zeros_like(img), img]),
  506                            (1, 2, 0))
  507     warped_rgb_img = warp(rgb_img, AffineTransform(rotation=0.1))
  508     assert np.allclose(rgb_img, warp(rgb_img, AffineTransform(rotation=0)))
  509     assert not np.allclose(rgb_img, warped_rgb_img)
  510     # assert no cross-talk between bands
  511     assert np.all(0 == warped_rgb_img[:, :, 1])
  512 
  513 
  514 def test_warp_coords_example():
  515     image = astronaut().astype(np.float32)
  516     assert 3 == image.shape[2]
  517     tform = SimilarityTransform(translation=(0, -10))
  518     coords = warp_coords(tform, (30, 30, 3))
  519     map_coordinates(image[:, :, 0], coords[:2])
  520 
  521 
  522 @pytest.mark.parametrize(
  523     'dtype', [np.uint8, np.int32, np.float16, np.float32, np.float64]
  524 )
  525 def test_downsize(dtype):
  526     x = np.zeros((10, 10), dtype=dtype)
  527     x[2:4, 2:4] = 1
  528     scaled = resize(x, (5, 5), order=0, anti_aliasing=False, mode='constant')
  529     expected_dtype = np.float32 if dtype == np.float16 else dtype
  530     assert scaled.dtype == expected_dtype
  531     assert scaled.shape == (5, 5)
  532     assert scaled[1, 1] == 1
  533     assert scaled[2:, :].sum() == 0
  534     assert scaled[:, 2:].sum() == 0
  535 
  536 
  537 def test_downsize_anti_aliasing():
  538     x = np.zeros((10, 10), dtype=np.double)
  539     x[2, 2] = 1
  540     scaled = resize(x, (5, 5), order=1, anti_aliasing=True, mode='constant')
  541     assert scaled.shape == (5, 5)
  542     assert np.all(scaled[:3, :3] > 0)
  543     assert scaled[3:, :].sum() == 0
  544     assert scaled[:, 3:].sum() == 0
  545 
  546     sigma = 0.125
  547     out_size = (5, 5)
  548     resize(x, out_size, order=1, mode='constant',
  549            anti_aliasing=True, anti_aliasing_sigma=sigma)
  550     resize(x, out_size, order=1, mode='edge',
  551            anti_aliasing=True, anti_aliasing_sigma=sigma)
  552     resize(x, out_size, order=1, mode='symmetric',
  553            anti_aliasing=True, anti_aliasing_sigma=sigma)
  554     resize(x, out_size, order=1, mode='reflect',
  555            anti_aliasing=True, anti_aliasing_sigma=sigma)
  556     resize(x, out_size, order=1, mode='wrap',
  557            anti_aliasing=True, anti_aliasing_sigma=sigma)
  558 
  559     with pytest.raises(ValueError):  # Unknown mode, or cannot translate mode
  560         resize(x, out_size, order=1, mode='non-existent',
  561                anti_aliasing=True, anti_aliasing_sigma=sigma)
  562 
  563 
  564 def test_downsize_anti_aliasing_invalid_stddev():
  565     x = np.zeros((10, 10), dtype=np.double)
  566     with pytest.raises(ValueError):
  567         resize(x, (5, 5), order=0, anti_aliasing=True, anti_aliasing_sigma=-1,
  568                mode='constant')
  569     with expected_warnings(["Anti-aliasing standard deviation greater"]):
  570         resize(x, (5, 15), order=0, anti_aliasing=True,
  571                anti_aliasing_sigma=(1, 1), mode="reflect")
  572         resize(x, (5, 15), order=0, anti_aliasing=True,
  573                anti_aliasing_sigma=(0, 1), mode="reflect")
  574 
  575 
  576 @pytest.mark.parametrize(
  577     'dtype', [np.uint8, np.int32, np.float16, np.float32, np.float64]
  578 )
  579 def test_downscale(dtype):
  580     x = np.zeros((10, 10), dtype=dtype)
  581     x[2:4, 2:4] = 1
  582     scaled = rescale(x, 0.5, order=0, anti_aliasing=False,
  583                      channel_axis=None, mode='constant')
  584     expected_dtype = np.float32 if dtype == np.float16 else dtype
  585     assert scaled.dtype == expected_dtype
  586     assert scaled.shape == (5, 5)
  587     assert scaled[1, 1] == 1
  588     assert scaled[2:, :].sum() == 0
  589     assert scaled[:, 2:].sum() == 0
  590 
  591 
  592 def test_downscale_anti_aliasing():
  593     x = np.zeros((10, 10), dtype=np.double)
  594     x[2, 2] = 1
  595     scaled = rescale(x, 0.5, order=1, anti_aliasing=True,
  596                      channel_axis=None, mode='constant')
  597     assert scaled.shape == (5, 5)
  598     assert np.all(scaled[:3, :3] > 0)
  599     assert scaled[3:, :].sum() == 0
  600     assert scaled[:, 3:].sum() == 0
  601 
  602 
  603 def test_downscale_to_the_limit():
  604     img = np.random.rand(3, 4)
  605     out = rescale(img, 1e-3)
  606 
  607     assert out.size == 1
  608 
  609 
  610 @pytest.mark.parametrize(
  611     'dtype', [np.uint8, np.int32, np.float16, np.float32, np.float64]
  612 )
  613 def test_downscale_local_mean(dtype):
  614     image1 = np.arange(4 * 6, dtype=dtype).reshape(4, 6)
  615     out1 = downscale_local_mean(image1, (2, 3))
  616     float_dtype = dtype if np.dtype(dtype).kind == 'f' else np.float64
  617     assert out1.dtype == float_dtype
  618 
  619     expected1 = np.array([[4., 7.],
  620                           [16., 19.]])
  621     assert_array_equal(expected1, out1)
  622 
  623     image2 = np.arange(5 * 8, dtype=dtype).reshape(5, 8)
  624     out2 = downscale_local_mean(image2, (4, 5))
  625     assert out2.dtype == float_dtype
  626     expected2 = np.array([[14., 10.8],
  627                           [8.5, 5.7]])
  628     rtol = 1e-3 if dtype == np.float16 else 1e-7
  629     assert_allclose(expected2, out2, rtol=rtol)
  630 
  631 
  632 def test_invalid():
  633     with pytest.raises(ValueError):
  634         warp(np.ones((4, 3, 3, 3)),
  635              SimilarityTransform())
  636 
  637 
  638 def test_inverse():
  639     tform = SimilarityTransform(scale=0.5, rotation=0.1)
  640     inverse_tform = SimilarityTransform(matrix=np.linalg.inv(tform.params))
  641     image = np.arange(10 * 10).reshape(10, 10).astype(np.double)
  642     assert_array_equal(warp(image, inverse_tform), warp(image, tform.inverse))
  643 
  644 
  645 def test_slow_warp_nonint_oshape():
  646     image = np.random.rand(5, 5)
  647 
  648     with pytest.raises(ValueError):
  649         warp(image, lambda xy: xy,
  650              output_shape=(13.1, 19.5))
  651 
  652     warp(image, lambda xy: xy, output_shape=(13.0001, 19.9999))
  653 
  654 
  655 def test_keep_range():
  656     image = np.linspace(0, 2, 25).reshape(5, 5)
  657     out = rescale(image, 2, preserve_range=False, clip=True, order=0,
  658                   mode='constant', channel_axis=None, anti_aliasing=False)
  659     assert out.min() == 0
  660     assert out.max() == 2
  661 
  662     out = rescale(image, 2, preserve_range=True, clip=True, order=0,
  663                   mode='constant', channel_axis=None, anti_aliasing=False)
  664     assert out.min() == 0
  665     assert out.max() == 2
  666 
  667     out = rescale(image.astype(np.uint8), 2, preserve_range=False,
  668                   mode='constant', channel_axis=None, anti_aliasing=False,
  669                   clip=True, order=0)
  670     assert out.min() == 0
  671     assert out.max() == 2
  672 
  673 
  674 def test_zero_image_size():
  675     with pytest.raises(ValueError):
  676         warp(np.zeros(0),
  677              SimilarityTransform())
  678     with pytest.raises(ValueError):
  679         warp(np.zeros((0, 10)),
  680              SimilarityTransform())
  681     with pytest.raises(ValueError):
  682         warp(np.zeros((10, 0)),
  683              SimilarityTransform())
  684     with pytest.raises(ValueError):
  685         warp(np.zeros((10, 10, 0)),
  686              SimilarityTransform())
  687 
  688 
  689 def test_linear_polar_mapping():
  690     output_coords = np.array([[0, 0],
  691                              [0, 90],
  692                              [0, 180],
  693                              [0, 270],
  694                              [99, 0],
  695                              [99, 180],
  696                              [99, 270],
  697                              [99, 45]])
  698     ground_truth = np.array([[100, 100],
  699                              [100, 100],
  700                              [100, 100],
  701                              [100, 100],
  702                              [199, 100],
  703                              [1, 100],
  704                              [100, 1],
  705                              [170.00357134, 170.00357134]])
  706     k_angle = 360 / (2 * np.pi)
  707     k_radius = 1
  708     center = (100, 100)
  709     coords = _linear_polar_mapping(output_coords, k_angle, k_radius, center)
  710     assert np.allclose(coords, ground_truth)
  711 
  712 
  713 def test_log_polar_mapping():
  714     output_coords = np.array([[0, 0],
  715                               [0, 90],
  716                               [0, 180],
  717                               [0, 270],
  718                               [99, 0],
  719                               [99, 180],
  720                               [99, 270],
  721                               [99, 45]])
  722     ground_truth = np.array([[101, 100],
  723                              [100, 101],
  724                              [99, 100],
  725                              [100, 99],
  726                              [195.4992586, 100],
  727                              [4.5007414, 100],
  728                              [100, 4.5007414],
  729                              [167.52817336, 167.52817336]])
  730     k_angle = 360 / (2 * np.pi)
  731     k_radius = 100 / np.log(100)
  732     center = (100, 100)
  733     coords = _log_polar_mapping(output_coords, k_angle, k_radius, center)
  734     assert np.allclose(coords, ground_truth)
  735 
  736 
  737 @pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
  738 def test_linear_warp_polar(dtype):
  739     radii = [5, 10, 15, 20]
  740     image = np.zeros([51, 51])
  741     for rad in radii:
  742         rr, cc, val = circle_perimeter_aa(25, 25, rad)
  743         image[rr, cc] = val
  744     image = image.astype(dtype, copy=False)
  745     warped = warp_polar(image, radius=25)
  746     assert warped.dtype == _supported_float_type(dtype)
  747     profile = warped.mean(axis=0)
  748     peaks = peak_local_max(profile)
  749     assert np.alltrue([peak in radii for peak in peaks])
  750 
  751 
  752 @pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
  753 def test_log_warp_polar(dtype):
  754     radii = [np.exp(2), np.exp(3), np.exp(4), np.exp(5),
  755              np.exp(5)-1, np.exp(5)+1]
  756     radii = [int(x) for x in radii]
  757     image = np.zeros([301, 301])
  758     for rad in radii:
  759         rr, cc, val = circle_perimeter_aa(150, 150, rad)
  760         image[rr, cc] = val
  761     image = image.astype(dtype, copy=False)
  762     warped = warp_polar(image, radius=200, scaling='log')
  763     assert warped.dtype == _supported_float_type(dtype)
  764     profile = warped.mean(axis=0)
  765     peaks_coord = peak_local_max(profile)
  766     peaks_coord.sort(axis=0)
  767     gaps = peaks_coord[1:] - peaks_coord[:-1]
  768     assert np.alltrue([x >= 38 and x <= 40 for x in gaps])
  769 
  770 
  771 def test_invalid_scaling_polar():
  772     with pytest.raises(ValueError):
  773         warp_polar(np.zeros((10, 10)), (5, 5), scaling='invalid')
  774     with pytest.raises(ValueError):
  775         warp_polar(np.zeros((10, 10)), (5, 5), scaling=None)
  776 
  777 
  778 def test_invalid_dimensions_polar():
  779     with pytest.raises(ValueError):
  780         warp_polar(np.zeros((10, 10, 3)), (5, 5))
  781     with pytest.raises(ValueError):
  782         warp_polar(np.zeros((10, 10)), (5, 5), channel_axis=-1)
  783     with pytest.raises(ValueError):
  784         warp_polar(np.zeros((10, 10, 10, 3)), (5, 5), channel_axis=-1)
  785 
  786 
  787 def test_bool_img_rescale():
  788     img = np.ones((12, 18), dtype=bool)
  789     img[2:-2, 4:-4] = False
  790     res = rescale(img, 0.5)
  791 
  792     expected = np.ones((6, 9))
  793     expected[1:-1, 2:-2] = False
  794 
  795     assert_array_equal(res, expected)
  796 
  797 
  798 def test_bool_img_resize():
  799     img = np.ones((12, 18), dtype=bool)
  800     img[2:-2, 4:-4] = False
  801     res = resize(img, (6, 9))
  802 
  803     expected = np.ones((6, 9))
  804     expected[1:-1, 2:-2] = False
  805 
  806     assert_array_equal(res, expected)
  807 
  808 
  809 def test_bool_and_anti_aliasing_errors():
  810     img = np.zeros((10, 10), dtype=bool)
  811 
  812     with pytest.raises(ValueError):
  813         rescale(img, 0.5, anti_aliasing=True)
  814 
  815     with pytest.raises(ValueError):
  816         resize(img, (5, 5), anti_aliasing=True)
  817 
  818 
  819 @pytest.mark.parametrize("order", [1, 2, 3, 4, 5])
  820 def test_bool_nonzero_order_errors(order):
  821     img = np.zeros((10, 10), dtype=bool)
  822 
  823     with pytest.raises(ValueError):
  824         rescale(img, 0.5, order=order)
  825 
  826     with pytest.raises(ValueError):
  827         resize(img, (5, 5), order=order)
  828 
  829     with pytest.raises(ValueError):
  830         warp(img, np.eye(3), order=order)
  831 
  832 
  833 @pytest.mark.parametrize('dtype', [np.uint8, bool, np.float32, np.float64])
  834 def test_order_0_warp_dtype(dtype):
  835 
  836     img = _convert(astronaut()[:10, :10, 0], dtype)
  837 
  838     assert resize(img, (12, 12), order=0).dtype == dtype
  839     assert rescale(img, 0.5, order=0).dtype == dtype
  840     assert rotate(img, 45, order=0).dtype == dtype
  841     assert warp_polar(img, order=0).dtype == dtype
  842     assert swirl(img, order=0).dtype == dtype
  843 
  844 
  845 @pytest.mark.parametrize(
  846     'dtype',
  847     [np.uint8, np.float16, np.float32, np.float64]
  848 )
  849 @pytest.mark.parametrize('order', [1, 3, 5])
  850 def test_nonzero_order_warp_dtype(dtype, order):
  851 
  852     img = _convert(astronaut()[:10, :10, 0], dtype)
  853 
  854     float_dtype = _supported_float_type(dtype)
  855 
  856     assert resize(img, (12, 12), order=order).dtype == float_dtype
  857     assert rescale(img, 0.5, order=order).dtype == float_dtype
  858     assert rotate(img, 45, order=order).dtype == float_dtype
  859     assert warp_polar(img, order=order).dtype == float_dtype
  860     assert swirl(img, order=order).dtype == float_dtype
  861 
  862 
  863 def test_resize_local_mean2d():
  864     x = np.zeros((5, 5), dtype=np.double)
  865     x[1, 1] = 1
  866     resized = resize_local_mean(x, (10, 10))
  867     ref = np.zeros((10, 10))
  868     ref[2:4, 2:4] = 1
  869     assert_array_almost_equal(resized, ref)
  870 
  871 
  872 @pytest.mark.parametrize('channel_axis', [0, 1, 2, -1, -2, -3])
  873 def test_resize_local_mean3d_keep(channel_axis):
  874     # keep 3rd dimension
  875     nch = 3
  876     x = np.zeros((5, 5, nch), dtype=np.double)
  877     x[1, 1, :] = 1
  878     # move channels to expected dimension
  879     x = np.moveaxis(x, -1, channel_axis)
  880     resized = resize_local_mean(x, (10, 10), channel_axis=channel_axis)
  881     # move channels back to last axis to match the reference image
  882     resized = np.moveaxis(resized, channel_axis, -1)
  883     with pytest.raises(ValueError):
  884         # output_shape too short
  885         resize_local_mean(x, (10, ))
  886     ref = np.zeros((10, 10, nch))
  887     ref[2:4, 2:4, :] = 1
  888     assert_array_almost_equal(resized, ref)
  889 
  890     channel_axis = channel_axis % x.ndim
  891     spatial_shape = (10, 10)
  892     out_shape = (
  893         spatial_shape[:channel_axis] + (nch,) + spatial_shape[channel_axis:]
  894     )
  895     resized = resize_local_mean(x, out_shape)
  896     # move channels back to last axis to match the reference image
  897     resized = np.moveaxis(resized, channel_axis, -1)
  898     assert_array_almost_equal(resized, ref)
  899 
  900 
  901 def test_resize_local_mean3d_resize():
  902     # resize 3rd dimension
  903     x = np.zeros((5, 5, 3), dtype=np.double)
  904     x[1, 1, :] = 1
  905     resized = resize_local_mean(x, (10, 10, 1))
  906     ref = np.zeros((10, 10, 1))
  907     ref[2:4, 2:4] = 1
  908     assert_array_almost_equal(resized, ref)
  909 
  910     # can't resize along specified channel axis
  911     with pytest.raises(ValueError):
  912         resize_local_mean(x, (10, 10, 1), channel_axis=-1)
  913 
  914 
  915 def test_resize_local_mean3d_2din_3dout():
  916     # 3D output with 2D input
  917     x = np.zeros((5, 5), dtype=np.double)
  918     x[1, 1] = 1
  919     resized = resize_local_mean(x, (10, 10, 1))
  920     ref = np.zeros((10, 10, 1))
  921     ref[2:4, 2:4] = 1
  922     assert_array_almost_equal(resized, ref)
  923 
  924 
  925 def test_resize_local_mean2d_4d():
  926     # resize with extra output dimensions
  927     x = np.zeros((5, 5), dtype=np.double)
  928     x[1, 1] = 1
  929     out_shape = (10, 10, 1, 1)
  930     resized = resize_local_mean(x, out_shape)
  931     ref = np.zeros(out_shape)
  932     ref[2:4, 2:4, ...] = 1
  933     assert_array_almost_equal(resized, ref)
  934 
  935 
  936 @pytest.mark.parametrize("dim", range(1, 6))
  937 def test_resize_local_mean_nd(dim):
  938     shape = 2 + np.arange(dim) * 2
  939     x = np.ones(shape)
  940     out_shape = (np.asarray(shape) * 1.5).astype(int)
  941     resized = resize_local_mean(x, out_shape)
  942     expected_shape = 1.5 * shape
  943     assert_array_equal(resized.shape, expected_shape)
  944     assert_array_equal(resized, 1)
  945 
  946 
  947 def test_resize_local_mean3d():
  948     x = np.zeros((5, 5, 2), dtype=np.double)
  949     x[1, 1, 0] = 0
  950     x[1, 1, 1] = 1
  951     resized = resize_local_mean(x, (10, 10, 1))
  952     ref = np.zeros((10, 10, 1))
  953     ref[2:4, 2:4, :] = 0.5
  954     assert_array_almost_equal(resized, ref)
  955     resized = resize_local_mean(x, (10, 10, 1), grid_mode=False)
  956     ref[1, 1, :] = 0.0703125
  957     ref[2, 2, :] = 0.5
  958     ref[3, 3, :] = 0.3828125
  959     ref[1, 2, :] = ref[2, 1, :] = 0.1875
  960     ref[1, 3, :] = ref[3, 1, :] = 0.1640625
  961     ref[2, 3, :] = ref[3, 2, :] = 0.4375
  962     assert_array_almost_equal(resized, ref)
  963 
  964 
  965 def test_resize_local_mean_dtype():
  966     x = np.zeros((5, 5))
  967     x_f32 = x.astype(np.float32)
  968     x_u8 = x.astype(np.uint8)
  969     x_b = x.astype(bool)
  970 
  971     assert resize_local_mean(x, (10, 10),
  972                              preserve_range=False).dtype == x.dtype
  973     assert resize_local_mean(x, (10, 10),
  974                              preserve_range=True).dtype == x.dtype
  975     assert resize_local_mean(x_u8, (10, 10),
  976                              preserve_range=False).dtype == np.double
  977     assert resize_local_mean(x_u8, (10, 10),
  978                              preserve_range=True).dtype == np.double
  979     assert resize_local_mean(x_b, (10, 10),
  980                              preserve_range=False).dtype == np.double
  981     assert resize_local_mean(x_b, (10, 10),
  982                              preserve_range=True).dtype == np.double
  983     assert resize_local_mean(x_f32, (10, 10),
  984                              preserve_range=False).dtype == x_f32.dtype
  985     assert resize_local_mean(x_f32, (10, 10),
  986                              preserve_range=True).dtype == x_f32.dtype
  987 
  988 
  989 @pytest.mark.parametrize("_type", [tuple, np.asarray, list])
  990 def test_output_shape_arg_type(_type):
  991     img = np.random.rand(3, 3)
  992     output_shape = _type([5, 5])
  993 
  994     assert resize(img, output_shape).shape == tuple(output_shape)