U
    /e
>                     @   s   d dl Z d dl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mZmZ d dlmZ d dlmZmZmZ dd	 Zd
Zdd ZdddZdd Zd ddZd!ddZd"ddZdd Zd#ddZdS )$    N)bisect_left)cycle)add
itemgetter)
accumulategroupbypluckunique)istask)applyfuncnameimport_requiredc                  C   s   dd l } ddlm} || jS )Nr   )parse)bokehZpackaging.versionr   __version__)r   parse_version r   F/tmp/pip-unpacked-wheel-dbjnr7gq/dask/diagnostics/profile_visualize.pyBOKEH_VERSION   s    r   z1Diagnostics plots require `bokeh` to be installedc                 C   sr   t | rn| d tttfkr0| d tt| d S | d tkrnt| d trnt| d d trnttt| d S | S )Nr      )r
   tuplelistsetmapunquotedict
isinstance)exprr   r   r   r      s    
r   <   c           	         sD  t | r|| d }|tkrdt| d }d}t| dkrBt| d nd}t| dkr^t| d ni }nPt|drdd	d
 |jD }dt|j }nt| d }d}| dd }i }|s|rt|t| t| t|t|   fdd|r"dkrdfdd
|D }nd}nd}|rbdkr\ddfdd
t	|
 D  }nd}nd}| d| | | S t| tr| sdS t| dkrt| dd  |}|dd d S t|d dt|   t|  d fdd
| D }d| dS n4z|  krW dS W dS W n tk
r>   Y dS X dS )a?  Return a nicely formatted string for a task.

    Parameters
    ----------
    task:
        Value within dask graph to render as text
    keys: iterable
        List of keys within dask graph
    label_size: int (optional)
        Maximum size of output label, defaults to 60

    Examples
    --------
    >>> from operator import add, mul
    >>> dsk = {'a': 1,
    ...        'b': 2,
    ...        'c': (add, 'a', 'b'),
    ...        'd': (add, (mul, 'a', 'b'), 'c'),
    ...        'e': (sum, ['a', 'b', 5]),
    ...        'f': (add,),
    ...        'g': []}

    >>> pprint_task(dsk['c'], dsk)
    'add(_, _)'
    >>> pprint_task(dsk['d'], dsk)
    'add(mul(_, _), _)'
    >>> pprint_task(dsk['e'], dsk)
    'sum([_, _, *])'
    >>> pprint_task(dsk['f'], dsk)
    'add()'
    >>> pprint_task(dsk['g'], dsk)
    '[]'
    r   r   )   r      funcs(c                 s   s   | ]}t |V  qd S N)r   .0fr   r   r   	<genexpr>O   s     zpprint_task.<locals>.<genexpr>Nc                    s   t |  S r$   pprint_task)tkeyslabel_size2r   r   <lambda>Z       zpprint_task.<locals>.<lambda>   z, c                 3   s   | ]} |V  qd S r$   r   r&   r+   pprintr   r   r(   ]   s     z... c                 3   s$   | ]\}}| d  | V  qdS )=Nr   r&   kvr3   r   r   r(   d   s    z, ...z[]z, ...]c                 3   s   | ]}t | V  qd S r$   r)   r2   r,   r   r   r(   t   s     []_*)r
   r   r   lenr   hasattrjoinr"   intsorteditemsr   r   r*   	TypeError)	Ztaskr-   
label_sizefuncheadtailargskwargsresultr   )r-   r.   r4   r   r*   $   s^    "

"


 

r*   c                    s   t dt}tt|}t|}|j|  }tt| }|tt	||t|d  }|| } tt| } t| |krt
d|  tt|t|   fdd|D S )a  Get a dict mapping funcs to colors from palette.

    Parameters
    ----------
    palette : string
        Name of the bokeh palette to use, must be a member of
        bokeh.palettes.all_palettes.
    funcs : iterable
        Iterable of function names
    zbokeh.palettesr   *   c                    s   g | ]} | qS r   r   )r&   nZcolor_lookupr   r   
<listcomp>   s     zget_colors.<locals>.<listcomp>)r   _BOKEH_MISSING_MSGrC   r	   r?   all_palettesr   r-   minr   randomRandomshuffler   zipr   )paletter"   palettesZunique_funcsZn_funcsZpalette_lookupr-   indexr   rO   r   
get_colors   s    

r[   profile.htmlTc                    s  t dt}ddlm} d kr8tjdtdd  d}|dkrL| j	 }t
| ts\| g}  fd	d
| D }t|dkr|d }	n|d }
|dd D ]B}|
j|_d|_d|_t jdk r| jd8  _q| jd8  _q|dd D ]>}d|j_d|_t jdk r| jd8  _q| jd8  _q|D ]}d|_d|_q,|dd
 |D }	|rd||	 |r|j||d ||	 |	S )a  Visualize the results of profiling in a bokeh plot.

    If multiple profilers are passed in, the plots are stacked vertically.

    Parameters
    ----------
    profilers : profiler or list
        Profiler or list of profilers.
    filename : string, optional
        Name of the plot output file.
    show : boolean, optional
        If True (default), the plot is opened in a browser.
    save : boolean, optional
        If True (default when not in notebook), the plot is saved to disk.
    mode : str, optional
        Mode passed to bokeh.output_file()
    **kwargs
        Other keyword arguments, passed to bokeh.figure. These will override
        all defaults set by visualize.

    Returns
    -------
    The completed bokeh plot object.
    bokeh.plottingr   )state	file_pathzrThe file_path keyword argument is deprecated and will be removed in a future release. Please use filename instead.r    )category
