a
    ~b\                     @   s  d dl mZmZmZ d dlmZ d dlmZ d dlZ	d dl
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 d dlmZ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l%m&Z& ddl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z<m=Z=m>Z> ddl?m@Z@ ddl;mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZI G dd de4e:ZJG dd deJZKG dd deKe5ZLG dd deKe6ZMG dd deJe9ZNdS )    )absolute_importdivisionunicode_literals)groupby)defaultdictN)gridplot)ColumnDataSourceColumnRowDivTitleLegendAxisColorBar)PanelTabs   )NoOpSelectionDisplay)	OrderedDictStoreAdjointLayoutNdLayoutLayoutEmpty	GridSpaceHoloMapElement)SkipRendering)	
basestringcftime_to_timestampcftime_typesget_method_owneris_param_methodunique_iterator
wrap_tuplewrap_tuple_streams_STANDARD_CALENDARS   )Link)DimensionedPlotGenericCompositePlotGenericLayoutPlotGenericElementPlotGenericOverlayPlotGenericAdjointLayoutPlotCallbackPlot)attach_streamsdisplayablecollate   )LinkCallback)	
TOOL_TYPESfilter_toolboxes	make_axisupdate_shared_sources
empty_plotdecode_bytestheme_attr_jsoncds_column_replaceget_defaultc                       s  e Zd ZdZejdddZejdddZejdddZ	ej
d	g d
ddZejddddZejddddZg dZdZdZe Zedd Zdd Zdd Zdd Zdd Zdd Zd d! Zed"d# Zed$d% Zd&d' Zd(d) Zd5 fd+d,	Z d6d/d0Z!d1d2 Z"d3d4 Z#  Z$S )7	BokehPlotzy
    Plotting baseclass for the Bokeh backends, implementing the basic
    plotting interface for Bokeh based plots.
    T
        Whether Elements drawing the data from the same object should
        share their Bokeh data source allowing for linked brushing
        and other linked behaviors.defaultdocz{label} {group} {dimensions}z
        The formatting string for the title of this plot, allows defining
        a label group separator and dimension labels.NzAlias for title.above)rC   ZbelowleftrightNz^
        The toolbar location, must be one of 'above', 'below',
        'left', 'right', None.rA   ZobjectsrB   r   Nz
        The width of the component (in pixels). This can be either
        fixed or preferred width, depending on width sizing policy.rA   ZboundsrB   z
        The height of the component (in pixels).  This can be either
        fixed or preferred height, depending on height sizing policy.)ZpanZbox_zoomZ
box_selectZlasso_selectZpoly_selectZypanZxpanz<span style="color:{color};font-family:{font};font-style:{fontstyle};font-weight:{fontstyle};font-size:{fontsize}">{title}</span>bokehc                 C   s   | j r| j jd S d S )Nid)rootrefself rO   <lib/python3.9/site-packages/holoviews/plotting/bokeh/plot.pyrJ   X   s    zBokehPlot.idc                 C   s   t dS )z
        Returns the data from an element in the appropriate format for
        initializing or updating a ColumnDataSource and a dictionary
        which maps the expected keywords arguments of a glyph to
        the column in the datasource.
        N)NotImplementedError)rN   elementrangesstylerO   rO   rP   get_data]   s    zBokehPlot.get_datac                 C   sJ   ddl m} | j|j_| j D ](}t||r|jD ]}|j| jd q0qd S )Nr3   )Selection1DCallback)index)	callbacksrV   selectedindices
isinstancestreamsupdate)rN   cdsrV   cbsrO   rO   rP   _update_selectedg   s    



zBokehPlot._update_selectedc                 C   s6   |  |}t|d}t| dr2| jdur2| | |S )zN
        Initializes a data source to be passed into the bokeh glyph.
        )datarY   N)_postprocess_datar   hasattrrY   ra   )rN   rb   r^   rO   rO   rP   _init_datasourceo   s
    


