a
    ~bo(                     @   s   zd dl mZ W n ey"   Y n0 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mZmZ ddlmZ G d	d
 d
eZee dS )    N   )	Interface	DataError   dimension_name)Element)	NdMapping
item_checksorted_context)utilc                   @   s  e 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 Zedd Zeg dfddZed*ddZeejfddZed+ddZe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ed'd( ZdS )-ArrayInterfacearrayFc                 C   s
   |j jjS N)datadtypetype)clsdatasetdim r   8lib/python3.9/site-packages/holoviews/core/data/array.pydimension_type   s    zArrayInterface.dimension_typec                    sr  |d u r|j }|d u r|j}dd || D }t tsBt rt fdd|D r fdd|D }ttdd |D dkrt	dt
| n*t trt fd	d|D st  }td
d |D  }t
t| nt trJdd  D  ttdd  D dkr(t	dn |  r@t
  nt	dnzt trt g krtt
dt|f nPt t
jsĈ d u rt
jg ddjnt  zt
  W n   d  Y n0 |d u r|j }|d u r|j} d u s jdks jjdv rt	dnP jdkrb|jrVt|t| dkrVt
t
t  g nt
 j  ||di fS )Nc                 S   s   g | ]}t |qS r   r   .0dr   r   r   
<listcomp>"       z'ArrayInterface.init.<locals>.<listcomp>c                 3   s   | ]}| v V  qd S r   r   r   r   r   r   	<genexpr>$   r   z&ArrayInterface.init.<locals>.<genexpr>c                    s*   g | ]"}t |tjr|nt | qS r   
isinstancenpndarrayZasarrayr   r   r   r   r   %   r   c                 s   s   | ]}|j jV  qd S r   r   kindr   r   r   r   r   &   r   r   z:ArrayInterface expects all columns to be of the same dtypec                 3   s   | ]}| v V  qd S r   r   r   r   r   r   r   )   r   c                 s   s&   | ]\}}t |t | V  qd S r   )r   Z
wrap_tuple)r   kvr   r   r   r   +   s   c                 S   s&   g | ]}t |tjr|nt|qS r   r    r   r   r   r   r   /   r   c                 s   s   | ]}|j jV  qd S r   r$   r   r   r   r   r   0   r   z3ArrayInterface expects data to be of uniform shape.r   r   )Zndmin)SUOz5ArrayInterface interface could not handle input type.)kdimsvdims)r+   r,   r!   dictr   Zis_dataframealllenset
ValueErrorr"   column_stacksorteditemsziplisttupleexpandedemptyr#   r   Tndimr   r%   Z_auto_indexable_1dZarange
atleast_2d)r   Zeltyper   r+   r,   
dimensionsr   Z	dict_datar   r   r   init   sX     

"


