a
    –½Df=0  ã                   @   sÖ   d dl Z d dl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 ddlmZmZmZmZmZmZ d	d
„ Zdd„ Zdd„ ZG dd„ deƒZdd„ Zddd„Zdd„ Zdd„ Zdd„ Z dS )é    Né   )ÚDataset)ÚBoundingBox)ÚPandasInterfaceÚdefault_datatype)Ú	Operation)ÚSlice)Úcartesian_productÚdatetime_typesÚ	is_cyclicÚis_nanÚ
one_to_oneÚsort_topologicallyc           	      C   s°   |   dd¡d } |  d¡}t dgt t |¡¡d d dgg¡}g }t | ¡}tt	|ƒd ƒD ]J}||d  }t
|| |du r„dn|d ƒ}|j| }t	|ƒr`| |¡ q`|S )z^
    Split a Path type containing a single NaN separated path into
    multiple subpaths.
    r   é   N)ÚsplitÚdimension_valuesÚnpÚconcatenateÚwhereÚisnanr   Ú	as_dframeÚrangeÚlenÚsliceZilocÚappend)	ÚpathÚvaluesÚsplitsZsubpathsÚdataÚiÚendÚslcÚsubpath© r#   úc/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/element/util.pyÚ
split_path   s    
(

r%   c                 C   s’  | \}}|\}}|j  ¡ \}}}	}
|j|j }}d| }d| }t|tƒr`t tt	|ƒƒ|j
¡}t|tƒr€t tt	|ƒƒ|j
¡}t|tƒr¾|jdu r˜|n
t||jƒ}|jdu r²|	n
t|	|jƒ}	t|tƒrü|jdu rÖ|n
t||jƒ}|jdu rð|
n
t|
|jƒ}
t||f|	|
ffd}t||ƒ}| |¡ ¡ \}}}	}
t|tƒsØt|ttfƒsT|g}t|ƒdkrv|tt|ƒƒ|  }g g  }}|D ]:}| ||¡\}}| ||d  ¡ | ||d  ¡ q„t |¡t |¡ }}	n¤t|tƒs|t|ttfƒsú|g}t|ƒdkr|tt|ƒƒ|  }g g  }}|D ]:}| ||¡\}}| ||d  ¡ | ||d  ¡ q*t |¡t |¡ }}
t||f|	|
ffdS )zµ
    Given a 2D selection consisting of slices/coordinates, a
    SheetCoordinateSystem and the shape of the array returns a new
    BoundingBox representing the sliced region.
    g      ð?N)Zpointsr   r   )ÚboundsZlbrtÚxdensityÚydensityÚ
isinstancer
   r   Ztimedelta64ÚintÚroundZ
_time_unitr   ÚstartÚmaxÚstopÚminr   r   Zcompute_boundsÚlistÚsetr   ÚfloatZclosest_cell_centerr   )ZslicesZscsÚshapeZxidxZyidxÚysÚxsÚlÚbÚrÚtr'   r(   ZxunitZyunitr&   r!   ZlsÚrsÚidxZxcÚ_ÚbsÚtsZycr#   r#   r$   Úcompute_slice_bounds)   sP    






r?   c                 C   s6   t | tjƒr| jn| }|D ]}t|ƒs|  S qtjS )z?
    Aggregation function to get the first non-zero value.
    )r)   ÚpdZSeriesr   r   r   Únan)Úxr   Úvr#   r#   r$   Ú	reduce_fn]   s
    
rD   c                   @   sH   e Zd ZdZejddgddZedd„ ƒZdd	„ Z	d
d„ Z
ddd„ZdS )Úcategorical_aggregate2da§  
    Generates a gridded Dataset of 2D aggregate arrays indexed by the
    first two dimensions of the passed Element, turning all remaining
    dimensions into value dimensions. The key dimensions of the
    gridded array are treated as categorical indices. Useful for data
    indexed by two independent categorical variables such as a table
    of population values indexed by country and year. Data that is
    indexed by continuous dimensions should be binned before
    aggregation. The aggregation will retain the global sorting order
    of both dimensions.

    >> table = Table([('USA', 2000, 282.2), ('UK', 2005, 58.89)],
                     kdims=['Country', 'Year'], vdims=['Population'])
    >> categorical_aggregate2d(table)
    Dataset({'Country': ['USA', 'UK'], 'Year': [2000, 2005],
             'Population': [[ 282.2 , np.nan], [np.nan,   58.89]]},
            kdims=['Country', 'Year'], vdims=['Population'])
    ZxarrayZgridzO
        The grid interface types to use when constructing the gridded Dataset.)ÚdefaultÚdocc                 C   sŒ  |j dddd… \}}| |d¡}| |d¡}|jjdvrFt |¡}|jjdvr`|t |¡fS |j|ttd 	¡ }i }d}|D ]œ}	|	 |d¡}