zBokehPlot._init_datasourcec                 C   sr   i }|  D ]`\}}t|}t|rdt|d trdtdd |D rZ| jd|d j  t	|d}|||< q|S )zs
        Applies necessary type transformation to the data before
        it is set on a ColumnDataSource.
        r   c                 s   s   | ]}|j tvV  qd S N)calendarr&   .0vrO   rO   rP   	<genexpr>       z.BokehPlot._postprocess_data.<locals>.<genexpr>zConverting cftime.datetime from a non-standard calendar (%s) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend.ms)
itemsr:   lenr[   r    anyparamwarningrg   r   )rN   rb   Znew_datakvaluesrO   rO   rP   rc   z   s    

zBokehPlot._postprocess_datac                    s   | j s
dS | |}tdd | D }| jr| jd j| jju r| jr|s| jd   jr fdd|	 D }|
| j dS t||r||_n|j| t| dr| jdur| | dS )z>
        Update datasource with data for a new frame.
        Nc                 s   s   | ]}t |d kV  qdS rG   )ro   rh   rO   rO   rP   rk      rl   z/BokehPlot._update_datasource.<locals>.<genexpr>r   c                    s"   i | ]\}}|| j  d  qS rf   )Z_chunk_lengthri   rs   rj   streamrO   rP   
<dictcomp>   rl   z0BokehPlot._update_datasource.<locals>.<dictcomp>rY   )Zdocumentrc   allrt   Z	streamingrb   current_frameZ_stream_dataZ_triggeringrn   rw   lengthr<   r]   rd   rY   ra   )rN   sourcerb   emptyrO   rv   rP   _update_datasource   s&    


zBokehPlot._update_datasourcec           	         s   |  dd tg} fdd| jD }|D ]b}|jD ]V}|jD ]J}| D ]$\}}||jv rJ|rJ|d |j|< qJ| jr>|jd| j	|_q>q4q*dS )z
        Iterates over all subplots and updates existing CustomJS
        callbacks with models that were replaced when compositing
        subplots into a CompositePlot and sets the plot id to match
        the root level bokeh model.
        c                 S   s   | S rf   rO   xrO   rO   rP   <lambda>   rl   z-BokehPlot._update_callbacks.<locals>.<lambda>c              	      s$   i | ]}|t  d t| iqS )type)listZselectr5   )ri   tplotrO   rP   rx      s   z/BokehPlot._update_callbacks.<locals>.<dictcomp>r   ZPLACEHOLDER_PLOT_IDN)
traverser,   _merged_toolsrX   rn   args	top_levelcodereplacerJ   )	rN   r   subplotsZmerged_toolssubplotr_   cZtoolZobjsrO   r   rP   _update_callbacks   s    


zBokehPlot._update_callbacksc                 C   s
   | j d S )z
        The plotting state that gets updated via the update method and
        used by the renderer to generate output.
        r   handlesrM   rO   rO   rP   state   s    zBokehPlot.statec                 C   s   g S )g
        Should return a list of plot objects that have changed and
        should be updated.
        rO   rM   rO   rO   rP   current_handles   s    zBokehPlot.current_handlesc              
   C   s   | j j}ttd|ttd|ttd|ttd|ttd|ttd|ttd|d}t|}| D ]&\}}t	|trdd|v rd|d ||< qd|S )Ntext_font_sizeZtitle_text_font_sizeZlabel_text_font_sizeZaxis_label_text_font_sizeZmajor_label_text_font_size)titleZlegend_titleZlegendlabelZticksZcticksZclabelvalue)
rendererthemer=   r   r   r   r   dictrn   r[   )rN   r   defaultsZ	processedrs   rj   rO   rO   rP   _get_fontsize_defaults   s    






	z BokehPlot._get_fontsize_defaultsc                    s   |  dd tg  D ]}t|tttfs,qt|j}g |_d|_|j	rR|j	
  t|trz|jD ]}||j7 }|  qbt|D ]} fdd|jD |_qqdS )z
        Cleans up references to the plot after the plot has been
        deleted. Traverses through all plots cleaning up Callbacks and
        Stream subscribers.
        c                 S   s   | S rf   rO   r   rO   rO   rP   r      rl   z#BokehPlot.cleanup.<locals>.<lambda>Nc                    s,   g | ]$\}}t |r t| vr||fqS rO   )r"   r!   )ri   pZ
subscriberplotsrO   rP   
<listcomp>   s   z%BokehPlot.cleanup.<locals>.<listcomp>)r   r>   r[   r*   r,   r-   r   r\   Z	_documentr   clearrX   cleanupsetZ_subscribers)rN   r   r\   callbackrw   rO   r   rP   r      s"    






zBokehPlot.cleanupfontsizec                    s&   t t| |||}dd | D S )z[
        Converts integer fontsizes to a string specifying
        fontsize in pt.
        c                 S   s(   i | ] \}}|t |tr|nd | qS )z%spt)r[   r   ru   rO   rO   rP   rx     s   z'BokehPlot._fontsize.<locals>.<dictcomp>)superr>   	_fontsizern   )rN   keyr   commonsize	__class__rO   rP   r      s    zBokehPlot._fontsize15pt  c                 C   s   d }| j r| |nd}|s |S t| jjd}|dd }|dd}|dd}	| dd	|}
|
|kr|d
|}
d|
v rtt|
d d d d }
| j	j
|||	|
|d}d| jv r| jd }nt|ddid}||_|S )N r   Z
text_colorZ	text_fontZArialZtext_font_styleZboldr   r   r   Zemg      ?)colorfont	fontstyler   r   zwhite-spaceZnowrap)widthrT   )
show_title_format_titler;   r   r   getr   strfloat_title_templateformatr   r   text)rN   r   Zdefault_fontsizer   Z	title_divr   Z
title_jsonr   r   r   r   Z
title_tagsrO   rO   rP   _get_title_div	  s2    
zBokehPlot._get_title_divc                 C   s  dd }dd }|  ||g}tt|dd ddd }g }i }g }|D ]\}}	t|	}	t|	dkrHi }
|	D ]\}}|
|jd j qnt|
}|	D ]\}}|j	d}|j
D ]}|  q|d	u rqn&d
| v r|j|d n|j|d t|dr
|jj|d | |jd< |jd< || q|| dd |jD |t|< qH|D ]d}|jrr|jrr| jd |jp~|j}|D ]}|||j q|j
D ]}|j| jd qqR|| jd< || jd< d	S )zd
        Syncs data sources between Elements, which draw data
        from the same object.
        c                 S   s   t | jj| fS rf   )rJ   rz   rb   r   rO   rO   rP   r   .  rl   z(BokehPlot.sync_sources.<locals>.<lambda>c                 S   s,   | j o*| jd uo*t| jjtj o*d| jv S )Nr|   )shared_datasourcerz   r[   rb   npZndarrayr   r   rO   rO   rP   r   /  s   c                 S   s   | d S )Nr   rO   r   rO   rO   rP   r   3  rl   )r   r3   r|   Zglyph_rendererNdata_source)r   )r|   viewr^   c                 S   s   g | ]}|qS rO   rO   ri   r   rO   rO   rP   r   M  rl   z*BokehPlot.sync_sources.<locals>.<listcomp>zXSupply either hooks or finalize_hooks not both; using hooks and ignoring finalize_hooks.)Zplot_idshared_sourcessource_cols)r   r   sortedr   ro   r]   r   rb   r   r   rX   resetZ
propertiesrd   r   appendrJ   hooksZfinalize_hooksrq   rr   rz   Z
initialize)rN   Zget_sourcesZ	filter_fnZdata_sourcesZgrouped_sourcesr   r   r   _groupZsource_datar   Z
new_sourcer   r   r   hookrO   rO   rP   sync_sources)  sR    




zBokehPlot.sync_sourcesc                 C   sb   t | }g }|D ]J\}}}tjd t| }|d u s|jrF|d u rFq||| j||| q|S )NrI   )r4   Z
find_linksr(   Z
_callbacksr   Z_requires_targetr   rK   )rN   linksrX   linkZsrc_plotZtgt_plotr_   rO   rO   rP   
init_links[  s    
zBokehPlot.init_links)r   T)r   r   )%__name__
__module____qualname____doc__rq   Booleanr   Stringr   Ztitle_formatObjectSelectortoolbarIntegerr   heightr   r   backendr   Zselection_displaypropertyrJ   rU   ra   re   rc   r~   r   r   r   r   r   r   r   r   r   __classcell__rO   rO   r   rP   r>   )   sB   



	
 2r>   c                   @   sT   e Zd ZdZejdg dddZejddidd	d
Zdd Z	dd Z
edd ZdS )CompositePlotz
    CompositePlot is an abstract baseclass for plot types that draw
    render multiple axes. It implements methods to add an overall title
    to such a plot.
    N)fixedstretch_widthstretch_heightstretch_bothZscale_widthZscale_heightZ
scale_bothNaS  

        How the component should size itself.

        * "fixed" :
          Component is not responsive. It will retain its original
          width and height regardless of any subsequent browser window
          resize events.
        * "stretch_width"
          Component will responsively resize to stretch to the
          available width, without maintaining any aspect ratio. The
          height of the component depends on the type of the component
          and may be fixed or fit to component's contents.
        * "stretch_height"
          Component will responsively resize to stretch to the
          available height, without maintaining any aspect ratio. The
          width of the component depends on the type of the component
          and may be fixed or fit to component's contents.
        * "stretch_both"
          Component is completely responsive, independently in width
          and height, and will occupy all the available horizontal and
          vertical space, even if this changes the aspect ratio of the
          component.
        * "scale_width"
          Component will responsively resize to stretch to the
          available width, while maintaining the original or provided
          aspect ratio.
        * "scale_height"
          Component will responsively resize to stretch to the
          available height, while maintaining the original or provided
          aspect ratio.
        * "scale_both"
          Component will responsively resize to both the available
          width and height, while maintaining the original or provided
          aspect ratio.
    rF   r   r   T
       Specifies various fontsizes of the displayed text.

       Finer control is available by supplying a dictionary where any
       unmentioned keys reverts to the default sizes, e.g:

          {'title': '15pt'}rA   Z
allow_NonerB   c                    s0    fdd j D }|D ]}| jd qdS )zo
        Should perform any linking required to update titles when dimensioned
        streams change.
        c                    s(   g | ] }t  fd d|jD r|qS )c                 3   s   | ]}| j v V  qd S rf   
dimensionsri   rs   rM   rO   rP   rk     rl   zECompositePlot._link_dimensioned_streams.<locals>.<listcomp>.<genexpr>)rp   contents)ri   r`   rM   rO   rP   r     rl   z;CompositePlot._link_dimensioned_streams.<locals>.<listcomp>r3   N)r\   Zadd_subscriber_stream_update)rN   r\   r`   rO   rM   rP   _link_dimensioned_streams  s    z'CompositePlot._link_dimensioned_streamsc                    sN   dd | j D  t fddt| j| jD }t|| j| j }| | d S )Nc                 S   s   g | ]}|j D ]}|qqS rO   r   )ri   r`   rs   rO   rO   rP   r     rl   z0CompositePlot._stream_update.<locals>.<listcomp>c                 3   s"   | ]\}}| v rd n|V  qd S rf   rO   )ri   drs   r   rO   rP   rk     rl   z/CompositePlot._stream_update.<locals>.<genexpr>)r\   tuplezipr   Zcurrent_keyr%   r   )rN   kwargsr   rO   r   rP   r     s     zCompositePlot._stream_updatec                 C   s   d| j v r| j d gS g S )r   r   r   rM   rO   rO   rP   r     s    zCompositePlot.current_handles)r   r   r   r   rq   r   sizing_mode	Parameterr   r   r   r   r   rO   rO   rO   rP   r   g  s   '	r   c                       s  e Zd ZdZejdddZejddiddd	Zej	dd
dZ
ej	dddZej	dddZej	dddZejdg dddZejdg dddZejddddZejddddZejdeefddZd' fdd	Zdd  Zdg fd!d"Zd#d$ Zed(d%d&Z  ZS ))GridPlotz\
    Plot a group of elements in a grid layout based on a GridSpace element
    object.
    2   zn
        Number of pixels to adjust row and column widths and height by
        to compensate for shared axes.r@   r   Z16ptTr   r   =
        Whether to merge all the tools into a single toolbarFz
        If enabled the x-axes of the GridSpace will be drawn from the
        objects inside the Grid rather than the GridSpace dimensions.zD
        Adds a legend based on the entries of the middle-right plot)ZbottomtopNTFzh
        Whether and where to display the xaxis, supported options are
        'bottom', 'top' and None.rF   )rD   rE   NTFzh
        Whether and where to display the yaxis, supported options are
        'left', 'right' and None.r   )r   ih  z&
        Rotation angle of the xticks.rH   z&
        Rotation angle of the yticks.x   z
        Defines the width and height of each plot in the grid, either
        as a tuple specifying width and height or an integer for a
        square plot.)rA   class_rB   Nr3   c                    s   t |tstdtt jf ||||d| |j\ _ _ 	||\ _
 _ jrp  fddtg d|v r jd d S )Nz GridPlot only accepts GridSpace.)layout
