
    >ie?                        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	 Zd
Zd ZddZd Z	 ddZ	 ddZddZd Z	 	 ddZdS )    )annotationsN)bisect_left)cycle)add
itemgetter)
accumulategroupbypluckunique)istask)applyfuncnameimport_requiredc                 6    dd l } ddlm}  || j                  S )Nr   )parse)bokehpackaging.versionr   __version__)r   parse_versions     Blib/python3.11/site-packages/dask/diagnostics/profile_visualize.pyBOKEH_VERSIONr      s0    LLL888888=*+++    z1Diagnostics plots require `bokeh` to be installedc                   t          |           r| d         t          t          t          fv r* | d         t	          t
          | d                             S | d         t          k    rdt          | d         t                    rIt          | d         d         t                    r(t          t	          t
          | d                             S | S )Nr      )r   tuplelistsetmapunquotedict
isinstance)exprs    r   r   r      s    d|| /7udC(((473wQ00111GtOO47D)) 471:t,,  GT!W--...Kr   <   c                Z  	
 t          |           r| d         }|t          u rlt          | d                   }d}t          |           dk    rt	          | d                   nd}t          |           dk    rt	          | d                   ni }not          |d          r<d                    d	 |j        D                       }dt          |j                  z  }nt          | d                   }d}| dd
         }i }|s|rUt          |t          |          z
  t          |          z
  t          |          t          |          z   z            		fd
|r+	dk    r"d                    
fd|D                       }nd}nd}|rM	dk    rDdd                    
fdt          |
                                          D                       z   }nd}nd}| d| | | S t          | t                    r| sdS t          |           dk    r&t          | d
d         |          }|d
d         dz   S t          |dz
  dt          |           z  z
  t          |           z            	d                    	fd| D                       }d| dS 	 | v rdS dS # t          $ r Y dS w xY 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   )       funcs(c              3  4   K   | ]}t          |          V  d S N)r   .0fs     r   	<genexpr>zpprint_task.<locals>.<genexpr>Q   s(      @@@@@@@@r   Nc                &    t          |           S r,   pprint_task)tkeyslabel_size2s    r   <lambda>zpprint_task.<locals>.<lambda>\   s    {1dK@@ r      z, c              3  .   K   | ]} |          V  d S r,   r'   )r.   r4   pprints     r   r0   zpprint_task.<locals>.<genexpr>_   s+       9 9q 9 9 9 9 9 9r   z... c              3  >   K   | ]\  }}| d  |           V  dS )=Nr'   )r.   kvr:   s      r   r0   zpprint_task.<locals>.<genexpr>f   sN       * *+/1aq&&66!99&&* * * * * *r   z, ...z[]z, ...]c              3  :   K   | ]}t          |          V  d S r,   r2   )r.   r4   r5   r6   s     r   r0   zpprint_task.<locals>.<genexpr>v   s/      MM1[D+>>MMMMMMr   []_*)r   r   r   lenr   hasattrjoinr)   intsorteditemsr!   r   r3   	TypeError)taskr5   
label_sizefuncheadtailargskwargsresultr6   r:   s    `       @@r   r3   r3   &   s%   D d|| 7Aw5==DG$$DD'*4yy1}}747###"D),TQWT!W%%%BFFtW%% xx@@TZ@@@@@S__,Q((8DF 	A6 	Ac$ii'#d))3TS[[9PQ K A@@@@F 	Qyy 9 9 9 9D 9 9 999D 	Q		 * * * *39&,,..3I3I* * * ! !  !F---v-t---	D$		  	4YY]] bqb4<<F#2#;))zA~CII=#d))KLLK99MMMMMMMMMMDt;;;	t||ss 	 	 	33	s   J 
J*)J*c                   t          dt                    }t          t          |                    }t	          |          }|j        |          }t          t          |                                                    }|t          t          ||          t	          |          dz
                     }||         } t          t          |                     } t	          |           |k    r't          j        d                              |            t          t          |t          |                               f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                     g | ]
}|         S r'   r'   )r.   ncolor_lookups     r   
<listcomp>zget_colors.<locals>.<listcomp>   s    +++LO+++r   )r   _BOKEH_MISSING_MSGrJ   r   rF   all_palettesr   r5   minr   randomRandomshuffler    zipr   )	paletter)   palettesunique_funcsn_funcspalette_lookupr5   indexrY   s	           @r   
get_colorsrh      s    /1CDDH&--((L,G*73N~**,,--..D[w//TQ??@EU#G6'??##G
7||gb!!'***L%..99::L++++U++++r   profile.htmlTc                   t          dt                    }ddlm} dv r1t	          j        dt          d                               d          }||                                j	         }t          | t                    s| g} fd	| D             }t          |          d
k    r	|d         }	n|d         }
|d
d         D ]T}|
j        |_        d|_        d|_        t!                      j        dk     r|xj        dz  c_        D|xj        dz  c_        U|dd         D ]M}d|j        _        d|_        t!                      j        dk     r|xj        dz  c_        =|xj        dz  c_        N|D ]}d|_        d|_        |                    d |D                       }	|r|                    |	           |r,|                    ||           |                    |	           |	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                *    g | ]} |j         d i S )r'   )_plot)r.   profrS   s     r   rZ   zvisualize.<locals>.<listcomp>   s+    777TJDJ    777r   r      r(      r@   K   c                    g | ]}|gS r'   r'   r-   s     r   rZ   zvisualize.<locals>.<listcomp>   s    +++!+++r   )mode)r   r[   bokeh.iorl   warningswarnFutureWarningpopcurstatenotebookr!   r   rF   x_rangetitlemin_border_topr   majorplot_heightheightxaxis
axis_labelmin_border_bottommin_border_leftmin_border_rightgridplotshowoutput_filesave)	profilersfilenamer   r   rw   rS   bprl   figsptopr/   s        `      r   	visualizer      s(   6 
)+=	>	>Bf+ #	
 	
 	
 	
 ::k**|>>##,,i&&  K	7777Y777D
