B
    &[_QP              
   @   s   d dl mZ d dlmZ d dlmZmZ d dlmZ	 d dl
mZ d dlmZ edZedZed	Zd
d Zd+ddZd,ddZdd Zdd Zdd Zdd Zdd Zd-ddZdd  Zd!d" Zd#d$ Zd.d)d*ZdS )/    )absolute_import)Number)
exceptionsoptional_importsN)
graph_objs)make_subplotsZpandasZnumpyzscipy.statsc             C   s   t | t j}t |}t |}t j|ddd}t j|ddd}t j|ddd}|| }d| }t |||| k }	t |||| k }
||||||	|
d	S )
z6
    Calculate statistics for use in violin plot.
    2   Zlinear)interpolation   lowerK   Zhigherg      ?)minmaxq1q2q3d1d2)npasarrayfloatr   r   Z
percentile)dataxvals_minvals_maxr   r   r   ZiqrZwhisker_distr   r    r   A/tmp/pip-install-l29rncou/plotly/plotly/figure_factory/_violin.py
calc_stats   s"    

r   #1f77b4rgb(0, 0, 0)c                sH    fddt t D }tj dd|d|tjjd|ddd	dd

S )zG
    Produces a sideways probability distribution fig violin plot.
    c                s4   g | ],}d d  |  d d |  d qS )z(pdf(y), y)=(z{:0.2f}z, ))format).0i)r   yr   r   
<listcomp>0   s   z$make_half_violin.<locals>.<listcomp>lines Ztonextxg      ?Zspline)widthcolorshapetext)
r   r$   modenamer+   fill	fillcolorline	hoverinfoZopacity)rangelenr   ScatterscatterLine)r   r$   r/   Z	linecolorr+   r   )r   r$   r   make_half_violin+   s    r7   c          	   C   s4   t j| | | gt|  t jj|dddddddS )z2
    Returns a rugplot fig for a violin plot.
    zline-ew-open)r)   symbolmarkersr'   Fr$   )r$   r   markerr,   r-   
showlegendr1   )r   r4   r3   r5   Marker)valsZpdf_maxdistancer)   r   r   r   make_violin_rugplotB   s    r?   c          	   C   s(   t jddg| |gddt jjddddS )z?
    Returns the scatterplot fig of most of a violin plot.
    r   r'   r&   g      ?z
rgb(0,0,0))r(   r)   )r   r$   r-   r,   r0   )r   r4   r5   r6   )r   r   r   r   r   make_non_outlier_intervalQ   s    r@   c          	   C   sB   t jddg| |gdd|  dd| gdt jjdddd	d
S )z@
    Makes the upper and lower quartiles for a violin plot.
    r   zlower-quartile: z{:0.2f}zupper-quartile: r&      z
rgb(0,0,0))r(   r)   r+   )r   r$   r+   r,   r0   r1   )r   r4   r!   r5   r6   )r   r   r   r   r   make_quartiles^   s    rB   c          	   C   s.   t jdg| gdd|  gdtddddd	S )
z;
    Formats the 'median' hovertext for a violin plot.
    r   zmedian: z{:0.2f}r9   Zsquarezrgb(255,255,255))r8   r)   r+   )r   r$   r+   r,   r:   r1   )r   r4   r!   dict)r   r   r   r   make_mediano   s    
rD   c          
   C   s    t jj| |ddddddd}|S )z-
    Makes the x-axis for a violin plot.
    Fr'   )titler2   showgridzerolineshowlinemirrorticksshowticklabels)r   layoutZXAxis)Zxaxis_titleZxaxis_rangexaxisr   r   r   
make_XAxis}   s    rN   c          
   C   s    t jj| dddddddd}|S )z-
    Makes the y-axis for a violin plot.
    TrA   F)rE   rK   Z	autorangeZticklenrH   rG   rF   rI   )r   rL   ZYAxis)Zyaxis_titleyaxisr   r   r   
make_YAxis   s    rP   Tc             C   s  t | t j} t| d }t| d }t| d }t| d }t| d }t| d }t| d }	t| }
t ||d}|
|}t |}|rd	| d
 nd}| | d |d g}t| ||dt|||dt	||	t
||t|g}|r|t| |||d ||fS )z?
    Refer to FigureFactory.create_violin() for docstring.
    r   r   r   r   r   r   r   d   g       @
   r   g?)r/   )r>   r)   )r   r   r   r   scipy_statsZgaussian_kdeZlinspacer   r7   r@   rB   rD   appendr?   )r=   r/   rugplotr   r   r   r   r   r   r   ZpdfZxxyyZmax_pdfr>   plot_xrange	plot_datar   r   r   
violinplot   s.    


rY   c             C   sB  g }x"| | D ]}||kr| | qW |r4|  | |g}t|}td|dddd}d}xt|D ]\}}t||| tj	}|t|krd}t
||| |d\}}t }x|D ]}||d|d  qW |d7 }|d d	|d t|| |i qhW |d d
dtdi |d j|
ddd||	d |S )zu
    Refer to FigureFactory.create_violin() for docstring.

    Returns fig for violin plot without colorscale.

       Tg?F)rowscolsshared_yaxeshorizontal_spacing
