B
    &[_YM                @   s  d dl 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 d dlmZmZ d dlZd dlZd dlZd dlmZmZmZ dZd	d
ddddddddddgddddddg dddddd g d!d"d#d$d%g Zd&d'd(d)d*gZd+d,d-d.gZd/d0d1gZee e e Z ej!ej"ej#ej$ej%ej&gZ'e'ej(ej)ej*g7 Z'G d2d3 d3e+Z,e, Z-[,da.d4d5 Z/d6d7 Z0ed8d9d:d;d<d=d>d?gZ1ed@dAdBdCdDgZ2dEdF Z3dGdH Z4dIdJ Z5dKdL Z6dMdN Z7dOdP Z8dQdR Z9dSdT Z:dUdV Z;dWdX Z<dYdZ Z=d[d\ Z>d]d^ Z?d_d` Z@dadb ZAdcdd ZBdedf ZCdgdh ZDdidj ZEdkdl ZFdmdn ZGdodp ZHdqdr ZIdsdt ZJdudv ZKdwdx ZLdydz ZMd{d| ZNd}d~ ZOdd ZPdd ZQdd ZRdddZSdd ZTdS )    N)
namedtupleOrderedDict   )IdentityMapConstantRange)ColorscaleValidator)qualitative
sequential)make_subplots_set_trace_grid_reference_subplot_type_for_trace_typeZpx_no_color_constantbasexyzabcrthetasizex_startx_end
hover_nametextnamesvaluesparents
wide_crossidsZerror_xZerror_x_minusZerror_yZerror_y_minusZerror_zZerror_z_minuslatlon	locationsanimation_group
dimensionscustom_data
hover_datapathwide_variableanimation_frame	facet_row	facet_col
line_groupcolorsymbol	line_dashc               @   s(   e Zd ZddddddddgZd	d
 ZdS )
PxDefaultstemplatewidthheightcolor_discrete_sequencecolor_continuous_scalesymbol_sequenceline_dash_sequencesize_maxc             C   s4   d | _ d | _d | _d | _d | _d | _d | _d| _d S )N   )r2   r3   r4   r5   r6   r7   r8   r9   )self r<   8/tmp/pip-install-l29rncou/plotly/plotly/express/_core.py__init__6   s    zPxDefaults.__init__N)__name__
__module____qualname__	__slots__r>   r<   r<   r<   r=   r1   *   s   r1   c             C   s   | a dS )z
    Arguments:
        token: A Mapbox token to be used in `plotly.express.scatter_mapbox` and         `plotly.express.line_mapbox` figures. See         https://docs.mapbox.com/help/how-mapbox-works/access-tokens/ for more details
    N)MAPBOX_TOKEN)tokenr<   r<   r=   set_mapbox_access_tokenH   s    rE   c             C   s   | j S )a  
    Extracts fit statistics for trendlines (when applied to figures generated with
    the `trendline` argument set to `"ols"`).

    Arguments:
        fig: the output of a `plotly.express` charting call
    Returns:
        A `pandas.DataFrame` with a column "px_fit_results" containing the `statsmodels`
        results objects, along with columns identifying the subset of the data the
        trendline was fit on.
    )_px_trendlines)figr<   r<   r=   get_trendline_resultsS   s    rH   Mappingshow_in_trace_namegrouperval_mapsequenceupdatervariablefacet	TraceSpecconstructorattrstrace_patchmarginalc             C   s&   y| d | S  t k
r    |S X d S )Nlabels)	Exception)argscolumnr<   r<   r=   	get_labelq   s    rZ   c             C   s8   dd | d   D }y|| S  tk
r2   |S X dS )zInvert mapping.
    Find key corresponding to value column in dict args["labels"].
    Returns `column` if the value does not exist.
    c             S   s   i | ]\}}||qS r<   r<   ).0keyvaluer<   r<   r=   
