a
    ~b1                     @   s   d dl 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 ddlmZ d	d
lmZ d	dlmZmZ d	dlmZ G dd deZee dS )    N   )BoundingBoxdimension_name)Element)	NdMapping
item_check)SliceSheetCoordinateSystem)util   )GridInterface)	Interface	DataError)finite_rangec                   @   s  e Zd ZdZejfZdZdZe	dd Z
e	dd Ze	d(dd	Ze	d
d Ze	dd Ze	d)ddZe	dd Ze	d*ddZe	d+ddZe	dd Ze	d,ddZe	ejfddZe	d-ddZe	g fd d!Ze	d"d# Ze	d$d% Ze	d&d' ZdS ).ImageInterfacezq
    Interface for 2 or 3D arrays representing images
    of raw luminance values, RGB values or HSV values.
    ZimageFc                    s6  |d u r|j }|d u r|j}i }dd || D }t trJtt|  t trt |d j t |d j  }}t	
||jpt	jj}	t	
||jpt	jj}
|	r|
stdt	|d |j\}}}}t	|d |j\}}}}t||f||ffd|d< t|dkr0tt |d j  nt fdd|D  |rd d d d d d	f  |r d d d	d d f  t|t|f} jd d
 }t|dkr|jstnt}||kr|s|dks|d|d ||f | t tjr jdvrtdnt|ts&td ||d|fS )Nc                 S   s   g | ]}t |qS  r   .0dr   r   8lib/python3.9/site-packages/holoviews/core/data/image.py
<listcomp>"       z'ImageInterface.init.<locals>.<listcomp>r   r   z:ImageInterface only supports regularly sampled coordinatesZpointsboundsc                    s   g | ]}t  |j qS r   )npflipudnamer   vddatar   r   r   1   r   r   )r   z`Key dimension values and value array %s shapes do not match. Expected shape %s, actual shape: %s)r      z"ImageInterface expects a 2D array.zPImageInterface may only be used on elements that subclass SheetCoordinateSystem.)kdimsvdims)r$   r%   
isinstancetupledictzipr   Zasarrayr   r   Zvalidate_regular_samplingZrtolZconfigZ
image_rtol
ValueErrorZbound_range
_time_unitr   lenr   ZdstackshapeZ_binnedr   ndarrayndim
issubclassr
   )clsZeltyper!   r$   r%   kwargs
dimensionsZxsZysZxvalidZyvalidlrxdensityZinvertxbtydensityZinvertyZexpectedr-   errorr   r    r   init   sL    
*
zImageInterface.initc                 C   s   dS )z.ImageInterface does not support irregular dataFr   )r1   datasetdimr   r   r   	irregularF   s    zImageInterface.irregularc                 C   s.   |r|j jd d S | |t| fS d S )Nr   )r!   r-   lengthr,   r3   )r1   r<   Zgriddedr   r   r   r-   K   s    zImageInterface.shapec                 C   s(   | |}|dv rtdS |jjS d S )Nr   r   float)get_dimension_indexr   dtyper!   )r1   r<   	dimensionidxr   r   r   rC   S   s    

zImageInterface.dtypec                 C   s   t j|jjd d t jdS )Nr   rC   )r   productr!   r-   Zintp)r1   r<   r   r   r   r?   \   s    zImageInterface.lengthTc                 C   s   d S Nr   )r1   r<   r%   r   r   r   validatea   s    zImageInterface.validatec                 C   s   |j S rH   r    )r1   r<   r3   r   r   r   redime   s    zImageInterface.redimNc           
         s    j }fdd jD }i }|D ]0}| j |jdd}t|dkr"|d ||j< q"|s\|rrt |  S |d ur| jkrt jdkṙ fdd|D }	t|	dkr|d|	f S |d|	d f S |S )	Nc                    s   g | ]}| vr|qS r   r   r   kdr$   r   r   r   l   r   z*ImageInterface.reindex.<locals>.<listcomp>Fexpandedr   r   c                    s   g | ]}  | j qS r   )rB   ndimsr   r<   r   r   r   v   r   .)r!   r$   valuesr   r,   r'   columnsr%   )
