|
20 | 20 | VLenUTF8Codec,
|
21 | 21 | ZstdCodec,
|
22 | 22 | )
|
| 23 | +from zarr.codecs.sharding import ShardingCodec |
23 | 24 | from zarr.core._info import ArrayInfo
|
24 | 25 | from zarr.core.array import (
|
25 | 26 | CompressorsLike,
|
@@ -478,121 +479,168 @@ def test_update_attrs(zarr_format: ZarrFormat) -> None:
|
478 | 479 | assert arr2.attrs["foo"] == "bar"
|
479 | 480 |
|
480 | 481 |
|
| 482 | +@pytest.mark.parametrize(("chunks", "shards"), [((2, 2), None), ((2, 2), (4, 4))]) |
481 | 483 | class TestInfo:
|
482 |
| - def test_info_v2(self) -> None: |
483 |
| - arr = zarr.create(shape=(4, 4), chunks=(2, 2), zarr_format=2) |
| 484 | + def test_info_v2(self, chunks: tuple[int, int], shards: tuple[int, int] | None) -> None: |
| 485 | + arr = zarr.create_array(store={}, shape=(8, 8), dtype="f8", chunks=chunks, zarr_format=2) |
484 | 486 | result = arr.info
|
485 | 487 | expected = ArrayInfo(
|
486 | 488 | _zarr_format=2,
|
487 | 489 | _data_type=np.dtype("float64"),
|
488 |
| - _shape=(4, 4), |
489 |
| - _chunk_shape=(2, 2), |
| 490 | + _shape=(8, 8), |
| 491 | + _chunk_shape=chunks, |
| 492 | + _shard_shape=None, |
490 | 493 | _order="C",
|
491 | 494 | _read_only=False,
|
492 | 495 | _store_type="MemoryStore",
|
493 |
| - _count_bytes=128, |
| 496 | + _count_bytes=512, |
494 | 497 | _compressor=numcodecs.Zstd(),
|
495 | 498 | )
|
496 | 499 | assert result == expected
|
497 | 500 |
|
498 |
| - def test_info_v3(self) -> None: |
499 |
| - arr = zarr.create(shape=(4, 4), chunks=(2, 2), zarr_format=3) |
| 501 | + def test_info_v3(self, chunks: tuple[int, int], shards: tuple[int, int] | None) -> None: |
| 502 | + arr = zarr.create_array(store={}, shape=(8, 8), dtype="f8", chunks=chunks, shards=shards) |
500 | 503 | result = arr.info
|
501 | 504 | expected = ArrayInfo(
|
502 | 505 | _zarr_format=3,
|
503 | 506 | _data_type=DataType.parse("float64"),
|
504 |
| - _shape=(4, 4), |
505 |
| - _chunk_shape=(2, 2), |
| 507 | + _shape=(8, 8), |
| 508 | + _chunk_shape=chunks, |
| 509 | + _shard_shape=shards, |
506 | 510 | _order="C",
|
507 | 511 | _read_only=False,
|
508 | 512 | _store_type="MemoryStore",
|
509 |
| - _codecs=[BytesCodec(), ZstdCodec()], |
510 |
| - _count_bytes=128, |
| 513 | + _codecs=[BytesCodec(), ZstdCodec()] |
| 514 | + if shards is None |
| 515 | + else [ShardingCodec(chunk_shape=chunks, codecs=[BytesCodec(), ZstdCodec()])], |
| 516 | + _count_bytes=512, |
511 | 517 | )
|
512 | 518 | assert result == expected
|
513 | 519 |
|
514 |
| - def test_info_complete(self) -> None: |
515 |
| - arr = zarr.create(shape=(4, 4), chunks=(2, 2), zarr_format=3, codecs=[BytesCodec()]) |
| 520 | + def test_info_complete(self, chunks: tuple[int, int], shards: tuple[int, int] | None) -> None: |
| 521 | + arr = zarr.create_array( |
| 522 | + store={}, |
| 523 | + shape=(8, 8), |
| 524 | + dtype="f8", |
| 525 | + chunks=chunks, |
| 526 | + shards=shards, |
| 527 | + compressors=(), |
| 528 | + ) |
516 | 529 | result = arr.info_complete()
|
517 | 530 | expected = ArrayInfo(
|
518 | 531 | _zarr_format=3,
|
519 | 532 | _data_type=DataType.parse("float64"),
|
520 |
| - _shape=(4, 4), |
521 |
| - _chunk_shape=(2, 2), |
| 533 | + _shape=(8, 8), |
| 534 | + _chunk_shape=chunks, |
| 535 | + _shard_shape=shards, |
522 | 536 | _order="C",
|
523 | 537 | _read_only=False,
|
524 | 538 | _store_type="MemoryStore",
|
525 |
| - _codecs=[BytesCodec()], |
526 |
| - _count_bytes=128, |
| 539 | + _codecs=[BytesCodec()] if shards is None else [ShardingCodec(chunk_shape=chunks)], |
| 540 | + _count_bytes=512, |
527 | 541 | _count_chunks_initialized=0,
|
528 |
| - _count_bytes_stored=373, # the metadata? |
| 542 | + _count_bytes_stored=373 if shards is None else 578, # the metadata? |
529 | 543 | )
|
530 | 544 | assert result == expected
|
531 | 545 |
|
532 |
| - arr[:2, :2] = 10 |
| 546 | + arr[:4, :4] = 10 |
533 | 547 | result = arr.info_complete()
|
534 |
| - expected = dataclasses.replace( |
535 |
| - expected, _count_chunks_initialized=1, _count_bytes_stored=405 |
536 |
| - ) |
| 548 | + if shards is None: |
| 549 | + expected = dataclasses.replace( |
| 550 | + expected, _count_chunks_initialized=4, _count_bytes_stored=501 |
| 551 | + ) |
| 552 | + else: |
| 553 | + expected = dataclasses.replace( |
| 554 | + expected, _count_chunks_initialized=1, _count_bytes_stored=774 |
| 555 | + ) |
537 | 556 | assert result == expected
|
538 | 557 |
|
539 |
| - async def test_info_v2_async(self) -> None: |
540 |
| - arr = await zarr.api.asynchronous.create(shape=(4, 4), chunks=(2, 2), zarr_format=2) |
| 558 | + async def test_info_v2_async( |
| 559 | + self, chunks: tuple[int, int], shards: tuple[int, int] | None |
| 560 | + ) -> None: |
| 561 | + arr = await zarr.api.asynchronous.create_array( |
| 562 | + store={}, shape=(8, 8), dtype="f8", chunks=chunks, zarr_format=2 |
| 563 | + ) |
541 | 564 | result = arr.info
|
542 | 565 | expected = ArrayInfo(
|
543 | 566 | _zarr_format=2,
|
544 | 567 | _data_type=np.dtype("float64"),
|
545 |
| - _shape=(4, 4), |
| 568 | + _shape=(8, 8), |
546 | 569 | _chunk_shape=(2, 2),
|
| 570 | + _shard_shape=None, |
547 | 571 | _order="C",
|
548 | 572 | _read_only=False,
|
549 | 573 | _store_type="MemoryStore",
|
550 |
| - _count_bytes=128, |
| 574 | + _count_bytes=512, |
551 | 575 | _compressor=numcodecs.Zstd(),
|
552 | 576 | )
|
553 | 577 | assert result == expected
|
554 | 578 |
|
555 |
| - async def test_info_v3_async(self) -> None: |
556 |
| - arr = await zarr.api.asynchronous.create(shape=(4, 4), chunks=(2, 2), zarr_format=3) |
| 579 | + async def test_info_v3_async( |
| 580 | + self, chunks: tuple[int, int], shards: tuple[int, int] | None |
| 581 | + ) -> None: |
| 582 | + arr = await zarr.api.asynchronous.create_array( |
| 583 | + store={}, |
| 584 | + shape=(8, 8), |
| 585 | + dtype="f8", |
| 586 | + chunks=chunks, |
| 587 | + shards=shards, |
| 588 | + ) |
557 | 589 | result = arr.info
|
558 | 590 | expected = ArrayInfo(
|
559 | 591 | _zarr_format=3,
|
560 | 592 | _data_type=DataType.parse("float64"),
|
561 |
| - _shape=(4, 4), |
562 |
| - _chunk_shape=(2, 2), |
| 593 | + _shape=(8, 8), |
| 594 | + _chunk_shape=chunks, |
| 595 | + _shard_shape=shards, |
563 | 596 | _order="C",
|
564 | 597 | _read_only=False,
|
565 | 598 | _store_type="MemoryStore",
|
566 |
| - _codecs=[BytesCodec(), ZstdCodec()], |
567 |
| - _count_bytes=128, |
| 599 | + _codecs=[BytesCodec(), ZstdCodec()] |
| 600 | + if shards is None |
| 601 | + else [ShardingCodec(chunk_shape=chunks, codecs=[BytesCodec(), ZstdCodec()])], |
| 602 | + _count_bytes=512, |
568 | 603 | )
|
569 | 604 | assert result == expected
|
570 | 605 |
|
571 |
| - async def test_info_complete_async(self) -> None: |
572 |
| - arr = await zarr.api.asynchronous.create( |
573 |
| - shape=(4, 4), chunks=(2, 2), zarr_format=3, codecs=[BytesCodec()] |
| 606 | + async def test_info_complete_async( |
| 607 | + self, chunks: tuple[int, int], shards: tuple[int, int] | None |
| 608 | + ) -> None: |
| 609 | + arr = await zarr.api.asynchronous.create_array( |
| 610 | + store={}, |
| 611 | + dtype="f8", |
| 612 | + shape=(8, 8), |
| 613 | + chunks=chunks, |
| 614 | + shards=shards, |
| 615 | + compressors=None, |
574 | 616 | )
|
575 | 617 | result = await arr.info_complete()
|
576 | 618 | expected = ArrayInfo(
|
577 | 619 | _zarr_format=3,
|
578 | 620 | _data_type=DataType.parse("float64"),
|
579 |
| - _shape=(4, 4), |
580 |
| - _chunk_shape=(2, 2), |
| 621 | + _shape=(8, 8), |
| 622 | + _chunk_shape=chunks, |
| 623 | + _shard_shape=shards, |
581 | 624 | _order="C",
|
582 | 625 | _read_only=False,
|
583 | 626 | _store_type="MemoryStore",
|
584 |
| - _codecs=[BytesCodec()], |
585 |
| - _count_bytes=128, |
| 627 | + _codecs=[BytesCodec()] if shards is None else [ShardingCodec(chunk_shape=chunks)], |
| 628 | + _count_bytes=512, |
586 | 629 | _count_chunks_initialized=0,
|
587 |
| - _count_bytes_stored=373, # the metadata? |
| 630 | + _count_bytes_stored=373 if shards is None else 578, # the metadata? |
588 | 631 | )
|
589 | 632 | assert result == expected
|
590 | 633 |
|
591 |
| - await arr.setitem((slice(2), slice(2)), 10) |
| 634 | + await arr.setitem((slice(4), slice(4)), 10) |
592 | 635 | result = await arr.info_complete()
|
593 |
| - expected = dataclasses.replace( |
594 |
| - expected, _count_chunks_initialized=1, _count_bytes_stored=405 |
595 |
| - ) |
| 636 | + if shards is None: |
| 637 | + expected = dataclasses.replace( |
| 638 | + expected, _count_chunks_initialized=4, _count_bytes_stored=501 |
| 639 | + ) |
| 640 | + else: |
| 641 | + expected = dataclasses.replace( |
| 642 | + expected, _count_chunks_initialized=1, _count_bytes_stored=774 |
| 643 | + ) |
596 | 644 | assert result == expected
|
597 | 645 |
|
598 | 646 |
|
|
0 commit comments