layout_numrS   keysc                    s   t  | jdS Nr'   r0   hmapr   rM   rO   rP   r     rl   z#GridPlot.__init__.<locals>.<lambda>axis_offsetzGridPlot axis_offset option is deprecated since 1.12.0 since subplots are now sized correctly and therefore no longer require an offset.)r[   r   	Exceptionr   r   __init__shapecolsrows_create_subplotsr   r   r   r   r,   rq   rr   )rN   r   rS   r   r   paramsr   rM   rP   r    s    
zGridPlot.__init__c                    s  t jtrj\}}njj }}t }d | jrNjd d nj}t fdd|D  jdjd}t	jddD ]<\}}	|j
 }
|j
 }t |	ts|	f}	j|	d }|d urt |tr|jn|j}|dj}nd }t|ttfv rtd	t|s(t|}i }|d ur>||d
< |d urP||d< |dkrbd|d< |dkr~|
dkr~d |d< |dkr|
dkrd |d< |
dkr|dkrd |d< d |d< d|vrd|d< jr
|jd kr
|
j
d kr
d|d< d|d< nd|d< js"d |d< js2d |d< tjjj |d }|d u rp|d ur̈j d|j!  q||fj"ddjj# j$jdt%|fi |}t |t&r|j'n|j(||	< |||	< q||fS )Nr3   c                    s   g | ]}| | fqS rO   compute_rangesri   r   Zframe_rangesr   rN   rO   rP   r     s   z-GridPlot._create_subplots.<locals>.<listcomp>FZshared_datarJ   TZ	full_gridr   zCannot plot nested Layouts.Zframe_widthZframe_heightr   endZalignxaxisyaxisZborderr   show_legendrE   Zlegend_positionzHBokeh plotting class for %s type not found, object will not be rendered.)r   r   r   r   rK   rS   uniformr   ))r[   	plot_sizer   r   r  dynamicr   clonerJ   	enumerater  rb   r   r   r   r   lookup_optionsoptionsr   r   r   r1   r2   r  r  shared_xaxisshared_yaxisr   registryr   r   rq   rr   r   r   rK   r  r   r*   r   r   )rN   r   rS   r   r   r   r   collapsed_layouticoordrr   r   vtypeoptsr   Zplotting_classr   rO   r
  rP   r    s    






