
    e=0                         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	 Zd
 Zd Z G d de          Zd ZddZd Zd Z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                    |                      dd          d         } |                     d          }t          j        dgt          j        t          j        |                    d         dz   dgg          }g }t          j        |           }t          t          |          dz
            D ][}||dz            }t          ||         |dn|dz
            }|j        |         }t          |          r|                    |           \|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sliceilocappend)	pathvaluessplitssubpathsdataiendslcsubpaths	            6lib/python3.11/site-packages/holoviews/element/util.py
split_pathr(      s    
 ::aAD""1%%F^aS"(28F+;+;"<"<Q"?"AD6JKKFH$T**D3v;;q=!! % %QqSkF1Is{ttA>>)C.w<< 	%OOG$$$O    c                 `   | \  }}|\  }}|j                                         \  }}}	}
|j        |j        }}d|z  }d|z  }t	          |t
                    r4t          j        t          t          |                    |j
                  }t	          |t
                    r4t          j        t          t          |                    |j
                  }t	          |t                    r<|j        |nt          ||j                  }|j        |	nt          |	|j                  }	t	          |t                    r<|j        |nt          ||j                  }|j        |
nt          |
|j                  }
t!          ||f|	|
ff          }t#          ||          }|                    |                                          \  }}}	}
t	          |t                    st	          |t&          t(          f          s|g}t+          |          dk    r"|t-          t+          |                    |z  z  }g g }}|D ]Q}|                    ||          \  }}|                    ||dz  z
             |                    ||dz  z              Rt          j        |          t          j        |          }	}nt	          |t                    st	          |t(          t&          f          s|g}t+          |          dk    r"|t-          t+          |                    |z  z  }g g }}|D ]Q}|                    ||          \  }}|                    ||dz  z
             |                    ||dz  z              Rt          j        |          t          j        |          }
}t!          ||f|	|
ff          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)pointsr   r   )boundslbrtxdensityydensity
isinstancer   r   timedelta64intround
_time_unitr   startmaxstopminr   r	   compute_boundslistsetr   floatclosest_cell_centerr   )slicesscsshapexidxyidxysxslbrtr.   r/   xunityunitr,   r%   lsrsidxxc_bstsycs                            r'   compute_slice_boundsrS   )   ss    JD$FB""JAq!Qs|hH[E[E!^$$ Bs5<<00#.AA!^$$ Bs5<<00#.AA$ :#AAQ
););"AAAty(9(9$ :#AAQ
););"AAAty(9(9!Q!Q 0111F 

C ##C((--//JAq!QdE"" &$s,,;dVdt99q==s4yy!1!1"!45HRB 	" 	"C++C33EBIIbqj!!!IIbqj!!!!vbzz26"::1e$$ 	&$d,,;dVdt99q==s4yy!1!1"!45HRB 	" 	"C++As33EArIIbqj!!!IIbqj!!!!vbzz26"::11v1v.////r)   c                     t          | t          j                  r| j        n| }|D ]}t	          |          s|c S t
          j        S )z?
    Aggregation function to get the first non-zero value.
    )r0   pdSeriesr   r   r   nan)xr   vs      r'   	reduce_fnrZ   ]   sR     $Ary118QXXqF  ayy 	HHH	6Mr)   c                   d    e Zd ZdZ ej        ddgd          Zed             Zd Z	d Z
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'])
    xarraygridzO
        The grid interface types to use when constructing the gridded Dataset.)defaultdocc                    |                     d          dd         \  }}|                    |d          }|                    |d          }|j        j        dvrt	          j        |          }|j        j        dvr|t	          j        |          fS |                    |t          t                    	                                }i }d}|D ]}	|	                    |d          }
t          |
          dk    r|
d	         g||
d	         <   n8t          t          |
          dz
            D ]}|
