
    e
M                     Z   d 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
 ddlmZmZ  G d d          Z G d	 d
e          Z G d dee          Z G d dee          Z G d dee          Z G d dee
          Z ed  e                                            D                       ZdS )z
Supplies Pane, Layout, NdLayout and AdjointLayout. Pane extends View
to allow multiple Views to be presented side-by-side in a NdLayout. An
AdjointLayout allows one or two Views to be adjoined to a primary View
to act as supplementary elements.
    N   )	traversal)	DimensionDimensionedViewableElementViewableTree)	NdMappingUniformNdMappingc                       e Zd ZdZd Zd ZdS )
Layoutablezv
    Layoutable provides a mix-in class to support the
    add operation for creating a layout from the operands.
    c                     t          d | |fD                       r*t          d| j        j         d|j        j         d          	 t	          | |g          S # t
          $ r
 t          cY S w xY w)zCompose objects into a Layoutc              3   @   K   | ]}t          |t                    V  d S N)
isinstanceint).0args     5lib/python3.11/site-packages/holoviews/core/layout.py	<genexpr>z%Layoutable.__add__.<locals>.<genexpr>   s,      66z#s##666666    z#unsupported operand type(s) for +: z and z. If you are trying to use a reduction like `sum(elements)` to combine a list of elements, we recommend you use `Layout(elements)` (and similarly `Overlay(elements)` for making an overlay from a list) instead.)any	TypeError	__class____name__LayoutNotImplementedErrorNotImplemented)xys     r   __add__zLayoutable.__add__   s    661v66666 	G F!+BV F F]^]h]q F F F G G G
	"1a&>>!" 	" 	" 	"!!!!	"s   A A,+A,c                 8    | j                             ||           S r   )r   r    selfothers     r   __radd__zLayoutable.__radd__"   s    ~%%eT222r   N)r   
__module____qualname____doc__r    r%    r   r   r   r      s<         " " "3 3 3 3 3r   r   c                       e Zd ZdZd ZdS )
Composablezu
    Composable is a mix-in class to allow Dimensioned objects to be
    embedded within Layouts and GridSpaces.
    c                 0   t          |t          t          t          f          rt	          | |g          S t          |t                    r*t	          |j                                        | gz             S t          dt          |          j	         d          )z%Compose objects into an AdjointLayoutzCannot append z to a AdjointLayout)
r   r   r	   EmptyAdjointLayoutdatavaluesr   typer   r"   s     r   
__lshift__zComposable.__lshift__,   s    eoy%@AA 	X $///}-- 	X !2!2!4!4dV!;<<<VT%[[-AVVVWWWr   N)r   r&   r'   r(   r2   r)   r   r   r+   r+   &   s2         
X X X X Xr   r+   c                   D     e Zd ZdZ ej        d           Z fdZ xZS )r-   z
    Empty may be used to define an empty placeholder in a Layout. It
    can be placed in a Layout just like any regular Element and
    container type via the + operator or by passing it to the Layout
    constructor as a part of a list.
    )defaultc                 :     t                      j        di | d S )Nr   )super__init__)r#   paramsr   s     r   r7   zEmpty.__init__A   s&    (((((((r   )	r   r&   r'   r(   paramStringgroupr7   __classcell__r   s   @r   r-   r-   7   sX          EL)))E) ) ) ) ) ) ) ) )r   r-   c                       e Zd ZdZ ej         ed           gd          Zg dZdZ	dZ
 fdZddZd Zed	             Zed
             Zej        d             Zej        d             Zd fd	Zd dZd!dZd Zd Zd Zed             Zed             Zed             Zed             Zed             Zd Zd Zd Zd Z  xZ!S )"r.   a,  
    An AdjointLayout provides a convenient container to lay out some
    marginal plots next to a primary plot. This is often useful to
    display the marginal distributions of a plot next to the primary
    plot. AdjointLayout accepts a list of up to three elements, which
    are laid out as follows with the names 'main', 'top' and 'right':

     _______________
    |     3     |   |
    |___________|___|
    |           |   |  1:  main
    |           |   |  2:  right
    |     1     | 2 |  3:  top
    |           |   |
    |___________|___|
    Tr4   constantmainrighttopFc                     d _         |r"t          |          dk    rt          d          |(t          d |D                       rt	          |          }t          |t                    r  fd|D             }|rt          d          n:t          |t                    r#t	          t           j        |                    }ni } t                      j
        |fi | d S )Nr      z2AdjointLayout accepts no more than three elements.c              3   @   K   | ]}t          |t                    V  d S r   )r   tupler   vs     r   r   z)AdjointLayout.__init__.<locals>.<genexpr>d   s,      #G#GQJq%$8$8#G#G#G#G#G#Gr   c                 &    g | ]}|j         v|S r)   )layout_orderr   kr#   s     r   