(





zGridPlot._create_subplotsc                    sR     j jd d }t|} fddt jD }t jjddD ]f\}}| j }| j } jt	|d }|d ur|j
||d}	|	|| |< ||	 qH|d  qHt|d d d  j j jd}	 |	}	  jd }
|
rt|
|	}	|
 jd< |	 jd	< | jd
<  |	  jr2    jrB   d _ jd	 S )Nc                    s    g | ]}d d t  jD qS )c                 S   s   g | ]}d qS rf   rO   r   rO   rO   rP   r   Q  rl   z7GridPlot.initialize_plot.<locals>.<listcomp>.<listcomp>)ranger  )ri   r  rM   rO   rP   r   Q  rl   z,GridPlot.initialize_plot.<locals>.<listcomp>Tr  rS   r   )merge_toolsr   toolbar_locationr   r   r   )r  r   r   r   r"  r  r  r   r   r$   initialize_plotr   r   r$  r   r   
_make_axesr   r	   r   r   r   r   r   r   drawn)rN   rS   r   passed_plotsr  r  r  r   r   r   r   rO   rM   rP   r&  N  s<    







zGridPlot.initialize_plotc              
   C   s  | j |\}}d\}}| jjdd}| jr| j}| j}| dd}	| jdddd}
t	t
d	d
 |D }td||| jjd |||	|
