
    e%                         d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZmZ dd
lmZmZ  G d dej                  Z G d de          ZdS )zp
Operations manipulate Elements, HoloMaps and Layouts, typically for
the purposes of analysis or visualization.
    N   )Datasetutil)ViewableElement)Element)Layout)Store)	NdOverlayOverlay)CallableHoloMapc                   R   e Zd ZdZ ej        d d          Z ej        dg dd          Z ej	        i de
efd	
          Z ej        dd          Z ej	        g e
efd          Zg Zg ZdZdZdZg Zed             Zedd            Zed             ZddZddZd Zd ZdS )	Operational  
    An Operation process an Element or HoloMap at the level of
    individual elements or overlays. If a holomap is passed in as input,
    a processed holomap is returned as output where the individual
    elements have been transformed accordingly. An Operation may turn
    overlays in new elements or vice versa.

    An Operation can be set to be dynamic, which will return a
    DynamicMap with a callback that will apply the operation
    dynamically. An Operation may also supply a list of Stream classes
    on a streams parameter, which can allow dynamic control over the
    parameters on the operation.
    z~
       The group string used to identify the output of the
       Operation. By default this should match the operation name.)defaultdocr   )r   TFa  
       Whether the operation should be applied dynamically when a
       specific frame is requested, specified as a Boolean. If set to
       'default' the mode will be determined based on the input type,
       i.e. if the data is a DynamicMap it will stay dynamic.)r   objectsr   Tay  
       Ranges to be used for input normalization (if applicable) in a
       format appropriate for the Normalization.ranges parameter.

       By default, no normalization is applied. If key-wise
       normalization is required, a 2-tuple may be supplied where the
       first component is a Normalization.ranges list and the second
       component is Normalization.keys. )r   
allow_Noneclass_r   Fa  
       If the operation is dynamic, whether or not linked streams
       should be transferred from the operation inputs for backends
       that support linked streams.

       For example if an operation is applied to a DynamicMap with an
       RangeXY, this switch determines whether the corresponding
       visualization should update this stream with range changes
       originating from the newly generated axes.zr
        List of streams that are applied if dynamic=True, allowing
        for dynamic interaction with the plot.)r   r   r   c                     t          |t                    r fd|D             S t          |t          t          f          rfd|D             S t          |t                    r|                              r|gng S dS )z
        Helper method that returns a list of elements that match the
        given path pattern of form {type}.{group}.{label}.

        The input may be a Layout, an Overlay type or a single
        Element.
        c                 F    g | ]}                     |          D ]}|S  )search).0cellelclspatterns      8lib/python3.11/site-packages/holoviews/core/operation.py
<listcomp>z$Operation.search.<locals>.<listcomp>_   s4    OOO4SZZg5N5NOOrBOOOO    c                 >    g | ]}|                               |S r   )matches)r   r   r   s     r   r   z$Operation.search.<locals>.<listcomp>a   s*    @@@2BJJw,?,?@B@@@r    N)
isinstancer   r
   r   r   r"   )r   elementr   s   ` `r   r   zOperation.searchU   s     gv&& 	POOOOO7OOOOg	7344 	A@@@@@@@@)) 	A ' 8 8@G99b@	A 	Ar     c                 r    t          fdD                       r                    d          j        S |S )z
        Returns a label if all the elements of an overlay agree on a
        consistent label, otherwise returns the default label.
        c              3   ^   K   | ]'}|j                             d           j         k    V  (dS r   N)labelgetr   r   overlays     r   	<genexpr>z.Operation.get_overlay_label.<locals>.<genexpr>l   s7      @@"rxQ--@@@@@@r    r   )allr*   r)   )r   r,   default_labels    ` r   get_overlay_labelzOperation.get_overlay_labelf   sC     @@@@@@@@@ 	!;;q>>''  r    c                     t          fdD                       r                    d          j        S t          d          )z
        Returns the extents if all the elements of an overlay agree on
        a consistent extents, otherwise raises an exception.
        c              3   ^   K   | ]'}|j                             d           j         k    V  (dS r(   )boundsr*   r+   s     r   r-   z/Operation.get_overlay_bounds.<locals>.<genexpr>x   s7      BBBry'++a..//BBBBBBr    r   z+Extents across the overlay are inconsistent)r.   r*   r3   
ValueError)r   r,   s    `r   get_overlay_boundszOperation.get_overlay_boundsr   sM     BBBB'BBBBB 	L;;q>>((JKKKr    Nc                    i }| j         D ]!}|                     || |                     "t          |dd          }t          |d          r|j        }|sd|_        |                     ||          }t          |d          r	|s||_        | j        r4t          j                    D ] }t          j	        |||| j        d           !| j
        D ]} || |fi |}| j        rt          |t                    rut          |t                    r`|s^|j                                        |_        |                    |j         | j        di | j        gz             |_        |j        |_        |S )	zw
        Applies the operation to the element, executing any pre- and
        post-processor hooks if defined.
        	_pipelineN
_in_methodTr   )level)
operationsr   )_preprocess_hooksupdategetattrhasattrr8   _process_transfer_optionsr	   loaded_backendstransfer_options_postprocess_hooks_propagate_datasetr#   r   datasetclone_datasetinstancer:   pr7   _transforms)	selfr$   keykwargshookelement_pipeline	in_methodretbackends	            r   _applyzOperation._apply~   s   
 * 	/ 	/DMM$$tW--...."7K>>7L)) 	**I *%)"mmGS))7L)) 	+) 	+!*G! 	 022  &S'4+A     + 	, 	,D$tS++F++CC# 	2