t
|
ƒdkr°|
d	 g||
d	 < n4tt
|
ƒd ƒD ]"}|
||d … \}}|g||< qÀ|r€|
jjd
v r
t |
¡d	k ¡ }q€t t |
¡|
¡}q€|s0t||ƒr<t |¡}n<t|ƒsxttjt|ƒŽ ƒ}t
|ƒt
|ƒkrn|nt |¡}t |¡t |¡fS )zi
        Get the coordinates of the 2D aggregate, maintaining the correct
        sorting order.
        T©ÚlabelNr   FZSUO)Zcontainer_typeZ
group_typer   r   )r   Úf)Ú
dimensionsr   ÚdtypeÚkindr   ÚsortÚgroupbyÚdictr   r   r   r   ÚdiffÚallZarray_equalr   r   r0   Ú	itertoolsÚchainr   Zasarray)ÚclsÚobjÚxdimÚydimÚxcoordsÚycoordsZgroupedZ	orderingsrN   ÚgroupÚvalsr   Úp1Úp2Zcoordsr#   r#   r$   Ú_get_coords   s:    
ÿ

 z#categorical_aggregate2d._get_coordsc                 C   s\  |   |¡\}}|jdd}| ¡ dd… }|dd… \}}t|ƒt|ƒf}t |¡}	||||i}
t||gdd\}}||||i}|D ]&}t |	¡}tj|dd…< |||j< q„t	}t
||j|j|gd}|jj||g|d}| ||g|¡}|sò|}t |¡}|j||gdd	 ¡  ¡ }| |¡}|D ]}| |¡ |¡|
|j< q$|j|
||g|| jjdS )
zr
        Generates a gridded Dataset from a column-based dataset and
        lists of xcoords and ycoords
        TrH   r   N)Úcopy)ÚkdimsÚvdimsÚdatatype©rc   F©rN   )r_   rK   r   r   Úprodr	   ÚemptyrA   Únamer   r   ra   rb   Ú	interfacer   Úreindexr   r   rO   ÚfirstÚreset_indexÚcloner   ÚreshapeÚprc   )ÚselfrV   rY   rZ   Z
dim_labelsrb   rW   rX   r3   ZnsamplesZ	grid_datar4   r5   r   Úvdimr   rL   Z
dense_dataZconcat_dataÚ	reindexedZaggÚdfr#   r#   r$   Ú_aggregate_dataset¦   s6    



ÿz*categorical_aggregate2d._aggregate_datasetc                 C   s¾   dd„ |j D ƒ}|j |¡j|dd ¡ }t|ƒt|ƒkr>dnd}|  |¡}tjj	||j
jd}| |¡}t|ƒ}tdd	„ |D ƒƒ}	|jD ]}
|||
j j |	¡jf7 }qŠ|j|| jj|d
S )Nc                 S   s   g | ]
}|j ‘qS r#   )rh   ©Ú.0Údr#   r#   r$   Ú
<listcomp>Ê   ó    zEcategorical_aggregate2d._aggregate_dataset_pandas.<locals>.<listcomp>Fre   Úuniquez
non-unique)Únamesc                 s   s   | ]}|j d  V  qdS )r   N)r3   ru   r#   r#   r$   Ú	<genexpr>Ñ   ry   zDcategorical_aggregate2d._aggregate_dataset_pandas.<locals>.<genexpr>)rc   rI   )ra   r   Z	set_indexrO   rk   r   r_   r@   Z
MultiIndexZfrom_productÚindexr{   rj   Útuplerb   rh   r   rn   ÚTrm   ro   rc   )rp   rV   Z
index_colsrs   rI   Úlevelsr}   rr   r   r3   rq   r#   r#   r$   Ú_aggregate_dataset_pandasÉ   s    


z1categorical_aggregate2d._aggregate_dataset_pandasNc                 C   sZ   t |tƒr|jjr|S |jdkr*tdƒ‚nt| ¡ ƒdk rBtdƒ‚t|dgd}|  |¡S )zÇ
        Generates a categorical 2D aggregate by inserting NaNs at all
        cross-product locations that do not already have a value assigned.
        Returns a 2D gridded Dataset object.
        r   z)Cannot aggregate more than two dimensionsé   zUMust have at two dimensions to aggregate overand one value dimension to aggregate on.Z	dataframerd   )	r)   r   ri   ZgriddedZndimsÚ
