a
    Df0i                     @   sp  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 ddlmZmZ dd	lmZmZ dd
lmZ ddlmZmZmZmZmZmZmZ ddlmZ G dd deZ G dd deZ!G dd deZ"ej#dg ddZ$ej#dddZ%eddZ&G dd dej'Z(G dd de(Z)G dd dZ*G d d! d!e*Z+G d"d# d#e*Z,G d$d% d%e*Z-d&d' Z.dS )(    )
namedtupleN)
bothmethod   )Dataset)ElementLayout)AdjointLayout)CallbackErrorStore)	NdOverlayOverlay)	GridSpace)CrossFilterSetDerivedPipe	PlotResetSelectionExprSequence
SelectModeStream)
DynamicMapc                   @   s   e Zd ZejddddZdS )_CmapNT)default
allow_Noneconstant)__name__
__module____qualname__param	Parametercmap r    r    `/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/selection.pyr      s   r   c                   @   s   e Zd ZejddddZdS )_SelectionExprOverrideNTz=
            dim expression of the current selection override)r   r   doc)r   r   r   r   r   selection_exprr    r    r    r!   r"      s   r"   c                       s4   e Zd ZejddZ fddZedd Z  Z	S )_SelectionExprLayersT)r   c                    s    t  j||gfddi| d S )NZ	exclusiveT)super__init__)selfZexpr_overrideZcross_filter_setparams	__class__r    r!   r'   %   s    z_SelectionExprLayers.__init__c                 C   sD   |d }|d }| dd d ur0dd|d giS dd|d giS d S )Nr   r   r$   exprsT)get)clsZstream_values	constantsZoverride_expr_valuesZcross_filter_set_valuesr    r    r!   transform_function*   s
    z'_SelectionExprLayers.transform_function)
r   r   r   r   Listr,   r'   classmethodr0   __classcell__r    r    r*   r!   r%   "   s   r%   ZStylesg      ?colorsalphaZRegionElement)region_elementZSelectionStreamsz'style_stream exprs_stream cmap_streams c                       sv   e Zd ZdZejdddZejdddZe fddZ	d	d
 Z
dd Zdd ZdddZedd Zdd Z  ZS )_base_link_selectionsa  
    Baseclass for linked selection functions.

    Subclasses override the _build_selection_streams class method to construct
    a _SelectionStreams namedtuple instance that includes the required streams
    for implementing linked selections.

    Subclasses also override the _expr_stream_updated method. This allows
    subclasses to control whether new selections override prior selections or
    whether they are combined with prior selections
    Fz@
        Whether to link any streams on the input to the output.r   r#   Tz3
        Whether to highlight the selected regions.c                    sD   t  jf i |}t|jd|_t |_i |_i |_| 	||_
|S )N)mode)r&   instancer   cross_filter_mode_cross_filter_streamr"   _selection_override_selection_expr_streams_plot_reset_streams_build_selection_streams_selection_streamsself_or_clsr)   instr*   r    r!   r;   Q   s    z_base_link_selections.instancec                 C   sJ   |j dkrd| _n4|j dkr$d| _n"|j dkr6d| _n|j dkrFd| _d S )Nreplace	overwriteappendunion	intersectsubtractinverse)newselection_mode)r(   eventr    r    r!   _update_mode`   s    



z"_base_link_selections._update_modec                 C   s   ddl m} t|| j| j| jd}|| j|< | j| t	|d| j
|< |jfdd}t||szt|d}|j| jd | j
| j|dg d	S )
z
        Register an Element or DynamicMap that may be capable of generating
        selection expressions in response to user interaction events
        r   )Table)r:   include_region
index_cols)sourcec                 S   s   | r|   |  d S N)clear_historyrO   	resettingstreamr    r    r!   clear_stream_history}   s    z=_base_link_selections._register.<locals>.clear_stream_historyr:   rX   N)elementrQ   r   rN   show_regionsrS   r?   r=   Zappend_input_streamr   r@   Zhistory_stream
isinstancer   r   watchrP   )r(   hvobjrQ   Zselection_expr_seqrZ   Zmode_streamr    r    r!   	_registerj   s     


z_base_link_selections._registerc                 K   s:   | j jf i | tjtjvr,tdtj | | S )NzCannot perform link_selections operation since the selected backend %r is not loaded. Load the plotting extension with hv.extension or import the plotting backend explicitly.)r   updater
   current_backendZ	renderersRuntimeError_selection_transformclone)r(   r_   kwargsr    r    r!   __call__   s    z_base_link_selections.__call__r    c           
         s8  ddl m} ttr*j}t|jdkrHtfdd|jD  S | t	j
