1010
1111from ngff_zarr import Methods , to_multiscales , to_ngff_zarr
1212
13- # from ._data import verify_against_baseline
13+ from ._data import verify_against_baseline
1414
1515zarr_version = version .parse (zarr .__version__ )
1616
2323def test_zarr_v3_compression (input_images ):
2424 dataset_name = "cthead1"
2525 image = input_images [dataset_name ]
26- # baseline_name = "2_4/RFC3_GAUSSIAN.zarr"
26+ baseline_name = "2_4/RFC3_GAUSSIAN.zarr"
2727 chunks = (64 , 64 )
2828 multiscales = to_multiscales (
2929 image , [2 , 4 ], chunks = chunks , method = Methods .ITKWASM_GAUSSIAN
3030 )
31- # store = zarr.storage.MemoryStore()
3231
3332 compressors = zarr .codecs .BloscCodec (
3433 cname = "zlib" , clevel = 5 , shuffle = zarr .codecs .BloscShuffle .shuffle
3534 )
3635
37- # chunks_per_shard = 2
38- # Test this with sharding also
39-
36+ # Test writing OME-Zarr 0.5 with compression
4037 version = "0.5"
4138 with tempfile .TemporaryDirectory () as tmpdir :
4239 to_ngff_zarr (tmpdir , multiscales , version = version , compressors = compressors )
@@ -66,3 +63,74 @@ def test_zarr_v3_compression(input_images):
6663 assert scale2 ["codecs" ][1 ]["configuration" ]["cname" ] == "zlib"
6764 assert scale2 ["codecs" ][1 ]["configuration" ]["clevel" ] == 5
6865 assert scale2 ["codecs" ][1 ]["configuration" ]["shuffle" ] == "shuffle"
66+
67+ verify_against_baseline (
68+ dataset_name , baseline_name , multiscales , version = version
69+ )
70+
71+ # Test writing OME-Zarr 0.5 with compression and sharding
72+ version = "0.5"
73+ chunks_per_shard = 2
74+
75+ with tempfile .TemporaryDirectory () as tmpdir :
76+ to_ngff_zarr (
77+ tmpdir ,
78+ multiscales ,
79+ version = version ,
80+ compressors = compressors ,
81+ chunks_per_shard = chunks_per_shard ,
82+ )
83+
84+ with open (tmpdir + "/zarr.json" ) as f :
85+ zarr_json = json .load (f )
86+ assert zarr_json ["zarr_format" ] == 3
87+ metadata = zarr_json ["consolidated_metadata" ]["metadata" ]
88+
89+ scale0 = metadata ["scale0/image" ]
90+ assert scale0 ["chunk_grid" ]["configuration" ]["chunk_shape" ][0 ] == 128
91+ assert scale0 ["chunk_grid" ]["configuration" ]["chunk_shape" ][1 ] == 128
92+
93+ sharding_codec = scale0 ["codecs" ][0 ]
94+ assert sharding_codec ["name" ] == "sharding_indexed"
95+ assert sharding_codec ["configuration" ]["chunk_shape" ][0 ] == 64
96+ assert sharding_codec ["configuration" ]["chunk_shape" ][1 ] == 64
97+
98+ blosc_codec = sharding_codec ["configuration" ]["codecs" ][1 ]
99+ assert blosc_codec ["name" ] == "blosc"
100+ assert blosc_codec ["configuration" ]["cname" ] == "zlib"
101+ assert blosc_codec ["configuration" ]["clevel" ] == 5
102+ assert blosc_codec ["configuration" ]["shuffle" ] == "shuffle"
103+
104+ scale1 = metadata ["scale1/image" ]
105+ sharding_codec = scale1 ["codecs" ][0 ]
106+ assert sharding_codec ["name" ] == "sharding_indexed"
107+
108+ blosc_codec = sharding_codec ["configuration" ]["codecs" ][1 ]
109+ assert blosc_codec ["name" ] == "blosc"
110+ assert blosc_codec ["configuration" ]["cname" ] == "zlib"
111+ assert blosc_codec ["configuration" ]["clevel" ] == 5
112+ assert blosc_codec ["configuration" ]["shuffle" ] == "shuffle"
113+
114+ verify_against_baseline (
115+ dataset_name , baseline_name , multiscales , version = version
116+ )
117+
118+
119+ def test_zarr_v3_compression_rejected_for_ome_zarr_04 (input_images ):
120+ """Test that compressors argument is rejected for OME-Zarr version 0.4"""
121+ dataset_name = "cthead1"
122+ image = input_images [dataset_name ]
123+ chunks = (64 , 64 )
124+ multiscales = to_multiscales (
125+ image , [2 , 4 ], chunks = chunks , method = Methods .ITKWASM_GAUSSIAN
126+ )
127+ store = zarr .storage .MemoryStore ()
128+
129+ compressors = zarr .codecs .BloscCodec (
130+ cname = "zlib" , clevel = 5 , shuffle = zarr .codecs .BloscShuffle .shuffle
131+ )
132+
133+ # Setting `compressors` argument for OME-Zarr 0.4 should raise an error
134+ version = "0.4"
135+ with pytest .raises (ValueError , match = "not supported for OME-Zarr version 0.4" ):
136+ to_ngff_zarr (store , multiscales , version = version , compressors = compressors )
0 commit comments