a
    ~b                     @   s   d dl mZ d dlZd dlZd dlmZmZ zd dlm	Z
 W n eyN   Y n0 d dlZddlm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mZmZ ddlmZ ddlmZmZm Z m!Z! G dd deZ"e#e" dS )    )absolute_importN)OrderedDictdefaultdict   )DictInterface)	Interface	DataError   dimension_name)Element)r   )	NdMapping
item_checksorted_context)util)finite_rangeis_daskdask_array_moduleget_array_typesc                   @   s  e Zd ZdZeeefZdZ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
dd Ze
dd Ze
dd Ze
dFddZe
dd Ze
dGddZe
dHddZe
d g fd!d"Ze
d#d$ Ze
d%d& Ze
d'd( Ze
d)d* Ze
dId+d,Ze
d-d. Ze
d/d0 Ze
dJd1d2Z e
e!j"fd3d4Z#e
g fd5d6Z$e
d7d8 Z%e
d9d: Z&e
d;d< Z'e
g dfd=d>Z(e
d?d@ Z)e
dAdB Z*e
dCdD Z+d S )KGridInterfacea  
    Interface for simple dictionary-based dataset format using a
    compressed representation that uses the cartesian product between
    key dimensions. As with DictInterface, the dictionary keys correspond
    to the column (i.e. dimension) names and the values are NumPy arrays
    representing the values in that column.

    To use this compressed format, the key dimensions must be orthogonal
    to one another with each key dimension specifying an axis of the
    multidimensional space occupied by the value dimension data. For
    instance, given an temperature recordings sampled regularly across
    the earth surface, a list of N unique latitudes and M unique
    longitudes can specify the position of NxM temperature samples.
    gridTc                    s  |d u r|j }|d u r|j}|s(tdt|dd || D }tdd |D }t trt t|krt d krt d jd kr d }dd	 t| d d D  | |< nd
d	 t| D  nt trd g krdt|dkrt	dd |D  nNt	dd |D  t|dkrHt
d |d j< nt
ddt|f |< nt fdd| j D sdd	 t|t  D  nNt t
jr jdkrt|dkr }t	dd |D  | |d j< n jddt|fkr" }t	dd |D  | |< nf jdkrr|jrft|t| dkrft
t
t  g nt
 j  fdd	t|D  nbt tr؈ g krdd	 |d  D   fdd	|d  D  nt tstdt|}	| v r$t | t s,t
 |  |< n|	|7 }	|	D ]H}
t|
}| vrRtd|
 t | t s0t
 |  |< q0dd |D }| v r|g}ndd |D }t fdd|D }|r҈ |d  jnd}t|dkr|n|d d d }t fdd|D }|D ]} | j}t|dkr0tnt}| v rd|d t|krX|d|d d }|st|dkstt|f| dkrt|dkrn\t|t|kr|d|t|t|f n.td d t||D r|d!|||f | q ||d"i fS )#Nz>GridInterface interface requires at least one value dimension.c                 S   s   g | ]}t |qS  r
   .0dr   r   7lib/python3.9/site-packages/holoviews/core/data/grid.py
<listcomp><       z&GridInterface.init.<locals>.<listcomp>c                 s   s   | ]}t |V  qd S Nr
   r   vdr   r   r   	<genexpr>=   r   z%GridInterface.init.<locals>.<genexpr>r   c                 S   s   i | ]\}}||qS r   r   r   r   vr   r   r   
