@@ -498,15 +498,43 @@ def info(self) -> ArrayInfo:
498498 from zarrita .sharding import ShardingCodec
499499
500500 zarray = self ._zarray
501+ dimension_names : tuple [str , ...]
501502 if (names := getattr (zarray .metadata , "dimension_names" , None )) is None :
502- dimension_names = ("c" , "x" , "y" , "z" )
503+ if (shape := getattr (zarray .metadata , "shape" , None )) is None :
504+ raise ValueError (
505+ "Unable to determine the shape of the Zarrita Array. Neither dimension_names nor shape are present in the metadata file zarr.json."
506+ )
507+ else :
508+ if len (shape ) == 2 :
509+ dimension_names = ("x" , "y" )
510+ num_channels = 1
511+ elif len (shape ) == 3 :
512+ dimension_names = ("x" , "y" , "z" )
513+ num_channels = 1
514+ elif len (shape ) == 4 :
515+ dimension_names = ("c" , "x" , "y" , "z" )
516+ num_channels = shape [0 ]
517+ else :
518+ raise ValueError (
519+ "Unusual shape for Zarrita array, please specify the dimension names in the metadata file zarr.json."
520+ )
503521 else :
504522 dimension_names = names
523+ if (shape := getattr (zarray .metadata , "shape" , None )) is None :
524+ shape = VecInt .ones (dimension_names )
525+ if "c" in dimension_names :
526+ num_channels = zarray .metadata .shape [dimension_names .index ("c" )]
527+ else :
528+ num_channels = 1
505529 x_index , y_index , z_index = (
506530 dimension_names .index ("x" ),
507531 dimension_names .index ("y" ),
508532 dimension_names .index ("z" ),
509533 )
534+ if "c" not in dimension_names :
535+ shape = (num_channels ,) + shape
536+ dimension_names = ("c" ,) + dimension_names
537+ array_shape = VecInt (shape , axes = dimension_names )
510538 if isinstance (zarray , Array ):
511539 if len (zarray .codec_pipeline .codecs ) == 1 and isinstance (
512540 zarray .codec_pipeline .codecs [0 ], ShardingCodec
@@ -516,7 +544,7 @@ def info(self) -> ArrayInfo:
516544 chunk_shape = sharding_codec .configuration .chunk_shape
517545 return ArrayInfo (
518546 data_format = DataFormat .Zarr3 ,
519- num_channels = zarray . metadata . shape [ 0 ] ,
547+ num_channels = num_channels ,
520548 voxel_type = zarray .metadata .dtype ,
521549 compression_mode = self ._has_compression_codecs (
522550 sharding_codec .codec_pipeline .codecs
@@ -536,12 +564,13 @@ def info(self) -> ArrayInfo:
536564 chunk_shape [z_index ],
537565 )
538566 ),
567+ shape = array_shape ,
539568 dimension_names = dimension_names ,
540569 )
541570 chunk_shape = zarray .metadata .chunk_grid .configuration .chunk_shape
542571 return ArrayInfo (
543572 data_format = DataFormat .Zarr3 ,
544- num_channels = zarray . metadata . shape [ 0 ] ,
573+ num_channels = num_channels ,
545574 voxel_type = zarray .metadata .dtype ,
546575 compression_mode = self ._has_compression_codecs (
547576 zarray .codec_pipeline .codecs
@@ -550,13 +579,14 @@ def info(self) -> ArrayInfo:
550579 chunk_shape [x_index ], chunk_shape [y_index ], chunk_shape [z_index ]
551580 )
552581 or Vec3Int .full (1 ),
582+ shape = array_shape ,
553583 chunks_per_shard = Vec3Int .full (1 ),
554584 dimension_names = dimension_names ,
555585 )
556586 else :
557587 return ArrayInfo (
558588 data_format = DataFormat .Zarr ,
559- num_channels = zarray . metadata . shape [ 0 ] ,
589+ num_channels = num_channels ,
560590 voxel_type = zarray .metadata .dtype ,
561591 compression_mode = zarray .metadata .compressor is not None ,
562592 chunk_shape = Vec3Int (
@@ -565,6 +595,7 @@ def info(self) -> ArrayInfo:
565595 zarray .metadata .chunks [z_index ],
566596 )
567597 or Vec3Int .full (1 ),
598+ shape = array_shape ,
568599 chunks_per_shard = Vec3Int .full (1 ),
569600 dimension_names = dimension_names ,
570601 )
@@ -634,9 +665,13 @@ def create(cls, path: Path, array_info: ArrayInfo) -> "ZarritaArray":
634665 def read (self , bbox : NDBoundingBox ) -> np .ndarray :
635666 shape = bbox .size .to_tuple ()
636667 zarray = self ._zarray
637- slice_tuple = (slice (None ),) + bbox .to_slices ()
638668 with _blosc_disable_threading ():
639- data = zarray [slice_tuple ]
669+ try :
670+ slice_tuple = (slice (None ),) + bbox .to_slices ()
671+ data = zarray [slice_tuple ]
672+ except IndexError :
673+ # The data is stored without channel axis
674+ data = zarray [bbox .to_slices ()]
640675
641676 shape_with_channels = (self .info .num_channels ,) + shape
642677 if data .shape != shape_with_channels :
0 commit comments