stacklevelNc                    s   g | ]}|j f  qS r   )Z_plot)r&   ZprofrK   r   r   rP      s     zvisualize.<locals>.<listcomp>r      r!      r:   K   c                 S   s   g | ]
}|gqS r   r   r%   r   r   r   rP      s     )mode)r   rQ   Zbokeh.ior^   warningswarnFutureWarningpopZcurstateZnotebookr   r   r?   x_rangetitleZmin_border_topr   majorplot_heightheightxaxis
axis_labelZmin_border_bottomZmin_border_leftZmin_border_rightZgridplotshowZoutput_filesave)Z	profilersfilenamerr   rs   rf   rK   bpr^   Zfigsptopr'   r   rb   r   	visualize   sP    






rx   Viridisc              	      s  t dt}ddlm} tdddddd	}d
|kr>|d
|d< d|krT|d|d< |jf | | rt|  \}}	}
}}tt	d| }dd |
 D }dd tt|
 t	dddD t|
t|}|jf dd ttD d| gd|}i }dd t|
|D  |d< }fddt||
D |d< fdd|D |d<  fdd|	D  |d< }t|||d< d d |D |d!< |j|d"}|j|dddddd#d$ n(|jf d%d td&D dd'gd|}d(|j_d(|j_d(|j_d)|j_d*|j_||}d+|_d,|_|S )-ae  Visualize the results of profiling in a bokeh plot.

    Parameters
    ----------
    results : sequence
        Output of Profiler.results
    dsk : dict
        The dask graph being profiled.
    palette : string, optional
        Name of the bokeh palette to use, must be a member of
        bokeh.palettes.all_palettes.
    label_size: int (optional)
        Maximum size of output labels in plot, defaults to 60
    **kwargs
        Other keyword arguments, passed to bokeh.figure. These will override
        all defaults set by visualize.

    Returns
    -------
    The completed bokeh plot object.
    r]   r   	HoverToolProfile Resultsz!hover,save,reset,xwheel_zoom,xpanabove   ,  rl   ZtoolsZtoolbar_locationwidthro   
plot_widthr   rn   ro      c                 S   s    i | ]\}}|d d |D qS )c                 S   s   g | ]}|j |j qS r   )Zend_time
start_timer&   ir   r   r   rP     s     z)plot_tasks.<locals>.<dictcomp>.<listcomp>r   r7   r   r   r   
<dictcomp>  s     zplot_tasks.<locals>.<dictcomp>c                 S   s   i | ]\}}|d  |qS )r   r   )r&   rN   r+   r   r   r   r     s    r   T)keyreversec                 S   s   g | ]}t |qS r   strr   r   r   r   rP   #  s     zplot_tasks.<locals>.<listcomp>Zy_rangerk   c                 S   s   g | ]\}}|| qS r   r   )r&   ser   r   r   rP   )  s     c                    s    g | ]\}}|d  |   qS )r    r   )r&   wr   leftr   r   rP   *  s     xc                    s   g | ]} | d  qS )r   r   r   )id_lkr   r   rP   +  s     yc                    s   g | ]}t | qS r   r)   r   dskrF   r   r   rP   ,  s     functioncolorc                 S   s   g | ]}t |qS r   r   r   r   r   r   rP   .  s     r   )dataZgray)sourcer   r   ro   r   r   
line_colorc                 S   s   g | ]}t |qS r   r   r   r   r   r   rP   <  s        
   Nz	Worker IDTime (s)as  
    <div>
        <span style="font-size: 14px; font-weight: bold;">Key:</span>&nbsp;
        <span style="font-size: 10px; font-family: Monaco, monospace;">@key</span>
    </div>
    <div>
        <span style="font-size: 14px; font-weight: bold;">Task:</span>&nbsp;
        <span style="font-size: 10px; font-family: Monaco, monospace;">@function</span>
    </div>
    Zfollow_mouse) r   rQ   bokeh.modelsr{   r   rj   updaterW   r   r   rD   	enumeraterC   rS   maxfigureranger?   r[   ColumnDataSourceZrectZgridZgrid_line_colorZaxisZaxis_line_colorZmajor_tick_line_coloryaxisrq   rp   selecttooltipsZpoint_policy)resultsr   rX   rF   rK   ru   r{   defaultsr-   ZtasksstartsendsidsZid_groupZtimingsrightrv   r   r   r"   r   hoverr   )r   r   rF   r   r   
plot_tasks   sx    


(

r   c                    s  t dt}ddlm} ddlm}m} tddddd	d
}d|krd|d|d< t	 j
dkrdtd d|kr|d|d< t	 j
dkrtd d|kr|d |jf | | rt|  \}}	}
t|t|  } fdd|D }|jf tdt|
dtd|  dd|}n"g  } }	}
|jf ddd|}|j| d }|j||
|d ddd d|j_d|t|	rzt|	nd|	rt|	ndd i|_|j||	|d ddd d! ||dd"d#d$ d%|j_|S )&a  Plot resource usage in a bokeh plot.

    Parameters
    ----------
    results : sequence
        Output of ResourceProfiler.results
    palette : string, optional
        Name of the bokeh palette to use, must be a member of
        bokeh.palettes.all_palettes.
    **kwargs
        Other keyword arguments, passed to bokeh.figure. These will override
        all defaults set by plot_resources.

    Returns
    -------
    The completed bokeh plot object.
    r]   r   )rY   )