<dictcomp>C   r   z&GridInterface.init.<locals>.<dictcomp>c                 S   s   i | ]\}}||qS r   r   r#   r   r   r   r%   F   r   c                 S   s   g | ]}|g fqS r   r   r   r   r   r   r   I   r   c                 S   s   g | ]}|j tg fqS r   namenparrayr   r   r   r   r   K   r   )r   r   r   c                 3   s(   | ] }t  td d |jD V  qdS )c                 s   s   | ]}|d ur|V  qd S r   r   )r   tr   r   r   r!   P   r   z/GridInterface.init.<locals>.<genexpr>.<genexpr>N)
isinstancetupletypes)r   	interfacedatar   r   r!   P   s   c                 S   s   i | ]\}}||qS r   r   r   kr$   r   r   r   r%   R   r   c                 S   s   g | ]}|j tg fqS r   r&   r   r   r   r   r   V   r   c                 S   s   g | ]}|j tg fqS r   r&   r   r   r   r   r   Z   r   c                    s"   i | ]\}}| d d |f qS r   r   )r   ir2   r/   r   r   r%   b   r   c                 S   s   i | ]}|t g qS r   )r(   r)   r   r   r   r   r%   d   r   c                    s   i | ]}|t d   qS ))r   )r(   emptyr   )ndimsr   r   r%   e   r   z;GridInterface must be instantiated as a dictionary or tuplez!Values for dimension %s not foundc                 S   s   g | ]}t |qS r   r
   r   r   r   r   r   x   r   c                 S   s   g | ]}t |qS r   r
   r   r   r   r   r   |   r   c                    s   g | ]}t  | qS r   lenr   kdr/   r   r   r   ~   r   r   c                    s   g | ]} | j qS r   shaper8   r/   r   r   r      r   zKThe shape of the value array does not match the number of value dimensions.r   zThe shape of the %s value array does not match the expected dimensionality indicated by the key dimensions. Expected %d-D array, found %d-D array.c                 s   s&   | ]\}}||ko|d  |kV  qdS r   Nr   )r   ser   r   r   r!      r   z`Key dimension values and value array %s shapes do not match. Expected shape %s, actual shape: %s)kdimsvdims) r@   rA   
ValueErrorr7   r,   r+   r;   ziplistr   r(   zerosr'   anyZ
interfacesvaluesZndarrayndimZ_auto_indexable_1dZcolumn_stackarangeZ
atleast_2dT	enumerateupdatedict	TypeErrorr   r)   r   r   set)clsZeltyper0   r@   rA   
dimensions
vdim_tupleZvalue_arrayr)   Zvalidate_dimsdimr'   Z
kdim_namesZ
vdim_namesZexpectedZirregular_shapeZvalid_shapeshapesvdimr;   errorr   )r0   r5   r   init0   s    
 


"


 

6zGridInterface.initc                    s   ddl m  td: t||d}| fdd|j D }W d    n1 sT0    Y  t|jdkr|	|jd d j }
fdd|D |jd d S ||jd	 S )
Nr   DatasetFr@   c                    s(   g | ] \}}|t | r|jn|fqS r   )r+   r0   r1   rX   r   r   r      s   z(GridInterface.concat.<locals>.<listcomp>r"   c                    s&   g | ]\}}| j ||jd fqS )rA   )concatr@   r1   )rP   rA   r   r   r      r   r   ) rY   r   r   Zcloner0   itemsr7   r@   groupbyr\   
concat_dim)rP   datasetsrQ   rA   r^   r   )rY   rP   rA   r   r\      s    
&zGridInterface.concatc           
         s   t |  \}}fdd|d  D }t|||j< D ]x  fdd|D }tdd |D }t|dkrtd	| j jf t	d
d |D rt
 jntj}	|	|d| j< q>|S )Nc                    s   i | ]\}}| vr||qS r   r   r1   r[   r   r   r%      r   z,GridInterface.concat_dim.<locals>.<dictcomp>r   c                    s   g | ]}| j  qS r   r'   )r   r   )rU   r   r   r      r   z,GridInterface.concat_dim.<locals>.<listcomp>c                 s   s   | ]}|j V  qd S r   r:   r   arrr   r   r   r!      r   z+GridInterface.concat_dim.<locals>.<genexpr>r   zyWhen concatenating gridded data the shape of arrays must match. %s found that arrays along the %s dimension do not match.c                 s   s   | ]}t |V  qd S r   )r   rc   r   r   r   r!      r   r"   )rC   r^   r(   r)   r'   rO   r7   r   __name__rF   r   stack)
rP   ra   rS   rA   rG   Zgridsnew_dataarraysrT   rf   r   )rU   rA   r   r`      s    
 zGridInterface.concat_dimc                 C   s   |j t| jdkS )Nr   )r0   r   rH   )rP   datasetrS   r   r   r   	irregular   s    zGridInterface.irregularc                 C   s,   | j ||dd}|jdv p*tt|dkS )NFexpanded)r   r<   r   )rG   r;   r7   r(   unique)rP   ri   rS   rG   r   r   r   isscalar   s    zGridInterface.isscalarc                    sP   |rdnd} fdd j |ddD }|rLt| jvrLtdt| | d S )Nallkeyc                    s   g | ]}| j vr|qS r   r/   r   ri   r   r   r      s   
