ó
ÎYc           @@  sö  d  Z  d d l m Z d d l m Z d d l m 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 d d
 l m Z m Z m Z m Z m 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" m# Z# d g Z$ e% e& e	 j' e j( g Z) e* e j+ g Z, d d g Z- d g d g d d g g Z. d e f d „  ƒ  YZ/ d e/ f d „  ƒ  YZ0 d e/ f d „  ƒ  YZ1 d e f d „  ƒ  YZ2 d e3 f d „  ƒ  YZ4 d „  Z5 d e3 f d „  ƒ  YZ6 d S(    sS    The classes and functionality used to transform data inputs to consistent
types.

i    (   t   absolute_import(   t   copy(   t   chain(   t
   itemgetterN(   t	   iteritems(   t   zip(   t   HasProps(   t   bokeh_integer_typest   Datetimet   Floatt   Listt   String(   t   ColumnDataSourcei   (   t   Columnt   ColumnLabel(   t   Binst   Stat(   t   collect_attribute_columnst   gen_column_namest   special_columnst   _charts_onest   xt   yt   ColumnAssignerc           B@  sJ   e  Z d  Z e e d d ƒZ e e d d ƒZ d d „ Z d d „ Z	 RS(   s  Defines behavior for assigning columns to dimensions.

    This class is used to collect assignments between columns and :class:`Builder`
    dimensions when none are provided. The :class:`ChartDataSource` receives a
    ColumnAssigner from each :class:`Builder`, which can implement custom behavior.

    Each subclass must implement the :meth:`get_assignment` method, which returns
    a `dict` mapping between each dimension in `dims` and one or more column names,
    or `None` if no assignment is made for the associated dimension.

    t   helpsã   
        The list of dimension names that are associated with the :class:`Builder`. The
        ColumnAssigner should return a dict with each dimension as a key when the
        :meth:`get_assignment` method is called.
        sé   
        This list of attribute names that are associated with the :class:`Builder`. These
        can be used to alter which dimensions are assigned which columns, versus which
        attributes are assigned which columns.
        c         K@  s2   | d k	 r | |  _ n  t t |  ƒ j |   d S(   sã   Create the assigner.

        Args:
            df (:class:`pandas.DataFrame`, optional): the data source to use for
                assigning columns from
            **properties: any attribute of the ColumnAssigner

        N(   t   Nonet   _dft   superR   t   __init__(   t   selft   dft
   properties(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR   6   s    	c         C@  s   t  d ƒ ‚ d  S(   Ns3   You must return map between each dim and selection.(   t   NotImplementedError(   R   t
   selections(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   get_assignmentC   s    N(
   t   __name__t
   __module__t   __doc__R
   R   t   dimst   attrsR   R   R"   (    (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR      s   				t   OrderedAssignerc           B@  s   e  Z d  Z d d „ Z RS(   s’   Assigns one column for each dimension that is not an attribute, in order.

    This is the default column assigner for the :class:`Builder`.

    c         C@  s€   | d k s* t t | j ƒ  ƒ ƒ d k rx g  |  j D] } | |  j k r4 | ^ q4 } d „  t | |  j j j	 ƒ  ƒ Dƒ S| Sd S(   sE   Get a mapping between dimension and selection when none are provided.i    c         S@  s   i  |  ] \ } } | | “ q S(    (    (   t   .0t   dimt   sel(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pys
   <dictcomp>R   s   	 N(
   R   t   lent   listt   keysR&   R'   R   R   t   columnst   tolist(   R   R!   R*   R&   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR"   N   s
    *+N(   R#   R$   R%   R   R"   (    (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR(   G   s   t   NumericalColumnsAssignerc           B@  s   e  Z d  Z d d „ Z RS(   s1   Assigns all numerical columns to the y dimension.c         C@  sè   t  | t ƒ r0 | j d ƒ } | j d ƒ } n d  } d  } i  } |  j j ƒ  } | j j ƒ  } | d  k	 r¦ | d  k r¦ g  | D] } | t | ƒ k r | ^ q } n* | d  k rÐ d } | d  k rÐ | } qÐ n  | | d <| | d <| S(   NR   R   t   index(	   t
   isinstancet   dictt   getR   R   t   _get_numeric_dataR/   R0   R-   (   R   R!   R   R   R   t   num_colst   col(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR"   [   s"    .

N(   R#   R$   R%   R   R"   (    (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR1   X   s   t   DataOperatorc           B@  s;   e  Z d  Z e e ƒ  d d d d ƒZ d „  Z d „  Z RS(   sA   An operation that transforms data before it is used for plotting.t   defaultR   s1   
        List of columns to perform operation on.c         C@  s   t  d ƒ ‚ d  S(   Ns3   Each data operator must implement the apply method.(   R    (   R   t   data(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   applyz   s    c         C@  s&   d j  |  j ƒ } d |  j j | f S(   Ns   , s   %s(%s)(   t   joinR/   t	   __class__R#   (   R   t   col_str(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   __repr__}   s    N(	   R#   R$   R%   R
   R   R   R/   R<   R@   (    (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR9   u   s
   		t	   DataGroupc           B@  sb   e  Z d  Z d „  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z	 e d „  ƒ Z
 d „  Z RS(	   sÝ   Contains subset of data and metadata about it.

    The DataGroup contains a map from the labels of each attribute
    associated with an :class:`AttrSpec` to the value of the attribute assigned to the
    DataGroup.
    c         C@  s   | |  _  | |  _ | |  _ d S(   s  Create a DataGroup for the data, with a label and associated attributes.

        Args:
            label (str): the label for the group based on unique values of each column
            data (:class:`pandas.DataFrame`): the subset of data associated with the group
            attr_specs dict(str, :class:`AttrSpec`): mapping between attribute name and
                the associated :class:`AttrSpec`.

        N(   t   labelR;   t
   attr_specs(   R   RB   R;   RC   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR   Š   s    
		c         C@  s~   t  | t ƒ r |  j | St  | t ƒ rJ t | ƒ d k rJ |  j | d St  | t ƒ rv t | ƒ d k rv |  j | Sd Sd S(   sÐ   Get the data associated with the selection of columns.

        Args:
            selection (List(Str) or Str): the column or columns selected

        Returns:
            :class:`pandas.DataFrame`

        i   i    N(   R3   t   strR;   R-   R,   R   (   R   t	   selection(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt
   get_values˜   s    
!!c         C@  s   t  |  j ƒ S(   s>   The :class:`ColumnDataSource` representation of the DataFrame.(   R   R;   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   source«   s    c         C@  s   |  j  | S(   sC   Get the value of the :class:`AttrSpec` associated with `spec_name`.(   RC   (   R   t	   spec_name(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   __getitem__°   s    c         C@  s   d t  |  j ƒ |  j f S(   Ns    <DataGroup(%s) - attributes: %s>(   RD   RB   RC   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR@   ´   s    c         C@  s   t  |  j j ƒ S(   N(   R,   R;   R2   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   __len__·   s    c         C@  s   t  |  j j ƒ  ƒ S(   N(   R-   RC   R.   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt
   attributesº   s    c         C@  s_   i  } |  j  d  k	 rA | j |  j  ƒ t |  j  j ƒ  ƒ | d <n
 d  | d <| j |  j ƒ | S(   Nt   chart_index(   RB   R   t   updatet   tuplet   itemsRC   (   R   t   row(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   to_dict¾   s    
(   R#   R$   R%   R   RF   t   propertyRG   RI   R@   RJ   RK   RQ   (    (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRA   ‚   s   					c         k@  sÕ  t  |   } | r†x¼|  j | d t ƒD]W\ } } i  } i  } x"t | ƒ D]\ } } | j d k	 rMt g  | j D] }	 | j |	 ƒ ^ qu ƒ }
 t | t ƒ rÉ t	 |
 Œ  | ƒ } t	 |
 Œ  | ƒ } n | } | d } t | t ƒ sô | f } n  t | t
 ƒ r t | t ƒ r | g } n  x0 t | | ƒ D] \ }	 } | | |	 <q0Wn d } | | | | <qM Wt d | d | d | ƒ Vq( WnK i  } x( t | ƒ D] \ } } | d | | <q™Wt d d d |  d | ƒ Vd S(   s‘  Convenience iterator around pandas groupby and attribute specs.

    Args:
        df (:class:`~pandas.DataFrame`): The entire data source being
            used for the Chart.
        **specs: Name, :class:`AttrSpec` pairing, used to identify the lowest
            level where the data is grouped.

    Yields:
        :class:`DataGroup`: each unique group of data to be used to produce glyphs

    t   sorti    RB   R;   RC   N(   R   t   groupbyt   FalseR   R/   R   RN   R2   R3   R   R-   R   RA   (   R   t   specst	   spec_colst   nameR;   R'   t   group_labelRH   t   specR8   t   name_idxRB   t   colst   value(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRT   Ê   s4    "+
 !t   ChartDataSourcec           B@  s   e  Z d  Z d  d  d  e d  d „ Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d  d d	 d
 „ Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d  d „ ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z  e d „  ƒ Z! e d „  ƒ Z" e d „  ƒ Z# e d „  ƒ Z$ RS(!   sÈ  Validates, normalizes, groups, and assigns Chart attributes to groups.

    Supported inputs are:

    - **Array-like**: list, tuple, :class:`numpy.ndarray`, :class:`pandas.Series`
    - **Table-like**:
        - records: list(dict)
        - columns: dict(list), :class:`pandas.DataFrame`, or blaze resource

    Converts inputs that could be treated as table-like data to pandas DataFrame,
    which is used for assigning attributes to data groups.

    c         K@  sô   | d k r t } n  | d k r* t } n  | j d d ƒ |  _ | pH g  |  _ | j d t ƒ |  _ | |  _	 g  |  _
 | |  _ | d |  j d t |  j	 ƒ d |  j ƒ |  _ |  j | |  |  _ |  j ƒ  |  j ƒ  |  j | ƒ |  _ |  j ƒ  d S(   s5  Create a :class:`ChartDataSource`.

        Args:
            df (:class:`pandas.DataFrame`): the original data source for the chart
            dims (List(Str), optional): list of valid dimensions for the chart.
            required_dims (List(List(Str)), optional): list of list of valid dimensional
                selections for the chart.
            selections (Dict(String, List(Column)), optional): mapping between a
                dimension and the column name(s) associated with it. This represents what
                the user selected for the current chart.
            column_assigner (:class:`ColumnAssigner`, optional): a reference to a
                ColumnAssigner class, which is used to collect dimension column
                assignment when keyword arguments aren't provided. The default value is
                :class:`OrderedAssigner`, which assumes you want to assign each column
                or array to each dimension of the chart in order that they are received.
            attrs (list(str)): list of attribute names the chart uses

        t
   input_typet   deepR   R&   R'   N(   R   t   DEFAULT_DIMSt   DEFAULT_REQ_DIMSt   popR_   R'   R   RU   t   _datat   _dimst
   operationst   _required_dimsR-   t   column_assignert   get_selectionst   _selectionst   setup_derived_columnst   apply_operationst   collect_metadatat   metat   _validate_selections(   R   R   R&   t   required_dimsR!   Rh   R'   t   kwargs(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR     s&    						

c         @  s   ‡  f d †  t  ˆ  j ƒ Dƒ S(   Nc         @  s.   i  |  ]$ \ } } | ˆ  j  k r | | “ q S(    (   R'   (   R)   R*   t   val(   R   (    s3   lib/python2.7/site-packages/bkcharts/data_source.pys
   <dictcomp>I  s   	 (   R   Rj   (   R   (    (   R   s3   lib/python2.7/site-packages/bkcharts/data_source.pyRC   G  s    c         K@  s  i  } x< |  j  D]1 } | j | d ƒ } | d k	 r | | | <q q Wt | j ƒ  ƒ d k r½ | d k r{ |  j j ƒ  } qÒ t | t ƒ r® t | j ƒ  ƒ d k rº | } qº qÒ t	 d ƒ ‚ n |  j j d | ƒ } x* |  j  D] } | | k rÜ d | | <qÜ qÜ W| S(   s  Maps chart dimensions to selections and checks input requirements.

        Returns:
            mapping between each dimension and the selected columns. If no selection is
            made for a dimension, then the dimension will be associated with `None`.

        i    sf   selections input must be provided as:                                  dict(dimension: column) or NoneR!   N(
   Re   Rc   R   R,   R.   Rh   R"   R3   R4   t
   ValueError(   R   R!   Rq   t
   select_mapR*   t
   dim_select(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRi   K  s$    c         C@  sÙ   |  j  j ƒ  } xº t |  j  ƒ D]© \ } } t | t ƒ r\ | j |  ƒ |  _ | j | | <n  t | t ƒ r¸ t | t	 ƒ rœ | j |  ƒ |  _ | j
 | | <q¸ t d | j | f ƒ ‚ n  |  j j | ƒ q W| |  _  d S(   s   Applies each data operation.s)   Stat input of %s for %s is not supported.N(   Rj   R   R   R3   R9   R<   Rd   RX   R   R   t   centers_columnt	   TypeErrorR>   Rf   t   append(   R   R!   R*   t   select(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRl   u  s    c         C@  s~   xw |  j  D]l } |  | } | d k	 r
 t | t ƒ r
 | t k r
 | |  j j j ƒ  k r
 t | |  j ƒ |  j | <q
 q
 Wd S(   sE   Attempt to add special case columns to the DataFrame for the builder.N(	   Re   R   R3   RD   R   R   R/   R0   Rd   (   R   R*   t   dim_selection(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRk   Œ  s    
c         C@  s"   | |  j  k r |  j  | Sd Sd S(   sê   Get the columns selected for the given dimension name.

        e.g. dim='x'

        Returns:
            the columns selected as a str or list(str). If the dimension is not in
            `_selections`, `None` is returned.

        N(   Rj   R   (   R   R*   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRI   –  s    
c         C@  s   | |  j  | <|  j ƒ  d  S(   N(   Rj   Rk   (   R   R*   R]   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   __setitem__¥  s    t   variableR]   c   	      C@  sF  x?|  j  D]4} |  j | } t | t ƒ r8 | j } n | } | |  j j j ƒ  k rc | d 7} n  | | k r
 | |  j | <| d k	 rì t g  | D] } | |  j j j ƒ  k ^ q’ ƒ r;t	 j
 |  j d | d | d | d | ƒ|  _ q;q>t t |  j j ƒ t | ƒ ƒ } t	 j
 |  j d | d | d | d | ƒ|  _ q
 q
 Wd S(   sÕ	  De-pivots `_data` from a 'wide' to 'tall' layout.

        A wide table is one where the column names represent a categorical variable
        and each contains only the values associated with each unique value of the
        categorical variable.

        This method uses the :func:`pandas.melt` function with additional logic
        to make sure that the same data source can have multiple operations applied,
        and so all other columns are maintained through the stacking process.

        Example:

            .. note::

                This example is fairly low level and is not something the typical
                user should worry about. The interface for data transformations from
                the user perspective are the :ref:`bkcharts_functions`.

            >>> data = {'a': [1, 2, 3, 4],
            ...         'b': [2, 3, 4, 5],
            ...         'month': ['jan', 'jan', 'feb', 'feb']
            ...         }

            >>> ds = ChartDataSource.from_data(data)
            >>> ds['x'] =['a', 'b'] # say we selected a and b for dimension x

            We may want to combine 'a' and 'b' together. The final
            data would look like the following:

            >>> ds.stack_measures(['c', 'd'], var_name='c_d_variable',
            ...                   value_name='c_d_value')
            >>> ds.df
            Out[35]:
                  month a_b_variable  a_b_value
                0   jan            a          1
                1   jan            a          2
                2   feb            a          3
                3   feb            a          4
                4   jan            b          2
                5   jan            b          3
                6   feb            b          4
                7   feb            b          5

            The transformed data will use the `var_name` and `value_name` inputs to
            name the columns. These derived columns can then be used as a single column
            to reference the values and the labels of the data. In the example, I could
            plot a_b_value vs month, and color by a_b_variable.

            What this does for you over the :meth:`pandas.melt` method is that it will
            apply the :class:`DataOperator` for a dimension if it exists (e.g.
            :class:`Blend`, generated by :func:`blend`), and it will try to handle the id
            columns for you so you don't lose other columns with the melt transformation.

        Returns:
            None

        t   _t   id_varst
   value_varst   var_namet
   value_nameN(   Re   Rj   R3   R9   R/   R   R0   R   t   allt   pdt   meltRd   R-   t   set(	   R   t   measurest   idsR€   R   R*   RE   t   dim_colst   measure(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   stack_measures©  s     <1"c         K@  s1   t  | ƒ d k r! t d ƒ ‚ n  t |  j |  S(   sN   Iterable of chart attribute specifications, associated with columns.

        Iterates over DataGroup, which represent the lowest level of data that is assigned
        to the attributes for plotting.

        Yields:
            a DataGroup, which contains metadata and attributes
            assigned to the group of data

        i    sB   You must provide one or more Attribute Specs to support iteration.(   R,   Rs   RT   Rd   (   R   RV   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRT     s    c   	      K@  s-  |  j  j ƒ  } | s! |  j } n  g  } g  } t } xO |  j |   D]> } | j d k ra t } n  | j | ƒ | j | j	 ƒ  ƒ qC W| rà t
 j j | d j	 ƒ  g ƒ } d | d <d | d <t
 j | | d d ƒ} | d =nI t
 j j | ƒ } t | d j j ƒ  ƒ } t
 j | | d d d | ƒ} | S(   sl  Produce new DataFrame from source data and `AttrSpec` provided.

        Args:
            **attr_specs (str, `AttrSpec`, optional): pairs of names and attribute spec
              objects. This is optional and not required only if the `ChartDataSource`
              already contains references to the attribute specs.

        Returns:
            pd.DataFrame: a new dataframe that includes a column for each of the
                attribute specs joined in, plus one special column called
                `chart_index`, which contains the unique items between the different
                attribute specs.

        i    t
   join_valuet   join_columnt   ont   howt   leftN(   Rd   R   RC   RU   RT   RB   R   t   TrueRx   RQ   Rƒ   t	   DataFramet   from_recordst   mergeR-   R.   (	   R   RC   R   t   groupst   rowst   no_indext   groupt	   attr_dataR\   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt
   join_attrs  s*    	


c         O@  sš  | j  d d ƒ } | d k	 re g  | D]7 } t | t ƒ sV t | t ƒ r% | | k r% | ^ q% } n  g  | D] } |  j | ƒ rl | ^ ql } g  | D]* } |  j | ƒ s¸ |  j | ƒ r” | ^ q” } t | ƒ d k r÷ t | ƒ d k r÷ t d ƒ ‚ n  t | ƒ d k r"t | ƒ d k r"g  t	 | ƒ D]H \ } } |  j
 | ƒ sR|  j | ƒ r(| d k	 r(| d k	 r(| ^ q(}	 t |	 ƒ d k r÷g  |	 D] }
 | |
 ^ q} |  j
 | ƒ rÌt t j | ƒ ƒ } n  t t | ƒ ƒ } | j ƒ  } x÷ |	 D]ï }
 g  } | |
 } |  j
 | ƒ rLt g  | D] } |  j | ƒ ^ q!ƒ rL| g } n  xh | D]` } xW t | | ƒ D]F \ } } t j j t j | ƒ t j | ƒ k ƒ ri| j | ƒ qiqiWqSWt | ƒ d k rÖ| d } n  | | |
 <qñW| } | | d <q"g  | D] } |  j
 | ƒ rþ| ^ qþ} n  | d k	 r;| | d <n  t | ƒ d k rgd | d	 <|  j | |  St | ƒ d k r–t | ƒ d k ršt d
 ƒ ‚ n
 | d } t | t ƒ rt g  | j ƒ  D] } |  j | ƒ ^ qÃƒ r
d | d	 <|  d t j j d | ƒ |  St d ƒ ‚ q–|  j | ƒ rQd | d	 <|  d t j j d | ƒ |  St | t j ƒ r}d | d	 <|  d | |  St d t | ƒ ƒ ‚ n  d S(   s1  Automatically handle all valid inputs.

        Attempts to use any data that can be represented in a Table-like format,
        along with any generated requirements, to produce a
        :class:`ChartDataSource`. Internally, these data types are generated, so that a
        :class:`pandas.DataFrame` can be generated.

        Identifies inputs that are array vs table like, handling them accordingly. If
        possible, existing column names are used, otherwise column names are generated.

        Returns:
            :class:`ColumnDataSource`

        R'   i    s&   Only input either array or table data.R&   Rp   i   R/   t
   iter_arrayR_   s   Input a single table data type.t
   dict_arrayR   R;   s1   Input of table-like dict must be column-oriented.t
   list_dictsR‘   s=   Unable to recognize inputs for conversion to dataframe for %sN(   Rc   R   R3   RD   t   is_arrayt   is_tablet   is_list_dictsR,   Rw   R   t   is_list_arraysR-   R   t   from_iterableR   R   R‚   R   Rƒ   t   SeriesRx   t   from_arraysR4   t   valuesR‘   t	   from_dictR’   t   type(   t   clst   argsRq   R'   t   argt   arrayst   tablest   kt   vt	   list_dimsR*   t	   col_namest
   new_kwargsRˆ   t
   dim_inputst	   dim_inputt   arrayt   col_namet   tableR8   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt	   from_data@  sr    *(7$$($
#*+

.


c         C@  sè   t  } t |  t ƒ rn t |  ƒ d k rn t |  d t ƒ rn t |  d d t ƒ rn t j |  d d ƒ rn | St |  t ƒ r± t g  |  D] } t j | ƒ ^ q‡ ƒ rä t } qä n3 t |  t j	 ƒ rä t |  j
 ƒ d k rä t } qä n  | S(   s_   Verify if input data is a list of array-like data.

        Returns:
            bool

        i   i    i   (   RU   R3   R-   R,   R^   R   R‚   R   t   npt   ndarrayt   shape(   R;   t   validR8   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR    ¸  s    40(c         C@  s   |  j  S(   N(   Rd   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR   Ô  s    c         C@  s   t  |  j ƒ S(   N(   R   R   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRG   Ø  s    c          K@  s$   |  j  |  d ƒ } | s d S| Sd S(   s…   Returns dimensions by name from kwargs.

        Returns:
            iterable(str): iterable of dimension names as strings

        R   R   N(   R   R   (   Rc   R   (   Rq   R&   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   _collect_dimensionsÜ  s    c         @  sr  t  ‡  f d †  | Dƒ ƒ rÂ t | ƒ } t t j | ƒ ƒ } | pR t t | ƒ ƒ } t | ƒ } | j d t ƒ } xd t	 | | ƒ D]8 \ } } g  | D] }	 | j
 d ƒ ^ q– }
 |
 | | <qƒ Wn | p× t t | ƒ ƒ } x] t | ƒ D]O \ } } t | t j ƒ rç | j } | | k r6| d k	 r6| | | <q6qç qç Wd „  t	 | | ƒ Dƒ } ˆ  d t j j d | ƒ |  S(   sy   Produce :class:`ColumnDataSource` from array-like data.

        Returns:
            :class:`ColumnDataSource`

        c         3@  s   |  ] } ˆ  j  | ƒ Vq d  S(   N(   R    (   R)   R³   (   R§   (    s3   lib/python2.7/site-packages/bkcharts/data_source.pys	   <genexpr>ó  s    R&   i    c         S@  s   i  |  ] \ } } | | “ q S(    (    (   R)   t   column_nameR³   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pys
   <dictcomp>  s   	 R   R;   N(   t   anyR   R-   R   R¡   R   R,   R5   Ra   R   Rc   t	   enumerateR3   Rƒ   R¢   RX   R   R‘   R¥   (   R§   Rª   t   column_namesRq   t   list_of_arraysR\   R&   R*   t   list_of_arrayR}   R+   t   iR³   RX   Rµ   (    (   R§   s3   lib/python2.7/site-packages/bkcharts/data_source.pyR£   ê  s"    	"	c         K@  s   |  d t  j j | ƒ |  S(   sy   Produce :class:`ColumnDataSource` from table-like dict.

        Returns:
            :class:`ColumnDataSource`

        R   (   Rƒ   R‘   R¥   (   R§   R;   Rq   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR¥     s    c         C@  s   t  j |  t ƒ p t  j |  ƒ S(   s}   Verify if data is table-like.

        Inspects the types and structure of data.

        Returns:
            bool

        (   R^   t	   _is_validt   TABLE_TYPESRŸ   (   R;   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRž     s    
c         C@  s5   t  |  t ƒ o4 t g  |  D] } t  | t ƒ ^ q ƒ S(   s]   Verify if data is row-oriented, table-like data.

        Returns:
            bool

        (   R3   R-   R‚   R4   (   R;   RP   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRŸ   "  s    c         C@  s'   t  j |  ƒ r t St  j |  t ƒ Sd S(   sJ   Verify if data is array-like.

        Returns:
            bool

        N(   R^   RŸ   RU   RÃ   t   ARRAY_TYPES(   R;   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR   ,  s    c         C@  s&   t  g  | D] } t |  | ƒ ^ q
 ƒ S(   s®   Checks for each type against data.

        Args:
            data: a generic source of data
            types: a list of classes

        Returns:
            bool

        (   R½   R3   (   R;   t   typest
   valid_type(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRÃ   :  s    c         C@  s·  |  j  } |  j } g  t | ƒ D] \ } } | d k	 r | ^ q } |  j d k	 r} g  | D] } | |  j k rY | ^ qY } n  t | ƒ d k r¯xU | D]M } t | ƒ t | ƒ k  rº q– n  t g  | D] } | | k ^ qÄ ƒ r– d Sq– Wd d d } g  | D], } d j g  | D] } d | ^ qƒ ^ qü }	 g  t | ƒ D]4 \ } } | d k	 r;d t | ƒ t | ƒ f ^ q;}
 t	 | d	 j |	 ƒ d
 j |
 ƒ d
 j |  j
 ƒ f ƒ ‚ n d Sd S(   sy   Raises selection error if selections are not valid compared to requirements.

        Returns:
            None

        i    NsP   Did not receive a valid combination of selections.

Valid configurations are: %ss   
Received inputs are: %ss   

Available columns are: %ss    and s   %s = <Any Column>s   %s = %ss    or s   , (   Rg   Rj   R   R   R'   R,   R‚   R=   RD   Rs   R/   (   R   Rp   R!   R*   R+   R&   t   reqt	   error_strt   required_dimt   req_strt   selection_str(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRo   H  s(    		1+%6D4c         C@  sC   t  |  t j ƒ r% t t ƒ j |  ƒ St f t } t  |  | ƒ Sd S(   sU   Verifies that value is a numerical type.

        Returns:
            bool

        N(   R3   Rƒ   R¢   R   R	   t   is_validt   floatR   (   R]   t   numbers(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt	   is_numbers  s    c         C@  s1   y t  |  ƒ } | t SWn t k
 r, t SXd S(   su   Verifies that value is a valid Datetime type, or can be converted to it.

        Returns:
            bool

        N(   R   R   Rs   RU   (   R]   t   dt(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   is_datetime  s    c         C@  s   i  S(   s6   Introspect which columns match to which types of data.(    (   R;   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyRm   ‘  s    c         C@  s
   |  j  j S(   s\   All column names associated with the data.

        Returns:
            List(Str)

        (   Rd   R/   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR/   —  s    c         C@  s
   |  j  j S(   s8   The index for the :class:`pandas.DataFrame` data source.(   Rd   R2   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR2   ¡  s    c         C@  s
   |  j  j S(   N(   Rd   R¤   (   R   (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR¤   ¦  s    c         C@  s   |  t  k r t St Sd S(   sm   Verify if the column provided matches to known computed columns.

        Returns:
            bool

        N(   t   COMPUTED_COLUMN_NAMESR   RU   (   t   column(    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   is_computedª  s    N(%   R#   R$   R%   R   R(   R   RR   RC   Ri   Rl   Rk   RI   R{   RŠ   RT   R™   t   classmethodR¶   t   staticmethodR    R   RG   R»   R£   R¥   Rž   RŸ   R   RÃ   Ro   RÐ   RÒ   Rm   R/   R2   R¤   RÕ   (    (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyR^     sB   	*	*		
		X		-x 

	+
(7   R%   t
   __future__R    R   t	   itertoolsR   t   operatorR   t   numpyR·   t   pandasRƒ   t   sixR   t	   six.movesR   t   bokeh.core.has_propsR   t   bokeh.core.propertiesR   R   R	   R
   R   t   bokeh.models.sourcesR   R   R   R   t   statsR   R   t   utilsR   R   R   RÓ   RN   R-   R¸   R¢   RÅ   R4   R‘   RÄ   Ra   Rb   R   R(   R1   R9   t   objectRA   RT   R^   (    (    (    s3   lib/python2.7/site-packages/bkcharts/data_source.pyt   <module>   s4   (	(H	C