a
    ij*                     @  s0  d Z ddlmZ ddlZddlZddlmZmZmZ ddlm	Z	 ddl
ZejrZddlmZ edZeZdej ZeeeeefZeZG d	d
 d
eZG dd dZe ZdddddddZddddZdddddZ dddddZ!ej"dd Z#ej$dd dd!d"Z%ej$dddd#d"Z%d$d" Z%dS )%zUNumpy utils.

Attributes:
  tau: The circle constant (2 * pi). (https://tauday.com/)
    )annotationsN)AnyOptionalTypeVar)epy)Array_T   c                   @  s   e Zd ZddddZdS )_LazyArrayMetaboolreturnc                 C  s
   t |S N)lazyis_array)clsobj r   S/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/etils/enp/numpy_utils.py__instancecheck__6   s    z _LazyArrayMeta.__instancecheck__N)__name__
__module____qualname__r   r   r   r   r   r
   4   s   r
   c                   @  s  e Zd ZdZeddddZeddddZedddd	Zed
d Zedd Z	edd Z
edd Zedd Zedd ZdddddZdddddZdddddZdddddZd dd!d"d#Zd dd!d$d%Zd dd!d&d'Zd dd!d(d)Zd*d+d ddd,d-d.Zddd/d0Zddd1d2Zddd3d4Zddd5d6Zddd7d8Zd9d: Zd;d< Zd=d> Zd?d@ Ze dAddAdBdCZ!d*d+d ddDdEdFdGZ"d*d+d ddHdIdJZ#edddKdLZ$G dMdN dNe%dOZ&dPS )Q_LazyImporterzLazy import module.

  Help to write code seamlessly working with np, Jax and TF.
  Because libs are lazily imported, TF and Jax are always optional dependencies.

  r   r   c                 C  s
   dt jv S )Njaxsysmodulesselfr   r   r   has_jaxB   s    z_LazyImporter.has_jaxc                 C  s
   dt jv S )N
tensorflowr   r   r   r   r   has_tfF   s    z_LazyImporter.has_tfc                 C  s
   dt jv S )Ntorchr   r   r   r   r   	has_torchJ   s    z_LazyImporter.has_torchc                 C  s   dd l }|S Nr   )r   )r   r   r   r   r   r   N   s    z_LazyImporter.jaxc                 C  s   dd l m} |S r%   )Z	jax.numpynumpy)r   jnpr   r   r   r'   T   s    z_LazyImporter.jnpc                 C  s   dd l }|S r%   )r!   )r   r!   r   r   r   tfZ   s    z_LazyImporter.tfc                 C  s   dd l m  m} |S r%   )Ztensorflow.experimental.numpyZexperimentalr&   )r   tnpr   r   r   r)   `   s    z_LazyImporter.tnpc                 C  s   dd l }|S r%   )r#   )r   r#   r   r   r   r#   f   s    z_LazyImporter.torchc                 C  s   t S r   )npr   r   r   r   r*   l   s    z_LazyImporter.npNpModule)xnpr   c                 C  s   |t u S r   )_npr   r,   r   r   r   	is_np_xnpp   s    z_LazyImporter.is_np_xnpc                 C  s   | j o|| ju S r   )r"   r)   r.   r   r   r   	is_tf_xnps   s    z_LazyImporter.is_tf_xnpc                 C  s   | j o|| ju S r   )r    r'   r.   r   r   r   