<dictcomp>}   s    z invert_label.<locals>.<dictcomp>rV   N)itemsrW   )rX   rY   Zreversed_labelsr<   r<   r=   invert_labelx   s
    r`   c             C   s   | | j jdkS )NZifc)dtypekind)dfcol_namer<   r<   r=   _is_continuous   s    re   c             C   sx   t | |}d| krp|dkr.d| kr.| d dksR|dkrJd| krJ| d dksR|dkrp|rjd| d pbd	|f S d	S n|S d S )
Nhistfuncr   orientationhr   vr   z%s of %scount)rZ   )rX   rY   rolelabelr<   r<   r=   get_decorated_label   s    
rm   c          	      s  dksdkr0t d|  i dgdd d dS dks@d	krd	krLd
nd}t d||  i dd tddD dd dkrdnddS d\ }}dkrd}dkrd}d}| |d  dkrt }n| |d   }t d| | || |d   fddd dS )Nr-   r*   F c             S   s   |S )Nr<   )traceri   r<   r<   r=   <lambda>       zmake_mapping.<locals>.<lambda>)rJ   rK   rL   rM   rO   rN   rP   r+   r,   r   r   c             S   s   g | ]}|qS r<   r<   )r[   ir<   r<   r=   
<listcomp>   s    z make_mapping.<locals>.<listcomp>r   i  c             S   s   |S )Nr<   )ro   ri   r<   r<   r=   rp      rq   rowcol)rJ   rO   rK   rL   rM   rN   rP   .r.   Zcolor_discretedashr0   _mapidentityTZ	_sequencec                s   |   |iiS )N)update)ro   ri   )parentrO   r<   r=   rp      rq   )rI   rangesplitr   copy)rX   rO   letterZvprefixarg_namerL   r<   )r{   rO   r=   make_mapping   sL    
r   c       #         s	  d kr  d r | |jd }j p,i }d}d}xjD ]} | t |}	|dkrЇ fdd| D }
 fdd|
D |d< jtj	krx|d D ]}t
d	d
|d< qW d|d< d|d< q@dk	s
jtjkr|dks
jtjtjgkr@|dkr@|dkrZd|kr(t
 |d< | |d d< d|d d< ||d d< d||	< q@|dkr|jtjkr d|d< q@|dkrjtjkr d|d< q@|dkrdkr  d r  d r t| d  d g  dkr ddlm} |j d d }| d  j}| d  j}|jjtjkr@|td! }nH|jjtjkry|tj}W n& tk
r   td" d  Y nX |jjtjkry|tj}W n tk
r   td#Y nX | d  ttt|t| |d< d$kr,|j j!||d%d&}|dddf |d< d'}nd(kr|j"||#|d%d&$ }|% |d< d)}t|j&d*kr|d+ d |j&d  d |j&d f 7 }n|d, d |j&d f 7 }|d-|j' 7 }d|t(  d < d.|t(  d < q@|)d/r8|dd0 }|*d1rd2nd3}||kr&i ||< | || |< q@|d4krP| |d5< q@|d6krjtjtjtjgkr | |d7< |dkr d8}q@|d9krVjtjtjtjgkr t+t
} ,d4pg }x|D ]t}|r| sqy d4 -|}W n. tt.t/fk
r(   t|}| | Y nX t |d}d:| ||< qW || |d5< q@|d;kr؈jtj0tj1gkr| |d< d<|d=< d>||	< q jtj2tj3tj4tj5gkr~d|krt
 |d<  ,d?r| |d d@< d<|d d=< dA||	< ng |d d@<  dB dk	r dB  }ni }x| D ]L}|,|dkr` dC t|t dC   ||< |d d@  ||  q,W nXd}jtj6tj7gkrdD}||krt
 ||< | || d;< d<|| d=< dE| ||	< q@|dFkr| |dG< q@|dHkr| ||< dI||	< q@|dJkrB| ||< |	dJkr4dKn|	}dL||< q@|dMkrt| ||< |	dMkrfdNn|	}dO||< q@|dGkr| ||< |	dGkrdPn|	}dQ||< q@|dRkrjtj2tj3tj4tj5gkr| |dS< |	dRkrdTn|	}dU||< n| ||< q@r| ||< dV| ||	< q@W jtj7tj6gk	r t8|} d9 rt+ d9 t
rxp|9 D ]d\}}t: |}| d9 krd d9 | d } | rt+| t;r|<dWdX|  ||< n
|=|}!qdW dYd |9 D }"|dZ>|" |d[< |d[  d\7  < ||fS )]a:  Populates a dict with arguments to update trace

    Parameters
    ----------
    args : dict
        args to be used for the trace
    trace_spec : NamedTuple
        which kind of trace to be used (has constructor, marginal etc.
        attributes)
    trace_data : pandas DataFrame
        data
    mapping_labels : dict
        to be used for hovertemplate
    sizeref : float
        marker sizeref

    Returns
    -------
    trace_patch : dict
        dict to be used to update trace
    fit_results : dict
        fit information to be used for trendlines
    Z