print_gridr   )r/   rU   rL   zxaxis{}zyaxis{}r'   closest)rE   r;   	hovermodeautosizeheightr(   )rT   sortgroupbyr3   r   	enumerater   r   	get_groupr   rY   r   Layoutappend_traceupdater!   rN   rP   )r   data_headergroup_headercolorsuse_colorscalegroup_statsrU   rd   rc   r(   rE   
group_namer-   gbLfigZcolor_indexkgrr=   rX   rW   rL   itemr   r   r   violin_no_colorscale   s@    
"rw   c       !      C   s  g }x"| | D ]}||kr| | qW |r4|  x|D ]}||kr:tdq:W | |g}t|}td|dddd}t|d tj	}t|d tj	}g }x|D ]}| ||  qW t
|}t|}xt|D ]\}}t||| tj}|| | ||  }t|||}t|d||d	\}}t }x |D ]}||d|d  q@W |d
 d|d t|| |i qW tjdgdgdtd||d|d gd|d ggdddd} || d| |d
 ddtdi |d
 j|
ddd||	d |S )zr
    Refer to FigureFactory.create_violin() for docstring.

    Returns fig for violin plot with colorscale.

    zRAll values/groups in the index column must be represented as a key in group_stats.rZ   Tg?F)r[   r\   r]   r^   r_   r   zrgb{})r/   rU   rL   zxaxis{}r9      )sizeZcminZcmaxZ
colorscaleZ	showscale)r   r$   r,   r:   r;   zyaxis{}r'   r`   )rE   r;   ra   rb   rc   r(   )rT   rd   r   PlotlyErrorre   r3   r   clrsZcolor_parserZunlabel_rgbr   r   rf   r   r   rg   r   Zfind_intermediate_colorrY   r!   r   rh   ri   rj   rN   r4   rC   rP   )!r   rk   rl   rm   rn   ro   rU   rd   rc   r(   rE   rp   r-   grouprq   rr   rs   ZlowcolorZ	highcolorZgroup_stats_valueskeyZ	max_valueZ	min_valuert   ru   r=   ZintermedZintermed_colorrX   rW   rL   rv   Ztrace_dummyr   r   r   violin_colorscale	  sj    



"r~   c             C   sF  g }x"| | D ]}||kr| | qW |r4|  x|D ]}||kr:tdq:W | |g}t|}td|dddd}xt|D ]\}}t	|
|| tj}t||| |d\}}t }x|D ]}||d|d  qW |d d	|d t|| |i qW |d d
dtdi |d j|
ddd||	d |S )zu
    Refer to FigureFactory.create_violin() for docstring.

    Returns fig for violin plot without colorscale.

    zMIf colors is a dictionary, all the group names must appear as keys in colors.rZ   Tg?F)r[   r\   r]   r^   r_   )r/   rU   rL   zxaxis{}zyaxis{}r'   r`   )rE   r;   ra   rb   rc   r(   )rT   rd   r   rz   re   r3   r   rf   r   r   rg   r   rY   r   rh   ri   rj   r!   rN   rP   )r   rk   rl   rm   rn   ro   rU   rd   rc   r(   rE   rp   r-   r|   rq   rr   rs   rt   ru   r=   rX   rW   rL   rv   r   r   r   violin_dictp  s>    