ValueErrorr   rK   r   )rp   rV   Úkeyr#   r#   r$   Ú_processÖ   s    

z categorical_aggregate2d._process)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚparamÚListrc   Úclassmethodr_   rt   r   r…   r#   r#   r#   r$   rE   h   s   
&#rE   c                 C   sL   t | ƒ}|sg g g fS tj| t |¡ d }t |¡}t |¡}||| fS )z7
    Lay out nodes on a circle and add node index.
    r   )r   r   ÚpiZarangeÚcosÚsin)ÚnodesÚNÚcircrB   Úyr#   r#   r$   Úcircular_layoutè   s    


r”   ©r   r   é2   c                 C   sÔ   t  dd|¡}| \}}|\}}|\}	}
|\}}d| d | dd| d  | |	  dd|  |d  |  |d |  }d| d | dd| d  | |
  dd|  |d  |  |d |  }t  ||g¡S )zd
    Compute quadratic bezier spline given start and end coordinate and
    two control points.
    r   r   r‚   r   )r   ZlinspaceZcolumn_stack)r,   r    Zc0Zc1ZstepsZsxZsyÚexZeyZcx0Zcy0Zcx1Zcy1r5   r4   r#   r#   r$   Úquadratic_bezierõ   s     &ÿ
ÿ&ÿ
ÿr˜   c                 C   sä   |   ¡ }d|j_| ¡ }| j  ¡ }| j\}}| jjdd… \}}}tj|||jg|jgd}|j|jd|jdid}tj|||jg|jgd}|j|jd|jd	id}| 	d¡j
dgd
d}g d¢}	t||	 j |jjdd¡ƒ}
|
S )zá
    Given a Graph element containing abstract edges compute edge
    segments directly connecting the source and target nodes. This
    operation depends on pandas and is a lot faster than the pure
    NumPy equivalent.
    Zgraph_edge_indexNr‚   ©Zleft_onZright_onÚsrc_xÚsrc_y©ÚcolumnsÚdst_xÚdst_yr   ©Úaxis)rš   r›   rž   rŸ   r   )Údframer}   rh   rl   r   ra   r@   ÚmergeÚrenameÚsort_valuesÚdropr0   r   rn   Úsize)ÚgraphÚedgesr   ÚsrcZtgtrB   r“   r;   rs   ÚcolsZedge_segmentsr#   r#   r$   Úconnect_edges_pd  s    

r¬   c           
      C   s,  |   ¡  ¡ }d|j_|jdddd ¡ }| j  ¡  ¡ }d|j_|jddgddd}| j\}}}| jjdd	… \}}}tj	|||jg|jgd
}	|	j
|jd|jdid}	tj	|	||jg|jgd
}	|	j
|jd|jdid}	tj	|	||jg|jgd
}	|	j
|jd|jdid}	|	 d¡jdgdd}	|	g d¢ S )zã
    Given a TriMesh element containing abstract edges compute edge
    segments directly connecting the source and target nodes. This
    operation depends on pandas and is a lot faster than the pure
    NumPy equivalent.
    Ztrimesh_edge_indexÚcolorÚignorer   )Úerrorsr¡   Z
node_indexÚzNr‚   r™   Úx0Úy0rœ   Úx1Úy1Úx2Úy2r    )r±   r²   r³   r´   rµ   r¶   )r¢   r`   r}   rh   r¦   rl   r   ra   r@   r£   r¤   r¥   )
Ztrimeshr©   r   Zv1Zv2Zv3rB   r“   r;   rs   r#   r#   r$   Úconnect_tri_edges_pd   s     r·   c                 C   sª   g }|   | j¡D ]”\}}| jdd…dd…|f }| jdd…dd…|f }t|ƒrXt|ƒs`tdƒ‚|  |jdd… ¡}|  |jdd… ¡}| t  |d |d g¡¡ q|S )zù
    Given a Graph element containing abstract edges compute edge
    segments directly connecting the source and target nodes.  This
    operation just uses internal HoloViews operations and will be a
    lot slower than the pandas equivalent.
    Nz+Could not find node positions for all edgesr   r   )Úarrayra   r   r   rƒ   r   r   )r¨   Úpathsr,   r    Zstart_dsZend_dsr#   r#   r$   Úconnect_edges:  s    rº   )r•   r•   r–   )!rS   Únumpyr   Zpandasr@   rŠ   Úcorer   Zcore.boundingregionr   Z	core.datar   r   Zcore.operationr   Zcore.sheetcoordsr   Z	core.utilr	   r
   r   r   r   r   r%   r?   rD   rE   r”   r˜   r¬   r·   rº   r#   r#   r#   r$   Ú<module>   s&    
4 