d}| jr| jjdkr| j}| j}| dd}	| jdddd}
t	t
dd
 |D }td||| jjd |||	|
d}|r|rt|}||gd |g }}| jrF|| }}| jrrd|_|d d d |d d d  }}t||g}nZ|r||g}| jr|d d d }t| }n,|r||g}| jr|d d d }t| }|S )NNNTr  Zxlabelr   xticksF)r   c                 S   s   g | ]}t |d  qS r   )r$   r   rO   rO   rP   r     rl   z'GridPlot._make_axes.<locals>.<listcomp>r   r   )fliprotationZ
label_sizeZ	tick_sizer3   Zylabelyticksc                 S   s   g | ]}|d  qS r3   rO   r   rO   rO   rP   r     rl   y)r   r   r   r   r!  )r   Zget_sizer   r   r  r  	xrotationr   r   r   r#   r7   kdimsr  Zndimsr  	yrotationr6   Zmarginr   r
   r	   )rN   r   r   r   Zx_axisZy_axisr   r-  r.  ZlsizeZtsizeZxfactorsZyfactorsZr1Zr2ZmodelsrO   rO   rP   r'  v  sR    

zGridPlot._make_axesc                 C   sf   |  | j||}| jjddD ]*}| jt|d}|dur||| q| |}|rb| jd  dS )
        Update the internal state of the Plot to represent the given
        key tuple (where integers represent frames). Returns this
        state.
        Tr  Nr   )	r  r   r   r   r   r$   update_framer   r   )rN   r   rS   r  r   r   rO   rO   rP   r6    s    
