a
    ~bIY                     @   s>  d dl mZmZmZ d dlmZ d dlZd dlZd dl	m
Z
mZmZmZmZmZmZ ddlmZ ddlmZmZ ddlmZmZmZ dd	lm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% ddlm&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z,m-Z- G dd de&e$e'Z.G dd dee.Z/G dd de%Z0G dd de.Z1dS )    )absolute_importdivisionunicode_literals)defaultdictN)StaticLayoutProviderNodesAndLinkedEdgesEdgesAndLinkedNodesPatchesBezierColumnDataSource	NodesOnly   )Dataset)Cycleabbreviated_exception)
basestringdimension_sanitizerunique_array)Graph)dim   )
ChordMixin)process_cmapget_directed_graph_paths   )ColorbarPlot	PointPlot)CompositeElementPlot
LegendPlot)base_propertiesline_propertiesfill_propertiestext_properties
rgba_tuplec                       sb  e Zd ZejdddZejdddZejdg ddd	Z	ejdg dd
d	Z
ejddgddZejdeefdddZejdeefdddZdddddZdd ee e D dd ee e D  g d Zeg d ZdZdZddgZedd Zdd  Zd8 fd"d#	Zd$d% Zd&d' Z d(d) Z!d*d+ Z"d,d- Z#d.d/ Z$d0d1 Z%d2d3 Z&d4d5 Z'd6d7 Z(  Z)S )9	GraphPlotg?z
      If directed option is enabled this determines the length of the
      arrows as fraction of the overall extent of the graph.defaultdocFzc
      Whether to draw arrows on the graph edges to indicate the
      directionality of each edge.nodes)edgesr(   Nz
        Determines policy for inspection of graph components, i.e. whether to highlight
        nodes or edges when selecting connected edges and nodes respectively.)r&   Zobjectsr'   z
        Determines policy for inspection of graph components, i.e. whether to highlight
        nodes or edges when hovering over connected edges and nodes respectively.hoverZtapz3
        A list of plugin tools to use on the plot.NTzS
        Deprecated in favor of color style mapping, e.g. `node_color=dim('color')`r&   class_Z
allow_Noner'   zS
        Deprecated in favor of color style mapping, e.g. `edge_color=dim('color')`nodeedge)scatter
multi_lineZpatchesbezierc                 C   s   g | ]}d | qS edge_ .0pr4   r4   >lib/python3.9/site-packages/holoviews/plotting/bokeh/graphs.py
<listcomp>>       zGraphPlot.<listcomp>c                 C   s   g | ]}d | qS Znode_r4   r5   r4   r4   r8   r9   ?   r:   )	node_sizecmap	edge_cmap	node_cmapZnode_radiusZnode_marker)r=   r>   r?   r   r   c                 C   s   | j r
dS | jrdS dS d S )NZ	patches_1Zbezier_1Zmulti_line_1)filledr1   selfr4   r4   r8   
edge_glyphN   s
    zGraphPlot.edge_glyphc                 C   sd   | j dkr2|j }|d jdfg|dd   }n*| j dkrXdd |jD }||j }ng }|i fS )Nr(   r   z@{index_hover}r   r)   c                 S   s&   g | ]}|d v r|j d| fn|qS )startendz@{%s_values})pprint_label)r6   Zkdr4   r4   r8   r9   \   s   z)GraphPlot._hover_opts.<locals>.<listcomp>)inspection_policyr(   
dimensionsrG   kdimsvdims)rB   elementZdimsrJ   r4   r4   r8   _hover_optsW   s    


zGraphPlot._hover_optscombinedc                    s   t t| |j||S N)superr$   get_extentsr(   )rB   rL   rangesZ
range_type	__class__r4   r8   rQ   d   s    zGraphPlot.get_extentsc                 C   s    t |tr|j}| d d S Nr   )
isinstancer   r(   rI   rB   rL   r4   r4   r8   _get_axis_dimsh   s    
zGraphPlot._get_axis_dimsc              
      s  | j}|sd S |d}|jd}t|ts:d }||}	t|j	}
