ó
¦–Õ\c           @` sÍ  d  d l  m Z m Z m Z d  d l Z d  d l m Z m Z d  d l m	 Z	 m
 Z
 d  d l m Z m Z m Z d  d l Z d  d l m Z d  d l m Z m Z m Z m Z m 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" m# Z# d d l$ m% Z% d d l& m' Z' d d l( m) Z) m* Z* d d l+ m, Z, m- Z. d d l/ m0 Z0 m1 Z1 d d l2 m3 Z3 d d l4 m5 Z5 d d l& m6 Z6 m7 Z7 m8 Z8 m9 Z9 d d l m: Z: e8 d ƒ Z; e; j< e= e j> f e j? ƒ e; j< e j@ jA e j@ j? ƒ e8 d ƒ ZB eB j< e= e j> f e. ƒ eB j< e j@ jA e, ƒ d d „ Z- d eD d d d d d eE d d „	 ZF d d d eE d „ ZG eD d d d „ ZH e e jI ƒ d d eD d d d „ ƒ ZI e e jJ ƒ d d eD d d d „ ƒ ZJ e e jK ƒ d eD d d d  „ ƒ ZK e e jL ƒ d eD d d d! „ ƒ ZL e e jM ƒ d eD d d d" „ ƒ ZM e e jN ƒ d eD d d d# „ ƒ ZN e e jO ƒ d d eD d d d$ „ ƒ ZO e6 eP ƒ h e e jQ ƒ d d eD d d d% „ ƒ ZQ e e jR ƒ d d d& „ ƒ ZR e e jS ƒ d d d' „ ƒ ZS Wd QXe e jT ƒ d eD d d d( „ ƒ ZT e e jU ƒ d eD d d d) „ ƒ ZU d* „  ZV d+ „  ZW e jI eV d, d- „ ZX e jI eV d, d d. „ ZY d, d d/ „ ZZ e e j[ ƒ d d eD d d d0 „ ƒ Z[ d d eD d d d1 „ Z\ e6 eP ƒ  e e j\ ƒ e\ ƒ Z\ Wd QXd e jI eV d, d2 „ Z] d3 „  Z^ d d  d, e jI d d4 „ Z_ d d  d, e jI d d5 „ Z` d d eD d  d d d6 „ Za e e jb ƒ d d eD d  d d d7 „ ƒ Zb d d eD d  d d d8 „ Zc e6 eP ƒ  e e jc ƒ ec ƒ Zc Wd QXe e jd ƒ d d eD d  d d d9 „ ƒ Zd d d eD d  d d d: „ Ze e6 eP ƒ  e e je ƒ ee ƒ Ze Wd QXeD d; „ Zf d< „  Zg d d= „ Zh d d> „ Zi d d? „ Zj d d d d@ „ Zk eD dA „ Zl dB „  Zm dC „  Zn el e jK e jo ƒ Zo el e jL e jp ƒ Zp el e jT em eE ƒ Zq el e jU en eE ƒ Zr d d d dD „ Zs dE „  Zt dF „  Zu e e jv ƒ d d d dG „ ƒ Zv e e jw ƒ d d d dH „ ƒ Zw dI d dJ „ Zx dI d dK „ Zy e e jz ƒ d  d  d d dL „ ƒ Zz d S(M   i    (   t   absolute_importt   divisiont   print_functionN(   t   partialt   wraps(   t   productt   repeat(   t	   factorialt   logt   ceil(   t   Integral(   t   composet   partition_allt   gett
   accumulatet   plucki   (   t   chunk(   t   _concatenate2t   Arrayt
   handle_out(   t	   blockwisei   (   t
   lol_tuples(   t   aranget   diagonal(   t   sqrt(   t   validate_axis(   t   zerost   ones(   t	   ma_dividet   divide(   t
   getargspect   builtins(   t   tokenize(   t   HighLevelGraph(   t   ignoringt   funcnamet   Dispatcht   deepmap(   t   configt   emptyR   c         C` sC   d „  } t  j t t j |  | d | ƒƒ ƒ } | |  | d | ƒS(   Nc         S` s   t  |  d t d ƒ ƒ S(   Nt   __array_priority__s   -inf(   t   getattrt   float(   t   x(    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   <lambda>&   t    t   keyt   dtype(   t   divide_lookupt   dispatcht   typeR   t   max(   t   at   bR/   R.   t   f(    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR   %   s    	'c         ` s¡  ˆ  d
 k r$ t t |  j ƒ ƒ ‰  n  t ˆ  t ƒ r? ˆ  f ‰  n  t ˆ  |  j ƒ ‰  | d
 k rl t d ƒ ‚ n  d t | ƒ j	 k r– t
 | d | ƒ} n  d t | ƒ j	 k rÀ t
 | d | ƒ} n  t t |  j ƒ ƒ } t | | |  | d ˆ  d t d |  j ƒ} t ‡  ‡ f d †  t | j ƒ Dƒ ƒ | _ t | | ˆ  | | | | d | d |
 ƒ} | r”ˆ d k r”t ‡  ‡ f d	 †  t | j ƒ Dƒ ƒ | _ n  t |	 | ƒ S(   s»   General version of reductions

    Parameters
    ----------
    x: Array
        Data being reduced along one or more axes
    chunk: callable(x_chunk, axis, keepdims)
        First function to be executed when resolving the dask graph.
        This function is applied in parallel to all original chunks of x.
        See below for function parameters.
    combine: callable(x_chunk, axis, keepdims), optional
        Function used for intermediate recursive aggregation (see
        split_every below). If omitted, it defaults to aggregate.
        If the reduction can be performed in less than 3 steps, it will not
        be invoked at all.
    aggregate: callable(x_chunk, axis, keepdims)
        Last function to be executed when resolving the dask graph,
        producing the final output. It is always invoked, even when the reduced
        Array counts a single chunk along the reduced axes.
    axis: int or sequence of ints, optional
        Axis or axes to aggregate upon. If omitted, aggregate along all axes.
    keepdims: boolean, optional
        Whether the reduction function should preserve the reduced axes,
        leaving them at size ``output_size``, or remove them.
    dtype: np.dtype, optional
        Force output dtype. Defaults to x.dtype if omitted.
    split_every: int >= 2 or dict(axis: int), optional
        Determines the depth of the recursive aggregation. If set to or more
        than the number of input chunks, the aggregation will be performed in
        two steps, one ``chunk`` function per input chunk and a single
        ``aggregate`` function at the end. If set to less than that, an
        intermediate ``combine`` function will be used, so that any one
        ``combine`` or ``aggregate`` function has no more than ``split_every``
        inputs. The depth of the aggregation graph will be
        :math:`log_{split_every}(input chunks along reduced axes)`. Setting to
        a low value can reduce cache size and network transfers, at the cost of
        more CPU and a larger dask graph.

        Omit to let dask heuristically decide a good default. A default can
        also be set globally with the ``split_every`` key in
        :mod:`dask.config`.
    name: str, optional
        Prefix of the keys of the intermediate and output nodes. If omitted it
        defaults to the function names.
    out: Array, optional
        Another dask array whose contents will be replaced. Omit to create a
        new one. Note that, unlike in numpy, this setting gives no performance
        benefits whatsoever, but can still be useful  if one needs to preserve
        the references to a previously existing Array.
    concatenate: bool, optional
        If True (the default), the outputs of the ``chunk``/``combine``
        functions are concatenated into a single np.array before being passed
        to the ``combine``/``aggregate`` functions. If False, the input of
        ``combine`` and ``aggregate`` will be either a list of the raw outputs
        of the previous step or a single output, and the function will have to
        concatenate it itself. It can be useful to set this to False if the
        chunk and/or combine steps do not produce np.arrays.
    output_size: int >= 1, optional
        Size of the output of the ``aggregate`` function along the reduced
        axes. Ignored if keepdims is False.

    Returns
    -------
    dask array

    **Function Parameters**

    x_chunk: numpy.ndarray
        Individual input chunk. For ``chunk`` functions, it is one of the
        original chunks of x. For ``combine`` and ``aggregate`` functions, it's
        the concatenation of the outputs produced by the previous ``chunk`` or
        ``combine`` functions. If concatenate=False, it's a list of the raw
        outputs from the previous functions.
    axis: tuple
        Normalized list of axes to reduce upon, e.g. ``(0, )``
        Scalar, negative, and None axes have been normalized away.
        Note that some numpy reduction functions cannot reduce along multiple
        axes at once and strictly require an int in input. Such functions have
        to be wrapped to cope.
    keepdims: bool
        Whether the reduction function should preserve the reduced axes or
        remove them.
    s   Must specify dtypeR/   t   axist   keepdimsc         3` s:   |  ]0 \ } } | ˆ  k r. ˆ f t  | ƒ n | Vq d  S(   N(   t   len(   t   .0t   it   c(   R7   t   output_size(    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>’   s   t   namet   concatenatei   c         3` s0   |  ]& \ } } | ˆ  k r$ ˆ f n | Vq d  S(   N(    (   R:   R;   R<   (   R7   R=   (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>—   s   N(   t   Nonet   tuplet   ranget   ndimt
   isinstanceR
   R   t
   ValueErrorR   t   argsR   R   t   TrueR/   t	   enumeratet   chunkst   _chunkst   _tree_reduceR   (   R+   R   t	   aggregateR7   R8   R/   t   split_everyt   combineR>   t   outR?   R=   t   indst   tmpt   result(    (   R7   R=   s4   lib/python2.7/site-packages/dask/array/reductions.pyt	   reduction+   s*    V*c	         ` s  ˆ  p t  j d d ƒ ‰  t ˆ  t ƒ rF t ‡  f d †  | Dƒ ƒ ‰  n\ t ˆ  t ƒ r– t j t ˆ  d t | ƒ ps d ƒ d ƒ }	 t j	 | |	 ƒ ‰  n t
 d ƒ ‚ d }
 xg t |  j ƒ D]V \ } }	 | ˆ  k r¸ ˆ  | d k r¸ t t j |
 t t |	 ˆ  | ƒ ƒ ƒ ƒ }
 q¸ q¸ Wt | p| d | d t ƒ} | rTt | t t d	 | ƒƒ } n  xO t |
 d ƒ D]= } t | |  ˆ  t d
 | d | p˜t | p•| ƒ d ƒ}  qeWt | d | d | ƒ} | rât | t t d	 | ƒƒ } n  t | |  ˆ  d | d
 | d | pt | ƒ d ƒS(   s…    Perform the tree reduction step of a reduction.

    Lower level, users should use ``reduction`` or ``arg_reduction`` directly.
    RM   i   c         3` s'   |  ] } | ˆ  j  | d  ƒ f Vq d S(   i   N(   R   (   R:   t   k(   RM   (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>¥   s    i   i   s#   split_every must be a int or a dictR7   R8   t   axesR/   R>   s   -partials
   -aggregate(   R&   R   RD   t   dictR
   R   R3   t   intR9   t   fromkeysRE   RH   t	   numblocksR	   R   R   RG   R   R   RB   t   partial_reduceR#   (   R+   RL   R7   R8   R/   RM   RN   R>   R?   t   nt   depthR;   t   func(    (   RM   s4   lib/python2.7/site-packages/dask/array/reductions.pyRK   œ   s,    ,2#c         ` s'  | p t  |  ƒ d t |  | | | | ƒ } g  t | j ƒ D]3 \ } } t t | j | d ƒ t | ƒ ƒ ƒ ^ q< } t t	 t t	 t
 | ƒ ƒ Œ  }	 g  t | j ƒ D]A \ } }
 | | k rÞ t d „  t | | |
 ƒ Dƒ ƒ n |
 ^ q£ } | sQg  t | j ƒ D] } | | k r | ^ q ‰ ‡ f d †  } t	 | |	 ƒ }	 t | | ƒ ƒ } n  i  } xœ t |	 t | Œ  ƒ D]… \ } } t d „  t | ƒ Dƒ ƒ ‰  t ‡  f d †  t | ƒ Dƒ ƒ } t | j f t | j ƒ ˆ  | ƒ } |  | f | | f | <qmWt j | | d | g ƒ} t | | | d | ƒS(	   s˜   Partial reduction across multiple axes.

    Parameters
    ----------
    func : function
    x : Array
    split_every : dict
        Maximum reduction block sizes in each dimension.

    Examples
    --------
    Reduce across axis 0 and 2, merging a maximum of 1 block in the 0th
    dimension, and 3 blocks in the 2nd dimension:

    >>> partial_reduce(np.min, x, {0: 1, 2: 3})    # doctest: +SKIP
    t   -i   c         s` s   |  ] } d  Vq d S(   i   N(    (   R:   t   p(    (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>Ô   s    c         ` s   t  ˆ  |  ƒ S(   N(   R   (   RT   (   t   out_axis(    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   Ø   R-   c         s` s7   |  ]- \ } } t  | ƒ d  k r | | d f Vq d S(   i   i    N(   R9   (   R:   R;   t   j(    (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>Ý   s    c         3` s%   |  ] } | d  ˆ  k r | Vq d S(   i    N(    (   R:   R;   (   t   decided(    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>Þ   s    t   dependenciesR/   (   R#   R    RH   RY   t   listR   R   RB   R   t   mapR9   RI   RA   RC   t   zipRV   R   R>   R!   t   from_collectionsR   (   R]   R+   RM   R8   R/   R>   R;   R[   t   partst   keysR<   t
   out_chunkst   gettert   dskRT   R_   t   dummyt   gt   graph(    (   Rb   R`   s4   lib/python2.7/site-packages/dask/array/reductions.pyRZ   ¾   s(    FT.""$c         C` sy   | d  k	 r | } n* t t j d d |  j ƒj ƒ  d t ƒ } t |  t j t j d | d | d | d | d | ƒ} | S(   Ni   R/   R7   R8   RM   RO   (   i   (	   R@   R)   t   npR'   R/   t   sumt   objectRS   R   (   R4   R7   R/   R8   RM   RO   t   dtRR   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRq   å   s    	*!c         C` ss   | d  k	 r | } n* t t j d d |  j ƒj ƒ  d t ƒ } t |  t j t j d | d | d | d | d | ƒS(   Ni   R/   R7   R8   RM   RO   (   i   (	   R@   R)   Rp   R'   R/   t   prodRr   RS   R   (   R4   R7   R/   R8   RM   RO   Rs   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRt   ð   s
    	*!c         C` s7   t  |  t j t j d | d | d |  j d | d | ƒS(   NR7   R8   R/   RM   RO   (   RS   R   t   minR/   (   R4   R7   R8   RM   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRu   ú   s    !c         C` s7   t  |  t j t j d | d | d |  j d | d | ƒS(   NR7   R8   R/   RM   RO   (   RS   R   R3   R/   (   R4   R7   R8   RM   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR3      s    !c         C` s4   t  |  t j t j d | d | d d d | d | ƒS(   NR7   R8   R/   t   boolRM   RO   (   RS   R   t   any(   R4   R7   R8   RM   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRw     s    !c         C` s4   t  |  t j t j d | d | d d d | d | ƒS(   NR7   R8   R/   Rv   RM   RO   (   RS   R   t   all(   R4   R7   R8   RM   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRx     s    !c         C` sv   | d  k	 r | } n- t t j t j d d |  j ƒƒ d t ƒ } t |  t j t j	 d | d | d | d | d | ƒS(   Ni   R/   R7   R8   RM   RO   (   i   (
   R@   R)   R   t   nansumRp   R'   R/   Rr   RS   Rq   (   R4   R7   R/   R8   RM   RO   Rs   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRy     s
    	-!c         C` sv   | d  k	 r | } n- t t j t j d d |  j ƒƒ d t ƒ } t |  t j	 t j
 d | d | d | d | d | ƒS(   Ni   R/   R7   R8   RM   RO   (   i   (   R@   R)   R   Ry   Rp   R'   R/   Rr   RS   t   nanprodRt   (   R4   R7   R/   R8   RM   RO   Rs   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRz     s    	-c      	   C` s%   t  t j t j d |  | | d | ƒS(   Ni    RO   (   t   cumreductionR   t	   nancumsumt   operatort   add(   R+   R7   R/   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR|   (  s    c      	   C` s%   t  t j t j d |  | | d | ƒS(   Ni   RO   (   R{   R   t
   nancumprodR}   t   mul(   R+   R7   R/   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR   -  s    c         C` s7   t  |  t j t j d | d | d |  j d | d | ƒS(   NR7   R8   R/   RM   RO   (   RS   R   t   nanminR/   (   R4   R7   R8   RM   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR   3  s    c         C` s7   t  |  t j t j d | d | d |  j d | d | ƒS(   NR7   R8   R/   RM   RO   (   RS   R   t   nanmaxR/   (   R4   R7   R8   RM   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR‚   :  s    c         ` s}  t  |  d ƒ r( t j t j |  ƒ |  S|  j ‰ | j d t ƒ } | j d d ƒ ‰  | j d t j	 ƒ } ˆ  d k r» t j
 ˆ d | ƒ} | t k r· t j d	 t ˆ ƒ | d | ƒS| St ˆ  t pÊ t ƒ sÜ ˆ  g ‰  n  t j
 g  ˆ  D] } ˆ | ^ qé ƒ } | t k r<t ‡  ‡ f d †  t t ˆ ƒ ƒ Dƒ ƒ } n+ t ‡  ‡ f d †  t t ˆ ƒ ƒ Dƒ ƒ } t j | | d | ƒS(
   s-    A reduction to count the number of elements t   maskR8   R7   R/   i   c         3` s+   |  ]! } | ˆ  k r ˆ | n d  Vq d S(   i   N(    (   R:   t   dim(   R7   t   shape(    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>U  s    c         3` s%   |  ] } | ˆ  k r ˆ | Vq d  S(   N(    (   R:   R„   (   R7   R…   (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>W  s    N(   i   (   t   hasattrR   Rq   Rp   t	   ones_likeR…   R   t   FalseR@   t   float64Rt   RG   t   fullR9   RD   RA   Rd   RB   (   R+   t   kwargsR8   R/   Rt   R„   t	   new_shape(    (   R7   R…   s4   lib/python2.7/site-packages/dask/array/reductions.pyt   numelA  s     	0&.+c         K` s   t  j t j |  ƒ |  S(   s-    A reduction to count the number of elements (   R   Rq   Rp   t   isnan(   R+   R‹   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   nannumel[  s    t   f8c         K` s<   | |  d | | } | |  d | | } i | d 6| d 6S(   NR/   R[   t   total(    (   R+   Rq   R   R/   R‹   R[   R‘   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt
   mean_chunk`  s    c   
      K` s“   t  |  t ƒ s |  g }  n  t d „  |  ƒ } t d „  |  ƒ } t | d | ƒj d | |  } t | d | ƒj d | |  }	 i | d 6|	 d 6S(   Nc         S` s   |  d S(   NR[   (    (   t   pair(    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   i  R-   c         S` s   |  d S(   NR‘   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   j  R-   RU   R7   R[   R‘   (   RD   Rd   R%   R   Rq   (
   t   pairsRq   R   R/   R7   R‹   t   nst   totalsR[   R‘   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   mean_combinef  s    !!c         K` s…   t  d „  |  ƒ } t  d „  |  ƒ } t | d | ƒj d | d | |  } t | d | ƒj d | d | |  } t | | d | ƒS(   Nc         S` s   |  d S(   NR[   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   q  R-   c         S` s   |  d S(   NR‘   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   r  R-   RU   R7   R/   (   R%   R   Rq   R   (   R”   R/   R7   R‹   R•   R–   R[   R‘   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   mean_aggp  s
    ''c         C` s   | d  k	 r | } n0 t t j t j d d
 d |  j ƒ ƒ d t ƒ } t |  t t	 d | d | d | d | d t
 d | d	 t ƒS(   NR…   i   R/   R7   R8   RM   RN   RO   R?   (   i   (   R@   R)   Rp   t   meanR'   R/   Rr   RS   R’   R˜   R—   Rˆ   (   R4   R7   R/   R8   RM   RO   Rs   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR™   y  s    	0c         C` s©   | d  k	 r | } n0 t t j t j d d d |  j ƒ ƒ d t ƒ } t |  t t	 d t
 j d t ƒt d | d | d | d | d	 | d
 t d t t d t
 j d t ƒƒS(   NR…   i   R/   Rq   R   R7   R8   RM   RO   R?   RN   (   i   (   R@   R)   Rp   R™   R'   R/   Rr   RS   R   R’   R   Ry   R   R˜   Rˆ   R—   (   R4   R7   R/   R8   RM   RO   Rs   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   nanmean„  s    	0c         K` s¥   | |  d | | } | |  |  j  t j ƒ } | | } g  t d | d ƒ D]# }	 | |  | |	 d | | ^ qN }
 t j |
 d d ƒ} i | d 6| d 6| d 6S(	   NR/   i   i   R7   iÿÿÿÿR‘   R[   t   M(   t   astypeRp   t   int64RB   t   stack(   t   At   orderRq   R   R/   R‹   R‘   R[   t   uR;   t   xsR›   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   moment_chunk•  s    
=c   
      C` s»   |  d | d f j  d | |  | | | | d | | } xv t d | d ƒ D]a } t | ƒ t | ƒ t | | ƒ }	 | |	 | |  d | | d f | | d | | 7} qR W| S(   N.i   R7   i   (   Rq   RB   R   (
   t   MsR•   t
   inner_termR    Rq   R7   R‹   R›   RT   t   coeff(    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   _moment_helperž  s
    >$;c      
   K` sM  t  |  t ƒ s |  g }  n  t t d „  |  ƒ d | ƒ} t t d „  |  ƒ d | ƒ} t t d „  |  ƒ d | ƒ}	 | | d <t | d <| j d | |  }
 | j d | |  } t |
 | d | ƒ} t | | d | ƒ| } g  t d | d	 ƒ D]$ } t |	 | | | | | | ƒ ^ qõ } t	 j
 | d d
 ƒ} i |
 d 6| d 6| d 6S(   Nc         S` s   |  d S(   NR‘   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   ©  R-   RU   c         S` s   |  d S(   NR[   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   ª  R-   c         S` s   |  d S(   NR›   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   «  R-   R/   R8   R7   i   i   iÿÿÿÿR‘   R[   R›   (   RD   Rd   R   R%   RG   Rq   R   RB   R§   Rp   Rž   (   R”   R    t   ddofR/   Rq   R7   R‹   R–   R•   R¤   R‘   R[   t   muR¥   t   oR¢   R›   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   moment_combine¦  s    

>c         K` s+  t  |  t ƒ s |  g }  n  t t d „  |  ƒ d | ƒ} t t d „  |  ƒ d | ƒ} t t d „  |  ƒ d | ƒ}	 | | d <| j ƒ  }
 t |
 d <| j d | |
  } t | j d | |
  | d | ƒ} t | | d | ƒ| } t |	 | | | | | | ƒ } t | | j d | |  | d | ƒS(   Nc         S` s   |  d S(   NR‘   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   ½  R-   RU   c         S` s   |  d S(   NR[   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   ¾  R-   c         S` s   |  d S(   NR›   (    (   R“   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR,   ¿  R-   R/   R8   R7   (	   RD   Rd   R   R%   t   copyRG   Rq   R   R§   (   R”   R    R¨   R/   Rq   R7   R‹   R–   R•   R¤   t
   keepdim_kwR[   R©   R¥   R›   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt
   moment_aggº  s    

$c   
      C` s6  t  | t ƒ s | d k  r+ t d ƒ ‚ n  | d k  r |  j d | ƒ } | d k rq t | j d | j d d ƒSt | j d | j d d ƒS| d  k	 r¢ | }	 n0 t	 t
 j t
 j d d d |  j ƒ ƒ d t ƒ }	 t |  t t d
 | ƒt t d
 | d | ƒd | d | d |	 d | d | d t d t t d
 | ƒƒS(   Ni    s   Order must be an integer >= 0i   R7   RI   R/   R   R…   i   R    R¨   R8   RM   RO   R?   RN   (   i   (   RD   R
   RE   Rq   R   R…   RI   R   R@   R)   Rp   t   varR/   Rr   RS   R   R£   R®   Rˆ   R«   (
   R4   R    R7   R/   R8   R¨   RM   RO   t   reducedRs   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   momentÏ  s     	0c         C` s‘   | d  k	 r | } n0 t t j t j d d d |  j ƒ ƒ d t ƒ } t |  t t	 t
 d | ƒd | d | d | d | d t d	 d
 d | d t ƒS(   NR…   i   R/   R¨   R7   R8   RM   RN   R>   R¯   RO   R?   (   i   (   R@   R)   Rp   R¯   R   R/   Rr   RS   R£   R   R®   R«   Rˆ   (   R4   R7   R/   R8   R¨   RM   RO   Rs   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyR¯   è  s    	0!c         C` s¸   | d  k	 r | } n0 t t j t j d d d |  j ƒ ƒ d t ƒ } t |  t t	 d t
 j d t ƒt t d t j d | ƒd | d | d | d	 | d
 t t d t j ƒd | d t ƒS(   NR…   i   R/   Rq   R   R¨   R7   R8   RM   RN   RO   R?   (   i   (   R@   R)   Rp   R¯   R   R/   Rr   RS   R   R£   R   Ry   R   R®   R«   Rˆ   (   R4   R7   R/   R8   R¨   RM   RO   Rs   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   nanvarõ  s    	0!c         C` sa   t  |  j d | d | d | d | d | d | ƒ ƒ } | r] | | j k r] | j | ƒ } n  | S(   NR7   R/   R8   R¨   RM   RO   (   R   R¯   R/   Rœ   (   R4   R7   R/   R8   R¨   RM   RO   RR   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   std  s
    $c         C` sa   t  t |  d | d | d | d | d | d | ƒƒ } | r] | | j k r] | j | ƒ } n  | S(   NR7   R/   R8   R¨   RM   RO   (   R   R²   R/   Rœ   (   R4   R7   R/   R8   R¨   RM   RO   RR   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   nanstd  s
    c         C` s'  t  | ƒ |  j k s$ |  j d k r* d n | d } |  d } |  d } | d k r | | d | d | ƒ} | j ƒ  | } | j ƒ  | } nŽ | | d | ƒ} t j t t t | j	 ƒ ƒ } | j
 | | ƒ t | ƒ } | | } | | } | rt j | | ƒ } t j | | ƒ } n  | | f S(   s4    Merge intermediate results from ``arg_*`` functionsi   i    t   valst   argR7   R8   N(   R9   RC   R@   t   ravelRp   t   ogridRA   Re   t   sliceR…   t   insertt   expand_dims(   t   dataR7   t   argfuncR8   Rµ   R¶   t
   local_argsRP   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   _arg_combine  s"    4



c         C` sˆ  t  | ƒ | j k s$ | j d k r* d  n | d } |  | d | d t ƒ} | | d | d t ƒ} | d  k rÐ | \ } }	 t j | j ƒ  d | j ƒ }
 t d „  t	 | |
 ƒ Dƒ ƒ } t j
 | |	 ƒ | (n
 | | 7} t | t j j ƒ r=d | j k rt j j | ƒ } n t j j | ƒ } t j j | | ƒ } n  t j d | j d d	 | j f d
 | j f g ƒ } | | d	 <| | d
 <| S(   Ni   i    R7   R8   c         s` s   |  ] \ } } | | Vq d  S(   N(    (   R:   Rª   R;   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>:  s    Ru   R…   R/   Rµ   R¶   (   R9   RC   R@   RG   Rp   t   unravel_indexR·   R…   RA   Rf   t   ravel_multi_indexRD   t   mat   masked_arrayt   __name__t   minimum_fill_valuet   maximum_fill_valuet   filledR'   R/   (   R]   R½   R+   R7   t   offset_infot   arg_axisRµ   R¶   t   offsett   total_shapet   indt	   total_indt
   fill_valueRR   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt	   arg_chunk3  s&    4


c         K` si   t  | | | d t ƒ\ } } t j d | j d d | j f d | j f g ƒ } | | d <| | d <| S(   NR8   R…   R/   Rµ   R¶   (   R¿   RG   Rp   R'   R…   R/   (   R]   R½   R¼   R7   R‹   R¶   Rµ   RR   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   arg_combineM  s    

c         K` s   t  | | | d t ƒd S(   NR8   i    (   R¿   Rˆ   (   R]   R½   R¼   R7   R‹   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   arg_aggV  s    c         K` sI   t  | | | d t ƒ\ } } t j t j | ƒ ƒ rE t d ƒ ‚ n  | S(   NR8   s   All NaN slice encountered(   R¿   Rˆ   Rp   Rw   RŽ   RE   (   R]   R½   R¼   R7   R‹   R¶   Rµ   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt
   nanarg_aggZ  s    c         ` s3  ˆ  d k r* t t |  j ƒ ƒ ‰  t } nQ t ˆ  t ƒ rf t ˆ  |  j ƒ ‰  ˆ  f ‰  |  j d k } n t d j	 ˆ  ƒ ƒ ‚ xQ ˆ  D]I } |  j
 | }	 t |	 ƒ d k r‚ t j |	 ƒ j ƒ  r‚ t d ƒ ‚ q‚ q‚ Wd j	 t ˆ  |  ˆ | | ƒ ƒ ‰ |  j ‰ t t t t |  j ƒ Œ  ƒ }
 t t d „  |  j
 Dƒ Œ  ƒ } | rWt | t |  j ƒ ƒ } n t ˆ  d | ƒ } t ‡  f d †  t |  j
 ƒ Dƒ ƒ }	 t ‡  ‡ ‡ ‡ f d †  t |
 | ƒ Dƒ ƒ } t j ˆ | d	 |  g ƒ} t | ˆ |	 d
 |  j ƒ} t j  d g ƒ j } t! | | ˆ  t" | | | ƒ } t# | | ƒ S(   s8   Generic function for argreduction.

    Parameters
    ----------
    x : Array
    chunk : callable
        Partialed ``arg_chunk``.
    combine : callable
        Partialed ``arg_combine``.
    agg : callable
        Partialed ``arg_agg``.
    axis : int, optional
    split_every : int or dict, optional
    i   s,   axis must be either `None` or int, got '{0}'sŒ   Arg-reductions do not work with arrays that have unknown chunksizes.  At some point in your computation this array lost chunking informations   arg-reduce-{0}c         s` s(   |  ] } t  t j | d   d ƒ Vq d S(   iÿÿÿÿi    N(   R   R}   R~   (   R:   t   bd(    (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>‰  s   i    c         3` s7   |  ]- \ } } | ˆ  k r+ d t  | ƒ n | Vq d S(   i   N(   i   (   R9   (   R:   R;   R<   (   R7   (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>  s    c         3` s;   |  ]1 \ } } ˆ f | ˆ ˆ f | ˆ  | f f Vq d  S(   N(    (   R:   RT   t   off(   R7   R   R>   t   old(    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>’  s    Rc   R/   N($   R@   RA   RB   RC   RG   RD   R
   R   t	   TypeErrort   formatRI   R9   Rp   RŽ   Rw   RE   R    R>   Rd   R   Re   RY   Rf   R   R…   R   RH   RV   R!   Rg   R   R/   t   argminRK   Rˆ   R   (   R+   R   RN   t   aggR7   RM   RO   R·   t   axRI   Ri   t   offsetsRÈ   Rl   Ro   RQ   R/   RR   (    (   R7   R   R>   RÕ   s4   lib/python2.7/site-packages/dask/array/reductions.pyt   arg_reductiona  s@    			'	c         ` s   t  t |  | ƒ ‰ t  t |  | ƒ ‰ | r? t  t |  | ƒ ‰  n t  t |  | ƒ ‰  t | ƒ d d d ‡  ‡ ‡ f d † ƒ } | S(   s¿    Create an argreduction callable

    Parameters
    ----------
    func : callable
        The reduction (e.g. ``min``)
    argfunc : callable
        The argreduction (e.g. ``argmin``)
    c      
   ` s"   t  |  ˆ ˆ ˆ  | d | d | ƒS(   NRM   RO   (   RÜ   (   R+   R7   RM   RO   (   RÙ   R   RN   (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   _­  s    N(   R   RÏ   RÐ   RÒ   RÑ   R   R@   (   R]   R½   t   is_nan_funcRÝ   (    (   RÙ   R   RN   s4   lib/python2.7/site-packages/dask/array/reductions.pyt   make_arg_reductionœ  s    
	!c         K` sZ   y t  j |  | |  SWn< t k
 rU t  j t j t j |  ƒ t j |  ƒ | |  SXd  S(   N(   R   t	   nanargminRE   Rp   t   whereRŽ   t   inf(   R+   R7   R‹   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt
   _nanargminµ  s    c         K` s[   y t  j |  | |  SWn= t k
 rV t  j t j t j |  ƒ t j |  ƒ | |  SXd  S(   N(   R   t	   nanargmaxRE   Rp   Rá   RŽ   Râ   (   R+   R7   R‹   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt
   _nanargmax¼  s    c      	   ` s   ˆ  d
 k r! ˆ j ƒ  ‰ d ‰  n  | d
 k rZ t |  t j d d ˆ j ƒƒ d t ƒ } n  t ˆ  t ƒ so t	 ‚ t
 ˆ  ˆ j ƒ ‰  ˆ j |  d ˆ  d | ƒ} d j |  j t |  ˆ  | | ˆ | ƒ ƒ } ˆ j ˆ  }	 t d
 d
 d
 ƒ }
 |
 f ˆ  t d d
 ƒ f |
 f ˆ j ˆ  d } t t g  t ˆ j ƒ D]- \ } } | ˆ  k rRt | ƒ n d g ^ q.Œ  ƒ } t ƒ  } xt | D]l } t ‡  ‡ f d †  t | ƒ Dƒ ƒ } t j | | | j f | | d f | <| j f | | | f | <qwWxó t d |	 ƒ D]â } | } t t g  t ˆ j ƒ D]- \ } } | ˆ  k r=t | ƒ n | g ^ qŒ  ƒ } x„ t | | ƒ D]s \ } } | d f | } | | d f | t j | j f | | f f | | <| | | j f | f | | f | <qbWq÷Wt j | | d	 | g ƒ} t | | ˆ j  | j ƒ } t! | | ƒ S(   sä   Generic function for cumulative reduction

    Parameters
    ----------
    func: callable
        Cumulative function like np.cumsum or np.cumprod
    binop: callable
        Associated binary operator like ``np.cumsum->add`` or ``np.cumprod->mul``
    ident: Number
        Associated identity like ``np.cumsum->0`` or ``np.cumprod->1``
    x: dask Array
    axis: int
    dtype: dtype

    Returns
    -------
    dask array

    See also
    --------
    cumsum
    cumprod
    i    R/   R7   s   {0}-{1}iÿÿÿÿi   c         3` s8   |  ]. \ } } | ˆ  k r, ˆ j  | | n d  Vq d S(   i   N(   RI   (   R:   R;   t   ii(   R7   R+   (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>õ  s   t   extraRc   N(   i    ("   R@   t   flattenR)   Rp   R'   R/   Rr   RD   R
   t   AssertionErrorR   RC   t
   map_blocksR×   RÄ   R    RY   R¹   Rd   R   RH   RB   RV   RA   RŠ   R>   Rf   R}   t   getitemR!   Rg   R   RI   R   (   R]   t   binopt   identR+   R7   R/   RO   t   mR>   R[   RŠ   t   slcR;   t   nbt   indicesRl   RÌ   R…   t   last_indicesRæ   RÕ   t
   this_sliceRo   RR   (    (   R7   R+   s4   lib/python2.7/site-packages/dask/array/reductions.pyR{   É  sB    	-3	F	&	F#,c         C` sv   t  |  t j j ƒ s* t  | t j j ƒ rn t j j |  ƒ t j j | ƒ } t j j | d t j j | ƒ ƒS|  | S(   NRƒ   (   RD   Rp   RÂ   RÃ   t   getdatat   getmaskarray(   R4   R5   t   values(    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   _cumsum_merge	  s    *""c         C` sv   t  |  t j j ƒ s* t  | t j j ƒ rn t j j |  ƒ t j j | ƒ } t j j | d t j j | ƒ ƒS|  | S(   NRƒ   (   RD   Rp   RÂ   RÃ   Rô   Rõ   (   R4   R5   Rö   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   _cumprod_merge  s    *""c      	   C` s"   t  t j t d |  | | d | ƒS(   Ni    RO   (   R{   Rp   t   cumsumR÷   (   R+   R7   R/   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRù     s    c      	   C` s"   t  t j t d |  | | d | ƒS(   Ni   RO   (   R{   Rp   t   cumprodRø   (   R+   R7   R/   RO   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRú     s    iÿÿÿÿc         C` s   t  | |  j ƒ } t t j d | ƒ} t t j d | ƒ} t |  d | d | d | d | d t d |  j d | d	 t	 | ƒ ƒS(
   s   Extract the k largest elements from a on the given axis,
    and return them sorted from largest to smallest.
    If k is negative, extract the -k smallest elements instead,
    and return them sorted from smallest to largest.

    This performs best when ``k`` is much smaller than the chunk size. All
    results will be returned in a single chunk along the given axis.

    Parameters
    ----------
    x: Array
        Data being sorted
    k: int
    axis: int, optional
    split_every: int >=2, optional
        See :func:`reduce`. This parameter becomes very important when k is
        on the same order of magnitude of the chunk size or more, as it
        prevents getting the whole or a significant portion of the input array
        in memory all at once, with a negative impact on network transfer
        too when running on distributed.

    Returns
    -------
    Selection of x with size abs(k) along the given axis.

    Examples
    --------
    >>> import dask.array as da
    >>> x = np.array([5, 1, 3, 6])
    >>> d = da.from_array(x, chunks=2)
    >>> d.topk(2).compute()
    array([6, 5])
    >>> d.topk(-2).compute()
    array([1, 3])
    RT   R   RN   RL   R7   R8   R/   RM   R=   (
   R   RC   R   R   t   topkt   topk_aggregateRS   RG   R/   t   abs(   R4   RT   R7   RM   t   chunk_combineRL   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyRû   !  s    $c         ` sõ   t  ˆ  |  j ƒ ‰  t |  j ˆ  d |  j ˆ  f d t j ƒ} | t ‡  f d †  t |  j ƒ Dƒ ƒ } |  j	 t
 j | d t ƒ} t t
 j d | ƒ} t t
 j d | ƒ} t | d | d | d | d ˆ  d	 t d t j d
 | d t d t | ƒ ƒ	S(   sÌ   Extract the indices of the k largest elements from a on the given axis,
    and return them sorted from largest to smallest. If k is negative, extract
    the indices of the -k smallest elements instead, and return them sorted
    from smallest to largest.

    This performs best when ``k`` is much smaller than the chunk size. All
    results will be returned in a single chunk along the given axis.

    Parameters
    ----------
    x: Array
        Data being sorted
    k: int
    axis: int, optional
    split_every: int >=2, optional
        See :func:`topk`. The performance considerations for topk also apply
        here.

    Returns
    -------
    Selection of np.intp indices of x with size abs(k) along the given axis.

    Examples
    --------
    >>> import dask.array as da
    >>> x = np.array([5, 1, 3, 6])
    >>> d = da.from_array(x, chunks=2)
    >>> d.argtopk(2).compute()
    array([3, 0])
    >>> d.argtopk(-2).compute()
    array([1, 2])
    RI   R/   c         3` s0   |  ]& } | ˆ  k r! t  d  ƒ n t j Vq d  S(   N(   R¹   R@   Rp   t   newaxis(   R:   R;   (   R7   (    s4   lib/python2.7/site-packages/dask/array/reductions.pys	   <genexpr>z  s   RT   R   RN   RL   R7   R8   RM   R?   R=   (   R   RC   R   R…   RI   Rp   t   intpRA   RB   Rê   R   t   argtopk_preprocessRr   R   t   argtopkt   argtopk_aggregateRS   RG   Rˆ   Rý   (   R4   RT   R7   RM   t   idxt
   a_plus_idxRþ   RL   (    (   R7   s4   lib/python2.7/site-packages/dask/array/reductions.pyR  U  s    !,	c         C` s+   t  |  d | d | d | ƒj d d | ƒS(   NRÊ   t   axis1t   axis2iÿÿÿÿR/   (   R   Rq   (   R4   RÊ   R  R  R/   (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   trace  s    ({   t
   __future__R    R   R   R}   t	   functoolsR   R   t	   itertoolsR   R   t   mathR   R   R	   t   numpyRp   t   numbersR
   t   toolzR   R   R   R   R   R-   R   t   coreR   R   R   R   R   t   creationR   R   t   ufuncR   t   utilsR   t   wrapR   R   t   numpy_compatR   R   t	   np_dividet   compatibilityR   R   t   baseR    t   highlevelgraphR!   R"   R#   R$   R%   R&   t   empty_lookupt   registerRr   t   ndarrayR'   RÂ   RÃ   R0   R@   Rˆ   RG   RS   RK   RZ   Rq   Rt   Ru   R3   Rw   Rx   Ry   t   AttributeErrorRz   R|   R   R   R‚   R   R   R’   R—   R˜   R™   Rš   R£   R§   R«   R®   R±   R¯   R²   R³   R´   R¿   RÏ   RÐ   RÑ   RÒ   RÜ   Rß   Rã   Rå   RØ   t   argmaxRà   Rä   R{   R÷   Rø   Rù   Rú   Rû   R  R  (    (    (    s4   lib/python2.7/site-packages/dask/array/reductions.pyt   <module>   sæ   ("	o!'
					
	
				;		@		4: