a
    uDfJ                  	   @  sr  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZ	d dl
mZmZ d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZmZmZ d	d
 Zd:ddZdd Zd;ddZdd Zdd Z dd Z!d<ddZ"d=ddZ#d>ddZ$d?d d!Z%ej&fd"d#Z'd$d% Z(d&d' Z)d(d) Z*d*d+ Z+d,d- Z,d.d/ Z-d0d1 Z.d@d2d3Z/d4d5 Z0dAd6d7Z1d8d9 Z2dS )B    )annotationsN)concatfrequencies)Array)	AxisError)is_dask_collectiontokenize)HighLevelGraph)has_keywordis_arraylikeis_cupy_typetypenamec                 C  s   t | r|  S | S d S N)r   getx r   ]/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/dask/array/utils.pynormalize_to_array   s    r   c              
     s  t | dr t| r t| r | j} |du r8| du r8tdt| rLt| } | du r\tj} n|du rtt | drt| j	}t
| tr| d|pd |d} t
| tst
| trdd	 | D }d
d	 t| |D }t
| tr|S t| S t | drt | drt
| jts| S |du r| j}z| tdd t| jD  }|j|kr|| jkr|tftdd t||j D   }|tdd t|jD  }n"|dkr| }n|d| }|tjju rtjjtjd| |p| j	ddd}W n. ty   tjd| |p| j	d}Y n0 t|r0t|}|r|j	|krz||}W nb ty   zHt fdddD r|j	jdv rtg |}n W Y d  n
d  0 0 |S )a  Normalize an array to appropriate meta object

    Parameters
    ----------
    x: array-like, callable
        Either an object that looks sufficiently like a Numpy array,
        or a callable that accepts shape and dtype keywords
    ndim: int
        Number of dimensions of the array
    dtype: Numpy dtype
        A valid input for ``np.dtype``

    Returns
    -------
    array-like with zero elements of the correct dtype
    _metaNz/You must specify the meta or dtype of the arraydtyper   r   )shaper   c                 S  s4   g | ],}t |tjrd nt|dr(|jnt|qS )r   ndim)
isinstancenumbersNumberhasattrr   len).0ar   r   r   
<listcomp>@   s   
z#meta_from_array.<locals>.<listcomp>c                 S  s&   g | ]\}}|d kr|nt ||qS r   )meta_from_array)r   r    Zndr   r   r   r!   H       r   c                 s  s   | ]}t d d dV  qdS r   Nslicer   _r   r   r   	<genexpr>V   r#   z"meta_from_array.<locals>.<genexpr>c                 s  s   | ]
}d V  qd S r   r   r'   r   r   r   r)   Y   r#   c                 s  s   | ]}t d d dV  qdS r$   r%   r'   r   r   r   r)   Z   r#   r   T)maskc                 3  s   | ]}|t  v V  qd S r   strr   ser   r   r)   l   s   )zinvalid literalz!could not convert string to floatZSU)r   r   r   r   
ValueErrornpisscalararrayndarrayr   r   typelisttuplezipr   r   rangeEllipsissumZreshapemaZmaskedempty	Exceptionastypeanykind)r   r   r   Zndimsr    metar   r0   r   r"      sn    




&

* 


r"   c                   sD  t jdd  t  tjdtd dd |D }dd | D }t| t jrb| | }n(z&t	| drvd	|d< | |i |}W n  t
y   zLt fd
ddD r n&W Y d   W d    W d    d S W Y d   nd   0  ty\   zXt|dkr"dt v r"|d }n&W Y d   W d    W d    d S W Y d   n6d   0  ty   Y W d    W d    d S 0 |rt|dd |krtt ||}W d    n1 s0    Y  t |rt |}|W  d    W  d    S 1 s0    Y  W d    n1 s60    Y  d S )Nignore)all)categoryc                 S  s    g | ]}t |rt|n|qS r   r   r"   )r   r   r   r   r   r!      r#   z compute_meta.<locals>.<listcomp>c                 S  s&   i | ]\}}|t |rt|n|qS r   rH   r   kvr   r   r   
<dictcomp>   s   z compute_meta.<locals>.<dictcomp>Zcomputing_metaTc                 3  s   | ]}|t  v V  qd S r   r,   r.   r0   r   r   r)      s   zcompute_meta.<locals>.<genexpr>)zunexpected keyword argumentzis an invalid keyword forz'Did not understand the following kwargs   z&zero-size array to reduction operationr   r   )r3   Zerrstatewarningscatch_warningssimplefilterRuntimeWarningitemsr   Z	vectorizer
   	TypeErrorrB   r2   r   r-   r@   getattr