zGridPlot.update_frame)Nr3   N)N)r   r   r   r   rq   r   r   r   r   r   r$  r  r  r  r   r  r  r2  r4  ZClassSelectorintr   r  r  r  r&  r'  r8   r6  r   rO   rO   r   rP   r     s0   
	Q(*r   c                       s   e Zd ZejdddZejdddZejdddZejdddZd fd	d
	Z	dd Z
dddZdd ZdddZedddZ  ZS )
LayoutPlotTz3
        Whether axes should be shared across plotsr@   Fr?   r   z<
        Whether to display overlaid plots in separate panesNc                    sR   t t j|fd|i|  |\ _ _ _ jrN  fddt	g d S )Nr   c                    s   t  | jdS r   r   r   rM   rO   rP   r     rl   z%LayoutPlot.__init__.<locals>.<lambda>)
r   r8  r  _init_layoutr   r   pathsr   r   r,   )rN   r   r   r  r   rM   rP   r    s    zLayoutPlot.__init__c                    s   d}j djd}d d  jr6jd d nj}t fdd|D   }ttrnj	nd }i i i   }}}	j
D ]j\}
}|jr||
fn|
|fd\}}t|tr|nt|g}|||
|f< ||	|
|f< dd	d
d}|t|d}tj| d }||
|fd\}}ttrB|rBtt||}t|jt}|s|jd u rbqn2|dd tgsjd|j  qn|d7 }|rdn|}j||| |d\}}|dj}t|||fi |}|||
|f< |r|||< q|||	fS )Nr   Fr  r3   c                    s   g | ]}| | fqS rO   r  r	  r
  rO   rP   r     s   z+LayoutPlot._init_layout.<locals>.<listcomp>r*  SingleZDualTriple)r3   r'   r   	positionsc                 S   s   | S rf   rO   r   rO   rO   rP   r     rl   z)LayoutPlot._init_layout.<locals>.<lambda>z%s is empty, skipping subplot.)numr   )r  rJ   r  r  r   r   Z
grid_itemsr[   r   r3  coordsr   Z	transposer   ro   AdjointLayoutPlotlayout_dictr   mainr   r   r   rq   rr   r  r  r  )rN   r   Zlayout_countr  r   Zlayout_itemslayout_dimensionsZlayout_subplotsZlayoutsr:  r  r   r   r   Zlayout_lenslayout_typer=  Z
layout_keyr   r}   r>  r   Zadjoint_layoutplotoptslayout_plotrO   r
  rP   r9    sL    "


