ó
¦–Õ\c           @` 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 „ ƒ Z d „  Z d „  Z d d d „ Z d d d „ Z d S(   i    (   t   absolute_importt   divisiont   print_function(   t   wraps(   t   NumberN(   t   merget   merge_sortedi   (   t   Arrayi   (   t   tokenize(   t   HighLevelGraph(   t   Iteratort   linearc         C` s-  t  |  ƒ } t  |  ƒ s" d  | f St | t ƒ r@ t | ƒ } n  |  j j d k r› t j |  j	 | d | ƒ} d d  l
 } | j j | |  j |  j ƒ | f St j |  j t j ƒ rð |  j d ƒ } t j | | d | ƒ} | j |  j ƒ | f St j |  j t j ƒ sd } n  t j |  | d | ƒ| f S(   Nt   categoryt   interpolationi    t   i8t   nearest(   t   lent   Nonet
   isinstanceR
   t   listt   dtypet   namet   npt
   percentilet   codest   pandast   Categoricalt
   from_codest
   categoriest   orderedt
   issubdtypet
   datetime64t   astypet   number(   t   at   qR   t   nt   resultt   pdt   a2(    (    s4   lib/python2.7/site-packages/dask/array/percentile.pyt   _percentile   s     
"	c         C` s*   d d l  m } | ƒ  } | j |  ƒ | S(   Ni    (   t   TDigest(   t   crickR)   t   update(   R"   R)   t   t(    (    s4   lib/python2.7/site-packages/dask/array/percentile.pyt   _tdigest_chunk#   s    	c         C` s@   d d l  m } | ƒ  } | j | Œ  t j | j |  d ƒ ƒ S(   Ni    (   R)   g      Y@(   R*   R)   R   R   t   arrayt   quantile(   t   qst   digestsR)   R,   (    (    s4   lib/python2.7/site-packages/dask/array/percentile.pyt   _percentiles_from_tdigest-   s    	t   defaultc         ` si  |  j  d k s t d ƒ ‚ n  t ˆ t ƒ r9 ˆ g ‰ n  t j ˆ ƒ ‰ t |  ˆ ˆ  ƒ } |  j } t j | t j	 ƒ r— t j g  d | ƒd j } n  d d d g } | | k rÁ t
 d ƒ ‚ n  | d k rÖ d } n | } | d k rœˆ  d	 k rœt j | t j ƒ st j | t j	 ƒ rœd
 d l m } | d d ƒ d | ‰ t ‡ f d †  t |  j ƒ  ƒ Dƒ ƒ }	 d | }
 i t ˆ t |	 ƒ f |
 d
 f 6} n~ d | ‰ t ‡  ‡ ‡ f d †  t |  j ƒ  ƒ Dƒ ƒ }	 d | }
 i t ˆ ˆ g t |  j d
 ƒ t |	 ƒ ˆ  f |
 d
 f 6} t |	 | ƒ }	 t j |
 |	 d |  g ƒ} t | |
 d t ˆ ƒ f f d | ƒS(   s]   Approximate percentile of 1-D array

    Parameters
    ----------
    a : Array
    q : array_like of float
        Percentile or sequence of percentiles to compute, which must be between
        0 and 100 inclusive.
    interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}, optional
        The interpolation method to use when the desired percentile lies
        between two data points ``i < j``. Only valid for ``method='dask'``.

        * 'linear': ``i + (j - i) * fraction``, where ``fraction``
        is the fractional part of the index surrounded by ``i``
        and ``j``.
        * 'lower': ``i``.
        * 'higher': ``j``.
        * 'nearest': ``i`` or ``j``, whichever is nearest.
        * 'midpoint': ``(i + j) / 2``.

    method : {'default', 'dask', 'tdigest'}, optional
        What method to use. By default will use dask's internal custom
        algorithm (``'dask'``).  If set to ``'tdigest'`` will use tdigest for
        floats and ints and fallback to the ``'dask'`` otherwise.

    See Also
    --------
    numpy.percentile : Numpy's equivalent Percentile function
    i   s+   Percentiles only implemented for 1-d arraysR   g      à?R3   t   daskt   tdigests1   method can only be 'default', 'dask' or 'tdigest'R   i    (   t   import_requiredR*   s=   crick is a required dependency for using the t-digest method.s   percentile_tdigest_chunk-c         3` s-   |  ]# \ } } ˆ  | f t  | f f Vq d  S(   N(   R-   (   t   .0t   it   key(   R   (    s4   lib/python2.7/site-packages/dask/array/percentile.pys	   <genexpr>t   s   s   percentile_tdigest-s   percentile_chunk-c         3` s3   |  ]) \ } } ˆ | f t  | ˆ ˆ  f f Vq d  S(   N(   R(   (   R7   R8   R9   (   R   R   R#   (    s4   lib/python2.7/site-packages/dask/array/percentile.pys	   <genexpr>   s   s   percentile-t   dependenciest   chunks(   t   ndimt   NotImplementedErrorR   R   R   R.   R   R   R   t   integert
   ValueErrort   floatingt
   dask.utilsR6   t   dictt	   enumeratet   __dask_keys__R2   t   sortedt   merge_percentilesR   R;   R   R	   t   from_collectionsR   (   R"   R#   R   t   methodt   tokenR   t   allowed_methodst   internal_methodR6   t   dskt   name2t   dsk2t   graph(    (   R   R   R#   s4   lib/python2.7/site-packages/dask/array/percentile.pyR   7   sF    		*

%

 t   lowerc         C` s  t  |  t ƒ r t |  ƒ }  n  t j |  ƒ }  t t t | ƒ ƒ } t | ƒ } | d k ro t | Œ  \ } } n  t | ƒ } t t g  t | | | ƒ D]$ \ } } } | r” | | | f ^ q” Œ  ƒ } | sÙ t d ƒ ‚ n  | \ } } } | d j	 j
 d k r\t |  | g  | D] }	 |	 j ^ q| | ƒ }
 d d l } | j j |
 | d j | d j ƒ St j | d j	 t j ƒ sd } n  t | ƒ t | ƒ k s±t | ƒ t | ƒ k rÀt d ƒ ‚ n  g  } xj t | | ƒ D]Y \ } } t j t | ƒ ƒ } t j | ƒ | d )| d | d <| | 9} | j | ƒ qÖWt t t | | ƒ Œ  } t | Œ  \ } } t j | ƒ } t j | ƒ } t j | ƒ } |  t | ƒ } | d k r¾t j | | | ƒ } n?t j | | d	 d
 ƒ} t j | | d	 d ƒd } t j | t | ƒ d | ƒ t j | | ƒ } t j | | ƒ } | d k rL| | } n± | d k re| | } n˜ | d k rŠd | | | | } ns | d k rñt j | | | ƒ } t j | | | ƒ } | | k } | } | | | | <| | } n t d ƒ ‚ | S(   s%   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])
    s   No non-trivial arrays foundi    R   NR   s3   qs, vals, and Ns parameters must be the same lengthi   R   t   sidet   leftt   rightRP   t   highert   midpointg      à?sO   interpolation can only be 'linear', 'lower', 'higher', 'midpoint', or 'nearest'(    R   R
   R   R   R.   t   mapR   t   zipR?   R   R   RF   R   R   R   R   R   R   R   R!   R   t   emptyt   difft   appendR   t   cumsumt   sumt   interpt   searchsortedt   minimumt   maximumt   abs(   t   finalqR0   t   valsR   t   NsR#   t   valt   Nt   Lt   vR%   R&   t   countst   countt   combined_vals_countst   combined_valst   combined_countst
   combined_qt	   desired_qt   rvRR   RS   RP   t   uppert   lower_residualt   upper_residualt   maskt   index(    (    s4   lib/python2.7/site-packages/dask/array/percentile.pyRF   ‹   sn    I.$	0

(   t
   __future__R    R   R   t	   functoolsR   t   numbersR   t   numpyR   t   toolzR   R   t   coreR   t   baseR   t   highlevelgraphR	   t   compatibilityR
   R   R(   R-   R2   R   RF   (    (    (    s4   lib/python2.7/site-packages/dask/array/percentile.pyt   <module>   s   	
	
T