
    e!                        d dl Zd dl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  G d	 d
ee
          Z G d dee          Z G d dee          Z G d deee
          Z G d de          Z G d deee
          ZdS )    N   Dataset)	Dimensionprocess_dimensions)Element	Element2D)get_param_valuesunique_iterator   )Selection1DExprSelection2DExprc                   p     e Zd ZdZdZdZd fd	Zed             Zd fd	Z	ddZ
d	 Zd fd
	ZddZ xZS )StatisticsElementa  
    StatisticsElement provides a baseclass for Element types that
    compute statistics based on the input data, usually a density.
    The value dimension of such elements are therefore usually virtual
    and not computed until the element is plotted.
    TFNc                    t          t                    r}j        j        dk    rm|                    t                               |p-                                d t          | j                           }t          fd|D                       |                    t          |g d                      t                      j        fi | |st          d          g| _        d S t          |          dk    r$t          t!          |           j         d          t%          d |          d         | _        d S )	N	dataframec              3   B   K   | ]}                     |          V  d S N)dimension_values).0ddatas     7lib/python3.11/site-packages/holoviews/element/stats.py	<genexpr>z-StatisticsElement.__init__.<locals>.<genexpr>   s1      AAa..q11AAAAAA    F)kdimsvdims_validate_vdimsDensityr   z expects at most one vdim.r   )
isinstancer   	interfacedatatypeupdater
   
dimensionslenr   tupledictsuper__init__r   r   
ValueErrortype__name__r   )selfr   r   r   params	__class__s    `   r   r)   zStatisticsElement.__init__   s3   tW%% 	B';66MM*400111@T__../?DJ/?@EAAAA5AAAAADdb%HHHIII((((( 	B#I../DJJJZZ!^^T

 3OOOPPP+D%88ADJJJr   c                     ddl m} | j        <t          t	          | j        |j        z                       } || dddg |          }|S t          | j        |          s || fddi| j        S | j        S )z?
        The Dataset that this object was created from
        r   r   N)datasetpipeline
transformsr   r"   r   F) r   _datasetlistr   r"   r    )r-   r   r"   r1   s       r   r1   zStatisticsElement.dataset'   s    
 	= ODM':J,JKKLLHgdD4D$&; ; ;GNDM733 	I74HHH$-HHH}r   c                     |                      |          | j        v}t                                          |||          S )a  Return the lower and upper bounds of values along dimension.

        Args:
            dimension: The dimension to compute the range on.
            data_range (bool): Compute range from data values
            dimension_range (bool): Include Dimension ranges
                Whether to include Dimension range and soft_range
                in range calculation

        Returns:
            Tuple containing the lower and upper bound
        )get_dimensionr   r(   range)r-   dim
data_rangedimension_rangeiskdimr/   s        r   r9   zStatisticsElement.range6   s9     ##C((
:ww}}S&/:::r   c                     |                      |d          }|| j        v r,t          j        t	          |           t          j                  S | j                            | |||          S )a  Return the values along the requested dimension.

        Args:
            dimension: The dimension to return values for
            expanded (bool, optional): Whether to expand values
                Whether to return the expanded values, behavior depends
                on the type of data:
                  * Columnar: If false returns unique values
                  * Geometry: If false returns scalar values per geometry
                  * Gridded: If false returns 1D coordinates
            flat (bool, optional): Whether to flatten array

        Returns:
            NumPy array of values along the requested dimension
        Tstrict)r8   r   npfullr%   nanr!   values)r-   r:   expandedflats       r   r   z"StatisticsElement.dimension_valuesF   s_        T 22$*73t99bf---~$$T3$???r   c                     |                      |          }|dS |j        |j        S || j        v rt          j        S | j                            | |          S )aB  Get the type of the requested dimension.

        Type is determined by Dimension.type attribute or common
        type of the dimension values, otherwise None.

        Args:
            dimension: Dimension to look up by name or by index

        Returns:
            Declared type of values along the dimension
        N)r8   r+   r   rA   float64r!   dimension_type)r-   r:   s     r   get_dimension_typez$StatisticsElement.get_dimension_type[   s]       %%;4X!8ODJ:~,,T3777r   c           	      >    |r fd|D             }n j         } fd|D             }|rRt          d                    t                     j        d                    d |D                                           t                                          |d          S )a  Convert dimension values to DataFrame.

        Returns a pandas dataframe of columns along each dimension,
        either completely flat or indexed by key dimensions.

        Args:
            dimensions: Dimensions to return as columns
            multi_index: Convert key dimensions to (multi-)index

        Returns:
            DataFrame of columns corresponding to each dimension
        c                 >    g | ]}                     |d           S Tr?   r8   r   r   r-   s     r   
<listcomp>z,StatisticsElement.dframe.<locals>.<listcomp>~   ,    QQQ$,,Qt,<<QQQr   c                 &    g | ]}|j         v |S  r   rO   s     r   rP   z,StatisticsElement.dframe.<locals>.<listcomp>        :::q!tz/////r   ^{} element does not hold data for value dimensions. Could not return data for {} dimension(s)., c                     g | ]	}|j         
S rS   namer   r   s     r   rP   z,StatisticsElement.dframe.<locals>.<listcomp>       SjSjSj_`TUTZSjSjSjr   F)r   r*   formatr+   r,   joinr(   dframe)r-   r$   multi_indexr   r/   s   `   r   r_   zStatisticsElement.dframep   s      	$QQQQjQQQJJJ::::J::: 	n --3VDJJ4GSjSjdiSjSjSjIkIk-l-ln n n ww~~j%000r   c           	      .    | j         }n fd|D             } fd|D             }|rRt          d                    t                     j        d                    d |D                                           t           fd|D                       S )a  Convert dimension values to a dictionary.

        Returns a dictionary of column arrays along each dimension
        of the element.

        Args:
            dimensions: Dimensions to return as columns

        Returns:
            Dictionary of arrays for each dimension
        Nc                 >    g | ]}                     |d           S rM   rN   rO   s     r   rP   z-StatisticsElement.columns.<locals>.<listcomp>   rQ   r   c                 &    g | ]}|j         v |S rS   rT   rO   s     r   rP   z-StatisticsElement.columns.<locals>.<listcomp>   rU   r   rV   rW   c                     g | ]	}|j         
S rS   rY   r[   s     r   rP   z-StatisticsElement.columns.<locals>.<listcomp>   r\   r   c                 H    g | ]}|j                             |          fS rS   )rZ   r   rO   s     r   rP   z-StatisticsElement.columns.<locals>.<listcomp>   s.    LLLAafd33A667LLLr   )r   r*   r]   r+   r,   r^   r'   )r-   r$   r   s   `  r   columnszStatisticsElement.columns   s     JJQQQQjQQQJ::::J::: 	n --3VDJJ4GSjSjdiSjSjSjIkIk-l-ln n n LLLLLLLMMMr   )NN)TT)NFr   )r,   
__module____qualname____doc___StatisticsElement__abstract_auto_indexable_1dr)   propertyr1   r9   r   rJ   r_   rf   __classcell__)r/   s   @r   r   r      s          J B B B B B B   X; ; ; ; ; ; @ @ @ @*8 8 8*1 1 1 1 1 10N N N N N N N Nr   r   c                       e Zd ZdZ ej        d d          Z ej         ed           ed          gd          Z	 ej         ed          gd	          Z
d
S )	Bivariatez
    Bivariate elements are containers for two dimensional data, which
    is to be visualized as a kernel density estimate. The data should
    be supplied in a tabular format of x- and y-columns.
    Tdefaultconstantxyr   r   rq   boundsr   r   r   Nr,   rg   rh   ri   paramStringgroupListr   r   r   rS   r   r   ro   ro      s          ELt<<<EEJ		#		#?$& & &E EJ		) 4 45eDDDEEEr   ro   c                       e Zd ZdZ ej        d d          Z ej         ed          gd          Z	 ej         ed          gd          Z