zLayoutPlot._init_layoutr   c                 C   sN  i }|j d|jd}d}|D ]&}	||	d}
|
du s|
dd ttgsLqt|
s\t|
}
t|d}t	|
t
rv|
jn|
j}tj| jj |d}| |
dj}i }|	dkrLtj||}|	d	kr|rd
|jv rdnd	}|r|jnd}t|j||ddgd|jd}nD|rd
|jv rdnd}|r.|jnd}t|j||dgd|jd}t|fi |}|| |tu r|
||	< d||	< qn|du r| jd|j  qt| jdkr|nd}||
f| j| j||d| j | j!|| jd	td| j"ifi |}|||	< t	|tr*t#|t$r*||	 j%||	< n||	 j&||	< |	dkr|}q||fS )<  
        Plot all the views contained in the AdjointLayout Object using axes
        appropriate to the layout configuration. All the axes are
        supplied by LayoutPlot - the purpose of the call is to
        invoke subplots with correct options and styles and hide any
        empty axes as necessary.
        Fr  Nc                 S   s   | S rf   rO   r   rO   rO   rP   r     rl   z-LayoutPlot._create_subplots.<locals>.<lambda>)Zadjoinedr   rB  rE   Zbarez
right-barer   Tr1  r3   )r   r  r   Zinvert_axeslabelledr+  r  ztop-barer   r   )r   r  r   rH  r/  r  zIBokeh plotting class for %s type not found, object  will not be rendered.)	r   r   rC  rS   r   rK   r  r   r   shared_axes)'r  rJ   r   r   r   r   r1   r2   r   r[   r   r   r   r   r  r   r   r  r  r@  r  r   r   r  r]   rq   rr   r   ro   r?  r   r   rK   r  rI  
issubclassr*   r   r   )rN   r   r=  rC  rS   r>  r   Zadjoint_cloneZ	main_plotposrR   Zsubplot_optsr  Z	plot_typerE  Z	side_optsr  r   r  r   r   rO   rO   rP   r    sz    








zLayoutPlot._create_subplotsc                    s  g }t | jD ]d}g }t | jD ]B}| j||fd}|du rBdnt|j}||dkr\dnd q |t| qg }t | jD ]d}g }t | jD ]B}| j||fd}|du rdnt|j}||dkrdnd q|t| qt	|}	t	|  fddt |	D }
|
S )z
        Computes an empty grid to position the plots on by expanding
        any AdjointLayouts into multiple rows and columns.
        Nr3   r'   c                    s   g | ]}d g  qS rf   rO   )ri   r   r  rO   rP   r   f  rl   z,LayoutPlot._compute_grid.<locals>.<listcomp>)
