ó
ÎYc           @@  sÃ  d  Z  d d l m Z d d l m Z d d l m 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 m Z d d	 l m Z d
 d l m Z d
 d l m Z d
 d l m Z d
 d l m Z d
 d l m  Z  d e f d „  ƒ  YZ! d e! f d „  ƒ  YZ" d e! f d „  ƒ  YZ# e	 j$ Z% d e! f d „  ƒ  YZ& d e! f d „  ƒ  YZ' d e! f d „  ƒ  YZ( d d e* d „ Z+ d d d „ Z, d d e- e- d „ Z. d S(   s   

i    (   t   absolute_import(   t   copy(   t   cycleN(   t   DashPattern(   t   HasProps(   t   Anyt   Boolt   Dictt   Eithert   Instancet   Listt   Overridet   String(   t   ColumnDataSourcei   (   t   DEFAULT_PALETTE(   t   ChartDataSource(   t   ColumnLabel(   t   marker_types(   t   Binst   AttrSpecc           B@  ss  e  Z d  Z e e ƒ Z e e d d ƒZ	 e
 d d ƒ Z e e e e ƒ d d ƒZ e d d d d ƒ Z e e e d d ƒZ e d d d d ƒ Z e d e d d ƒ Z e d e d d	 ƒ Z e e d d
 ƒZ d d d d d d „ Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z d d d „ Z  d „  Z! d „  Z" d d d „ Z# d „  Z$ d „  Z% e& d „  ƒ Z' RS(   sC  A container for assigning attributes to values and retrieving them as needed.

    A special function this provides is automatically handling cases where the provided
    iterator is too short compared to the distinct values provided.

    Once created as attr_spec, you can do attr_spec[data_label], where data_label must
    be a one dimensional tuple of values, representing the unique group in the data.

    See the :meth:`AttrSpec.setup` method for the primary way to provide an existing
    AttrSpec with data and column values and update all derived property values.
    t   defaultt   helps(   Name of the attribute the spec provides.sW  
        The label or list of column labels that correspond to the columns that will be
        used to find all distinct values (single column) or combination of values (
        multiple columns) to then assign a unique attribute to. If not enough unique
        attribute values are found, then the attribute values will be cycled.
        sæ   
        The default value for the attribute, which is used if no column is assigned to
        the attribute for plotting. If the default value is not provided, the first
        value in the `iterable` property is used.
        sù   
        Created by the attribute specification when `iterable` and `data` are
        available. The `attr_map` will include a mapping between the distinct value(s)
        found in `columns` and the attribute value that has been assigned.
        s„   
        The attribute specification calculates this list of distinct values that are
        found in `columns` of `data`.
        sÒ   
        A boolean flag to tell the attribute specification to sort `items`, when it is
        calculated. This affects which value of `iterable` is assigned to each distinct
        value in `items`.
        s¸   
        A boolean flag to tell the attribute specification how to sort `items` if the
        `sort` property is set to `True`. The default setting for `ascending` is `True`.
        sZ  
        If an attribute spec is binning data, so that we can map one value in the
        `iterable` to one value in `items`, then this attribute will contain an instance
        of the Bins stat. This is used to create unique labels for each bin, which is
        then used for `items` instead of the actual unique values in `columns`.
        c         K@  s˜  |  j  | ƒ | d <| d k	 r2 t | ƒ | d <n  | d k ro | d k	 ro t | ƒ } t t | ƒ ƒ | d <n | d k	 rˆ | | d <n  | d k	 r¡ | | d <n  | d k	 rº | | d <n  t t |  ƒ j |   |  j	 d k r|  j
 d k	 rt t t |  j
 ƒ ƒ ƒ |  _	 n  |  j d k	 rd|  j d k	 rd| d k rK|  j j ƒ  } n  |  j | d |  j ƒn  |  j d k	 r”|  j
 d k	 r”|  j ƒ  |  _ n  d S(   s›  Create a lazy evaluated attribute specification.

        Args:
            columns: a list of column labels
            df(:class:`~pandas.DataFrame`): the data source for the attribute spec.
            iterable: an iterable of distinct attribute values
            default: a value to use as the default attribute when no columns are passed
            items: the distinct values in columns. If items is provided as input,
                then the values provided are used instead of being calculated. This can
                be used to force a specific order for assignment.
            **properties: other properties to pass to parent :class:`HasProps`
        t   columnst   dataR   t   iterablet   itemsN(   t   _ensure_listt   NoneR   R   t   nextt   itert   superR   t   __init__R   R   R   R   t   to_dft   _generate_itemsR   t   _create_attr_mapt   attr_map(   t   selfR   t   dfR   R   R   t
   propertiest   default_iter(    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR   S   s*    !c         C@  s7   t  |  t ƒ r |  g St  |  t ƒ r/ t |  ƒ S|  Sd S(   sK   Always returns a list with the provided value. Returns the value if a list.N(   t
   isinstancet   strt   tuplet   list(   t   attr(    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR   €   s
    
c         C@  s   t  |  t ƒ s |  f S|  Sd S(   sC   Return tuple with the provided value. Returns the value if a tuple.N(   R(   R*   (   R,   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   _ensure_tupleŠ   s    c         C@  s   t  |  j ƒ  ƒ |  _ d S(   s;   Stores the first value of iterable into `default` property.N(   R   t   _setup_iterableR   (   R$   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   _setup_default’   s    c         C@  s   t  t |  j ƒ ƒ S(   s<   Default behavior is to copy and cycle the provided iterable.(   R   R   R   (   R$   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR.   –   s    c         C@  sœ   |  j  rZ y | j d | d |  j ƒ } WqZ t k
 rV | j  d | d |  j ƒ } qZ Xn  | | j ƒ  } g  | j d t ƒ D] } t | ƒ ^ q} |  _ d S(   s6   Produce list of unique tuples that identify each item.t   byt	   ascendingR   t   indexN(	   t   sortt   sort_valuesR1   t   AttributeErrort   drop_duplicatest
   to_recordst   FalseR*   R   (   R$   R%   R   R   t   x(    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR!   š   s    	"c         @  sQ   | d k	 r+ | d k	 r+ |  j | | ƒ n  |  j ƒ  ‰  ‡  f d †  |  j ƒ  Dƒ S(   s;   Creates map between unique values and available attributes.c         @  s   i  |  ] } t  ˆ  ƒ | “ q S(    (   R   (   t   .0t   item(   R   (    s2   lib/python2.7/site-packages/bkcharts/attributes.pys
   <dictcomp>°   s   	 N(   R   R!   R.   t   _item_tuples(   R$   R%   R   (    (   R   s2   lib/python2.7/site-packages/bkcharts/attributes.pyR"   ¨   s    c         C@  s#   g  |  j  D] } |  j | ƒ ^ q
 S(   N(   R   R-   (   R$   R;   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR<   ²   s    c         C@  s]   |  j  | ƒ } t g  | D] } | |  j j k ^ q ƒ rF | |  _ n | |  _ |  j ƒ  d S(   s=   Set columns property and update derived properties as needed.N(   R   t   allR   t   column_namesR   R   R/   (   R$   R   t   col(    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   set_columnsµ   s
    +	c         C@  s‰   | d k	 r | |  _ n  | d k	 rC |  j d k	 rC |  j | ƒ n  |  j d k	 r… |  j d k	 r… |  j |  j j ƒ  |  j ƒ |  _ n  d S(   s5   Set the data and update derived properties as needed.N(   R   R   R@   R   R"   R    R#   (   R$   R   R   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   setupÁ   s    c         C@  s   |  j  d | d |  j ƒ d  S(   NR   R   (   RA   R   (   R$   R   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   update_dataÌ   s    c         C@  sO   |  j  s |  j S|  j | ƒ |  j  j ƒ  k r; |  j ƒ  n  |  j  |  j | ƒ S(   s=   Lookup the attribute to use for the given unique group label.(   R#   R   R-   t   keysRA   (   R$   R;   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   __getitem__Ï   s
    	c         C@  sZ   |  j  s t j ƒ  St j j |  j ƒ  d |  j ƒ} t j t |  j  j ƒ  ƒ d | ƒSd  S(   Nt   namesR2   (	   R#   t   pdt   Seriest
   MultiIndext   from_tuplesR<   R   R+   t   values(   R$   R2   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   seriesÛ   s    	
!N((   t   __name__t
   __module__t   __doc__R	   R   R   R
   R   R   R   R   t   attrnameR   R   R   R   R   R#   R   R   t   TrueR3   R1   R   t   binsR   t   staticmethodR   R-   R/   R.   R!   R"   R<   R@   RA   RB   RD   t   propertyRK   (    (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR      s@   								,
			
				t	   ColorAttrc           B@  sV   e  Z d  Z e d d ƒ Z e d e ƒ Z e d e ƒ Z	 d „  Z
 d „  Z d „  Z RS(   s   An attribute specification for mapping unique data values to colors.

    .. note::
        Should be expanded to support more complex coloring options.
    R   t   colorc         K@  sE   | j  d d  ƒ } | d  k	 r+ | | d <n  t t |  ƒ j |   d  S(   Nt   paletteR   (   t   popR   R   RT   R   (   R$   t   kwargsR   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR   î   s    c      	   C@  sà   |  j  s% t t |  ƒ j | | ƒ n· t | ƒ d k rÐ t j | | d ƒ rÐ t d t | ƒ d | d d t |  j	 ƒ d t
 ƒ |  _ |  j r§ |  j j d |  j ƒ n  g  |  j D] } | j d ^ q± |  _ n t d ƒ ‚ d	 S(
   s6   Produce list of unique tuples that identify each item.i   i    t   sourcet   columnRQ   t	   aggregateR1   sd   Binned colors can only be created for one column of                                  numerical data.N(   t   binR   RT   R!   t   lenR   t	   is_numberR   R   R   R8   RQ   R3   R1   t   labelR   t
   ValueError(   R$   R%   R   R\   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR!   ô   s    	)	)c         C@  s¦   |  j  d } | j | | j | d <xE |  j D]: } | j d | j j | j | d j | j ƒ | f <q/ Wt j | j | d t	 |  j
 ƒ d |  j ƒ| j | <d  S(   Ni    t   _valuest
   categoriest   ordered(   R   t   _dataRQ   R_   t   ixt   isinRJ   RF   t   CategoricalR+   R   R3   (   R$   R   R?   R\   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   add_bin_labels  s    8"(   RL   RM   RN   R   RO   R   R   R   R8   R\   R   R!   Rh   (    (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyRT   ä   s   		t
   MarkerAttrc           B@  sA   e  Z d  Z e d d ƒ Z e d e e j ƒ  ƒ ƒ Z d „  Z	 RS(   sE   An attribute specification for mapping unique data values to markers.R   t   markerc         K@  sE   | j  d d  ƒ } | d  k	 r+ | | d <n  t t |  ƒ j |   d  S(   Nt   markersR   (   RW   R   R   Ri   R   (   R$   RX   R   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR     s    (
   RL   RM   RN   R   RO   R+   R   RC   R   R   (    (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyRi     s   t   DashAttrc           B@  s5   e  Z d  Z e d d ƒ Z e d e ƒ Z d „  Z RS(   sI   An attribute specification for mapping unique data values to line dashes.R   t   dashc         K@  sE   | j  d d  ƒ } | d  k	 r+ | | d <n  t t |  ƒ j |   d  S(   NRm   R   (   RW   R   R   Rl   R   (   R$   RX   R   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR   )  s    (   RL   RM   RN   R   RO   t   dashesR   R   (    (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyRl   $  s   t   IdAttrc           B@  s&   e  Z d  Z e d d ƒ Z d „  Z RS(   sI   An attribute specification for mapping unique data values to line dashes.R   t   idc         C@  s   t  t d t |  j ƒ ƒ ƒ S(   Ni    (   R   t   rangeR]   R   (   R$   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR.   4  s    (   RL   RM   RN   R   RO   R.   (    (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyRo   0  s   t   CatAttrc           B@  s8   e  Z d  Z e d d ƒ Z d „  Z d „  Z d „  Z RS(   s  An attribute specification for mapping unique data values to labels.

    .. note::
        this is a special attribute specification, which is used for defining which
        labels are used for one aspect of a chart (grouping) vs another (stacking or
        legend)
    R   t   nestc         K@  s   t  t |  ƒ j |   d  S(   N(   R   Rr   R   (   R$   RX   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR   B  s    c         C@  s   t  |  j ƒ S(   N(   R   R   (   R$   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyR.   E  s    c         C@  s@   |  j  d k	 r8 g  |  j  D] } | j | ƒ ^ q } | Sg  Sd S(   s3   Provides a list of levels the attribute represents.N(   R   R   R2   (   R$   R   R?   t   levels(    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt
   get_levelsH  s    %(   RL   RM   RN   R   RO   R   R.   Ru   (    (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyRr   8  s
   		c         K@  s7   | d k	 r | | d <n  |  | d <| | d <t |   S(   sœ  Produces a ColorAttr specification for coloring groups of data based on columns.

    Args:
        columns (str or list(str), optional): a column or list of columns for coloring
        palette (list(str), optional): a list of colors to use for assigning to unique
            values in `columns`.
        **kwargs: any keyword, arg supported by :class:`AttrSpec`

    Returns:
        a `ColorAttr` object
    RV   R   R\   N(   R   RT   (   R   RV   R\   RX   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyRU   Y  s
    

c         K@  s-   | d k	 r | | d <n  |  | d <t |   S(   sH   Specifies detailed configuration for a marker attribute.

    Args:
        columns (list or str):
        markers (list(str) or str): a custom list of markers. Must exist within
            :data:`marker_types`.
        **kwargs: any keyword, arg supported by :class:`AttrSpec`

    Returns:
        a `MarkerAttr` object
    Rk   R   N(   R   Ri   (   R   Rk   RX   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyRj   m  s    
c         K@  sA   | d k	 r | | d <n  |  | d <| | d <| | d <t |   S(   s   Specifies detailed configuration for a chart attribute that uses categoricals.

    Args:
        columns (list or str): the columns used to generate the categorical variable
        cats (list, optional): overrides the values derived from columns
        sort (bool, optional): whether to sort the categorical values (default=True)
        ascending (bool, optional): whether to sort the categorical values (default=True)
        **kwargs: any keyword, arg supported by :class:`AttrSpec`

    Returns:
        a `CatAttr` object
    t   catsR   R3   R1   N(   R   Rr   (   R   Rv   R3   R1   RX   (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   cat  s    


(/   RN   t
   __future__R    R   t	   itertoolsR   t   pandasRF   t   bokeh.core.enumsR   t   bokeh.core.has_propsR   t   bokeh.core.propertiesR   R   R   R   R	   R
   R   R   t   bokeh.models.sourcesR   t    R   t   data_sourceR   R&   R   t   utilsR   t   statsR   R   RT   Ri   Ra   Rn   Rl   Ro   Rr   R   R8   RU   Rj   RP   Rw   (    (    (    s2   lib/python2.7/site-packages/bkcharts/attributes.pyt   <module>   s.   :Î1	!