
    bi<4                        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 d d	lmZ d
 ZdZd Zd Z	 ddZ	 ddZddZd Z	 	 ddZy)    )annotationsN)bisect_left)cycle)add
itemgetter)
accumulategroupbypluckunique)import_optional_dependency)istask)import_requiredc                 H    t        d      } ddlm}  || j                        S )Nbokehr   )Version)r   packaging.versionr   __version__)r   r   s     ]/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/dask/diagnostics/profile_visualize.pyBOKEH_VERSIONr      s     &w/E)5$$%%    z1Diagnostics plots require `bokeh` to be installedc                "   t        |       r| d   t        t        t        fv r | d   t	        t
        | d               S | d   t        k(  rEt        | d   t              r2t        | 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11GtO47D)471:t,GT!W-..Kr   c                   t        dt              }t        t        |            }t	        |      }|j
                  |    }t        |j                               }|t        t        ||      t	        |      dz
           }||   } t        t        |             } t	        |       |kD  r$t        j                  d      j                  |        t        t        |t        |                   }|D 	cg c]  }	||	   	 c}	S c c}	w )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   *   )r   _BOKEH_MISSING_MSGsortedr   lenall_paletteskeysminr   r   randomRandomshuffler   zipr   )
palettefuncspalettesunique_funcsn_funcspalette_lookupr'   indexcolor_lookupns
             r   
get_colorsr6   '   s     /1CDH&-(L,G**73N.%%'(D[w/TQ?@EU#G6'?#G
7|gb!!'*L%.9:L%*+LO+++s   *C9c                >   t        dt              }ddlm} d|v r-t	        j
                  dt        d       |j                  d      }||j                         j                   }t        | t              s| g} | D cg c]  } |j                  di | }	}t        |	      d	k(  r|	d   }
n|	d   }|	d	d D ]6  }|j                  |_        d|_        d
|_        |xj"                  dz  c_        8 |	dd D ]/  }d|j$                  _        d
|_        |xj"                  dz  c_        1 |	D ]  }d|_        d|_         |j/                  |	D cg c]  }|g c}      }
|r|j1                  |
       |r$|j3                  ||       |j5                  |
       |
S c c}w c c}w )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.   )category
stacklevelNr         K   )mode )r   r#   bokeh.ior9   warningswarnFutureWarningpopcurstatenotebookr   r   _plotr%   x_rangetitlemin_border_topheightxaxis
axis_labelmin_border_bottommin_border_leftmin_border_rightgridplotshowoutput_filesave)	profilersfilenamerV   rX   rB   kwargsbpr9   proffigsptopfs                r   	visualizerb   C   s   6 
)+=	>Bf+ #	
 ::k*|>>#,,,i&K	-67TJDJJ  7D7
4yA~G1gab 	AAIAG!AHHNH		
 cr 	A!%AGG"$AHHNH	  	$A "A!#A	$ KKd+!+,


xd+

H1 8$ ,s   <F	
Fc                J   t        dt              }ddlm} t	        ddddd	      }	d
|v r|j                  d
      |d<   d|v r|j                  d      |d<    |	j                  d#i | | rt        |  \  }
}}}}t        t        d      |       }|j                         D ci c]-  \  }}||D cg c]  }|j                  |j                  z
   c}/ }}}}t        t        |j                         t        d      d            D ci c]  \  }}|d   | }}} |j                  d#t!        t#        |            D cg c]  }t%        |       c}d||z
  gd|	}i }t        ||      D cg c]
  \  }}||z
   c}}x|d<   }t        ||      D cg c]  \  }}|dz  |z   |z
   c}}|d<   |D cg c]
  }||   dz    c}|d<   |D cg c]  }t'        |       c}x|d<   }t)        ||      |d<   |
D cg c]  }t%        |       c}|d<   |j+                  |      }|j-                  |dddddd       n7 |j                  d#t!        d      D cg c]  }t%        |       c}ddgd|	}d|j.                  _        d|j2                  _        d|j2                  _        d|j8                  _        d |j<                  _        |j?                  |      }d!|_         d"|_!        |S c c}w c c}}}w c c}}w c c}w c c}}w c c}}w c c}w c c}w c c}w c c}w )$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.
    r8   r   	HoverToolProfile Resultsz!hover,save,reset,xwheel_zoom,xpanabove   ,  rM   toolstoolbar_locationwidthrO   
plot_widthrm   plot_heightrO      r   T)keyreversey_rangerL   r;   xyfunctioncolorrq   )datagray)sourceru   rv   rO   rm   rx   
line_color   
   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_mouserC   )"r   r#   bokeh.modelsre   r   rH   updater,   r	   r   itemsend_time
start_time	enumerater$   figureranger%   strreprr6   ColumnDataSourcerectgridgrid_line_coloraxisaxis_line_colormajor_tick_line_coloryaxisrQ   rP   selecttooltipspoint_policy) resultsdskr   r   r-   
label_sizer[   r\   re   defaultsr'   tasksstartsendsidsid_groupkvitimingsr5   tid_lkr_   ry   serm   wr.   r{   hovers                                    r   
plot_tasksr      s*   8 
)+=	>B&1 H v **\2w!::m4xHOOf),g&eVT3:a='2DLNNDT
 
:@1aA15a

Q\\)55
 

 $w}}JqM4H
A aD!G
 
 BII 