z*GridInterface.validate.<locals>.<listcomp>r'   labelz`Supplied data does not contain specified dimensions, the following dimensions were not found: %s)rQ   r,   r0   r   repr)rP   ri   rA   dimsZ	not_foundr   rq   r   validate   s    zGridInterface.validatec                 C   s*   ||  v r| ||dd}nd S |jjS )NF)rQ   rG   dtypetype)rP   ri   rS   rd   r   r   r   dimension_type   s    zGridInterface.dimension_typec                 C   s&   t dd |jD }||jv r"|S dS )Nc                 s   s   | ]}|j V  qd S r   rb   r   r   r   r   r!      r   z'GridInterface.packed.<locals>.<genexpr>F)r,   rA   r0   )rP   ri   rR   r   r   r   packed   s    zGridInterface.packedc                 C   sl   |j |ddj}| |}|r@||v r@|j| d||f }n
|j| }t|rbt|gj	S |j	S d S )NTstrict.)
get_dimensionr'   rz   r0   indexr   rn   r(   r)   rw   )rP   ri   	dimensionr'   rR   r0   r   r   r   rw      s    


zGridInterface.dtypeFc                 C   s`   |  |}|r$|j| jd d }n|j|jd j j}|r@|S tj|tjdt|	 fS d S )Nr"   r   rw   )
rz   r0   r;   rA   r'   r(   productZintpr7   rQ   )rP   ri   griddedrR   r;   r   r   r   r;      s    
zGridInterface.shapec                 C   s   |  |d S )Nr   r:   rP   ri   r   r   r   length   s    zGridInterface.lengthr   c                    s
  t |}tjjdkr>t|r>t|d tjtj	fr>|
d}|j  dkr\t jg |jdS |j  dkr~dt j| d }nt dg}t j|dg dt j|dg d }t j|dg dt j|dg d }t fd	d
t|jD }t j||| | |g dS )a  
        >>> GridInterface._infer_interval_breaks(np.arange(5))
        array([-0.5,  0.5,  1.5,  2.5,  3.5,  4.5])
        >>> GridInterface._infer_interval_breaks([[0, 1], [3, 4]], axis=1)
        array([[-0.5,  0.5,  1.5],
               [ 2.5,  3.5,  4.5]])
        r	   r   Z
datetime64r   r   g      ?axisr"   c                 3   s(   | ] }| krt d dnt d V  qd S )Nr"   )slice)r   nr   r   r   r!     s   z7GridInterface._infer_interval_breaks.<locals>.<genexpr>)r(   asarraysysversion_infomajorr7   r+   dtdatetimedateastyper;   r)   rw   diffZtaker,   rangerH   concatenate)rP   Zcoordr   ZdeltasfirstZlastZ	trim_lastr   r   r   _infer_interval_breaks   s    	
*
$$z$GridInterface._infer_interval_breaksc                 C   s^  |j |dd}| ||}|s"|r~|r4|j|j }nt||}|rz|j|j|jd j jkrz| j|dd}| j|dd}|S |j|j }|rt	
|dd |dd k r|ddd }| |d}||jv r||}	||jv ot||jkot|||j|	 d  d k}
nd}
|r4|
s4| |}n&|sZ|
rZ|dd t	|d	  }|S )
z
        Returns the coordinates along a dimension.  Ordered ensures
        coordinates are in ascending order and expanded creates
        ND-array matching the dimensionality of the dataset.
        Tr{   r   r   r   Nr"   Fg       @)r}   rj   r0   r'   r   Zexpand_grid_coordsr;   rA   r   r(   ro   r@   get_dimension_indexr7   r5   r   )rP   ri   rS   orderedrl   edgesrj   r0   r;   idxZisedgesr   r   r   coords  s2    "
zGridInterface.coordsNc           
         s~  |du r j dddddd } fdd|D fddD rxt|jkrf| }ntj|td	}t fd
djD sЇfddjD }fdd|D }|r|	|ddd }d}g }jddd D ]b} 
|}	t|	dd |	dd k r<|	jdks<|tddd d}q|td q|r^|t| n|}tjdk rz| }|S )a   
        Canonicalize takes an array of values as input and reorients
        and transposes it to match the canonical format expected by
        plotting functions. In certain cases the dimensions defined
        via the kdims of an Element may not match the dimensions of
        the underlying data. A set of data_coords may be passed in to
        define the dimensionality of the data, which can then be used
        to np.squeeze the data to remove any constant dimensions. If
        the data is also irregular, i.e. contains multi-dimensional
        coordinates, a set of virtual_coords can be supplied, required
        by some interfaces (e.g. xarray) to index irregular datasets
        with a virtual integer index. This ensures these coordinates
        are not simply dropped.
        Nrp   r'   rr   r"   c                    s$   g | ]}t  |t r|qS r   )r+   r   r   )r   r'   r   r   r   r   O  s   z.GridInterface.canonicalize.<locals>.<listcomp>c                    s$   g | ]}| j  vr|qS r   )r@   r~   r   )ri   ru   virtual_coordsr   r   r   Q  s   r   c                 3   s   | ]}  |V  qd S r   )rj   r   r   r   r   r!   Y  r   z-GridInterface.canonicalize.<locals>.<genexpr>c                    s   g | ]}  |jqS r   )r~   r'   r8   )ru   r   r   r   Z  r   c                    s&   g | ]  t  fd dD  qS )c                    s   g | ]} |krd qS r<   r   r   r3   r   r   r   [  r   z9GridInterface.canonicalize.<locals>.<listcomp>.<listcomp>)sum)r   )droppedr   r   r   [  r   Fr   Tr	   )rQ   r7   rH   flattenr(   squeezer,   rF   r@   	transposer   ro   appendr   )
rP   ri   r0   Zdata_coordsr   indsinvertZslicesr   r   r   )rP   ri   ru   r   r   r   canonicalize;  s2    
,zGridInterface.canonicalizec           	      C   s   t |r|| S t|trd|j|j }}d\}}|d urB|| }|d urR|| }t|d |d S t|tjrg }|D ]}|||  qx|S )N)NNr   )	r(   rn   r+   r   startstopr   Iterabler   )	rP   r~   r   r   r   Z	new_startZnew_stopZ	new_indexindr   r   r   invert_indexq  s    

zGridInterface.invert_indexc                    s8  i  g }d}t t|jd d d |D ]J\}\}}| ||jd}t|rT|g}nd}||  |j< || q&|jD ]*}|j vrx| ||j}| |j< d}qx| D ]p}	|	|jv r| 	||	sq| j
||	ddd}
|r
t|jdkr
|
tdd |D    S |
t|  |	j< qt fdd| D S )	NTr"   F)flatcomputer   c                 s   s   | ]}|d  V  qdS )r   Nr   r   r   r   r   r   r!     r   z&GridInterface.ndloc.<locals>.<genexpr>c                 3   s   | ]} |j  V  qd S r   rb   r   Zselectedr   r   r!     r   )rK   rC   r@   r   r'   r(   rn   r   rQ   rj   rG   r7   rA   r,   )rP   ri   indicesZadjusted_indsZ
all_scalarr3   r9   r   r   r   rd   r   r   r   ndloc  s.    &



zGridInterface.ndlocc                    s   t    fdd|j D S )Nc                    s.   i | ]&\}}| r&t | jr&| n|qS r   )r+   Arraypersistr1   dar   r   r%     s   z)GridInterface.persist.<locals>.<dictcomp>r   r0   r^   r   r   r   r   r     s    
zGridInterface.persistc                    s   t    fdd|j D S )Nc                    s.   i | ]&\}}| r&t | jr&| n|qS r   )r+   r   r   r1   r   r   r   r%     s   z)GridInterface.compute.<locals>.<dictcomp>r   r   r   r   r   r     s    
zGridInterface.computec                 C   s   |j |dd}||jv s*|j|j jdkr| |}|rT|j| d|j|f }	n|j|j }	|rp| ||	}	t }
|r|
rt	|	|
j
r|	 }	|r|	j S |	S |r| j||jd|d}	|r|	j S |	S | j||j|dS d S )NTr{   r   .)rl   r   )r   )r}   rA   r0   r'   rH   rz   r~   r   r   r+   r   r   rJ   r   r   )rP   ri   rS   rl   r   r   Z
keep_indexr   rR   r0   r   r   r   r   rG     s     
zGridInterface.valuesc                    s  fdd|D d|v r$|d }nfddj D }||d< fddD }|rzt|dkrnd|d  }td	| i |d
krtn|}t|trt n
|	d | t
fdd|D } fddD }	fdd|D fddtjD 7 g }
tt|	 D ]:}tt||}|rljf i |}t|rb|n| }n jfi |}t|st|t r|jdkrjd jt|i}t||D ]\}}t|||< qn`|s@t|t rjd j|i}jD ]4}||j }|d d d }t|||j< q
||fi }|
t||f q(t|trtd ||
dW  d    S 1 s0    Y  n||
S d S )Nc                    s   g | ]} j |d dqS Tr{   r}   r   rq   r   r   r     r   z)GridInterface.groupby.<locals>.<listcomp>r@   c                    s   g | ]}| vr|qS r   r   r   kdim)rQ   r   r   r     r   c                    s"   g | ]} j |j jd kr|qS r<   )r0   r'   rH   r   rq   r   r   r     r   r   z'%s'r   z3Cannot groupby irregularly sampled dimension(s) %s.rawc                 3   s   | ]}| d  vV  qdS )r@   Nr   r   )group_kwargsr   r   r!     r   z(GridInterface.groupby.<locals>.<genexpr>c                    s   g | ]}  |jqS r   )r   r'   r   r   r   r   r     r   c                    s"   g | ]} j  j| d  qS r<   r5   r@   r~   r8   rq   r   r   r     r   c                    s   g | ]}| vr|qS r   r   r   r3   )r   r   r   r     r   r   r"   FrZ   )r@   r7   rB   rM   
issubclassr   rL   r   Zget_param_valuespoprF   r   r5   rC   Zcartesian_productselectr(   rn   columnsr+   r   r;   rA   r'   
atleast_1dr   r   r   r,   r   r   )rP   ri   Z	dim_namesZcontainer_typeZ
group_typekwargsr@   ZinvalidZdrop_dimkeysZgrouped_dataZ
unique_keyr   Z
group_datarS   r$   rU   r0   r   )rP   ri   rQ   r   r   r   r_     sZ    



