a
    –½DfàN  ã                   @   s   d Z ddlZddlmZ ddlZddlZddlm	Z	m
Z
mZ ddlmZmZmZ ddlmZ dd	lmZmZ G d
d„ dƒZdd„ Zd$dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZG d d!„ d!eƒZ G d"d#„ d#eƒZ!dS )%zK
Defines mix-in classes to handle support for linked brushing on
elements.
é    N)Ú	find_specé   )ÚDatasetÚ	NdOverlayÚutil)ÚLassoÚSelection1DÚSelectionXY)Údimé   )ÚHSpanÚVSpanc                       sJ   e Zd ZdZefZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z	e
d
d„ ƒZ‡  ZS )ÚSelectionIndexExprNc                    s   t ƒ j|i |¤Ž d| _d S )NF)ÚsuperÚ__init__Ú_index_skip)ÚselfÚargsÚkwargs©Ú	__class__© úh/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/element/selection.pyr      s    zSelectionIndexExpr.__init__c                 C   s   d S ©Nr   ©r   r   r   r   Ú_empty_region   s    z SelectionIndexExpr._empty_regionc           	         s,  dˆ _ |sdS ˆ j|td}t|ƒdkrŽ|d }t|ƒj|j| dd}|jjdkrtt	d	d
„ |D ƒƒrtdd„ |D ƒ}t|ƒ 
tt |¡ƒ¡}n”tˆ j |d ¡tjƒ}‡ fdd„|D ƒ}t|d tjg|dd … ¢R Ž j|j| ddd}t|d tjg|dd … ¢R Ž j
|dd}t|tj|ƒ}|d d fS )NT©NNN)ÚkdimsÚnew_typer   r   F©ÚexpandedÚOc                 s   s   | ]}t |tjƒV  qd S r   )Ú
isinstanceÚnpZndarray)Ú.0Úvr   r   r   Ú	<genexpr>'   ó    z:SelectionIndexExpr._get_index_selection.<locals>.<genexpr>c                 S   s    g | ]}t  |¡D ]}|‘qqS r   )r   Úunique_iterator)r$   Zarrr%   r   r   r   Ú
<listcomp>(   r'   z;SelectionIndexExpr._get_index_selection.<locals>.<listcomp>c                    s    g | ]}t ˆ j |¡tjƒ‘qS r   ©r
   ÚdatasetÚget_dimensionr#   Zravel©r$   Úcr   r   r   r)   ,   r'   ©r    Úflat©Úobject)r   Úcloner   Úlenr
   ÚapplyZilocÚdtypeÚkindÚallÚisinÚlistr   r(   r+   r,   r#   ÚshapeÚ
unique_zipÚlzipÚreshape)	r   ÚindexÚ
index_colsZdsÚ	index_dimÚvalsÚexprÚ	get_shapeÚcontainsr   r   r   Ú_get_index_selection   s$     ÿ*z'SelectionIndexExpr._get_index_selectionc                 K   s4   |  d¡}|  d¡}|d u s$|d u r(dS |  ||¡S )Nr?   r@   r   )ÚgetrF   )r   r   r?   r@   r   r   r   Ú$_get_selection_expr_for_stream_value4   s
    

z7SelectionIndexExpr._get_selection_expr_for_stream_valuec                 C   s   d S r   r   )Úregion1Úregion2Ú	operationr   r   r   Ú_merge_regions;   s    z!SelectionIndexExpr._merge_regions)Ú__name__Ú
__module__Ú__qualname__Ú_selection_dimsr   Ú_selection_streamsr   r   rF   rH   ÚstaticmethodrL   Ú__classcell__r   r   r   r   r      s   r   c                 C   sâ   t j| dddk ¡ }|r¾ddlm} ddlm} zddlm} W n t	y^   t	dƒd ‚Y n0 | d |d d …df  }}|||t  
|j|j ¡fƒ}	||gƒ}
||
|	d	d
d}|jdd	dS t|  ¡ | ¡ |ƒ}| | j¡S d S )Nr   )Zaxisr   )ÚPolygons)ÚImager   )Ú	rasterizezDLasso selection on gridded data requires datashader to be available.FÚany)ÚtargetZdynamicZ
aggregator)r0   )r#   Údiffr8   ÚpathrT   ZrasterrU   Zoperation.datashaderrV   ÚImportErrorÚemptyr;   Zdimension_valuesÚspatial_select_columnarÚflattenr>   )ÚxvalsÚyvalsÚgeometryZrectilinearrT   rU   rV   ÚxsÚysrX   ÚpolyÚsel_maskr   r   r   Úspatial_select_gridded@   s"    ÿ
rf   c              
   C   sN  dt jv rªdd l}t| |jƒrª| j d¡} |j d¡}zLdd l}| | ||jdgdgddg|d d …df |d d …df ¡}|jW S  t	y¨   t
 | ¡} t
 |¡}Y n0 dt jv r2dd lm} t| |jƒr2z8d| _d	|_|  ¡  |¡}|jd