$
zArrayInterface.initTc                 C   sL   |rt | n|j}|jjdkr.|jjd nd}||k rHtd| | d S )Nr   zPSupplied data does not match specified dimensions, expected at least %s columns.)r/   r=   ndimsr   r;   shaper   )r   r   r,   r?   Zncolsr   r   r   validateN   s    zArrayInterface.validatec                 C   s*   | |}tt|jd d |f dkS Nr   )get_dimension_indexr/   r"   uniquer   )r   r   r   idxr   r   r   isscalarW   s    
zArrayInterface.isscalarc                    s2   |r( fdd|D } j d d |f S  j S d S )Nc                    s   g | ]}  |qS r   rC   r   r   r   r   r   `   r   z(ArrayInterface.array.<locals>.<listcomp>r   )r   r   r=   indicesr   rH   r   r   ]   s    zArrayInterface.arrayc                 C   s   |j jS r   )r   r   )r   r   	dimensionr   r   r   r   f   s    zArrayInterface.dtypec                 C   s   |j  }tj|||ddS )Nr   )axis)r   copyr"   insert)r   r   rJ   Zdim_posvaluesZvdimr   r   r   r   add_dimensionk   s    
zArrayInterface.add_dimensionc           	         s    j }t|dkr(|  |d  }nN fdd  D }t fdd|D } j |tjj	}|j|dd }|| }|r|d d d S |S )	Nr   r   c                    s   g | ]}|j  jjfqS r   )namer   r   r   rH   r   r   r   w   r   z'ArrayInterface.sort.<locals>.<listcomp>c                 3   s   | ]}  |jV  qd S r   )get_dimensionrP   r   rH   r   r   r   x   r   z&ArrayInterface.sort.<locals>.<genexpr>)order)
r   r/   rN   Zargsortr=   r7   viewr"   Zrecarrayr:   )	r   r   Zbyreverser   ZsortingZdtypesZsort_fieldsZsorted_datar   rH   r   sortq   s    zArrayInterface.sortc           
      C   sH   |j }||}|jdkr&t|j}|d d |f }	|sDt|	S |	S rB   )r   rC   r;   r"   r<   r:   r   Zunique_array)
r   r   r   r8   ZflatZcomputeZ
keep_indexr   Zdim_idxrN   r   r   r   rN      s    


zArrayInterface.valuesc                 C   s   t |j}|||< |S r   )r"   rL   r   )r   r   maskZ
mask_valueZmaskedr   r   r   rW      s    zArrayInterface.maskNc                    s$   || } fdd|D }t |S )Nc                    s   g | ]}  |qS r   )Zdimension_valuesr   rH   r   r   r      r   z*ArrayInterface.reindex.<locals>.<listcomp>)r"   r2   )r   r   r+   r,   Zdimsr   r   rH   r   reindex   s    zArrayInterface.reindexc              	      s  j  fddD fddD }fddjD }j} d d |f  }	|	jj|	jd  }
|	ttj	|
f}tj
|dd\}}|  |	| }i }|dkrt|tr|t ||d	< || g }fd
d D }|D ]tj fddt|D } | d d |f |dkstt|trdfddt|| D n|fi ||tf qt|trtdT td* ||dW  d    W  d    S 1 s0    Y  W d    n1 s0    Y  n||S d S )Nc                    s   g | ]} j |d dqS )T)strictrQ   r   rH   r   r   r      r   z*ArrayInterface.groupby.<locals>.<listcomp>c                    s   g | ]}  |qS r   rG   r   rH   r   r   r      r   c                    s   g | ]}| vr|qS r   r   )r   Zkdim)r=   r   r   r      s   r   T)Zreturn_indexrawr+   c                    s   g | ]}|vr  |qS r   rG   r   )r   r=   r   r   r      s   c                    s(   g | ] \}} d d |f | kqS r   r   )r   iZd_idx)r   groupr   r   r      s   c                    s$   i | ]\}}|j  d d |f qS r   )rP   )r   r\   r   )
group_datar   r   
<dictcomp>   r   z*ArrayInterface.groupby.<locals>.<dictcomp>F)r+   )r   r+   r,   rL   r   itemsizer@   rT   r"   ZvoidrD   rV   
issubclassr   updater   Zget_param_valuesr=   Zlogical_andreduce	enumerater-   appendr7   r	   r
   r   )r   r   r=   Zcontainer_typeZ
group_typekwargsZdim_idxsr+   r,   rI   Zgroup_shaperT   _rE   Zunique_indicesZgroup_kwargsZgrouped_dataZcol_idxsrW   r   )r   r   r=   r]   r^   r   groupby   sD    



\zArrayInterface.groupbyc                 K   sh   |d u r|  ||}| ||}t|j|d d f }t|dkrd|rdt|jdkrd|d|jf }|S )Nr   r   )Zselect_maskindexedr"   r<   r   r/   r,   r?   )r   r   Zselection_maskZ	selectionri   r   r   r   r   select   s    zArrayInterface.selectc           	      C   sb   |j }d}|D ]J}d}t|r&|g}t|D ] \}}||d d |f |kM }q.||O }q|| S )NFT)r   r"   rF   rd   )	r   r   Zsamplesr   rW   sampleZsample_maskr\   r'   r   r   r   rk      s    
zArrayInterface.samplec                 C   s   |j dkr|d S |S )z~
        Given a dataset object and data in the appropriate format for
        the interface, return a simple scalar.
        )r   r   )r   r   )r@   )r   r   r   r   r   r   unpack_scalar   s    
zArrayInterface.unpack_scalarc                    sl    j  }| D ]2\}} |d u r*q |}||d d |f< q fdd| D }t|g| S )Nc                    s"   g | ]\}}  |d u r|qS r   rZ   )r   r   arrrH   r   r   r      r   z)ArrayInterface.assign.<locals>.<listcomp>)r   rL   r4   rQ   rC   r"   r2   )r   r   Znew_datar   r   rm   rE   Znew_colsr   rH   r   assign   s    

zArrayInterface.assignc              	   K   s   | |}t|r"| ||tdn
d|jfg}g }|D ]d\}}	t|tjrb|j|	fddi|}
n||	fddi|}
|	t
|t|
r|
fn|
g q6t|g fS )Nr[   r   rK   r   )rX   r/   rh   r6   r   r!   r"   Zufuncrc   re   ZconcatenaterF   r<   )r   r   r=   functionrf   Z	reindexedZgroupedrowsr&   r]   Zreducedr   r   r   	aggregate   s    

&zArrayInterface.aggregatec                    s   |\}}t |rNt|tjr( |}t |r@ j||f S  |g}nt|tsj fdd|D }t |rz|g} j|d d f d d |f }|jdkrt 	|j
S |S )Nc                    s   g | ]}  |qS r   rG   r   rH   r   r   r     r   z'ArrayInterface.iloc.<locals>.<listcomp>r   )r"   rF   r!   r   Z
basestringrC   r   slicer;   r<   r:   )r   r   indexrp   Zcolsr   r   rH   r   iloc  s    





zArrayInterface.iloc)T)TTTF)NN)N)__name__
__module____qualname__r"   r#   typesZdatatypeZnamedclassmethodr   r>   rA   rF   r   r   rO   rV   rN   nanrW   rX   rh   rj   rk   rl   rn   rq   rt   r   r   r   r   r      sN   

2




3




r   )Zitertools.izipZizipr5   ImportErrorZnumpyr"   Z	interfacer   r   rJ   r   elementr   Z	ndmappingr	   r
   r    r   r   registerr   r   r   r   <module>   s     