
W[c        	   @  s"  d  Z  d d l m Z m 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 j Z d d l j Z d d l m Z m Z d d l m Z d d l m Z e e j  d	 k Z e e j  d
 k Z d d d d d d g Z d   Z d   Z  d   Z! d d  Z" d   Z# d   Z$ d d d d  Z& d   Z' d d e( e( e) e) d e) d  Z* d   Z+ d   Z, d d  Z- d d d  Z. d    Z/ d!   Z0 d"   Z1 d d#  Z2 e( d d$  Z3 d%   Z4 d&   Z5 d d'  Z6 d(   Z7 d)   Z8 d*   Z9 d d+ d,  Z: d S(-   s)   Small plotting-related utility functions.i(   t   print_functiont   divisionN(   t   statsi   (   t   urlopent   urlretrieve(   t   HTTPException(   t   LooseVersions   0.15s   1.5.0t
   desaturatet   saturatet   set_hls_valuest   despinet   get_dataset_namest   load_datasetc         C  s   |  t  j |   S(   s  Helper method for removing NA values from array-like.

    Parameters
    ----------
    arr : array-like
        The array-like from which to remove NA values.

    Returns
    -------
    clean_arr : array-like
        The original array with NA values removed.

    (   t   pdt   notnull(   t   arr(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt	   remove_na   s    c         O  s9   y |  j  | |   SWn t k
 r4 |  j | |   SXd S(   s=   Wrapper to handle different pandas sorting API pre/post 0.17.N(   t   sort_valuest   AttributeErrort   sort(   t   dft   argst   kwargs(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   sort_df*   s    c   	      C  s   t  j |   j d d  }  t  j |  } g  } xZ t t  j |    D]C \ } \ } } | | } | | } | | } | j | | g  qF Wt  j |  j } | S(   s  Convert intervals to error arguments relative to plot heights.

    Parameters
    ----------
    cis: 2 x n sequence
        sequence of confidence interval limits
    heights : n sequence
        sequence of plot heights

    Returns
    -------
    errsize : 2 x n array
        sequence of error size relative to height values in correct
        format as argument for plt.bar

    i   i(	   t   npt
   atleast_2dt   reshapet
   atleast_1dt	   enumeratet	   transposet   appendt   asarrayt   T(	   t   cist   heightst   errsizet   it   lowt   hight   ht   elowt   ehigh(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   ci_to_errsize2   s    (


i
   c         C  sK   t  j |  |  \ } } | | j   } | d | d } | d  | | f S(   sK  Return arguments to plt.bar for pmf-like histogram of an array.

    Parameters
    ----------
    a: array-like
        array to make histogram of
    bins: int
        number of bins

    Returns
    -------
    x: array
        left x position of bars
    h: array
        height of bars
    w: float
        width of bars

    i   i    i(   R   t	   histogramt   sum(   t   at   binst   nt   xR'   t   w(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   pmf_histP   s    c         C  sx   d | k o d k n s+ t  d   n  t j j |   } t j |   \ } } } | | 9} t j | | |  } | S(   sl  Decrease the saturation channel of a color by some percent.

    Parameters
    ----------
    color : matplotlib color
        hex, rgb-tuple, or html color name
    prop : float
        saturation channel of color will be multiplied by this value

    Returns
    -------
    new_color : rgb tuple
        desaturated color code in RGB tuple representation

    i    i   s   prop must be between 0 and 1(   t
   ValueErrort   mplcolt   colorConvertert   to_rgbt   colorsyst
   rgb_to_hlst
   hls_to_rgb(   t   colort   propt   rgbR'   t   lt   st	   new_color(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyR   j   s    
c         C  s   t  |  d d S(   s  Return a fully saturated color with the same hue.

    Parameters
    ----------
    color :  matplotlib color
        hex, rgb-tuple, or html color name

    Returns
    -------
    new_color : rgb tuple
        saturated color code in RGB tuple representation

    R>   i   (   R	   (   R:   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyR      s    c         C  sy   t  j j |   } t t j |    } x< t | | | g  D]% \ } } | d k	 r= | | | <q= q= Wt j |   } | S(   sn  Independently manipulate the h, l, or s channels of a color.

    Parameters
    ----------
    color : matplotlib color
        hex, rgb-tuple, or html color name
    h, l, s : floats between 0 and 1, or None
        new values for each channel in hls space

    Returns
    -------
    new_color : rgb tuple
        new color code in RGB tuple representation

    N(	   R4   R5   R6   t   listR7   R8   R   t   NoneR9   (   R:   R'   R=   R>   R<   t   valsR$   t   val(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyR	      s    "c         K  s0   t  j   } | j |  |  | j | |  d S(   s   Grab current axis and label it.N(   t   pltt   gcat
   set_xlabelt
   set_ylabel(   t   xlabelt   ylabelR   t   ax(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   axlabel   s    c         C  s  |  d k r* | d k r* t j   j } n0 |  d k	 rB |  j } n | d k	 rZ | g } n  x| D]w}	 x d d d d g D] }
 t   |
 } |	 j |
 j |  | d k	 rz | rz y | j |
 d  } Wn t k
 r | } n Xt	 |	 j |
 d | f  qz qz W| r| rt
 d   |	 j j D  } t
 d   |	 j j D  } |	 j j d  x |	 j j D] } | | _ qgWx  |	 j j D] } | | _ qWn  | r5| r5t
 d	   |	 j j D  } t
 d
   |	 j j D  } |	 j j d  x |	 j j D] } | | _ qWx  |	 j j D] } | | _ qWn  | ra |	 j   } | j rt j | t |	 j    k |  d } t j | t |	 j    k |  d } |	 j d j | |  |	 j d j | |  | j | | k  } | j | | k  } |	 j |  n  |	 j   } | j rt j | t |	 j    k |  d } t j | t |	 j    k |  d } |	 j d j | |  |	 j d j | |  | j | | k  } | j | | k  } |	 j |  qqa qa Wd S(   s  Remove the top and right spines from plot(s).

    fig : matplotlib figure, optional
        Figure to despine all axes of, default uses current figure.
    ax : matplotlib axes, optional
        Specific axes object to despine.
    top, right, left, bottom : boolean, optional
        If True, remove that spine.
    offset : int or dict, optional
        Absolute distance, in points, spines should be moved away
        from the axes (negative values move spines inward). A single value
        applies to all spines; a dict can be used to set offset values per
        side.
    trim : bool, optional
        If True, limit spines to the smallest and largest major tick
        on each non-despined axis.

    Returns
    -------
    None

    t   topt   rightt   leftt   bottomi    t   outwardc         s  s   |  ] } | j  Vq d  S(   N(   t   tick1On(   t   .0t   t(    (    s,   lib/python2.7/site-packages/seaborn/utils.pys	   <genexpr>   s    c         s  s   |  ] } | j  Vq d  S(   N(   RQ   (   RR   RS   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pys	   <genexpr>   s    c         s  s   |  ] } | j  Vq d  S(   N(   RQ   (   RR   RS   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pys	   <genexpr>   s    c         s  s   |  ] } | j  Vq d  S(   N(   RQ   (   RR   RS   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pys	   <genexpr>   s    iN(   RA   RD   t   gcft   axest   localst   spinest   set_visiblet   getR   t   _set_spine_positiont   anyt   yaxist
   majorTickst
   minorTickst   set_ticks_positiont   tick2Ont   xaxist
   get_xtickst   sizeR   t   compresst   mint   get_xlimt   maxt
   set_boundst
   set_xtickst
   get_ytickst   get_ylimt
   set_yticks(   t   figRJ   RL   RM   RN   RO   t   offsett   trimRU   t   ax_it   sidet
   is_visibleRC   t   maj_ont   min_onRS   t   xtickst	   firsttickt   lasttickt   newtickst   yticks(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyR
      sn    
!		c         C  sV   |  j  } | d k	 r- | j } | j | _ n  |  j |  | d k	 rR | | _ n  d S(   sL  
    Set the spine's position without resetting an associated axis.

    As of matplotlib v. 1.0.0, if a spine has an associated axis, then
    spine.set_position() calls axis.cla(), which resets locators, formatters,
    etc.  We temporarily replace that call with axis.reset_ticks(), which is
    sufficient for our purposes.
    N(   t   axisRA   t   clat   reset_tickst   set_position(   t   spinet   positionRz   R{   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyRZ     s    			c         C  sU   t  |  j   | | | d  } t |  j    | | | d  } t j | | |  S(   s0   Establish support for a kernel density estimate.i    i   (   Rg   Re   R   t   linspace(   t   datat   bwt   gridsizet   cutt   clipt   support_mint   support_max(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   _kde_support,  s    !!c         C  s   g  } y t  |  } Wn  t k
 r8 | g } d } n Xxi t |  D][ \ } } | d k ry t j |  j   |  } n t j t j | |  |  } | j	 |  qF Wt j
 |  } | s | j   } n  | S(   s  Like scoreatpercentile but can take and return array of percentiles.

    Parameters
    ----------
    a : array
        data
    pcts : sequence of percentile values
        percentile or percentiles to find score at
    axis : int or None
        if not None, computes scores over this axis

    Returns
    -------
    scores: array
        array of scores at requested percentiles
        first dimension is length of object passed to ``pcts``

    i    N(   t   lent	   TypeErrorR   RA   R   t   scoreatpercentilet   ravelR   t   apply_along_axisR   R   t   squeeze(   R-   t   pctsRz   t   scoresR/   R$   t   pt   score(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   percentiles3  s    	
i_   c         C  s,   d | d d | d f } t  |  | |  S(   s2   Return a percentile range from an array of values.i2   i   (   R   (   R-   t   whichRz   R   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   ciX  s    c         C  sD   |  d k  r d S|  d k  r  d S|  d k  r0 d S|  d k  r@ d Sd	 S(
   s?   Return a R-style significance string corresponding to p values.gMbP?s   ***g{Gz?s   **g?t   *g?t   .t    (    (   R   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt	   sig_stars^  s    c         C  s;   t  j |   }  t j |  d  } t j |  d  } | | S(   s*   Calculate the IQR for an array of numbers.i   iK   (   R   R   R   R   (   R-   t   q1t   q3(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   iqrk  s    c          C  ss   d d l  m }  t d  } |  |  } g  | j d i d d 6 D]- } | j j d  rB | j j d d  ^ qB S(	   s?   Report available example datasets, useful for reporting issues.i(   t   BeautifulSoups(   https://github.com/mwaskom/seaborn-data/R-   s   js-navigation-opent   classs   .csvR   (   t   bs4R   R   t   find_allt   textt   endswitht   replace(   R   t   httpt   gh_listR=   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyR   s  s    c         C  sk   |  d k r3 t j j d t j j d d   }  n  t j j |   }  t j j |   sg t j |   n  |  S(   sC  Return the path of the seaborn data directory.

    This is used by the ``load_dataset`` function.

    If the ``data_home`` argument is not specified, the default location
    is ``~/seaborn-data``.

    Alternatively, a different default location can be specified using the
    environment variable ``SEABORN_DATA``.
    t   SEABORN_DATAt   ~s   seaborn-dataN(	   RA   t   ost   environRY   t   patht   joint
   expandusert   existst   makedirs(   t	   data_home(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   get_data_home  s    c         K  sD  d } | j  |   } | rm t j j t |  t j j |   } t j j |  sd t | |  n  | } n  t j	 | |  } | j
 d j   j   r | j
 d  } n  t s | S|  d k rGt j | d d d d d g  | d <t j | d	 d
 d g  | d	 <t j | d d d g  | d <t j | d d d g  | d <n  |  d k ryt j | d | j j    | d <n  |  d k rt j | d d d d g  | d <t j | d d d d g  | d <t j | d d d g  | d <n  |  d k r@t j | d  d! d" d# g  | d  <t j | d$ t d%   | d$ <n  | S(&   s:  Load a dataset from the online repository (requires internet).

    Parameters
    ----------
    name : str
        Name of the dataset (`name`.csv on
        https://github.com/mwaskom/seaborn-data).  You can obtain list of
        available datasets using :func:`get_dataset_names`
    cache : boolean, optional
        If True, then cache data locally and use the cache on subsequent calls
    data_home : string, optional
        The directory in which to cache data. By default, uses ~/seaborn-data/
    kws : dict, optional
        Passed to pandas.read_csv

    sD   https://raw.githubusercontent.com/mwaskom/seaborn-data/master/{}.csvit   tipst   dayt   Thurt   Frit   Satt   Sunt   sext   Malet   Femalet   timet   Luncht   Dinnert   smokert   Yest   Not   flightst   montht   exercises   1 mins   15 mins   30 mint   kindt   restt   walkingt   runningt   diets   no fats   low fatt   titanicR   t   Firstt   Secondt   Thirdt   deckt   ABCDEFG(   t   formatR   R   R   R   t   basenameR   R   R   t   read_csvt   iloct   isnullt   allt   pandas_has_categoricalst   CategoricalR   t   uniqueR@   (   t   namet   cacheR   t   kwsR   t	   full_patht
   cache_pathR   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyR     s8    	&  #&#####c         C  sx   |  s
 t  SyU g  |  D] } | j   ^ q } g  | D] } | j |  ^ q3 } t |  d k SWn t k
 rs t  SXd S(   s   Return a boolean for whether the list of ticklabels have overlaps.

    Parameters
    ----------
    labels : list of ticklabels

    Returns
    -------
    overlap : boolean
        True if any of the labels overlap.

    i   N(   t   Falset   get_window_extentt   count_overlapsRg   t   RuntimeError(   t   labelsR=   t   bboxest   bt   overlaps(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   axis_ticklabels_overlap  s    "c         C  s"   t  |  j    t  |  j    f S(   s   Return booleans for whether the x and y ticklabels on an Axes overlap.

    Parameters
    ----------
    ax : matplotlib Axes

    Returns
    -------
    x_overlap, y_overlap : booleans
        True when the labels on that axis overlap.

    (   R   t   get_xticklabelst   get_yticklabels(   RJ   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   axes_ticklabels_overlap  s    c         C  s   | d k r t |  d  r' |  j } n y |  j j } Wn t t f k
 r y |  j   } Wn  t k
 r t j |   } n Xy, t j	 |   j
 t j  t j |  } Wq t t f k
 r | } q Xn Xt t j |  } n  t |  S(   s  Return a list of unique data values.

    Determine an ordered list of levels in ``values``.

    Parameters
    ----------
    values : list, array, Categorical, or Series
        Vector of "categorical" values
    order : list-like, optional
        Desired order of category levels to override the order determined
        from the ``values`` object.

    Returns
    -------
    order : list
        Ordered list of category levels not including null values.

    t
   categoriesN(   RA   t   hasattrR   t   catR   R   R   R   R   R   t   astypet   floatR   R3   t   filterR   R@   (   t   valuest   order(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   categorical_order  s"    c          C  sT   t  rI t j d }  y g  |  D] } | d ^ q SWqI t k
 rE qI Xn  t j d S(   s@   Return the list of colors in the current matplotlib color cycle.s   axes.prop_cycleR:   s   axes.color_cycle(   t
   mpl_ge_150t   mplt   rcParamst   KeyError(   t   cylR0   (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   get_color_cycle  s    c         C  s   t  j j j |   d d  d d  f } t j | d k | d | d d d  } | j d d	 d
 g  } y | j   SWn t k
 r | SXd S(   s  Calculate the relative luminance of a color according to W3C standards

    Parameters
    ----------
    color : matplotlib color or sequence of matplotlib colors
        Hex code, rgb-tuple, or html color name.

    Returns
    -------
    luminance : float(s) between 0 and 1

    Ni   g#?gףp=
)@g)\(?gzG?g333333@gz6?g,C?g]m{?(	   R   t   colorsR5   t   to_rgba_arrayR   t   wheret   dott   itemR3   (   R:   R<   t   lum(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   relative_luminance,  s    ++c         C  s   t  |  t  r8 y |  j d  SWq8 t k
 r4 |  SXn  y* t  |  t  rN |  S|  j   j d  SWn7 t k
 r t  |  t  r |  j d  S|  j   Sn Xd S(   sp  Return a Unicode string representing a Python object.

    Unicode strings (i.e. type ``unicode`` in Python 2.7 and type ``str`` in
    Python 3.x) are returned unchanged.

    Byte strings (i.e. type ``str`` in Python 2.7 and type ``bytes`` in
    Python 3.x) are returned as UTF-8-encoded strings.

    For other objects, the method ``__str__()`` is called, and the result is
    returned as a UTF-8-encoded string.

    Parameters
    ----------
    obj : object
        Any Python object

    Returns
    -------
    s : unicode (Python 2.7) / str (Python 3.x)
        UTF-8-encoded string representation of ``obj``
    s   utf-8N(   t
   isinstancet   strt   decodeR   t   unicodet   __str__t	   NameErrort   bytes(   t   obj(    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   to_utf8B  s    s   https://google.comc           s>   d d l     d k r%  f d   S    f d   } | S(   s   
    Decorator that will skip a test if `url` is unreachable.

    Parameters
    ----------
    t : function, optional
    url : str, optional
    iNc           s   t  |  d   S(   Nt   url(   t   _network(   R0   (   R  (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   <lambda>  s    c            sQ   y t    } Wn# t t f k
 r5   j    n X| j    |  |   Sd  S(   N(   R   t   IOErrorR   t   SkipTestt   close(   R   R   t   f(   t   noseRS   R  (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   wrapper  s    
(   R  RA   (   RS   R  R  (    (   R  RS   R  s,   lib/python2.7/site-packages/seaborn/utils.pyR  t  s
    		(;   t   __doc__t
   __future__R    R   R7   R   t   numpyR   t   scipyR   t   pandasR   t
   matplotlibR   t   matplotlib.colorsR   R4   t   matplotlib.pyplott   pyplotRD   t!   external.six.moves.urllib.requestR   R   t   external.six.moves.http_clientR   t   distutils.versionR   t   __version__R   R   t   __all__R   R   R*   R2   R   R   RA   R	   RK   t   TrueR   R
   RZ   R   R   R   R   R   R   R   R   R   R   R   R   R   R  R  (    (    (    s,   lib/python2.7/site-packages/seaborn/utils.pyt   <module>   sT   					#		Y		%			:		'			2