a
    h                     @   s   d Z ddlZddlZddlmZ dd ZG dd deeZG d	d
 d
e	e
Zdd Zdd Zd'ddZd(ddZdd Zdd Zdd ZddddZdd Zdd  Zd!d" Zd)d#d$Zd%d& ZdS )*zLAssorted utilities, which do not need anything other then torch and stdlib.
    N   )_dtypes_implc                 C   s4   t | trdS zt|  W n ty.   Y dS 0 dS )NFT)
isinstancestrlen	Exception)seq r	   @/var/www/auris/lib/python3.9/site-packages/torch/_numpy/_util.pyis_sequence   s    
r   c                   @   s   e Zd ZdS )	AxisErrorN__name__
__module____qualname__r	   r	   r	   r
   r      s   r   c                   @   s   e Zd ZdS )UFuncTypeErrorNr   r	   r	   r	   r
   r      s   r   c                 C   s    |d ur| j |kr| |} | S N)dtypeto)tensorr   r	   r	   r
   cast_if_needed    s    
r   c                 C   s$   t | jdk r | t  j} | S )N   )r   Z	_categoryr   r   Zdefault_dtypesZfloat_dtype)xr	   r	   r
   cast_int_to_float'   s    r   c                 C   s@   | |   kr|k s,n t d|  d| | dk r<| |7 } | S )Nzaxis z) is out of bounds for array of dimension r   )r   )axndimargnamer	   r	   r
   normalize_axis_index/   s
    r   Fc                    s   t | ttfvr4zt| g} W n ty2   Y n0 t fdd| D } |stttt	| t| kr rt
d  dnt
d| S )a  
    Normalizes an axis argument into a tuple of non-negative integer axes.

    This handles shorthands such as ``1`` and converts them to ``(1,)``,
    as well as performing the handling of negative indices covered by
    `normalize_axis_index`.

    By default, this forbids axes from being specified multiple times.
    Used internally by multi-axis-checking logic.

    Parameters
    ----------
    axis : int, iterable of int
        The un-normalized index or indices of the axis.
    ndim : int
        The number of dimensions of the array that `axis` should be normalized
        against.
    argname : str, optional
        A prefix to put before the error message, typically the name of the
        argument.
    allow_duplicate : bool, optional
        If False, the default, disallow an axis from being specified twice.

    Returns
    -------
    normalized_axes : tuple of int
        The normalized axis index, such that `0 <= normalized_axis < ndim`
    c                    s   g | ]}t | qS r	   )r   .0r   r   r   r	   r
   
<listcomp>\       z(normalize_axis_tuple.<locals>.<listcomp>zrepeated axis in `z
` argumentzrepeated axis)typetuplelistoperatorindex	TypeErrorr   setmapint
ValueError)axisr   r   Zallow_duplicater	   r    r
   normalize_axis_tuple8   s    r.   c                 C   s(   | d u r| S t | dkr td| d S )Nr   zdoes not handle tuple axisr   )r   NotImplementedErrorr-   r	   r	   r
   allow_only_single_axise   s
    r1   c                    sT   t  ttfvr f t t|  }t | t|  fddt|D }|S )Nc                    s    g | ]}| v rd nt qS r   )nextr   r-   Zshape_itr	   r
   r!   t   r"   z expand_shape.<locals>.<listcomp>)r#   r%   r$   r   r.   iterrange)Z	arr_shaper-   Zout_ndimshaper	   r4   r
   expand_shapem   s    
r8   c                 C   s:   |d u r d| }|  | } nt| j|}| |} | S )Nr2   )expand
contiguousr8   r7   Zreshape)r   r-   r   r7   r	   r	   r
   apply_keepdimsx   s    
r;   r0   c                 G   s.   | du r"t dd |D }|dfS || fS dS )z#Flatten the arrays if axis is None.Nc                 s   s   | ]}|  V  qd S r   )flatten)r   arr	   r	   r
   	<genexpr>   r"   z$axis_none_flatten.<locals>.<genexpr>r   r$   )r-   tensorsr	   r	   r
   axis_none_flatten   s    rA   c                 C   s>   t j}|| j||ds4td| j d| d| dt| |S )a  Dtype-cast tensor to target_dtype.

    Parameters
    ----------
    t : torch.Tensor
        The tensor to cast
    target_dtype : torch dtype object
        The array dtype to cast all tensors to
    casting : str
        The casting mode, see `np.can_cast`

     Returns
     -------
    `torch.Tensor` of the `target_dtype` dtype

     Raises
     ------
     ValueError
        if the argument cannot be cast according to the `casting` rule

    )castingzCannot cast array data from z to z according to the rule '')r   Zcan_cast_implr   r(   r   )ttarget_dtyperB   Zcan_castr	   r	   r
   typecast_tensor   s    
rF   c                    s   t  fdd| D S )Nc                 3   s   | ]}t | V  qd S r   )rF   )r   rD   rB   rE   r	   r
   r>      r"   z#typecast_tensors.<locals>.<genexpr>r?   )r@   rE   rB   r	   rG   r
   typecast_tensors   s    rH   c              
   C   sV   zt | }W nB tyP } z*d|  dt| d}t|W Y d }~n
d }~0 0 |S )Nzfailed to convert z! to ndarray. 
Internal error is: .)torchZ	as_tensorr   r   r/   )objr   eZmesgr	   r	   r
   _try_convert_to_tensor   s    rM   c              	   C   s   t | tjr| }n>t }tttj zt| }W t| nt| 0 t	||}||j
 }|dkr|d| |j }|r| }|S )a  The core logic of the array(...) function.

    Parameters
    ----------
    obj : tensor_like
        The thing to coerce
    dtype : torch.dtype object or None
        Coerce to this torch dtype
    copy : bool
        Copy or not
    ndmin : int
        The results as least this many dimensions
    is_weak : bool
        Whether obj is a weakly typed python scalar.

    Returns
    -------
    tensor : torch.Tensor
        a tensor object with requested dtype, ndim and copy semantics.

    Notes
    -----
    This is almost a "tensor_like" coersion function. Does not handle wrapper
    ndarrays (those should be handled in the ndarray-aware layer prior to
    invoking this function).
    r   r2   )r   rJ   ZTensorZget_default_dtypeZset_default_dtyper   Zget_default_dtype_forfloat32rM   r   r   viewr7   clone)rK   r   copyZndminr   Zdefault_dtypeZ
ndim_extrar	   r	   r
   _coerce_to_tensor   s    


rR   c                  G   s   ddl m} t| dkrt S t| dkrz| d }t||rB|jS t|trtg }|D ]}t|}|| qTt|S |S nt| tsJ t| S dS )zHConvert all ndarrays from `inputs` to tensors. (other things are intact)r   )ndarrayr   N)	Z_ndarrayrS   r   r,   r   r   r$   ndarrays_to_tensorsappend)inputsrS   Zinput_resultZ	sub_inputZ
sub_resultr	   r	   r
   rT      s     

rT   )N)NF)NFr   )__doc__r&   rJ    r   r   r,   
IndexErrorr   r(   RuntimeErrorr   r   r   r   r.   r1   r8   r;   rA   rF   rH   rM   rR   rT   r	   r	   r	   r
   <module>   s&   

	
-	 	
8