20
20
from zarr .abc .codec import Codec
21
21
from zarr .abc .store import set_or_delete
22
22
from zarr .attributes import Attributes
23
- from zarr .buffer import Factory , NDArrayLike , NDBuffer
23
+ from zarr .buffer import BufferPrototype , NDArrayLike , NDBuffer , default_buffer_prototype
24
24
from zarr .chunk_grids import RegularChunkGrid
25
25
from zarr .chunk_key_encodings import ChunkKeyEncoding , DefaultChunkKeyEncoding , V2ChunkKeyEncoding
26
26
from zarr .codecs import BytesCodec
@@ -414,8 +414,8 @@ async def _get_selection(
414
414
self ,
415
415
indexer : Indexer ,
416
416
* ,
417
+ prototype : BufferPrototype ,
417
418
out : NDBuffer | None = None ,
418
- factory : Factory .Create = NDBuffer .create ,
419
419
fields : Fields | None = None ,
420
420
) -> NDArrayLike :
421
421
# check fields are sensible
@@ -432,7 +432,7 @@ async def _get_selection(
432
432
f"shape of out argument doesn't match. Expected { indexer .shape } , got { out .shape } "
433
433
)
434
434
else :
435
- out_buffer = factory (
435
+ out_buffer = prototype . nd_buffer . create (
436
436
shape = indexer .shape ,
437
437
dtype = out_dtype ,
438
438
order = self .order ,
@@ -444,7 +444,7 @@ async def _get_selection(
444
444
[
445
445
(
446
446
self .store_path / self .metadata .encode_chunk_key (chunk_coords ),
447
- self .metadata .get_chunk_spec (chunk_coords , self .order ),
447
+ self .metadata .get_chunk_spec (chunk_coords , self .order , prototype = prototype ),
448
448
chunk_selection ,
449
449
out_selection ,
450
450
)
@@ -456,14 +456,14 @@ async def _get_selection(
456
456
return out_buffer .as_ndarray_like ()
457
457
458
458
async def getitem (
459
- self , selection : Selection , * , factory : Factory . Create = NDBuffer . create
459
+ self , selection : Selection , * , prototype : BufferPrototype = default_buffer_prototype
460
460
) -> NDArrayLike :
461
461
indexer = BasicIndexer (
462
462
selection ,
463
463
shape = self .metadata .shape ,
464
464
chunk_grid = self .metadata .chunk_grid ,
465
465
)
466
- return await self ._get_selection (indexer , factory = factory )
466
+ return await self ._get_selection (indexer , prototype = prototype )
467
467
468
468
async def _save_metadata (self , metadata : ArrayMetadata ) -> None :
469
469
to_save = metadata .to_buffer_dict ()
@@ -475,7 +475,7 @@ async def _set_selection(
475
475
indexer : Indexer ,
476
476
value : NDArrayLike ,
477
477
* ,
478
- factory : Factory . NDArrayLike = NDBuffer . from_ndarray_like ,
478
+ prototype : BufferPrototype ,
479
479
fields : Fields | None = None ,
480
480
) -> None :
481
481
# check fields are sensible
@@ -497,14 +497,14 @@ async def _set_selection(
497
497
# We accept any ndarray like object from the user and convert it
498
498
# to a NDBuffer (or subclass). From this point onwards, we only pass
499
499
# Buffer and NDBuffer between components.
500
- value_buffer = factory (value )
500
+ value_buffer = prototype . nd_buffer . from_ndarray_like (value )
501
501
502
502
# merging with existing data and encoding chunks
503
503
await self .metadata .codec_pipeline .write (
504
504
[
505
505
(
506
506
self .store_path / self .metadata .encode_chunk_key (chunk_coords ),
507
- self .metadata .get_chunk_spec (chunk_coords , self .order ),
507
+ self .metadata .get_chunk_spec (chunk_coords , self .order , prototype ),
508
508
chunk_selection ,
509
509
out_selection ,
510
510
)
@@ -518,14 +518,14 @@ async def setitem(
518
518
self ,
519
519
selection : Selection ,
520
520
value : NDArrayLike ,
521
- factory : Factory . NDArrayLike = NDBuffer . from_ndarray_like ,
521
+ prototype : BufferPrototype = default_buffer_prototype ,
522
522
) -> None :
523
523
indexer = BasicIndexer (
524
524
selection ,
525
525
shape = self .metadata .shape ,
526
526
chunk_grid = self .metadata .chunk_grid ,
527
527
)
528
- return await self ._set_selection (indexer , value , factory = factory )
528
+ return await self ._set_selection (indexer , value , prototype = prototype )
529
529
530
530
async def resize (
531
531
self , new_shape : ChunkCoords , delete_outside_chunks : bool = True
@@ -714,7 +714,9 @@ def __setitem__(self, selection: Selection, value: NDArrayLike) -> None:
714
714
def get_basic_selection (
715
715
self ,
716
716
selection : BasicSelection = Ellipsis ,
717
+ * ,
717
718
out : NDBuffer | None = None ,
719
+ prototype : BufferPrototype = default_buffer_prototype ,
718
720
fields : Fields | None = None ,
719
721
) -> NDArrayLike :
720
722
if self .shape == ():
@@ -725,57 +727,101 @@ def get_basic_selection(
725
727
BasicIndexer (selection , self .shape , self .metadata .chunk_grid ),
726
728
out = out ,
727
729
fields = fields ,
730
+ prototype = prototype ,
728
731
)
729
732
)
730
733
731
734
def set_basic_selection (
732
- self , selection : BasicSelection , value : NDArrayLike , fields : Fields | None = None
735
+ self ,
736
+ selection : BasicSelection ,
737
+ value : NDArrayLike ,
738
+ * ,
739
+ fields : Fields | None = None ,
740
+ prototype : BufferPrototype = default_buffer_prototype ,
733
741
) -> None :
734
742
indexer = BasicIndexer (selection , self .shape , self .metadata .chunk_grid )
735
- sync (self ._async_array ._set_selection (indexer , value , fields = fields ))
743
+ sync (self ._async_array ._set_selection (indexer , value , fields = fields , prototype = prototype ))
736
744
737
745
def get_orthogonal_selection (
738
746
self ,
739
747
selection : OrthogonalSelection ,
748
+ * ,
740
749
out : NDBuffer | None = None ,
741
750
fields : Fields | None = None ,
751
+ prototype : BufferPrototype = default_buffer_prototype ,
742
752
) -> NDArrayLike :
743
753
indexer = OrthogonalIndexer (selection , self .shape , self .metadata .chunk_grid )
744
- return sync (self ._async_array ._get_selection (indexer = indexer , out = out , fields = fields ))
754
+ return sync (
755
+ self ._async_array ._get_selection (
756
+ indexer = indexer , out = out , fields = fields , prototype = prototype
757
+ )
758
+ )
745
759
746
760
def set_orthogonal_selection (
747
- self , selection : OrthogonalSelection , value : NDArrayLike , fields : Fields | None = None
761
+ self ,
762
+ selection : OrthogonalSelection ,
763
+ value : NDArrayLike ,
764
+ * ,
765
+ fields : Fields | None = None ,
766
+ prototype : BufferPrototype = default_buffer_prototype ,
748
767
) -> None :
749
768
indexer = OrthogonalIndexer (selection , self .shape , self .metadata .chunk_grid )
750
- return sync (self ._async_array ._set_selection (indexer , value , fields = fields ))
769
+ return sync (
770
+ self ._async_array ._set_selection (indexer , value , fields = fields , prototype = prototype )
771
+ )
751
772
752
773
def get_mask_selection (
753
- self , mask : MaskSelection , out : NDBuffer | None = None , fields : Fields | None = None
774
+ self ,
775
+ mask : MaskSelection ,
776
+ * ,
777
+ out : NDBuffer | None = None ,
778
+ fields : Fields | None = None ,
779
+ prototype : BufferPrototype = default_buffer_prototype ,
754
780
) -> NDArrayLike :
755
781
indexer = MaskIndexer (mask , self .shape , self .metadata .chunk_grid )
756
- return sync (self ._async_array ._get_selection (indexer = indexer , out = out , fields = fields ))
782
+ return sync (
783
+ self ._async_array ._get_selection (
784
+ indexer = indexer , out = out , fields = fields , prototype = prototype
785
+ )
786
+ )
757
787
758
788
def set_mask_selection (
759
- self , mask : MaskSelection , value : NDArrayLike , fields : Fields | None = None
789
+ self ,
790
+ mask : MaskSelection ,
791
+ value : NDArrayLike ,
792
+ * ,
793
+ fields : Fields | None = None ,
794
+ prototype : BufferPrototype = default_buffer_prototype ,
760
795
) -> None :
761
796
indexer = MaskIndexer (mask , self .shape , self .metadata .chunk_grid )
762
- sync (self ._async_array ._set_selection (indexer , value , fields = fields ))
797
+ sync (self ._async_array ._set_selection (indexer , value , fields = fields , prototype = prototype ))
763
798
764
799
def get_coordinate_selection (
765
800
self ,
766
801
selection : CoordinateSelection ,
802
+ * ,
767
803
out : NDBuffer | None = None ,
768
804
fields : Fields | None = None ,
805
+ prototype : BufferPrototype = default_buffer_prototype ,
769
806
) -> NDArrayLike :
770
807
indexer = CoordinateIndexer (selection , self .shape , self .metadata .chunk_grid )
771
- out_array = sync (self ._async_array ._get_selection (indexer = indexer , out = out , fields = fields ))
808
+ out_array = sync (
809
+ self ._async_array ._get_selection (
810
+ indexer = indexer , out = out , fields = fields , prototype = prototype
811
+ )
812
+ )
772
813
773
814
# restore shape
774
815
out_array = out_array .reshape (indexer .sel_shape )
775
816
return out_array
776
817
777
818
def set_coordinate_selection (
778
- self , selection : CoordinateSelection , value : NDArrayLike , fields : Fields | None = None
819
+ self ,
820
+ selection : CoordinateSelection ,
821
+ value : NDArrayLike ,
822
+ * ,
823
+ fields : Fields | None = None ,
824
+ prototype : BufferPrototype = default_buffer_prototype ,
779
825
) -> None :
780
826
# setup indexer
781
827
indexer = CoordinateIndexer (selection , self .shape , self .metadata .chunk_grid )
@@ -792,25 +838,33 @@ def set_coordinate_selection(
792
838
if hasattr (value , "shape" ) and len (value .shape ) > 1 :
793
839
value = value .reshape (- 1 )
794
840
795
- sync (self ._async_array ._set_selection (indexer , value , fields = fields ))
841
+ sync (self ._async_array ._set_selection (indexer , value , fields = fields , prototype = prototype ))
796
842
797
843
def get_block_selection (
798
844
self ,
799
845
selection : BlockSelection ,
846
+ * ,
800
847
out : NDBuffer | None = None ,
801
848
fields : Fields | None = None ,
849
+ prototype : BufferPrototype = default_buffer_prototype ,
802
850
) -> NDArrayLike :
803
851
indexer = BlockIndexer (selection , self .shape , self .metadata .chunk_grid )
804
- return sync (self ._async_array ._get_selection (indexer = indexer , out = out , fields = fields ))
852
+ return sync (
853
+ self ._async_array ._get_selection (
854
+ indexer = indexer , out = out , fields = fields , prototype = prototype
855
+ )
856
+ )
805
857
806
858
def set_block_selection (
807
859
self ,
808
860
selection : BlockSelection ,
809
861
value : NDArrayLike ,
862
+ * ,
810
863
fields : Fields | None = None ,
864
+ prototype : BufferPrototype = default_buffer_prototype ,
811
865
) -> None :
812
866
indexer = BlockIndexer (selection , self .shape , self .metadata .chunk_grid )
813
- sync (self ._async_array ._set_selection (indexer , value , fields = fields ))
867
+ sync (self ._async_array ._set_selection (indexer , value , fields = fields , prototype = prototype ))
814
868
815
869
@property
816
870
def vindex (self ) -> VIndex :
0 commit comments