HOME


sh-3ll 1.0
DIR:/usr/local/lib/python3.6/site-packages/xarray/core/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.6/site-packages/xarray/core/__pycache__/alignment.cpython-36.pyc
3

���h)7�@s�ddlZddlZddlmZddlZddlZddlm	Z	m
Z
ddlmZddl
mZmZddl
mZddlmZmZmZd	d
�Ze�fdd�Ze�fd
d�Zdd�Zdd�Zddd�Zddd�Zdd�Zdd�ZdS) �N)�defaultdict�)�ops�utils)�_maybe_promote)�	iteritems�OrderedDict)�
is_full_slice)�Variable�
Coordinate�broadcast_variablescCsd|dkrtjtjtj�S|dkr0tjtjtj�S|dkrBtjd�S|dkrTtjd�Std|��dS)	N�outer�inner�leftr�rightrzinvalid value for join: %s���)�	functools�partial�reduce�operator�or_�and_�
itemgetter�
ValueError)�join�r�5/tmp/pip-build-5_djhm0z/xray/xarray/core/alignment.py�_get_joiners

rcCsHtt�}x:|D]2}x,t|j�D]\}}||kr||j|�qWqW|S)N)r�listr�indexes�append)�objects�exclude�all_indexes�obj�k�vrrr�_get_all_indexess
r'cs2t|��t||d�}t�fdd�t|�D��}|S)N)r"c3s<|]4\}�t�fdd��dd�D��r|���fVqdS)c3s|]}�dj|�VqdS)rN)�equals)�.0�idx)r&rr�	<genexpr>,sz*_join_indexes.<locals>.<genexpr>.<genexpr>rN)�any)r)r%)�joiner)r&rr++sz _join_indexes.<locals>.<genexpr>)rr'�dictr)rr!r"r�joined_indexesr)r-r�
_join_indexes%sr0cs�|jdd�}|jdd�}|r,tdt|���t||�}g}x@|D]8�t�fdd�|j�D��}|j�jfd|i|���q@Wt|�S)a�align(*objects, join='inner', copy=True)

    Given any number of Dataset and/or DataArray objects, returns new
    objects with aligned indexes.

    Array from the aligned objects are suitable as input to mathematical
    operators, because along each dimension they have the same indexes.

    Missing values (if ``join != 'inner'``) are filled with NaN.

    Parameters
    ----------
    *objects : Dataset or DataArray
        Objects to align.
    join : {'outer', 'inner', 'left', 'right'}, optional
        Method for joining the indexes of the passed objects along each
        dimension:
        - 'outer': use the union of object indexes
        - 'inner': use the intersection of object indexes
        - 'left': use indexes from the first object with each dimension
        - 'right': use indexes from the last object with each dimension
    copy : bool, optional
        If ``copy=True``, the returned objects contain all new variables. If
        ``copy=False`` and no reindexing is required then the aligned objects
        will include original variables.

    Returns
    -------
    aligned : same as *objects
        Tuple of objects with aligned coordinates.
    rr�copyTz,align() got unexpected keyword arguments: %sc3s$|]\}}|�jkr||fVqdS)N)�dims)r)r%r&)r$rrr+Yszalign.<locals>.<genexpr>)	�pop�	TypeErrorrr0r.�itemsr �reindex�tuple)r!�kwargsrr1r/�resultZvalid_indexersr)r$r�align0s 

r:csV|jdd�}|jdd��|jdt��}|s0t�t|||d��t��fdd�|D��S)	z�partial_align(*objects, join='inner', copy=True, exclude=set()

    Like align, but don't align along dimensions in exclude. Not public API.
    rrr1Tr")r"c3s"|]}|jfd�i���VqdS)r1N)r6)r)r$)r1r/rrr+isz partial_align.<locals>.<genexpr>)r3�set�AssertionErrorr0r7)r!r8rr"r)r1r/r�
partial_align_s
r=r
Fcs�ddlm�ddlm�m�m�t|���j����fdd�|j�D��dd��j�D�}t	�fd	d�|D�||d
��}�jt
||���S)zKAlign all DataArrays in the provided dict, leaving other values alone.
    r)�	DataArrayr)�Series�	DataFrame�Panelc3s.|]&\}}t|���f�r|�|�fVqdS)N)�
isinstance)r)r%r&)r>r@rAr?rrr+usz"align_variables.<locals>.<genexpr>cSsg|]\}}t|d�r|�qS)r)�hasattr)r)r%r&rrr�
<listcomp>ysz#align_variables.<locals>.<listcomp>csg|]}�|�qSrr)r)�a)�
new_variablesrrrDzs)rr1)�	dataarrayr>�pandasr?r@rAr�updater5r:�zip)�	variablesrr1Z	alignableZalignedr)r>r@rAr?rFr�align_variableslsrLTcsdi}i�i}i}|dk	r2tjdkr*td��||d<x�t|�D]�\}	}
|
j�|	<|	|kr<tj||	�}|
j|fd|i|��}t|��|	<|dk||	<||	j	�r�t
d�||	<|||	||	<tj||	tj
|
j��r<t
d�||	<q<Wdd�}
d	d
�}t�}�xdt|�D�]V\}	}|	|k�r4t|j||	|j|j�}�n|||�}|||�}|
|��r,||j}t|j�\}}t|tj��r�t�fdd�|jD��}tj||d
�}||d<t|j||jdd�}|||<nf|j|dd�}xBt|�D]6\}}t|��s�tj|�|}tj ||||d�}�q�Wt|j||jdd�}n&|
|��r@||}n|�rN|j!�n|}|||	<�qW|S)awConform a dictionary of aligned variables onto a new set of variables,
    filling in missing values with NaN.

    WARNING: This method is not public API. Don't use it directly.

    Parameters
    ----------
    variables : dict-like
        Dictionary of xarray.Variable objects.
    indexes : dict-like
        Dictionary of xarray.Coordinate objects associated with variables.
    indexers : dict
        Dictionary with keys given by dimension names and values given by
        arrays of coordinates tick labels. Any mis-matched coordinate values
        will be filled in with NaN, and any mis-matched dimension names will
        simply be ignored.
    method : {None, 'nearest', 'pad'/'ffill', 'backfill'/'bfill'}, optional
        Method to use for filling index values in ``indexers`` not found in
        this dataset:
          * None (default): don't fill gaps
          * pad / ffill: propgate last valid index value forward
          * backfill / bfill: propagate next valid index value backward
          * nearest: use nearest valid index value
    tolerance : optional
        Maximum distance between original and new labels for inexact matches.
        The values of the index at the matching locations most satisfy the
        equation ``abs(index[indexer] - target) <= tolerance``.
    copy : bool, optional
        If `copy=True`, the returned dataset contains only copied
        variables. If `copy=False` and no reindexing is required then
        original variables from this dataset are returned.

    Returns
    -------
    reindexed : OrderedDict
        Another dict, with the items in variables but replaced indexes.
    Nz0.17z5the tolerance argument requires pandas v0.17 or newer�	tolerance�methodrcSstdd�|D��S)Ncss|]}t|�VqdS)N)r	)r)r*rrrr+�szAreindex_variables.<locals>.any_not_full_slices.<locals>.<genexpr>)r,)�indexersrrr�any_not_full_slices�sz.reindex_variables.<locals>.any_not_full_slicescst�fdd�|jD��S)Nc3s|]}�j|td��VqdS)N)�get�slice)r)�d)rOrrr+�sz:reindex_variables.<locals>.var_indexers.<locals>.<genexpr>)r7r2)�varrOr)rOr�var_indexers�sz'reindex_variables.<locals>.var_indexersc3s|]}�|VqdS)Nr)r)�dim)�to_shaperrr+�sz$reindex_variables.<locals>.<genexpr>)�dtype.T)ZfastpathF)r1)�axis)"�pd�__version__�NotImplementedErrorr�sizerZsafe_cast_to_indexZget_indexer�len�allrR�npZarray_equalZarangerrr2�attrs�encoding�datarrXrBZndarrayr7�emptyr