d„ |tjtddW S  ty0   t
 | ¡} t
 |¡}Y n0 |d d …df  ¡ |d d …df  ¡  }	}
|d d …df  ¡ |d d …df  ¡  }}| |	k| |
k@ ||k@ ||k@ }| | }|| }|d u rötdƒd urÔd}n"tdƒd urèd}nd}t	|ƒd ‚ttdœ| }||||ƒ}t| tjƒr8|||jt
 |¡d  < n||t
 |¡d < |S )NÚcudfr   ÚfloatÚ	selection)r?   r   Zdaskr_   r`   c                 S   s   t | j| j|ƒS r   )r]   r_   r`   )Údfra   r   r   r   Ú<lambda>o   r'   z)spatial_select_columnar.<locals>.<lambda>©r6   )ÚmetaÚspatialpandasÚshapelyzYLasso selection on tabular data requires either spatialpandas or shapely to be available.)rn   ro   )ÚsysÚmodulesrg   r"   ZSeriesÚvaluesÚastypeÚ	cuspatialZpoint_in_polygonr[   r#   ZasarrayZdask.dataframeÚ	dataframeÚnameZto_frameÚjoinZmap_partitionsÚpdÚboolÚ	ExceptionÚminÚmaxr   Ú_mask_spatialpandasÚ_mask_shapelyr?   Úwhere)r_   r`   ra   Zgeom_methodrg   rt   ÚresultÚddrj   Úx0Úx1Úy0Úy1re   Úmasked_xvalsÚmasked_yvalsÚmsgZgeom_functionZ	geom_maskr   r   r   r]   S   sh    
ú

ý
** 

