Example #1
0
def test_normalize_dimension_separator():
    assert None is normalize_dimension_separator(None)
    assert '/' == normalize_dimension_separator('/')
    assert '.' == normalize_dimension_separator('.')
    with pytest.raises(ValueError):
        normalize_dimension_separator('X')
Example #2
0
def create(shape,
           chunks=True,
           dtype=None,
           compressor='default',
           fill_value=0,
           order='C',
           store=None,
           synchronizer=None,
           overwrite=False,
           path=None,
           chunk_store=None,
           filters=None,
           cache_metadata=True,
           cache_attrs=True,
           read_only=False,
           object_codec=None,
           dimension_separator=None,
           **kwargs):
    """Create an array.

    Parameters
    ----------
    shape : int or tuple of ints
        Array shape.
    chunks : int or tuple of ints, optional
        Chunk shape. If True, will be guessed from `shape` and `dtype`. If
        False, will be set to `shape`, i.e., single chunk for the whole array.
        If an int, the chunk size in each dimension will be given by the value
        of `chunks`. Default is True.
    dtype : string or dtype, optional
        NumPy dtype.
    compressor : Codec, optional
        Primary compressor.
    fill_value : object
        Default value to use for uninitialized portions of the array.
    order : {'C', 'F'}, optional
        Memory layout to be used within each chunk.
    store : MutableMapping or string
        Store or path to directory in file system or name of zip file.
    synchronizer : object, optional
        Array synchronizer.
    overwrite : bool, optional
        If True, delete all pre-existing data in `store` at `path` before
        creating the array.
    path : string, optional
        Path under which array is stored.
    chunk_store : MutableMapping, optional
        Separate storage for chunks. If not provided, `store` will be used
        for storage of both chunks and metadata.
    filters : sequence of Codecs, optional
        Sequence of filters to use to encode chunk data prior to compression.
    cache_metadata : bool, optional
        If True, array configuration metadata will be cached for the
        lifetime of the object. If False, array metadata will be reloaded
        prior to all data access and modification operations (may incur
        overhead depending on storage and data access pattern).
    cache_attrs : bool, optional
        If True (default), user attributes will be cached for attribute read
        operations. If False, user attributes are reloaded from the store prior
        to all attribute read operations.
    read_only : bool, optional
        True if array should be protected against modification.
    object_codec : Codec, optional
        A codec to encode object arrays, only needed if dtype=object.
    dimension_separator : {'.', '/'}, optional
        Separator placed between the dimensions of a chunk.
        .. versionadded:: 2.8

    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>

    Create an array with different some different configuration options::

        >>> from numcodecs import Blosc
        >>> compressor = Blosc(cname='zstd', clevel=1, shuffle=Blosc.BITSHUFFLE)
        >>> z = zarr.create((10000, 10000), chunks=(1000, 1000), dtype='i1', order='F',
        ...                 compressor=compressor)
        >>> z
        <zarr.core.Array (10000, 10000) int8>

    To create an array with object dtype requires a filter that can handle Python object
    encoding, e.g., `MsgPack` or `Pickle` from `numcodecs`::

        >>> from numcodecs import MsgPack
        >>> z = zarr.create((10000, 10000), chunks=(1000, 1000), dtype=object,
        ...                 object_codec=MsgPack())
        >>> z
        <zarr.core.Array (10000, 10000) object>

    Example with some filters, and also storing chunks separately from metadata::

        >>> from numcodecs import Quantize, Adler32
        >>> store, chunk_store = dict(), dict()
        >>> z = zarr.create((10000, 10000), chunks=(1000, 1000), dtype='f8',
        ...                 filters=[Quantize(digits=2, dtype='f8'), Adler32()],
        ...                 store=store, chunk_store=chunk_store)
        >>> z
        <zarr.core.Array (10000, 10000) float64>

    """

    # handle polymorphic store arg
    store = normalize_store_arg(store)

    # API compatibility with h5py
    compressor, fill_value = _kwargs_compat(compressor, fill_value, kwargs)

    # optional array metadata
    if dimension_separator is None:
        dimension_separator = getattr(store, "_dimension_separator", None)
    else:
        if getattr(store, "_dimension_separator", None) != dimension_separator:
            raise ValueError(
                f"Specified dimension_separtor: {dimension_separator}"
                f"conflicts with store's separator: "
                f"{store._dimension_separator}")
    dimension_separator = normalize_dimension_separator(dimension_separator)

    # initialize array metadata
    init_array(store,
               shape=shape,
               chunks=chunks,
               dtype=dtype,
               compressor=compressor,
               fill_value=fill_value,
               order=order,
               overwrite=overwrite,
               path=path,
               chunk_store=chunk_store,
               filters=filters,
               object_codec=object_codec,
               dimension_separator=dimension_separator)

    # instantiate array
    z = Array(store,
              path=path,
              chunk_store=chunk_store,
              synchronizer=synchronizer,
              cache_metadata=cache_metadata,
              cache_attrs=cache_attrs,
              read_only=read_only)

    return z