ó
ßüÚ\c           @` sF   d  d l  m Z m Z m Z d d l m Z d e f d „  ƒ  YZ d S(   i    (   t   print_functiont   divisiont   absolute_importi   (   t   SchedulerPlugint   GraphLayoutc           B` s8   e  Z d  Z d „  Z d d d „ Z d „  Z d „  Z RS(   s=   Dynamic graph layout during computation

    This assigns (x, y) locations to all tasks quickly and dynamically as new
    tasks are added.  This scales to a few thousand nodes.

    It is commonly used with distributed/bokeh/scheduler.py::GraphPlot, which
    is rendered at /graph on the diagnostic dashboard.
    c         C` sì   i  |  _  i  |  _ i  |  _ | |  _ i  |  _ i  |  _ d |  _ d |  _ d |  _ g  |  _	 g  |  _
 g  |  _ g  |  _ g  |  _ | j |  ƒ |  j j rè d „  | j j ƒ  Dƒ } d „  | j j ƒ  Dƒ } |  j |  j d | d | ƒn  d  S(   Ni    c         S` s8   i  |  ]. \ } } g  | j  D] } | j ^ q | “ q S(    (   t   dependenciest   key(   t   .0t   kt   tst   ds(    (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pys
   <dictcomp>$   s   	c         S` s"   i  |  ] \ } } | j  | “ q S(    (   t   priority(   R   R   R	   (    (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pys
   <dictcomp>'   s   	 R   R   (   t   xt   yt	   collisiont	   schedulert   indext
   index_edget   next_yt
   next_indext   next_edge_indext   newt	   new_edgest   state_updatest   visible_updatest   visible_edge_updatest
   add_plugint   taskst   itemst   update_graph(   t   selfR   R   R   (    (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pyt   __init__   s*    														c         ` sX  t  | d ‡  f d †  d t ƒ} x0| rS| j ƒ  } | ˆ j k s$ | ˆ j k rZ q$ n  | | } | r"t ‡ f d †  | Dƒ ƒ sÁ | j | ƒ | j t  | d ‡  f d †  d t ƒƒ q$ q@t ‡ f d †  | Dƒ ƒ ‰ t ‡ ‡ ‡ f d †  | Dƒ ƒ } t	 ‡ f d †  | Dƒ ƒ d	 }	 n d
 }	 ˆ j
 } ˆ j
 d	 7_
 |	 | f ˆ j k r¡|	 | }
 } ˆ j |	 | f \ }	 } | d 7} |	 | f ˆ j |
 | f <n |	 | f ˆ j |	 | f <|	 ˆ j | <| ˆ j | <ˆ j ˆ j | <ˆ j d	 ˆ _ ˆ j j | ƒ xI | D]A } | | f } ˆ j ˆ j | <ˆ j d	 7_ ˆ j j | ƒ qWq$ Wd  S(   NR   c         ` s   ˆ  j  |  d ƒ S(   Ni    (   t   get(   R   (   R   (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pyt   <lambda>-   t    t   reversec         3` s   |  ] } | ˆ  j  k Vq d  S(   N(   R   (   R   t   dep(   R   (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pys	   <genexpr>4   s    c         ` s   ˆ  j  |  d ƒ S(   Ni    (   R    (   R   (   R   (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pyR!   7   R"   c         3` s%   |  ] } t  ˆ  j | j ƒ Vq d  S(   N(   t   lenR   t
   dependents(   R   R$   (   R   (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pys	   <genexpr><   s    c         3` s4   |  ]* } ˆ j  | t ˆ  j | j ƒ ˆ Vq d  S(   N(   R   R%   R   R&   (   R   R$   (   R   R   t
   total_deps(    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pys	   <genexpr>?   s   c         3` s   |  ] } ˆ  j  | Vq d  S(   N(   R   (   R   R$   (   R   (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pys	   <genexpr>B   s    i   i    gš™™™™™¹?(   t   sortedt   Truet   popR   R   t   allt   appendt   extendt   sumt   maxR   R   R   R   R   R   R   R   R   (   R   R   R   R   t   kwargst   stackR   t   depsR   R   t   old_xt   old_yR$   t   edge(    (   R   R   R   R'   sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pyR   ,   sJ    !	
"#	
c   
      O` sJ  | d k r, |  j  j |  j | | f ƒ n|  j j |  j | d f ƒ |  j j | } xK | j D]@ } | | j f } |  j j |  j	 j
 | | j f ƒ d f ƒ qc Wx< | j D]1 } |  j j |  j	 j
 | j | f ƒ d f ƒ q± Wy" |  j |  j | |  j | f =Wn t k
 rn Xx' |  j |  j |  j g D] }	 |	 | =q5Wd  S(   Nt	   forgottent   False(   R   R,   R   R   R   R   R&   R   R   R   R*   R   R   R   R   t   KeyError(
   R   R   t   startt   finisht   argsR0   t   taskR$   R5   t
   collection(    (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pyt
   transition[   s"     	&	&"c         C` sô   g  |  _  g  |  _ g  |  _ g  |  _ g  |  _ i  |  _ d |  _ i  |  _ d |  _ xœ |  j	 D]‘ } |  j |  j | <|  j d 7_ |  j  j
 | ƒ xY |  j j | j D]D } | j | f } |  j |  j | <|  j d 7_ |  j j
 | ƒ q¤ Wq[ Wd S(   s®    Reset the index and refill new and new_edges

        From time to time GraphPlot wants to remove invisible nodes and reset
        all of its indices.  This helps.
        i    i   N(   R   R   R   R   R   R   R   R   R   R   R,   R   R   R   R   (   R   R   R$   R5   (    (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pyt   reset_indexs   s$    									N(   t   __name__t
   __module__t   __doc__R   t   NoneR   R>   R?   (    (    (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pyR      s
   	/	N(   t
   __future__R    R   R   t   pluginR   R   (    (    (    sC   lib/python2.7/site-packages/distributed/diagnostics/graph_layout.pyt   <module>   s   