o
    Nrf?                     @  s   d dl mZ 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 )$    )annotationsN)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   )Version)bokehZpackaging.versionr   __version__)r   r    r   k/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/dask/diagnostics/profile_visualize.pyBOKEH_VERSION   s   
r   z1Diagnostics plots require `bokeh` to be installedc                 C  sr   t | r7| d tttfv r| d tt| d S | d tkr7t| d tr7t| d d tr7ttt| d S | S )Nr      )r   tuplelistsetmapunquotedict
isinstance)exprr   r   r   r      s   r   <   c           	        s(  t | r| d }|tu r1t| d }d}t| dkr t| d nd}t| dkr.t| d ni }n(t|drIdd	d
 |jD }dt|j }nt| d }d}| dd }i }|s]|rxt|t| t| t|t|   fdd|rdkrdfdd
|D }nd}nd}|rdkrd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 z|  v rW dS W dS  ty   Y dS w )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>Q       zpprint_task.<locals>.<genexpr>Nc                   s   t |  S r$   pprint_task)tkeyslabel_size2r   r   <lambda>\   s    zpprint_task.<locals>.<lambda>   z, c                 3  s    | ]} |V  qd S r$   r   r&   r,   pprintr   r   r(   _   r)   z... c                 3  s&    | ]\}}| d  | V  qdS )=Nr   r&   kvr3   r   r   r(   f   s    
z, ...z[]z, ...]c                 3  s    | ]	}t | V  qd S r$   r*   r2   r-   r   r   r(   v   s    []_*)r   r   r   lenr   hasattrjoinr"   intsorteditemsr   r   r+   	TypeError)	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rBt
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>       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   rP   r   
get_colors   s   

r]   profile.htmlTc                   s~  t dt}ddlm} d v rtjdtdd  d}|du r&| j	 }t
| ts.| g}  fd	d
| D }t|dkrB|d }	nf|d }
|dd D ]!}|
j|_d|_d|_t jdk rf| jd8  _qL| jd8  _qL|dd D ]}d|j_d|_t jdk r| jd8  _qt| jd8  _qt|D ]}d|_d|_q|dd
 |D }	|r||	 |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 d i  qS )r   )Z_plot)r&   ZprofrL   r   r   rQ      s    zvisualize.<locals>.<listcomp>r      r!      r:   K   c                 S  s   g | ]}|gqS r   r   r%   r   r   r   rQ      s    )mode)r   rS   Zbokeh.ior`   warningswarnFutureWarningpopZcurstate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filenameru   rv   rh   rL   bpr`   Zfigsptopr'   r   rd   r   	visualize   sP   





r{   Viridisc              	     s  t dt}ddlm} tdddddd	}	d
|v r|d
|d< d|v r*|d|d< |	jd-i | | rt|  \}
}}}}tt	d| }dd |
 D }dd tt|
 t	dddD |jd-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d-d%d td&D dd'gd|	}d(|j_d(|j_d(|j_d)|j_d*|j_||}d+|_d,|_|S ).a  Visualize the results of profiling in a bokeh plot.

    Parameters
    ----------
    results : sequence
        Output of Profiler.results
    dsk : dict
        The dask graph being profiled.
    start_time : float
        Start time of the profile in seconds
    end_time : float
        End time of the profile in seconds
    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   ,  ro   ZtoolsZtoolbar_locationwidthrr   
plot_widthr   rq   rr      c                 S  s    i | ]\}}|d d |D qS )c                 S  s   g | ]}|j |j qS r   )end_time
start_timer&   ir   r   r   rQ         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&   rO   r,   r   r   r   r      s    r   T)keyreversec                 S     g | ]}t |qS r   strr   r   r   r   rQ   (  rR   zplot_tasks.<locals>.<listcomp>Zy_rangern   c                 S  s   g | ]\}}|| qS r   r   )r&   ser   r   r   rQ   .  r   c                   s    g | ]\}}|d  |   qS )r    r   )r&   wr   r   r   r   rQ   /  s     xc                   s   g | ]} | d  qS )r   r   r   )id_lkr   r   rQ   0  r   yc                   s   g | ]}t | qS r   r*   r   dskrG   r   r   rQ   1  r   functioncolorc                 S  r   r   r   r   r   r   r   rQ   3  rR   r   )datagray)sourcer   r   rr   r   r   
line_colorc                 S  r   r   r   r   r   r   r   rQ   A  rR      
   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_mouser   )r   rS   bokeh.modelsr~   r   rl   updaterY   r   r   rD   	enumeraterC   figureranger?   r]   ColumnDataSourcerectgridZgrid_line_colorZaxisZaxis_line_colorZmajor_tick_line_coloryaxisrt   rs   selecttooltipsZpoint_policy)resultsr   r   r   rZ   rG   rL   rx   r~   defaultsr.   tasksstartsendsZidsZid_groupZtimingsry   r   r   r"   r   hoverr   )r   r   rG   r   r   