line_closer   Nrn   r%   c                sv   g | ]n\}}r|krj tjks2t d  |rj tjksjdk	rN|ksjt d  |   d kr||fqS )
data_frameNZdimensions_max_cardinality)rR   go	Parcoordsre   Parcatslenunique)r[   namerY   )rX   
attr_value
trace_specr<   r=   rs      s   z%make_trace_kwargs.<locals>.<listcomp>c                s"   g | ]\}}t t ||d qS ))rl   r   )dictrZ   )r[   r   rY   )rX   r<   r=   rs      s   T)matchesaxisz%{x}z%{xaxis.title.text}z%{y}z%{yaxis.title.text})r   r   r   r   markerareaZsizemodesizerefz%{marker.size}
marginal_xrj   
marginal_y	trendline)olslowessr   r   r   )byi ʚ;zCould not convert value of 'x' ('%s') into a numeric type. If 'x' contains stringified dates, please convert to a datetime column.z3Could not convert value of 'y' into a numeric type.r   drop)missingz<b>LOWESS trendline</b><br><br>r   z<b>OLS trendline</b><br>   z%s = %g * %s + %g<br>z%s = %g<br>zR<sup>2</sup>=%f<br><br>z%{y} <b>(trend)</b>error   minusZ
arrayminusarrayr&   Z
customdatar   Z	hovertextz<b>%{hovertext}</b><br><br>r'   z%%{customdata[%d]}r.   
coloraxis1	coloraxisz%{z}color_is_continuouscolorsz%{color}Zcolor_discrete_mapr5   linez%%{%s.color}r$   r    r#   z%{location}r   r]   z%{value}r   r{   z	%{parent}idz%{id}r   rV   rl   z%{label}z%%{%s}}z%s}c             S   s   g | ]\}}|d  | qS )=r<   )r[   kri   r<   r<   r=   rs     s    z<br>Zhovertemplatez<extra></extra>)?appendilocrT   r~   rS   rm   	iteritemsrR   r   Splomr   	HistogramHistogram2dHistogram2dContourr   ZdropnaZstatsmodels.apiapisort_valuesr   ra   typenpZ
datetime64astypeintZobject_Zfloat64
ValueErrorZlogical_not
logical_orisnanZnonparametricr   ZOLSZadd_constantZfitZpredictparamsZrsquaredrZ   
startswithendswith
isinstancegetindexAttributeErrorKeyError
ChoroplethChoroplethmapboxSunburstTreemapPie
Funnelarear   r   r   r_   r`   strreplacepopjoin)#rX   r   Z
trace_datamapping_labelsr   rT   fit_resultsZhover_header	attr_nameZ
attr_labelZdimsdsmZsorted_trace_datar   r   r   Zerror_xyZarrZhover_is_dictZcustomdata_colsru   positionZattr_label_colmappingcatZ	colorableZ_labelZmapping_labels_copyr   ri   Zk_args	formatter_Zhover_linesr<   )rX   r   r   r=   make_trace_kwargs   s   














"
$


























r   c             C   sr   t jtt jtt jtt jtt jtt j	t
t jt
t jt
t jtt jti
}xtD ]}t||< qFW ||krn|| | || d S )N)r   Z	Scatter3dconfigure_3d_axesZScatterternaryconfigure_ternary_axesScatterpolarconfigure_polar_axesScatterpolarglZBarpolarScattermapboxconfigure_mapboxr   DensitymapboxZ
Scattergeoconfigure_geor   
cartesiansconfigure_cartesian_axes)rX   rR   rG   ordersZconfiguratorsr   r<   r<   r=   configure_axes  s    

r   c             C   s   d| }d| }|| krP| | rPd|d< || krl| | rldd | | D |d< n|| krl| | rl| | |d< | | |krd|d	< t |tjjr|| |  ntt|| |  |d
< d S )Nlog_range_logr   c             S   s   g | ]}t |d qS )
   )mathr   )r[   r   r<   r<   r=   rs     s    z+set_cartesian_axis_opts.<locals>.<listcomp>r|   r   categoryordercategoryarray)r   r   layoutZXAxislistreversed)rX   r   r   r   Zlog_keyZ	range_keyr<   r<   r=   set_cartesian_axis_opts  s    r   c             C   s  d| d | d gkrd|j d< t|j}t|jd }x"|jddD ]}t| |d	| qDW x"|jdd
D ]}t| |d| qhW | d r|jdddd |d | d j jjd kr|j| d dk|d | d j j	jd kr|j
d|d | d rL|j
dddd |d | d j j	jd kr*|j
| d dk|d | d j jjd krL|jd|d t| | d	 d	}| d rx|j|ddd n*x(td|d D ]}|j||dd qW t| | d d}	| d r|j
|	ddd n*x(td|d D ]}
|j
|	d|
d qW d| kr| d r|j
dd d| kr8| d r8|jdd d	t|d  }| d r~x*td|d dD ]}|j|d |d qdW | d rx*td|d dD ]}
|j
dd |
d qW d S )N	histogramr   r   overlaybarmoder   r   )ru   r   )rt   r   Frn   )showticklabelsshowlineticksr|   rt   r2   )showgridrt   T)r   r   r   r|   ru   )r   ru   )
title_textrt   ru   log_xr   )r   log_yr   )r   r   rt   x2)r   r   ru   )r   r   	_grid_refselect_yaxesr   select_xaxesupdate_yaxesyaxisr   xaxisupdate_xaxesrm   r|   r   )rX   rG   r   nrowsncolsr   r   y_titlert   x_titleru   Z	matches_yr<   r<   r=   !configure_cartesian_marginal_axes  sV    






r  c             C   s  d| kr| d s d| kr0| d r0t | || d S t| | d d}x.|jddD ]}|j|d t| |d| qNW t| | d d}x6|jddD ]&}d	| kr|j|d t| |d| qW d
| kr| d
 r|jdd d| kr| d r|jdd d	| kr|jdd |jS )Nr   r   r   r   )ru   )r   r   )rt   is_timeliner   r   )r   r   date)	r  rm   r   rz   r   r   r   r   r   )rX   rG   r   r   r   r  r   r<   r<   r=   r   L  s(    
r   c          	   C   sJ   |j ttt| | d dtt| | d dtt| | d ddd d S )Nr   )r   r   r   )ZaaxisZbaxisZcaxis)Zternary)update_layoutr   rZ   )rX   rG   r   r<   r<   r=   r   n  s
    r   c             C   s   t t t | d | d dt  dd}xDdD ]<\}}| | |kr*d|d | d	< || |  |d | d
< q*W |d d }| d rd|d< | d rdd | d D |d< n| d r| d |d< | d r| d |d d< |j|d d S )N	directionZstart_angle)r  Zrotation)angularaxis
radialaxis)polar))r   r  )r   r  r   r	  r   r   r  Zlog_rr   r   Zrange_rc             S   s   g | ]}t |d qS )r   )r   r   )r[   r   r<   r<   r=   rs     s    z(configure_polar_axes.<locals>.<listcomp>r|   Zrange_thetaZsector)r   )r   rz   )rX   rG   r   r   varr   r  r<   r<   r=   r   x  s$    r   c          	   C   s   t t t t| | d dt t| | d dt t| | d ddd}xdD ]}|d |d	  }| d
|  rd|d< | d|  rdd | d|  D |d< n| d|  r| d|  |d< | | |krJd|d< || |  |d< qJW |j|d d S )Nr   )r   r   r   )r   r   Zzaxis)scene)r   r   r   r  r   r   r   r   r   c             S   s   g | ]}t |d qS )r   )r   r   )r[   r   r<   r<   r=   rs     s    z%configure_3d_axes.<locals>.<listcomp>r|   r   r   r   )r   )r   rZ   rz   )rX   rG   r   r   r   r   r<   r<   r=   r     s"    
r   c             C   sl   | d }|sHd| krHd| krHt | d | d   | d | d   d}|jt t|| d | d dd	 d S )
Ncenterr!   r"   r   )r!   r"   zoomZmapbox_style)Zaccesstokenr  r  style)Zmapbox)r   Zmeanr  rC   )rX   rG   r   r  r<   r<   r=   r     s    r   c             C   s,   |j t| d | d t| d ddd d S )Nr  scope
projection)r   )r  r  r  )Zgeo)r  r   )rX   rG   r   r<   r<   r=   r     s
    r   c                s    fddd| kr| d rt |jdkrd dgdddd gd	gd
ddgddddddddd	dd	g|j_d	dddt| | d d idddddd	fdd|jD d	g|j_d S )Nc                s    |  t jkddd| dddS )N)durationZredrawZ	immediateTZlinear)r  Zeasing)framemodeZfromcurrentZ
transition)r   Scatter)r  )rR   r<   r=   
frame_args  s    z0configure_animation_controls.<locals>.frame_argsr*   r   i  z&#9654;animate)rX   rl   methodr   z&#9724;leftr   F   )r   tFbuttonsg?righttop)	r  r  padZ
showactiver   r   xanchorr   yanchorprefixr   <   )r   r  g?c                s&   g | ]}|j g d g|j ddqS )r   r  )rX   rl   r  )r   )r[   f)r  r<   r=   rs     s   z0configure_animation_controls.<locals>.<listcomp>)	activer   r  Zcurrentvaluer  r   r   r   Zsteps)r   framesr   ZupdatemenusrZ   Zsliders)rX   rR   rG   r<   )rR   r  r=   configure_animation_controls  s4    

r&  c       
      C   s  |t jt jgkrrd| krr| d dksL| d dkrrt| d dkrr| d d krr|t jkrlt j}d|krr|d= nt j}t|||d g}xdD ]}d	| | kr| d	|  rd }t|d
krdnd|dkrdndd}| d	|  dkrtt j|d	| gtf d|d||d}n| d	|  dkr@tt j	|ddgt|d|d}n| d	|  dkrrtt j
|ddgtdd|d}nP| d	|  dkrddd}tt j
|ddgtdd did!d"d#d$|| id%|d}d |ksd | krd&|jkrt |jd&< | d' d" }	|	|jd& d < || qW d(| kr~| d( r~t|t jkr>t jnt jd(gtd)d*d d}| d+ rtt| d+ d,|jd-< || |S ).NZrender_modeZwebglautor   i  r*   rg   )r   r   Z	marginal_r   x1r   r   y1y2)r   r   r   g      ?)opacitybingroup)rR   rS   rT   rU   Zviolinr   r'   )Z
scalegroupZboxT)ZnotchedZrugzline-ns-openzline-ew-openzrgba(255,255,255,0)r.   allr   Zpointsr/   )Z	fillcolorr   Z	boxpointsjitterZhoveronr   r   r6   r   lines)r  Ztrendline_color_override)r.   r   )r   r  r   r   	Scatterglr   rQ   r   r   ViolinBoxrT   r   )
rX   rR   rS   rT   resultr   r   Zaxis_mapsymbolsZfirst_default_colorr<   r<   r=   make_trace_spec   s    







r5  c             C   s   dS )Nrn   r<   )r   r<   r<   r=   	one_groupV  s    r6  c             C   s  xFddgdddg dddg D ]&}|| kr| | d krt t|| |< qW | d d krvtjjd k	rntjj| d< nd	| d< ytj| d  | d< W n( tk
r   tj| d | d< Y nX d| kr| d d kr| d jj	j
rd
d | d jj	j
D | d< | d d krt
j| d< d| kr\| d d krD| d jjrD| d jj| d< | d d kr\tj| d< d| kr| d d kr| d jjrdd | d jjD | d< | d rt| d sdddddg| d< d| kr2| d d kr| d jjrdd | d jjD | d< | d rt| d s2ddddddg| d< | dd d k	rZ| dd rZd | d< | dd d k	r| dd rd | d< d S )Nr5   r6   r7   r8   r2   r3   r4   r9   Zplotlyc             S   s   g | ]}|d  qS )r   r<   )r[   r   r<   r<   r=   rs   {  s    z)apply_default_cascade.<locals>.<listcomp>c             S   s   g | ]}|j jqS r<   )r   r/   )r[   scatterr<   r<   r=   rs     s    ZcircleZdiamondZsquarer   Zcrossc             S   s   g | ]}|j jqS r<   )r   rw   )r[   r7  r<   r<   r=   rs     s    Zsoliddotrw   ZlongdashZdashdotZlongdashdotr,   r   r+   r   )getattrdefaultspioZ	templatesdefaultrW   r   r   Template
colorscaler
   ZViridisZcolorwayr	   ZD3datar7  anyr   )rX   paramr<   r<   r=   apply_default_cascadeZ  sT     






  rB  c             C   s    | |kr| S t d| | f d S )NzaA name conflict was encountered for argument '%s'. A column or index with name '%s' is ambiguous.)	NameError)
field_namereserved_namesr<   r<   r=   _check_name_not_reserved  s
    rF  c             C   s   | d }t  }x| D ]}|tkr"q|tkr2| | n| | g}|dkrFqx|D ]}|dkr\qLqLt|trr|| qLt|tjr|j}|rt	||r||| k}|r|| qL||j
krL|jdk	rL||j qLW qW |S )z
    This function builds a list of columns of the data_frame argument used
    as arguments, either as str/int arguments or given as columns
    (pandas series type).
    r   N)setall_attrablesarray_attrablesr   r   addpdSeriesr   hasattrr   )rX   rc   rE  fieldr   argr   Zin_dfr<   r<   r=   _get_reserved_col_names  s,    


rP  c          	   C   s   |dkst |tst |tr dS t |tjr0dS yt| W n tk
rP   dS X xZ|D ]R}t |tspt |tr| dks|| jkrdS qXyt| W qX tk
r   dS X qXW dS )zReturns True if arg looks like it's a list of columns or references to columns
    in df_input, and False otherwise (in which case it's assumed to be a single column
    or reference to a column).
    NFT)r   r   r   rK  
MultiIndexiter	TypeErrorcolumns)df_inputrO  r   r<   r<   r=   _is_col_list  s"    

rV  c             C   s>   t | ts2t | tr t| dks2t | ts2| dkr6dS dS dS )zReturns True if x is an iterable which can be transformed into a pandas Series,
    False for the other types of possible values of a `hover_data` dict.
    A tuple of length 2 is a special case corresponding to a (format, data) tuple.
    r   NFT)r   r   tupler   bool)r   r<   r<   r=   _isinstance_listlike  s    

rY  c             C   s,   x&| d k	r&|| j ks||kr&d| }qW |S )Nr   )rT  )rU  rd   extrar<   r<   r=   _escape_col_name  s    r[  c             C   s   t | jddS )aU  
    assuming x is list-like or even an existing pd.Series, return a new pd.Series with
    no index, without extracting the data from an existing Series via numpy, which
    seems to mangle datetime columns. Stripping the index from existing pd.Series is
    required to get things to match up right in the new DataFrame we're building
    T)r   )rK  rL  reset_index)r   r<   r<   r=   to_unindexed_series  s    r]  c          	      s<  | d }|dk	}t  }t }t }t }	|r6t| nt }
d| krn| d dkrn|s^tdn||j ||j< d| ko| d ot| d t}|r"x| d D ]}t	| d | rd| d | f| d |< t| d | t
s| d | df| d |< |r| d | d dk	r||krtd| qW xtD ]ȉ  tkrF|  gn|  }|dks*|dgkrjq* tkrz gn fd	d
tt|D }xZtt||D ]F\}\}}t|}|dkrʐqd}t|t jrtd| t|ts t|trFt|jdk	rt|jn||
}t|tr8|j||< n
|| nBt|ts^t|ds dkr|r| d t| d dk	rt|}| d | d }|rt||krtd|t|tt|j|f t|||< n|std| n||jkrR|r|||fkrqn2d|tt|j|f }|dkrH|d7 }t|nX|rt|| |krtd|t|| tt|j|f nt|}t|| ||< n|r.t|dr.||jkr |jdks|j|krd}n|j}t||||g}n.|jdk	r.|j|kr.|||j kr.|j}|dkrBt||
}|rxt||krxtd|t|tt|j|f t||t|< |dk	st d tkrt||  < n"t|   trnt||   |<  dkr|	!t| qW q*W x|D ]}tt|||< qW x|D ]}|| ||< qW ||	fS )ac  
    After this function runs, the `all_attrables` keys of `args` all contain only
    references to columns of `df_output`. This function handles the extraction of data
    from `args["attrable"]` and column-name-generation as appropriate, and adds the
    data to `df_output` and then replaces `args["attrable"]` with the appropriate
    reference.
    r   Nr%   zjNo data were provided. Please provide data either with the `data_frame` or with the `dimensions` argument.r'   Tr   zIAmbiguous input: values for '%s' appear both in hover_data and data_framec                s   g | ]} d  t | qS )r   )r   )r[   rr   )rD  r<   r=   rs   F  s    z/process_args_into_dataframe.<locals>.<listcomp>ziArgument '%s' is a pandas MultiIndex. pandas MultiIndex is not supported by plotly express at the moment.__len__zAll arguments should have the same length. The length of hover_data key `%s` is %d, whereas the length of previously-processed arguments %s is %dzString or int arguments are only possible when a DataFrame or an array is provided in the `data_frame` argument. No DataFrame was provided, but argument '%s' is of type str or int.z^Value of '%s' is not the name of a column in 'data_frame'. Expected one of %s but received: %sr   z6
 To use the index, pass it in directly as `df.index`.zAll arguments should have the same length. The length of column argument `df[%s]` is %d, whereas the length of  previously-processed arguments %s is %dr   zAll arguments should have the same length. The length of argument `%s` is %d, whereas the length of  previously-processed arguments %s is %dzData-frame processing failure, likely due to a internal bug. Please report this to https://github.com/plotly/plotly.py/issues/new and we will try to replicate and fix it.r)   )"rK  	DataFramer   r   rG  rP  r   rT  r   rY  rW  rH  rI  r   r|   r   	enumerateziprQ  rS  r   r   rF  rl   r   r]   r   rM  r]  r   r   r[  AssertionErrorrJ  )rX   	wide_modevar_name
value_namerU  df_provided	df_output	constantsrangeswide_id_varsrE  Zhover_data_is_dictr   Zargument_listZ
field_listrr   argumentrN  lengthrd   Zreal_argumenterr_msgr<   )rD  r=   process_args_into_dataframe	  s    	 
 












rn  c                s2  xJ| D ]B}|t kr| | dk	rt| | tr8t| | n
t| | | |< qW | d dk	}|r~t| d tjs~t| d | d< | d }| dddk}| dddk}|rdnt|| d }|rdnt|| d }d}	d}
d}d}tj	tj
g}|tkr|r|rtd|r|r|rd}	t|jtjr:tdt|j| d	< |jj}
|
d
ksd|
|krhd}
|tjkr| ddpd}n| ddpd}|| d< d| d< n||krd}	|r| d n| d | d	< |r| d	 |jkr|jj}
t| d	 tjrt| d	 | d	< |
d
ks.|r2|
|kr2d}
|tjkrN|rHdnd}n|rXdnd}d| |rjdnd< d| d< |s|s|rdnd}|	rt|dg }t||
g }
d}|tjtjtjg| kr|	sJ||krJxldD ]d}| |ddkr|r|jnt | |< |tjkr |}n$| d dkr|dkr<dnd| d< qW |	r|dkr||kr| d dkr|r|dnd| d< |rt|jtjrtd|j| d< ntt|d|
|gd| d< d}t| ddtkr| d tkrd}d| d< t| |	|
|\} t|d|
|g}|	s|r|tjkr|dkrDdnd}t|| | sj|| |< d||< n$| d dkr|dkrdnd| d< ||kr| d= |	r fdd| d	 D }| d	= |dkr| d }n|dkr| d }n| d }| d= d}xN|D ]F}|| jj}|dkrdn|}|dkr2|}n||kr tdq W |j  ||
|d}t!|jt!t"|jks~t#d ||
 $t||
< |dk}|tjtjg| kr|| |rdnd< || |rdnd< |tj	kr| d p|
| d< d!| kr| d! p|
| d!< |tjkrt||r\|| |r2dnd< || |rDdnd< | d pT|
| d< n>|| |rjdnd< || |r|dnd< d||< | d p|
| d< |tj%tj&gkr|p|
| |rdnd< || |rdnd< |tjkr|| |rdnd< || |rdnd< | d p|
| d< |r&d| d< || d< | S )"a  
    Constructs a dataframe and modifies `args` in-place.

    The argument values in `args` can be either strings corresponding to
    existing columns of a dataframe, or data arrays (lists, numpy arrays,
    pandas columns, series).

    Parameters
    ----------
    args : OrderedDict
        arguments passed to the px function and subsequently modified
    constructor : graph_object trace class
        the trace type selected for this figure
    Nr   r   r   FzPCannot accept list of column references or list of columns for both `x` and `y`.TznData frame columns is a pandas MultiIndex. pandas MultiIndex is not supported by plotly express at the moment.r)   )Nr]   r   rO   rg   rh   ri   r   Z__x__Z__y__r]   )r   r   zlData frame index is a pandas MultiIndex. pandas MultiIndex is not supported by plotly express at the moment.r   )rl   r.   rj   r   c                s   g | ]}| kr|qS r<   r<   )r[   r   )rj  r<   r=   rs   m  s    z#build_dataframe.<locals>.<listcomp>)rr   r#  unumberzLPlotly Express cannot process wide-form data with columns of different type.)Zid_varsZ
value_varsrd  re  zWide-mode name-inference failure, likely due to a internal bug. Please report this to https://github.com/plotly/plotly.py/issues/new and we will try to replicate and fix it.r-   )'rI  r   r   r   rK  r_  r   rV  r   r   r   r   r   rT  rQ  rS  r   FunnelZIndexr   r[  r  Barr   r   r   r   NO_COLORrn  re   ra   rb   Zmeltr   rG  rb  r   r1  r2  )rX   rR   rN  rf  rU  Zno_xZno_yZwide_xZwide_yrc  rd  Zwide_cross_namere  Zhist2d_typesZwide_orientationZmissing_bar_dimZaxno_colorrg  Z
count_nameZ	other_dimZwide_value_varsra   ri   Zv_dtypeZorient_vr<   )rj  r=   build_dataframe  s"   





$









ru  c             C   s   | j t| jd}| }|t}t|jddj	d }xJ|D ]B}|j
| }t|j	d d }||d   sBtd|j
| qBW d||< t|jdd dd}xNt|d d	 D ]:\}}||d  |kr|d |krtd
|j
|d  dqW d S )N)r   r   )r   r   z*None entries cannot have not-None childrenrn   c             S   s
   d | S )Nrn   )r   )r   r<   r<   r=   rp     rq   z-_check_dataframe_all_leaves.<locals>.<lambda>z4Non-leaves rows are not permitted in the dataframe 
zis not a leaf.)r   r   rT  Zisnullr   r   r   Znonzeror@  r   r   r-  r   applyr`  )rc   Z	df_sortedZ	null_maskZnull_indicesZnull_row_indexrt   rr   Zrow_stringsr<   r<   r=   _check_dataframe_all_leaves  s&    


