Array creation (zarr.creation)

zarr.creation.create(shape, chunks, dtype=None, compression='default', compression_opts=None, fill_value=None, order='C', store=None, synchronizer=None, overwrite=False)

Create an array.

Parameters:

shape : int or tuple of ints

Array shape.

chunks : int or tuple of ints

Chunk shape.

dtype : string or dtype, optional

NumPy dtype.

compression : string, optional

Name of primary compression library, e.g., ‘blosc’, ‘zlib’, ‘bz2’, ‘lzma’.

compression_opts : object, optional

Options to primary compressor. E.g., for blosc, provide a dictionary with keys ‘cname’, ‘clevel’ and ‘shuffle’.

fill_value : object

Default value to use for uninitialised portions of the array.

order : {‘C’, ‘F’}, optional

Memory layout to be used within each chunk.

store : MutableMapping, optional

Array storage. If not provided, a Python dict will be used, meaning array data will be stored in memory.

synchronizer : zarr.sync.ArraySynchronizer, optional

Array synchronizer.

overwrite : bool, optional

If True, delete all pre-existing data in store before creating the array.

Returns:

z : zarr.core.Array

Examples

Create an array with default settings:

>>> import zarr
>>> z = zarr.create((10000, 10000), chunks=(1000, 1000))
>>> z
zarr.core.Array((10000, 10000), float64, chunks=(1000, 1000), order=C)
  compression: blosc; compression_opts: {'clevel': 5, 'cname': 'lz4', 'shuffle': 1}
  nbytes: 762.9M; nbytes_stored: 316; ratio: 2531645.6; initialized: 0/100
  store: builtins.dict
zarr.creation.empty(shape, chunks, dtype=None, compression='default', compression_opts=None, order='C', store=None, synchronizer=None)

Create an empty array.

For parameter definitions see zarr.creation.create().

Notes

The contents of an empty Zarr array are not defined. On attempting to retrieve data from an empty Zarr array, any values may be returned, and these are not guaranteed to be stable from one access to the next.

zarr.creation.zeros(shape, chunks, dtype=None, compression='default', compression_opts=None, order='C', store=None, synchronizer=None)

Create an array, with zero being used as the default value for uninitialised portions of the array.

For parameter definitions see zarr.creation.create().

Examples

>>> import zarr
>>> z = zarr.zeros((10000, 10000), chunks=(1000, 1000))
>>> z
zarr.core.Array((10000, 10000), float64, chunks=(1000, 1000), order=C)
  compression: blosc; compression_opts: {'clevel': 5, 'cname': 'lz4', 'shuffle': 1}
  nbytes: 762.9M; nbytes_stored: 313; ratio: 2555910.5; initialized: 0/100
  store: builtins.dict
>>> z[:2, :2]
array([[ 0.,  0.],
       [ 0.,  0.]])
zarr.creation.ones(shape, chunks, dtype=None, compression='default', compression_opts=None, order='C', store=None, synchronizer=None)

Create an array, with one being used as the default value for uninitialised portions of the array.

For parameter definitions see zarr.creation.create().

Examples

>>> import zarr
>>> z = zarr.ones((10000, 10000), chunks=(1000, 1000))
>>> z
zarr.core.Array((10000, 10000), float64, chunks=(1000, 1000), order=C)
  compression: blosc; compression_opts: {'clevel': 5, 'cname': 'lz4', 'shuffle': 1}
  nbytes: 762.9M; nbytes_stored: 313; ratio: 2555910.5; initialized: 0/100
  store: builtins.dict
>>> z[:2, :2]
array([[ 1.,  1.],
       [ 1.,  1.]])
zarr.creation.full(shape, chunks, fill_value, dtype=None, compression='default', compression_opts=None, order='C', store=None, synchronizer=None)

Create an array, with fill_value being used as the default value for uninitialised portions of the array.

For parameter definitions see zarr.creation.create().

Examples

>>> import zarr
>>> z = zarr.full((10000, 10000), chunks=(1000, 1000), fill_value=42)
>>> z
zarr.core.Array((10000, 10000), float64, chunks=(1000, 1000), order=C)
  compression: blosc; compression_opts: {'clevel': 5, 'cname': 'lz4', 'shuffle': 1}
  nbytes: 762.9M; nbytes_stored: 314; ratio: 2547770.7; initialized: 0/100
  store: builtins.dict
