a
    ~b0K                     @   s   d dl mZ d dlZd dlZd dlZd dl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
 d
eZG dd deZG dd deZG dd deZG dd dejZdS )    )absolute_importN   )util)Element)	NdMapping   )finite_rangec                       s"   e Zd ZdZd fdd	Z  ZS )	DataErrorz7DataError is raised when the data cannot be interpretedNc                    s.   |d urd || g}tt| | d S )Nz

)joinerrorsuperr	   __init__)selfmsg	interface	__class__ <lib/python3.9/site-packages/holoviews/core/data/interface.pyr      s    zDataError.__init__)N)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r	      s   r	   c                   @   s(   e Zd Zdd Zdd Zedd ZdS )Accessorc                 C   s
   || _ d S Ndataset)r   r   r   r   r   r      s    zAccessor.__init__c                 C   s   ddl m} ddlm} | jj}|s,d| j_zp| | j|}|st||r|jt	| t	| jd|gd}| jj
j| jj
j|g t	| jd|_W |sd	| j_n|sd	| j_0 |S )
Nr   )Dataset   )methodT_perform_getitem)Z
input_typeoutput_typemethod_nameargs)
operationsr"   F)datar   Zoperation.elementr    r   Z
_in_methodr!   
isinstanceinstancetypeZpipeliner%   Z	_pipeline)r   indexr   r    Z	in_methodresZ
getitem_opr   r   r   __getitem__   s.    


zAccessor.__getitem__c                 C   s
   t  d S r   )NotImplementedError)clsr   r*   r   r   r   r!   5   s    zAccessor._perform_getitemN)r   r   r   r   r,   classmethodr!   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZedd ZdS )ilocaK  
    iloc is small wrapper object that allows row, column based
    indexing into a Dataset using the ``.iloc`` property.  It supports
    the usual numpy and pandas iloc indexing semantics including
    integer indices, slices, lists and arrays of values. For more
    information see the ``Dataset.iloc`` property docstring.
    c           	         s<  t |}t|dkr(|d td f}nt|dkr<td|\}}|tu rTtd } j ||f} j j	t 
|r|S |td krnjt|tr  |d  }n*t
|r |g}n fdd|D }fdd|D fdd|D t  jjg j }d	d |D }|s*d
dg} j||dS )Nr   r   r   z=Tabular index not understood, index must be at most length 2.c                    s   g | ]}  |qS r   )get_dimension.0dr   r   r   
<listcomp>]       z)iloc._perform_getitem.<locals>.<listcomp>c                    s   g | ]}| v r|qS r   r   r2   )kdimsr   r   r5   ^   r6   c                    s   g | ]}| v r|qS r   r   r2   vdimsr   r   r5   _   r6   c                 S   s&   g | ]}|t jv rt j| js|qS r   )	Interface
interfacesgriddedr3   Zdtr   r   r   r5   b   s   	dataframeZ
dictionary)r7   r9   datatype)r   
wrap_tuplelenslice
IndexErrorEllipsisr   r0   r7   r9   isscalarr'   
dimensionsnpr1   Zunique_iteratorr?   clone)	r.   r   r*   ZrowsZcolsr&   dimsZ	datatypesr?   r   )r   r7   r9   r   r!   C   s4    



ziloc._perform_getitemNr   r   r   r   r/   r!   r   r   r   r   r0   :   s   r0   c                   @   s   e Zd ZdZedd ZdS )ndlocaK  
    ndloc is a small wrapper object that allows ndarray-like indexing
    for gridded Datasets using the ``.ndloc`` property. It supports
    the standard NumPy ndarray indexing semantics including
    integer indices, slices, lists and arrays of values. For more
    information see the ``Dataset.ndloc`` property docstring.
    c                 C   sr   |}t |}|jjstd|j||}t|r:|S i }t|drPd |d< |j	|fd|jj
g|j
 i|S )Nz4Cannot use ndloc on non nd-dimensional datastructureZboundsr?   )r   r@   r   r<   rC   rK   rG   rE   hasattrrH   r?   )r.   r   indicesdsselectedparamsr   r   r   r!   p   s    


