| 5207 | |
| 5208 | |
| 5209 | def test_blockview(): |
| 5210 | x = da.arange(10, chunks=2) |
| 5211 | blockview = BlockView(x) |
| 5212 | assert x.blocks == blockview |
| 5213 | assert isinstance(blockview[0], da.Array) |
| 5214 | |
| 5215 | assert_eq(blockview[0], x[:2]) |
| 5216 | assert_eq(blockview[-1], x[-2:]) |
| 5217 | assert_eq(blockview[:3], x[:6]) |
| 5218 | assert_eq(blockview[[0, 1, 2]], x[:6]) |
| 5219 | assert_eq(blockview[[3, 0, 2]], np.array([6, 7, 0, 1, 4, 5])) |
| 5220 | assert_eq(blockview.shape, tuple(map(len, x.chunks))) |
| 5221 | assert_eq(blockview.size, math.prod(blockview.shape)) |
| 5222 | assert_eq( |
| 5223 | blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)] |
| 5224 | ) |
| 5225 | |
| 5226 | x = da.random.default_rng().random((20, 20), chunks=(4, 5)) |
| 5227 | blockview = BlockView(x) |
| 5228 | assert_eq(blockview[0], x[:4]) |
| 5229 | assert_eq(blockview[0, :3], x[:4, :15]) |
| 5230 | assert_eq(blockview[:, :3], x[:, :15]) |
| 5231 | assert_eq(blockview.shape, tuple(map(len, x.chunks))) |
| 5232 | assert_eq(blockview.size, math.prod(blockview.shape)) |
| 5233 | assert_eq( |
| 5234 | blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)] |
| 5235 | ) |
| 5236 | |
| 5237 | x = da.ones((40, 40, 40), chunks=(10, 10, 10)) |
| 5238 | blockview = BlockView(x) |
| 5239 | assert_eq(blockview[0, :, 0], np.ones((10, 40, 10))) |
| 5240 | assert_eq(blockview.shape, tuple(map(len, x.chunks))) |
| 5241 | assert_eq(blockview.size, math.prod(blockview.shape)) |
| 5242 | assert_eq( |
| 5243 | blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)] |
| 5244 | ) |
| 5245 | |
| 5246 | x = da.ones((2, 2), chunks=1) |
| 5247 | with pytest.raises(ValueError): |
| 5248 | blockview[[0, 1], [0, 1]] |
| 5249 | with pytest.raises(ValueError): |
| 5250 | blockview[np.array([0, 1]), [0, 1]] |
| 5251 | with pytest.raises(ValueError) as info: |
| 5252 | blockview[np.array([0, 1]), np.array([0, 1])] |
| 5253 | assert "list" in str(info.value) |
| 5254 | with pytest.raises(ValueError) as info: |
| 5255 | blockview[None, :, :] |
| 5256 | assert "newaxis" in str(info.value) and "not supported" in str(info.value) |
| 5257 | with pytest.raises(IndexError) as info: |
| 5258 | blockview[100, 100] |
| 5259 | |
| 5260 | |
| 5261 | def test_blocks_indexer(): |