r1   r<   r$   r%   r!   Zdropped_kdimsZconstantrL   ZvalsZindsr   r<   r$   r   reindexi   s     (zImageInterface.reindexc                 C   sv   |j |dd}|rPt||}|rL|j|jjkrL| j|dd}| j|dd}|S | j||dd}|rn| |S |S d S )NTstrictr   )axisr   FrN   )get_dimensionr   Zexpand_grid_coordsr-   r!   Z_infer_interval_breaksrR   )r1   r<   r=   ZorderedrO   Zedgesr!   rR   r   r   r   coordsz   s    
zImageInterface.coordsc                 C   s$  |j |dd}||}|dv r|jr|j \}}}}|rN|| }}	|j}
n|| }}	|j}
d|
 d }t|tjrt	
tt||j}|| |	| f}nd|  k rt|jd k rn nZ|d8 }t	|jd d d d |f }|jd ur| ||j}t|t	|t	|}nd}|S )	NTrV   r@   g      ?       @r   r   )NN)rY   rB   r   lbrtr9   r6   r&   r   datetime_typesr   Ztimedelta64introundr+   r,   r%   Z
atleast_3dr!   ZnodataZreplace_valuer   ZnanminZnanmax)r1   objr=   dim_idxr4   r7   r5   r8   ZlowZhighZdensityZhalfdZdranger!   r   r   r   range   s*    


$zImageInterface.rangec                 C   s  | |}|dv r||j \}}	}
}|jjdd \}}t|tjt|	tj }}||
ksd|dkrtj	|f||rvdndd}nF|rt
||
||j}n.t|
| | }t||d  |
|d  |}|	|ks|dkrtj	|f|	|rdndd}nH|rt
|	|||j}n.t||	 | }t|	|d  ||d  |}|rlt||t|d	  }|rf| S |jS |rv|S |S n||j|  krt| k rn nR|jjdkr|jdddd||j f }n|j}t|}|r|j S |S dS dS )
zL
        The set of samples available along a particular dimension.
        r@   Nr   r   zdatetime64[us]rA   rF   r[   r   )rB   r   r\   r!   r-   r&   r   r]   r   ZfullZ
date_ranger+   rA   ZlinspaceZmeshgridabsZflattenTrP   r,   r3   r/   r   )r1   r<   r=   rO   ZflatZcomputeZ
keep_indexra   r4   r7   r5   r8   Zdim2Zdim1ZxdateZydateZxlinZxstepZylinZysteprR   r!   r   r   r   rR      s8    

& 
zImageInterface.valuesc                 C   s"   |j  d}||t|< |S )NrA   )r!   copyZastyper   r   )r1   r<   maskZmask_valZmaskedr   r   r   rf      s    zImageInterface.maskc                    s  dd    D  t fdd|jD }tdd |D sL|j|j|  S |\}}|j \}}}	}
t|t	r|j
du r~|n
t||j
}|jdu r|	n
t|	|j}	t|t	r|j
du r|n
t||j
}|jdu r|
n
t|
|j}
t||f|	|
ffd}t||}||jS )	zH
        Slice the underlying numpy array in sheet coordinates.
        c                 S   s(   i | ] \}}|t |tr t| n|qS r   )r&   r'   slice)r   kZselr   r   r   
<dictcomp>   s   z)ImageInterface.select.<locals>.<dictcomp>c                 3   s*   | ]"}|j  v r |j  ntd V  qd S rH   )r   rg   rK   	selectionr   r   	<genexpr>   s   z(ImageInterface.select.<locals>.<genexpr>c                 S   s   g | ]}t |tqS r   )r&   rg   )r   elr   r   r   r      r   z)ImageInterface.select.<locals>.<listcomp>Nr   )itemsr'   r$   anyr!   Zsheet2matrixidxr   r\   r&   rg   startmaxstopminr   r	   Z	submatrix)r1   r<   Zselection_maskrk   rZ   ZxidxZyidxr4   r7   r5   r8   r   Zslcr   rj   r   select   s&    


