ó
¦–Õ\c           @   s´  d  d l  Z  d  d l Z d  d l m Z m Z d  d l m Z d  d l m Z m	 Z	 e  j
 d d d g ƒ d „  ƒ Z e d	 „ Z d
 „  Z d „  Z e  j j d d ƒ d „  ƒ Z d „  Z e  j j d d ƒ d „  ƒ Z d „  Z e  j j d d ƒ d „  ƒ Z d „  Z d „  Z e  j j d d ƒ d „  ƒ Z d „  Z d „  Z d „  Z d „  Z e  j j d d ƒ d „  ƒ Z d „  Z d „  Z  d „  Z! d  „  Z" d! „  Z# d" „  Z$ d# „  Z% d S($   iÿÿÿÿN(   t   ndependenciest   order(   t   get_deps(   t   addt   inct   paramst   abcdet   edcbac         C   s   |  j  S(   N(   t   param(   t   request(    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyR   	   s    c         C   s   t  |  d | ƒ|  k S(   Nt   reverse(   t   sorted(   t   LR
   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   issorted   s    c          G   s   d  S(   N(    (   t   args(    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   f   s    c            s×  |  \ ‰  } } } } t  ‡  f d †  t d ƒ Dƒ ƒ } | j i t ˆ  d f ˆ  d f f | d f 6t ˆ  d f ˆ  d f f | d f 6ƒ t | ƒ } t | ˆ  d f | ˆ  d f ƒ d k sÄ t ‚ t | ˆ  d f | ˆ  d f ƒ d k sô t ‚ t  ‡  f d †  t d ƒ Dƒ ƒ } | j i t ˆ  d f ˆ  d f f | d f 6t ˆ  d f ˆ  d f f | d f 6ƒ t | ƒ } t | ˆ  d f | ˆ  d f ƒ d k s£t ‚ t | ˆ  d f | ˆ  d f ƒ d k sÓt ‚ d  S(   Nc         3   s$   |  ] } ˆ  | f t  f f Vq d  S(   N(   R   (   t   .0t   i(   t   a(    s4   lib/python2.7/site-packages/dask/tests/test_order.pys	   <genexpr>   s    i   i    i   i   i   c         3   s$   |  ] } ˆ  | f t  f f Vq d  S(   N(   R   (   R   R   (   R   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys	   <genexpr>    s    (   t   dictt   ranget   updateR   R   t   abst   AssertionError(   R   t   bt   ct   dt   et   o(    (   R   s4   lib/python2.7/site-packages/dask/tests/test_order.pyt#   test_ordering_keeps_groups_together   s    "+&00"+&0t   reasons   Can't please 'em allc         C   s_  |  \ } } } } } i t  f | d f 6t  f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f f | d f 6} t | ƒ } | | d f | | d f k  s¸ t ‚ i t  f | d f 6t  f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f f | d f 6} t | ƒ } | | d f | | d f k s[t ‚ d S(   sV   

    b0    b1  b2
    |      \  /
    a0      a1

    a0 should be run before a1
    i    i   i   N(   R   R   R   (   R   R   R   R   R   R   t   dskR   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_avoid_broker_nodes)   s    
#N&#Nc            sÑ   |  \ ‰  ‰ ‰ } } ‡  ‡ f d †  d d d g Dƒ } t  ˆ d f f | ˆ  d f <| j ‡ ‡ f d †  d d d d g Dƒ ƒ d | ˆ <t | ƒ } | ˆ d f d k s± t ‚ | ˆ d f d k sÍ t ‚ d	 S(
   sã   
               a3
              /|
            a2 |
           /|  |
         a1 |  |
        /|  |  |
      a0 |  |  |
      |  |  |  |
      b0 b1 b2 b3
        \ \ / /
           c

    We really want to run b0 quickly
    c            s8   i  |  ]. } t  ˆ  | d  f ˆ | f f ˆ  | f “ q S(   i   (   R   (   R   R   (   R   R   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys
   <dictcomp>U   s   	 i   i   i   i    c            s(   i  |  ] } t  ˆ d  f ˆ  | f “ q S(   i   (   R   (   R   R   (   R   R   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys
   <dictcomp>W   s   	 i   i   N(   R   R   R   R   (   R   R   R   R   R   (    (   R   R   R   s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_base_of_reduce_preferredD   s    ",
c         C   s	  |  \ } } } } } i t  | d f f | d f 6t  | d f f | d f 6t  | d f | d f f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f f | d f 6} t | ƒ } | | d f | | d f k  st ‚ d S(   sî   
         a1
         |
         a2
         |
         a3    d1
        /  \  /
      b1    c1
      |     |
      b2    c2
            |
            c3

    Prefer b1 over c1 because it won't stick around waiting for d1 to complete
    i   i   i   N(   R   R   R   (   R   R   R   R   R   R   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_avoid_upwards_branching`   s    "c         C   s  |  \ } } } } } i t  | d f f | d f 6t  | d f f | d f 6t  | d f | d f f | d f 6t  | d f f | d f 6t  f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f f | d f 6} t | ƒ } | | d f | | d f k  s‰t ‚ d S(   s  
         a1
         |
    e2   a2  d2  d3
    |    |    \  /
    e1   a3    d1
     \  /  \  /
      b1    c1
      |     |
      b2    c2
            |
            c3

    Prefer c1 over b1 because c1 will stay in memory less long while b1
    computes
    i   i   i   N(   R   R   R   (   R   R   R   R   R   R   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt$   test_avoid_upwards_branching_complex   s     "s   this case is ambiguousc         C   s±   |  \ } } } } } i t  | | | f | 6t  | | f | 6t  | f | 6d | 6d | 6} t | ƒ } | | | | k  s t ‚ | | | | k  s­ | | | | k  s­ t ‚ d S(   s@  
    It's not clear who should run first, e or d

    1.  d is nicer because it exposes parallelism
    2.  e is nicer (hypothetically) because it will be sooner released
        (though in this case we need d to run first regardless)

            a
          / | \   .
         b  c |
        / \ | /
       e    d
    i   i   N(   R   R   R   (   R   R   R   R   R   R   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt#   test_deep_bases_win_over_dependents¤   s
    Dc         C   s”   |  \ } } } } } i d | 6t  | f | 6t  | f | 6d | 6t  | f | 6} t | ƒ } | | | | k  sv t ‚ | | | | k  s t ‚ d S(   sq   
        c
        |
    e   b
    |   |
    d   a

    Prefer longer chains first so we should start with c
    i   N(   R   R   R   (   R   R   R   R   R   R   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_prefer_deep»   s    
$c         C   s?   t  d „  t d ƒ Dƒ ƒ } t | ƒ \ } } t | | ƒ d  S(   Nc         s   s-   |  ]# } d  | d t  d  | f f Vq d S(   s   x%si   N(   R   (   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys	   <genexpr>Ï   s    i'  (   R   R   R   R    (   R   R   t   dependenciest
   dependents(    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_stacklimitÎ   s    c   
      C   s“   |  \ } } } } } d „  t  d ƒ Dƒ } t | ƒ } t | ƒ | d <t | ƒ } i d d 6}	 |	 j d „  t | ƒ Dƒ ƒ | |	 k s t ‚ d  S(   Nc         S   s%   i  |  ] } t  | f d  | f “ q S(   t   x(   R   (   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys
   <dictcomp>×   s   	 i
   t   yi    c         S   s#   i  |  ] \ } } | d  | “ q S(   i   (    (   R   R   t   k(    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys
   <dictcomp>Ý   s   	 (   R   R   t   listR   R   t	   enumerateR   (
   R   R   R   R   R   R   R   t   x_keysR   t   expected(    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_break_ties_by_strÔ   s    c         C   s8   t  i t d f d 6t d f d 6t d d f d 6ƒ d  S(	   Ni   R)   i   R*   i    t   z(   R*   i    (   R*   i    (   R   R   R   (   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt)   test_order_doesnt_fail_on_mixed_type_keysâ   s    c             sÈ  t  j d ƒ }  d \ } } |  j j d | | f d d ƒ } } xn t d ƒ D]` } | d  d  … d  d  d  … f | d  d  … d  d  … d  f j d d	 ƒ } | j d d	 ƒ } qM W| | d  d  … d  f j d d ƒ } t | j	 ƒ  ƒ } t
 | ƒ ‰  g  | j ƒ  D] }	 ˆ  |	 ^ q}
 t ‡  f d
 †  |
 Dƒ ƒ t |
 ƒ d k sJt ‚ g  | j ƒ  D] } | D] }	 ˆ  |	 ^ qaqW}
 t ‡  f d †  |
 Dƒ ƒ t |
 ƒ d k s¬t ‚ t |
 ƒ |
 k sÄt ‚ d  S(   Ns
   dask.arrayi   ic   t   sizet   chunksi   i   t   axisi    c         3   s%   |  ] } | t  ˆ  ƒ d  k  Vq d S(   i   N(   t   len(   R   R)   (   R   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys	   <genexpr>ô   s    i   c         3   s%   |  ] } | t  ˆ  ƒ d  k Vq d S(   i   N(   R6   (   R   R)   (   R   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys	   <genexpr>÷   s    (   i   ic   (   i   N(   i   i   (   t   pytestt   importorskipt   randomt   normalt   NoneR   t   cumsumt   sumR   t   __dask_graph__R   t   __dask_keys__R6   R   R   (   t   dat   At   Bt   origR)   t   _R*   t   wR   R+   R   t   kk(    (   R   s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_gh_3055è   s    %H&#202c         C   sM   |  \ } } } } } i d | 6d | d f 6d | | d f 6} t  | ƒ d  S(   Ni   i   i   (   R   (   R   R   R   R   R   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_type_comparisions_okü   s    *c         C   s   |  \ } } } } } i t  f | 6t  | f | 6t  | f | 6t  | f | 6t  | f | 6} t | ƒ } | | | | k  s t ‚ | | | | k  s™ t ‚ d S(   s±   

         a
         |
      d  b  e
       \ | /
         c

    Prefer to finish d and e before starting b.  That way c can be released
    during the long computations.
    N(   R   R   R   (   R   R   R   R   R   R   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_prefer_short_dependents  s
    Ds#   This is challenging to do preciselyc      	      s]  |  \ } } } } } g  | | | | g D] } | d ^ q( \ } } }	 }
 | | | | | |	 | | |
 g	 } g  ‰  ‡  f d †  } i	 | | ƒ f | 6| | ƒ f | 6| | ƒ f | 6| |	 ƒ f |	 6| | ƒ | | f | 6| | ƒ | | f | 6| | ƒ |	 f | 6| | ƒ | | f | 6| |
 ƒ |	 f |
 6} t  j | | | |
 g ƒ ˆ  | k sYt ‚ d S(   s    
            aa
           / |
      b   d  bb dd
     / \ /|  | /
    a   c e  cc

    Prefer to run acb first because then we can get that out of the way
    i   c            s   ‡ ‡  f d †  } | S(   Nc             s   ˆ  j  ˆ ƒ d  S(   N(   t   append(   R   (   t   logR)   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyRD   )  s    (    (   R)   RD   (   RK   (   R)   s4   lib/python2.7/site-packages/dask/tests/test_order.pyR   (  s    N(   t   daskt   getR   (   R   R   R   R   R   R   R)   t   aat   bbt   cct   ddR/   R   R   (    (   RK   s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_run_smaller_sections  s     5!c      	      s¤  |  \ } } } } } g  d D] } | | ^ q \ } } }	 g  d D] } | | ^ qB \ }
 } } g  d D] } | | ^ qh \ } } } |	 | | | | |
 | | | g	 } g  ‰  ‡  f d †  } i	 | |	 ƒ f |	 6| | ƒ |	 f | 6| | ƒ | f | 6| | ƒ f | 6| | ƒ | | f | 6| |
 ƒ | f |
 6| | ƒ f | 6| | ƒ | | f | 6| | ƒ | f | 6} t  | ƒ t j | | |
 | g ƒ ˆ  | k s t ‚ d S(   s§   

            c1
            |
        b1  c2
        |  /|
    a1  b2  c3
    |  /|
    a2  b3
    |
    a3

    Prefer to finish a1 stack before proceding to b2
    t   123c            s   ‡ ‡  f d †  } | S(   Nc             s   ˆ  j  ˆ ƒ d  S(   N(   RJ   (   R   (   RK   R)   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyRD   W  s    (    (   R)   RD   (   RK   (   R)   s4   lib/python2.7/site-packages/dask/tests/test_order.pyR   V  s    N(   R   RL   RM   R   (   R   R   R   R   R   R   R   t   a1t   a2t   a3t   b1t   b2t   b3t   c1t   c2t   c3R/   R   R   (    (   RK   s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_local_parents_of_reduction<  s*    &&&		
c      	      sÐ  |  \ ‰  ‰ } } } g  d D] } ˆ  | ^ q \	 } } } } } }	 }
 } } g  d D] } ˆ | ^ qT \ } } } } i t  f | 6t  f | 6t  f | 6t  f | 6t  | f | 6t  | f | 6t  | | f | 6t  | f | 6t  | | f | 6t  | f |	 6t  | | f |
 6t  | f | 6t  | f | 6} t | ƒ ‰ d t ‡  ‡ f d †  d Dƒ ƒ k  o^d k  n sit ‚ d t ‡ ‡ f d †  d Dƒ ƒ k  o™d k  n s¤t ‚ ˆ t | | | | g ƒ d	 k sÌt ‚ d
 S(   sâ   

    a1  a2  a3  a4  a5  a6  a7 a8  a9
     \  |  /  \ |  /  \ |  / \ |  /
        b1      b2      b3     b4

    Want to finish off a local group before moving on.
    This is difficult because all groups are connected.
    t	   123456789t   1234i   c         3   s-   |  ]# } ˆ ˆ  | t  ˆ ƒ d  k  Vq d S(   i   N(   R6   (   R   R   (   R   R   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys	   <genexpr>‰  s    i   i   c         3   s-   |  ]# } ˆ ˆ  | t  ˆ ƒ d  k  Vq d S(   i   N(   R6   (   R   R   (   R   R   (    s4   lib/python2.7/site-packages/dask/tests/test_order.pys	   <genexpr>Š  s    i   i    N(   R   R   R=   R   t   min(   R   R   RD   R   RT   RU   RV   t   a4t   a5t   a6t   a7t   a8t   a9RW   RX   RY   t   b4R   (    (   R   R   R   s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_nearest_neighbork  s(    