LinearAxisRange1dr|   zsave,reset,xwheel_zoom,xpanr}   r~   r   r   r   r   r!   /Use width instead of plot_width with Bokeh >= 3rn   ro   1Use height instead of plot_height with Bokeh >= 3rF   c                    s   g | ]}|  qS r   r   r   r   r   r   rP     s     z"plot_resources.<locals>.<listcomp>d   r   r   )r   r   )r   r      r   z% CPU)r   
line_widthlegend_labelZmemoryr    ZMemory)r   y_range_namer   r   zMemory (MB))r   rq   r   r   )r   rQ   r   rY   r   r   r   r   rj   r   rm   rg   rh   r   rW   rS   r   r   
fix_boundsrR   liner   rq   Zextra_y_rangesZ
add_layoutrp   )r   rX   rK   ru   rY   r   r   r   r+   Zmemcpur   rv   colorsr   r   r   plot_resourcesS  st    



 &r   c                 C   s   | t || | fS )z6Adjust end point to ensure span of at least `min_span`)r   )startendZmin_spanr   r   r   r     s    r   c                    s   t dt}ddlm} tdddddd	}	d
|krT|d
|d< t jdkrTt	d d|kr|d|d< t jdkrt	d |	j
f | | rtt|  dd \}
}tt|
| }t fdd| }i }| D ]l\}}t|d}|D ],}||j  |j7  < ||j  |j8  < qdgttttdt|  ||< qdgfdd|D  }|jf ddt|gi|	}t| t|| D ]>\\}}|jdd|d|||fdd|D dd qn|jf ddgddgd|	}d| d |j_d!|j _|!|}d"|_"|S )#a  Visualize the results of profiling in a bokeh plot.

    Parameters
    ----------
    results : sequence
        Output of CacheProfiler.results
    dsk : dict
        The dask graph being profiled.
    start_time : float
        Start time of the profile.
    metric_name : string
        Metric used to measure cache size
    palette : string, optional
        Name of the bokeh palette to use, must be a member of
        bokeh.palettes.all_palettes.
    label_size: int (optional)
        Maximum size of output labels in plot, defaults to 60
    **kwargs
        Other keyword arguments, passed to bokeh.figure. These will override
        all defaults set by visualize.

    Returns
    -------
    The completed bokeh plot object.
    r]   r   rz   r|   z hover,save,reset,wheel_zoom,xpanr}   r~   r   r   r   r   r!   r   rn   ro   r   Nc                    s   t | d  S )Nr   r)   )dr   r   r   r/     r0   zplot_cache.<locals>.<lambda>r   c                    s   g | ]}|  qS r   r   r   )r   r   r   rP     s     zplot_cache.<locals>.<listcomp>rk   r   r   c                    s   g | ]} qS r   r   r   )r   r   r   rP     s     )r   r   label)r   r   r   r   r   zCache Size (r   r   z
    <div>
        <span style="font-size: 14px; font-weight: bold;">Task:</span>&nbsp;
        <span style="font-size: 10px; font-family: Monaco, monospace;">@label</span>
    </div>
    )#r   rQ   r   r{   r   rj   r   rm   rg   rh   r   r   rW   rC   r	   r   rD   fromkeysZ
cache_timeZmetricZ	free_timer   r   r   r   r   r[   r-   r   r   r   rq   rp   r   r   )r   r   r   Zmetric_namerX   rF   rK   ru   r{   r   r   r   Zticsgroupsr   r8   valsZcntsr9   rv   valr   r   r   )r   r   rF   r   r   
plot_cache  s^    


($
r   )r   )r\   TNN)ry   r   )ry   )ry   r   )rT   rg   bisectr   	itertoolsr   operatorr   r   Ztlzr   r   r   r	   Z	dask.corer
   Z
dask.utilsr   r   r   r   rQ   r   r*   r[   rx   r   r   r   r   r   r   r   r   <module>   s.   
\       
N
i
T   