a
    Y                     @   s   d Z 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mZmZmZmZmZ ddlmZm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G dd deZdS )z Properties for modeling Chart inputs, constraints, and dependencies.

selection spec:
    [['x'], ['x', 'y']]
    [{'x': categorical, 'y': numerical}]

    )absolute_importN)HasProps)PrimitiveProperty)Arraybokeh_integer_typesBoolEitherIntListString   )special_columnstitle_from_columnsc                       s4   e Zd ZdZ fddZdd Z fddZ  ZS )ColumnzRepresents column-oriented data.

    This property is used to provide a consistent interface for column-like data. The
    property both validates the data types set, and transforms all column-like data into
    `pd.Series` data.
    c                    s6   t t| |}t|tjp4t|tjp4t|tp4|S N)superr   _is_seq
isinstancepdSeriesZIndexlist)selfvalueZis_array	__class__ 2lib/python3.9/site-packages/bkcharts/properties.pyr      s    zColumn._is_seqc                 C   s
   t |S r   )r   r   r   r   r   r   r   _new_instance   s    zColumn._new_instancec                    sl   |d u rd S t |tjr |j}nt|j}tt| |}zt|W S  tyf   td| Y n0 d S )NzCould not transform %r)r   r   r   valuesr   r   	transform
ValueError)r   r   ZarrZtrans_arrayr   r   r   r    "   s    zColumn.transform)__name__
__module____qualname____doc__r   r   r    __classcell__r   r   r   r   r      s   r   c                       s    e Zd ZdZ fddZ  ZS )LogicalzcA boolean like data type.

    This property is valid for both python and numpy boolean types.
    c                    st   zt t| | W nZ tyn   t|tr6t|}t|tjrjt	|}t
|dkr^Y d S td| Y n0 d S )N   z5expected a Bool or array with 2 unique values, got %s)r   r'   validater!   r   r   npZarrayZndarrayuniquelen)r   r   r   r   r   r   r)   8   s    


zLogical.validate)r"   r#   r$   r%   r)   r&   r   r   r   r   r'   3   s   r'   c                       s6   e Zd ZdZd	 fdd	Z fddZdd Z  ZS )
ColumnLabelz"Specify a column by name or index.Nc                    s*   t tf}|| _tt| j|||d d S )N)defaulthelp)r   r	   columnsr   r-   __init__)r   r0   r.   r/   typesr   r   r   r1   K   s    zColumnLabel.__init__c                    sb   t t| | | jr^t|tv r>t| j|kr4dS tdn || jvr^|tvr^td| j dS )zKIf we are given a column list, make sure that the column provided is valid.NzNot a valid column selection.z7Column provided is not in the list of valid columns: %s)	r   r-   r)   r0   typer   r,   r!   r   r   r   r   r   r)   Q   s    
zColumnLabel.validatec                 C   s   dS )NzColumn Name or Column Stringr   r   r   r   r   __str___   s    zColumnLabel.__str__)NNN)r"   r#   r$   r%   r1   r)   r5   r&   r   r   r   r   r-   H   s   r-   c                       s   e Zd ZdZe ZeeeeddZee	ee	ddZ
eeeeddZeeeeddZee	ee	ddZ fddZdd Zedd	 Zd
d Zdd Zedd Zedd Zedd Zedd Zedd Z  ZS )	Dimensiona  Configures valid Chart column selections.

    A dimension is Chart property that is assigned one or more columns names or indices.
    Each column can match one or more column types, which are important to charts,
    because the type of column selection can greatly affect the behavior of generalized
    Charts.

    The Dimension also provides convenient utilities for accessing information
    about the current provided configuration at the global, non-grouped level.

    The dimension configuration does not require the data, but when the data is
    added using the `set_data` method, then validation can occur of the settings
    by using the `valid` and `invalid` types identified by the selection.
    N)r.   c                    s2   ||d< t t| jf i | t | _d | _d S )Nname)r   r6   r1   r   	DataFrame_data_chart_source)r   r7   Z
propertiesr   r   r   r1   |   s    
zDimension.__init__c              	   C   sN   t | j}g }|D ]6}| }z|| || W q tyF   Y q0 q|S )z,Returns all property types that are matched.)r   validr)   appendr!   )r   Zcol_dataZvalid_typesmatchesZ
valid_typeZpropr   r   r   get_valid_types   s    

zDimension.get_valid_typesc                 C   sN   | j js| jdu rtdS | jtt v r>t| j | j S | j | j S dS )zmThe data selected for the Dimension.

        Returns pd.Series(1) if data is empty or no selection.
        Nr   )r9   empty	selectionr   r   r   r   keysr4   r   r   r   data   s
    
zDimension.datac                 C   s   t | jjS r   )r,   rB   indexr4   r   r   r   __len__   s    zDimension.__len__c                 C   s.   || j  | _|| _|j| _t| jjj| _dS )zSet data property so that builders has access to configuration metadata.

        Args:
            data (`ChartDataSource`): the data source associated with the chart
        N)r7   r@   r:   Zdfr9   r   r0   r   )r   rB   r   r   r   set_data   s    zDimension.set_datac                 C   s.   t | jtjr| j S | jjdd S dS )z-The minimum of one to many column selections.r   ZaxisN)r   rB   r   r   minr4   r   r   r   rG      s    
zDimension.minc                 C   s.   t | jtjr| j S | jjdd S dS )z-The maximum of one to many column selections.r   rF   N)r   rB   r   r   maxr4   r   r   r   rH      s    
zDimension.maxc                 C   s,   t | jtjr | jj| jd  S | jjS d S )Nr   )r   rB   r   r8   Zdtypesr@   dtyper4   r   r   r   rI      s    zDimension.dtypec                 C   s    | j du rdS | j | jS dS )zHCheck the `ChartDataSource` to see if the selection is a derived column.NF)r:   Zis_computedr@   r4   r   r   r   computed   s    
zDimension.computedc                 C   s
   t | jS )z5A title formatted representation of selected columns.)r   r@   r4   r   r   r   selected_title   s    zDimension.selected_title)r"   r#   r$   r%   r   r7   r   r
   Z	alt_namesr-   r0   r   r;   Zinvalidr@   r1   r>   propertyrB   rD   rE   rG   rH   rI   rJ   rK   r&   r   r   r   r   r6   c   s.   




r6   c                       s    e Zd ZdZ fddZ  ZS )EitherColumnz'Allow providing option of column types.c                    s@   t t| ||}t|tr |S t|tjr4| S tdd S )NzFailed when comparing Columns)	r   rM   r=   r   boolr   r   allr!   )r   newoldZ
comparisonr   r   r   r=      s    
zEitherColumn.matches)r"   r#   r$   r%   r=   r&   r   r   r   r   rM      s   rM   )r%   Z
__future__r   Znumpyr*   Zpandasr   Zbokeh.core.has_propsr   Zbokeh.core.property.basesr   Zbokeh.core.propertiesr   r   r   r   r	   r
   r   Zutilsr   r   r   r'   r-   r6   rM   r   r   r   r   <module>   s   $ s