ó
¦–Õ\c           @` s¥  d  Z  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 d d	 l m Z m Z m Z d d
 l m Z d d l m Z d d l m Z y d d l	 m Z Wn e k
 rÚ e j Z n Xd „  Z e
 j Z e
 j Z e
 j Z e
 j Z e e
 j ƒ Z e e
 j  ƒ Z  e e
 j! ƒ Z! e e
 j" ƒ Z" e
 j# Z# e
 j$ Z$ e
 j% Z% e
 j& Z& y d d l	 m' Z' m( Z( Wn# e k
 r§e j' Z' e j( Z( n Xe
 j) Z) e
 j* Z* e
 j+ Z+ e e, ƒ  e
 j- Z- Wd QXe
 j. Z. e e, ƒ  e
 j/ Z/ Wd QXe
 j0 Z0 e e, ƒ  e
 j1 Z1 Wd QXe2 d „ Z3 d d „ Z5 d „  Z6 d „  Z7 d „  Z8 d „  Z9 d „  Z: d „  Z; d d „ Z< d d „ Z= d „  Z> d „  Z? d S(   s-    A set of NumPy functions to apply per chunk i    (   t   absolute_importt   divisiont   print_function(   t   wraps(   t   concatNi   (   t   numpy_compati   (   t	   Containert   Iterablet   Sequence(   t   flatten(   t   ignoring(   t   Integral(   t   take_along_axisc         ` s%   t  ˆ  ƒ d d ‡  f d † ƒ } | S(   sU   
    A wrapper for functions that don't provide keepdims to ensure that they do.
    c   	      ` sÇ   ˆ  |  d | | | Ž} | s" | S| } | d  k rF t |  j ƒ } n  t | t t t f ƒ sj | g } n  t ƒ  } xC t |  j ƒ D]2 } | | k r¢ | d 7} qƒ | t d  ƒ f 7} qƒ W| | } | S(   Nt   axis(   N(	   t   Nonet   ranget   ndimt
   isinstanceR   R   R   t   tuplet   slice(	   t   xR   t   keepdimst   argst   kwargst   rt   axest   r_slicet	   each_axis(   t
   a_callable(    s/   lib/python2.7/site-packages/dask/array/chunk.pyt   keepdims_wrapped_callable   s    	
N(   R   R   (   R   R   (    (   R   s/   lib/python2.7/site-packages/dask/array/chunk.pyt   keepdims_wrapper   s    	(   t
   nancumprodt	   nancumsumc         ` sä   x0 t  | j ƒ D] } | ˆ  k r d ˆ  | <q q W| rk t ‡  f d †  t | j ƒ Dƒ ƒ } | | } n  t t g  t  | j ƒ D]% } | j | ˆ  | ˆ  | f ^ q ƒ ƒ } |  | j | ƒ d t t  d | j d d ƒ ƒ ƒS(   s¾   Coarsen array by applying reduction to fixed size neighborhoods

    Parameters
    ----------
    reduction: function
        Function like np.sum, np.mean, etc...
    x: np.ndarray
        Array to be coarsened
    axes: dict
        Mapping of axis to coarsening factor

    Examples
    --------
    >>> x = np.array([1, 2, 3, 4, 5, 6])
    >>> coarsen(np.sum, x, {0: 2})
    array([ 3,  7, 11])
    >>> coarsen(np.max, x, {0: 3})
    array([3, 6])

    Provide dictionary of scale per dimension

    >>> x = np.arange(24).reshape((4, 6))
    >>> x
    array([[ 0,  1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10, 11],
           [12, 13, 14, 15, 16, 17],
           [18, 19, 20, 21, 22, 23]])

    >>> coarsen(np.min, x, {0: 2, 1: 3})
    array([[ 0,  3],
           [12, 15]])

    You must avoid excess elements explicitly

    >>> x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
    >>> coarsen(np.min, x, {0: 3}, trim_excess=True)
    array([1, 4])
    i   c         3` sJ   |  ]@ \ } } | ˆ  | r5 t  d  | ˆ  | ƒ n t  d d ƒ Vq d S(   i    N(   R   R   (   t   .0t   it   d(   R   (    s/   lib/python2.7/site-packages/dask/array/chunk.pys	   <genexpr>‹   s   R   i   (   R   R   R   t	   enumeratet   shapeR   t   reshape(   t	   reductionR   R   t   trim_excessR"   t   indt   newshape(    (   R   s/   lib/python2.7/site-packages/dask/array/chunk.pyt   coarsen^   s    (	>c         C` sz   t  | t ƒ r" | g |  j } n  t  | t ƒ rb g  t |  j ƒ D] } | j | d ƒ ^ qA } n  |  t d „  | Dƒ ƒ S(   sD   Trim boundaries off of array

    >>> x = np.arange(24).reshape((4, 6))
    >>> trim(x, axes={0: 0, 1: 1})
    array([[ 1,  2,  3,  4],
           [ 7,  8,  9, 10],
           [13, 14, 15, 16],
           [19, 20, 21, 22]])

    >>> trim(x, axes={0: 1, 1: 1})
    array([[ 7,  8,  9, 10],
           [13, 14, 15, 16]])
    i    c         s` s+   |  ]! } t  | | r | n d  ƒ Vq d  S(   N(   R   R   (   R!   t   ax(    (    s/   lib/python2.7/site-packages/dask/array/chunk.pys	   <genexpr>ª   s    (   R   R   R   t   dictR   t   getR   (   R   R   R"   (    (    s/   lib/python2.7/site-packages/dask/array/chunk.pyt   trim—   s
    1c         ` s¥   | t  k s t ‚ ˆ  d ‰  t | ƒ |  j ˆ  k r9 |  St j |  | d ˆ  ƒ}  | d k rn t | d ƒ n
 t | ƒ ‰ |  t ‡  ‡ f d †  t	 |  j
 ƒ Dƒ ƒ S(   s	   Chunk and combine function of topk

    Extract the k largest elements from a on the given axis.
    If k is negative, extract the -k smallest elements instead.
    Note that, unlike in the parent function, the returned elements
    are not sorted internally.
    i    R   c         3` s-   |  ]# } | ˆ  k r ˆ n	 t  d  ƒ Vq d  S(   N(   R   R   (   R!   R"   (   R   t   k_slice(    s/   lib/python2.7/site-packages/dask/array/chunk.pys	   <genexpr>¼   s   N(   t   Truet   AssertionErrort   absR%   t   npt	   partitionR   R   R   R   R   (   t   at   kR   R   (    (   R   R0   s/   lib/python2.7/site-packages/dask/array/chunk.pyt   topk­   s    
)c         ` s}   | t  k s t ‚ t |  | ˆ  | ƒ }  ˆ  d ‰  t j |  d ˆ  ƒ}  | d k  rV |  S|  t ‡  f d †  t |  j ƒ Dƒ ƒ S(   sn    Final aggregation function of topk

    Invoke topk one final time and then sort the results internally.
    i    R   c         3` s9   |  ]/ } | ˆ  k r' t  d d d  ƒ n	 t  d ƒ Vq d S(   iÿÿÿÿN(   R   R   (   R!   R"   (   R   (    s/   lib/python2.7/site-packages/dask/array/chunk.pys	   <genexpr>Ë   s   (   R1   R2   R8   R4   t   sortR   R   R   (   R6   R7   R   R   (    (   R   s/   lib/python2.7/site-packages/dask/array/chunk.pyt   topk_aggregateÀ   s    
c         C` s
   |  | f S(   s_    Preparatory step for argtopk

    Put data together with its original indices in a tuple.
    (    (   R6   t   idx(    (    s/   lib/python2.7/site-packages/dask/array/chunk.pyt   argtopk_preprocessÏ   s    c   
      ` s^  | t  k s t ‚ ˆ  d ‰  t |  t ƒ r¥ t t |  ƒ ƒ }  t j g  |  D] \ } } | ^ qJ ˆ  ƒ } t j g  |  D]! \ } } t j | | j ƒ ^ qu ˆ  ƒ } n |  \ } } t	 | ƒ | j ˆ  k rÎ |  St j
 | | d ˆ  ƒ}	 | d k rt | d ƒ n
 t | ƒ ‰ |	 t ‡  ‡ f d †  t | j ƒ Dƒ ƒ }	 t | |	 ˆ  ƒ t | |	 ˆ  ƒ f S(   s*   Chunk and combine function of argtopk

    Extract the indices of the k largest elements from a on the given axis.
    If k is negative, extract the indices of the -k smallest elements instead.
    Note that, unlike in the parent function, the returned elements
    are not sorted internally.
    i    R   c         3` s-   |  ]# } | ˆ  k r ˆ n	 t  d  ƒ Vq d  S(   N(   R   R   (   R!   R"   (   R   R0   (    s/   lib/python2.7/site-packages/dask/array/chunk.pys	   <genexpr>ï   s   N(   R1   R2   R   t   listR	   R4   t   concatenatet   broadcast_toR%   R3   t   argpartitionR   R   R   R   R   R   (
   t
   a_plus_idxR7   R   R   t   ait   _R6   t   idxiR;   t   idx2(    (   R   R0   s/   lib/python2.7/site-packages/dask/array/chunk.pyt   argtopk×   s    
+	4)c         ` s•   | t  k s t ‚ t |  | ˆ  | ƒ \ } } ˆ  d ‰  t j | d ˆ  ƒ} t | | ˆ  ƒ } | d k  rn | S| t ‡  f d †  t | j ƒ Dƒ ƒ S(   s™    Final aggregation function of argtopk

    Invoke argtopk one final time, sort the results internally, drop the data
    and return the index only.
    i    R   c         3` s9   |  ]/ } | ˆ  k r' t  d d d  ƒ n	 t  d ƒ Vq d S(   iÿÿÿÿN(   R   R   (   R!   R"   (   R   (    s/   lib/python2.7/site-packages/dask/array/chunk.pys	   <genexpr>  s   (	   R1   R2   RF   R4   t   argsortR   R   R   R   (   RA   R7   R   R   R6   R;   RE   (    (   R   s/   lib/python2.7/site-packages/dask/array/chunk.pyt   argtopk_aggregateô   s    
c         C` s6   t  j |  | | | ƒ } t | ƒ | k r2 | d  S| S(   Niÿÿÿÿ(   R4   t   aranget   len(   t   startt   stopt   stept   lengtht   dtypet   res(    (    s/   lib/python2.7/site-packages/dask/array/chunk.pyRI     s    c         K` s   |  j  | |  S(   N(   t   astype(   R   t   astype_dtypeR   (    (    s/   lib/python2.7/site-packages/dask/array/chunk.pyRQ     s    t   Cc         C` sN   | d k r( t  j |  ƒ }  |  j | ƒ St  j |  ƒ }  |  j j | ƒ j Sd  S(   NRS   (   R4   t   ascontiguousarrayt   viewt   asfortranarrayt   T(   R   RO   t   order(    (    s/   lib/python2.7/site-packages/dask/array/chunk.pyRU     s
    c         ` sŒ   ˆ j  t j ƒ ‰ t j ˆ d k  ˆ | ˆ ƒ ‰ ˆ | ‰ ˆ d k ˆ |  j ˆ  k  @} ˆ | ‰ |  t ‡  ‡ f d †  t |  j ƒ Dƒ ƒ S(   sµ   Chunk function of `slice_with_int_dask_array_on_axis`.
    Slice one chunk of x by one chunk of idx.

    Parameters
    ----------
    x: ndarray, any dtype, any shape
        i-th chunk of x
    idx: ndarray, ndim=1, dtype=any integer
        j-th chunk of idx (cartesian product with the chunks of x)
    offset: ndarray, shape=(1, ), dtype=int64
        Index of the first element along axis of the current chunk of x
    x_size: int
        Total size of the x da.Array along axis
    axis: int
        normalized axis to take elements from (0 <= axis < x.ndim)

    Returns
    -------
    x sliced along axis, using only the elements of idx that fall inside the
    current chunk.
    i    c         3` s-   |  ]# } | ˆ  k r ˆ n	 t  d  ƒ Vq d  S(   N(   R   R   (   R!   R"   (   R   R;   (    s/   lib/python2.7/site-packages/dask/array/chunk.pys	   <genexpr>@  s   (   RQ   R4   t   int64t   whereR%   R   R   R   (   R   R;   t   offsett   x_sizeR   t
   idx_filter(    (   R   R;   s/   lib/python2.7/site-packages/dask/array/chunk.pyt   slice_with_int_dask_array  s    

c   	      ` s  |  j  t j ƒ }  t j |  d k  |  t | ƒ |  ƒ }  d } d } t j |  ƒ ‰ x‚ | D]z } |  | k |  | | k  @} t j | ƒ } ˆ t j | | d | d ƒ 7‰ | | 7} | j d k rY | | d 7} qY qY W| t ‡  ‡ f d †  t	 | j
 ƒ Dƒ ƒ S(   sT   Final aggregation function of `slice_with_int_dask_array_on_axis`.
    Aggregate all chunks of x by one chunk of idx, reordering the output of
    `slice_with_int_dask_array`.

    Note that there is no combine function, as a recursive aggregation (e.g.
    with split_every) would not give any benefit.

    Parameters
    ----------
    idx: ndarray, ndim=1, dtype=any integer
        j-th chunk of idx
    chunk_outputs: ndarray
        concatenation along axis of the outputs of `slice_with_int_dask_array`
        for all chunks of x and the j-th chunk of idx
    x_chunks: tuple
        dask chunks of the x da.Array along axis, e.g. ``(3, 3, 2)``
    axis: int
        normalized axis to take elements from (0 <= axis < x.ndim)

    Returns
    -------
    Selection from all chunks of x for the j-th chunk of idx, in the correct
    order
    i    i   iÿÿÿÿc         3` s-   |  ]# } | ˆ  k r ˆ n	 t  d  ƒ Vq d  S(   N(   R   R   (   R!   R"   (   R   t	   idx_final(    s/   lib/python2.7/site-packages/dask/array/chunk.pys	   <genexpr>w  s   (   RQ   R4   RY   RZ   t   sumt
   zeros_liket   cumsumt   sizeR   R   R   (	   R;   t   chunk_outputst   x_chunksR   t   x_chunk_offsett   chunk_output_offsett   x_chunkR]   t   idx_cum(    (   R   R_   s/   lib/python2.7/site-packages/dask/array/chunk.pyt#   slice_with_int_dask_array_aggregateE  s    %!
(@   t   __doc__t
   __future__R    R   R   t	   functoolsR   t   toolzR   t   numpyR4   t    R   t   npcompatt   compatibilityR   R   R   t   coreR	   t   utilsR
   t   numbersR   R   t   ImportErrorR   R`   t   prodt   mint   maxt   argmint	   nanargmint   argmaxt	   nanargmaxt   anyt   allt   nansumt   nanprodR   R    t   nanmint   nanmaxt   meant   AttributeErrort   nanmeant   vart   nanvart   stdt   nanstdt   FalseR+   R   R/   R8   R:   R<   RF   RH   RI   RQ   RU   R^   Rj   (    (    (    s/   lib/python2.7/site-packages/dask/array/chunk.pyt   <module>   sl   	#														9								-