8)


;;c          C   s[   i t  f d 6t  f d 6t  f d	 6}  t |  ƒ } | i d d
 6d d 6d d 6k sW t ‚ d S(   s%    Prefer ordering tasks by name first R   i   i   i   i    N(   R   i   (   R   i   (   R   i   (   R   i   (   R   i   (   R   i   (   R   R   R   (   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_string_orderingŽ  s
    $c          C   su   i t  d f d 6t  d f d	 6t  d f d
 6t  f d 6}  t |  ƒ } | i d d 6d d 6d d 6d d 6k sq t ‚ d S(   s?    Prefer ordering tasks by name first even when in dependencies R   R   i   i   i   i    N(   R   i   (   R   i   (   R   i   (   R   i   (   R   i   (   R   i   (   R   R   R   (   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_string_ordering_dependents—  s    *c         C   s4  |  \ } } } } } i d | d f 6d | d f 6d | d f 6t  | d f | d f | d f f | d f 6d | d f 6d | d f 6t  | d f | d f | d f f | d f 6} t | ƒ } | | d f | | d f k  sä t ‚ | | d f | | d f k  s
t ‚ | | d f | | d f k  s0t ‚ d  S(   Ni    i   i   (   R   R   R   (   R   R   R   R   RD   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_prefer_short_narrow¢  s    +.&&c         C   s›  |  \ } } } } } | | } i d | 6t  | d d f | d f 6t  | d d f | d f 6d | d f 6t  | d f | d f | d f f | d f 6t  | d d f | d f 6t  | d d f | d f 6t  | d f | d f | d f f | d f 6} t | ƒ } | | d f | | d f k  s%t ‚ | | d f | | d f k  sKt ‚ | | d f | | d f k  sqt ‚ | | d f | | d f k  s—t ‚ d S(   s˜  
    From https://github.com/dask/dask-ml/issues/206#issuecomment-395869929

    Two cases, one where chunks of an array are independent, and one where the
    chunks of an array have a shared source. We handled the independent one
    "well" earlier.

    Good:

                    c2
                   / \ \
                  /   \ \
                c1     \ \
              / | \     \ \
            c0  a0 b0   a1 b1

    Bad:

                    c2
                   / \ \
                  /   \ \
                c1     \ \
              / | \     \ \
            c0  a0 b0   a1 b1
                   \ \   / /
                    \ \ / /
                      a-b


    The difference is that all the `a` and `b` tasks now have a common
    ancestor.

    We would like to choose c1 *before* a1, and b1 because

    * we can release a0 and b0 once c1 is done
    * we don't need a1 and b1 to compute c1.
    i    i   i   N(   R   R   R   (   R   R   R   R   RD   t   abR   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_prefer_short_ancestor´  s     &
+.&&&c         C   s  |  \ } } } } } i t  f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f | d f f | d f 6t  f | d f 6t  | d f | d f | d f f | d f 6t  f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f | d f | d f f | d f 6t  f | d f 6t  | d f | d f | d f f | d f 6t  f | d f 6t  | d f f | d f 6t  | d f f | d f 6t  | d f | d f f | d f 6} t | ƒ } | | d f | | d f k  s| | d f | | d f k  st ‚ d S(   sÞ   
      b1      b3      b5
       |\    / | \  / |
      c1  c2  c3  c4  c5
       |/  | \ | / | \|
      d1  d2  d3  d4  d5
       |       |      |
      e1      e2      e5

    Want to finish b1 before we start on e5
    i   i   i   i   i   N(   R   R   R   (   R   R   R   R   R   R   R   R   (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   test_map_overlapï  s(    "+++%(&   R7   RL   t
   dask.orderR    R   t	   dask.coreR   t   dask.utils_testR   R   t   fixtureR   t   FalseR   R   R   t   markt   xfailR    R!   R"   R#   R$   R%   R(   R0   R2   RG   RH   RI   RR   R]   Rh   Ri   Rj   Rk   Rm   Rn   (    (    (    s4   lib/python2.7/site-packages/dask/tests/test_order.pyt   <module>   s8   !				%						&	/	#					;