
    &`i!                        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rd dlmZ  ed           G d	 d
e                      Z ed          dedefd            ZdedefdZdeee         e	j        ef         dedefdZd ZdedefdZde	j        defdZdee	j        dee         f         de	j        fdZ ed          dee         de	j        fd            ZdS )    N)TYPE_CHECKINGAnyListProtocolSequenceUnion)TENSOR_COLUMN_NAME)	PublicAPI)DeveloperAPI)	ABCSeriesbeta)	stabilityc                       e Zd ZdZd Zd ZdS )	ArrayLikez:Protocol matching ndarray-like objects (like torch.Tensor)c                     d S N selfs    x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/air/util/tensor_extensions/utils.py	__array__zArrayLike.__array__           c                     d S r   r   r   s    r   __len__zArrayLike.__len__   r   r   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s8        DD      r   r   valuereturnc                 x    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    s    r   is_ndarray_liker'       s?    
 	{## 	.E9%%	.5"*---r   c                 B    dd l }t          | |j        |j        f          S Nr   )pyarrowr$   ArrayChunkedArray)r    pas     r   _is_arrow_arrayr.   +   s'    ebh8999r   column_valuescolumn_namec                     t          |           dk    sJ |t          k    pRt          |           pCt          |           p4t	          | d         t
          j                  pt          | d                   S r)   )lenr	   _is_ndarray_tensor"_is_ndarray_like_not_pyarrow_arrayr$   r%   r&   )r/   r0   s     r   _should_convert_to_tensorr5   1   s     }!!!! 	)) 	@ m,,	@ .m<<	@ mA&
33	@ .mA.>??r   c                 @    t          |           ot          |            S r   )r'   r.   )r/   s    r   r4   r4   K   s     =))P/-2P2P.PPr   tc                     t          | t          j                  sdS | j        dk    rdS | j        j        t          j        u r5t          |           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_r2   )r7   s    r   r3   r3   O   sn     a$$ u 	vzzt 
	#	#A

z!A$
7S7S
t5r   arrc                 2   | 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                  s dS |j        |k    r dS .dS )zReturn whether the provided NumPy ndarray is comprised of variable-shaped
    tensors.

    NOTE: This is an O(rows) check.
    Fr   r9   NT)r;   r<   r%   r=   r2   r$   r&   shape)r>   r@   as      r   "_is_ndarray_variable_shaped_tensorrB   c   s     y~RZ''u
3xx1}}uc!fbj)) uFLEW  !RZ(( 	557e44 4r   valuesr   c                    	 t          j                    5  t          j                            t          j                  dk    rd}t          j        j        }nd}t          j        }t          j        d|           t          j	        | |          }|cddd           S # 1 swxY w Y   dS # t          $ r3}t          |          }d|v sd|v rt          |           cY d}~S |dd}~ww xY 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)rC   copy_if_neededwarning_typer>   e	error_strs         r   _create_possibly_ragged_ndarrayrX   x   sQ   !$&& 	 	
 v""2>22g==!%!}F!&!;!(\BBBB(6777C	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	     FF	8IEE?9LL )00000000 #sG   B, A=BB, B##B, &B#'B, ,
C)6%C$C)!C$$C)alphac                 ~    t          j        t          |           t                    }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.
    )r;   N)r%   emptyr2   objectlist)rC   r>   s     r   rS   rS      s7    X (3v;;f
-
-
-C&\\CFJr   )rH   typingr   r   r   r   r   r   numpyr%   ray.air.constantsr	   ray.utilr
   ray.util.annotationsr   pandas.core.dtypes.genericr   r   boolr'   r.   r&   rR   r5   r4   r3   rB   rX   rS   r   r   r   <module>re      sy                       0 0 0 0 0 0       - - - - - - 5444444          9      :9 : : : : :cBJ	9:IL	   4Q Q Q# $    (BJ 4    *+"*k8C=89+Z+ + + +\ W.(3- .BJ . . . . . .r   