MCPcopy Create free account
hub / github.com/dask/dask / test_blockview

Function test_blockview

dask/array/tests/test_array_core.py:5366–5415  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5364
5365
5366def test_blockview():
5367 x = da.arange(10, chunks=2)
5368 blockview = BlockView(x)
5369 assert x.blocks == blockview
5370 assert isinstance(blockview[0], da.Array)
5371
5372 assert_eq(blockview[0], x[:2])
5373 assert_eq(blockview[-1], x[-2:])
5374 assert_eq(blockview[:3], x[:6])
5375 assert_eq(blockview[[0, 1, 2]], x[:6])
5376 assert_eq(blockview[[3, 0, 2]], np.array([6, 7, 0, 1, 4, 5]))
5377 assert_eq(blockview.shape, tuple(map(len, x.chunks)))
5378 assert_eq(blockview.size, math.prod(blockview.shape))
5379 assert_eq(
5380 blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)]
5381 )
5382
5383 x = da.random.default_rng().random((20, 20), chunks=(4, 5))
5384 blockview = BlockView(x)
5385 assert_eq(blockview[0], x[:4])
5386 assert_eq(blockview[0, :3], x[:4, :15])
5387 assert_eq(blockview[:, :3], x[:, :15])
5388 assert_eq(blockview.shape, tuple(map(len, x.chunks)))
5389 assert_eq(blockview.size, math.prod(blockview.shape))
5390 assert_eq(
5391 blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)]
5392 )
5393
5394 x = da.ones((40, 40, 40), chunks=(10, 10, 10))
5395 blockview = BlockView(x)
5396 assert_eq(blockview[0, :, 0], np.ones((10, 40, 10)))
5397 assert_eq(blockview.shape, tuple(map(len, x.chunks)))
5398 assert_eq(blockview.size, math.prod(blockview.shape))
5399 assert_eq(
5400 blockview.ravel(), [blockview[idx] for idx in np.ndindex(blockview.shape)]
5401 )
5402
5403 x = da.ones((2, 2), chunks=1)
5404 with pytest.raises(ValueError):
5405 blockview[[0, 1], [0, 1]]
5406 with pytest.raises(ValueError):
5407 blockview[np.array([0, 1]), [0, 1]]
5408 with pytest.raises(ValueError) as info:
5409 blockview[np.array([0, 1]), np.array([0, 1])]
5410 assert "list" in str(info.value)
5411 with pytest.raises(ValueError) as info:
5412 blockview[None, :, :]
5413 assert "newaxis" in str(info.value) and "not supported" in str(info.value)
5414 with pytest.raises(IndexError) as info:
5415 blockview[100, 100]
5416
5417
5418def test_blocks_indexer():

Callers

nothing calls this directly

Calls 7

ravelMethod · 0.95
BlockViewClass · 0.90
assert_eqFunction · 0.90
arangeMethod · 0.45
prodMethod · 0.45
randomMethod · 0.45
onesMethod · 0.45

Tested by

no test coverage detected