Zastype�	enumerater	Zcumsumr�insertr1)rKrrOrNrMr1Zto_indexersZ
from_indexersZget_indexer_kwargs�name�index�targetZindexerrPrUZ	reindexedrTZnew_varZ	assign_toZassign_fromrcrXZ
fill_value�shapeZnew_datarY�indicesr)rWr�reindex_variablessl(












rlcs(ddlm�ddlm�t|�}x>|j�D]2\}�t�fdd��dd�D��s*td|��q*Wt��t��xB|D]:}x4|j	D]*}|�kr~|j
|j�|<�|j�|<q~WqrW���fdd	�}���fd
d�}g}xJ|D]B}t
|��r�|j||��q�t
|���r|j||��q�td��q�Wt|�S)
a�Explicitly broadcast any number of DataArray or Dataset objects against
    one another.

    xarray objects automatically broadcast against each other in arithmetic
    operations, so this function should not be necessary for normal use.

    Parameters
    ----------
    *args: DataArray or Dataset objects
        Arrays to broadcast against each other.

    Returns
    -------
    broadcast: tuple of xarray objects
        The same data as the input arrays, but with additional dimensions
        inserted so that all data arrays have the same dimensions and shape.

    Raises
    ------
    ValueError
        If indexes on the different objects are not aligned.

    Examples
    --------

    Broadcast two data arrays against one another to fill out their dimensions:

    >>> a = xr.DataArray([1, 2, 3], dims='x')
    >>> b = xr.DataArray([5, 6], dims='y')
    >>> a
    <xarray.DataArray (x: 3)>
    array([1, 2, 3])
    Coordinates:
      * x        (x) int64 0 1 2
    >>> b
    <xarray.DataArray (y: 2)>
    array([5, 6])
    Coordinates:
      * y        (y) int64 0 1
    >>> a2, b2 = xr.broadcast(a, b)
    >>> a2
    <xarray.DataArray (x: 3, y: 2)>
    array([[1, 1],
           [2, 2],
           [3, 3]])
    Coordinates:
      * x        (x) int64 0 1 2
      * y        (y) int64 0 1
    >>> b2
    <xarray.DataArray (x: 3, y: 2)>
    array([[5, 6],
           [5, 6],
           [5, 6]])
    Coordinates:
      * y        (y) int64 0 1
      * x        (x) int64 0 1 2

    Fill out the dimensions of all data variables in a dataset:

    >>> ds = xr.Dataset({'a': a, 'b': b})
    >>> ds2, = xr.broadcast(ds)  # use tuple unpacking to extract one dataset
    >>> ds2
    <xarray.Dataset>
    Dimensions:  (x: 3, y: 2)
    Coordinates:
      * x        (x) int64 0 1 2
      * y        (y) int64 0 1
    Data variables:
        a        (x, y) int64 1 1 2 2 3 3
        b        (x, y) int64 5 6 5 6 5 6
    r)r>)�Datasetc3s|]}�dj|�VqdS)rN)r()r)�vi)r&rrr+Rszbroadcast.<locals>.<genexpr>NzMcannot broadcast arrays: the %s index is not aligned (use xarray.align first)csB|jj��}t|j�}|j��t��}�||||j|j|jd�S)N)rgrarb)	�variable�expand_dimsr�coordsrIr7rgrarb)�arrayrcrqr2)r>�
common_coords�dims_maprr�_broadcast_array^s

z#broadcast.<locals>._broadcast_arraycsLt�}x"|jD]}|j|j��||<qWt|j�}|j���|||j�S)N)r�	data_varsrKrprqrIra)Zdsrvr%rq)rmrsrtrr�_broadcast_datasetfs

z%broadcast.<locals>._broadcast_datasetz.all input must be Dataset or DataArray objects)rGr>Zdatasetrmr'r5r_rrr2rqror]rBr r7)�argsr#r%�argrVrurwr9r)r>rmrsrtr&r�	broadcasts0H



rzcGstjdtdd�t|�S)NzCxarray.broadcast_arrays is deprecated: use xarray.broadcast instead�)�
stacklevel)�warnings�warn�DeprecationWarningrz)rxrrr�broadcast_arrays|s
r�)r
F)NNT)rr�collectionsrZnumpyr`rHrZ�rr�commonrZpycompatrrr	ror
rrrr;r'r0r:r=rLrlrzr�rrrr�<module>s(
	/

w