r]   c                 C   sR   ddl m}m} ||  d¡| d¡fƒ}|t ||d d… g¡ ¡ gƒ}| |¡S )Nr   )Ú
PointArrayÚPolygonrh   r   )Zspatialpandas.geometryr‰   rŠ   rs   r#   Úconcatenater^   Z
intersects)r†   r‡   ra   r‰   rŠ   Úpointsrd   r   r   r   r}   Œ   s     r}   c                    sL   ddl m‰ m} ‡ fdd„t| |ƒD ƒ}||ƒ‰tj‡fdd„|D ƒtdS )Nr   )ÚPointrŠ   c                 3   s   | ]\}}ˆ ||ƒV  qd S r   r   )r$   ÚxÚy)r   r   r   r&   •   r'   z _mask_shapely.<locals>.<genexpr>c                    s   g | ]}ˆ   |¡‘qS r   ©rE   ©r$   Úp©rd   r   r   r)   —   r'   z!_mask_shapely.<locals>.<listcomp>rl   )Úshapely.geometryr   rŠ   Úzipr#   Úarrayry   )r†   r‡   ra   rŠ   rŒ   r   )r   rd   r   r~   “   s    r~   c                 C   s&   | j dkrt| ||ƒS t| ||ƒS d S )Nr   )Úndimrf   r]   )r_   r`   ra   r   r   r   Úspatial_selectš   s    
r˜   c                    sp   zNddl m}m‰  ‡ fdd„t| |||ƒD ƒ}||ƒ‰t ‡fdd„|D ƒ¡W S  tyj   tdƒd ‚Y n0 d S )Nr   )rŠ   Úboxc                 3   s$   | ]\}}}}ˆ ||||ƒV  qd S r   r   )r$   r‚   r„   rƒ   r…   )r™   r   r   r&   £   r'   z&spatial_geom_select.<locals>.<genexpr>c                    s   g | ]}ˆ   |¡‘qS r   r   r‘   r“   r   r   r)   ¦   r'   z'spatial_geom_select.<locals>.<listcomp>úBLasso selection on geometry data requires shapely to be available.)r”   rŠ   r™   r•   r#   r–   r[   )Zx0valsZy0valsZx1valsZy1valsra   rŠ   Úboxesr   )r™   rd   r   Úspatial_geom_select    s    
ÿÿrœ   c                    sh   zFddl m‰  ‡ fdd„t| |ƒD ƒ}ˆ |ƒ‰t ‡fdd„|D ƒ¡W S  tyb   tdƒd ‚Y n0 d S )Nr   ©rŠ   c                 3   s$   | ]\}}ˆ t  ||g¡ƒV  qd S r   )r#   Zcolumn_stack©r$   rb   rc   r   r   r   r&   ®   r'   z&spatial_poly_select.<locals>.<genexpr>c                    s   g | ]}ˆ   |¡‘qS r   r   r‘   r“   r   r   r)   °   r'   z'spatial_poly_select.<locals>.<listcomp>rš   )r”   rŠ   r•   r#   r–   r[   )r_   r`   ra   r›   r   )rŠ   rd   r   Úspatial_poly_select«   s    ÿrŸ   c                    s0   |\‰ ‰‰‰t  ‡ ‡‡‡fdd„t| |ƒD ƒ¡S )Nc                    sH   g | ]@\}}ˆ t  |¡kˆt  |¡k@ ˆt  |¡k@ ˆt  |¡k@ ‘qS r   )r#   ZnanminZnanmaxrž   ©r‚   rƒ   r„   r…   r   r   r)   ·   s   þÿÿz)spatial_bounds_select.<locals>.<listcomp>)r#   r–   r•   )r_   r`   Úboundsr   r    r   Úspatial_bounds_selectµ   s    þr¢   c                   @   sj   e Zd ZdZdZeeefZdd„ Z	dd„ Z
dd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdS )ÚSelection2DExprzc
    Mixin class for Cartesian 2D elements to add basic support for
    SelectionExpr streams.
    r   c                 C   s(   ddl m} ddlm} |g ƒ|g ƒ S )Nr   ©Ú
Rectangles©ÚPath)Úgeomr¥   rZ   r§   )r   r¥   r§   r   r   r   r   Æ   s    zSelection2DExpr._empty_regionc           
      K   s¸   d\}}|d \}}}}d|v r€|d }t |tƒrP|}tt|ƒƒtt|ƒƒ }}|d }	t |	tƒr€|	}tt|ƒƒtt|ƒƒ }}||kr’|| }}||kr¤|| }}||f|||f|fS )N)NNr¡   Úx_selectionÚy_selection)r"   r:   ÚintÚround)
r   r   ÚxcatsÚycatsr‚   r„   rƒ   r…   ÚxselÚyselr   r   r   Ú_get_selectionË   s     