tr tjtj j
 }|jjjdjdS t	j
trtjdddkrtfd	d|jD  S tjddd
kr|jd }j|d|jS jd S n
ttrtjtj t
 }t|dddurjjd}| ||jj|j|djdS S ttttt t!fr0t"fdd# D  ttr fdd}t|}	t$ % |	jjdd< n }	t&|	dr,|	 }	|	S S dS )zs
        Transform an input HoloViews object into a dynamic object with linked
        selections enabled.
        r   )initialize_dynamicc                    s   g | ]}  |qS r    rd   .0elr(   r    r!   
<listcomp>   s   z>_base_link_selections._selection_transform.<locals>.<listcomp>N)cachenameZdynamic_mulc                    s   g | ]}j | d qS )
operationsri   rj   rr   r(   r    r!   rn      s   Zdynamic_operationr   rq   zQlinked selection: Encountered DynamicMap that we don't know how to recurse into:
selection_displaylinkc                    s    g | ]\}}| | fqS r    ri   )rk   kvrs   r    r!   rn      s   c                     sj   g }   D ]F\}}t|d D ]0\}}|t|kr>|g  || ||f q qtfdd|D S )Nr    c                    s   g | ]}  |qS r    )re   )rk   n)r_   r    r!   rn          zO_base_link_selections._selection_transform.<locals>.compose.<locals>.<listcomp>)items	enumeratelenrH   r   )argsrf   rM   rw   rx   irl   )datar_   r    r!   compose   s    
z;_base_link_selections._selection_transform.<locals>.composecollate)'plotting.utilrh   r]   r   callbackr}   inputsr   r   
issubclasstyper   r`   r
   registryrb   rt   build_selectionrB   r?   r-   _cachegetattrrd   applyZ	operationr   warningre   link_inputsr   r   r   r   dictr{   listvalueshasattr)
r(   r_   rr   rh   r   Zchartobjr[   r   Z	new_hvobjr    )r   r_   rr   r(   r!   rd      sn    





z*_base_link_selections._selection_transformc                 C   s
   t  dS )zg
        Subclasses should override this method to return a _SelectionStreams
        instance
        NNotImplementedError)r.   rE   r    r    r!   rA      s    z._base_link_selections._build_selection_streamsc                 K   s
   t  dS )a  
        Called when one of the registered HoloViews objects produces a new
        selection expression.  Subclasses should override this method, and
        they should use the input expression to update the `exprs_stream`
        property of the _SelectionStreams instance that was produced by
        the _build_selection_streams.

        Subclasses have the flexibility to control whether the new selection
        express overrides previous selections, or whether it is combined with
        previous selections.
        Nr   )r(   r_   r$   Zbboxr7   rf   r    r    r!   _expr_stream_updated   s    z*_base_link_selections._expr_stream_updated)r    )r   r   r   __doc__r   Booleanr   r\   r   r;   rP   r`   rg   rd   r2   rA   r   r3   r    r    r*   r!   r8   >   s   
 
H
r8   c                       s   e Zd ZdZejddgdddZejdddZej	dd	dZ
ejdd
ddZejg ddddZejdddZejdddZe fddZejdd
ddd Zdd Zd$ddZedd Zedd Zed d! Zed"d# Z  ZS )%link_selectionsz
    Operation which automatically links selections between elements
    in the supplied HoloViews object. Can be used a single time or
    be used as an instance to apply the linked selections across
    multiple objects.
    rG   rJ   zP
        Determines how to combine selections across different
        elements.)Zobjectsr   r#   Na7  
        If provided, selection switches to index mode where all queries
        are expressed solely in terms of discrete values along the
        index_cols.  All Elements given to link_selections must define the index_cols, either as explicit dimensions or by sharing an underlying Dataset that defines them.r9   zi
        dim expression of the current selection or None to indicate
        that everything is selected.Tz[
        Color of selected data, or None to use the original color of
        each element.)r   r   r#   )rG   rJ   rI   rL   zU
        Determines how to combine successive selections on the same
        element.g?z"
        Alpha of unselected data.z"
        Color of unselected data.c                    s>   t  jf i |}i |_i |_d|_g |_i |_| | |S )NT)r&   r;   Z_obj_selectionsZ_obj_regionsZ_reset_regions	_datasetsr   _install_param_callbacksrC   r*   r    r!   r;     s    