|
|}|	jv rt|jj
ddd}n"|	dkr|jjdv rd }nt|}|d u rdnd}|d	|d
| dd j D |d u s|jjdv r|	jvrd\}}n|jjdkr|tj}|tj}|jjdvrN|
d7 }
dd |D }dd |D }t|}t trz fdd|D }nt|p t|}|
|vr|||
< t| d}||||||dd}|
|d}jrdnd}||d| < ||d| < ||d| < d S )Nstyle
edge_colorr   F)ZexpandeduifZviridisZtab20r>   r=   c                 S   s   i | ]\}}|t |qS r4   )r#   r6   kvr4   r4   r8   
<dictcomp>   r:   z.GraphPlot._get_edge_colors.<locals>.<dictcomp>)NNfZSUZ_str__c                 S   s   g | ]}t |qS r4   strr6   r`   r4   r4   r8   r9      r:   z.GraphPlot._get_edge_colors.<locals>.<listcomp>c                 s   s   | ]}t |V  qd S rO   ra   rc   r4   r4   r8   	<genexpr>   r:   z-GraphPlot._get_edge_colors.<locals>.<genexpr>c              	      s"   g | ]}  | d jqS )ZNaN)getZ_default_nanrc   r=   Z
nan_colorsrB   r4   r8   r9      r:   )r=   r.   Zedge_colormapper)field	transformZ
fill_colorZ
line_colorr3   Zedge_nonselection_Zedge_selection_)get_dimensionedge_color_indexlookup_optionskwargsre   rV   r   Zget_dimension_indexr   namedimension_values_node_columnsr(   dtypekindr   Zclipping_colorsitemsastypenpint32listdictr   lenZ_get_colormapperr@   )rB   rL   rR   Z	edge_dataedge_mappingrY   ZcdimZelstylecycleidxrg   ZcvalsZfactorsZdefault_cmapcolorsZ
edge_styleZmapperrh   Z
color_typer4   rf   r8   _get_edge_colorsn   sT    







zGraphPlot._get_edge_colorsc                    s4  i i  }}| j rdnd\ |jd ur|jjd|jjd}t|t|kr fdd|D |d< fdd|D |d	< dd	d
}ntdt|t|f n| jr,|j	jd d \}}||j
 d }||j
 d }	t|	d |	d  |d |d  | j }
t||
}dd |D |d< dd |D |d	< ||fS )N)r   r   )r   r   array)ZdatatyperI   c                    s   g | ]}|d d  f qS rO   r4   r6   path)xidxr4   r8   r9      r:   z-GraphPlot._get_edge_paths.<locals>.<listcomp>xsc                    s   g | ]}|d d  f qS rO   r4   r   )yidxr4   r8   r9      r:   ys)r   r   zQEdge paths do not match the number of supplied edges.Expected %d, found %d paths.r   rN   r   r   c                 S   s   g | ]}|d d df qS )Nr   r4   r6   Zarrr4   r4   r8   r9      r:   c                 S   s   g | ]}|d d df qS )Nr   r4   r   r4   r4   r8   r9      r:   )invert_axesZ
_edgepathsZ_split_edgepathssplit	edgepathsrJ   rx   
ValueErrordirectedr(   rm   rt   Zhypotarrowhead_lengthr   )rB   rL   rR   	path_datamappingr)   ZxdimZydimZx_rangeZy_rangeZ	arrow_lenZarrowsr4   )r   r   r8   _get_edge_paths   s(    

*
zGraphPlot._get_edge_pathsc                    s8  j }|jd< d_  jd} jddg}|jjdvrdd t|D tjfd	d
|D tj	d}fddt
||D }n$|tj	}fddt
||D }d|i}	|dd }
 djd}t|trd|vr|}nd }j j||d|dd\}}|
d ur&|s&|
|d< |	| |}d|v r\dd | D }|d |d< i }t|rv| d nd fddtdD \}}|jjdkr|tj	|tj	 }}nN|jjdvrtjfdd
|D tj	d}tjfdd
|D tj	d}t||d} |||| |sT |\}}|| || djv rjdkrȈ jdfd d
 jdD |	d!<  j d"d  D ]} j||	t|j< qnFjd#kr  D ]0}t|j}|dv r|d$7 } |||< qd%|	j |d&|i}d%|j |i}|||fS )'Nstatic_sourceFr   r   r   r[   c                 S   s   i | ]\}}||qS r4   r4   )r6   ir^   r4   r4   r8   r_      r:   z&GraphPlot.get_data.<locals>.<dictcomp>c                    s   g | ]} | qS r4   r4   )r6   n)node_indicesr4   r8   r9      r:   z&GraphPlot.get_data.<locals>.<listcomp>)rp   c                    s4   i | ],\}\}}t  | jr(||fn||fqS r4   rb   r   r6   r]   xy)r   rB   r4   r8   r_      s   
c                    s0   i | ](\}\}}t | jr$||fn||fqS r4   r   r   rA   r4   r8   r_      s   
indexZ
node_colorrY   r=   node_fill_colorT)rm   r|   Zint_categoriesc                 S   s   i | ]\}}|d vr||qS ))r   node_nonselection_fill_colorr4   r\   r4   r4   r8   r_      s   r   c                 3   s   | ]}  |V  qd S rO   rn   r6   r   )rL   r4   r8   rd      r:   z%GraphPlot.get_data.<locals>.<genexpr>r`   Zuic                    s   g | ]} | qS r4   re   )r6   r   nan_noder   r4   r8   r9      r:   c                    s   g | ]} | qS r4   r   )r6   r   r   r4   r8   r9      r:   rD   r*   r(   c                    s   g | ]}  |qS r4   Zpprint_valuer6   r^   )	index_dimr4   r8   r9      r:   Zindex_hoverr   r)   Z_values	scatter_1layout)!r   handlesr(   rn   r~   rp   rq   	enumeratert   ru   ziprs   poprk   rl   re   rV   r   Z_get_color_dataupdaterr   rx   maxrangerw   r}   r   rH   ri   rI   r   rm   rC   )rB   rL   rR   rY   Zstaticr(   Znode_positionsr   r   Z
point_dataZfixed_colorrz   r|   ZcdataZcmappingZpoint_mappingry   rE   rF   r   ZpdataZpmappingdZdim_namedatar   r4   )rL   r   r   r   rB   r8   get_data   sz    





  



