σ
mάJ]c           @` sZ  d  Z  d d l m Z m Z m Z m Z d d l Z e j e  Z	 d d l
 Z
 d d l Z d d l Z d d l 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 e d
  Z e e j e j  e j e j  e j e j  e j e j  e j e j   e j e j!  e j e j"  e j e j#  g  Z$ e e j e j% e j& e j' g  Z( e rψy e j) Z* Wn e+ k
 r±e j, j) Z* n Xe( j- e*  e( j- e j.  e( j- e j/  e( j- e0 e j1   n  e j' d d  Z2 e j3 d d  Z4 e j j5 d  Z6 e e  d d j7 d   e$ D  Z  d4 Z8 d    Z9 d!   Z: d"   Z; d#   Z< d$   Z= d%   Z> d&   Z? d'   Z@ e e@ j  d d( j7 d)   e$ D  e@ _  eA d d*  ZC d+   ZD eA d d,  ZE eA d d-  ZF eG d d.  ZH d d d/  ZI d0   ZJ d1   ZK d2   ZL d3 aM e   ZN eO e(  ZP d S(5   uι   
Functions for helping with serialization and deserialization of
Bokeh objects.

Certain NumPy array dtypes can be serialized to a binary format for
performance and efficiency. The list of supported dtypes is:

{binary_array_types}

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   Locki   (   t   settingsi   (   t   format_docstring(   t   import_optionalu   pandasu   mst   binary_array_typesu   
c         c` s#   |  ] } d  t  |  d Vq d S(   u   * ``np.u   ``N(   t   str(   t   .0t   x(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pys	   <genexpr>U   s    u   array_encoding_disabledu   convert_datetime_arrayu   convert_datetime_typeu   convert_timedelta_typeu   decode_base64_dictu   encode_binary_dictu   encode_base64_dictu   is_datetime_typeu   is_timedelta_typeu   make_globally_unique_idu   make_idu   serialize_arrayu   transform_arrayu   transform_array_to_listu   transform_column_source_datau   traverse_datau   transform_seriesc         C` s   t  |  t  S(   uΙ    Whether an object is any date, time, or datetime type recognized by
    Bokeh.

    Arg:
        obj (object) : the object to test

    Returns:
        bool : True if ``obj`` is a datetime type

    (   t
   isinstancet	   _dt_tuple(   t   obj(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   is_datetime_typeo   s    c         C` s   t  |  t j t j f  S(   uΈ    Whether an object is any timedelta type recognized by Bokeh.

    Arg:
        obj (object) : the object to test

    Returns:
        bool : True if ``obj`` is a timedelta type

    (   R   t   dtt	   timedeltat   npt   timedelta64(   R   (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   is_timedelta_type|   s    
c         C` s>   t  |  t j  r  |  j   d St  |  t j  r: |  t Sd S(   uΌ    Convert any recognized timedelta value to floating point absolute
    milliseconds.

    Arg:
        obj (object) : the object to convert

    Returns:
        float : milliseconds

    g     @@N(   R   R   R   t   total_secondsR   R   t   NP_MS_DELTA(   R   (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   convert_timedelta_type   s    c         C` s[  t  r |  t  j k r t j St  rE t |  t  j  rE |  j   j d
 St  re t |  t  re |  j d St  r t |  t  j	  r |  j d St |  t
 j  rΎ |  j d d	  t } | j   d St |  t
 j  rυ t
 j |  j   d    t j   d St |  t j  r|  t } | t St |  t
 j  rW|  j d |  j d |  j d |  j d Sd	 S(   uΝ    Convert any recognized date, time, or datetime value to floating point
    milliseconds since epoch.

    Arg:
        obj (object) : the object to convert

    Returns:
        float : milliseconds

    i
   g      @t   tzinfog     @@i   iθ  i  i<   Ng    .Ag    .Ag    .A(   t   pdt   NaTR   t   nanR   t   Periodt   to_timestampt   valuet   _pd_timestampt	   TimedeltaR   t   datetimet   replacet   Nonet   DT_EPOCHR   t   datet	   timetuplet
   datetime64t   NP_EPOCHR   t   timet   hourt   minutet   secondt   microsecond(   R   t   difft   epoch_delta(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   convert_datetime_type   s$      %
c         C` s9  t  |  t j  s |  Sy: t j d  } | j d  | j d  j d  k } WnI t k
 r } | j d k r d t j k r t	 } q |  q |  n X|  j
 j d k r| rε |  j
 t j
 d  k r|  j d  d }  qq5|  j d
  j d  d }  n1 |  j
 j d k r5|  j d  j d  d }  n  |  S(   u   Convert NumPy datetime arrays to arrays to milliseconds since epoch.

    Args:
        array : (obj)
            A NumPy array of datetime to convert

            If the value passed in is not a NumPy array, it will be returned as-is.

    Returns:
        array

    u   2001u   int64u   datetime64[ms]u-   'module' object has no attribute 'datetime64'u   PyPyu   Mu   datetime64[ns]i
   g      @u   datetime64[us]g     @@u   mu   timedelta64[us](   u-   'module' object has no attribute 'datetime64'g    .A(   R   R   t   ndarrayR'   t   astypet   AttributeErrort   argst   syst   versiont   Falset   dtypet   kind(   t   arrayt   dt2001t   legacy_datetime64t   e(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   convert_datetime_arrayΓ   s*    	
c           C` s>   t  j t  r3 t  t d 7a t t  SWd QXn t   Sd S(   u   Return a new unique ID for a Bokeh object.

    Normally this function will return simple monotonically increasing integer
    IDs (as strings) for identifying Bokeh objects within a Document. However,
    if it is desirable to have globally unique for every object, this behavior
    can be overridden by setting the environment variable ``BOKEH_SIMPLE_IDS=no``.

    Returns:
        str

    i   N(   R   t
   simple_idst   Truet   _simple_id_lockt
   _simple_idR	   t   make_globally_unique_id(    (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   make_idπ   s
    
c           C` s   t  t j    S(   u΅    Return a globally unique UUID.

    Some situations, e.g. id'ing dynamically created Divs in HTML documents,
    always require globally unique IDs.

    Returns:
        str

    (   R	   t   uuidt   uuid4(    (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyRC     s    
c         C` s   |  j  t k S(   uέ    Determine whether an array may be binary encoded.

    The NumPy array dtypes that can be encoded are:

    {binary_array_types}

    Args:
        array (np.ndarray) : the array to check

    Returns:
        bool

    (   R8   t   BINARY_ARRAY_TYPES(   R:   (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   array_encoding_disabled  s    u   
    c         c` s#   |  ] } d  t  |  d Vq d S(   u   * ``np.u   ``N(   R	   (   R
   R   (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pys	   <genexpr>$  s   c         C` s"   t  |   }  t |  d | d | S(   u   Transform a NumPy arrays into serialized format

    Converts un-serializable dtypes and returns JSON serializable
    format

    Args:
        array (np.ndarray) : a NumPy array to be transformed
        force_list (bool, optional) : whether to only output to standard lists
            This function can encode some dtypes using a binary encoding, but
            setting this argument to True will override that and cause only
            standard Python lists to be emitted. (default: False)

        buffers (set, optional) :
            If binary buffers are desired, the buffers parameter may be
            provided, and any columns that may be sent as binary buffers
            will be added to the set. If None, then only base64 encoding
            will be used (default: None)

            If force_list is True, then this value will be ignored, and
            no buffers will be generated.

            **This is an "out" parameter**. The values it contains will be
            modified in-place.


    Returns:
        JSON

    t
   force_listt   buffers(   R>   t   serialize_array(   R:   RI   RJ   (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   transform_array'  s    c         C` sέ   |  j  j d	 k rz t j |   j   rz |  j d  } d | t j |   <d | t j |   <d | t j |   <| j	   S|  j  j d k rΣ t
 rΣ t
 j |   j   rΣ |  j d  } d | t
 j |   <| j	   S|  j	   S(
   u₯    Transforms a NumPy array into a list of values

    Args:
        array (np.nadarray) : the NumPy array series to transform

    Returns:
        list or dict

    u   uu   iu   fu   objectu   NaNu   Infinityu	   -Infinityu   O(   u   uu   iu   f(   R8   R9   R   t   isfinitet   anyR2   t   isnant   isposinft   isneginft   tolistR   t   isnull(   R:   t   transformed(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   transform_array_to_listJ  s    
(
-
c         C` sC   t  |  t j  r$ |  j   j } n	 |  j } t | d | d | S(   uΟ   Transforms a Pandas series into serialized form

    Args:
        series (pd.Series) : the Pandas series to transform
        force_list (bool, optional) : whether to only output to standard lists
            This function can encode some dtypes using a binary encoding, but
            setting this argument to True will override that and cause only
            standard Python lists to be emitted. (default: False)

        buffers (set, optional) :
            If binary buffers are desired, the buffers parameter may be
            provided, and any columns that may be sent as binary buffers
            will be added to the set. If None, then only base64 encoding
            will be used (default: None)

            If force_list is True, then this value will be ignored, and
            no buffers will be generated.

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

    Returns:
        list or dict

    RI   RJ   (   R   R   t   PeriodIndexR   t   valuesRL   (   t   seriesRI   RJ   t   vals(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   transform_series`  s    	c         C` s   t  |  t j j  r* |  j t j  }  n  t |   s< | rF t |   S|  j d se t j	 |   }  n  | d k r{ t |   St |  |  Sd S(   uΜ   Transforms a NumPy array into serialized form.

    Args:
        array (np.ndarray) : the NumPy array to transform
        force_list (bool, optional) : whether to only output to standard lists
            This function can encode some dtypes using a binary encoding, but
            setting this argument to True will override that and cause only
            standard Python lists to be emitted. (default: False)

        buffers (set, optional) :
            If binary buffers are desired, the buffers parameter may be
            provided, and any columns that may be sent as binary buffers
            will be added to the set. If None, then only base64 encoding
            will be used (default: None)

            If force_list is True, then this value will be ignored, and
            no buffers will be generated.

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

    Returns:
        list or dict

    u   C_CONTIGUOUSN(   R   R   t   mat   MaskedArrayt   filledR   RH   RU   t   flagst   ascontiguousarrayR#   t   encode_base64_dictt   encode_binary_dict(   R:   RI   RJ   (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyRK     s    

c         C` sω   | r? t  d   |  D  r? g  |  D] } t | d | ^ q# Sg  } x­ |  D]₯ } t |  t k rΉ t j |  r| d } n- t j |  r© | d k r  d } q© d } n  | j |  qL t | t	 t
 f  rδ | j t |   qL | j |  qL W| S(   uκ   Recursively traverse an object until a flat list is found.

    If NumPy is available, the flat list is converted to a numpy array
    and passed to transform_array() to handle ``nan``, ``inf``, and
    ``-inf``.

    Otherwise, iterate through all items, converting non-JSON items

    Args:
        obj (list) : a list of values or lists
        use_numpy (bool, optional) toggle NumPy as a dependency for testing
            This argument is only useful for testing (default: True)
    c         s` s!   |  ] } t  | t j  Vq d  S(   N(   R   R   R1   (   R
   t   el(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pys	   <genexpr>΅  s    RJ   u   NaNi    u   Infinityu	   -Infinity(   t   allRL   t   typet   floatt   mathRO   t   isinft   appendR   t   listt   tuplet   traverse_data(   R   t	   use_numpyRJ   Rb   t   obj_copyt   item(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyRk   §  s     #			c         C` sΞ   | d k r t |   n	 t |  } i  } x | D] } t ry t |  | t j t j f  ry t |  | d | | | <q1 t |  | t j  r¬ t	 |  | d | | | <q1 t
 |  | d | | | <q1 W| S(   uψ   Transform ``ColumnSourceData`` data to a serialized format

    Args:
        data (dict) : the mapping of names to data columns to transform

        buffers (set, optional) :
            If binary buffers are desired, the buffers parameter may be
            provided, and any columns that may be sent as binary buffers
            will be added to the set. If None, then only base64 encoding
            will be used (default: None)

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

        cols (list[str], optional) :
            Optional list of subset of columns to transform. If None, all
            columns will be transformed (default: None)

    Returns:
        JSON compatible dict

    RJ   N(   R#   t   setR   R   t   Seriest   IndexRZ   R   R1   RL   Rk   (   t   dataRJ   t   colst   to_transformt	   data_copyt   key(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   transform_column_source_dataΚ  s    $%c         C` s]   t    } t d |  |  j   f } | j |  i | d 6|  j d 6|  j j d 6t j d 6S(   u|   Send a numpy array as an unencoded binary buffer

    The encoded format is a dict with the following structure:

    .. code:: python

        {
            '__buffer__' :  << an ID to locate the buffer >>,
            'shape'      : << array shape >>,
            'dtype'      : << dtype name >>,
            'order'      : << byte order at origin (little or big)>>
        }

    Args:
        array (np.ndarray) : an array to encode

        buffers (set) :
            Set to add buffers to

            **This is an "out" parameter**. The values it contains will be
            modified in-place.

    Returns:
        dict

    t   idu
   __buffer__u   shapeu   dtypeu   order(	   RD   t   dictt   tobytesRh   t   shapeR8   t   nameR5   t	   byteorder(   R:   RJ   t	   buffer_idt   buf(    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyRa   ξ  s    	
c         C` s7   i t  j |  j  j d  d 6|  j d 6|  j j d 6S(   u   Encode a NumPy array using base64:

    The encoded format is a dict with the following structure:

    .. code:: python

        {
            '__ndarray__' : << base64 encoded array data >>,
            'shape'       : << array shape >>,
            'dtype'       : << dtype name >>,
        }

    Args:

        array (np.ndarray) : an array to encode

    Returns:
        dict

    u   utf-8u   __ndarray__u   shapeu   dtype(   t   base64t	   b64encodeRr   t   decodeR{   R8   R|   (   R:   (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyR`     s    
c         C` se   t  j |  d  } t j t j | d |  d  } t |  d  d k ra | j |  d  } n  | S(   uί    Decode a base64 encoded array into a NumPy array.

    Args:
        data (dict) : encoded array data to decode

    Data should have the format encoded by :func:`encode_base64_dict`.

    Returns:
        np.ndarray

    u   __ndarray__R8   u   dtypeu   shapei   (   R   t	   b64decodeR   t   copyt
   frombuffert   lent   reshape(   Rr   t   b64R:   (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   decode_base64_dict/  s
    "iη  (   u   array_encoding_disabledu   convert_datetime_arrayu   convert_datetime_typeu   convert_timedelta_typeu   decode_base64_dictu   encode_binary_dictu   encode_base64_dictu   is_datetime_typeu   is_timedelta_typeu   make_globally_unique_idu   make_idu   serialize_arrayu   transform_arrayu   transform_array_to_listu   transform_column_source_datau   traverse_datau   transform_series(Q   t   __doc__t
   __future__R    R   R   R   t   loggingt	   getLoggert   __name__t   logR   R!   R   Rf   R5   t	   threadingR   RE   t   numpyR   R   t   stringR   t   dependenciesR   R   Ro   R8   t   float32t   float64t   uint8t   int8t   uint16t   int16t   uint32t   int32RG   R%   R)   R'   t   DATETIME_TYPESt	   TimestampR   R3   t   tslibt   addR    R   Rd   R   R(   R   R   t   utcfromtimestampR$   t   joint   __all__R   R   R   R0   R>   RD   RC   RH   R7   R#   RL   RU   RZ   RK   R@   Rk   Rw   Ra   R`   R   RB   RA   Rj   R   (    (    (    s7   lib/python2.7/site-packages/bokeh/util/serialization.pyt   <module>   s’   "%                				+	-			#	"%#$	&			