B
    Y              
   @   s   d Z ddlZddlZddddddd	d
ddg
ZdddZdddZd ddZd!ddZd"ddZ	d#ddZ
d$dd	Zd%dd
Zd&ddZd'ddZd(ddZdd Zd)ddZdS )*z<Alternative methods of calculating moving window statistics.    Nmove_sum	move_meanmove_stdmove_varmove_minmove_maxmove_argminmove_argmaxmove_median	move_rankc             C   s   t tj| |||dS )z%Slow move_sum for unaccelerated dtype)axis)	move_funcnpZnansum)awindow	min_countr    r   3lib/python3.7/site-packages/bottleneck/slow/move.pyr      s    c             C   s   t tj| |||dS )z&Slow move_mean for unaccelerated dtype)r   )r   r   Znanmean)r   r   r   r   r   r   r   r      s    c             C   s   t tj| ||||dS )z%Slow move_std for unaccelerated dtype)r   ddof)r   r   Znanstd)r   r   r   r   r   r   r   r   r      s    c             C   s   t tj| ||||dS )z%Slow move_var for unaccelerated dtype)r   r   )r   r   Znanvar)r   r   r   r   r   r   r   r   r      s    c             C   s   t tj| |||dS )z%Slow move_min for unaccelerated dtype)r   )r   r   Znanmin)r   r   r   r   r   r   r   r   !   s    c             C   s   t tj| |||dS )z%Slow move_max for unaccelerated dtype)r   )r   r   Znanmax)r   r   r   r   r   r   r   r   &   s    c             C   s   dd }t || |||dS )z(Slow move_argmin for unaccelerated dtypec             S   s   t j| dd} td g| j }td d d||< | | } yt j| |d}W nx tk
r   |  } t | }t j| t j	|d t j
| |dt j}|jdkrt j}nt j||d}t j||< Y nX |S )NF)copyr   )r   )wherer   )r   arrayslicendimZ	nanargmin
ValueErrorr   isnancopytoinfargminastypefloat64nanall)r   r   flipidxmaskr   r   r   r   -   s     

zmove_argmin.<locals>.argmin)r   )r   )r   r   r   r   r   r   r   r   r   +   s    c             C   s   dd }t || |||dS )z(Slow move_argmax for unaccelerated dtypec             S   s   t j| dd} td g| j }td d d||< | | } yt j| |d}W nz tk
r   |  } t | }t j| t j	 |d t j
| |dt j}|jdkrt j}nt j||d}t j||< Y nX |S )NF)r   r   )r   )r   r   )r   r   r   r   Z	nanargmaxr   r   r   r   r   argmaxr    r!   r"   r#   )r   r   r$   r%   r&   r   r   r   r'   E   s     

zmove_argmax.<locals>.argmax)r   )r   )r   r   r   r   r'   r   r   r   r	   C   s    c             C   s   t tj| |||dS )z(Slow move_median for unaccelerated dtype)r   )r   r   Z	nanmedian)r   r   r   r   r   r   r   r
   [   s    c             C   s   t t| |||dS )z&Slow move_rank for unaccelerated dtype)r   )r   lastrank)r   r   r   r   r   r   r   r   `   s    c          	   K   st  t j|dd}|dkr|}n2|}||kr>d}t|||f n|dkrNtd|jdkr`td|dkrptd|d	k rtd
||j| krtdt|jjt jrt 	|}nt 
|j}tdg|j }	t|	}
t t td xbt|j| D ]P}t||d	 }t|d	 | |d	 |	|< ||
|< | ||	 fd|i|||
< qW W dQ R X t||||}t j||< |S )z>Generic moving window function implemented with a python loop.F)r   Nz1min_count (%d) cannot be greater than window (%d)r   z&`min_count` must be greater than zero.z(moving window functions require ndim > 0z)An `axis` value of None is not supported.   z`window` must be at least 1.z`window` is too long.ignorer   )r   r   r   r   shape
issubclassdtypetypeZinexactZ
empty_likeemptyr   listwarningscatch_warningssimplefilterrangemin_maskr"   )funcr   r   r   r   kwargsZmcmsgyidx1idx2iwinr%   r   r   r   r   g   s@    


*
r   c       
      C   s   | | k |}td g| j }td g| j }td g| j }t|d ||< td | ||< td |||< || }|||  }tj| jtjd}	||k |	|< || |k |	|< |	S )N)r-   )Zcumsumr   r   r   r/   r+   bool)
r   r   r   r   nr;   r<   Zidx3Znidx1r%   r   r   r   r6      s    r6   c             C   s`  t j| dd} | j}| jdkrlt| j}|| t j|| jd}|	t j
 |jdkrh|jdkrht j
}|S tdg| }tdd||< tdg| }d||< t |  |}| | }|| k|}	|| k|}
|	|	 |
 d d	 }||d  }d	|d
  }|dkr0|dkrd}t | | r\t j
}n,t ||dkd t |t | | t j
 |S )a  
    The ranking of the last element along the axis, ignoring NaNs.

    The ranking is normalized to be between -1 and 1 instead of the more
    common 1 and N. The results are adjusted for ties.

    Parameters
    ----------
    a : ndarray
        Input array. If `a` is not an array, a conversion is attempted.
    axis : int, optional
        The axis over which to rank. By default (axis=-1) the ranking
        (and reducing) is performed over the last axis.

    Returns
    -------
    d : array
        In the case of, for example, a 2d array of shape (n, m) and
        axis=1, the output will contain the rank (normalized to be between
        -1 and 1 and adjusted for ties) of the the last element of each row.
        The output in this example will have shape (n,).

    Examples
    --------
    Create an array:

    >>> y1 = larry([1, 2, 3])

    What is the rank of the last element (the value 3 in this example)?
    It is the largest element so the rank is 1.0:

    >>> import numpy as np
    >>> from la.afunc import lastrank
    >>> x1 = np.array([1, 2, 3])
    >>> lastrank(x1)
    1.0

    Now let's try an example where the last element has the smallest
    value:

    >>> x2 = np.array([3, 2, 1])
    >>> lastrank(x2)
    -1.0

    Here's an example where the last element is not the minimum or maximum
    value:

    >>> x3 = np.array([1, 3, 4, 5, 2])
    >>> lastrank(x3)
    -0.5

    F)r   r   )r-   r)   Nr   g      ?g       @g      ?)r   r   r   sizer0   r+   popr/   r-   Zfillr"   r   r   sumZputmask)r   r   r   r+   rZindlastZindlast2r@   Z	a_indlastger   r   r   r(      s:    5




r(   )Nr   )Nr   )Nr   r   )Nr   r   )Nr   )Nr   )Nr   )Nr   )Nr   )Nr   )Nr   )r   )__doc__r1   Znumpyr   __all__r   r   r   r   r   r   r   r	   r
   r   r   r6   r(   r   r   r   r   <module>   s$   











&