contextlibsuppressAttributeErrorrA   r4   r5   )funcZ_dtypeargskwargsZ	args_metaZkwargs_metarD   r   r0   r   compute_meta|   sH    
:

: *
r[   Fc                 K  s   t | } t |}t| dd dkrft| ds4t|drNtjj| |fddi|S tj| |fd|i|S |r| j|jkotdd t| j	|j	D S | |k S )	Nr   Or+   Zmasked_equalT	equal_nanc                 s  s.   | ]&\}}t |rt |n||kV  qd S r   )r3   isnan)r   r    br   r   r   r)      s   zallclose.<locals>.<genexpr>)
r   rT   r   r3   r>   allcloser   rF   r:   Zflat)r    r_   r]   rZ   r   r   r   r`      s    r`   c                 C  s$   dd }t | j|dt |j|dkS )Nc                 S  s   t | tr| dddfS | S d S )N)r   r-   )rJ   r   r   r   key   s    
zsame_keys.<locals>.key)rb   )sorteddask)r    r_   rb   r   r   r   	same_keys   s    re   c                 C  s   | j od| j vS Nr   )r   r   r   r   r   
_not_empty   s    rg   c              
   C  s   t | tsdS |   tdd | jD s.J tt| j }dd | D }t	 }|
 D ]>}| j D ].}z|t||  W qn ty   Y qn0 qnq`t|dk sJ |dS )z2Check that graph is well named and non-overlappingNc                 s  s   | ]}t |ttfV  qd S r   )r   r9   r-   )r   rJ   r   r   r   r)      r#   z_check_dsk.<locals>.<genexpr>c                 S  s   i | ]\}}|d kr||qS )rM   r   rI   r   r   r   rL      r#   z_check_dsk.<locals>.<dictcomp>   )r   r	   validaterF   Zlayersr   r   valuesrR   setkeysaddr   KeyErrorr   )ZdskZfreqsZnon_oneZkey_collisionsrJ   Zlayerr   r   r   
_check_dsk   s    

ro   Tc                 C  sn   |rt | t |ksJ t| |D ]F\}}t|s>t|r\|rht|t|kshJ q"||ks"J q"d S r   )r   r:   mathr^   )r    r_   
check_ndim	check_nanZaaZbbr   r   r   assert_eq_shape   s    rs   c                 C  s   | j |d} tjdd | jD  D ]}| j| jf|  }t|drJ| }t|dsbtj	|dd}t
dd t| j|D }t||j|d	d
 |j| jks"J dq"| S )N	schedulerc                 s  s   | ]}t t|V  qd S r   )r;   r   )r   cr   r   r   r)      r#   z _check_chunks.<locals>.<genexpr>resultr   r\   r*   c                 s  s   | ]\}}|| V  qd S r   r   )r   rv   ir   r   r   r)      r#   F)rq   rr   z6maybe you forgot to pass the scheduler to `assert_eq`?)Zpersist	itertoolsproductchunksrd   namer   rw   r3   r5   r9   r:   rs   r   r   )r   rq   ru   idxchunkZexpected_shaper   r   r   _check_chunks   s     



r   c           
      C  s   | }d }d }t | rt| r| jd us*J | j}	|r>t| j t| dd }|r\t| ||d} | j|d} | }t| dr~| 	 } t| dst
j| dd} t| r| j|jksJ |rt|j| jdd	 n$t| dst
j| dd} t| dd }	| |	||fS )
Nr   )rq   ru   rt   todenser   r\   r*   F)rr   )r   r   r   ro   rd   rT   r   computer   r   r3   r5   rg   rs   r   )
r   check_shapecheck_graphcheck_chunksrq   ru   Z
x_originalZx_metaZ
x_computedadtr   r   r   _get_dt_meta_computed   s2    



r   syncc                 K  sT  | }|}t | tttfr"t| } t |tttfr<t|}t| |||||
d\} }}}t||||||
d\}}}}|rt|t|krtd| d| dzj| j	|j	ksJ d| j	 d|j	 d|r"| j	r| n| 
 }|j	r|n|
 }t|t|ks"J dt| dt| d|rt| drNt|drNt| j|j t|drd|jj d	| j d}|jj| jksJ ||d urd
t|j d	t| d}t|jt|ksJ |t|st|sdt| dt| d}t|t|ksJ |t|drd|jj d	|j d}|jj|jksTJ ||d urdt|j d	t| d}t|jt|ksJ |t|st|sdt| dt| d}t|t|ksJ |d}t| |fd|	i|sJ |W dS  ty   Y n0 | |k}t |tjrF| sPJ n
|sPJ dS )N)r   r   r   rq   ru   z#a and b have different dtypes: (a: z, b: )z"a and b have different shapes (a: z!a and b have different types (a: r   z<compute()-ing 'a' changes its number of dimensions (before: z	, after: z,compute()-ing 'a' changes its type (before: zRcompute()-ing 'a' results in a different type than implied by its metadata (meta: z, computed: z<compute()-ing 'b' changes its number of dimensions (before: z,compute()-ing 'b' changes its type (before: zRcompute()-ing 'b' results in a different type than implied by its metadata (meta: zHfound values in 'a' and 'b' which differ by more than the allowed amountr]   T)r   r8   intfloatr3   r5   r   r-   AssertionErrorr   itemr7   r   	assert_eqr   r   r4   r`   rS   r6   rF   )r    r_   r   r   Z
check_metar   rq   Z
check_typeZcheck_dtyper]   ru   rZ   Z
a_originalZ
b_originalr   Za_metaZ
a_computedZbdtZb_metaZ
b_computedZ_a_bmsgrv   r   r   r   r   &  s    

	



 
r   c                   s0   t  fdd|D r$tj |dS dd S dS )zkLike functools.wraps, but safe to use even if wrapped is not a function.

    Only needed on Python 2.
    c                 3  s   | ]}t  |V  qd S r   )r   )r   attrwrappedr   r   r)     r#   zsafe_wraps.<locals>.<genexpr>)assignedc                 S  s   | S r   r   r   r   r   r   <lambda>  r#   zsafe_wraps.<locals>.<lambda>N)rF   	functoolswraps)r   r   r   r   r   
