
    Cd                    <    d dl mZ d dlmZ d Zd	dZ	 	 	 	 d
dZdS )    )annotations)corec                l    | j         t          fdt          j                  D                       S )Nc              3  V   K   | ]#}j                             |          j        V  $d S )N)domaindimtile).0ischemas     4lib/python3.11/site-packages/dask/array/tiledb_io.py	<genexpr>z$_tiledb_to_chunks.<locals>.<genexpr>   s6      FFa!!!$$)FFFFFF    )r   listrangendim)tiledb_arrayr   s    @r   _tiledb_to_chunksr      s8     FFFFF53E3EFFFFFFr   Nc                .   ddl }|pt                      }|                    dd          }t          | |j                  r| }n|                    | |||          }|j        j        rt          d          |s>|j        j	        dk    rt          d          |j                            d          j        }|j        rt          d          |pt          |          }t          |          |j        j        k    sJ t#          j        ||d	| z  
          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
ValueErrornattr	TypeErrorr   r   
iswritabler   lenr   r   
from_array)	uri	attributechunksstorage_optionskwargsr   tiledb_configr   tdbs	            r   from_tiledbr/      s-   > MMM#-tvvM


E4
(
(C#v|$$ Nkk#ImkMM
z CABBB 0:a>  
 
**/I
~ B@AAA-(--Fv;;#*/))))?3[3->????r   TFc                6   ddl }|pt                      }|p|                    dd          }t          j        | j                  st          d          t          |t                    r%d | j        D             }	 |j	        || f|	||d|}
nVt          ||j
                  r2|}
| j        |
j        k    r| j        |
j        k    st          d          nt          d          |
j        r|
j        st          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                    g | ]
}|d          S )r    )r
   cs     r   
<listcomp>zto_tiledb.<locals>.<listcomp>   s    ...1!A$...r   )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   _check_regular_chunksr*   r"   r   str
empty_liker   dtyper   isopenr%   store)darrayr(   r6   r7   r+   r   r,   r   r-   r*   r.   s              r   	to_tiledbr?   J   sk   v MMM#-tvvM

/""5$//C%fm44 
>
 
 	

 #s 
.....f
$]
 
GM
 
 
C	&	& 
**1H1HP   2I
 1
 
 	

 J J3> JHIII<<%<VVVr   )NNN)TFNN)
__future__r   
dask.arrayr   r   r/   r?   r2   r   r   <module>rB      s    " " " " " "      G G G
<@ <@ <@ <@D ]W ]W ]W ]W ]W ]Wr   