
    e-                     z    d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	  G d d	e          Z
 G d
 de
          ZdS )a5  
Data normalization operations.

Normalizing input data into a valid range is a common operation and
often required before further processing. The semantics of
normalization are dependent on the element type being normalized
making it difficult to provide a general and consistent interface.

The Normalization class is used to define such an interface and
subclasses are used to implement the appropriate normalization
operations per element type. Unlike display normalization, data
normalizations result in transformations to the stored data within
each element.
    N   )Overlay)	Operation)
match_spec)Rasterc                        e Zd ZdZ ej        dd          Z ej        i dee	fd          Z
 ej        ddd	
          Zd fd	ZddZd ZddZ xZS )Normalizationaj  
    Base class for all normalization operation.

    This class standardizes how normalization is specified using the
    ranges and keys parameter. The ranges parameter is designed to be
    very flexible, allowing a concise description for simple
    normalization while allowing complex key- and element- specific
    normalization to also be specified.
    Fz
       Whether normalization is allowed to use the minimum and maximum
       values of the existing data to infer an appropriate range)defaultdocTa_  
       The simplest value of this parameter is None to skip all
       normalization. The next simplest value is an empty dictionary
       to only applies normalization to Dimensions with explicitly
       declared ranges.

       The next most common specification is a dictionary of values
       and tuple ranges. The value keys are the names of the
       dimensions to be normalized and the tuple ranges are of form
       (lower-bound, upper-bound). For instance, you could specify:

       {'Height':(0, 200), 'z':(0,1)}

       In this case, any element with a 'Height' or 'z'
       dimension (or both) will be normalized to the supplied ranges.

       Finally, element-specific normalization may also be specified
       by supplying a match tuple of form (<type>, <group>,
       <label>). A 1- or 2-tuple may be supplied by omitting the
       <group>, <label> or just the <label> components
       respectively. This tuple key then uses the dictionary
       value-range specification described above.

      For instance, you could normalize only the Image elements of
      group pattern using:

      {('Image','Pattern'):{'Height':(0, 200), 'z':(0,1)}})


      Key-wise normalization is possible for all these formats by
      supplying a list of such dictionary specification that will then
      be zipped with the keys parameter (if specified).
      )r
   
allow_Noneclass_r   Nz
       If supplied, this list of keys is zipped with the supplied list
       of ranges.

       These keys are used to supply key specific normalization for
       HoloMaps containing matching key values, enabling per-element
       normalization.)r
   r   r   c                 d    |i }t          |||          } t                      j        |fi |S N)rangeskeys)dictsuper__call__)selfelementr   r   params	__class__s        Alib/python3.11/site-packages/holoviews/operation/normalization.pyr   zNormalization.__call__S   s@    >FfF666uww226222    c                     |i }t          |||          }t          j        | |          | _        |                     ||          S r   )r   paramParamOverridesp_process)r   r   keyr   r   r   s         r   process_elementzNormalization.process_elementZ   sH    >FfF666%dF33}}Wc***r   c                      j         d         } j         d         }|i k    r! fd                                D             S ||}n|r$t          |t                    st	          d          t          |          t          |          k    r=	 |                    |          }||         }n-# t          $ r t          d          dw xY wt	          d          t          |          S )zo
        Method to get the appropriate normalization range dictionary
        given a key and element.
        r   r   c                 \    i | ](}|j                             |j         j                  )S  )namerange
data_range).0dr   r   s     r   
<dictcomp>z,Normalization.get_ranges.<locals>.<dictcomp>k   s?     3 3 3 FGMM!&$/BB 3 3 3r   Nz@Key list specified but ranges parameter not specified as a list.z+Could not match element key to defined keysz4Key list length must match length of supplied ranges)
r   
dimensions
isinstancelist
ValueErrorlenindex	ExceptionKeyErrorr   )r   r   r    r   r   specsr0   s   ``     r   
get_rangeszNormalization.get_rangesb   s)   
 vf~!R<<3 3 3 3 3$//113 3 3 3<EE 	U*VT22 	U 9 : : :YY#f++%%X

3u X X XLMMSWWX STTT'5)))s   B. .C	c                      t          d          )NzNormalization not implemented)NotImplementedError)r   viewr    s      r   r   zNormalization._process   s    !"ABBBr   NNN)__name__
__module____qualname____doc__r   Booleanr'   ClassSelectorr   r-   r   Listr   r   r!   r4   r   __classcell__)r   s   @r   r	   r	      s          u 3D E E EJ !U )-t  ;
! ! !FF 5:dt :   D3 3 3 3 3 3+ + + +* * *:C C C C C C C Cr   r	   c                        e Zd ZdZddZd ZdS )raster_normalizationa  
    Normalizes elements of type Raster.

    For Raster elements containing (NxM) data, this will normalize the
    array/matrix into the specified range if value_dimension matches
    a key in the ranges dictionary.

    For elements containing (NxMxD) data, the (NxM) components of the
    third dimensional are normalized independently if the
    corresponding value dimensions are selected by the ranges
    dictionary.
    Nc                 6   t          |t                    r|                     ||          S t          |t                    rK|                    d          }|                                D ]\  }}|                     ||          ||<   |S t          d          )NF)shared_dataz5Input element must be a Raster or subclass of Raster.)r,   r   _normalize_rasterr   cloneitemsr.   )r   rasterr    overlay_clonekels         r   r   zraster_normalization._process   s    ff%% 	V))&#666(( 	V"LLUL;;M D D2$($:$:2s$C$Ca    TUUUr   c                    t          |t                    s|S |                    |j                                                  }|                     ||          }t          d |j        D                       D ]\  }}|                    |d          }d |v r |rut          |j        j
                  dk    rX||         }|j        d d d d fxx         |d         z  cc<   |d         |d         z
  }|r|j        d d d d fxx         |z  cc<   |rQ|j        d d d d |fxx         |d         z  cc<   |d         |d         z
  }|r|j        d d d d |fxx         |z  cc<   |S )Nc              3   $   K   | ]}|j         V  d S r9   )r%   )r(   r)   s     r   	<genexpr>z9raster_normalization._normalize_raster.<locals>.<genexpr>   s$      $B$BQV$B$B$B$B$B$Br   r8   r   r      )r,   r   rG   datacopyr4   	enumeratevdimsgetr/   shape)	r   rI   r    norm_rasterr   depthr%   depth_ranger&   s	            r   rF   z&raster_normalization._normalize_raster   s   &&))8&=ll6;#3#3#5#566--$$B$BV\$B$B$BBB 	9 	9KE4 **T<88K{""X 
9s;#3#9::a??$Tl 111%%%Q7%%%$Q+a.8 3$QQQqqqS)))U2))) 9 111U+++{1~=+++$Q+a.8 9$QQQqqqY///58///r   r9   )r:   r;   r<   r=   r   rF   r$   r   r   rC   rC      sE         	V 	V 	V 	V    r   rC   )r=   r   corer   core.operationr   	core.utilr   r   r   r	   rC   r$   r   r   <module>r]      s            & & & & & & " " " " " "      hC hC hC hC hCI hC hC hCX- - - - -= - - - - -r   