zSelection2DExpr._get_selectionc                    sÎ   t |ƒdkrB|d }t|ƒj|ddd}t|ƒ tt |¡ƒ¡}nˆtˆ j ¡ t	j
ƒ}‡ fdd„|D ƒ}t|d tjg|dd … ¢R Ž j|ddd}t|d tjg|dd … ¢R Ž j|dd}t|t	j|ƒ}|S )	Nr   r   FTr/   c                    s    g | ]}t ˆ j |¡tjƒ‘qS r   r*   r-   r   r   r   r)   ç   r'   z3Selection2DExpr._get_index_expr.<locals>.<listcomp>r1   )r4   r
   r5   r9   r:   r   r(   r+   r,   r#   r;   r<   r=   r>   )r   r@   ÚselrA   rB   rC   rD   rE   r   r   r   Ú_get_index_exprà   s     ÿ*zSelection2DExpr._get_index_exprc                 K   s  ddl m} | jf i |¤Ž\\}}}\}}	}
|p6||f}|
pB||	f}|j||j|i}| d¡}|r’| jjddgdjf i |¤Ž}|  ||¡}d }nr|r¦t	|ƒ 
|¡}nt	|ƒ|kt	|ƒ|k@ }|
rÒt	|ƒ 
|
¡}nt	|ƒ|kt	|ƒ|	k@ }||@ }|||||	fgƒ}|||fS ©Nr   r¤   r@   ru   Ú
dictionary©Údatatype)r¨   r¥   r±   rv   rG   r+   r3   Úselectr³   r
   r9   )r   ÚxdimÚydimr   r¥   r‚   rƒ   r­   r„   r…   r®   r¯   r°   Úbboxr@   ri   Úselection_exprÚregion_elementZxexprZyexprr   r   r   Ú_get_bounds_selectionï   s&     
z%Selection2DExpr._get_bounds_selectionc              	   K   sž   ddl m} |j|d d …df |j|d d …df i}tjt|t|ƒ|d}| d¡}|r|| | | ¡ }	|  ||	¡}
|
|d fS |||t	 
||d d… g¡gƒfS ©Nr   r¦   r   ©ra   r@   )rZ   r§   rv   r
   Úpiper˜   rG   r5   r³   r#   r‹   ©r   r¹   rº   ra   r   r§   r»   rC   r@   ri   r¼   r   r   r   Ú_get_lasso_selection  s    (

z$Selection2DExpr._get_lasso_selectionc                 C   sn   ddl m} t| |ƒr.| j ¡ d d… \}}n|  ¡ d d… \}}| j d¡j dd¡}|rf|| }}||fS )Nr   )ÚGraphr   ÚplotÚinvert_axesF)ZgraphsrÄ   r"   ÚnodesÚ
dimensionsÚoptsrG   r   )r   rÄ   r¹   rº   rÆ   r   r   r   Ú_get_selection_dims  s    

z#Selection2DExpr._get_selection_dimsc                 K   s   |  d¡o| j}|rd| _|S )Nr@   F©rG   r   ©r   r   Úskipr   r   r   Ú_skip  s    zSelection2DExpr._skipc           
      K   sD  ddl m} ddlm} | d¡d u rb| d¡d u rb| d¡d u rb| d¡sbd d |g ƒ|g ƒ fS | d¡}|  ¡ }| d¡d ur®|d ur®|  |d |¡\}}}|d |  ¡ fS | jf i |¤ŽrÂd S d|v r | j	|i |¤Ž\}}}	|||	d u ròd n
|	|g ƒ fS d|v r@| j
|i |¤Ž\}}}	|||	d u r2d n
|g ƒ|	 fS d S )	Nr   r¤   r¦   r¡   r©   ra   r?   r@   )r¨   r¥   rZ   r§   rG   rÊ   rF   r   rÎ   r¾   rÃ   )
r   r   r¥   r§   r@   ÚdimsrC   Ú_r»   Úregionr   r   r   rH   %  s*    ÿÿ


z4Selection2DExpr._get_selection_expr_for_stream_valuec           	      C   sh   | d u s|dkr|S |   d¡}|  d¡}| |j ||g¡¡}|   d¡}|  d¡}| ||g¡}|| S )NÚ	overwriter   r   )rG   r3   Z	interfacer‹   )	rI   rJ   rK   Zrect1Zrect2ZrectsZpoly1Zpoly2Zpolysr   r   r   rL   <  s    