>>> z[:2, :2]
array([[ 42.,  42.],
       [ 42.,  42.]])
zarr.creation.array(data, chunks=None, dtype=None, compression='default', compression_opts=None, fill_value=None, order='C', store=None, synchronizer=None)

Create an array filled with data.

The data argument should be a NumPy array or array-like object. For other parameter definitions see zarr.creation.create().

Examples

>>> import numpy as np
>>> import zarr
>>> a = np.arange(100000000).reshape(10000, 10000)
>>> z = zarr.array(a, chunks=(1000, 1000))
>>> z
zarr.core.Array((10000, 10000), int64, chunks=(1000, 1000), order=C)
  compression: blosc; compression_opts: {'clevel': 5, 'cname': 'lz4', 'shuffle': 1}
  nbytes: 762.9M; nbytes_stored: 17.1M; ratio: 44.7; initialized: 100/100
  store: builtins.dict
zarr.creation.open(path, mode='a', shape=None, chunks=None, dtype=None, compression='default', compression_opts=None, fill_value=0, order='C', synchronizer=None)

Open an array stored in a directory on the file system.

Parameters:

path : string

Path to directory in which to store the array.

mode : {‘r’, ‘r+’, ‘a’, ‘w’, ‘w-‘}

Persistence mode: ‘r’ means readonly (must exist); ‘r+’ means read/write (must exist); ‘a’ means read/write (create if doesn’t exist); ‘w’ means create (overwrite if exists); ‘w-‘ means create (fail if exists).

shape : int or tuple of ints

Array shape.

chunks : int or tuple of ints

Chunk shape.

dtype : string or dtype, optional

NumPy dtype.

compression : string, optional

Name of primary compression library, e.g., ‘blosc’, ‘zlib’, ‘bz2’, ‘lzma’.

compression_opts : object, optional

Options to primary compressor. E.g., for blosc, provide a dictionary with keys ‘cname’, ‘clevel’ and ‘shuffle’.

fill_value : object

Default value to use for uninitialised portions of the array.

order : {‘C’, ‘F’}, optional

Memory layout to be used within each chunk.

synchronizer : zarr.sync.ArraySynchronizer, optional

Array synchronizer.

Returns:

z : zarr.core.Array

Notes

There is no need to close an array. Data are automatically flushed to the file system.

Examples

>>> import numpy as np
>>> import zarr
>>> z1 = zarr.open('example.zarr', mode='w', shape=(10000, 10000),
...                chunks=(1000, 1000), fill_value=0)
>>> z1[:] = np.arange(100000000).reshape(10000, 10000)
>>> z1
zarr.core.Array((10000, 10000), float64, chunks=(1000, 1000), order=C)
  compression: blosc; compression_opts: {'clevel': 5, 'cname': 'lz4', 'shuffle': 1}
  nbytes: 762.9M; nbytes_stored: 24.8M; ratio: 30.8; initialized: 100/100
  store: zarr.storage.DirectoryStore
>>> z2 = zarr.open('example.zarr', mode='r')
>>> z2
zarr.core.Array((10000, 10000), float64, chunks=(1000, 1000), order=C)
  compression: blosc; compression_opts: {'clevel': 5, 'cname': 'lz4', 'shuffle': 1}
  nbytes: 762.9M; nbytes_stored: 24.8M; ratio: 30.8; initialized: 100/100
  store: zarr.storage.DirectoryStore
>>> np.all(z1[:] == z2[:])
True
zarr.creation.empty_like(a, shape=None, chunks=None, dtype=None, compression=None, compression_opts=None, order=None, store=None, synchronizer=None)

Create an empty array like a.

zarr.creation.zeros_like(a, shape=None, chunks=None, dtype=None, compression=None, compression_opts=None, order=None, store=None, synchronizer=None)

Create an array of zeros like a.

zarr.creation.ones_like(a, shape=None, chunks=None, dtype=None, compression=None, compression_opts=None, order=None, store=None, synchronizer=None)

Create an array of ones like a.

zarr.creation.full_like(a, shape=None, chunks=None, fill_value=None, dtype=None, compression=None, compression_opts=None, order=None, store=None, synchronizer=None)

Create a filled array like a.

zarr.creation.open_like(a, path, mode='a', shape=None, chunks=None, dtype=None, compression=None, compression_opts=None, fill_value=None, order=None, synchronizer=None)

Open a persistent array like a.