def _write_op(self, f, *args, **kwargs): # guard condition if self._read_only: err_read_only() return self._synchronized_op(f, *args, **kwargs)
def _write_op(self, f, *args, **kwargs): # guard condition if self._read_only: err_read_only() # synchronization if self._synchronizer is None: return f(*args, **kwargs) else: # synchronize on the root group with self._synchronizer[group_meta_key]: return f(*args, **kwargs)
def _write_op(self, f, *args, **kwargs): # guard condition if self._read_only: err_read_only() if self._synchronizer is None: # no synchronization lock = nolock else: # synchronize on the root group lock = self._synchronizer[group_meta_key] with lock: return f(*args, **kwargs)
def __setitem__(self, item, value): """Modify data for some portion of the array. Examples -------- Setup a 1-dimensional array:: >>> import zarr >>> z = zarr.zeros(100000000, chunks=1000000, dtype='i4') >>> z Array((100000000,), int32, chunks=(1000000,), order=C) nbytes: 381.5M; nbytes_stored: ...; ratio: ...; initialized: 0/100 compressor: Blosc(cname='lz4', clevel=5, shuffle=SHUFFLE, blocksize=0) store: dict Set all array elements to the same scalar value:: >>> z[:] = 42 >>> z[:] array([42, 42, 42, ..., 42, 42, 42], dtype=int32) Set a portion of the array:: >>> z[:100] = np.arange(100) >>> z[-100:] = np.arange(100)[::-1] >>> z[:] array([0, 1, 2, ..., 2, 1, 0], dtype=int32) Setup a 2-dimensional array:: >>> z = zarr.zeros((10000, 10000), chunks=(1000, 1000), dtype='i4') >>> z Array((10000, 10000), int32, chunks=(1000, 1000), order=C) nbytes: 381.5M; nbytes_stored: ...; ratio: ...; initialized: 0/100 compressor: Blosc(cname='lz4', clevel=5, shuffle=SHUFFLE, blocksize=0) store: dict Set all array elements to the same scalar value:: >>> z[:] = 42 >>> z[:] array([[42, 42, 42, ..., 42, 42, 42], [42, 42, 42, ..., 42, 42, 42], [42, 42, 42, ..., 42, 42, 42], ..., [42, 42, 42, ..., 42, 42, 42], [42, 42, 42, ..., 42, 42, 42], [42, 42, 42, ..., 42, 42, 42]], dtype=int32) Set a portion of the array:: >>> z[0, :] = np.arange(z.shape[1]) >>> z[:, 0] = np.arange(z.shape[0]) >>> z[:] array([[ 0, 1, 2, ..., 9997, 9998, 9999], [ 1, 42, 42, ..., 42, 42, 42], [ 2, 42, 42, ..., 42, 42, 42], ..., [9997, 42, 42, ..., 42, 42, 42], [9998, 42, 42, ..., 42, 42, 42], [9999, 42, 42, ..., 42, 42, 42]], dtype=int32) """ # guard conditions if self._read_only: err_read_only() # refresh metadata if not self._cache_metadata: self._load_metadata_nosync() # normalize selection selection = normalize_array_selection(item, self._shape) # check value shape expected_shape = tuple(s.stop - s.start for s in selection if isinstance(s, slice)) if np.isscalar(value): pass elif expected_shape != value.shape: raise ValueError('value has wrong shape, expecting %s, found %s' % (str(expected_shape), str(value.shape))) # determine indices of chunks overlapping the selection chunk_range = get_chunk_range(selection, self._chunks) # iterate over chunks in range for cidx in itertools.product(*chunk_range): # determine chunk offset offset = [i * c for i, c in zip(cidx, self._chunks)] # determine required index range within chunk chunk_selection = tuple( slice(max(0, s.start - o), min(c, s.stop - o)) if isinstance(s, slice) else s - o for s, o, c in zip(selection, offset, self._chunks)) if np.isscalar(value): # put data self._chunk_setitem(cidx, chunk_selection, value) else: # assume value is array-like # determine index within value value_selection = tuple( slice(max(0, o - s.start), min(o + c - s.start, s.stop - s.start)) for s, o, c in zip(selection, offset, self._chunks) if isinstance(s, slice)) # put data self._chunk_setitem(cidx, chunk_selection, value[value_selection])
def __setitem__(self, key, value): if self.mode == 'r': err_read_only() value = ensure_bytes(value) self.zf.writestr(key, value)
def __setitem__(self, item, value): """Modify data for some portion of the array. Examples -------- Setup a 1-dimensional array:: >>> import zarr >>> z = zarr.zeros(100000000, chunks=1000000, dtype='i4') >>> z Array((100000000,), int32, chunks=(1000000,), order=C) nbytes: 381.5M; nbytes_stored: 301; ratio: 1328903.7; initialized: 0/100 compressor: Blosc(cname='lz4', clevel=5, shuffle=1) store: dict Set all array elements to the same scalar value:: >>> z[:] = 42 >>> z[:] array([42, 42, 42, ..., 42, 42, 42], dtype=int32) Set a portion of the array:: >>> z[:100] = np.arange(100) >>> z[-100:] = np.arange(100)[::-1] >>> z[:] array([0, 1, 2, ..., 2, 1, 0], dtype=int32) Setup a 2-dimensional array:: >>> z = zarr.zeros((10000, 10000), chunks=(1000, 1000), dtype='i4') >>> z Array((10000, 10000), int32, chunks=(1000, 1000), order=C) nbytes: 381.5M; nbytes_stored: 323; ratio: 1238390.1; initialized: 0/100 compressor: Blosc(cname='lz4', clevel=5, shuffle=1) store: dict Set all array elements to the same scalar value:: >>> z[:] = 42 >>> z[:] array([[42, 42, 42, ..., 42, 42, 42], [42, 42, 42, ..., 42, 42, 42], [42, 42, 42, ..., 42, 42, 42], ..., [42, 42, 42, ..., 42, 42, 42], [42, 42, 42, ..., 42, 42, 42], [42, 42, 42, ..., 42, 42, 42]], dtype=int32) Set a portion of the array:: >>> z[0, :] = np.arange(z.shape[1]) >>> z[:, 0] = np.arange(z.shape[0]) >>> z[:] array([[ 0, 1, 2, ..., 9997, 9998, 9999], [ 1, 42, 42, ..., 42, 42, 42], [ 2, 42, 42, ..., 42, 42, 42], ..., [9997, 42, 42, ..., 42, 42, 42], [9998, 42, 42, ..., 42, 42, 42], [9999, 42, 42, ..., 42, 42, 42]], dtype=int32) """ # guard conditions if self._read_only: err_read_only() # refresh metadata if not self._cache_metadata: self._load_metadata_nosync() # normalize selection selection = normalize_array_selection(item, self._shape) # check value shape expected_shape = tuple( s.stop - s.start for s in selection if isinstance(s, slice) ) if np.isscalar(value): pass elif expected_shape != value.shape: raise ValueError('value has wrong shape, expecting %s, found %s' % (str(expected_shape), str(value.shape))) # determine indices of chunks overlapping the selection chunk_range = get_chunk_range(selection, self._chunks) # iterate over chunks in range for cidx in itertools.product(*chunk_range): # determine chunk offset offset = [i * c for i, c in zip(cidx, self._chunks)] # determine required index range within chunk chunk_selection = tuple( slice(max(0, s.start - o), min(c, s.stop - o)) if isinstance(s, slice) else s - o for s, o, c in zip(selection, offset, self._chunks) ) if np.isscalar(value): # put data self._chunk_setitem(cidx, chunk_selection, value) else: # assume value is array-like # determine index within value value_selection = tuple( slice(max(0, o - s.start), min(o + c - s.start, s.stop - s.start)) for s, o, c in zip(selection, offset, self._chunks) if isinstance(s, slice) ) # put data self._chunk_setitem(cidx, chunk_selection, value[value_selection])