| 
 | 1 | +import dataclasses  | 
 | 2 | +import textwrap  | 
 | 3 | +from typing import Any, Literal  | 
 | 4 | + | 
 | 5 | +import numcodecs.abc  | 
 | 6 | +import numpy as np  | 
 | 7 | + | 
 | 8 | +from zarr.abc.codec import Codec  | 
 | 9 | +from zarr.core.metadata.v3 import DataType  | 
 | 10 | + | 
 | 11 | + | 
 | 12 | +@dataclasses.dataclass(kw_only=True)  | 
 | 13 | +class GroupInfo:  | 
 | 14 | +    """  | 
 | 15 | +    Visual summary for a Group.  | 
 | 16 | +
  | 
 | 17 | +    Note that this method and its properties is not part of  | 
 | 18 | +    Zarr's public API.  | 
 | 19 | +    """  | 
 | 20 | + | 
 | 21 | +    _name: str  | 
 | 22 | +    _type: Literal["Group"] = "Group"  | 
 | 23 | +    _zarr_format: Literal[2, 3]  | 
 | 24 | +    _read_only: bool  | 
 | 25 | +    _store_type: str  | 
 | 26 | +    _count_members: int | None = None  | 
 | 27 | +    _count_arrays: int | None = None  | 
 | 28 | +    _count_groups: int | None = None  | 
 | 29 | + | 
 | 30 | +    def __repr__(self) -> str:  | 
 | 31 | +        template = textwrap.dedent("""\  | 
 | 32 | +        Name        : {_name}  | 
 | 33 | +        Type        : {_type}  | 
 | 34 | +        Zarr format : {_zarr_format}  | 
 | 35 | +        Read-only   : {_read_only}  | 
 | 36 | +        Store type  : {_store_type}""")  | 
 | 37 | + | 
 | 38 | +        if self._count_members is not None:  | 
 | 39 | +            template += "\nNo. members : {_count_members}"  | 
 | 40 | +        if self._count_arrays is not None:  | 
 | 41 | +            template += "\nNo. arrays  : {_count_arrays}"  | 
 | 42 | +        if self._count_groups is not None:  | 
 | 43 | +            template += "\nNo. groups  : {_count_groups}"  | 
 | 44 | +        return template.format(**dataclasses.asdict(self))  | 
 | 45 | + | 
 | 46 | + | 
 | 47 | +def human_readable_size(size: int) -> str:  | 
 | 48 | +    if size < 2**10:  | 
 | 49 | +        return f"{size}"  | 
 | 50 | +    elif size < 2**20:  | 
 | 51 | +        return f"{size / float(2**10):.1f}K"  | 
 | 52 | +    elif size < 2**30:  | 
 | 53 | +        return f"{size / float(2**20):.1f}M"  | 
 | 54 | +    elif size < 2**40:  | 
 | 55 | +        return f"{size / float(2**30):.1f}G"  | 
 | 56 | +    elif size < 2**50:  | 
 | 57 | +        return f"{size / float(2**40):.1f}T"  | 
 | 58 | +    else:  | 
 | 59 | +        return f"{size / float(2**50):.1f}P"  | 
 | 60 | + | 
 | 61 | + | 
 | 62 | +def byte_info(size: int) -> str:  | 
 | 63 | +    if size < 2**10:  | 
 | 64 | +        return str(size)  | 
 | 65 | +    else:  | 
 | 66 | +        return f"{size} ({human_readable_size(size)})"  | 
 | 67 | + | 
 | 68 | + | 
 | 69 | +@dataclasses.dataclass(kw_only=True)  | 
 | 70 | +class ArrayInfo:  | 
 | 71 | +    """  | 
 | 72 | +    Visual summary for an Array.  | 
 | 73 | +
  | 
 | 74 | +    Note that this method and its properties is not part of  | 
 | 75 | +    Zarr's public API.  | 
 | 76 | +    """  | 
 | 77 | + | 
 | 78 | +    _type: Literal["Array"] = "Array"  | 
 | 79 | +    _zarr_format: Literal[2, 3]  | 
 | 80 | +    _data_type: np.dtype[Any] | DataType  | 
 | 81 | +    _shape: tuple[int, ...]  | 
 | 82 | +    _chunk_shape: tuple[int, ...] | None = None  | 
 | 83 | +    _order: Literal["C", "F"]  | 
 | 84 | +    _read_only: bool  | 
 | 85 | +    _store_type: str  | 
 | 86 | +    _compressor: numcodecs.abc.Codec | None = None  | 
 | 87 | +    _filters: tuple[numcodecs.abc.Codec, ...] | None = None  | 
 | 88 | +    _codecs: list[Codec] | None = None  | 
 | 89 | +    _count_bytes: int | None = None  | 
 | 90 | +    _count_bytes_stored: int | None = None  | 
 | 91 | +    _count_chunks_initialized: int | None = None  | 
 | 92 | + | 
 | 93 | +    def __repr__(self) -> str:  | 
 | 94 | +        template = textwrap.dedent("""\  | 
 | 95 | +        Type               : {_type}  | 
 | 96 | +        Zarr format        : {_zarr_format}  | 
 | 97 | +        Data type          : {_data_type}  | 
 | 98 | +        Shape              : {_shape}  | 
 | 99 | +        Chunk shape        : {_chunk_shape}  | 
 | 100 | +        Order              : {_order}  | 
 | 101 | +        Read-only          : {_read_only}  | 
 | 102 | +        Store type         : {_store_type}""")  | 
 | 103 | + | 
 | 104 | +        kwargs = dataclasses.asdict(self)  | 
 | 105 | +        if self._chunk_shape is None:  | 
 | 106 | +            # for non-regular chunk grids  | 
 | 107 | +            kwargs["chunk_shape"] = "<variable>"  | 
 | 108 | +        if self._compressor is not None:  | 
 | 109 | +            template += "\nCompressor         : {_compressor}"  | 
 | 110 | + | 
 | 111 | +        if self._filters is not None:  | 
 | 112 | +            template += "\nFilters            : {_filters}"  | 
 | 113 | + | 
 | 114 | +        if self._codecs is not None:  | 
 | 115 | +            template += "\nCodecs             : {_codecs}"  | 
 | 116 | + | 
 | 117 | +        if self._count_bytes is not None:  | 
 | 118 | +            template += "\nNo. bytes          : {_count_bytes}"  | 
 | 119 | +            kwargs["_count_bytes"] = byte_info(self._count_bytes)  | 
 | 120 | + | 
 | 121 | +        if self._count_bytes_stored is not None:  | 
 | 122 | +            template += "\nNo. bytes stored   : {_count_bytes_stored}"  | 
 | 123 | +            kwargs["_count_stored"] = byte_info(self._count_bytes_stored)  | 
 | 124 | + | 
 | 125 | +        if (  | 
 | 126 | +            self._count_bytes is not None  | 
 | 127 | +            and self._count_bytes_stored is not None  | 
 | 128 | +            and self._count_bytes_stored > 0  | 
 | 129 | +        ):  | 
 | 130 | +            template += "\nStorage ratio      : {_storage_ratio}"  | 
 | 131 | +            kwargs["_storage_ratio"] = f"{self._count_bytes / self._count_bytes_stored:.1f}"  | 
 | 132 | + | 
 | 133 | +        if self._count_chunks_initialized is not None:  | 
 | 134 | +            template += "\nChunks Initialized : {_count_chunks_initialized}"  | 
 | 135 | +        return template.format(**kwargs)  | 
0 commit comments