zSelection2DExpr._merge_regionsN)rM   rN   rO   Ú__doc__rP   r	   r   r   rQ   r   r±   r³   r¾   rÃ   rÊ   rÎ   rH   rR   rL   r   r   r   r   r£   ¼   s   
r£   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSelectionGeomExprc                 C   sH   | j \}}}}| j d¡j dd¡}|r<||||f\}}}}||||fS )NrÅ   rÆ   F)r   rÉ   rG   r   )r   Úx0dimÚy0dimÚx1dimÚy1dimrÆ   r   r   r   rÊ   K  s
    z%SelectionGeomExpr._get_selection_dimsc                 K   s*  ddl m} | jf i |¤Ž\\}}}	\}
}}|	p6||f}|pB|
|f}|j||j||j||j|i}| d¡}|rž| jjddgdjf i |¤Ž}|  ||¡}d }n‚t	|ƒ|kt	|ƒ|k@ }t	|ƒ|
kt	|ƒ|k@ }t	|ƒ|kt	|ƒ|k@ }t	|ƒ|
kt	|ƒ|k@ }||@ |@ |@ }|||
||fgƒ}|||fS r´   )
r¨   r¥   r±   rv   rG   r+   r3   r¸   r³   r
   )r   rÕ   rÖ   r×   rØ   r   r¥   r‚   rƒ   r­   r„   r…   r®   r¯   r°   r»   r@   ri   r¼   r½   Zx0exprZy0exprZx1exprZy1exprr   r   r   r¾   R  s"     
z'SelectionGeomExpr._get_bounds_selectionc              
   K   sÎ   ddl m} |j|d d …df |j|d d …df |j|d d …df |j|d d …df i}tjt|t|ƒt|ƒt|ƒ|d}	| d¡}
|
r¬| |	 | ¡ }|  |
|¡}||d fS |	||t	 
||d d… g¡gƒfS r¿   )rZ   r§   rv   r
   rÁ   rœ   rG   r5   r³   r#   r‹   )r   rÕ   rÖ   r×   rØ   ra   r   r§   r»   rC   r@   ri   r¼   r   r   r   rÃ   h  s    $$þ"

z&SelectionGeomExpr._get_lasso_selectionN)rM   rN   rO   rÊ   r¾   rÃ   r   r   r   r   rÔ   I  s   rÔ   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSelectionPolyExprc                 K   s&   |  d¡o| jod|v}|r"d| _|S )z}
        Do not skip geometry selections until polygons support returning
        indexes on lasso based selections.
        r@   ra   FrË   rÌ   r   r   r   rÎ   z  s    zSelectionPolyExpr._skipc                 K   s°   ddl m} | jf i |¤Ž\\}}}\}}	}|j||f|j||	fi}
| d¡}tjt|t|ƒ||||	fd}|r˜| |j| dd }|  	||¡}||
d fS ||
|||||	fgƒfS )Nr   r¤   r@   )r¡   Fr   )
r¨   r¥   r±   rv   rG   r
   rÁ   r¢   r5   r³   )r   r¹   rº   r   r¥   r‚   rƒ   rÐ   r„   r…   r»   r@   rC   ri   r¼   r   r   r   r¾   „  s     

ÿ
z'SelectionPolyExpr._get_bounds_selectionc              	   K   s¢   ddl m} |j|d d …df |j|d d …df i}tjt|t|ƒ|d}| d¡}|r€| |j| dd }	|  ||	¡}
|
|d fS |||t	 
||d d… g¡gƒfS )Nr   r¦   r   rÀ   r@   Fr   )rZ   r§   rv   r
   rÁ   rŸ   rG   r5   r³   r#   r‹   rÂ   r   r   r   rÃ   ’  s    (