&


.zGridInterface.groupbyc           	      C   st  t jr|jjdkrt |}t|tr.t| }t|t rB|}nt|trd}|j	d urh||j	|kM }|j
d ur|||j
k M }|du rtj|jtjd}nt|ttfrg }|D ]}|||k qtj|}nt|r||}np|d u rd }nb||k}|jdks|jrRt|dkrRtt|| }tjt|tjd}d||< n|}|d u rptj|jtd}|S )NMTr   r   r   )r   pdrw   kindZparse_datetime_selectionr+   r,   r   r   r   r   r(   Zonesr;   boolrO   rD   r   Z
logical_orreducecallabler5   _binnedr   ZargminabsrE   r7   )	rP   ri   rG   r   maskZ	iter_slcsZikZ
index_maskZ
data_indexr   r   r   key_select_mask  s@    





$

zGridInterface.key_select_maskc                    s  |d urt d| j|j}fdd|jD }|r>td| |}fdd|D }i  g }t|D ]\}	\}
}| ||
}| 	||
|}| 
|||}|rt|st|ttfrtd||
f |j|	d}n|jr| j	||
dd	d
}t|}t|r| |  }}||k r>td|||
f n||krZtd|||
f tt|g|d d dg}tjt|tjd}d	||< |||d  }n0t|r|| | d  }n|dd }n|| }t|t| }}|| t|rt|gn| |
j< qldd |D d d d }tjdd |D  }|jD ]j}| ||
rVt }|rt|j|j |jr|j|j j |  |j< nt |j |  |j< qV|jD ]^}t }|rt|j|j |jr|j|j j |  |j< nt|j|j |  |j< q|rt|jdkrt }t! |jd j }|rvt||jrv|" }t|r|S |d S t fdd|jD S  S )Nz2Masked selections currently not supported for {0}.c                    s   g | ]}| v r|qS r   r   r   rU   	selectionr   r   r   .  r   z(GridInterface.select.<locals>.<listcomp>z^Cannot slice value dimensions in compressed format, convert to expanded format before slicing.c              	      s&   g | ]}|  |j  |jfqS r   )getr'   rs   r   r   r   r   r   4  s   ztIndexing not supported for irregularly sampled data. %s value along %s dimension.must be a slice or 2D boolean mask.r   FT)r   z6Index %s less than lower bound of %s for %s dimension.zBIndex %s more than or equal to upper bound of %s for %s dimension.r   r   r   r	   c                 S   s   g | ]}t |qS r   )r(   argwhere)r   r$   r   r   r   r   \  r   r"   c                 S   s0   g | ](}|j d kr"tt|nt|qS r<   )rH   r(   r   r   r   r   r   r   r   ]  s   r   c                    s   g | ]}t  |j qS r   )r(   r   r'   r   r/   r   r   r   w  s   )#rB   formatre   r@   rA   
IndexErrorindexedrK   rj   r   r   r(   rn   r+   rO   rD   maxr   r   minZdigitizerE   r7   r   r   r   r)   r'   Zix_r   r0   r   vindexr   r   )rP   ri   Zselection_maskr   rQ   Zval_dimsr   Zfull_selectionZvalue_selectr3   rS   r   rj   rG   r   r   r   ZeminZemaxr   int_indsr~   r   r   rU   rd   r   )r0   r   r   r   (  s    