||dz            \  }}|g||<   |r`|
j        j        d
v r+t	          j        |
          d	k                                    }t	          j        t	          j        |
          |
          }|st          ||          rt	          j        |          }nmt!          |          s^t#          t%          j        t)          |                     }t          |          t          |          k    r|nt	          j        |          }t	          j        |          t	          j        |          fS )zi
        Get the coordinates of the 2D aggregate, maintaining the correct
        sorting order.
        TlabelNr   FSUO)container_type
group_typer   r   )r#   f)
dimensionsr   dtypekindr   sortgroupbydictr   r   r   r   diffallarray_equalr   r   r:   	itertoolschainr   asarray)clsobjxdimydimxcoordsycoordsgrouped	orderingsrk   groupvalsr#   p1p2coordss                  r'   _get_coordsz#categorical_aggregate2d._get_coords   sI    ^^$^//3
d&&tU33&&tU33=U**gg&&G=U**BGG,,,, ++d4)0  2 228&(( 		 	? 	?E))$66D4yyA~~&*1gY	$q'""s4yy{++ ) )A!!AaC%[FB%'DIbMM ?:?j00GDMM1,1133DD>"'$-->>D 	R:i11 	Rgg&&GG9%% 	R)/+=i+H+HIJJF #Fs7|| ; ;ffAQAQGz'""BJw$7$777r)   c                    |                      |          \  }}|                    d          }|                                dd         }|dd         \  }}t          |          t          |          f}t          j        |          }	||||i}
t          ||gd          \  }}||||i}|D ]1}t          j        |	          }t          j        |dd<   |||j        <   2t          }t          ||j        |j        |g          }|j                            ||g|          }|                    ||g|          }|s|}t!          j        |          }|                    ||gd	                                                                          }|                    |          }|D ]2}|                    |                              |          |
|j        <   3|                    |
||g|| j        j                  S )
zr
        Generates a gridded Dataset from a column-based dataset and
        lists of xcoords and ycoords
        Trb   r   N)copy)kdimsvdimsdatatyper   Frk   )r   rh   r   r   prodr
   emptyrW   namer   r   r   r   	interfacer   reindexr   r   rl   firstreset_indexcloner   reshapepr   )selfru   rx   ry   
dim_labelsr   rv   rw   r@   nsamples	grid_datarC   rD   r"   vdimr   ri   
dense_dataconcat_data	reindexedaggdfs                         r'   _aggregate_datasetz*categorical_aggregate2d._aggregate_dataset   s   
  ++C00^^$^//
  $^
dWs7||,75>>7D'2	"GW#5DAAABb$# 	% 	%DXh''FF111I$DOO T#)ugVVV
m//S0AE/RR''te<<	 	C&y11ZZt5Z117799EEGGoob!!  	M 	MD#&#7#7#=#=#E#Ee#L#LIdi  yy4,e"&&/  3 3 	3r)   c                    d |j         D             }|j                            |                              |d                                          }t          |          t          |          k    rdnd}|                     |          }t          j        	                    ||j
        j                  }|                    |          }t          |          }t          d |D                       }	|j        D ]0}
|||
j                 j                            |	          j        fz  }1|                    || j        j        |          S )	Nc                     g | ]	}|j         
S  )r   .0ds     r'   
<listcomp>zEcategorical_aggregate2d._aggregate_dataset_pandas.<locals>.<listcomp>   s    000af000r)   Fr   uniquez
non-unique)namesc              3   0   K   | ]}|j         d          V  dS )r   N)r@   r   s     r'   	<genexpr>zDcategorical_aggregate2d._aggregate_dataset_pandas.<locals>.<genexpr>   s(      //Qagaj//////r)   )r   rc   )r   r"   	set_indexrl   r   r   r   rU   
MultiIndexfrom_productindexr   r   tupler   r   r   r   Tr   r   r   )r   ru   
index_colsr   rc   levelsr   r   r"   r@   r   s              r'   _aggregate_dataset_pandasz1categorical_aggregate2d._aggregate_dataset_pandas   s,   00ci000
X
++33JU3KKQQSSGGs3xx//\!!#&&**6*HHJJu%%	V}}//$/////I 	D 	DDYty)088??ACCDDyyuyEEEr)   Nc                 2   t          |t                    r|j        j        r|S |j        dk    rt          d          t          |                                          dk     rt          d          t          |dg          }|                     |          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.	dataframer   )	r0   r   r   griddedndims
ValueErrorr   rh   r   )r   ru   keys      r'   _processz categorical_aggregate2d._process   s     c7## 	I(= 	IJY]]HIII!!""Q&& H I I I c[M222--c222r)   )N)__name__
__module____qualname____doc__paramListr   classmethodr   r   r   r   r   r)   r'   r\   r\   h   s         & uz8V"4 ;R S S SH $8 $8 [$8L!3 !3 !3FF F F3 3 3 3 3 3r)   r\   c                     t          |           }|sg g g fS t          j        |z  t          j        |          z  dz  }t          j        |          }t          j        |          }||| fS )z7
    Lay out nodes on a circle and add node index.
    r   )r   r   piarangecossin)nodesNcircrX   ys        r'   circular_layoutr      sf     	E

A B|5729Q<<!D
tA
tAq%=r)   r   r   2   c                 b   t          j        dd|          }| \  }}|\  }}|\  }	}
|\  }}d|z
  dz  |z  dd|z
  dz  z  |z  |	z  z   dd|z
  z  |dz  z  |z  z   |dz  |z  z   }d|z
  dz  |z  dd|z
  dz  z  |z  |
z  z   dd|z
  z  |dz  z  |z  z   |dz  |z  z   }t          j        ||g          S )zd
    Compute quadratic bezier spline given start and end coordinate and
    two control points.
    r   r   r   r   )r   linspacecolumn_stack)r5   r$   c0c1stepssxsyexeycx0cy0cx1cy1rD   rC   s                  r'   quadratic_bezierr      s   
 K1e$$EFBFBHCHCU7Q,r/A%!|,U236
6
QuW+eQh
s
"#%*AXb[1BU7Q,r/A%!|,U236
6
QuW+eQh
s
"#%*AXb[1B?B8$$$r)   c                    |                                  }d|j        _        |                                }| j                                         }| j        \  }}| j        j        dd         \  }}}t          j        |||j        g|j        g          }|                    |j        d|j        di          }t          j        |||j        g|j        g          }|                    |j        d|j        d	i          }|	                    d          
                    dg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.
    graph_edge_indexNr   left_onright_onsrc_xsrc_ycolumnsdst_xdst_yr   axis)r   r   r   r   r   )dframer   r   r   r   r   rU   mergerenamesort_valuesdropr:   r   r   size)graphedgesr   srctgtrX   r   rM   r   colsedge_segmentss              r'   connect_edges_pdr     sF    LLNNE)EKEK  E{HC!"1"%IAq#	%
chZ	H	H	HB	AFGQVW=	>	>B	"echZ38*	E	E	EB	AFGQVW=	>	>B	*	+	+	0	02D1EA	0	N	NB///DD001EEFFMr)   c                    |                                                                  }d|j        _        |                    ddd                                          }| j                                                                         }d|j        _        |                    ddgdd          }| j        \  }}}| j        j        dd	         \  }}}t          j	        |||j        g|j        g
          }	|	
                    |j        d|j        di          }	t          j	        |	||j        g|j        g
          }	|	
                    |j        d|j        di          }	t          j	        |	||j        g|j        g
          }	|	
                    |j        d|j        di          }	|	                    d                              dg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.
    trimesh_edge_indexcolorignorer   )errorsr   
node_indexzNr   r   x0y0r   x1y1x2y2r   )r   r   r   r   r  r  )r   r   r   r   r   r   r   r   rU   r   r   r   )
trimeshr   r   v1v2v3rX   r   rM   r   s
             r'   connect_tri_edges_pdr     s    NN!!##E+EKJJwxaJ88DDFFEM  ""''))E#EKJJ~hQJ??EJBB#BQB'IAq#	%	SXJ	G	G	GB	AFD!&$7	8	8B	"ebgY#(	D	D	DB	AFD!&$7	8	8B	"ebgY#(	D	D	DB	AFD!&$7	8	8B	,	-	-	2	24H3IPQ	2	R	RB22233r)   c                    g }|                      | j                  D ]\  }}| j        dddd|f         }| j        dddd|f         }t          |          rt          |          st	          d          |                     |j        dd                   }|                     |j        dd                   }|                    t          j         |d         |d         g                     |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   )arrayr   r   r   r   r   r   )r   pathsr5   r$   start_dsend_dss         r'   connect_edgesr  :  s     Ekk%+.. 3 3
s;qqq!!!U{+QQQ3Y'8}} 	LCKK 	LJKKKx~bqb122ll6<+,,RXuQxQ0112222Lr)   )r   r   r   )!rq   numpyr   pandasrU   r   corer   core.boundingregionr   	core.datar   r   core.operationr   core.sheetcoordsr	   	core.utilr
   r   r   r   r   r   r(   rS   rZ   r\   r   r   r   r  r  r   r)   r'   <module>r     s                      - - - - - - 9 9 9 9 9 9 9 9 & & & & & & $ $ $ $ $ $                 &10 10 10h  }3 }3 }3 }3 }3i }3 }3 }3@
 
 
% % % %"  44 4 44    r)   