def test_rescale(): # same scale factor x = np.zeros((5, 5), dtype=np.double) x[1, 1] = 1 scaled = rescale(x, 2, order=0, channel_axis=None, anti_aliasing=False, mode='constant') ref = np.zeros((10, 10)) ref[2:4, 2:4] = 1 assert_array_almost_equal(scaled, ref) # different scale factors x = np.zeros((5, 5), dtype=np.double) x[1, 1] = 1 scaled = rescale(x, (2, 1), order=0, channel_axis=None, anti_aliasing=False, mode='constant') ref = np.zeros((10, 5)) ref[2:4, 1] = 1 assert_array_almost_equal(scaled, ref)
def test_keep_range(): image = np.linspace(0, 2, 25).reshape(5, 5) out = rescale(image, 2, preserve_range=False, clip=True, order=0, mode='constant', channel_axis=None, anti_aliasing=False) assert out.min() == 0 assert out.max() == 2 out = rescale(image, 2, preserve_range=True, clip=True, order=0, mode='constant', channel_axis=None, anti_aliasing=False) assert out.min() == 0 assert out.max() == 2 out = rescale(image.astype(np.uint8), 2, preserve_range=False, mode='constant', channel_axis=None, anti_aliasing=False, clip=True, order=0) assert out.min() == 0 assert out.max() == 2
def test_bool_and_anti_aliasing_errors(): img = np.zeros((10, 10), dtype=bool) with pytest.raises(ValueError): rescale(img, 0.5, anti_aliasing=True) with pytest.raises(ValueError): resize(img, (5, 5), anti_aliasing=True)
def test_rescale_invalid_scale(): x = np.zeros((10, 10, 3)) with testing.raises(ValueError): rescale(x, (2, 2), multichannel=False, anti_aliasing=False, mode='constant') with testing.raises(ValueError): rescale(x, (2, 2, 2), multichannel=True, anti_aliasing=False, mode='constant')
def test_rescale_invalid_scale(): x = np.zeros((10, 10, 3)) with pytest.raises(ValueError): rescale(x, (2, 2), channel_axis=None, anti_aliasing=False, mode='constant') with pytest.raises(ValueError): rescale(x, (2, 2, 2), channel_axis=-1, anti_aliasing=False, mode='constant')
def test_rescale_multichannel_defaults(): x = np.zeros((8, 3), dtype=np.double) scaled = rescale(x, 2, order=0, anti_aliasing=False, mode='constant') assert scaled.shape == (16, 6) x = np.zeros((8, 8, 3), dtype=np.double) scaled = rescale(x, 2, order=0, anti_aliasing=False, mode='constant') assert scaled.shape == (16, 16, 6)
def test_bool_nonzero_order_errors(order): img = np.zeros((10, 10), dtype=bool) with pytest.raises(ValueError): rescale(img, 0.5, order=order) with pytest.raises(ValueError): resize(img, (5, 5), order=order) with pytest.raises(ValueError): warp(img, np.eye(3), order=order)
def test_rescale_multichannel_deprecated_multiscale(): x = np.zeros((5, 5, 3), dtype=np.float64) with expected_warnings(["`multichannel` is a deprecated argument"]): scaled = rescale(x, (2, 1), order=0, multichannel=True, anti_aliasing=False, mode='constant') assert scaled.shape == (10, 5, 3) # repeat prior test, but check for positional multichannel _warnings with expected_warnings(["Providing the `multichannel` argument"]): scaled = rescale(x, (2, 1), 0, 'constant', 0, True, False, True, anti_aliasing=False) assert scaled.shape == (10, 5, 3)
def test_warp_clip(): x = np.zeros((5, 5), dtype=np.float64) x[2, 2] = 1 outx = rescale(x, 3, order=3, clip=False, anti_aliasing=False, mode='constant') assert outx.min() < 0 outx = rescale(x, 3, order=3, clip=True, anti_aliasing=False, mode='constant') assert_array_almost_equal(outx.min(), 0) assert_array_almost_equal(outx.max(), 1)
def test_rescale_channel_axis_multiscale(channel_axis): x = np.zeros((5, 5, 3), dtype=np.float64) x = np.moveaxis(x, -1, channel_axis) scaled = rescale(x, scale=(2, 1), order=0, channel_axis=channel_axis, anti_aliasing=False, mode='constant') scaled = np.moveaxis(scaled, channel_axis, -1) assert scaled.shape == (10, 5, 3)
def test_rescale_multichannel_multiscale(): x = np.zeros((5, 5, 3), dtype=np.double) scaled = rescale(x, (2, 1), order=0, multichannel=True, anti_aliasing=False, mode='constant') assert_equal(scaled.shape, (10, 5, 3))
def test_bool_array_warnings(): img = np.zeros((10, 10), dtype=bool) with expected_warnings(['Input image dtype is bool']): rescale(img, 0.5, anti_aliasing=True) with expected_warnings(['Input image dtype is bool']): resize(img, (5, 5), anti_aliasing=True) with expected_warnings(['Input image dtype is bool']): rescale(img, 0.5, order=1) with expected_warnings(['Input image dtype is bool']): resize(img, (5, 5), order=1) with expected_warnings(['Input image dtype is bool']): warp(img, np.eye(3), order=1)
def test_order_0_warp_dtype(dtype): img = _convert(astronaut()[:10, :10, 0], dtype) assert resize(img, (12, 12), order=0).dtype == dtype assert rescale(img, 0.5, order=0).dtype == dtype assert rotate(img, 45, order=0).dtype == dtype assert warp_polar(img, order=0).dtype == dtype assert swirl(img, order=0).dtype == dtype
def test_bool_img_rescale(): img = np.ones((12, 18), dtype=bool) img[2:-2, 4:-4] = False res = rescale(img, 0.5) expected = np.ones((6, 9)) expected[1:-1, 2:-2] = False assert_array_equal(res, expected)
def test_downscale_anti_aliasing(): x = np.zeros((10, 10), dtype=np.double) x[2, 2] = 1 scaled = rescale(x, 0.5, order=1, anti_aliasing=True, multichannel=False, mode='constant') assert_equal(scaled.shape, (5, 5)) assert np.all(scaled[:3, :3] > 0) assert_equal(scaled[3:, :].sum(), 0) assert_equal(scaled[:, 3:].sum(), 0)
def test_downscale(): x = np.zeros((10, 10), dtype=np.double) x[2:4, 2:4] = 1 scaled = rescale(x, 0.5, order=0, anti_aliasing=False, multichannel=False, mode='constant') assert_equal(scaled.shape, (5, 5)) assert_equal(scaled[1, 1], 1) assert_equal(scaled[2:, :].sum(), 0) assert_equal(scaled[:, 2:].sum(), 0)
def test_downscale_anti_aliasing(): x = np.zeros((10, 10), dtype=np.float64) x[2, 2] = 1 scaled = rescale(x, 0.5, order=1, anti_aliasing=True, channel_axis=None, mode='constant') assert scaled.shape == (5, 5) assert np.all(scaled[:3, :3] > 0) assert scaled[3:, :].sum() == 0 assert scaled[:, 3:].sum() == 0
def _coarsenImage(image, f): ''' seems to be a more precise (but slower) way to down-scale an image ''' from skimage.morphology import square from skimage.filters import rank from skimage.transform._warps import rescale selem = square(f) arri = rank.mean(image, selem=selem) return rescale(arri, 1 / f, order=0)
def test_nonzero_order_warp_dtype(dtype, order): img = _convert(astronaut()[:10, :10, 0], dtype) float_dtype = _supported_float_type(dtype) assert resize(img, (12, 12), order=order).dtype == float_dtype assert rescale(img, 0.5, order=order).dtype == float_dtype assert rotate(img, 45, order=order).dtype == float_dtype assert warp_polar(img, order=order).dtype == float_dtype assert swirl(img, order=order).dtype == float_dtype
def test_downscale(dtype): x = np.zeros((10, 10), dtype=dtype) x[2:4, 2:4] = 1 scaled = rescale(x, 0.5, order=0, anti_aliasing=False, channel_axis=None, mode='constant') expected_dtype = np.float32 if dtype == np.float16 else dtype assert scaled.dtype == expected_dtype assert scaled.shape == (5, 5) assert scaled[1, 1] == 1 assert scaled[2:, :].sum() == 0 assert scaled[:, 2:].sum() == 0
def test_rescale_multichannel(): # 1D + channels x = np.zeros((8, 3), dtype=np.float64) scaled = rescale(x, 2, order=0, channel_axis=-1, anti_aliasing=False, mode='constant') assert scaled.shape == (16, 3) # 2D scaled = rescale(x, 2, order=0, channel_axis=None, anti_aliasing=False, mode='constant') assert scaled.shape == (16, 6) # 2D + channels x = np.zeros((8, 8, 3), dtype=np.float64) scaled = rescale(x, 2, order=0, channel_axis=-1, anti_aliasing=False, mode='constant') assert scaled.shape == (16, 16, 3) # 3D scaled = rescale(x, 2, order=0, channel_axis=None, anti_aliasing=False, mode='constant') assert scaled.shape == (16, 16, 6) # 3D + channels x = np.zeros((8, 8, 8, 3), dtype=np.float64) scaled = rescale(x, 2, order=0, channel_axis=-1, anti_aliasing=False, mode='constant') assert scaled.shape == (16, 16, 16, 3) # 4D scaled = rescale(x, 2, order=0, channel_axis=None, anti_aliasing=False, mode='constant') assert scaled.shape == (16, 16, 16, 6)
def test_rescale_multichannel(): # 1D + channels x = np.zeros((8, 3), dtype=np.double) scaled = rescale(x, 2, order=0, multichannel=True, anti_aliasing=False, mode='constant') assert_equal(scaled.shape, (16, 3)) # 2D scaled = rescale(x, 2, order=0, multichannel=False, anti_aliasing=False, mode='constant') assert_equal(scaled.shape, (16, 6)) # 2D + channels x = np.zeros((8, 8, 3), dtype=np.double) scaled = rescale(x, 2, order=0, multichannel=True, anti_aliasing=False, mode='constant') assert_equal(scaled.shape, (16, 16, 3)) # 3D scaled = rescale(x, 2, order=0, multichannel=False, anti_aliasing=False, mode='constant') assert_equal(scaled.shape, (16, 16, 6)) # 3D + channels x = np.zeros((8, 8, 8, 3), dtype=np.double) scaled = rescale(x, 2, order=0, multichannel=True, anti_aliasing=False, mode='constant') assert_equal(scaled.shape, (16, 16, 16, 3)) # 4D scaled = rescale(x, 2, order=0, multichannel=False, anti_aliasing=False, mode='constant') assert_equal(scaled.shape, (16, 16, 16, 6))
def test_downscale_to_the_limit(): img = np.random.rand(3, 4) out = rescale(img, 1e-3) assert out.size == 1