zndloc._perform_getitemNrJ   r   r   r   r   rK   h   s   rK   c                   @   s  e Zd Zi ZdZdZdZdZdZe	dd Z
e	dd Ze	d	d
 Ze	dAddZe	dd Ze	dBddZe	dCddZe	dd Ze	dd Ze	dd Ze	dd Ze	dDddZe	dd Ze	dd  Ze	d!d" Ze	d#d$ Ze	d%d& Ze	dEd'd(Ze	dFd)d*Ze	d+d, Ze	d-d. Ze	d/d0 Ze	d1d2 Z e	d3d4 Z!e	d5d6 Z"e	d7d8 Z#e	d9d: Z$e	d;d< Z%e	d=d> Z&e	d?d@ Z'dS )Gr:   Nr   FTc                 C   s   dS )zI
        Indicates whether the required dependencies are loaded.
        Tr   r.   r   r   r   loaded   s    zInterface.loadedc                 C   s   t || jv S )a8  
        Indicates whether the interface is designed specifically to
        handle the supplied object's type. By default simply checks
        if the object is one of the types declared on the class,
        however if the type is expensive to import at load time the
        method may be overridden.
        )r)   types)r.   objr   r   r   applies   s    	zInterface.appliesc                 C   s   || j |j< d S r   )r;   r?   )r.   r   r   r   r   register   s    zInterface.registerc                 C   sL   |p| j }g }|D ]4}|dus*|jj |kr<|j||g|d}|| q|S )z
        Given a list of Dataset objects, cast them to the specified
        datatype (by default the format matching the current interface)
        with the given cast_type (if specified).
        N)r?   new_type)r?   r   rH   append)r.   datasetsr?   Z	cast_typecastrN   r   r   r   rZ      s    
zInterface.castc                 C   s`   t | jd}d}| jr(d}|d |d< n | jr4d}nd}||  |d< ||d< d	jf i |S )
N)r   z0http://holoviews.org/user_guide/%s_Datasets.htmlza list of tabularZTabularurlr<   Ztabularr?   zZ{interface} expects {datatype} data, for more information on supported datatypes see {url})dictr   multir<   
capitalizeformat)r.   infor[   r?   r   r   r   r      s    zInterface.errorc                    sP  t tr@t}|d u r&|dn|}|d u r<|dn|}tdrt jtrtjt	r|d u rfddj
D }|sj
}j}|j
|v r|j
j
v r|jrjn|jrt fdd|D rdd jd d d	D n|jr~t fd
d|D r~g }jD ]0}	||	}
j|	j|
|
 d}|| qjD ]}||j|ddd qTt|n,d|v rtjr nt  n>t trtfdd|| D nt tjrt|d u rj
} fdd|D }fdd|D r:fdd|D  }g }|D ]}| sbt|dkrbqBz"| ||\}}W  qDW nb t!y    Y nN t"y } z4|v st|dkr|||df W Y d }~n
d }~0 0 qBd}|r<|d \}}}d|j#|f }d$||g}t%&t!t!||t'( d t!||||fS )Nr7   r9   r   c                    s   g | ]}| j v r|qS r   )r?   r=   )eltyper   r   r5      r6   z(Interface.initialize.<locals>.<listcomp>c                 3   s$   | ]}| j v r j | jV  qd S r   )r;   r]   r=   rQ   r   r   	<genexpr>   r6   z'Interface.initialize.<locals>.<genexpr>c                 S   s   g | ]}|qS r   r   r2   r   r   r   r5      r6   columnsc                 3   s   | ]} j | jV  qd S r   )r;   r<   r=   rQ   r   r   rb      r6   )expandedflatF)re   computer>   c                 3   s   | ]}  |V  qd S r   )dimension_valuesr2   r&   r   r   rb      r6   c                    s    g | ]}| j v r j | qS r   )r;   )r3   prQ   r   r   r5      s   
c                    s   g | ]}|  r|qS r   )rU   )r3   intfcrh   r   r   r5      r6   c                    s   g | ]}| d  kr|qS )r   r   )r3   el)headr   r   r5      r6   r   TzUNone of the available storage backends were able to support the supplied data format.r   z%s raised following error:

 %s r   ))r'   r   r   Zget_param_valuesgetrL   r   r)   
issubclassr:   r?   namedr&   r]   anysplitr<   r7   	irregularrg   namerX   r9   valuestuplepddframerc   Zgenerator_typeslistrR   rA   initr	   	Exceptionr   r
   sixZreraisesysexc_info)r.   ra   r&   r7   r9   r?   Zpvalsr   Znew_dataZkdrs   coordsZvdZprioritizedZpriority_errorsrI   Z	extra_kwser   rj   _Zpriority_errorr   )r.   r&   ra   rl   r   
initialize   sx    

( 





*zInterface.initializec                    sB   |rdnd} fdd j |ddD }|r>tdt| | d S )Nallkeyc                    s   g | ]}| j vr|qS r   rh   r2   r   r   r   r5     s   
z&Interface.validate.<locals>.<listcomp>rt   )Zlabelz`Supplied data does not contain specified dimensions, the following dimensions were not found: %s)rF   r	   repr)r.   r   r9   rI   Z	not_foundr   r   r   validate  s    zInterface.validatec                 C   s   |S )zC
        Should return a persisted version of the Dataset.
        r   r.   r   r   r   r   persist  s    zInterface.persistc                 C   s   |S )zB
        Should return a computed version of the Dataset.
        r   r   r   r   r   rf   &  s    zInterface.computec                    s    t  fdd dd  D  S )Nc                 3   s"   | ]}|j  d  j dfvV  qdS )r   )r   N)shape)r3   arrayarraysr   r   rb   /  r6   z%Interface.expanded.<locals>.<genexpr>r   )rq   )r.   r   r   r   r   rd   -  s    zInterface.expandedc                 C   s   t | j||dddkS )NF)rd   r   )rA   ru   )r.   r   dimr   r   r   rE   1  s    zInterface.isscalarc                 C   s4   z|  |||W S  ty.   |  || Y S 0 dS )z
        Compatibility method introduced for v1.13.0 to smooth
        over addition of per_geom kwarg for isscalar method.
        N)rE   	TypeError)r.   r   r   Zper_geomr   r   r   isunique5  s    zInterface.isuniquec                 C   s<   |j |ddj}|j| }t|r2t|gjS |jS d S )NT)strict)r1   rt   r&   r   rE   rG   r   dtype)r.   r   	dimensionrt   r&   r   r   r   r   @  s
    