<listcomp>z*AdjointLayout.__init__.<locals>.<listcomp>g   s&    GGGqAT5F,F,F,F,F,Fr   z'Wrong AdjointLayout positions provided.)
main_layerlen	Exceptionalldictr   listziprL   r6   r7   )r#   r/   r8   	wrong_posr   s   `   r   r7   zAdjointLayout.__init___   s     	RCIIMMPQQQ#G#G$#G#G#G G G::DdD!! 	GGGGDGGGI K IJJJKd## 	D-t4455DDD(((((((r   c                    |r|n| |r| n|g }t          t                    rt          t                    rg }|                    j        j        z             j        pj        ij                                        j                                        k    r#|                    j        j        z             nU|j        j        gz  }nCj        |                    j                   n!j        |                    j                   j        pj        ij                                        j                                        k    r#|                    j        j        z             nU|j        j        gz  }nCj        |                    j                   n!j        |                    j                   t          |          dk    rt          d          nkt          t                    r!fd| j	        D             }j        z  |d<   n5t          t                    r fd| j	        D             }j        z  |d<   |r| 
                    |          S t          S )NrF   zAdjointLayouts could not be overlaid, the dimensions of the adjoined plots do not match and the AdjointLayout can hold no more than two adjoined plots.c                 <    g | ]}|j         v j         |         S r)   r/   )r   olayer1s     r   rO   z)AdjointLayout.__mul__.<locals>.<listcomp>   5     3 3 3!"fk!1!1 %k!n!1!1!1r   r   c                 <    g | ]}|j         v j         |         S r)   rZ   )r   r[   layer2s     r   rO   z)AdjointLayout.__mul__.<locals>.<listcomp>   r]   r   )r   r.   appendrB   rC   
