
    bi                    :    d dl mZ d dlmZ d ZddZ	 	 	 	 ddZy)	    )annotations)corec                j    | j                   t        fdt        j                        D              S )Nc              3  h   K   | ])  }j                   j                  |      j                   + y w)N)domaindimtile).0ischemas     O/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/dask/array/tiledb_io.py	<genexpr>z$_tiledb_to_chunks.<locals>.<genexpr>   s&     Fa!!!$))Fs   /2)r   listrangendim)tiledb_arrayr   s    @r   _tiledb_to_chunksr      s(      FF53EFFF    Nc                H   ddl }|xs
 t               }|j                  dd      }t        | |j                        r| }n|j                  | |||      }|j                  j                  rt        d      |sI|j                  j                  dkD  rt        d      |j                  j                  d      j                  }|j                  rt        d      |xs 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    > #-tvM


E4
(C#v||$kk#ImkM
zzABB::a> 
 

*//I
~~@AA-(-Fv;#**//)))??3[3->??r   c                j   ddl }|xs
 t               }|xs |j                  dd      }t        j                  | j
                        st        d      t        |t              r5| j
                  D 	cg c]  }	|	d   	 }
}	 |j                  || f|
||d|}n`t        ||j                        r?|}| j                  |j                  k(  r| j                  |j                  k(  st        d      t        d      |j                  r|j                  st        d      | j                  |d	||
      S c c}	w )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.)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(   r2   r3   r+   r   r,   r   r-   cr*   r.   s               r   	to_tiledbr<   J   s0   v #-tvM

/""5$/C%%fmm4>
 	

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

 JJ3>>HII<<%<VV+ /s   1D0)NNN)TFNN)
__future__r   
dask.arrayr   r   r/   r<    r   r   <module>r@      s-    " G
<@D ]Wr   