zInterface.dtypec                 C   s:   | d}||k}t|dr*||tjS t||tjS )z9
        Replace `nodata` value in data with NaN
        Zfloat64where)astyperL   r   rG   NaN)r.   r&   Znodatamaskr   r   r   replace_valueI  s
    

zInterface.replace_valuec              
   C   s  t jt|t jd}| D ]\}}t|tr8t| }| ||}t	
|rpt	jrpzt	|}W n   Y n0 t|trt L tdd |jdur||j|kM }|jdur|||jk M }W d   n1 s0    Y  qt|ttfrTg }|D ]H}t * tdd |||k W d   q1 s60    Y  q|t j|M }qt|rl|||M }q||k}	|jdkrt |	dkrt t || }
t jt|t jd}d||
< q||	M }q|S )a  
        Given a Dataset object and a dictionary with dimension keys and
        selection keys (i.e. tuple ranges, slices, sets, lists, or literals)
        return a boolean mask over the rows in the Dataset object that
        have been selected.
        )r   ignorezinvalid value encounteredNr   r   T)rG   ZonesrA   boolitemsr'   rv   rB   ru   r   Z
isdatetimerw   Zparse_datetime_selectionwarningscatch_warningsfilterwarningsstartstopsetry   rX   Z
logical_orreducecallableZndimssumZargminabsZzeros)r.   r   	selectionr   r   selZarrZ	iter_slcsZikZ
index_maskZ
data_indexr   r   r   select_maskT  sB    




.
0


zInterface.select_maskc                    sB   t |  tdd | D }t fdd|jD }|o@|S )z
        Given a Dataset object and selection to be applied returns
        boolean to indicate whether a scalar value has been indexed.
        c                 s   s,   | ]$}t |ttttf o"t| V  qd S r   )r'   rv   rB   r   ry   r   )r3   r   r   r   r   rb     s   z$Interface.indexed.<locals>.<genexpr>c                 3   s   | ]}| v V  qd S r   r   r2   rO   r   r   rb     r6   )ry   keysr   ru   r7   )r.   r   r   Z