zlink_selections.instancer$   )r^   c           	      C   s   | j }| jD ]\}}}|j}| j|i  | j|< }||v rV|| }|j|jd  S |  t	||| j}|||< |j|r|jn|d qd S )Nr   )
r$   r   _plot_idr   r-   rO   r   clearSelectionDisplay_select)	r(   Zsel_exprpipedsrawrefds_cacher   Zsel_dsr    r    r!   _update_pipes-  s    zlink_selections._update_pipesc                 C   s@   d}t |tsd}t|}t|jd}| j|||f |jjS )ao  
        Returns a parameter which reflects the current selection
        when applied to the supplied data, making it easy to create
        a callback which depends on the current selection.

        Args:
            data: A Dataset type or data which can be cast to a Dataset

        Returns:
            A parameter which reflects the current selection
        FTr   )r]   r   r   r   r   rH   r   )r(   r   r   r   r    r    r!   selection_param<  s    
zlink_selections.selection_paramc                 C   sP   |du r| j n|}|du r|S t|t}|s4t|}||| }|rJ|S |jS )aG  
        Filters the provided data based on the current state of the
        current selection expression.

        Args:
            data: A Dataset type or data which can be cast to a Dataset
            selection_expr: Optionally provide your own selection expression

        Returns:
            The filtered data
        N)r$   r]   r   r   r   )r(   r   r$   exprZ
is_datasetfilteredr    r    r!   filterP  s    
zlink_selections.filterc           	         s    fdd} j |dg  fdd} j |dg  fdd} j |d	g  fd
d} j |dg  fdd} jj |dg  j D ],}|fdd}td| |jj |dg qd S )Nc                     s$    j  D ]}|   j|_q
d S rU   )r?   r   resetrN   r:   _rY   rE   r    r!   update_selection_modeg  s    zGlink_selections._install_param_callbacks.<locals>.update_selection_moderN   c                     s    j    j j _d S rU   )r=   r   r<   r:   )r   r   r    r!   update_cross_filter_modeq  s    
zJlink_selections._install_param_callbacks.<locals>.update_cross_filter_moder<   c                     s$    j  D ]} j|_|  q
d S rU   )r?   r   r\   rR   rO   r   r   r    r!   update_show_regiony  s    zDlink_selections._install_param_callbacks.<locals>.update_show_regionr\   c                     s<    j } jj }t|t|kr8 jr*d _ jj|d d S )NF)r$   )r$   r=   reprr\   r>   rO   )r   new_selection_exprZcurrent_selection_exprr   r    r!   update_selection_expr  s    zGlink_selections._install_param_callbacks.<locals>.update_selection_exprr$   c                     s$    j j}t jt|kr | _d S rU   )r=   r$   r   )r   r   r   r    r!   selection_expr_changed  s    zHlink_selections._install_param_callbacks.<locals>.selection_expr_changedc                 S   s   | r|   d S rU   )rV   rW   r    r    r!   rZ     s    zFlink_selections._install_param_callbacks.<locals>.clear_stream_historyzregistering reset for rX   )r   r^   r=   r?   r   printZplot_reset_stream)	rD   rE   r   r   r   r   r   rY   rZ   r    r   r!   r   e  s4    	
z(link_selections._install_param_callbacksc                    sl   t jjgjdtjdtjdg  fdd}j|g d t	j
j}t| dS )Nr4   r   c                     sP   j jg}j|jd  d jjd  d d urL d jjd d S )Nr4   r   r   r   )unselected_colorselected_colorrO   unselected_alphaunselected_cmapselected_cmap)r   r5   cmap_streamsrE   style_streamr    r!   update_colors  s
    z?link_selections._build_selection_streams.<locals>.update_colors)r   r   r   )r   exprs_streamr   )_Stylesr   r   r   r   r   r   r   r^   r%   r>   r=   _SelectionStreams)r.   rE   r   r   r    r   r!   rA     s"    


z(link_selections._build_selection_streamsc                 C   s   | j du rdS t| j S )z=
        The datashader colormap for unselected data
        N)r   _color_to_cmaprm   r    r    r!   r     s    