4yyA~~G1gabb 	 	AAIAG!A$q((#Bcrc 	 	A!%AG"$A$q((#B 	$ 	$A "A!#AKK++d+++,, 



 
xd+++



Hr   Viridisc           	        t          dt                    }ddlm} t	          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 t          t          |
                                t          d          d                    D              |j        d+d t          t                              D             d|z
  gd|	}i }d t          ||          D             x|d<   }fdt          ||          D             |d<   fd|D             |d<   fd|D             x|d<   }t!          ||          |d<   d |
D             |d<   |                    |           }|                    |dddddd!"           n) |j        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.
    rk   r   	HoverToolProfile Resultsz!hover,save,reset,xwheel_zoom,xpanabove   ,  r   toolstoolbar_locationwidthr   
plot_widthr   r   r      c                .    i | ]\  }}|d  |D             S )c                ,    g | ]}|j         |j        z
  S r'   )end_time
start_timer.   is     r   rZ   z)plot_tasks.<locals>.<dictcomp>.<listcomp>  s!    555a
Q\)555r   r'   )r.   r>   r?   s      r   
<dictcomp>zplot_tasks.<locals>.<dictcomp>  s:     
 
 
:@1aA551555
 
 
r   c                &    i | ]\  }}|d          |S )r   r'   )r.   rX   r4   s      r   r   zplot_tasks.<locals>.<dictcomp>   s2     
 
 
A aD!
 
 
r   r   T)keyreversec                ,    g | ]}t          |          S r'   strr   s     r   rZ   zplot_tasks.<locals>.<listcomp>(  s    777SVV777r   y_ranger   c                    g | ]
\  }}||z
  S r'   r'   )r.   ses      r   rZ   zplot_tasks.<locals>.<listcomp>.  s      G G G6AqQ G G Gr   c                ,    g | ]\  }}|d z  |z   z
  S )r&   r'   )r.   wr   r   s      r   rZ   zplot_tasks.<locals>.<listcomp>/  s*    MMMAQUQY+MMMr   xc                &    g | ]}|         d z   S )r   r'   )r.   r   id_lks     r   rZ   zplot_tasks.<locals>.<listcomp>0  s!    ///aU1X\///r   yc                2    g | ]}t          |          S r'   r2   )r.   r   dskrN   s     r   rZ   zplot_tasks.<locals>.<listcomp>1  s%    #S#S#SK3
$C$C#S#S#Sr   functioncolorc                ,    g | ]}t          |          S r'   r   r   s     r   rZ   zplot_tasks.<locals>.<listcomp>3  s    ,,,!s1vv,,,r   r   )datagray)sourcer   r   r   r   r   
line_colorc                ,    g | ]}t          |          S r'   r   r   s     r   rZ   zplot_tasks.<locals>.<listcomp>A  s    888!s1vv888r      
   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>
    follow_mouser'   )r   r[   bokeh.modelsr   r    r|   updatera   r	   r   rK   	enumeraterJ   figurerangerF   rh   ColumnDataSourcerectgridgrid_line_coloraxisaxis_line_colormajor_tick_line_coloryaxisr   r   selecttooltipspoint_policy)resultsr   r   r   rb   rN   rS   r   r   defaultsr5   tasksstartsendsidsid_grouptimingsr   r   r   r)   r   hoverr   s    ``  `                 @r   
plot_tasksr      s@   8 
)+=	>	>B&&&&&&1   H v **\22w!::m44xHOf (W),g&eVT3:a=='22
 
DLNNDTDT
 
 

 
#w}}JqMM4HHH 
 
 
 BI 
77U3u::%6%6777:-.
 
 
 
  G GS5F5F G G GGWMMMM#eV:L:LMMMS	////3///S	#S#S#S#S#SU#S#S#SSZ5"7E22W,,t,,,U$$$$//	 	 	
 	
 	
 	
 BIV88uQxx8881b'VVXVV!AF!AF#'AF $AG#AGHHYE	EN (EHr   c           	     ^   t          dt                    }ddlm} ddlm}m} t          ddddd	
          }	d|v rC|                    d          |d<   t                      j
        dk    rt          j        d           d|v rC|                    d          |d<   t                      j
        dk    rt          j        d           d|v r|                    d            |	j        d%i | | r^t          |  \  }
}}||}fd|
D             }
 |j        d%t!          dt#          |          d          t!          d|z
  d          d|	}ng x}