all_scalarZ	all_kdimsr   r   r   indexed  s    zInterface.indexedc              	   C   s   | |}|jjdkr&| | fS t|dkr>tjtjfS zb|jjdvsPJ t	 6 t
dd t|t|t|W  d    W S 1 s0    Y  W nT ttf y   dd t|D }t|stjtjf Y S |d |d f Y S 0 d S )	NMr   ZSUOr   z All-NaN (slice|axis) encounteredc                 S   s   g | ]}|d ur|qS r   r   r3   vr   r   r   r5     r6   z#Interface.range.<locals>.<listcomp>)rg   r   kindminmaxrA   rG   r   r   r   r   r   ZnanminZnanmaxAssertionErrorr   r   Zpython2sort)r.   r   r   columnr   r   r   range  s    

<zInterface.rangec                 C   s.  ddl m}m} |p|}t|tr<|j}t|j  \}}nDt|t	rnt
dd |D rng dgt|  }}ntdt|j |d }|p|jj}|dkr|}n|d	krd
}t|dkr|s| j| jrtd| |j||}|d }|r t	t||n|}	|jj|	||jd}
|j|
||j |dS )zR
        Utility function to concatenate an NdMapping of Dataset objects.
        r   )r   default_datatypec                 s   s   | ]}t |t V  qd S r   )r'   rv   r   r   r   r   rb     r6   z(Interface.concatenate.<locals>.<genexpr>r   zLConcatenation only supported for NdMappings and lists of Datasets, found %s.r   r   ZimageZgridzDatasets with %s datatype cannot be concatenated without defining the dimensions to concatenate along. Ensure you pass in a NdMapping (e.g. a HoloMap) of Dataset types, not a list.r8   )r7   rW   ) r   r   r'   r   r7   zipr&   r   ry   r   rA   r	   r)   r   r   r?   r;   r<   rZ   concatr9   rH   )r.   rY   r?   rW   r   r   rF   r   templater&   Zconcat_datar   r   r   concatenate  s2    
zInterface.concatenatec           
      C   sx   t |rdd lm} |j}n t |r8dd l}|j}ntj}|||||d\}}	t |rp||	}	||}||	fS )Nr   )binsdensityweights)	r   Zis_dask_arrayZ
dask.arrayr   	histogramZis_cupy_arraycupyrG   Zasnumpy)
r.   r   r   r   r   dar   r   ZhistZedgesr   r   r   r     s    




zInterface.histogramc                    s*    fdd|j D }| j|||fi |S )Nc                    s   g | ]}| vr|qS r   r   )r3   Zkdimreduce_dimsr   r   r5     r6   z$Interface.reduce.<locals>.<listcomp>)r7   Z	aggregate)r.   r   r   functionkwargsr7   r   r   r   r     s    zInterface.reducec                 C   s   t ||S r   )r   r   r.   r   rF   r   r   r   r     s    zInterface.arrayc                 C   s   t ||S r   )r   rx   r   r   r   r   rx     s    zInterface.dframec                 C   s   t ||S r   )r   rc   r   r   r   r   rc     s    zInterface.columnsc                 C   s   |j jS r   )r&   r   r   r   r   r   r     s    zInterface.shapec                 C   s
   t |jS r   )rA   r&   r   r   r   r   length  s    zInterface.lengthc                 C   s   t | |S r   )r   r   r   r   r   r   nonzero  s    zInterface.nonzeroc                 C   s   |j S r   rh   r   r   r   r   redim  s    zInterface.redimc                 C   s   dS )NFr   r   r   r   r   	has_holes  s    zInterface.has_holesc                 C   s@   |  ||jd }tt|dd }g gt|d  gS )Nr   floatr   )ru   r7   rG   r   Zisnanr   rA   )r.   r   r   Zsplitsr   r   r   holes  s    zInterface.holesc                 C   s   |  S )zw
        Returns the data of a Dataset as a dataframe avoiding copying
        if it already a dataframe type.
        )rx   r   r   r   r   	as_dframe  s    zInterface.as_dframe)NN)N)T)F)NN)TN)(r   r   r   r;   r?   rS   r<   r]   rp   r/   rR   rU   rV   rZ   r   r   r   r   rf   rd   rE   r   r   r   r   r   r   r   r   r   r   rx   rc   r   r   r   r   r   r   r   r   r   r   r   r:      s   




M	








,

%









r:   )Z
__future__r   r}   r   r|   ZparamZnumpyrG   r   r   elementr   Z	ndmappingr   r   
ValueErrorr	   objectr   r0   rK   ZParameterizedr:   r   r   r   r   <module>   s   	!.