$


 zGridInterface.selectc              	   C   s   |  ||}| |}t|j}|rf|j|  }z|||< W q tyb   |d}|||< Y q0 n`|jD ]X}||j   ||j< }z|||< W ql ty   |d ||j< }|||< Y ql0 ql|S )Nfloat)	r   rz   r   r0   copyrB   r   rA   r'   )rP   ri   r   Zmask_valrz   Zmaskedr    Zmarrr   r   r   r   |  s$    



zGridInterface.maskc                    s   j } jdd} fdd jD }tt}|D ]PtrHgt|krjfddt|D g g  }}t	|D ]X\}	}
 j
|	 }|  ||
}|du rtt|nt|}|| |||  q~t	|tj| D ]\}	}||	 | qt	 j|D ]v\}}t }tt|ddd |j}|rjt||jrj||j | jt|  n||j |j|  qq2dd	 | D }|S )
zC
        Samples the gridded data into dataset of samples.
        r'   rr   c                    s   g | ]} j |j qS r   )r0   r'   r   rq   r   r   r     r   z(GridInterface.sample.<locals>.<listcomp>c                    s$   g | ]}|t  k r | nd qS r   r6   r   )sampler   r   r     s   Nr"   c                 S   s    i | ]\}}|t | qS r   )r(   r   r   )r   r   rh   r   r   r   r%     r   z(GridInterface.sample.<locals>.<dictcomp>)r5   rQ   rA   r   rD   r(   rn   r7   r   rC   r0   r   rI   r   r   Zmeshgridr   Zravel_multi_indexr,   r;   r+   r   r'   r   r   r   r^   )rP   ri   Zsamplesr5   rQ   rh   r0   Zsampledr   r   r   Zcdatar   r   rd   rU   r)   r   Z
flat_indexZconcatenatedr   )ri   r   r   r     s6    



 
 zGridInterface.samplec              	      s   dd D  fddD }t  fdd jD }t }g }|  }	|	r j|	 }
|rz||
fd|i|||	< q|
||	< nl jD ]d} j|j }
t|
r|jnt	j}z"|||
fd|i|||j< W q t
y   || Y q0 q||fS )Nc                 S   s   g | ]}t |qS r   r
   r8   r   r   r   r     r   z+GridInterface.aggregate.<locals>.<listcomp>c                    s   i | ]}| j | qS r   r/   r   rq   r   r   r%     r   z+GridInterface.aggregate.<locals>.<dictcomp>c                 3   s*   | ]"}|vr j  | d  V  qdS r=   )r5   r   r   ri   r@   r   r   r!     s   
z*GridInterface.aggregate.<locals>.<genexpr>r   )r,   r@   r   rz   r0   rA   r'   r   r   r(   rN   r   )rP   ri   r@   functionr   r0   axesr   r   rR   rG   rU   r   r   r   r   	aggregate  s*    



"zGridInterface.aggregatec                    sB  fdd j D r,fdd jD ng i }D ]0}| j |jdd}t|dkr8|d ||j< q8fdd	 j D }t|tkr" t fd
dD }t fddD }	D ]L}
||
j }t|dkr||d d d }|	rt	j
||	d}|||
j< q|S r>t   S |S )Nc                    s   g | ]}| vr|qS r   r   r8   rZ   r   r   r     r   z)GridInterface.reindex.<locals>.<listcomp>c                    s   g | ]}| vr|qS r   r   r   r[   r   r   r     s   Frk   r   r   c                    s"   i | ]\}}|  vr||qS r   r   )r   r2   rG   )dropped_kdimsdropped_vdimsr   r   r%     s   z)GridInterface.reindex.<locals>.<dictcomp>c                 3   s$   | ]} j  j| d  V  qdS r=   r   r   rq   r   r   r!     s   z(GridInterface.reindex.<locals>.<genexpr>c                 3   s"   | ]} j | d  V  qdS r=   )r5   r~   r   )ri   joined_dimsr   r   r!     s   r"   r   )r@   rA   rG   r'   r7   r0   r^   r,   r   r(   r   r   )rP   ri   r@   rA   Zconstantr9   Zvalsr0   r   Zdropped_axesrU   Zvdatar   )ri   r   r   r   r@   rA   r   reindex  s<    
zGridInterface.reindexc                 C   s*   |st dt|}t|jfi ||iS )Nz3Cannot add key dimension to a dense representation.)	Exceptionr   rM   r0   )rP   ri   r   Zdim_posrG   rU   rS   r   r   r   add_dimension  s    zGridInterface.add_dimensionc                 C   s(   |r||j | fv r|jS tdd S )NzgCompressed format cannot be sorted, either instantiate in the desired order or use the expanded format.)r@   rQ   r0   r   )rP   ri   Zbyreverser   r   r   sort  s    zGridInterface.sortc           	         s   |\}}d}t |r2t |} j|ddg}n*t|trJ  | }n fdd|D }t |rl|g}g }|D ]}|| j |dd|  qt|rt }|r|rt|d |j	r|d 
 d S |d d S t|S )NFTr{   c                    s   g | ]} j |d dqS r   r   r   rq   r   r   r   
  r   z&GridInterface.iloc.<locals>.<listcomp>)r   r   )r(   rn   r}   r+   r   rQ   r   rG   r   r   r   r,   )	rP   ri   r~   ZrowsZcolsZscalarrg   r   r   r   rq   r   iloc   s&    



zGridInterface.ilocc                 C   s  |j |dd}|jr>||jv r>| ||}| j|||dd}n| j||ddd}|jd urh| ||j}t }t	|dkrt
jt
jfS |jjdkr| |  }}n:zt
|t
| }}W n ty   t
jt
jf Y S 0 |rt||jrt|g|||R  S t|||S )NTr{   )rl   r   F)rl   r   r   r   )r}   r   r@   rj   r   rG   ZnodataZreplace_valuer   r7   r(   ZNaNrw   r   r   r   ZnanminZnanmaxrN   r+   r   r   r   )rP   ri   r   rl   r)   r   ZdminZdmaxr   r   r   r     s&    
zGridInterface.rangec                 C   s   t |j}| D ]n\}}||jv rp| ||}|jdksft|dd  |d d k rf|d d d }|||< q| ||||< q|S )Nr   r"   )	r   r0   r^   r@   r   rH   r(   ro   r   )rP   ri   rg   r0   r2   r$   r   r   r   r   assign5  s    

(
zGridInterface.assign)T)F)r   )FFF)TTTFT)N),re   
__module____qualname____doc__rM   r   cyODictr-   Zdatatyper   classmethodrW   r\   r`   rj   rn   rv   ry   rz   rw   r;   r   r   r   r   r   r   r   r   rG   r_   r   r   r(   nanr   r   r   r   r   r   r   r   r   r   r   r   r   r      s   

g








#5



  
>
&S"




r   )$Z
__future__r   r   r   r   collectionsr   r   Zitertools.izipZiziprC   ImportErrorZnumpyr(   Z
dictionaryr   r.   r   r   r   r   elementr   r   Z	ndmappingr   r   r   r]   r   r   r   r   r   r   registerr   r   r   r   <module>   s0         0