ó
˜íYc        
   @   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 d „ Z d 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 „  Z d d „ Z d S(   s<   Alternative methods of calculating moving window statistics.iÿÿÿÿNt   move_sumt	   move_meant   move_stdt   move_vart   move_mint   move_maxt   move_argmint   move_argmaxt   move_mediant	   move_rankc         C   s   t  t j |  | | d | ƒS(   s%   Slow move_sum for unaccelerated dtypet   axis(   t	   move_funct   npt   nansum(   t   at   windowt	   min_countR
   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR       s    c         C   s   t  t j |  | | d | ƒS(   s&   Slow move_mean for unaccelerated dtypeR
   (   R   R   t   nanmean(   R   R   R   R
   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR      s    i    c      	   C   s"   t  t j |  | | d | d | ƒS(   s%   Slow move_std for unaccelerated dtypeR
   t   ddof(   R   R   t   nanstd(   R   R   R   R
   R   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR      s    c      	   C   s"   t  t j |  | | d | d | ƒS(   s%   Slow move_var for unaccelerated dtypeR
   R   (   R   R   t   nanvar(   R   R   R   R
   R   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR      s    c         C   s   t  t j |  | | d | ƒS(   s%   Slow move_min for unaccelerated dtypeR
   (   R   R   t   nanmin(   R   R   R   R
   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR   !   s    c         C   s   t  t j |  | | d | ƒS(   s%   Slow move_max for unaccelerated dtypeR
   (   R   R   t   nanmax(   R   R   R   R
   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR   &   s    c         C   s"   d „  } t  | |  | | d | ƒS(   s(   Slow move_argmin for unaccelerated dtypec         S   s  t  j |  d t ƒ}  t d  ƒ g |  j } t d  d  d ƒ | | <|  | }  y t  j |  d | ƒ} Wn£ t k
 r	|  j ƒ  }  t  j	 |  ƒ } t  j
 |  t  j d | ƒt  j |  d | ƒj t  j ƒ } | j d k rä t  j } q
t  j | d | ƒ} t  j | | <n X| S(   Nt   copyiÿÿÿÿR
   t   wherei    (   R   t   arrayt   Falset   slicet   Nonet   ndimt	   nanargmint
   ValueErrorR   t   isnant   copytot   inft   argmint   astypet   float64t   nant   all(   R   R
   t   flipt   idxt   mask(    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR#   -   s     
!R
   (   R   (   R   R   R   R
   R#   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR   +   s    	c         C   s"   d „  } t  | |  | | d | ƒS(   s(   Slow move_argmax for unaccelerated dtypec         S   s  t  j |  d t ƒ}  t d  ƒ g |  j } t d  d  d ƒ | | <|  | }  y t  j |  d | ƒ} Wn¤ t k
 r
|  j ƒ  }  t  j	 |  ƒ } t  j
 |  t  j d | ƒt  j |  d | ƒj t  j ƒ } | j d k rå t  j } qt  j | d | ƒ} t  j | | <n X| S(   NR   iÿÿÿÿR
   R   i    (   R   R   R   R   R   R   t	   nanargmaxR   R   R    R!   R"   t   argmaxR$   R%   R&   R'   (   R   R
   R(   R)   R*   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR,   E   s     
!R
   (   R   (   R   R   R   R
   R,   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR   C   s    	c         C   s   t  t j |  | | d | ƒS(   s(   Slow move_median for unaccelerated dtypeR
   (   R   R   t	   nanmedian(   R   R   R   R
   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR   [   s    c         C   s   t  t |  | | d | ƒS(   s&   Slow move_rank for unaccelerated dtypeR
   (   R   t   lastrank(   R   R   R   R
   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR	   `   s    c      
   K   s  t  j | d t ƒ} | d k r* | } nL | } | | k r[ d } t | | | f ƒ ‚ n | d k rv t d ƒ ‚ n  | j d k r” t d ƒ ‚ n  | d k r¯ t d ƒ ‚ n  | d k  rÊ t d ƒ ‚ n  | | j | k rì t d	 ƒ ‚ n  t | j j	 t  j
 ƒ rt  j | ƒ } n t  j | j ƒ } t d ƒ g | j }	 t |	 ƒ }
 t j ƒ  ‰ t j d
 ƒ xt t | j | ƒ D]_ } t | | d ƒ } t | d | | d ƒ |	 | <| |
 | <|  | |	 d | | | |
 <qxWWd QXt | | | | ƒ } t  j | | <| S(   s>   Generic moving window function implemented with a python loop.R   s1   min_count (%d) cannot be greater than window (%d)i    s&   `min_count` must be greater than zero.s(   moving window functions require ndim > 0s)   An `axis` value of None is not supported.i   s   `window` must be at least 1.s   `window` is too long.t   ignoreR
   N(   R   R   R   R   R   R   t   shapet
   issubclasst   dtypet   typet   inexactt
   empty_liket   emptyR   t   listt   warningst   catch_warningst   simplefiltert   ranget   mint   _maskR&   (   t   funcR   R   R   R
   t   kwargst   mct   msgt   yt   idx1t   idx2t   it   winR)   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR   g   s@    	
'c   
      C   sì   |  |  k j  | ƒ } t d  ƒ g |  j } t d  ƒ g |  j } t d  ƒ g |  j } t | d  ƒ | | <t d  | ƒ | | <t d  | ƒ | | <| | } | | | } t j |  j d t j ƒ}	 | | k  |	 | <| | | k  |	 | <|	 S(   NR2   (   t   cumsumR   R   R   R   R6   R0   t   bool(
   R   R   R   R
   t   nRC   RD   t   idx3t   nidx1R)   (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR=      s    
c         C   sä  t  j |  d t ƒ}  |  j } |  j d k rŸ t |  j ƒ } | j | ƒ t  j | d |  j	 ƒ} | j
 t  j ƒ | j d k r› | j d k r› t  j } n  | St d	 ƒ g | } t d d	 ƒ | | <t d	 ƒ g | } d | | <t  j |  ƒ j | ƒ } |  | } | |  k j | ƒ }	 | |  k j | ƒ }
 |	 |	 |
 d d } | | d } d | d } | d k r¤| d k r‚d } n  t  j |  | ƒ ràt  j } qàn< t  j | | d k d ƒ t  j | t  j |  | ƒ t  j ƒ | S(
   sŒ  
    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

    R   i    R2   i   iÿÿÿÿg      ð?g       @g      à?N(   R   R   R   R   t   sizeR7   R0   t   popR6   R2   t   fillR&   R   R   R    t   sumt   putmask(   R   R
   R   R0   t   rt   indlastt   indlast2RI   t	   a_indlastt   gt   e(    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyR.   Ÿ   s:    5	

	#(   t   __doc__R8   t   numpyR   t   __all__R   R    R   R   R   R   R   R   R   R   R	   R   R=   R.   (    (    (    s3   lib/python2.7/site-packages/bottleneck/slow/move.pyt   <module>   s$   	&	