zlink_selections.unselected_cmapc                 C   s   | j du rdS t| j S )z;
        The datashader colormap for selected data
        N)r   r   rm   r    r    r!   r     s    zlink_selections.selected_cmap)N)r   r   r   r   r   ZSelectorr<   r1   rS   r   r$   ZColorr   rN   Z	Magnituder   r   r   r;   dependsr   r   r   r   r2   rA   propertyr   r   r3   r    r    r*   r!   r      s4   




=
!
r   c                   @   s0   e Zd ZdZdd Zd	ddZed
ddZdS )r   z
    Base class for selection display classes.  Selection display classes are
    responsible for transforming an element (or DynamicMap that produces an
    element) into a HoloViews object that represents the current selection
    state.
    c                 C   s   | S rU   r    )r(   r[   r    r    r!   rg     s    zSelectionDisplay.__call__Nc                 C   s   |d u ri }t  d S rU   r   r(   selection_streamsr_   rr   region_streamro   r    r    r!   r     s    z SelectionDisplay.build_selectionc              
   C   s  |d u ri }ddl m}m} ddlm} t||r| j}d }|j|v rn||j }||v rd|| }q||  ni  }||j< z|j	j
r|d u r|j|dddd}||j	|| }	n|j	jr|d u r|j|dddd}|j| }	nxt| ||fr6t|j	dr6|d u r|j|ddd}||j	|| }	n(|d u rR|j|dddd	}|j|d
}	W n ty }
 z<t|
dddd}td| d| d|
W Y d }
~
n<d }
~
0  ty }
 ztd|
 |
W Y d }
~
n
d }
~
0 0 |||< n| }	|	S )Nr   )CurveSpread)dimTF)expandedZflatstrictmask)
keep_indexr   )Zcomputer   r   )Zselection_mask" .zRlinked_selection aborted because it could not display selection for all elements: z on 'z'.zUlinked_selection aborted because it could not display selection for all elements: %s.)r[   r   r   Zutil.transformr   r]   datasetr   r   Z	interfaceZgriddedr   re   r   ZmultiZilocr   selectKeyErrorstrrF   r	   	Exception)r[   r$   ro   r   r   r   r   r   r   	selectioneZ	key_errorr    r    r!   r     s^    






zSelectionDisplay._select)NN)N)r   r   r   r   rg   r   staticmethodr   r    r    r    r!   r     s
   
r   c                   @   s   e Zd ZdZdddZdS )NoOpSelectionDisplayz
    Selection display class that returns input element unchanged. For use with
    elements that don't support displaying selections.
    Nc                 C   s   |S rU   r    r   r    r    r!   r     s    z$NoOpSelectionDisplay.build_selection)NN)r   r   r   r   r   r    r    r    r!   r     s   r   c                   @   sZ   e Zd ZdZdddZdd Zdd
dZedd Zdd Z	dd Z
dddZdd Zd	S )OverlaySelectionDisplayz
    Selection display base class that represents selections by overlaying
    colored subsets on top of the original element in an Overlay container.
    colorFTc                 C   s.   t |ttfs|g| _n|| _|| _|| _d S rU   )r]   r   tuplecolor_propsis_cmapsupports_region)r(   
color_propr   r   r    r    r!   r'   &  s
    
z OverlaySelectionDisplay.__init__c                    s    fddj D S )Nc                    s   i | ]}|j r gn qS r    )r   rk   r   r   r(   r    r!   
<dictcomp>/  s   z<OverlaySelectionDisplay._get_color_kwarg.<locals>.<dictcomp>)r   )r(   r   r    r   r!   _get_color_kwarg.  s    z(OverlaySelectionDisplay._get_color_kwargNc                    sf  ddl m} t|jj}|s"t S g }t|D ]T}	|jg}
|	dkrN|jddn|}|j	|	 }|j
 j|g|
 |	|dd}|| q.t|D ]<}	||	 }|j	|	 }|j|g}
|j
 j|	|