safe_wraps  s    r   c                 C  s,   z| j W S  ty&   t| j  Y S 0 dS )z!Determine dtype of an array-like.N)r   rW   r3   
asanyarray)r    r   r   r   	_dtype_of  s    r   c                 O  sZ   | du rt j|i |S zt j|dt| i|W S  tyT   t j|i | Y S 0 dS )z
    Use the `like=` from `np.arange` to create a new array dispatching
    to the downstream library. If that fails, falls back to the
    default NumPy behavior, resulting in a `numpy.ndarray`.
    Nlike)r3   Zaranger"   rS   )r   rY   rZ   r   r   r   arange_safe  s    r   c                 K  s   ||u rt |dr|S t|tr0||fi |S t|trPt|jrP|jdd}z| |fdt|i|W S  ty   | |fi | Y S 0 d S )NZ__array_function__r   rt   r   )r   r   r   r   r   r   r"   rS   )Znp_funcZda_funcr    r   rZ   r   r   r   _array_like_safe  s    


r   c                 K  s$   ddl m} ttj|| |fi |S )a  
    If `a` is `dask.array`, return `dask.array.asarray(a, **kwargs)`,
    otherwise return `np.asarray(a, like=like, **kwargs)`, dispatching
    the call to the library that implements the like array. Note that
    when `a` is a `dask.Array` backed by `cupy.ndarray` but `like`
    isn't, this function will call `a.compute(scheduler="sync")`
    before `np.array`, as downstream libraries are unlikely to know how
    to convert a `dask.Array` and CuPy doesn't implement `__array__` to
    prevent implicit copies to host.
    r   )r5   )Zdask.array.routinesr5   r   r3   )r    r   rZ   r5   r   r   r   
