o
    bi                     @   sJ  d dl Z d dlmZmZmZmZmZmZ d dlZ	d dl
mZ d dlmZ d dlmZ er2d dlmZ eddG d	d
 d
eZedddedefddZdedefddZdeee e	jef dedefddZdd ZdedefddZde	jdefddZdee	jdee f de	jfdd Zed!ddee de	jfd"d#ZdS )$    N)TYPE_CHECKINGAnySequenceUnionListProtocol)TENSOR_COLUMN_NAME)	PublicAPI)DeveloperAPI)	ABCSeriesbeta)	stabilityc                   @   s    e Zd ZdZdd Zdd ZdS )	ArrayLikez:Protocol matching ndarray-like objects (like torch.Tensor)c                 C      d S N selfr   r   X/home/ubuntu/.local/lib/python3.10/site-packages/ray/air/util/tensor_extensions/utils.py	__array__      zArrayLike.__array__c                 C   r   r   r   r   r   r   r   __len__   r   zArrayLike.__len__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   valuereturnc                 C   s"   t | dot | dot| tj S )z]Checks whether objects are ndarray-like (for ex, torch.Tensor)
    but NOT and ndarray itselfr   r   )hasattr
isinstancenpndarray)r   r   r   r   is_ndarray_like   s
   
r"   c                 C   s   dd l }t| |j|jfS Nr   )pyarrowr   ArrayChunkedArray)r   par   r   r   _is_arrow_array$   s   r(   column_valuescolumn_namec                 C   sD   t | dksJ |tkp!t| p!t| p!t| d tjp!t| d S r#   )lenr   _is_ndarray_tensor"_is_ndarray_like_not_pyarrow_arrayr   r    r!   )r)   r*   r   r   r   _should_convert_to_tensor*   s   
r.   c                 C   s   t | ot|  S r   )r"   r(   )r)   r   r   r   r-   D   s   r-   tc                 C   sP   t | tjsdS | jdkrdS | jjtju r&t| dkr&t | d tjr&dS dS )zReturn whether provided ndarray is a tensor (ie ndim > 1).

    NOTE: Tensor is defined as a NumPy array such that `len(arr.shape) > 1`
    F   Tr   )r   r    r!   ndimdtypetypeobject_r+   )r/   r   r   r   r,   H   s   
*r,   arrc                 C   sx   | j jtjur	dS t| dkrdS t| d tjsdS | d j}| dd D ]}t|tjs1 dS |j|kr9 dS q&dS )zReturn whether the provided NumPy ndarray is comprised of variable-shaped
    tensors.

    NOTE: This is an O(rows) check.
    Fr   r0   NT)r2   r3   r    r4   r+   r   r!   shape)r5   r6   ar   r   r   "_is_ndarray_variable_shaped_tensor\   s   

r8   valuesr   c              
   C   s   z=t  . tjtjdkrd}tjj}nd}tj}t jd|d tj	| |d}|W  d   W S 1 s6w   Y  W dS  t
yb } zt|}d|v sQd|v r[t| W  Y d}~S |dd}~ww )	a;  
    Create a possibly ragged ndarray.
    Using the np.array() constructor will fail to construct a ragged ndarray that has a
    uniform first dimension (e.g. uniform channel dimension in imagery). This function
    catches this failure and tries a create-and-fill method to construct the ragged
    ndarray.
    z2.0.0NFignore)category)copyz*could not broadcast input array from shapez.The requested array has an inhomogeneous shape)warningscatch_warningsr    libNumpyVersion__version__
exceptionsVisibleDeprecationWarningsimplefilterarray
ValueErrorstrcreate_ragged_ndarray)r9   copy_if_neededwarning_typer5   e	error_strr   r   r   _create_possibly_ragged_ndarrayq   s&   


(rM   alphac                 C   s&   t jt| td}t| |dd< |S )a  Create an array that contains arrays of different length

    If you're working with variable-length arrays like images, use this function to
    create ragged arrays instead of ``np.array``.

    .. note::
        ``np.array`` fails to construct ragged arrays if the input arrays have a uniform
        first dimension:

        .. testsetup::

            import numpy as np
            from ray.air.util.tensor_extensions.utils import create_ragged_ndarray

        .. doctest::

            >>> values = [np.zeros((3, 1)), np.zeros((3, 2))]
            >>> np.array(values, dtype=object)
            Traceback (most recent call last):
                ...
            ValueError: could not broadcast input array from shape (3,1) into shape (3,)
            >>> create_ragged_ndarray(values)
            array([array([[0.],
                          [0.],
                          [0.]]), array([[0., 0.],
                                         [0., 0.],
                                         [0., 0.]])], dtype=object)

        Or if you're creating a ragged array from a single array:

        .. doctest::

            >>> values = [np.zeros((3, 1))]
            >>> np.array(values, dtype=object)[0].dtype
            dtype('O')
            >>> create_ragged_ndarray(values)[0].dtype
            dtype('float64')

        ``create_ragged_ndarray`` avoids the limitations of ``np.array`` by creating an
        empty array and filling it with pointers to the variable-length arrays.
    )r2   N)r    emptyr+   objectlist)r9   r5   r   r   r   rH      s   ,rH   )r=   typingr   r   r   r   r   r   numpyr    ray.air.constantsr   ray.utilr	   ray.util.annotationsr
   pandas.core.dtypes.genericr   r   boolr"   r(   r!   rG   r.   r-   r,   r8   rM   rH   r   r   r   r   <module>   s<     



.