def test_downsize(): x = cp.zeros((10, 10), dtype=np.double) x[2:4, 2:4] = 1 scaled = resize(x, (5, 5), order=0, anti_aliasing=False, mode="constant") assert_array_equal(scaled.shape, (5, 5)) assert_array_equal(scaled[1, 1], 1) assert_array_equal(scaled[2:, :].sum(), 0) assert_array_equal(scaled[:, 2:].sum(), 0)
def test_bool_array_warnings(): img = cp.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, cp.eye(3), order=1)
def test_bool_img_resize(): img = cp.ones((12, 18), dtype=bool) img[2:-2, 4:-4] = False res = resize(img, (6, 9)) expected = cp.ones((6, 9)) expected[1:-1, 2:-2] = False assert_array_equal(res, expected)
def test_resize3d_keep(): # keep 3rd dimension x = cp.zeros((5, 5, 3), dtype=np.double) x[1, 1, :] = 1 resized = resize(x, (10, 10), order=0, anti_aliasing=False, mode="constant") with pytest.raises(ValueError): # output_shape too short resize(x, (10, ), order=0, anti_aliasing=False, mode="constant") ref = cp.zeros((10, 10, 3)) ref[2:4, 2:4, :] = 1 assert_array_almost_equal(resized, ref) resized = resize(x, (10, 10, 3), order=0, anti_aliasing=False, mode="constant") assert_array_almost_equal(resized, ref)
def test_resize2d(): x = cp.zeros((5, 5), dtype=np.double) x[1, 1] = 1 resized = resize(x, (10, 10), order=0, anti_aliasing=False, mode="constant") ref = cp.zeros((10, 10)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)
def test_resize3d_2din_3dout(): # 3D output with 2D input x = cp.zeros((5, 5), dtype=np.double) x[1, 1] = 1 resized = resize(x, (10, 10, 1), order=0, anti_aliasing=False, mode="constant") ref = cp.zeros((10, 10, 1)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)
def test_resize3d_resize(): # resize 3rd dimension x = cp.zeros((5, 5, 3), dtype=np.double) x[1, 1, :] = 1 resized = resize(x, (10, 10, 1), order=0, anti_aliasing=False, mode="constant") ref = cp.zeros((10, 10, 1)) ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref)
def test_downsize_anti_aliasing_invalid_stddev(): x = cp.zeros((10, 10), dtype=np.double) with pytest.raises(ValueError): resize( x, (5, 5), order=0, anti_aliasing=True, anti_aliasing_sigma=-1, mode="constant", ) with expected_warnings(["Anti-aliasing standard deviation greater"]): resize( x, (5, 15), order=0, anti_aliasing=True, anti_aliasing_sigma=(1, 1), mode="reflect", ) resize( x, (5, 15), order=0, anti_aliasing=True, anti_aliasing_sigma=(0, 1), mode="reflect", )
def test_resize_dtype(): x = cp.zeros((5, 5)) x_f32 = x.astype(np.float32) x_u8 = x.astype(np.uint8) x_b = x.astype(bool) assert resize(x, (10, 10), preserve_range=False).dtype == x.dtype assert resize(x, (10, 10), preserve_range=True).dtype == x.dtype assert resize(x_u8, (10, 10), preserve_range=False).dtype == np.double assert resize(x_u8, (10, 10), preserve_range=True).dtype == np.double assert resize(x_b, (10, 10), preserve_range=False).dtype == np.double assert resize(x_b, (10, 10), preserve_range=True).dtype == np.double assert resize(x_f32, (10, 10), preserve_range=False).dtype == x_f32.dtype assert resize(x_f32, (10, 10), preserve_range=True).dtype == x_f32.dtype
def test_resize_nd(): for dim in range(1, 6): shape = 2 + np.arange(dim) * 2 x = cp.ones(tuple(shape)) out_shape = np.asarray(shape) * 1.5 resized = resize(x, out_shape, order=0, mode="reflect", anti_aliasing=False) expected_shape = 1.5 * shape assert_array_equal(resized.shape, expected_shape) assert cp.all(resized == 1)
def test_resize2d_4d(): # resize with extra output dimensions x = cp.zeros((5, 5), dtype=np.double) x[1, 1] = 1 out_shape = (10, 10, 1, 1) resized = resize(x, out_shape, order=0, anti_aliasing=False, mode="constant") ref = cp.zeros(out_shape) ref[2:4, 2:4, ...] = 1 assert_array_almost_equal(resized, ref)
def test_resize3d_bilinear(): # bilinear 3rd dimension x = cp.zeros((5, 5, 2), dtype=np.double) x[1, 1, 0] = 0 x[1, 1, 1] = 1 resized = resize(x, (10, 10, 1), order=1, mode="constant", anti_aliasing=False) ref = cp.zeros((10, 10, 1)) ref[1:5, 1:5, :] = 0.03125 ref[1:5, 2:4, :] = 0.09375 ref[2:4, 1:5, :] = 0.09375 ref[2:4, 2:4, :] = 0.28125 assert_array_almost_equal(resized, ref)
def test_downsize_anti_aliasing(): x = cp.zeros((10, 10), dtype=np.double) x[2, 2] = 1 scaled = resize(x, (5, 5), order=1, anti_aliasing=True, mode="constant") assert_array_equal(scaled.shape, (5, 5)) assert cp.all(scaled[:3, :3] > 0) assert_array_equal(scaled[3:, :].sum(), 0) assert_array_equal(scaled[:, 3:].sum(), 0) sigma = 0.125 out_size = (5, 5) resize( x, out_size, order=1, mode="constant", anti_aliasing=True, anti_aliasing_sigma=sigma, ) resize( x, out_size, order=1, mode="edge", anti_aliasing=True, anti_aliasing_sigma=sigma, ) resize( x, out_size, order=1, mode="symmetric", anti_aliasing=True, anti_aliasing_sigma=sigma, ) resize( x, out_size, order=1, mode="reflect", anti_aliasing=True, anti_aliasing_sigma=sigma, ) resize( x, out_size, order=1, mode="wrap", anti_aliasing=True, anti_aliasing_sigma=sigma, ) with pytest.raises(ValueError): # Unknown mode, or cannot translate mode resize( x, out_size, order=1, mode="non-existent", anti_aliasing=True, anti_aliasing_sigma=sigma, )