B
    Y                 @   sh   d dl Zd dl mZmZ dddddgZdddZdd	dZd
d Zdd Zej	dfddZ
dddZdS )    N)	partitionargpartitionrankdatananrankdatar   r   pushc             C   s   t t| |S )z5Slow rankdata function used for unaccelerated dtypes.)_rankscipy_rankdata)aaxis r   =lib/python3.7/site-packages/bottleneck/slow/nonreduce_axis.pyr      s    c             C   s   t t| |S )z8Slow nanrankdata function used for unaccelerated dtypes.)r   _nanrankdata_1d)r	   r
   r   r   r   r      s    c             C   sh   t j|dd}|d kr"| }d}|jdkr>|jt jdd}n&t | ||}|jt jkrd|t j}|S )NF)copyr   T)nparrayravelsizeZastypefloat64Zapply_along_axisdtype)Zfunc1dr	   r
   yr   r   r   r      s    
r   c             C   s>   t j| jt jd}|t j t |  }t| | ||< |S )N)r   )r   emptyshaper   fillnanisnanr   )r	   r   idxr   r   r   r      s
    r   c       
   	   C   s|  |dkrt dt| }|j}|dks4||d krBt|||}|dkr\|dddf }n|dkrh|S t| }t|jdd }t|jdd }|tj	 |tj	 tj
ddv xnt|jd D ]\}|| |k}	tj	||	< |d|f  }	||	 ||	|f< |d|f }	|||	< ||	|f ||	< qW W dQ R X |dksT||d krft||d |}|dkrx|d S |S )	z(Slow push used for unaccelerated dtypes.Nz`axis` cannot be Noner      r   ignore)Zinvalid.)
ValueErrorr   r   ndimZrollaxisr   r   r   r   r   Zerrstaterange)
r	   nr
   r   r    ZfidxZrecentcountir   r   r   r   r   '   s:    


averagec             C   s   |dkrt d|tt| } |dkr2dnd}tj| |d}tj|jtjd}tj	|jtjd||< |dkr||d S | | } tj
d	| dd
 | d
d kf }| | }|dkr|S tj
t|d t|f }|dkr|| S |dkr||d  d S d|| ||d   d  S )a  
    rankdata(a, method='average')
    Assign ranks to data, dealing with ties appropriately.
    Ranks begin at 1.  The `method` argument controls how ranks are assigned
    to equal values.  See [1]_ for further discussion of ranking methods.
    Parameters
    ----------
    a : array_like
        The array of values to be ranked.  The array is first flattened.
    method : str, optional
        The method used to assign ranks to tied elements.
        The options are 'average', 'min', 'max', 'dense' and 'ordinal'.
        'average':
            The average of the ranks that would have been assigned to
            all the tied values is assigned to each value.
        'min':
            The minimum of the ranks that would have been assigned to all
            the tied values is assigned to each value.  (This is also
            referred to as "competition" ranking.)
        'max':
            The maximum of the ranks that would have been assigned to all
            the tied values is assigned to each value.
        'dense':
            Like 'min', but the rank of the next highest element is assigned
            the rank immediately after those assigned to the tied elements.
        'ordinal':
            All values are given a distinct rank, corresponding to the order
            that the values occur in `a`.
        The default is 'average'.
    Returns
    -------
    ranks : ndarray
         An array of length equal to the size of `a`, containing rank
         scores.
    References
    ----------
    .. [1] "Ranking", http://en.wikipedia.org/wiki/Ranking
    Examples
    --------
    >>> from scipy.stats import rankdata
    >>> rankdata([0, 2, 3, 2])
    array([ 1. ,  2.5,  4. ,  2.5])
    >>> rankdata([0, 2, 3, 2], method='min')
    array([ 1,  2,  4,  2])
    >>> rankdata([0, 2, 3, 2], method='max')
    array([ 1,  3,  4,  3])
    >>> rankdata([0, 2, 3, 2], method='dense')
    array([ 1,  2,  3,  2])
    >>> rankdata([0, 2, 3, 2], method='ordinal')
    array([ 1,  2,  4,  3])
    )r%   minmaxdenseordinalzunknown method "{0}"r)   Z	mergesortZ	quicksort)Zkind)r   r   TNr   r(   r   r'   r&   g      ?)r   formatr   r   ZasarrayZargsortr   r   ZintpZarangeZr_ZcumsumZnonzerolen)r	   methodZalgoZsorterinvZobsr(   r#   r   r   r   r   S   s(    4"
r   )N)N)r%   )Znumpyr   r   r   __all__r   r   r   r   infr   r   r   r   r   r   <module>   s   

,