22import textwrap
33from typing import Literal
44
5- import zarr .abc .store
6-
75# Group
86# Name : /
97# Type : zarr.hierarchy.Group
1412# No. groups : 0
1513
1614
17- # In [19]: z.info
18- # Out[19]:
19- # Type : zarr.core.Array
20- # Data type : int32
21- # Shape : (1000000,)
22- # Chunk shape : (100000,)
23- # Order : C
24- # Read-only : False
25- # Compressor : Blosc(cname='lz4', clevel=5, shuffle=SHUFFLE, blocksize=0)
26- # Store type : zarr.storage.KVStore
27- # No. bytes : 4000000 (3.8M)
28- # No. bytes stored : 320
29- # Storage ratio : 12500.0
30- # Chunks initialized : 0/10
31-
32-
3315@dataclasses .dataclass (kw_only = True )
3416class GroupInfo :
3517 name : str
3618 type : Literal ["Group" ] = "Group"
19+ zarr_format : Literal [2 , 3 ]
3720 read_only : bool
3821 store_type : str
3922 count_members : int | None = None
@@ -44,17 +27,95 @@ def __repr__(self) -> str:
4427 template = textwrap .dedent ("""\
4528 Name : {name}
4629 Type : {type}
30+ Zarr format : {zarr_format}
4731 Read-only : {read_only}
4832 Store type : {store_type}""" )
4933
5034 if self .count_members is not None :
51- template += ( "\n No. members : {count_members}" )
35+ template += "\n No. members : {count_members}"
5236 if self .count_arrays is not None :
53- template += ( "\n No. arrays : {count_arrays}" )
37+ template += "\n No. arrays : {count_arrays}"
5438 if self .count_groups is not None :
55- template += ("\n No. groups : {count_groups}" )
56- return template .format (
57- ** dataclasses .asdict (self )
58- )
39+ template += "\n No. groups : {count_groups}"
40+ return template .format (** dataclasses .asdict (self ))
41+
42+
43+ def human_readable_size (size : int ) -> str :
44+ if size < 2 ** 10 :
45+ return f"{ size } "
46+ elif size < 2 ** 20 :
47+ return f"{ size / float (2 ** 10 ):.1f} K"
48+ elif size < 2 ** 30 :
49+ return f"{ size / float (2 ** 20 ):.1f} M"
50+ elif size < 2 ** 40 :
51+ return f"{ size / float (2 ** 30 ):.1f} G"
52+ elif size < 2 ** 50 :
53+ return f"{ size / float (2 ** 40 ):.1f} T"
54+ else :
55+ return f"{ size / float (2 ** 50 ):.1f} P"
56+
57+
58+ def byte_info (size : int ) -> str :
59+ if size < 2 ** 10 :
60+ return size
61+ else :
62+ return f"{ size } ({ human_readable_size (size )} )"
63+
64+
65+ @dataclasses .dataclass (kw_only = True )
66+ class ArrayInfo :
67+ type : Literal ["Array" ] = "Array"
68+ zarr_format : Literal [2 , 3 ]
69+ data_type : str
70+ shape : tuple [int ,]
71+ chunk_shape : tuple [int ,]
72+ order : Literal ["C" , "F" ]
73+ read_only : bool
74+ store_type : str
75+ compressor : str | None = None
76+ filters : list [str ] | None = None
77+ codecs : list [str ] | None = None
78+ count_bytes : int | None = None
79+ count_bytes_stored : int | None = None
80+ count_chunks_initialized : int | None = None
81+
82+ def __repr__ (self ) -> str :
83+ template = textwrap .dedent ("""\
84+ Type : {type}
85+ Zarr format : {zarr_format}
86+ Data type : {data_type}
87+ Shape : {shape}
88+ Chunk shape : {chunk_shape}
89+ Order : {order}
90+ Read-only : {read_only}
91+ Store type : {store_type}""" )
92+
93+ kwargs = dataclasses .asdict (self )
94+ if self .compressor is not None :
95+ template += "\n Compressor : {compressor}"
96+
97+ if self .filters is not None :
98+ template += "\n Filters : {filters}"
99+
100+ if self .codecs is not None :
101+ template += "\n Codecs : {codecs}"
102+
103+ if self .count_bytes is not None :
104+ template += "\n No. bytes : {count_bytes}"
105+ kwargs ["count_bytes" ] = byte_info (self .count_bytes )
106+
107+ if self .count_bytes_stored is not None :
108+ template += "\n No. bytes stored : {count_bytes_stored}"
109+ kwargs ["count_stored" ] = byte_info (self .count_bytes_stored )
110+
111+ if (
112+ self .count_bytes is not None
113+ and self .count_bytes_stored is not None
114+ and self .count_bytes_stored > 0
115+ ):
116+ template += "\n Storage ratio : {storage_ratio}"
117+ kwargs ["storage_ratio" ] = f"{ self .count_bytes / self .count_bytes_stored :.1f} "
59118
60- # def _repr_html_(self): ...
119+ if self .count_chunks_initialized is not None :
120+ template += "\n Chunks Initialized : {count_chunks_initialized}"
121+ return template .format (** kwargs )
0 commit comments