array_safe  s    r   c                 K  s$   ddl m} ttj|| |fi |S )a  
    If a is dask.array, return dask.array.asarray(a, **kwargs),
    otherwise return np.asarray(a, like=like, **kwargs), dispatching
    the call to the library that implements the like array. Note that
    when a is a dask.Array but like isn't, this function will call
    a.compute(scheduler="sync") before np.asarray, as downstream
    libraries are unlikely to know how to convert a dask.Array.
    r   )asarray)dask.array.corer   r   r3   )r    r   rZ   r   r   r   r   asarray_safe  s    	r   c                 K  s$   ddl m} ttj|| |fi |S )a  
    If a is dask.array, return dask.array.asanyarray(a, **kwargs),
    otherwise return np.asanyarray(a, like=like, **kwargs), dispatching
    the call to the library that implements the like array. Note that
    when a is a dask.Array but like isn't, this function will call
    a.compute(scheduler="sync") before np.asanyarray, as downstream
    libraries are unlikely to know how to convert a dask.Array.
    r   )r   )r   r   r   r3   )r    r   rZ   r   r   r   r   asanyarray_safe  s    	r   c                   sr   t | ttfr$t fdd| D S t | tjs<td|  |   k sN|  kr^td|  f | dk rn|  7 } | S )z#Validate an input to axis= keywordsc                 3  s   | ]}t | V  qd S r   )validate_axis)r   axr   r   r   r)     r#   z validate_axis.<locals>.<genexpr>z%Axis value must be an integer, got %sz2Axis %d is out of bounds for array of dimension %dr   )r   r9   r8   r   IntegralrS   r   )axisr   r   r   r   r     s    
r   c                 C  sf   |r| j }tj| ddd}n|j }tj|dddj}d|dkd | }| | ||j  } }| |fS )a  Sign correction to ensure deterministic output from SVD.

    This function is useful for orienting eigenvectors such that
    they all lie in a shared but arbitrary half-space. This makes
    it possible to ensure that results are equivalent across SVD
    implementations and random number generator states.

    Parameters
    ----------

    u : (M, K) array_like
        Left singular vectors (in columns)
    v : (K, N) array_like
        Right singular vectors (in rows)
    u_based_decision: bool
        Whether or not to choose signs based
        on `u` rather than `v`, by default False

    Returns
    -------

    u : (M, K) array_like
        Left singular vectors with corrected sign
    v:  (K, N) array_like
        Right singular vectors with corrected sign
    r   T)r   ZkeepdimsrM   g       @g      ?)r   r3   r=   TrA   )urK   Zu_based_decisionr   Zsignsr   r   r   svd_flip   s    r   c                 O  sJ   |d }t |r(dd l}t|jj| }ndd l}t|j| }||i |S rf   )r   Zcupyx.scipy.linalgrT   scipyZlinalgZscipy.linalg)	func_namerY   rZ   r    ZcupyxrX   r   r   r   r   scipy_linalg_safe*  s    r   c                 C  s   t d| ||dS )NZsolve_triangular)lower)r   )r    r_   r   r   r   r   solve_triangular_safe:  s    r   c                 C  s@   | dkr(t jdtt dtdd tS tdt d|  d S )Nr   zeAxisError was deprecated after version 2021.10.0 and will be removed in a future release. Please use z	 instead.rh   )rG   
stacklevelzmodule z has no attribute )rN   warnr   r   FutureWarningrW   __name__)r|   r   r   r   __getattr__>  s    r   )NN)F)TT)TN)TTTTN)	TTTTTTTTr   )F)F)3
__future__r   rU   r   ry   rp   r   rN   numpyr3   Ztlzr   r   r   r   Zdask.array.numpy_compatr   Z	dask.baser   r   Zdask.highlevelgraphr	   Z
dask.utilsr
   r   r   r   r   r"   r[   r`   re   rg   ro   rs   r   r   r   WRAPPER_ASSIGNMENTSr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sd   
a4



     
*         
k

*
