| 5385 | |
| 5386 | |
| 5387 | def test_blockview(): |
| 5388 | x = da.arange(10, chunks=2) |
| 5389 | blockview = BlockView(x) |
| 5390 | assert x.blocks == blockview |
| 5391 | assert isinstance(blockview[0], da.Array) |
| 5392 | |
| 5393 | assert_eq(blockview[0], x[:2]) |
| 5394 | assert_eq(blockview[-1], x[-2:]) |
| 5395 | assert_eq(blockview[:3], x[:6]) |
| 5396 | assert_eq(blockview[[0, 1, 2]], x[:6]) |
| 5397 | assert_eq(blockview[[3, 0, 2]], np.array([6, 7, 0, 1, 4, 5])) |
| 5398 | assert_eq(blockview.shape, tuple(map(len, x.chunks))) |
| 5399 | assert_eq(blockview.size, math.prod(blockview.shape)) |
| 5400 | assert_eq( |
| 5401 | blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)] |
| 5402 | ) |
| 5403 | |
| 5404 | x = da.random.default_rng().random((20, 20), chunks=(4, 5)) |
| 5405 | blockview = BlockView(x) |
| 5406 | assert_eq(blockview[0], x[:4]) |
| 5407 | assert_eq(blockview[0, :3], x[:4, :15]) |
| 5408 | assert_eq(blockview[:, :3], x[:, :15]) |
| 5409 | assert_eq(blockview.shape, tuple(map(len, x.chunks))) |
| 5410 | assert_eq(blockview.size, math.prod(blockview.shape)) |
| 5411 | assert_eq( |
| 5412 | blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)] |
| 5413 | ) |
| 5414 | |
| 5415 | x = da.ones((40, 40, 40), chunks=(10, 10, 10)) |
| 5416 | blockview = BlockView(x) |
| 5417 | assert_eq(blockview[0, :, 0], np.ones((10, 40, 10))) |
| 5418 | assert_eq(blockview.shape, tuple(map(len, x.chunks))) |
| 5419 | assert_eq(blockview.size, math.prod(blockview.shape)) |
| 5420 | assert_eq( |
| 5421 | blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)] |
| 5422 | ) |
| 5423 | |
| 5424 | x = da.ones((2, 2), chunks=1) |
| 5425 | with pytest.raises(ValueError): |
| 5426 | blockview[[0, 1], [0, 1]] |
| 5427 | with pytest.raises(ValueError): |
| 5428 | blockview[np.array([0, 1]), [0, 1]] |
| 5429 | with pytest.raises(ValueError) as info: |
| 5430 | blockview[np.array([0, 1]), np.array([0, 1])] |
| 5431 | assert "list" in str(info.value) |
| 5432 | with pytest.raises(ValueError) as info: |
| 5433 | blockview[None, :, :] |
| 5434 | assert "newaxis" in str(info.value) and "not supported" in str(info.value) |
| 5435 | with pytest.raises(IndexError) as info: |
| 5436 | blockview[100, 100] |
| 5437 | |
| 5438 | |
| 5439 | def test_blocks_indexer(): |