ó
ÎYc        	   @`  s‹  d  Z  d d l m Z m Z d d l Z d d l Z d d l 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 m Z d d l m Z m Z d d	 l m Z d d
 l m Z m Z 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' m( Z( m) Z) m* Z* m+ Z+ d d d „  ƒ  YZ, d e f d „  ƒ  YZ- e e- ƒ d d d e/ d e/ e/ d „ ƒ Z0 d S(   sù   This is the Bokeh charts interface. It gives you a high level API
to build complex plot is a simple way.

This is the Chord class which lets you build your Chord charts
just passing the arguments to the Chart class and calling the proper
functions.
i    (   t   absolute_importt   divisionN(   t   cost   sint   pi(   t	   Dimension(   t   create_and_buildt   Builder(   t
   MarkerAttrt	   ColorAttrt   CatAttr(   t   color_in_equal_spacet   help(   t   Range1d(   t   Arct   Beziert   Text(   t   GlyphRenderer(   t   ColumnDataSource(	   t   Instancet   Boolt   Stringt   Arrayt   Floatt   Anyt   Seqt   Eithert   Intt   Areac           B`  s   e  Z d  Z d „  Z RS(   sÀ    It represents an arc area. It will create a list of available points through the arc representing that area and
    then those points will be used as start and end for the beziers lines.
    c         C`  s€   | |  _  | |  _ | |  _ t j | | | ƒ } g  | D] } t | ƒ t | ƒ g ^ q7 |  _ |  j  t |  j ƒ k s| t	 ‚ d  S(   N(
   t   n_connt   start_pointt	   end_pointt   npt   linspaceR   R   t   free_pointst   lent   AssertionError(   t   selfR   R   R   t   free_points_anglest   angle(    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyt   __init__,   s    			.(   t   __name__t
   __module__t   __doc__R(   (    (    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyR   '   s   t   ChordBuilderc           B`  sã   e  Z d  Z i e ƒ  d 6e ƒ  d 6e ƒ  d 6Z d g Z e d ƒ Z	 e
 e ƒ Z e
 e ƒ Z e
 e ƒ Z e ƒ  Z e ƒ  Z e ƒ  Z e ƒ  Z e e ƒ  ƒ Z e e e e ƒ  e ƒ  ƒ ƒ ƒ Z d „  Z d „  Z d „  Z d „  Z  RS(	   s‚   This is the Chord builder and it is in charge of plotting
    Chord graphs in an easy and intuitive way.

    Essentially, we provide a way to ingest the data, make the proper
    calculations and push the references into a source object.
    We additionally make calculations for the ranges. And finally add
    the needed glyphs (markers) taking the references from the source.

    t   colort   markert   stackt   valuesc         C`  s?   |  j  s d n d } t | | ƒ |  _ t | | ƒ |  _ d  S(   Ngš™™™™™ñ?gÍÌÌÌÌÌü?(   t   labelR   t   x_ranget   y_range(   R%   t   rng(    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyt
   set_rangesW   s    c         C`  s@  |  j  sö|  j j |  j } |  j j |  j } | j | ƒ j ƒ  } | j d } t j	 t
 j | | f ƒ d | d | ƒ} |  j s– t | ƒ |  _ n  |  j d  k rü xB |  j j j ƒ  D]. \ } } | | |  j | |  j c d 7<q¸ W| j ƒ  |  _ n  |  j d  k	 rt |  j t ƒ s/t |  j t ƒ rƒx? |  j j j ƒ  D]+ \ } } |  j | | |  j | |  j <qBW| j ƒ  |  _ qót |  j t ƒ róxC |  j j j ƒ  D]/ \ } } | |  j | | |  j | |  j <q¨W| j ƒ  j |  _ qóqn |  j j j ƒ  |  _ |  j r<t |  j ƒ |  j j d k s<t ‚ n  d  S(   Ni    t   columnst   indexi   (   t   square_matrixR0   t   _datat   origint   destinationt   appendt   uniquet   shapet   pdt	   DataFrameR    t   zerosR1   t   listt   valuet   Nonet   iterrowst
   get_valuest   matrixt
   isinstancet   intt   floatt   strt   Tt   dfR#   R$   (   R%   t   sourcet   targett   uniont   Nt   mt   _t   row(    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyt   setup\   s0    	*	&$#'	c         C`  s`  |  j  j d d ƒ |  j  j d d ƒ |  j  j ƒ  } | | j ƒ  d t } t j | j d d ƒ } | | d )| j ƒ  } g  t | j d ƒ D] } t	 | | j d ƒ ^ q‘ } t
 j i | d  d 6| d d 6| d 6ƒ } t | ƒ |  _ |  j rtd	 } | d  | d d
 } t
 j i | d 6t j | ƒ | d 6t j | ƒ | d 6t |  j ƒ d 6ƒ }	 t |	 ƒ |  _ n  g  }
 xH t | j d ƒ D]3 } |
 j t | | | d  | | d | ƒ ƒ qŽWg  } x¸ t |  j  ƒ D]§ \ } } |
 | } | | } | | } xz t | ƒ D]l \ } } |
 | } xS t t | ƒ ƒ D]? } | j j ƒ  } | j j ƒ  } | j | | | | g ƒ q;WqWqÛWt
 j | d t ƒ} d d d d d d g | _ | j j d ƒ d | d <| j j d ƒ d | d <| j j d ƒ d | d <| j j d ƒ d | d <| j  j d ƒ | j  j d ƒ j ƒ  d | _  t | ƒ |  _! d  S(   Nt   axisi    i   i   iÿÿÿÿt   start_anglet	   end_anglet
   line_colorgš™™™™™ñ?g       @t   anglest   text_xt   text_yt   textt   dtypet   start_xt   start_yt   end_xt   end_yt   colorst   weightt   float64t   cx0t   cy0t   cx1t   cy1i¸  ("   RG   t   sumt   diagonalR   R    RA   R>   t   cumsumt   rangeR   R?   R@   R   t	   arcs_dataR1   R   R   RB   t	   text_dataR<   R   t	   enumerateRI   R"   t   popRK   R6   R_   t   astypeR`   Ra   Rb   Rd   t   connection_data(   R%   t   weights_of_areast   areas_in_radianst   pointst   areaRc   Rn   t   text_radiusRZ   t   text_positionst	   all_areast   it   all_connectionst   jt   region1RN   R-   Rd   t   kt   region2RO   RS   t   p1t   p2t   connections_df(    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyt   process_data€   sT    5
7			1



'/c         c`  s  t  d d d d d d d d d	 d	 d
 d
 d d d d d d d d ƒ 
} t d |  j d | ƒ Vt d d d d d d d d d d d d d d ƒ } t d |  j d | ƒ V|  j ri d d 6d d 6d  d! 6d" d# 6} t d d$ d d% d& d& d' d( |  } t d |  j d | ƒ Vn  d) S(*   sˆ   Use the marker glyphs to display the arcs and beziers.
        Takes reference points from data loaded at the ColumnDataSource.
        t   x0R_   t   y0R`   t   x1Ra   t   y1Rb   Rf   Rg   Rh   Ri   t
   line_alphaRd   RY   Rc   t   data_sourcet   glypht   xi    t   yt   radiusi   t
   line_widthi
   RW   RX   s   #000000t
   text_colort   8ptt   text_font_sizet   leftt
   text_alignt   middlet   text_baselineR[   R\   R]   R'   RZ   N(   R   R   Rs   R   Rn   R1   R   Ro   (   R%   t   bezierst   arcst
   text_propst   labels(    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyt   yield_renderersÂ   s>    			
	(!   R)   R*   R+   R	   R   R
   t   default_attributest
   dimensionsR   R0   R   R   Rn   Ro   Rs   R   R:   R;   R   RC   R   R8   R   R1   R   R   R   R   RG   R5   RU   R„   R›   (    (    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyR,   9   s&   	
					!		$	Bc   
      K`  s…   | | d <| | d <| | d <| | d <| | d <| | d <| | d <t  t |  |  }	 t |	 j d _ t |	 j d _ d	 |	 _ |	 S(
   sJ
  
    Create a chord chart using :class:`ChordBuilder <bkcharts.builders.chord_builder.ChordBuilder>`
    to render a chord graph from a variety of value forms.
    This chart displays the inter-relationships between data in a matrix.

    The data can be generated by the chart interface. Given a :class:`DataFrame <pandas.DataFrame>`,
    select two columns to be used as arcs with `source` and `target` attributes, passing by the name of those columns.
    The :class:`Chord <bkcharts.builders.chord_builder.Chord>` chart will then deduce the
    relationship between the arcs.

    The value of the connections can be inferred automatically by counting `source` and `target`. If you prefer
    you can assign a fixed value for all the connections with `value` simply passing by a number. A third option is to
    pass a reference to a third column in the :class:`DataFrame <pandas.DataFrame>` with the values for the connections.

    If you want to plot the relationships in a squared matrix, simply pass the matrix and set `square_matrix` attribute
    to `True`.

    Reference: `Chord diagram on Wikipedia <https://en.wikipedia.org/wiki/Chord_diagram>`_

    Args:
        data (:ref:`userguide_charts_data_types`): the data source for the chart.
        source (list(str) or str, optional): Data source to use as origin of the connection to a destination.
        target (list(str) or str, optional): Data source to use as destination of a connection.
        value (list(num) or num, optional): The value the connection should have.
        square_matrix (bool, optional): If square matrix, avoid any calculations during the setup.
        label (list(str), optional): The labels to be put in the areas.

    Returns:
        :class:`Chart`: includes glyph renderers that generate the chord

    Examples:

    .. bokeh-plot::
        :source-position: above

        import pandas as pd
        from bkcharts import Chord
        from bokeh.io import show, output_file
        from bokeh.sampledata.les_mis import data

        nodes = data['nodes']
        links = data['links']

        nodes_df = pd.DataFrame(nodes)
        links_df = pd.DataFrame(links)

        source_data = links_df.merge(nodes_df, how='left', left_on='source', right_index=True)
        source_data = source_data.merge(nodes_df, how='left', left_on='target', right_index=True)
        source_data = source_data[source_data["value"] > 5]  # Select those with 5 or more connections

        chord_from_df = Chord(source_data, source="name_x", target="name_y", value="value")
        output_file('chord_from_df.html')
        show(chord_from_df)

    R:   R;   RC   R8   R1   t   xgridt   ygridi    N(   R   R,   t   FalseR“   t   visiblet   belowRD   t   outline_line_color(
   t   dataRN   RO   RC   R8   R1   Rž   RŸ   t   kwt   chart(    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyt   Chordð   s    :






	(    (1   R+   t
   __future__R    R   t   numpyR    t   pandasR?   t   mathR   R   R   t   bkcharts.propertiesR   t   bkcharts.builderR   R   t   bkcharts.attributesR   R	   R
   t   bkcharts.utilsR   R   t   bokeh.modelsR   t   bokeh.models.glyphsR   R   R   t   bokeh.models.renderersR   t   bokeh.models.sourcesR   t   bokeh.core.propertiesR   R   R   R   R   R   R   R   R   R   R,   RD   R    R§   (    (    (    s>   lib/python2.7/site-packages/bkcharts/builders/chord_builder.pyt   <module>   s"   @·	