rx  c                sB  | d | d ddd }t |ddd   d}g }x*|D ]"}|d }|| | |< q>W |}dd }i }d}| d	 ryt| d	  | d	 < W n$ tk
r   td
| d	  Y nX | d r| d | d	 kr| d	 d }	| d	  |	< |	| d	< | d	  n:djkr dnddd tjD  d <  | d	< d| < | d rt| d sv|}d}n fdd}
|
}||| d < tt	j
|}x |D ]}||kr|||< qW tt	|t	dddg }tjdddg| d}x&|D ]}|| | j||< q W x2t|D ]$\}}tj|jd}||d |}| }||  t|d< d|d< ||  t|d< |t|d k r|d }xh|t|k r|||   td |d  |d< |||   td |d  |d< |d7 }qW |d jd|d< |rD|| ||< |j|dd}q.W | d r|rd}x||jkr|d7 }qnW | d  t||< |j|d}|| d< d| d< d| d< d| d < d| d!< | d r>| d" s| d g| d"< nHt| d" tr,| d" | d s>d#| d" | d < n| d" | d  | S )$zU
    Build dataframe for sunburst or treemap when the path argument is provided.
    r   r(   Nrv  FZ
_path_copyc             S   s$   |   }t|dkr|d S dS d S )Nr   r   z(?))r   r   )r   uniquesr<   r<   r=   aggfunc_discrete  s    z5process_dataframe_hierarchy.<locals>.aggfunc_discreter   zDColumn `%s` of `df` could not be converted to a numerical data type.r.   Z_sumrj   rn   c             S   s   g | ]}t |qS r<   )r   )r[   elr<   r<   r=   rs     s    z/process_dataframe_hierarchy.<locals>.<listcomp>r   sumTc                s   t j| j| j f dS )N)weights)r   Zaveragelocr   )r   )count_colnamerc   r<   r=   aggfunc_continuous  s    z7process_dataframe_hierarchy.<locals>.aggfunc_continuousrV   r{   r   )rT  /)Zignore_indexZsort_color_if_discrete_color0)r   r    r   r   r'   )TN)rx  r   rK  Z
to_numericr   rT  r   r   re   rG  
differencer_  r   ra   r`  groupbyZaggr\  r~   r   r   rstripr   r   r   r   )rX   r(   Zdiscrete_colornew_pathrd   Znew_col_namerz  Zagg_fZaggfunc_colorZnew_value_col_namer  colsru   Zdf_all_treesrr   levelZdf_treeZdfgjZsort_col_namer<   )r  rc   r=   process_dataframe_hierarchy  s    








&&

r  c          	   C   s   d| d< | d dks | d dkr(t dy0t| d | d  }t| d | d  }W n  t tfk
rx   tdY nX || d	| d | d < | d | d
< | d= | d | d< | d= | S )z8
    Massage input for bar traces for px.timeline()
    Tr  r   Nr   z#Both x_start and x_end are requiredr   zCBoth x_start and x_end must refer to data convertible to datetimes.ztimedelta64[ms]r   r   )r   rK  Zto_datetimerS  r   )rX   r   r   r<   r<   r=   process_dataframe_timelineB  s    r  c                s$   fddt t D }g }d}d krN d rN d  d    d d  }d krd	 krd
 krt|d n\ d rt d  d r|d d d< n.|tjtjgkr|d d d< n
|d nxd ks|tjkr|d nZ|tj	tj
gkr@|d  d rJ d d kr,g  d<  d  d  n
|d td|ko d o|tj	tj
gko|tjtjgkp d}nd}d kr|d d kr|d d kr d d k	} d d k	}	 d d krD|tjtjgkr|	rD|sDd d< n.|tjtjtjtjgkrD|rD|	sDd d<  d d kr|r|	rt d  d }
t d  d }|
r|sd d< |r|
sd d<  d d krd d< |tjkrH|r|	r d d krd  d< |d<  d } d }|dkr|nd |d< |dkr*d n||d< |dkr@dnd|d <  d |d< |tjtjgkr|tjkrvd!nd"}|| d kr d d k	r d  d kr d dkrd#||< n( d  d kr d dkrd#||< || d krd$||< |tjkr6 d% d k	r6 d d kr6d  d< |d< |tjtjgkrTd}d&|d'< d( krΈ d( d krd) krΈ d) d#krtd*d+|d,< n:|tjtj	tjtj
gkr d( |d(< nt d( d+|d,< d krd- d. rd/nd0 |d1< n<|tjkr0d ks|tjkr0d2 d. r&d3nd0 |d1< d4 krLt d4 d5|d6< d7 kr d dkrhd8nd9} d dkr~d8nd9} d7  |< d  |<  d8d d k	s̈ d9d d k	s̈ d:d d k	rd d;< x"tD ]}| kr|| qW  fd<d|D }t |||}||||fS )=Nc                s   g | ]}| kr|qS r<   r<   )r[   r   )rX   r<   r=   rs   \  s    z infer_config.<locals>.<listcomp>r   r   r   r9   r   r.   r6   r5   Tr   Fzmarker.colorr-   z
line.colorr'   r0   z	line.dashr/   zmarker.symbolrg   r   r   rh   ri   rf   r|  nbinsZnbinsxZnbinsyr,  ZboxmodeZ
violinmoder   groupr   r   r   r+  r   g      ?)r+  r   r/  r   z+markers+textrn   r  markersz+textZ
line_shape)shaper   rU   r   r   r+   facet_col_wrapc                s   g | ]}t  |qS r<   )r   )r[   r   )rX   r<   r=   rs     s    )direct_attrablesrI  maxr   re   r   r   r   r   r   r   rX  r   r   r  r1  r2  rr  rq  r   r   r   r   r   group_attrablesr5  )rX   rR   rT   layout_patchrS   Zgrouped_attrsr   show_colorbarZhas_xZhas_yZx_is_continuousZy_is_continuousrg   r  r  r   Zother_positionr   grouped_mappingstrace_specsr<   )rX   r=   infer_config[  s     















 
 





r  c                s  d| kri n
| d   g }i }x|jD ]}t|dkr>|f}|| xx|D ]p  tkrN| d    } krt| < n(x&|D ]}|  kr  | qW t|  jd| < qNW q(W x<t	tt
|D ](\  tkrt| fddd}qW ||fS )aB  
    `orders` is the user-supplied ordering (with the remaining data-frame-supplied
    ordering appended if the column is used for grouping). It includes anything the user
    gave, for any variable, including values not present in the dataset. It is used
    downstream to set e.g. `categoryarray` for cartesian axes

    `group_names` is the set of groups, ordered by the order above

    `group_values` is a subset of `orders` in both keys and values. It contains a key
     for every grouped mapping and its values are the sorted *data* values for these
     mappings.
    Zcategory_ordersr   r   )r\   c                s&   |    kr"   |  S dS )Nrv  )r   )g)ru   rr   r   r<   r=   rp     rq   zget_orderings.<locals>.<lambda>)r~   groupsr   r   r6  r   r   sortedr   r   r`  )rX   rK   groupedZgroup_namesZgroup_values