d	S )
Distributionz
    Distribution elements provides a representation for a
    one-dimensional distribution which can be visualized as a kernel
    density estimate. The data should be supplied in a tabular format
    and will use the first column.
    Trp   Valuer   r   rv   r   rx   Nry   rS   r   r   r   r      sv          EL$???EEJ		' 2 23FCCCEEJ		) 4 45fEEEEEEr   r   c                       e Zd ZdZ ej        d d          Z ej        g d          Z ej         e	d          gd          Z
dZdS )	
BoxWhiskerz
    BoxWhisker represent data as a distributions highlighting the
    median, mean and various percentiles. It may have a single value
    dimension and any number of key dimensions declaring the grouping
    of each violin.
    Trp   )r   Nrv   rt   r   N)r,   rg   rh   ri   rz   r{   r|   r}   r   r   r   _inverted_exprrS   r   r   r   r      sl          EL===EEJr)444EEJ		#/>>>ENNNr   r   c                   6    e Zd ZdZ ej        d d          ZdS )Violinz
    Violin elements represent data as 1D distributions visualized
    as a kernel-density estimate. It may have a single value dimension
    and any number of key dimensions declaring the grouping of each
    violin.
    Trp   N)r,   rg   rh   ri   rz   r{   r|   rS   r   r   r   r      s0          ELD999EEEr   r   c                       e Zd ZdZ ej        d d          Z ej         ed           ed          gd          Z	dS )	HexTilesa  
    HexTiles is a statistical element with a visual representation
    that renders a density map of the data values as a hexagonal grid.

    Before display the data is aggregated either by counting the values
    in each hexagonal bin or by computing aggregates.
    Trp   rs   rt   ru   rv   N)
r,   rg   rh   ri   rz   r{   r|   r}   r   r   rS   r   r   r   r      sb          ELd;;;EEJ		#		#?$& & &EEEr   r   )numpyrA   rz   	core.datar   core.dimensionr   r   core.elementr   r	   	core.utilr
   r   	selectionr   r   r   ro   r   r   r   r   rS   r   r   <module>r      s              : : : : : : : : - - - - - - - - 9 9 9 9 9 9 9 9 7 7 7 7 7 7 7 7RN RN RN RN RN RN RN RNjE E E E E!2 E E EF F F F F?$5 F F F    '9   ": : : : :Z : : :& & & & & & & & & &r   