@@ -493,7 +493,7 @@ def __getitem__(self, item):
493
493
else :
494
494
return out [()]
495
495
496
- def __setitem__ (self , key , value ):
496
+ def __setitem__ (self , item , value ):
497
497
"""Modify data for some portion of the array.
498
498
499
499
Examples
@@ -567,7 +567,7 @@ def __setitem__(self, key, value):
567
567
self ._load_metadata_nosync ()
568
568
569
569
# normalize selection
570
- selection = normalize_array_selection (key , self ._shape )
570
+ selection = normalize_array_selection (item , self ._shape )
571
571
572
572
# check value shape
573
573
expected_shape = tuple (
@@ -674,14 +674,14 @@ def _chunk_getitem(self, cidx, item, dest):
674
674
else :
675
675
dest [()] = tmp
676
676
677
- def _chunk_setitem (self , cidx , key , value ):
677
+ def _chunk_setitem (self , cidx , item , value ):
678
678
"""Replace part or whole of a chunk.
679
679
680
680
Parameters
681
681
----------
682
682
cidx : tuple of ints
683
683
Indices of the chunk.
684
- key : tuple of slices
684
+ item : tuple of slices
685
685
Location of region within the chunk.
686
686
value : scalar or ndarray
687
687
Value to set.
@@ -690,19 +690,19 @@ def _chunk_setitem(self, cidx, key, value):
690
690
691
691
# synchronization
692
692
if self ._synchronizer is None :
693
- self ._chunk_setitem_nosync (cidx , key , value )
693
+ self ._chunk_setitem_nosync (cidx , item , value )
694
694
else :
695
695
# synchronize on the chunk
696
696
ckey = self ._chunk_key (cidx )
697
697
with self ._synchronizer [ckey ]:
698
- self ._chunk_setitem_nosync (cidx , key , value )
698
+ self ._chunk_setitem_nosync (cidx , item , value )
699
699
700
- def _chunk_setitem_nosync (self , cidx , key , value ):
700
+ def _chunk_setitem_nosync (self , cidx , item , value ):
701
701
702
702
# obtain key for chunk storage
703
703
ckey = self ._chunk_key (cidx )
704
704
705
- if is_total_slice (key , self ._chunks ):
705
+ if is_total_slice (item , self ._chunks ):
706
706
# totally replace chunk
707
707
708
708
# optimization: we are completely replacing the chunk, so no need
@@ -717,11 +717,22 @@ def _chunk_setitem_nosync(self, cidx, key, value):
717
717
718
718
else :
719
719
720
- # ensure array is contiguous
721
- if self ._order == 'F' :
722
- chunk = np .asfortranarray (value , dtype = self ._dtype )
720
+ if not self ._compressor and not self ._filters :
721
+
722
+ # https://github.com/alimanfoo/zarr/issues/79
723
+ # Ensure a copy is taken so we don't end up storing
724
+ # a view into someone else's array.
725
+ # N.B., this assumes that filters or compressor always
726
+ # take a copy and never attempt to apply encoding in-place.
727
+ chunk = np .array (value , dtype = self ._dtype ,
728
+ order = self ._order )
729
+
723
730
else :
724
- chunk = np .ascontiguousarray (value , dtype = self ._dtype )
731
+ # ensure array is contiguous
732
+ if self ._order == 'F' :
733
+ chunk = np .asfortranarray (value , dtype = self ._dtype )
734
+ else :
735
+ chunk = np .ascontiguousarray (value , dtype = self ._dtype )
725
736
726
737
else :
727
738
# partially replace the contents of this chunk
@@ -747,7 +758,7 @@ def _chunk_setitem_nosync(self, cidx, key, value):
747
758
chunk = chunk .copy (order = 'K' )
748
759
749
760
# modify
750
- chunk [key ] = value
761
+ chunk [item ] = value
751
762
752
763
# encode chunk
753
764
cdata = self ._encode_chunk (chunk )
0 commit comments