dd}|||	< q|d urZ jrZ fdd	}||jg}
|jddj
||
dd
}t|tr|jnt|}t|dd dksBt||rP|d| n
|| t| S )Nr   )	HistogramFru   T)streamslayer_numberro   per_element)r   r   r   c                    s$   |d }|d u r|   } ||S )Nr   )Z_empty_region_style_region_element)r[   r7   r5   rf   r   rm   r    r!   update_regionP  s    z>OverlaySelectionDisplay.build_selection.<locals>.update_region)Zlink_datasetZ_selection_dims)r[   r   r}   r   r5   r   ranger   re   r   r   _build_layer_callbackrH   _apply_style_callbackr   r]   r   r   r   r   insertr   )r(   r   r_   rr   r   ro   r   Z
num_layersZlayersr   r   r   Zcmap_streamZlayerr   ZregionZeltyper    rm   r!   r   2  s@    





z'OverlaySelectionDisplay.build_selectionc                 C   s<   g }|j D ]$}t|dr$|j|d}|| q
|j|dS )Nr   r   rq   )rr   r   r;   rH   )r.   pipeliner   rr   opr    r    r!   _inject_cmap_in_pipeline`  s    

z0OverlaySelectionDisplay._inject_cmap_in_pipelinec           	      K   sJ   |  ||| |}|j}|d ur,| ||}||u r>||jS ||S d S rU   )r   r   r   r   )	r(   r[   r,   r   r   ro   rf   r   r   r    r    r!   r   i  s    
z-OverlaySelectionDisplay._build_layer_callbackc           	      K   sP   i }|dkrd|d< nd}|d ur*||d< |r6|| nd }| j |||fi |S )Nr   FZcolorbarr   r   )_build_element_layer)	r(   r[   r   r5   r   r6   rf   optsr   r    r    r!   r   s  s    
z-OverlaySelectionDisplay._apply_style_callbackc                 C   s
   t  d S rU   r   )r(   r[   Zlayer_colorZlayer_alphar$   r    r    r!   r   ~  s    z,OverlaySelectionDisplay._build_element_layerc                 C   s
   t  d S rU   r   )r(   r7   r   r    r    r!   r     s    z-OverlaySelectionDisplay._style_region_element)r   FT)NN)T)r   r   r   r   r'   r   r   r2   r   r   r   r   r   r    r    r    r!   r      s   

.


r   c                   @   s$   e Zd ZdZd	ddZd
ddZdS )ColorListSelectionDisplayzd
    Selection display class for elements that support coloring by a
    vectorized color list.
    r   r6   Nc                 C   s   |g| _ |g| _|| _d S rU   )r   Zalpha_propsbackend)r(   r   Z
alpha_propr   r    r    r!   r'     s    z"ColorListSelectionDisplay.__init__c           	         sL   |d u ri } fdd}|j |jg}|j||d|d}|D ]}||}q:|S )Nc                    s   ddl m} | j}|dd  }d }|p,d}||ddd   fdd	dd  D }	t|}
t|gt|	 }tj|
d
d}tt	dt||D ](\}}|s||d d < q|||
|< q|| fddjD }| |jf jdd|S )Nr   linear_gradientr   z#e6e9ec#000000      c                    s   g | ]}|p qS r    r    )rk   c)
backup_clrr    r!   rn     rz   zWColorListSelectionDisplay.build_selection.<locals>._build_selection.<locals>.<listcomp>Zint8)Zdtypec                    s   i | ]
}| qS r    r    r   )r5   r    r!   r     rz   zWColorListSelectionDisplay.build_selection.<locals>._build_selection.<locals>.<dictcomp>T)r   re   )r   r   r   r}   nparrayr   zeroszipr   r   r   r   r   r   )rl   r5   r6   r,   rf   r   r   Zselection_exprsr   Zselected_colorsry   ZclrsZ
color_indsr   r   Z
color_optsrm   )r   r5   r!   _build_selection  s"    zCColorListSelectionDisplay.build_selection.<locals>._build_selectionT)r   r   ro   )r   r   r   )	r(   r   r_   rr   r   ro   r  Zsel_streamsr   r    rm   r!   r     s    
z)ColorListSelectionDisplay.build_selection)r   r6   N)NN)r   r   r   r   r'   r   r    r    r    r!   r     s   
r   c                 C   s8   ddl m} |d| dd }|| ddd }|||dS )z<
    Create a light to dark cmap list from a base color
    r   r   z#ffffffr   r   r   @   )r   r   )r   r   Zstart_colorZ	end_colorr    r    r!   r     s    r   )/collectionsr   numpyr   r   Zparam.parameterizedr   Z	core.datar   Zcore.elementr   r   Zcore.layoutr   Zcore.optionsr	   r
   Zcore.overlayr   r   Zcore.spacesr   r   r   r   r   r   r   r   r   utilr   r   r"   r%   Zdefiner   Z_RegionElementr   ZParameterizedFunctionr8   r   r   r   r   r   r   r    r    r    r!   <module>   s:   $	 : `@
e0