group_namery  valr<   )ru   rr   r   r=   get_orderings  s,    


r  c       -         sR  |pi }|pi }t   t | |tjtjgkrF d d k	rFt  |dkr\tj}t  t |||\}}}}dd |D pt	g} d j
|dd}	t ||	\}
}g }g }x|D ]}|jr|jdkrt  d	 d
 fdd||j D }|jdkr,t  d d
 fdd||j D }x@||j D ]2}||jkr8|jt|jt|j  |j|< q8W qW t| j}i }t }g }d }}d }xJ|
D ]@}|	t|dkr|n|d }t }t }d}xnt|||D ]^\}}}|t	krt |}t|jts0t|||< |jr0t|||< |jdkr|}qW d| }||krht ||< || }xl|D ]b}|j|d} |jtj tj!tj"tj#tj$tj%tjtjgkr| j&||dko||kd |jtjtj'tj(tj)gkr| j&d|d |*| d| _+d| _,xt-|D ]\}!}||! }||jkr\|jt|jt|j  |j|< y|.| |j|  W n t/k
rL   ||d kr|jtj'tj(tj)gkr|jdkrn||d kr|jtj)gkr|jdkr| j&t0|j| dd nL|jtj"tj#gkrF|jdkrF| j&dgt| |j| gd ddd n Y nX |jdkrf|j| }"n(t1 2ddr|j3dkrd}"nd}" 2d d}#|jdkr|j| }|#rd|d |#  }"d|d |#  }n|j3d!krd}nd}t4||"}|"dkr|"| _+t4||}|dkr|| _,qW |d jtj5krh|jtj(krh| j6j7rh| j&t0| j6j7dd t8 |||9 |\}$}%| &|$ |%d k	r|:|9  |%|d" d#< ||krt0g |d$||< || d% :|  qxW qW d&d | D }&t|&dkrt;|& fd'd(d)}&|r|tj%tj$gkr8d*nd}' d+ pLd d g}(t<d,d-})t0|)= d.  d/ |(d |(d t0t>  |' |'d0d1|d2< x$d3D ]}* |* r |* ||*< qW t0dd4|d5< |rd||d5 d6<  d7 r d7 |d6< n  d8 j?j@jAd krd9d:i|d;< d< krN d< rN d8 j?jBjCd krNd=|d5 d>< tD ||&||||}+xT|&D ]L},xD|,d% D ]8} t| tjErqvtF| |+j?|+jG|| j+ d | j, qvW qhW |+Ht|&dkr|&d d% ng  |+I| d8 kr d8 d k	r|+jI d8 dd? t|dkr"|&ng |+_JtKL||+_MtN ||+ tO ||+ |+S )@Nr(   Ztimelinec             S   s   g | ]}|j ptqS r<   )rK   r6  )r[   r   r<   r<   r=   rs   ,  s    zmake_figure.<locals>.<listcomp>r   F)sortru   r,   r   c                s   g | ]} t | qS r<   )r   )r[   s)r!  r<   r=   rs   :  s    rt   r+   c                s   g | ]} t | qS r<   )r   )r[   r  )r!  r<   r=   rs   =  s    r   r   rn   r*   z, )r   )Zlegendgroup
showlegendT)ZalignmentgroupZoffsetgroupr/   r.   )r.   )r   r   )r   r>  Z	showscaler  r   r   r  r   rv  Zpx_fit_results)r?  r   r?  c             S   s   g | ]}|qS r<   r<   )r[   r#  r<   r<   r=   rs     s    c                s    d   | d S )Nr*   r   )r   )r#  )rX   r   r<   r=   rp     rq   zmake_figure.<locals>.<lambda>)r\   r   range_colorr>  make_figurer6   Zcolor_continuous_midpoint)r   )r>  ZcmidZcminZcmaxZcolorbarr   )r4   r3   )Ztracegroupgaplegendr   titler2   r  r"  marginr   Zconstant
itemsizing)r2   	overwrite)PrB  ru  r   r   r   r  rr  r  r  r6  r  r  rK   rP   rZ   rL   rM   r   r   r   r   Z	get_groupra  r   r   r   rJ   rO   r   r   rG  rR   r   r   r   r   r   r   rz   r1  r2  r   rJ  _subplot_row_subplot_colr`  rN   r   r   rX  r   rU   r  r   r   r.   r   r~   r   r  r   Zvalidate_coercerm   r   r  r  r  r  init_figurer   r   r   Z
add_tracesr  r%  rK  r_  rF   r   r&  )-rX   rR   rT   r  r  r  r   r  rK   r  Zsorted_group_namesZsorted_group_values
col_labels
row_labelsmr  subplot_typeZtrace_names_by_framer%  Ztrendline_rowsr   r   Ztrace_name_labelsr  r  r   Z
frame_nameru   r\   Z
trace_nameZtrace_namesr   ro   rr   rt   r  patchr   
frame_listZcolorvarr  Zcolorscale_validatorri   rG   r  r<   )rX   r   r!  r=   r    sf   


* 





 
















$
r  c                s   fddt |D }xb|D ]Z}xT|d D ]H}	|	jd }
|	jd }t|	tjr`ddi||
 |< q*d|	ji||
 |< q*W qW dg  }dg| }|dkrt| d	d