zGraphPlot.get_datac                 C   s>   t |tr4| jd r&|d|j| q:|j| n||_dS )z>
        Update datasource with data for a new frame.
        r   r   N)rV   r   r   Ztriggerr   r   graph_layout)rB   sourcer   r4   r4   r8   _update_datasource  s
    

zGraphPlot._update_datasourcec                 C   s   | j r
tnt}| }dD ]r}t|j|d d}|du r:qt|}| | j|i }	| 	|	||}
|f i t|
fi |}t
|j|d | qdS )z*Replace edge renderer with filled renderer) 
selection_nonselection_hover_muted_glyphN)r@   r	   r
   
propertiesgetattredge_rendererrw   Z_process_propertiesrC   Z_filter_propertiessetattr)rB   rendererr   ry   Zglyph_modelZallowed_propertiesZ
glyph_typer   Zgroup_propertiesZpropsZfilteredZ	new_glyphr4   r4   r8   _init_filled_edges  s    zGraphPlot._init_filled_edgesc              	      sn  g }i i  }}	d| j fD ]}
||
i }t|}| jd|
ddd t " | ||||}W d   n1 s0    Y  | 	|}|| j
|
d < || fdd| j D }| |||||}| D ]*\ }t fdd	|D rq|| < q|	||
i  qd
d | D }||	 |di }t|d}|| j
d< t||g |fS )z2Computes the args and kwargs for the GraphRendererr   _N_sourcec                    s   g | ]}| kr|qS r4   r4   )r6   Zsg)style_groupr4   r8   r9   /  r:   z3GraphPlot._get_graph_properties.<locals>.<listcomp>c                 3   s   | ]}  |V  qd S rO   )
startswith)r6   o)r]   r4   r8   rd   3  r:   z2GraphPlot._get_graph_properties.<locals>.<genexpr>c                 S   s&   i | ]\}}|d krd|vr||qS )r   Zlegendr4   )r6   r7   r^   r4   r4   r8   r_   7  s   z3GraphPlot._get_graph_properties.<locals>.<dictcomp>r   )r   Zlayout_source)rC   r   rw   _style_groupsre   joinr   r   Z_apply_transformsZ_init_datasourcer   appendvaluesZ_glyph_propertiesrr   anyr   r   tuple)rB   plotrL   r   r   rR   rY   Zsourcesr   ZmappingskeyZgdataZgroup_styler   ZothersZglyph_propsr7   r   r4   )r]   r   r8   _get_graph_properties  s4    
 0