r"  r  r  r   r   ro   r   r   maxsum)rN   Zwidthsr   Zc_widthsr  r   	nsubplotsZheightsZ	r_heightsr  gridrO   rL  rP   _compute_gridL  s(    zLayoutPlot._compute_gridc                    s  |  | j| jd d }| jjd| j}|d u r4i n|j}|  }|d u rNg n|}d}tt	}g }d}	d}
t
| jD ]fdd| j D }tdd |D }|r|d	7 }t
| jD ]H | j fd } fd
d| j D }tdd |D }|r|  d	7  < |d}|d u r*q| jr:t|nd }|j||d}t|}dd |D }| j}|rtdd |D }tdd |D }|	|O }	|
|O }
|r|rd}n|rd}n
|rd}| jrx|jd j| jd dd}|sd| j f }|d	kr|d }nN|dkr<t|g| j| j|d}n,|d d g|d d g}t|| j| j|d}|||f q|dkr|d || d	   | d	 < ||  }|d	kr|d | | d	 < |d	 | | < n|d | | t	| < ||d  qqtd|v r|d }n.|	r.|
r.d}n|	r:d}n|
rFd}nd }| jrtdd |D }dd |D }t||d}nt|}t|| j| j|d}| | jd }|r|| j d< t!|||d }|| j d< || j d!< | "| | j#r| $  | j%r| &  d"| _'| j d S )#Nr!  r   r   Fc                    s$   g | ]\}}|d   kr||fqS r,  rO   ri   rs   sp)r  rO   rP   r   z  rl   z.LayoutPlot.initialize_plot.<locals>.<listcomp>c                 s   s    | ]\}}t |jd kV  qdS )r'   Nro   r   rR  rO   rO   rP   rk   {  rl   z-LayoutPlot.initialize_plot.<locals>.<genexpr>r3   c                    s$   g | ]\}}|d   kr||fqS r0  rO   rR  )r   rO   rP   r     rl   c                 s   s    | ]\}}t |jd kV  qdS )r3   NrT  rR  rO   rO   rP   rk     rl   r#  c                 S   s   h | ]}|j d vr|j qS ))Nautor   r   )ri   rS  rO   rO   rP   	<setcomp>  s   
z-LayoutPlot.initialize_plot.<locals>.<setcomp>c                 s   s    | ]}d D ]}||v V  q
qdS ))r   bothNrO   ri   mr`   rO   rO   rP   rk     rl   c                 s   s    | ]}d D ]}||v V  q
qdS ))r   rX  NrO   rY  rO   rO   rP   rk     rl   r   r   r   rB  r    r'   )r$  r%  r   )childrenr$  r%  r   r   c                 S   s   g | ]\}}|qS rO   rO   )ri   r   r   rO   rO   rP   r     rl   c                 S   s   g | ]\}}t ||d qS ))childr   )r   )ri   r   r]  rO   rO   rP   r     rl   )tabsr   )r\  r%  r$  r   r   rV  r   T)(r  r   r   r   r   r   r   rQ  r   r7  r"  r  r   rn   rp   r  rI  r   r&  ro   r   r^  r   joinr:  r   r$  r   r   r6   r   r   r   r	   r   r   r   r   r   r(  )rN   r   rS   r   Z	plot_gridr)  Zr_offsetZcol_offsetsZ	tab_plotsr   r   rowZ
row_paddedr   colZ
col_paddedZc_offsetZshared_plotsr   rO  Zmodesr   Zresponsive_widthZresponsive_heightr   rP  Zplot_columnZpanelsrF  rO   )r   r  rP   r&  k  s    





 






zLayoutPlot.initialize_plotc                 C   sb   |  | j||}| jD ].\}}| j||fd}|dur||| q| |}|r^|| jd< dS )r5  Nr   )r  r   r?  r   r   r6  r   r   )rN   r   rS   r  r   r   r   rO   rO   rP   r6    s    
zLayoutPlot.update_frame)N)r   )NN)N)r   r   r   rq   r   rI  r   r$  r^  r  r9  r  rQ  r&  r8   r6  r   rO   rO   r   rP   r8    s   9
J
r8  c                       s8   e Zd Zi Z fddZdg fddZdddZ  ZS )	r@  c                    s<   || _ || _| j| j d | _tt| jf d|i| d S )Nr=  r   )r   rD  rA  view_positionsr   r@  r  )rN   r   rD  r   r  r   rO   rP   r     s    zAdjointLayoutPlot.__init__Nc                 C   st   |du rg }g }| j D ]H}| j|d}|du rB|tdd q|| }||j||d qd| _|spdg}|S )rG  Nr   r#  T)rb  r   r   r   r9   r&  r(  )rN   rS   r   Zadjoined_plotsrK  r   r)  rO   rO   rP   r&    s    

z!AdjointLayoutPlot.initialize_plotc                 C   s2   d }dD ]$}| j |}|d ur|||}q|S )N)rB  rE   r   )r   r   r6  )rN   r   rS   r   rK  r   rO   rO   rP   r6  #  s    zAdjointLayoutPlot.update_frame)N)r   r   r   r  r  r&  r6  r   rO   rO   r   rP   r@    s   r@  )OZ
__future__r   r   r   	itertoolsr   collectionsr   Znumpyr   rq   Zbokeh.layoutsr   Zbokeh.modelsr   r	   r
   r   r   r   r   r   Zbokeh.models.widgetsr   r   Z	selectionr   corer   r   r   r   r   r   r   r   r   Zcore.optionsr   Z	core.utilr   r   r    r!   r"   r#   r$   r%   r&   r   r(   r   r)   r*   r+   r,   r-   r.   r/   utilr0   r1   r2   rX   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r   r   r8  r@  rO   rO   rO   rP   <module>   s4   (,,$,  @O }  L