z&SelectionPolyExpr._get_lasso_selectionN)rM   rN   rO   rÎ   r¾   rÃ   r   r   r   r   rÙ   x  s   
rÙ   c                   @   s:   e Zd ZdZdZdZefZdd„ Zdd„ Z	e
dd	„ ƒZd
S )ÚSelection1DExprzi
    Mixin class for Cartesian 1D Chart elements to add basic support for
    SelectionExpr streams.
    r   Fc                 C   sB   | j  d¡j dd¡}|r | jr*|s0| jr0t}nt}td|ƒ iƒS )NrÅ   rÆ   Fr   )rÉ   rG   r   Ú_inverted_exprr   r   r   )r   rÆ   Ú	region_elr   r   r   r   ª  s
    zSelection1DExpr._empty_regionc                 K   s@  | j  d¡j dd¡}|r | jr*|s0| jr0t}nt}| dd ¡d u rhd|v rPd ntd|ƒ iƒ}d d |fS |d \}}}}||krŠ|| }}||krœ|| }}t|  ¡ ƒdkr¾|  ¡ d }	d }
n|  ¡ d d… \}	}
|rð||||f\}}}}d	}nd
}| jr&|
d ur
|
}	|| }}|rdndd }| |¡}|	j	||fi}|d urht| j
ƒdkrh||| j
d j	< | d¡}|r¨| jjddgdjf i |¤Ž}|  ||¡}d }nŽt|tƒrØ|	| j
d d… v rØt|	ƒ |¡}nLt|	ƒ|kt|	ƒ|k@ }t|tƒr$t| j
ƒdkr$|t| j
d ƒ |¡M }td|||ƒiƒ}|||fS )NrÅ   rÆ   Fr¡   r@   r   r   r   rª   r©   r   rŽ   Z
_selectionru   rµ   r¶   )rÉ   rG   r   rÛ   r   r   r   r4   rÈ   rv   r   r+   r3   r¸   r³   r"   r:   r
   r9   )r   r   rÆ   rÜ   rÑ   r‚   r„   rƒ   r…   r¹   rº   Z	cat_kwargZcatsr»   r@   ri   r¼   r½   r   r   r   rH   ²  sP    





 z4Selection1DExpr._get_selection_expr_for_stream_valuec                    s  ˆ d u s|dkr|S dd„ ˆ D ƒdd„ |D ƒ }t |ƒ}d }||krüt |ƒ}g }|D ]˜\}}t |¡rTt |¡srqTd}	t|ƒD ]Z\}
\}}||krª||krªt||ƒ}d}	n||krÈ||krÈt||ƒ}d}	|	r~||f||
< q~|	sT| ||f¡ qTt |ƒ}|}q<t‡ fdd„t|ƒD ƒƒS )NrÒ   c                 S   s   g | ]
}|j ‘qS r   )Údata)r$   Údr   r   r   r)   í  r'   z2Selection1DExpr._merge_regions.<locals>.<listcomp>FTc                    s&   g | ]\}\}}|ˆ j  ||¡f‘qS r   )Úlastr3   )r$   ÚiÚlÚu©rI   r   r   r)     r'   )r4   r   ÚisfiniteÚ	enumerater|   r{   Úappendr   )rI   rJ   rK   rÝ   ÚprevÚnewÚ
contiguousrá   râ   Úoverlaprà   ÚplZpur   rã   r   rL   é  s4    

zSelection1DExpr._merge_regionsN)rM   rN   rO   rÓ   rP   rÛ   r	   rQ   r   rH   rR   rL   r   r   r   r   rÚ   ž  s   7rÚ   )N)"rÓ   rp   Úimportlib.utilr   Únumpyr#   Zpandasrx   Úcorer   r   r   Zstreamsr   r   r	   Zutil.transformr
   Ú
annotationr   r   r   rf   r]   r}   r~   r˜   rœ   rŸ   r¢   r£   rÔ   rÙ   rÚ   r   r   r   r   Ú<module>   s,   .
9
 /&