x}} |j        d%ddd|	}|j        |         d         }|                    |
||d         dd           d|j        _        d |t!          |rt-          |          nd|rt#          |          ndd           i|_        |                    |
||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.
    rk   r   )rc   )
LinearAxisRange1dr   zsave,reset,xwheel_zoom,xpanr   r   r   r   r   r   r(   /Use width instead of plot_width with Bokeh >= 3r   r   1Use height instead of plot_height with Bokeh >= 3rN   c                    g | ]}|z
  S r'   r'   )r.   r   lefts     r   rZ   z"plot_resources.<locals>.<listcomp>  s    !!!!QX!!!r   d   r   r   )r   r   )r   r      r   z% CPU)r   
line_widthlegend_labelmemoryr&   Memory)r   y_range_namer   r   zMemory (MB))r   r   rightr   r'   )r   r[   r   rc   r   r   r   r    r|   r   r   ry   rz   r   ra   r   
fix_boundsmaxr\   liner   r   r]   extra_y_ranges
add_layoutr   )r   r   r   rb   rS   r   rc   r   r   r   r4   memcpur   r   colorsr   s                   @r   plot_resourcesr   X  s   , 
)+=	>	>B00000000+   H v **\22w?? A%%MKLLL!::m44x?? A%%MMNNN v

<   HOf D7m3!!!!q!!!BI 
q#c((C00q%$,22
 
 
 
 C#BIChCC(CC"7+A.FFF	Qi     !AG''C.CQC0HCS#NN
A
 FF	Qi     LLmLLLgVVV#AGHr   c                ,    | t          || |z             fS )z6Adjust end point to ensure span of at least `min_span`)r   )startendmin_spans      r   r   r     s    #c58+,,,,r   c                   t          dt                    }ddlm}	 t	          ddddd	          }
d
|v rC|                    d
          |d<   t                      j        dk    rt          j	        d           d|v rC|                    d          |d<   t                      j        dk    rt          j	        d            |
j
        d!i | | rt          t          |            dd         \  }}t          t          ||z                       }t          fd|           }i }|                                D ]\  }}t                              |d          }|D ]6}||j        xx         |j        z  cc<   ||j        xx         |j        z  cc<   7dgt          t+          t,          t/          dt          |                                                                        z   ||<   dgfd|D             z   } |j        d!dd|z
  gi|
}t          |                                t3          ||                                                    D ]E\  \  }}|                    dd|d|                    ||fd|D             d                     Fn |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.
    rk   r   r   r   z hover,save,reset,wheel_zoom,xpanr   r   r   r   r   r   r(   r   r   r   r   Nc                2    t          | d                   S )Nr   r2   )dr   rN   s    r   r7   zplot_cache.<locals>.<lambda>  s    ;qtS*#E#E r   r   c                    g | ]}|z
  S r'   r'   )r.   r   r   s     r   rZ   zplot_cache.<locals>.<listcomp>  s    333a*n333r   r   r   r   c                    g | ]}S r'   r'   )r.   r   r   s     r   rZ   zplot_cache.<locals>.<listcomp>  s    3E3E3EAC3E3E3Er   )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   r[   r   r   r    r|   r   r   ry   rz   r   r   ra   rJ   r   r	   rK   fromkeys
cache_timemetric	free_timer   r   r
   r   rh   r5   r   r   r   r   r   r   r   )r   r   r   r   metric_namerb   rN   rS   r   r   r   r   r   ticsgroupsr   r>   valscntsr?   r   valr   r   r   s    ``   `                 @r   
plot_cacher    sb   J 
)+=	>	>B&&&&&&0   H v **\22w?? A%%MKLLL!::m44x?? A%%MMNNNHOf DCM**122.fVd]++,,EEEEEwOO||~~ 	R 	RGAt==q))D . .Q\"""ah."""Q[!!!QX-!!!!cDCq&:N:N1O1O!P!PQQQDGGs3333d3333BIEEq(Z"78EHEE!$TZZ\\:gtyy{{3S3S!T!T 		 		JS#FF **S3E3E3E3E3E3E3EFF      		 BICq"g2wCC(CC6666AG#AGHHYEEN Hr   )r#   )ri   TNN)r   r#   )r   ) 
__future__r   r^   ry   bisectr   	itertoolsr   operatorr   r   tlzr   r	   r
   r   	dask.corer   
dask.utilsr   r   r   r   r[   r   r3   rh   r   r   r   r   r  r'   r   r   <module>r     s   " " " " " "               $ $ $ $ $ $ $ $ 2 2 2 2 2 2 2 2 2 2 2 2       7 7 7 7 7 7 7 7 7 7, , , I 
 
 
Y Y Y Yx, , ,: DHK K K K^ GIi i i iXV V V Vr- - - ` ` ` ` ` `r   