B
    T\m                 @   s   d dl mZmZmZ d dlmZ d dlm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 ddlmZ ee	jdddZdddZdddZdS )    )absolute_importdivisionprint_function)wraps)NumberN)mergemerge_sorted   )Array   )tokenize)HighLevelGraph)Iteratorlinearc             C   s   t | }t | sd |fS t|tr*t|}| jjdkrhtj| j||d}dd l	}|j
|| j| j|fS t| jtjr| d}tj|||d}|| j|fS t| jtjsd}tj| ||d|fS )Ncategory)interpolationr   Zi8nearest)len
isinstancer   listdtypenamenp
percentilecodespandasCategorical
from_codes
categoriesordered
issubdtypeZ
datetime64Zastypenumber)aqr   nresultpdZa2 r'   4lib/python3.7/site-packages/dask/array/percentile.py_percentile   s     

r)   c       	         s   | j dkstdttr"gtt| t }d| t fddt	| 
 D }d| }|dftgt| jd  t| fi}| j}t|tjrtjg |dd	 j}t||}tj||| gd
}t||tff|dS )z` Approximate percentile of 1-D array

    See :func:`numpy.percentile` for more information
    r	   z+Percentiles only implemented for 1-d arrayszpercentile_chunk-c             3   s&   | ]\}}|ft | ffV  qd S )N)r)   ).0ikey)r   r   r#   r'   r(   	<genexpr>0   s   zpercentile.<locals>.<genexpr>zpercentile-r   )r   g      ?)Zdependencies)chunksr   )ndimNotImplementedErrorr   r   r   arrayr   r   dict	enumerateZ__dask_keys__merge_percentilesr   r.   sortedr   r    Zintegerr   r   Zfrom_collectionsr
   )	r"   r#   r   tokenZdskZname2Zdsk2r   Zgraphr'   )r   r   r#   r(   r   #   s&    



r   lowerc             C   s  t | trt| } t| } ttt|}t|}|dkrFt| \}}t|}ttdd t|||D  }|sxtd|\}}}|d jj	dkrt
| |dd |D ||}ddl}|j||d j|d jS t|d jtjsd}t|t|kst|t|krtd	g }xVt||D ]H\}	}
tt|	}t|	|d
d< |	d |d< ||
9 }|| q$W ttt|| }t| \}}t|}t|}t|}| t| }|dkrt|||}ntj||dd}tj||ddd
 }t|t|d
 | t||}t||}|dkr6|| }n|dkrJ|| }nx|dkrjd|| ||   }nX|dkrt|| | }t|| | }||k}|}|| ||< || }ntd|S )a%   Combine several percentile calculations of different data.

    Parameters
    ----------

    finalq : numpy.array
        Percentiles to compute (must use same scale as ``qs``).
    qs : sequence of :class:`numpy.array`s
        Percentiles calculated on different sets of data.
    vals : sequence of :class:`numpy.array`s
        Resulting values associated with percentiles ``qs``.
    Ns : sequence of integers
        The number of data elements associated with each data set.
    interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
        Specify the type of interpolation to use to calculate final
        percentiles.  For more information, see :func:`numpy.percentile`.

    Examples
    --------

    >>> finalq = [10, 20, 30, 40, 50, 60, 70, 80]
    >>> qs = [[20, 40, 60, 80], [20, 40, 60, 80]]
    >>> vals = [np.array([1, 2, 3, 4]), np.array([10, 11, 12, 13])]
    >>> Ns = [100, 100]  # Both original arrays had 100 elements

    >>> merge_percentiles(finalq, qs, vals, Ns=Ns)
    array([ 1,  2,  3,  4, 10, 11, 12, 13])
    Nc             S   s    g | ]\}}}|r|||fqS r'   r'   )r*   r#   valNr'   r'   r(   
<listcomp>f   s    z%merge_percentiles.<locals>.<listcomp>zNo non-trivial arrays foundr   r   c             S   s   g | ]
}|j qS r'   )r   )r*   vr'   r'   r(   r:   n   s    r   z3qs, vals, and Ns parameters must be the same lengthr	   r   left)Zsiderightr7   ZhigherZmidpointg      ?zOinterpolation can only be 'linear', 'lower', 'higher', 'midpoint', or 'nearest')r   r   r   r   r1   mapzip
ValueErrorr   r   r4   r   r   r   r   r   r    r!   r   emptyZdiffappendr   ZcumsumsumZinterpZsearchsortedZminimumZmaximumabs)ZfinalqZqsZvalsr   ZNsLr%   r&   Zcountsr#   r9   countZcombined_vals_countsZcombined_valsZcombined_countsZ
combined_qZ	desired_qrvr<   r=   r7   upperZlower_residualZupper_residualmaskindexr'   r'   r(   r4   @   sn    


$











r4   )r   )r   )r7   N)Z
__future__r   r   r   	functoolsr   Znumbersr   Znumpyr   Ztoolzr   r   Zcorer
   baser   Zhighlevelgraphr   Zcompatibilityr   r   r)   r4   r'   r'   r'   r(   <module>   s   
