
    tf?                        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y)    )annotationsN)bisect_left)cycle)add
itemgetter)
accumulategroupbypluckunique)istask)applyfuncnameimport_requiredc                 :    dd l } ddlm}  || j                        S )Nr   )Version)bokehpackaging.versionr   __version__)r   r   s     l/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/dask/diagnostics/profile_visualize.pyBOKEH_VERSIONr      s    )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                2  	
 t        |       rp| d   }|t        u rMt        | d         }d}t        |       dkD  rt	        | d         nd}t        |       dkD  rt	        | d         ni }n^t        |d      r;dj                  d	 |j                  D              }dt        |j                        z  }nt        | d         }d}| dd
 }i }|s|rAt        |t        |      z
  t        |      z
  t        |      t        |      z   z        		fd
|r#	dkD  rdj                  
fd|D              }nd}nd}|r=	dkD  r5ddj                  
fdt        |j                               D              z   }nd}nd}| d| | | S t        | t              rs| syt        |       dkD  rt        | d
d |      }|d
d dz   S t        |dz
  dt        |       z  z
  t        |       z        	dj                  	fd| D              }d| dS 	 | v ryy# t        $ r Y y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  2   K   | ]  }t        |        y wN)r   ).0fs     r   	<genexpr>zpprint_task.<locals>.<genexpr>Q   s     @@s   Nc                    t        |       S r*   pprint_task)tkeyslabel_size2s    r   <lambda>zpprint_task.<locals>.<lambda>\   s    {1dK@ r      z, c              3  .   K   | ]  } |        y wr*   r%   )r+   r1   pprints     r   r-   zpprint_task.<locals>.<genexpr>_   s      9q 9s   z... c              3  >   K   | ]  \  }}| d  |         yw)=Nr%   )r+   kvr7   s      r   r-   zpprint_task.<locals>.<genexpr>f   s)      *+/1aqc6!9+&*s   z, ...z[]z, ...]c              3  8   K   | ]  }t        |        y wr*   r/   )r+   r1   r2   r3   s     r   r-   zpprint_task.<locals>.<genexpr>v   s     M1[D+>Ms   []_*)r   r   r   lenr   hasattrjoinr'   intsorteditemsr    r   r0   	TypeError)taskr2   
label_sizefuncheadtailargskwargsresultr3   r7   s    `       @@r   r0   r0   &   s'   D d|Aw5=DG$DD'*4y1}747#"D),TQWT!W%BFtW%xx@TZZ@@S_,Q(8DF6c$i'#d)3TS[9PQK AFQyy 9D 99DQ		 *39&,,.3I* !  !Fqvhtf--	D$	Y] bq4<F#2;))zA~CI=#d)KLK99MMMDtfA;	t| 		s   H
 
	HHc                   t        dt              }t        t        |            }t	        |      }|j
                  |    }t        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_MSGrG   r   rC   all_palettesr   r2   minr   randomRandomshuffler   zipr   )
paletter'   palettesunique_funcsn_funcspalette_lookupr2   indexcolor_lookupns
             r   
get_colorsrc      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   3D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 ]c  }|j                  |_        d|_        d
|_        t#               j$                  dk  r|xj&                  dz  c_        O|xj(                  dz  c_        e |	dd D ]\  }d|j*                  _        d
|_        t#               j$                  dk  r|xj&                  dz  c_        H|xj(                  dz  c_        ^ |	D ]  }d|_        d|_         |j5                  |	D cg c]  }|g c}      }
|r|j7                  |
       |r$|j9                  ||       |j;                  |
       |
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.r$   )category
stacklevelNr      r&      r=   K   )moder%   )r   rT   bokeh.iorf   warningswarnFutureWarningpopcurstatenotebookr    r   _plotrC   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   rm   rP   bprf   proffigsptopr,   s                r   	visualizer      s   6 
)+=	>Bf+ #	
 ::k*|>>#,,,i&K	-67TJDJJ  7D7
4yA~G1gab 	AAIAG!A$$q(#B	 cr 	A!%AGG"$A$$q(#B	  	$A "A!#A	$ KKd+!+,


xd+

H= 80 ,s   <G0$
G5c                N   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.
    re   r   	HoverToolProfile Resultsz!hover,save,reset,xwheel_zoom,xpanabove   ,  rw   toolstoolbar_locationwidthr{   
plot_widthr   rz   r{      r   T)keyreversey_rangerv   r$   xyfunctioncolorr   )datagray)sourcer   r   r{   r   r   
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_mouser%   )"r   rT   bokeh.modelsr   r   rr   updaterZ   r	   r   rH   end_time
start_time	enumeraterG   figurerangerC   strr0   rc   ColumnDataSourcerectgridgrid_line_coloraxisaxis_line_colormajor_tick_line_coloryaxisr}   r|   selecttooltipspoint_policy) resultsdskr   r   r[   rK   rP   r   r   defaultsr2   tasksstartsendsidsid_groupr;   r<   itimingsrb   r1   id_lkr   r   ser   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	MR#SK3
$C#SS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/#S, 9sH   #K50 K0K5
K< L
1LL8LL LL"
0K5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.
    re   r   )r\   )
LinearAxisRange1dr   zsave,reset,xwheel_zoom,xpanr   r   r   r   r   r   r&   /Use width instead of plot_width with Bokeh >= 3rz   r{   1Use height instead of plot_height with Bokeh >= 3rK   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   rT   r   r\   r   r   r   r   rr   r   ry   ro   rp   r   rZ   r   
fix_boundsmaxrU   liner   r}   rV   extra_y_ranges
add_layoutr|   )r   r   r   r[   rP   r   r\   r   r   r   r1   memcpuleftr   r   r   colorss                     r   plot_resourcesr   X  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     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        f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.
    re   r   r   r   z hover,save,reset,wheel_zoom,xpanr   r   r   r   r   r   r&   r   rz   r{   r   Nc                $    t        | d         S )Nr   r/   )dr   rK   s    r   r4   zplot_cache.<locals>.<lambda>  s    ;qtS*#E r   r   rv   r   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   rT   r   r   r   rr   r   ry   ro   rp   r   r   rZ   rG   r   r	   rH   fromkeys
cache_timemetric	free_timer   r   r
   r   rc   r2   r   r   r   r}   r|   r   r   )r   r   r   r   metric_namer[   rK   rP   r   r   r   r   r   ticsgroupsr   r;   valscntsr<   r   r   r   valr   r   s    `    `                   r   
plot_cacher     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]+,EwO||~ 	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   J<		K)<   )zprofile.htmlTNN)Viridisr   )r   ) 
__future__r   rW   ro   bisectr   	itertoolsr   operatorr   r   tlzr   r	   r
   r   	dask.corer   
dask.utilsr   r   r   r   rT   r   r0   rc   r   r   r   r   r   r%   r   r   <module>r      s{    "     $ 2 2  7 7& I 
Yx,: DHK^ GIiXVr- `r   