zGraphPlot._get_graph_propertiesc                    sP   t fdd|D |d  fdd|jD } fddjD }|| |_dS )z2Reorders renderers based on the defined draw orderc                    s   i | ]}| j |d   qS )_glyph_renderer)r   r6   rrA   r4   r8   r_   D  s   z0GraphPlot._reorder_renderers.<locals>.<dictcomp>)graphc                    s   g | ]}|   vr|qS r4   )r   r   	renderersr4   r8   r9   F  r:   z0GraphPlot._reorder_renderers.<locals>.<listcomp>c                    s   g | ]}| v r | qS r4   r4   )r6   r]   r   r4   r8   r9   G  r:   N)rw   r   _draw_order)rB   r   r   r   otherZgraph_renderersr4   )r   rB   r8   _reorder_renderersB  s    zGraphPlot._reorder_renderersc                 C   sd   | j dkrt |_ n| j dkr(t |_ nt |_ | jdkrDt |_n| jdkrXt |_nt |_d S )Nr(   r)   )selection_policyr   r   r   rH   )rB   r   r4   r4   r8   _set_interaction_policiesJ  s    







z#GraphPlot._set_interaction_policiesc              
   C   sN  | j | j }| |||\}}}|j| jd< dd || j  D }| ||||||\}	}