dimensionsrD   rQ   
ValueErrorrL   cloner   )r#   r$   reverseadjoined_itemsr\   r_   s       @@r   __mul__zAdjointLayout.__mul__r   s   !+t +efm,, "	5FM1R1R "	5N!!&+fk"9:::|'FL,D<**,,0G0G0I0III"))&,v|*CDDDD"v|V\&BBNN)%%fl3333)%%fl333z%&**@:((**fj.C.C.E.EEE"))&*VZ*?@@@@"vz6:&>>NN'%%fj1111'%%fj111>""Q&&  "I J J J '
 .. 	53 3 3 3d6G 3 3 3N &f 4N1.. 	53 3 3 3d6G 3 3 3N & 4N1 	"::n---!!r   c                 0    |                      |d          S NT)rd   rf   r"   s     r   __rmul__zAdjointLayout.__rmul__       ||E4|000r   c                 z    | j         r3| j         j        t          | j                   j        k    r| j         j        S dS )z!Group inherited from main elementr.   )rB   r;   r1   r   r#   s    r   r;   zAdjointLayout.group   s6     9 	#DOO,DDD9?""?r   c                 ,    | j         r| j         j        ndS )z!Label inherited from main element )rB   labelrm   s    r   rp   zAdjointLayout.label   s     #')3ty3r   c                     d S r   r)   )r#   r;   s     r   r;   zAdjointLayout.group        Dr   c                     d S r   r)   )r#   rp   s     r   rp   zAdjointLayout.label   rr   r   Nr   c                 L    t                                          |||          S )a  Clone object and apply new group and/or label.

        Applies relabeling to child up to the supplied depth.

        Args:
            label (str, optional): New label to apply to returned object
            group (str, optional): New group to apply to returned object
            depth (int, optional): Depth to which relabel will be applied
                If applied to container allows applying relabeling to
                contained objects up to the specified depth

        Returns:
            Returns relabelled object
        )rp   r;   depthr6   relabelr#   rp   r;   ru   r   s       r   rw   zAdjointLayout.relabel   s!     wwU%uEEEr   c                 2    || j         v r| j         |         n|S )a5  
        Returns the viewable corresponding to the supplied string
        or integer based key.

        Args:
            key: Numeric or string index: 0) 'main' 1) 'right' 2) 'top'
            default: Value returned if key not found

        Returns:
            Indexed value or supplied default
        rZ   )r#   keyr4   s      r   getzAdjointLayout.get   s      "%	!1!1ty~~w>r   c                 r    |                      |d          j        }| j                            |||          S )a  Return the values along the requested dimension.

        Applies to the main object in the AdjointLayout.

        Args:
            dimension: The dimension to return values for
            expanded (bool, optional): Whether to expand values
                Whether to return the expanded values, behavior depends
                on the type of data:
                  * Columnar: If false returns unique values
                  * Geometry: If false returns scalar values per geometry
                  * Gridded: If false returns 1D coordinates
            flat (bool, optional): Whether to flatten array

        Returns:
            NumPy array of values along the requested dimension
        T)strict)get_dimensionnamerB   dimension_values)r#   	dimensionexpandedflats       r   r   zAdjointLayout.dimension_values   s9    $ &&y&>>C	y)))XtDDDr   c                 V   |dk    r| S dt          |t                    r|dd         |d         }t          |t                    rF|t          |           k    r3|dk    r| j        }|dk    r| j        }|dk    r| j        }r|         }|S t          |t                    r-|| j        v r$| j        |         S | j        |                   dS t          |t                    r3|j
        ,|j        %| n |                     fd| D                       S t          d| d          )	z.Index into the AdjointLayout by index or labelr)   Nr   r      c                      g | ]
}|         S r)   r)   )r   el
data_slices     r   rO   z-AdjointLayout.__getitem__.<locals>.<listcomp>  s5     ?O ?O ?OCE @B*~ ?O ?O ?Or   zKey z not found in AdjointLayout.)r   rH   r   rQ   rB   rC   rD   strr/   slicestartstoprc   KeyError)r#   rz   r/   r   s      @r   __getitem__zAdjointLayout.__getitem__   sv   "99K
c5!! 	QRRJa&Cc3 	EC3t99$4$4axx$axx$axx$2$z"24KS!! 		EcTY&6&6!y~%	#z****U## 	E	(9ch>N%-444:: ?O ?O ?O ?OIM?O ?O ?O 4P 4P P C#CCCDDDr   c                     |dv r=t          |t          t          t          f          r|| j        |<   d S t          d          t          d| d          )NrA   z)AdjointLayout only accepts Element types.z	Position z not valid in AdjointLayout.)r   r   r
   r-   r/   rb   rR   )r#   rz   values      r   __setitem__zAdjointLayout.__setitem__  sa    ***%/3CU!KLL N!&	# !LMMMIIIIJJJr   c                 d      fd j         D             }t          d |D             |gz             S )z%Add another plot to the AdjointLayoutc                 F    g | ]}j                             |d           S r   r/   r{   rM   s     r   rO   z,AdjointLayout.__lshift__.<locals>.<listcomp>  s)    CCCAq$''CCCr   c                     g | ]}||S r   r)   rI   s     r   rO   z,AdjointLayout.__lshift__.<locals>.<listcomp>  s    @@@A!-a---r   )rL   r.   )r#   r$   viewss   `  r   r2   zAdjointLayout.__lshift__  sC    CCCC1BCCC@@@@@E7JKKKr   c                 4    | j                                         S r   )rB   ra   rm   s    r   ddimszAdjointLayout.ddims  s    y##%%%r   c                 8    | j                             dd          S )z-Returns the main element in the AdjointLayoutrB   Nr   rm   s    r   rB   zAdjointLayout.main   s     y}}VT***r   c                 8    | j                             dd          S )z7Returns the right marginal element in the AdjointLayoutrC   Nr   rm   s    r   rC   zAdjointLayout.right%  s     y}}Wd+++r   c                 8    | j                             dd          S )z5Returns the top marginal element in the AdjointLayoutrD   Nr   rm   s    r   rD   zAdjointLayout.top*  s     y}}UD)))r   c                     d | j                                         D             }|                     t          |                    S )Nc                 X    g | ]'\  }}t          |t                    r	||j        fn||f(S r)   )r   r	   last)r   rN   rJ   s      r   rO   z&AdjointLayout.last.<locals>.<listcomp>1  sI     0 0 0Q !+1i 8 8D!QVq!f 0 0 0r   )r/   itemsr   rT   )r#   r   s     r   r   zAdjointLayout.last/  sD    0 0!Y__..0 0 0~~d5kk***r   c                 N    t          | j                                                  S r   )rU   r/   keysrm   s    r   r   zAdjointLayout.keys6  s    DINN$$%%%r   c                 N    t          | j                                                  S r   )rU   r/   r   rm   s    r   r   zAdjointLayout.items:  s    DIOO%%&&&r   c              #   |   K   d}|t          |           k     r$| |         V  |dz  }|t          |           k     "d S d S )Nr   r   )rQ   )r#   is     r   __iter__zAdjointLayout.__iter__>  sI      #d))mmq'MMMFA #d))mmmmmmr   c                 *    t          | j                  S )z$Number of items in the AdjointLayout)rQ   r/   rm   s    r   __len__zAdjointLayout.__len__D  s    49~~r   FNNr   r   )TT)"r   r&   r'   r(   r9   Listr   kdimsrL   _deep_indexable_auxiliary_componentr7   rf   rj   propertyr;   rp   setterrw   r{   r   r   r   r2   r   rB   rC   rD   r   r   r   r   r   r<   r=   s   @r   r.   r.   F   sL        " EJ		/ : :;dKKKE+++LO ) ) ) ) )&+" +" +" +"\1 1 1 # # X# 4 4 X4 \   \ 
