U
    Cvf                     @  sD  d dl mZ d dlZd dlZd dlZd dlmZ d dl	m
Z
 zd dlZdZW n ek
rh   eZdZY nX dd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZG dd dZefddZd.ddZdd  Zd/d!d"Zed#Zed$Zed%Zed&Z ed'Z!ed(Z"ed)Z#ed*Z$ed+Z%ed,Z&ed-Z'dS )0    )annotationsN)normalize_axis_index)OPTIONSTFc                 C  s<   t jdd |jD  }|d | |f ||d   }| | S )Nc                 S  s   g | ]}t |qS  )nparange).0sr   r   7/tmp/pip-unpacked-wheel-h316xyqg/xarray/core/nputils.py
<listcomp>   s     z&_select_along_axis.<locals>.<listcomp>)r   Zix_shape)valuesidxaxisZ	other_indslr   r   r
   _select_along_axis   s    r   c                 C  s.   t || j}tjt|  |d}t| ||S )Nr   )r   ndimr   argmaxpdisnullr   )r   r   Z	idx_firstr   r   r
   nanfirst   s    r   c                 C  sR   t || j}td f| td d df }dtjt| |  |d }t| ||S )Nr   )r   r   slicer   r   r   r   r   )r   r   revZidx_lastr   r   r
   nanlast!   s    r   c                 C  s0   t jt| t jd}t jt| t jd|| < |S )aU  Return indices for an inverse permutation.

    Parameters
    ----------
    indices : 1D np.ndarray with dtype=int
        Integer positions to assign elements to.

    Returns
    -------
    inverse_permutation : 1D np.ndarray with dtype=int
        Integer indices to take from the original array to create the
        permutation.
    dtype)r   emptylenZintpr   )indicesinverse_permutationr   r   r
   r!   (   s    r!   c                 G  s6   t | tr2tj| j}| r tjntj}||td} | S )Nr   )
isinstanceboolr   	broadcastr   Zoneszeros)resultargsr   constructorr   r   r
   _ensure_bool_is_ndarray<   s
    
r)   c              
   C  s<   t  * t dd t| |k| |W  5 Q R  S Q R X d S Nignorezelementwise comparison failedwarningscatch_warningsfilterwarningsr)   selfotherr   r   r
   array_eqJ   s    
r3   c              
   C  s<   t  * t dd t| |k| |W  5 Q R  S Q R X d S r*   r,   r0   r   r   r
   array_neP   s    
r4   c                 C  s4   | d }| dd D ]}||d kr* dS |}qdS )z?Given a non-empty list, does it consist of contiguous integers?r      NFTr   )Z	positionspreviouscurrentr   r   r
   _is_contiguousV   s    r8   c                 C  st   t | ts| f} dd t| D }|r.t|s2dS dd | D }ttj| j}|d t| }t|}||fS )zHIndices of the advanced indexes subspaces for mixed indexing and vindex.c                 S  s   g | ]\}}t |ts|qS r   r"   r   )r   ikr   r   r
   r   d   s    
 z/_advanced_indexer_subspaces.<locals>.<listcomp>)r   r   c                 S  s   g | ]}t |ts|qS r   r9   )r   r;   r   r   r
   r   o   s     
 r   )	r"   tuple	enumerater8   r   r   r$   r   r   )keyZadvanced_index_positionsZ
non_slicesr   mixed_positionsvindex_positionsr   r   r
   _advanced_indexer_subspaces`   s    

rA   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	NumpyVIndexAdapterzObject that implements indexing like vindex on a np.ndarray.

    This is a pure Python implementation of (some of) the logic in this NumPy
    proposal: https://github.com/numpy/numpy/pull/6256
    c                 C  s
   || _ d S N)_array)r1   arrayr   r   r
   __init__}   s    zNumpyVIndexAdapter.__init__c                 C  s    t |\}}t| j| ||S rC   rA   r   ZmoveaxisrD   )r1   r>   r?   r@   r   r   r
   __getitem__   s    zNumpyVIndexAdapter.__getitem__c                 C  s$   t |\}}t|||| j|< dS )z0Value must have dimensionality matching the key.NrG   )r1   r>   valuer?   r@   r   r   r
   __setitem__   s    zNumpyVIndexAdapter.__setitem__N)__name__
__module____qualname____doc__rF   rH   rJ   r   r   r   r
   rB   v   s   rB   c                   s   d fdd	} |_ |S )Nc                   s   | dd }tt d }trtd rt| tjr|d k	rt|ts| j	j
dkr| j	jr|d ksnt	|| j	kr|dd  || fd|i|}nt | fd|i|}|S )Nr   Zuse_bottleneckZuifcr   )getgetattrbn_USE_BOTTLENECKr   r"   r   Zndarrayr<   r   kindZisnativepop)r   r   kwargsr   Zbn_funcr&   namenpmoduler   r
   f   s0    

z$_create_bottleneck_method.<locals>.f)N)rK   )rW   rX   rY   r   rV   r
   _create_bottleneck_method   s    rZ   c                 C  s   t |jd d ft j}t | }t |st jj|| d d f | |  |d\|d d< }}}|jdkrr|nt j|d< t	||jd  |S )Nr5   rcondr   r   )
r   fullr   nanisnanalllinalglstsqsizewarn_on_deficient_rank)Zarrxr\   outmaskZresidrank_r   r   r
   _nanpolyfit_1d   s    

6rj   c                 C  s   | |krt jdtjdd d S )Nz!Polyfit may be poorly conditioned   )
stacklevel)r-   warnr   ZRankWarning)rh   orderr   r   r
   rd      s    rd   c                 C  s  |rF|j dk}|r&||jd d}tjt|dd}t| jd d |jd f}t|rttd|d d |f | |d d |f< t| rtj	j
| |d d | f |d\|d d| f< }}}	|jdkr|ntj|d| f< t|| jd  |d dd d f }
|dd d f }|r|
|
jd }
||jd }nDtj	j
| ||d\}
}}}	|jdkrz|
d tj }t|| jd  |
|fS )Nr5   r   r   r[   r   )r   Zreshaper   r   anyr_   r   Zapply_along_axisrj   ra   rb   rc   r^   rd   )lhsrhsr\   ZskipnaZ	added_dimZnan_colsrf   Zresidsrh   ri   ZcoeffsZ	residualsr   r   r
   least_squares   s>    

     rr   nanminnanmaxnanmean	nanmediannanvarnanstdnanprod	nancumsum
nancumprod	nanargmin	nanargmax)N)NF)(
__future__r   r-   Znumpyr   Zpandasr   Znumpy.core.multiarrayr   Zxarray.core.optionsr   Z
bottleneckrQ   rR   ImportErrorr   r   r   r!   r)   r3   r4   r8   rA   rB   rZ   rj   rd   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r   r   r   r
   <module>   sH   