%*3u:%67SV7:-.
 
 585F G6AqQ GGW:=eV:LMAQUQY+MS	+./aU1X\/S	5:#;DG#;;Z5"7E2W'+,!s1v,U$$$$/	 	 	
 BIIVuQx8!s1v81b'VXV!AFF!AFF#'AFF $AGG#AGGHHYE	EN (EHo 6

 8 !HM/#;, 9sH   #K30 K.K3
K: L 
1LL8LL>LL 
.K3c           	        t        dt              }ddlm} ddlm}m} t        ddddd	
      }	d|v r@|j                  d      |d<   t               j                  dk\  rt        j                  d       d|v r@|j                  d      |d<   t               j                  dk\  rt        j                  d       d|v r|j                  d        |	j                  d$i | | rZt        |  \  }
}}|}|}|
D cg c]  }||z
  	 }
} |j                  d$t!        dt#        |      d      t!        d||z
  d      d|	}ng x}
x}} |j                  d$ddd|	}|j$                  |   d   }|j'                  |
||d   dd       d|j(                  _        d |t!        |rt-        |      nd|rt#        |      ndd       i|_        |j'                  |
||d   ddd       |j1                   |dd !      d"       d#|j2                  _        |S c c}w )%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.
    r8   r   )r/   )
LinearAxisRange1drf   zsave,reset,xwheel_zoom,xpanrg   rh   ri   rj   rn   rm      /Use width instead of plot_width with Bokeh >= 3ro   rO   1Use height instead of plot_height with Bokeh >= 3r   d   r   rs   )r   r   )r   r      rp   z% CPU)rx   
line_widthlegend_labelmemoryr;   Memory)rx   y_range_namer   r   zMemory (MB))r   rQ   rightr   rC   )r   r#   r   r/   r   r   r   r   rH   r   majorrE   rF   r   r,   r   
fix_boundsmaxr&   liner   rQ   r(   extra_y_ranges
add_layoutrP   )r   r   r   r-   r[   r\   r/   r   r   r   r   memcpuleftr   r   r_   colorss                     r   plot_resourcesr      s4   , 
)+=	>B0+ H v **\2w?  A%MMKL!::m4x?  A%MMMN v

< HOOf7m3 !!QX!!BII 
q#c(C0q%$,2
 
 C#BIIChC(C""7+A.FFF	Qi   !AGG'CCQCCS#N
A
 FF	Qi   LLmLgV#AGGHC "s   4H	c                $    | t        || |z         fS )z6Adjust end point to ensure span of at least `min_span`)r   )startendmin_spans      r   r   r   P  s    #c58+,,,r   c                   t        dt              }ddlm}	 t	        ddddd	      }
d
|v r@|j                  d
      |d<   t               j                  dk\  rt        j                  d       d|v r@|j                  d      |d<   t               j                  dk\  rt        j                  d        |
j                  di | | rt        t        |        dd \  }}t        t        ||z               }t        d |       }i }|j!                         D ]  \  }}t        j#                  |d      }|D ]D  }||j$                  xx   |j&                  z  cc<   ||j(                  xx   |j&                  z  cc<   F dgt        t+        t,        t/        dt        |j!                                                 z   ||<    dg|D cg c]  }||z
  	 c}z   } |j0                  ddd||z
  gi|
}t        |j!                         t3        ||j5                                     D ]?  \  \  }}}|j7                  dd|d|j9                  |||D cg c]  }| c}d             A n |j0                  dddgddgd|
}d| d|j:                  _        d|j>                  _        |jA                  |	      }d|_!        |S c c}w c c}w ) 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.
    r8   r   rd   rf   z hover,save,reset,wheel_zoom,xpanrg   rh   ri   rj   rn   rm   r   r   ro   rO   r   Nc                    t        |       S )N)r   )ds    r   <lambda>zplot_cache.<locals>.<lambda>  s
    47 r   r   rL   ru   rv   )ru   rv   label)r|   r   r{   r~   rs   zCache Size ()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>
    rC   )"r   r#   r   re   r   rH   r   r   rE   rF   r   r   r,   r$   r   r	   r   fromkeys
cache_timemetric	free_timer   r   r
   r   r6   r'   r   r   r   rQ   rP   r   r   )r   r   r   r   metric_namer-   r   r[   r\   re   r   r   r   ticsgroupsry   r   valscntsr   r   r_   rq   valrx   r   s                             r   
plot_cacher   U  s   J 
)+=	>B&0 H v **\2w?  A%MMKL!::m4x?  A%MMMNHOOfCM*12.fVd]+,*G4||~ 	RGAt==q)D .Q\\"ahh."Q[[!QXX-!. cDCq&:N1O!PQQDG	R sd3a*n33BIIEq(Z"78EHE!$TZZ\:gtyy{3S!T 		JS#FF **S3EAC3EF  		 BIICq"g2wC(C'}A6AGG#AGGHHYEEN H5 4 4Fs   ?J7	J<)zprofile.htmlTNN)Viridis<   )r   )
__future__r   r)   rE   bisectr   	itertoolsr   operatorr   r   tlzr   r	   r
   r   dask._compatibilityr   	dask.corer   
dask.utilsr   r   r#   r   r6   rb   r   r   r   r   rC   r   r   <module>r      st    "     $ 2 2 :  && I 
,: DHER GIiXVr- `r   