\   \ F F F F F F$? ? ? ?E E E E,E E E8K K KL L L & & X& + + X+ , , X, * * X* + + X+& & &' ' '        r   r.   c                        e Zd ZdZeeefZd
 fd	Ze	d             Z
e	d             Zd Zd Ze	d             Z fd	Z xZS )NdLayoutz
    NdLayout is a UniformNdMapping providing an n-dimensional
    data structure to display the contained Elements and containers
    in a layout. Using the cols method the NdLayout can be rearranged
    with the desired number of columns.
    Nc                 \    d| _         d | _         t                      j        d||d| d S )N   )initial_itemsr   r)   )	_max_cols_styler6   r7   )r#   r   r   r8   r   s       r   r7   zNdLayout.__init__T  sL     	#}E 	# 	#!	# 	# 	# 	# 	#r   c                 *    t          j        |           S r   )r   uniformrm   s    r   r   zNdLayout.uniform[  s     &&&r   c                     t          |                                           }|| j        k    rd|fS || j        z  }|| j        z  }||rdndz   t          || j                  fS )z@Tuple indicating the number of rows and columns in the NdLayout.r   r   )rQ   r   r   minr#   numnrowslast_row_colss       r   shapezNdLayout.shape`  sl     $))++$.  s8Ot~%dn,=/aaa0#c4>2J2JJJr   c                     t          |                                           g k    ri S | j        fdt          | j                                                  D             S )z
        Compute a dict of {(row,column): (key, value)} elements from the
        current set of items and specified number of columns.
        c                 6    i | ]\  }\  }}|z  |z  f||fS r)   r)   )r   idxrz   itemcolss       r   