zImageInterface.selectc                    sX   t |d dkrF jd jdd |D i}t jf i |  S  fdd|D S )a[  
        Sample the Raster along one or both of its dimensions,
        returning a reduced dimensionality type, which is either
        a ItemTable, Curve or Scatter. If two dimension samples
        and a new_xaxis is provided the sample will be the value
        of the sampled unit indexed by the value in the new_xaxis
        tuple.
        r   r   c                 S   s   g | ]}|d  qS )r   r   )r   sr   r   r   r      r   z)ImageInterface.sample.<locals>.<listcomp>c                    s"   g | ]}| j  | f qS r   )r!   Z_coord2matrix)r   crQ   r   r   r      r   )r,   r$   r   r'   rt   rS   rR   )r1   r<   samplesrt   r   rQ   r   sample   s    
zImageInterface.samplec           
         s  fdd|D fddj D }i dkr6tnttr\t |d< | tdkrd j	d dd	}j	t
d dd	fd
dtjjt
d  D }tj  fddt||D }	n8tfddtt D   fdd D }	t|trvtd ||	dW  d    S 1 sj0    Y  n||	S d S )Nc                    s   g | ]}  |qS r   )rY   r   rQ   r   r   r     r   z*ImageInterface.groupby.<locals>.<listcomp>c                    s   g | ]}| vr|qS r   r   r   Zkdim)r3   r   r   r     r   rawr$   r   r   FrN   c                    s(   g | ] } r|t d fn
t d |fqS rH   )rg   r   i)didxr   r   r     s   c                    s,   g | ]$\}}| | ffi fqS r   r   )r   ru   rv   )r!   group_kwargs
group_typexvalsr   r   r     s   c                    s   g | ]}  |qS r   )dimension_valuesr{   rQ   r   r   r     r   c                    s6   g | ].}|d  j  | j d  gfi fqS rH   )rP   )r   g)r<   r~   r   r   r   r     s   rM   )r$   r(   r0   r   updater   Zget_param_valuesr,   rB   r   rc   rb   r!   r-   r   r   r)   r3   r   r   )
r1   r<   Z	dim_namesZcontainer_typer   r2   r$   rZ   rw   groupsr   )r!   r<   r}   r3   r~   r   r   r   groupby  s6    


"
.zImageInterface.groupbyc                 C   sT   t |st|dkr|S t| d }t|| dkrP||jv rP|| d S dS )z~
        Given a dataset object and data in the appropriate format for
        the interface, return a simple scalar.
        r   r   N)r   isscalarr,   listkeysr%   )r1   r<   r!   keyr   r   r   unpack_scalar$  s
    zImageInterface.unpack_scalarc                    s   dd D t  fdd jD }t| jfd|i|}st jdkrpt|rd|n|d g fS dd	 t j|D g fS nNt|dkrڈd | j	 |d d
d jd j
|d r|d d d n|ig fS d S )Nc                 S   s   g | ]}t |qS r   r   rK   r   r   r   r   4  r   z,ImageInterface.aggregate.<locals>.<listcomp>c                 3   s*   | ]"}|vr j  | d  V  qdS )r   N)rP   rB   ry   rT   r   r   rl   5  s   
z+ImageInterface.aggregate.<locals>.<genexpr>rX   r   r   c                 S   s    i | ]\}}|j t|gqS r   )r   r   Zarray)r   r   vr   r   r   ri   =  r   z,ImageInterface.aggregate.<locals>.<dictcomp>FrN   r"   )r'   r$   r   Z
atleast_1dr!   r,   r%   r   r)   rR   r   )r1   r<   r$   functionr2   Zaxesr!   r   rT   r   	aggregate2  s    "zImageInterface.aggregate)F)T)NN)FFF)TTTF)N)__name__
__module____qualname____doc__r   r.   typesZdatatypeZnamedclassmethodr;   r>   r-   rC   r?   rI   rJ   rU   rZ   rb   rR   nanrf   rt   rx   r   r   r   r   r   r   r   r      sN   
+




 +
"
r   )Znumpyr   Zboundingregionr   rD   r   elementr   Z	ndmappingr   r   Zsheetcoordsr	   r
    r   Zgridr   Z	interfacer   r   r   r   registerr   r   r   r   <module>   s     7