3(@(@ 	27G,,	25>	2"?0022CL,55+6!DM++DF++:  6  CM
 &1CO
r    c                     |S )a  
        Process a single input element and outputs new single element
        or overlay. If a HoloMap is passed into an Operation, the
        individual components are processed sequentially with the
        corresponding key passed as the optional key argument.
        r   )rK   r$   rL   s      r   r?   zOperation._process   s	     r    c                 b     j         rJt          |t                    s5|                     fd|                                D                       S t           d          r j        rY j                                      } j        j	        
                    |            fd                                D              j        
                                j                                       n!t          j          j                   _                             |          S )z
        The process_element method allows a single element to be
        operated on given an externally supplied key.
        c                 6    i | ]\  }}| j         |fi S r   )process_element)r   kr   rL   paramsrK   s      r   
<dictcomp>z-Operation.process_element.<locals>.<dictcomp>   sM     "@ "@ "@&+a #$%9T%9"c%L%LV%L%L "@ "@ "@r    rI   c                 8    i | ]\  }}|j         j        v||S r   )rI   _extra_keywords)r   rX   vrK   s      r   rZ   z-Operation.process_element.<locals>.<dictcomp>   s.    ]]]41aQdfF\=\=\!Q=\=\=\r    allow_extra_keywords)_per_elementr#   r   rF   itemsr>   _allow_extra_keywordsrI   _extract_extra_keywordsr\   r<   _check_paramsparamParamOverridesrS   )rK   r$   rL   rY   extrass   ` `` r   rW   zOperation.process_element   sT   
  	AZ%A%A 	A== "@ "@ "@ "@ "@ "@/6}}"@ "@ "@ A A A4 		[) ^77??&--f555]]]]6<<>>]]]FMM&!!!F  (((()$?C?Y[ [ [DF{{7C(((r    c                 b    t          |          }|                                D ]\  }}t          j        |d          r |            ||<   )t	          |t
          j                  r<t	          |j        t
          j                  rt          |j        |j
                  ||<   t          j         | j                   _         j        j        sd|d<   t	          |t                    r3|                     fd|                                D                       S  j        rt	          |t$                    s j        s*t	          |t&                    r                     |          S nd|vr j        j        |d<    j        |d<    j        |d	<    j        j        |d
<    |j         fi |S )NT)has_depsr^   Fdynamicc                 H    g | ]\  }}|                     ||           fS ))rL   )rS   )r   rX   r   rK   s      r   r   z&Operation.__call__.<locals>.<listcomp>   sH     &D &D &D*/!R ()$++ba+*@*@&A &D &D &Dr    streamsper_elementlink_datasetlink_inputs)dictra   r   is_param_methodr#   re   	ParameterownerParameterizedr=   namerf   rb   rI   rj   r   rF   r`   r   r   rS   rl   rD   ro   apply)rK   r$   rM   rY   rX   r]   s   `     r   __call__zOperation.__call__   s   fLLNN 	5 	5DAq#A555 5ACCq		Au// 5JqwH[4\4\ 5#AGQV44q	%dF;?;UW W Wv~ 
	/ %F9'7++ ,}} &D &D &D &D3:==??&D &D &D E E E$ ,GW)E)E ,(,-7-Q-Q,{{7+++f$$ $F9 $ 1}!%!8~ $ 2}w}T,,V,,,r    )r%   )N)__name__
__module____qualname____doc__re   StringgroupObjectSelectorrj   ClassSelectorrp   tupleinput_rangesBooleanro   listrl   r;   rC   rb   r`   rD   r@   classmethodr   r0   r5   rS   r?   rW   rw   r   r    r   r   r      s         EL 3F G G GE #e"9+C+C+C JAB B BG '5&rd/3Um B,- - -L  %- 45 6 6 6K "e!"dD\ H2 3 3 3G  L  A A [A  ! ! ! [! L L [L% % % %P   ) ) )*- - - - -r    r   c                   F     e Zd ZdZ ej        ed          Z fdZ xZ	S )OperationCallablez
    OperationCallable allows wrapping an Operation and the objects it is
    processing to allow traversing the operations applied on a
    DynamicMap.
    z?
        The Operation being wrapped into an OperationCallable.)r   r   c                 b    d|vrt          d           t                      j        |fi | d S )N	operationz5An OperationCallable must have an operation specified)r4   super__init__)rK   callablerM   	__class__s      r   r   zOperationCallable.__init__   sB    f$$TUUU,,V,,,,,r    )
rx   ry   rz   r{   re   r   r   r   r   __classcell__)r   s   @r   r   r      sh          $#9 ;B C C CI- - - - - - - - -r    r   )r{   re   r%   r   r   	dimensionr   r$   r   layoutr   optionsr	   r,   r
   r   spacesr   r   ParameterizedFunctionr   r   r   r    r   <module>r      s             & & & & & &                   ' ' ' ' ' ' ' ' % % % % % % % %L- L- L- L- L-+ L- L- L-`- - - - - - - - - -r    