r| d	 dksd| kr| d rd}nd}|g|d  d| g }d}n4| ddr| dd p
d}n| dd pd}t| dd
r|| d dksTd| krZ| d rZd}nd}|g d  d| g }d}n| dd pd}nd}d}| dd}|r(d g|   }x"t	||  k r|
d  qW xJt |D ]>}x6t  D ]*}||d |   |  ||  | < qW qW t| |dd|r>g n
tt||rRg n||r^|ng ||||dd}x|jjD ]}|jd d qzW |S )Nc                s   g | ]}i g  qS r<   r<   )r[   r   )r   r<   r=   rs     s    zinit_figure.<locals>.<listcomp>r?  r   r   domaing      ?Zxyr   Fr   r.   gGz?gzG?g{Gz?r  r   Zfacet_row_spacinggQ?gQ?r   g{Gzt?Zfacet_col_spacingg{Gz?g?r-  zbottom-left)Zrowsr  specsZshared_xaxesZshared_yaxesZ
row_titlesZcolumn_titlesZsubplot_titleshorizontal_spacingvertical_spacingrow_heightscolumn_widthsZ
start_cell)font)r|   r  r  r   r   r   r   rX  r   r   r   r   r   r   r   annotationsrz   )rX   r  r  r   r   r  r  r  r  ro   Zrow0Zcol0r  r  Z	main_sizer  r  r  Zsubplot_labelsrr   r  rG   Zannotr<   )r   r=   r    sl    





"0r  )NN)UZplotly.graph_objsZ
graph_objsr   Z	plotly.ioior;  collectionsr   r   Z_special_inputsr   r   r   Z_plotly_utils.basevalidatorsr   Zplotly.colorsr	   r
   r   ZpandasrK  Znumpyr   Zplotly.subplotsr   r   r   rs  r  rI  r  Zrenameable_group_attrablesrH  r  r0  rr  rq  r2  r1  r   r   r   r   objectr1   r:  rC   rE   rH   rI   rQ   rZ   r`   re   rm   r   r   r   r   r  r   r   r   r   r   r   r&  r5  r6  rB  rF  rP  rV  rY  r[  r]  rn  ru  rx  r  r  r  r  r  r  r<   r<   r<   r=   <module>   s   J-  E"

:VM

 K [} )
 s