U
    /e]                     @   s,   d dl mZ dd ZdddZdd	d
ZdS )    )corec                    s"   | j  t fddt jD S )Nc                 3   s   | ]} j |jV  qd S )N)domainZdimtile).0ischema 8/tmp/pip-unpacked-wheel-dbjnr7gq/dask/array/tiledb_io.py	<genexpr>   s     z$_tiledb_to_chunks.<locals>.<genexpr>)r   listrangendim)Ztiledb_arrayr	   r   r
   _tiledb_to_chunks   s    r   Nc           	      K   s   ddl }|pt }|dd}t| |jr0| }n|j| |||d}|jjrRtd|sz|jj	dkrlt
dn|jdj}|jrtd|pt|}t||jjksttj||d	|  d
S )a  Load array from the TileDB storage format

    See https://docs.tiledb.io for more information about TileDB.

    Parameters
    ----------
    uri: TileDB array or str
        Location to save the data
    attribute: str or None
        Attribute selection (single-attribute view on multi-attribute array)


    Returns
    -------

    A Dask Array

    Examples
    --------

    >>> import tempfile, tiledb
    >>> import dask.array as da, numpy as np
    >>> uri = tempfile.NamedTemporaryFile().name
    >>> _ = tiledb.from_numpy(uri, np.arange(0,9).reshape(3,3))  # create a tiledb array
    >>> tdb_ar = da.from_tiledb(uri)  # read back the array
    >>> tdb_ar.shape
    (3, 3)
    >>> tdb_ar.mean().compute()
    4.0
    r   Nkey)attrconfigr   z&Sparse TileDB arrays are not supported   zOkeyword 'attribute' must be providedwhen loading a multi-attribute TileDB arrayz%TileDB array must be open for readingz	tiledb-%s)name)tiledbdictpop
isinstanceArrayopenr   sparse
ValueErrorZnattr	TypeErrorr   r   
iswritabler   lenr   AssertionErrorr   Z
from_array)	uri	attributechunksstorage_optionskwargsr   tiledb_configr   tdbr	   r	   r
   from_tiledb	   s&    
r(   TFc                 K   s   ddl }|pt }|p |dd}t| js6tdt|trndd | jD }	|j	|| f|	||d|}
n:t||j
r|}
| j|
jkr| j|
jkstdntd	|
jr|
jstd
| j|
d||dS )a  Save array to the TileDB storage format

    Save 'array' using the TileDB storage manager, to any TileDB-supported URI,
    including local disk, S3, or HDFS.

    See https://docs.tiledb.io for more information about TileDB.

    Parameters
    ----------

    darray: dask.array
        A dask array to write.
    uri:
        Any supported TileDB storage location.
    storage_options: dict
        Dict containing any configuration options for the TileDB backend.
        see https://docs.tiledb.io/en/stable/tutorials/config.html
    compute, return_stored: see ``store()``
    key: str or None
        Encryption key

    Returns
    -------

    None
        Unless ``return_stored`` is set to ``True`` (``False`` by default)

    Notes
    -----

    TileDB only supports regularly-chunked arrays.
    TileDB `tile extents`_ correspond to form 2 of the dask
    `chunk specification`_, and the conversion is
    done automatically for supported arrays.

    Examples
    --------

    >>> import dask.array as da, tempfile
    >>> uri = tempfile.NamedTemporaryFile().name
    >>> data = da.random.random(5,5)
    >>> da.to_tiledb(data, uri)
    >>> import tiledb
    >>> tdb_ar = tiledb.open(uri)
    >>> all(tdb_ar == data)
    True

    .. _chunk specification: https://docs.tiledb.io/en/stable/tutorials/tiling-dense.html
    .. _tile extents: http://docs.dask.org/en/latest/array-chunks.html
    r   Nr   z^Attempt to save array to TileDB with irregular chunking, please call `arr.rechunk(...)` first.c                 S   s   g | ]}|d  qS )r   r	   )r   cr	   r	   r
   
<listcomp>   s     zto_tiledb.<locals>.<listcomp>)r   r   r   z>Target TileDB array layout is not compatible with source arrayzc'uri' must be string pointing to supported TileDB store location or an open, writable TileDB array.z-Target TileDB array is not open and writable.F)lockcomputereturn_stored)r   r   r   r   Z_check_regular_chunksr#   r   r   strZ
empty_liker   Zdtyper   Zisopenr   store)Zdarrayr!   r,   r-   r$   r   r%   r   r&   r#   r'   r	   r	   r
   	to_tiledbH   s>    ;

   r0   )NNN)TFNN)Z
dask.arrayr   r   r(   r0   r	   r	   r	   r
   <module>   s   
B    