plot_tasks   st   

(


r   c                   s  t dt}ddlm} ddlm}m} tddddd	d
}	d|v r2|d|d< t	 j
dkr2td d|v rH|d|d< t	 j
dkrHtd d|v rQ|d |	jd'i | | rt|  \}
}}| |} fdd|
D }
|jd'tdt|dtd|  dd|	}ng  }
 }}|jd'ddd|	}|j| d }|j|
||d ddd d|j_d|t|rt|nd|rt|ndd i|_|j|
||d ddd d! ||dd"d#d$ d%|j_|S )(aT  Plot resource usage in a bokeh plot.

    Parameters
    ----------
    results : sequence
        Output of ResourceProfiler.results
    start_time : float
        Start time of the profile in seconds
    end_time : float
        End time of the profile in seconds
    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   )r[   )
LinearAxisRange1dr   zsave,reset,xwheel_zoom,xpanr   r   r   r   r   r   r!   /Use width instead of plot_width with Bokeh >= 3rq   rr   1Use height instead of plot_height with Bokeh >= 3rG   c                      g | ]}|  qS r   r   r   leftr   r   rQ     rR   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   rt   rightr   Nr   )r   rS   r   r[   r   r   r   r   rl   r   rp   ri   rj   r   rY   r   
fix_boundsmaxrT   liner   rt   rU   Zextra_y_rangesZ
add_layoutrs   )r   r   r   rZ   rL   rx   r[   r   r   r   r,   Zmemcpur   ry   colorsr   r   r   plot_resourcesX  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
|v r*|d
|d< t jdkr*t	d d|v r@|d|d< t jdkr@t	d |
j
d#i | | rtt|  dd \}}tt|| }t fdd| }i }| D ]6\}}t|d}|D ]}||j  |j7  < ||j  |j8  < qzdgttttdt|  ||< qndgfdd|D  }|jd#dd| gi|
}t| t|| D ]\\}}|jdd|d|||fdd|D dd qn|jd#ddgddgd|
}d| d |j_d!|j_| |	}d"|_!|S )$a1  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 in seconds
    end_time : float
        End time of the profile in seconds
    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   r}   r   z hover,save,reset,wheel_zoom,xpanr   r   r   r   r   r   r!   r   rq   rr   r   Nc                   s   t | d  S )Nr   r*   )dr   r   r   r0         zplot_cache.<locals>.<lambda>r   c                   r   r   r   r   r   r   r   rQ     rR   zplot_cache.<locals>.<listcomp>rn   r   r   c                   s   g | ]} qS r   r   r   )r   r   r   rQ     r   )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   )"r   rS   r   r~   r   rl   r   rp   ri   rj   r   r   rY   rC   r
   r   rD   fromkeysZ
cache_timeZmetricZ	free_timer   r   r	   r   r]   r.   r   r   r   rt   rs   r   r   )r   r   r   r   Zmetric_namerZ   rG   rL   rx   r~   r   r   r   Zticsgroupsr   r8   valsZcntsr9   ry   valr   r   r   )r   r   rG   r   r   
plot_cache  s`   
%

($
r   )r   )r^   TNN)r|   r   )r|   ) 
__future__r   rV   ri   bisectr   	itertoolsr   operatorr   r   Ztlzr   r   r	   r
   Z	dask.corer   Z
dask.utilsr   r   r   r   rS   r   r+   r]   r{   r   r   r   r   r   r   r   r   <module>   s.    
\
O

lY