def test_view_as_blocks_1D_array(): A = cp.arange(10) B = view_as_blocks(A, (5,)) # fmt: off cp.testing.assert_array_equal( B, cp.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) )
def test_view_as_blocks_2D_array(): A = cp.arange(4 * 4).reshape(4, 4) B = view_as_blocks(A, (2, 2)) # fmt: off cp.testing.assert_array_equal( B[0, 1], cp.array([[2, 3], [6, 7]]) ) # fmt: on assert B[1, 0, 1, 1] == 13
def test_view_as_blocks_3D_array(): A = cp.arange(4 * 4 * 6).reshape(4, 4, 6) B = view_as_blocks(A, (1, 2, 2)) assert B.shape == (4, 2, 3, 1, 2, 2) # fmt: off cp.testing.assert_array_equal( B[2:, 0, 2], cp.array([[[[52, 53], [58, 59]]], [[[76, 77], [82, 83]]]]) )
def test_views_non_contiguous(): A = cp.arange(16).reshape((4, 4)) A = A[::2, :] with expected_warnings(["Cannot provide views"]): res_b = view_as_blocks(A, (2, 2)) res_w = view_as_windows(A, (2, 2)) print(res_b) print(res_w) # fmt: off expected_b = [[[[0, 1], [8, 9]], [[2, 3], [10, 11]]]] expected_w = [[[[ 0, 1], [ 8, 9]], [[ 1, 2], [ 9, 10]], [[ 2, 3], [10, 11]]]] # fmt: on cp.testing.assert_array_equal(res_b, expected_b) cp.testing.assert_array_equal(res_w, expected_w)
def test_view_as_blocks_block_not_a_tuple(): A = cp.arange(10) with pytest.raises(TypeError): view_as_blocks(A, [5])
def test_view_as_blocks_1D_array_wrong_block_shape(): A = cp.arange(10) with pytest.raises(ValueError): view_as_blocks(A, (3,))
def test_view_as_blocks_wrong_block_dimension(): A = cp.arange(10) with pytest.raises(ValueError): view_as_blocks(A, (2, 2))
def test_view_as_blocks_block_too_large(): A = cp.arange(10) with pytest.raises(ValueError): view_as_blocks(A, (11,))
def test_view_as_blocks_negative_shape(): A = cp.arange(10) with pytest.raises(ValueError): view_as_blocks(A, (-2,))