<dictcomp>z'NdLayout.grid_items.<locals>.<dictcomp>r  sJ     F F F$C#t cDj)C; F F Fr   )rU   r   r   	enumerater/   r   )r#   r   s    @r   
grid_itemszNdLayout.grid_itemsk  sr    
 		""BY~F F F F(1$)//2C2C(D(DF F F 	Fr   c                     || _         | S a:  Sets the maximum number of columns in the NdLayout.

        Any items beyond the set number of cols will flow onto a new
        row. The number of columns control the indexing and display
        semantics of the NdLayout.

        Args:
            ncols (int): Number of columns to set on the NdLayout
        r   r#   ncolss     r   r   zNdLayout.colsv       r   c                 F   g }|                                  D ]v\  }}t          |t                    r$||                    |j        |j        f          f}n#t          |t                    r
||j        f}n||f}|                    |           w|                     |          S )z
        Returns another NdLayout constituted of the last views of the
        individual elements (if they are maps).
        )r   r   r	   rc   last_keyr   r.   r`   )r#   
last_itemsrN   rJ   r   s        r   r   zNdLayout.last  s     
jjll 	$ 	$FQ!Y'' 177AJ#7889A}-- 16{1vd####zz*%%%r   c                 j     t                      j        |i |}| j        |_        | j        |_        |S )a  Clones the NdLayout, overriding data and parameters.

        Args:
            data: New data replacing the existing data
            shared_data (bool, optional): Whether to use existing data
            new_type (optional): Type to cast object to
            *args: Additional arguments to pass to constructor
            **overrides: New keyword arguments to pass to constructor

        Returns:
            Cloned NdLayout object
        )r6   rc   r   idr#   args	overridesrc   r   s       r   rc   zNdLayout.clone  s6     t1y11.7r   )NN)r   r&   r'   r(   r   r.   r
   	data_typer7   r   r   r   r   r   r   rc   r<   r=   s   @r   r   r   J  s          !-1ABI# # # # # # ' ' X' K K XKF F F   & & X&"        r   r   c                        e Zd ZdZ ej        d d          ZdZd fd	Zd Z	e
d             Z fdZ fd	Zd
 Zd fd	Zd ZddZd Z xZS )r   a  
    A Layout is an ViewableTree with ViewableElement objects as leaf
    values. Unlike ViewableTree, a Layout supports a rich display,
    displaying leaf items in a grid style layout. In addition to the
    usual ViewableTree indexing, Layout supports indexing of items by
    their row and column index in the layout.

    The maximum number of columns in such a layout may be controlled
    with the cols method.
    Tr?   Nc                 T    d| j         d<    t                      j        |||fi | d S )Nr   r   )__dict__r6   r7   )r#   r   
identifierparentkwargsr   s        r   r7   zLayout.__init__  s8    %&k"
F==f=====r   c                 $    ddl m }  ||           S )a  Packs Layout of DynamicMaps into a single DynamicMap that returns a Layout

        Decollation allows packing a Layout of DynamicMaps into a single DynamicMap
        that returns a Layout of simple (non-dynamic) elements. All nested streams are
        lifted to the resulting DynamicMap, and are available in the `streams`
        property.  The `callback` property of the resulting DynamicMap is a pure,
        stateless function of the stream values. To avoid stream parameter name
        conflicts, the resulting DynamicMap is configured with
        positional_stream_args=True, and the callback function accepts stream values
        as positional dict arguments.

        Returns:
            DynamicMap that returns a Layout
        r   	decollater   )r#   r   s     r   r   zLayout.decollate  s$     	)(((((yr   c                     t          |           }|| j        k    rd|fS || j        z  }|| j        z  }||rdndz   t          || j                  fS )z>Tuple indicating the number of rows and columns in the Layout.r   r   )rQ   r   r   r   s       r   r   zLayout.shape  sb     $ii$.  s8Ot~%dn,=/aaa0#c4>2J2JJJr   c                    t          |t                    rN|t          |           k     r,t          | j                                                  |         S t          d          t          |t                    rt          d          t          |          dk    rt          d |D                       s|t          d          t          d          fk    r| S |\  }}|| j	        z  |z   }t          | j        
                                          }|t          |          k    s|| j	        k    rt          d| d          ||         }t                                          |          S )z0Allows indexing Layout by row and column or pathzElement out of range.zdA Layout may not be sliced, ensure that you are slicing on a leaf (i.e. not a branch) of the Layout.r   c                 8    g | ]}t          |t                    S r)   )r   r   )r   rN   s     r   rO   z&Layout.__getitem__.<locals>.<listcomp>  s"    %F%F%FQjC&8&8%F%F%Fr   NzIndex z  is outside available item range)r   r   rQ   rU   r/   r0   r   r   r   r   r   r6   r   )r#   rz   rowcolr   r   r   s         r   r   zLayout.__getitem__  sX   c3 	WSYYDI,,..//442333U## 	W V W W Ws88q==%F%F#%F%F%F!G!G=uT{{E$KK000+HC&,C	(())Dc$ii3$.#8#8MMMMNNNs)Cww""3'''r   c                 R     t                      j        |i |}| j        |_        |S )a  Clones the Layout, overriding data and parameters.

        Args:
            data: New data replacing the existing data
            shared_data (bool, optional): Whether to use existing data
            new_type (optional): Type to cast object to
            *args: Additional arguments to pass to constructor
            **overrides: New keyword arguments to pass to constructor

        Returns:
            Cloned Layout object
        )r6   rc   r   r   s       r   rc   zLayout.clone  s-     t1y11.r   c                     || _         | S r   r   r   s     r   r   zLayout.cols  r   r   r   c                 J    t                                          |||          S )a  Clone object and apply new group and/or label.

        Applies relabeling to children up to the supplied depth.

        Args:
            label (str, optional): New label to apply to returned object
            group (str, optional): New group to apply to returned object
            depth (int, optional): Depth to which relabel will be applied
                If applied to container allows applying relabeling to
                contained objects up to the specified depth

        Returns:
            Returns relabelled object
        rv   rx   s       r   rw   zLayout.relabel  s     wwueU333r   c                 ^      fdt                                                     D             S )Nc                 p    i | ]2\  }\  }}t          t          j        |j                            ||f3S r)   )rH   npunravel_indexr   )r   r   pathr   r#   s       r   r   z%Layout.grid_items.<locals>.<dictcomp>  sT     B B B%C$ b&sDJ77884, B B Br   )r   r   rm   s   `r   r   zLayout.grid_items  sF    B B B B)24::<<)@)@B B B 	Br   Fc                     ddl m} t          t          |f          st          S t          fd| D                       }| j        |_        |S )Nr   )HoloMapc                 (    g | ]}r|z  n|z  S r)   r)   )r   rJ   r$   rd   s     r   rO   z"Layout.__mul__.<locals>.<listcomp>%  s*    GGGQG8q5GGGr   )spacesr   r   r   r   r   r   )r#   r$   rd   r   layouts    ``  r   rf   zLayout.__mul__!  sg    ######%/7!;<< 	"!!GGGGG$GGGHH>r   c                 0    |                      |d          S rh   ri   r"   s     r   rj   zLayout.__rmul__)  rk   r   )NNNr   r   )r   r&   r'   r(   r9   r:   r;   r   r7   r   r   r   r   rc   r   rw   r   rf   rj   r<   r=   s   @r   r   r     s%       	 	 ELD999EO> > > > > >  $ K K XK( ( ( ( ((    $  4 4 4 4 4 4"B B B   1 1 1 1 1 1 1r   r   c                     h | ]F\  }}t          |t                    t          |t                    st          |t                    D|GS r)   )r   r1   
issubclassr   r   )r   _k_vs      r   	<setcomp>r   -  sg     M M Mvr2!"d++M1;B1L1LM4>r64J4JM M M Mr   )r(   numpyr   r9   ro   r   r   r   r   r   r   	ndmappingr	   r
   r   r+   r-   r.   r   r   rU   localsr   __all__r)   r   r   <module>r      s               L L L L L L L L L L L L 2 2 2 2 2 2 2 23 3 3 3 3 3 3 3,X X X X X X X X") ) ) ) )K ) ) )@ @ @ @ @J @ @ @H[ [ [ [ [z+ [ [ [|B1 B1 B1 B1 B1Z B1 B1 B1J $ M M!1!1 M M M N Nr   