"r   F  X  Violin and Rug Plotc             C   s  t |trt|d}nt|d}|dkrt | trjt| dkrNtdt	dd | D sjtdt
rt | t
jjjr|dkrtd| | j } t| |d |d	\}}tj|
d
tjjdd|d
|	td|tddd	}|d td
d
dd tj||d}|S t | t
jjjs*td|dkr>td|d
krt |trtt| |||||||||	|
}|S t| |||||||||	|
}|S ndt |trtdt|dk rtdt |tstdt| |||||||||	|
}|S dS )a}  
    **deprecated**, use instead the plotly.graph_objects trace
    :class:`plotly.graph_objects.Violin`.

    :param (list|array) data: accepts either a list of numerical values,
        a list of dictionaries all with identical keys and at least one
        column of numeric values, or a pandas dataframe with at least one
        column of numbers.
    :param (str) data_header: the header of the data column to be used
        from an inputted pandas dataframe. Not applicable if 'data' is
        a list of numeric values.
    :param (str) group_header: applicable if grouping data by a variable.
        'group_header' must be set to the name of the grouping variable.
    :param (str|tuple|list|dict) colors: either a plotly scale name,
        an rgb or hex color, a color tuple, a list of colors or a
        dictionary. An rgb color is of the form 'rgb(x, y, z)' where
        x, y and z belong to the interval [0, 255] and a color tuple is a
        tuple of the form (a, b, c) where a, b and c belong to [0, 1].
        If colors is a list, it must contain valid color types as its
        members.
    :param (bool) use_colorscale: only applicable if grouping by another
        variable. Will implement a colorscale based on the first 2 colors
        of param colors. This means colors must be a list with at least 2
        colors in it (Plotly colorscales are accepted since they map to a
        list of two rgb colors). Default = False
    :param (dict) group_stats: a dictioanry where each key is a unique
        value from the group_header column in data. Each value must be a
        number and will be used to color the violin plots if a colorscale
        is being used.
    :param (bool) rugplot: determines if a rugplot is draw on violin plot.
        Default = True
    :param (bool) sort: determines if violins are sorted
        alphabetically (True) or by input order (False). Default = False
    :param (float) height: the height of the violin plot.
    :param (float) width: the width of the violin plot.
    :param (str) title: the title of the violin plot.

    Example 1: Single Violin Plot

    >>> from plotly.figure_factory import create_violin
    >>> import plotly.graph_objs as graph_objects

    >>> import numpy as np
    >>> from scipy import stats

    >>> # create list of random values
    >>> data_list = np.random.randn(100)

    >>> # create violin fig
    >>> fig = create_violin(data_list, colors='#604d9e')

    >>> # plot
    >>> fig.show()

    Example 2: Multiple Violin Plots with Qualitative Coloring

    >>> from plotly.figure_factory import create_violin
    >>> import plotly.graph_objs as graph_objects

    >>> import numpy as np
    >>> import pandas as pd
    >>> from scipy import stats

    >>> # create dataframe
    >>> np.random.seed(619517)
    >>> Nr=250
    >>> y = np.random.randn(Nr)
    >>> gr = np.random.choice(list("ABCDE"), Nr)
    >>> norm_params=[(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]

    >>> for i, letter in enumerate("ABCDE"):
    ...     y[gr == letter] *=norm_params[i][1]+ norm_params[i][0]
    >>> df = pd.DataFrame(dict(Score=y, Group=gr))

    >>> # create violin fig
    >>> fig = create_violin(df, data_header='Score', group_header='Group',
    ...                    sort=True, height=600, width=1000)

    >>> # plot
    >>> fig.show()

    Example 3: Violin Plots with Colorscale

    >>> from plotly.figure_factory import create_violin
    >>> import plotly.graph_objs as graph_objects

    >>> import numpy as np
    >>> import pandas as pd
    >>> from scipy import stats

    >>> # create dataframe
    >>> np.random.seed(619517)
    >>> Nr=250
    >>> y = np.random.randn(Nr)
    >>> gr = np.random.choice(list("ABCDE"), Nr)
    >>> norm_params=[(0, 1.2), (0.7, 1), (-0.5, 1.4), (0.3, 1), (0.8, 0.9)]

    >>> for i, letter in enumerate("ABCDE"):
    ...     y[gr == letter] *=norm_params[i][1]+ norm_params[i][0]
    >>> df = pd.DataFrame(dict(Score=y, Group=gr))

    >>> # define header params
    >>> data_header = 'Score'
    >>> group_header = 'Group'

    >>> # make groupby object with pandas
    >>> group_stats = {}
    >>> groupby_data = df.groupby([group_header])

    >>> for group in "ABCDE":
    ...     data_from_group = groupby_data.get_group(group)[data_header]
    ...     # take a stat of the grouped data
    ...     stat = np.median(data_from_group)
    ...     # add to dictionary
    ...     group_stats[group] = stat

    >>> # create violin fig
    >>> fig = create_violin(df, data_header='Score', group_header='Group',
    ...                     height=600, width=1000, use_colorscale=True,
    ...                     group_stats=group_stats)

    >>> # plot
    >>> fig.show()
    ZrgbNr   zRIf data is a list, it must be nonempty and contain either numbers or dictionaries.c             s   s   | ]}t |tV  qd S )N)
isinstancer   )r"   elementr   r   r   	<genexpr>R  s    z create_violin.<locals>.<genexpr>z0If data is a list, it must contain only numbers.zVdata_header must be the column name with the desired numeric data for the violin plot.)r/   rU   F   )ry   r'   r`   )	rE   rb   fontrc   r;   r(   rM   rO   ra   rO   )rH   rK   rJ   )r   rL   zGError. You must use a pandas DataFrame if you are using a group header.zFThe colors param cannot be a dictionary if you are using a colorscale.rx   zHcolors must be a list with at least 2 colors. A Plotly scale is allowed.z,Your group_stats param must be a dictionary.)r   rC   r{   Zvalidate_colors_dictZvalidate_colorslistr3   r   rz   allpdcoreframeZ	DataFramevaluestolistrY   r   rh   rL   ZFontrN   rP   rj   ZFigurer   rw   r~   )r   rk   rl   rm   rn   ro   rU   rd   rc   r(   rE   Zvalid_colorsrX   rW   rL   rs   r   r   r   create_violin  s     




r   )r   r   )r   )r   T)
NNNFNTFr   r   r   )
__future__r   Znumbersr   Zplotlyr   r   Zplotly.colorsrm   r{   Zplotly.graph_objsr   Zplotly.subplotsr   Z
get_moduler   r   rS   r   r7   r?   r@   rB   rD   rN   rP   rY   rw   r~   r   r   r   r   r   r   <module>   s<   





'CgJ         