is_jax_xnpv   s    z_LazyImporter.is_jax_xnpc                 C  s   | j o|| ju S r   )r$   r#   r.   r   r   r   is_torch_xnpy   s    z_LazyImporter.is_torch_xnpr   xr   c                 C  s   t |tjtjfS r   )
isinstancer*   ndarraygenericr   r4   r   r   r   is_np|   s    z_LazyImporter.is_npc                 C  s&   | j o$t|| jj| jj| jjjjfS r   )	r"   r5   r)   r6   r(   Z
TensorSpecZ__internal__typesTensorr8   r   r   r   is_tf   s    
z_LazyImporter.is_tfc                 C  s   | j ot|| jjS r   )r    r5   r'   r6   r8   r   r   r   is_jax   s    z_LazyImporter.is_jaxc                 C  s   | j ot|| jjS r   )r$   r5   r#   r;   r8   r   r   r   is_torch   s    z_LazyImporter.is_torchTstrict)r4   r@   r   c                C  s>   |rdnt |t}| |p<| |p<| |p<| |p<|S )NF)r5   _ARRAY_LIKE_TYPESr9   r=   r<   r>   )r   r4   r@   Zis_array_liker   r   r   r      s    
z_LazyImporter.is_arrayc                 C  s   t |tjpt|tjS r   )r5   r*   dtyper   
issubclassr7   r   rB   r   r   r   is_np_dtype   s    z_LazyImporter.is_np_dtypec                 C  s   | j ot|| jjjS r   )r"   r5   r(   dtypesZDTyperD   r   r   r   is_tf_dtype   s    z_LazyImporter.is_tf_dtypec                 C  s&   | j ot|t| jj}| |p$|S r   )r    r5   typer'   Zfloat32rE   )r   rB   Z	check_jaxr   r   r   is_jax_dtype   s    z_LazyImporter.is_jax_dtypec                 C  s   | j ot|| jjS r   )r$   r5   r#   rB   rD   r   r   r   is_torch_dtype   s    z_LazyImporter.is_torch_dtypec                 C  s(   |  |p&| |p&| |p&| |S r   )rE   rI   rG   rJ   rD   r   r   r   is_dtype   s    
z_LazyImporter.is_dtypec                 C  s`   |  |r|j}nD| |r4ddlm} ||}n"| |sV| |sVtd|t	
|S )Nr   compatzInvalid dtype: )rG   Zas_numpy_dtyperJ   	etils.enprM   Zdtype_torch_to_nprI   rE   	TypeErrorr*   rB   r   rB   rM   r   r   r   as_np_dtype   s    

z_LazyImporter.as_np_dtypec                 C  s   | j j| |S r   )r(   rF   as_dtyperQ   rD   r   r   r   as_tf_dtype   s    z_LazyImporter.as_tf_dtypec                 C  s
   |  |S r   )rQ   rD   r   r   r   as_jax_dtype   s    z_LazyImporter.as_jax_dtypec                 C  s   ddl m} || |S )Nr   rL   )rN   rM   Zdtype_np_to_torchrQ   rP   r   r   r   as_torch_dtype   s    z_LazyImporter.as_torch_dtype)r,   c                C  sb   |  |r| |S | |r(| |S | |r<| |S | |rP| |S td|dS )z'Normalize to dtype for the given `xnp`.zUnknown xnp: N)	r/   rQ   r0   rS   r1   rT   r2   rU   rO   )r   rB   r,   r   r   r   rR      s    







z_LazyImporter.as_dtypezOptional[_np.dtype])
array_liker@   r   c                C  sh   |  |r|j}nL|r,tdt| dn2t|tr>tj}n t|trLdS tdt| | 	|S )z,Returns the dtype associated with the array.z$Cannot extract dtype from non-array z, when strict=True.N)
r   rB   rO   rH   r5   r   r*   Zbool_rA   rR   )r   rV   r@   rB   r   r   r   dtype_from_array   s    


z_LazyImporter.dtype_from_array)r4   r@   c                C  sh   |  |r| jS | |r | jS | |r.tS | |r>| jS |sPt|t	rPtS t
dt|j dS )a  Returns the numpy module associated with the given array.

    Args:
      x: Either tf, jax or numpy array.
      strict: If `False`, default to `np.array` if the array can't be infered (
        to support array-like: list, tuple,...)

    Returns:
      The numpy module.
    z*Cannot infer the numpy module from array: N)r=   r'   r<   r)   r9   r*   r>   r#   r5   rA   rO   rH   r   )r   r4   r@   r   r   r   get_xnp   s    



z_LazyImporter.get_xnpc                 C  s   | j ot| jjdS )z(Returns `True` if numpy mode is enabled.Zreshape)r"   hasattrr(   r;   r   r   r   r   is_tnp_enabled  s    z_LazyImporter.is_tnp_enabledc                   @  s   e Zd ZdZdS )z_LazyImporter.LazyArrayzRepresent `tf.Tensor`, `jax.ndarray`, `np.ndarray`, `torch.Tensor`.

    Allow to check isinstance without triggering imports from other modules:

    ```
    assert isinstance(jnp.zeros((2,)), enp.lazy.LazyArray)
    ```
    N)r   r   r   __doc__r   r   r   r   	LazyArray  s   r\   )	metaclassN)'r   r   r   r[   propertyr    r"   r$   r   r'   r(   r)   r#   r*   r/   r0   r1   r2   r9   r<   r=   r>   r   rE   rG   rI   rJ   rK   rQ   rS   rT   rU   r-   rR   rW   rX   rZ   r
   r\   r   r   r   r   r   :   sX   







 r   Tr?   r   r   arrayr@   c                C  s   t j| |dS )a  Returns the numpy module associated with the given array.

  Args:
    array: Either tf, jax or numpy array.
    strict: If `False`, default to `np.array` if the array can't be infered (
      to support array-like: list, tuple,...)

  Returns:
    The numpy module.
  r?   )r   rX   r_   r   r   r   get_np_module   s    ra   r   c                 C  s:   zt | } W n ty"   Y dS 0 | jt jt jt jhv S )z#Returns True if the dtype is `str`.F)r*   rB   rO   rH   Zobject_Zstr_bytes_)rB   r   r   r   is_dtype_str.  s
    rc   r   r3   c                 C  s,   t | ttfrdS t| r$t| jS dS dS )zReturns True if the given array is a `str` array.

  Note: Also returns True for scalar `str`, `bytes` values. For compatibility
  with `tensor.numpy()` which returns `bytes`

  Args:
    x: The array to test

  Returns:
    True or False
  TFN)r5   bytesstrr   rc   rB   r4   r   r   r   is_array_str8  s
    
rg   c                 C  s0   t | tjrdS tjr(t | tjjr(dS dS dS )z-Returns `True` if array is np or `jnp` array.TFN)r5   r*   r6   r   r    r'   rf   r   r   r   r   M  s
    r   c                 C  s   t | tr| dS | S )zDecodes bytes -> str array.utf8)r5   rd   decoderf   r   r   r   _to_str_arrayW  s    rj   rd   re   c                 C  s   d S r   r   rf   r   r   r   normalize_bytes2str^  s    rk   c                 C  s   d S r   r   rf   r   r   r   rk   c  s    c                 C  s:   t | tr| S t | tr"| dS t| r2t| S | S dS )a)  Normalize `bytes` array to `str` (UTF-8).

  Example of usage:

  ```python
  for ex in tfds.as_numpy(ds):  # tf.data returns `tf.string` as `bytes`
    ex = tf.nest.map_structure(enp.normalize_bytes2str, ex)
  ```

  Args:
    x: Any array

  Returns:
    x: `bytes` array are decoded as `str`
  rh   N)r5   re   rd   ri   rg   rj   rf   r   r   r   rk   j  s    


)&r[   
__future__r   r   typingr   r   r   Zetilsr   r&   r*   TYPE_CHECKINGZetils.enp.typingr   r   r+   pitauintr   floatlisttuplerA   r-   rH   r
   r   r   ra   rc   rg   r   Z	vectorizerj   overloadrk   r   r   r   r   <module>   s8   
 d