|j|	i |
}| j	sx| j
r| ||
| | | |r|rt| ||||||| | jr| ||| || jd< |j| jd< |j| j| jd < |jj| jd< |jj| j| jd < d	| jv rJ| jd	 jd
kr8g | jd	 _| jd	 j| d S )NZprevious_idc                 S   s   i | ]\}}d |vr||qS )Zcolorr4   r\   r4   r4   r8   r_   `  s   z*GraphPlot._init_glyphs.<locals>.<dictcomp>Zglyph_rendererscatter_1_glyph_rendererr   Zscatter_1_glyphZ_glyphr*   auto)rY   cyclic_indexr   Z_plot_idr   rC   rr   r   r   r@   r1   r   r   r   _init_glyphsr   r   Znode_rendererr   r   r   r   )rB   r   rL   rR   r   rY   r   r   ry   Z
graph_argsr   r   r4   r4   r8   r   Y  s4    

zGraphPlot._init_glyphs)rN   )*__name__
__module____qualname__paramNumberr   Booleanr   ZObjectSelectorr   rH   ListZtoolsClassSelectorr   intZcolor_indexrj   r   r   r!   r    
style_optsZ_nonvectorized_stylesr@   r1   ro   propertyrC   rM   rQ   rX   r}   r   r   r   r   r   r   r   r   __classcell__r4   r4   rS   r8   r$      sJ   
1K&r$   c                       s   e Zd ZejeefddZejdddZ	ejdee
fddd	Zd
dddZejdd ee D  Zg dZdd Z fddZ fddZ fddZ  ZS )	ChordPlotzM
        The dimension or dimension value transform used to draw labels from.)r,   r'   FzA
        Whether or not to show a complete frame around the plot.r%   NTzF
      Index of the dimension from which the node labels will be drawnr+   r-   r.   Zlabel)r/   r0   textc                 C   s   g | ]}d | qS )Zlabel_r4   r5   r4   r4   r8   r9     r:   zChordPlot.<listcomp>)multi_line_2r   text_1c                 C   sv   | j d }| j d }dD ]X}t||d }t||d }|r|sBq|jdd}dd | D }|jf i | qd S )	Nmulti_line_2_glyph_rendererr   )r   r   r   r   r   r   F)Zinclude_defaultsc                 S   s&   i | ]\}}d |v r| d d|qS )fillline)replacer\   r4   r4   r8   r_     s   z(ChordPlot._sync_arcs.<locals>.<dictcomp>)r   r   Zproperties_with_valuesrr   r   )rB   arc_rendererscatter_rendererZgtyper   Z	arc_glyphZscatter_propsstylesr4   r4   r8   
_sync_arcs  s    

zChordPlot._sync_arcsc                    s\   t t| |||| d| jv rX| jd }| jd }|j|_|j|_|j| jd< |   d S )Nmulti_line_2_glyphr   r   Zmulti_line_2_source)rP   r   r   r   viewZdata_sourcer   )rB   r   rL   rR   r   r   r   rS   r4   r8   r     s    


zChordPlot._init_glyphsc                    s*   d| j v r|   tt| ||| d S )Nr   )r   r   rP   r   _update_glyphsrB   rL   rR   rY   rS   r4   r8   r     s    
zChordPlot._update_glyphsc                    sB  | ddtt| |||\}}}|j}tt}tt|j	D ]X}|||d  \}	}
t
|	|
d}t
|t
| }}|d | |d | qB|d | |d |d< ddd	d
|d< |j	| j}| j|rr| jd n"|r|nttr|j	d u r&|||fS |j	|jr||jd }|jjdv rt||dk  tt
 fddtdD |j	jf i |j	jd jifddtdD \}}tt rڈj!|dd}n|j	}fdd|D }t
"||}t#||dd |D |d|d< t#dddddd|d< |||fS )NZlabel_offsetg?r      Zarc_xsZarc_ysr   r   
   )r   r   Z
line_widthzeCannot declare style mapping for 'labels' option and declare a label_index; ignoring the label_index.r   r[   c                    s   g | ]}  |qS r4   r   r   )r)   r4   r8   r9     r:   z&ChordPlot.get_data.<locals>.<listcomp>c                 3   s   | ]}  | V  qd S rO   r   r   )r(   offsetr4   r8   rd     r:   z%ChordPlot.get_data.<locals>.<genexpr>T)Zflatc                    s   g | ]}  |qS r4   r   r   )labelsr4   r8   r9     r:   c                 S   s   g | ]}t |qS r4   ra   )r6   lr4   r4   r8   r9     r:   )r   r   r   angler   r   r   r   r   Zmiddle)r   r   r   r   Ztext_baseline)$r   rP   r   r   Z_anglesr   rv   r   rx   r(   rt   ZlinspaceZcosZsinr   r   ri   label_indexr   r   ZwarningrV   r   rK   rn   rp   rq   r   uniqueZselectrJ   rm   r   ZapplyZarctan2rw   )rB   rL   rR   rY   r   r   ZanglesZarcsr   rE   rF   Zvalsr   r   Z	label_dimr   r   rS   )r)   r   r(   r   r8   r     sR    

  zChordPlot.get_data)r   r   r   r   r   r   r   r   r   Z
show_framer   r   r   r$   r   r   r"   r   r   r   r   r   r   r4   r4   rS   r8   r   ~  s   r   c                   @   s   e Zd ZdZdd ZdS )NodePlotzJ
    Simple subclass of PointPlot which hides x, y position on hover.
    c                 C   s   |  dd  i fS rU   )rI   rW   r4   r4   r8   rM     s    zNodePlot._hover_optsN)r   r   r   __doc__rM   r4   r4   r4   r8   r     s   r   c                       sz   e Zd ZejdddZdd ee e D dd ee e D  g d Z	g dZ
d	d
 Z fddZ fddZ  ZS )TriMeshPlotFz9
        Whether the triangles should be drawn as filled.r%   c                 C   s   g | ]}d | qS r2   r4   r5   r4   r4   r8   r9     r:   zTriMeshPlot.<listcomp>c                 C   s   g | ]}d | qS r;   r4   r5   r4   r4   r8   r9     r:   )r<   r=   r>   r?   r   r   r   c                 C   s   | j | j }|d}||jvr&| j}||}|j|}|r|s|g d}|j|}|| jdd}|j	|t
|j|dd}|j |S )NrZ   r  r   )ZaxisT)Zvdim)rY   r   re   r(   rj   ri   r~   rn   ZmeanZadd_dimensionrx   rK   r   )rB   rL   rY   rZ   Zsimplex_dimZ
vertex_dimZ	simpliceszr4   r4   r8   _process_vertices  s    


zTriMeshPlot._process_verticesc                    s$   |  |}tt| |||| d S rO   )r  rP   r  r   )rB   r   rL   rR   r   rS   r4   r8   r     s    
zTriMeshPlot._init_glyphsc                    s"   |  |}tt| ||| d S rO   )r  rP   r  r   r   rS   r4   r8   r   
  s    
zTriMeshPlot._update_glyphs)r   r   r   r   r   r@   r   r    r!   r   ro   r  r   r   r   r4   r4   rS   r8   r    s   r  )2Z
__future__r   r   r   collectionsr   r   Znumpyrt   Zbokeh.modelsr   r   r   r	   r
   r   r   Z	core.datar   Zcore.optionsr   r   Z	core.utilr   r   r   rL   r   Zutil.transformr   Zmixinsr   utilr   r   Zchartr   r   r   r   r   r   r    r!   r"   r#   r$   r   r   r  r4   r4   r4   r8   <module>   s(   $  ec
