a
    jDfaJ                     @   s  d Z ddlZddlZddlmZ ddlmZmZ ddlZ	ddl
ZddlmZmZmZ ddlmZ ddl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mZ ddlm Z m!Z! dd Z"dd Z#d?ddZ$e"d@eee%dddZ&e%dddZ'edd Z(edAeee%dddZ(e"dBe%dd dZ(ed!d" Z)edCeee%dd#d"Z)e"dDd$d"Z)e"dEe%dd%d&Z*d'd( Z+e"dFd+d,Z,e"dGd-d.Z-d/d0 Z.dHd1d2Z/dIe!ed3d4d5Z0ee%d6d7d8Z1ee%d9d:d;Z2eeee!f e%d<d=d>Z3dS )Jz)Testing functions exposed to the user API    N)Hashable)Unionoverload)duck_array_ops
formattingutils)Coordinates)	DataArray)Dataset)DataTree)diff_datatree_repr)IndexPandasIndexPandasMultiIndexdefault_indexes)IndexVariableVariablec                    s   t   fdd}|S )Nc                     sP   d}t  0 dd t jD t _ | i |W  d    S 1 sB0    Y  d S )NTc                 S   s   g | ]}|d  dkr|qS )r   error ).0fr   r   f/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/xarray/testing/assertions.py
<listcomp>       z4ensure_warnings.<locals>.wrapper.<locals>.<listcomp>)warningscatch_warningsfilters)argskwargs__tracebackhide__funcr   r   wrapper   s    
z ensure_warnings.<locals>.wrapper)	functoolswraps)r!   r"   r   r    r   ensure_warnings   s    	r%   c                 C   s"   | j jdkrtjj| ddS | S )NSzutf-8replace)dtypekindnpcoreZdefchararraydecode)datar   r   r   _decode_string_data%   s    r.   h㈵>:0yE>Tc                    sn   t dd | |fD r*|r*t| } t|}g d t  fdd| |fD rXt| |S tj| |||dS d S )Nc                 s   s   | ]}|j jd kV  qdS )r&   Nr(   r)   r   Zarrr   r   r   	<genexpr>,   r   z*_data_allclose_or_equiv.<locals>.<genexpr>)MmOr&   Uc                 3   s   | ]}|j j v V  qd S Nr1   r2   Zexact_dtypesr   r   r3   0   r   rtolatol)anyr.   r   array_equivallclose_or_equiv)Zarr1Zarr2r;   r<   decode_bytesr   r9   r   _data_allclose_or_equiv+   s    rA   Fab	from_rootc                 C   sf   d}t | t|sJ t | trP|r0| j} |j}| j||dsbJ t| |dntt|  ddS )a  
    Two DataTrees are considered isomorphic if every node has the same number of children.

    Nothing about the data or attrs in each node is checked.

    Isomorphism is a necessary condition for two trees to be used in a nodewise binary operation,
    such as tree1 + tree2.

    By default this function does not check any part of the tree above the given node.
    Therefore this function can be used as default to check that two subtrees are isomorphic.

    Parameters
    ----------
    a : DataTree
        The first object to compare.
    b : DataTree
        The second object to compare.
    from_root : bool, optional, default is False
        Whether or not to first traverse to the root of the trees before checking for isomorphism.
        If a & b have no parents then this has no effect.

    See Also
    --------
    DataTree.isomorphic
    assert_equal
    assert_identical
    TrE   
isomorphicz not of type DataTreeN)
isinstancetyper   rootrG   r   	TypeErrorrC   rD   rE   r   r   r   r   assert_isomorphic6   s    
rM   )check_dim_orderc                 C   s@   d}t | tttfs|S |s<t| jt|jkr<|j| j S |S )z*Helper for assert_equal/allclose/identicalT)rH   r   r	   r
   setdimsZ	transpose)rC   rD   rN   r   r   r   r   maybe_transpose_dimsb   s    rQ   c                 C   s   d S r8   r   rC   rD   r   r   r   assert_equaln   s    rS   c                 C   s   d S r8   r   rB   r   r   r   rS   r   s    c                 C   s  d}t | t |ks,t| tr(t|ts,J t| ||}t| ttfrd| |sbJ t| |dnt| t	r| |sJ t
| |dntt| tr| |sJ t| |dnLt| tr|r| j} |j}| j||dsJ t| |dntt |  ddS )a  Like :py:func:`numpy.testing.assert_array_equal`, but for xarray
    objects.

    Raises an AssertionError if two objects are not equal. This will match
    data values, dimensions and coordinates, but not names or attributes
    (except for Dataset objects for which the variable names must match).
    Arrays with NaN in the same location are considered equal.

    For DataTree objects, assert_equal is mapped over all Datasets on each node,
    with the DataTrees being equal if both are isomorphic and the corresponding
    Datasets at each node are themselves equal.

    Parameters
    ----------
    a : xarray.Dataset, xarray.DataArray, xarray.Variable, xarray.Coordinates
        or xarray.core.datatree.DataTree. The first object to compare.
    b : xarray.Dataset, xarray.DataArray, xarray.Variable, xarray.Coordinates
        or xarray.core.datatree.DataTree. The second object to compare.
    from_root : bool, optional, default is True
        Only used when comparing DataTree objects. Indicates whether or not to
        first traverse to the root of the trees before checking for isomorphism.
        If a & b have no parents then this has no effect.
    check_dim_order : bool, optional, default is True
        Whether dimensions must be in the same order.

    See Also
    --------
    assert_identical, assert_allclose, Dataset.equals, DataArray.equals
    numpy.testing.assert_array_equal
    TequalsrF   & not supported by assertion comparisonN)rI   rH   r   rQ   r   r	   rT   r   diff_array_reprr
   diff_dataset_reprdiff_coords_reprr   rJ   r   rK   )rC   rD   rE   rN   r   r   r   r   rS   v   s(     


 c                 C   s   d S r8   r   rR   r   r   r   assert_identical   s    rY   c                 C   s   d S r8   r   rB   r   r   r   rY      s    c                 C   s4  d}t | t |ks,t| tr(t|ts,J t| trT| |sRJ t| |dnt| tr| j|jksnJ | |sJ t| |dnt| t	tfr| |sJ t
| |dnxt| tr| |sJ t| |dnPt| tr|r| j} |j}| j||ds0J t| |dntt |  ddS )af  Like :py:func:`xarray.testing.assert_equal`, but also matches the
    objects' names and attributes.

    Raises an AssertionError if two objects are not identical.

    For DataTree objects, assert_identical is mapped over all Datasets on each
    node, with the DataTrees being identical if both are isomorphic and the
    corresponding Datasets at each node are themselves identical.

    Parameters
    ----------
    a : xarray.Dataset, xarray.DataArray, xarray.Variable or xarray.Coordinates
        The first object to compare.
    b : xarray.Dataset, xarray.DataArray, xarray.Variable or xarray.Coordinates
        The second object to compare.
    from_root : bool, optional, default is True
        Only used when comparing DataTree objects. Indicates whether or not to
        first traverse to the root of the trees before checking for isomorphism.
        If a & b have no parents then this has no effect.
    check_dim_order : bool, optional, default is True
        Whether dimensions must be in the same order.

    See Also
    --------
    assert_equal, assert_allclose, Dataset.equals, DataArray.equals
    T	identicalrF   rU   N)rI   rH   r   r   rZ   r   rV   r	   namer
   rW   rX   r   rJ   r   rK   rL   r   r   r   rY      s0    


c           	         s  d}t | t |ksJ t| ||}tjt|||d d _ fdd}t| trv|| |}|stJ tj	| | dnt| t
rtj| j|j|do|| j|j}|sJ tj	| | dnZt| tr| j|jkotj| j|j|d}|sJ tj| | dntt |  ddS )	a  Like :py:func:`numpy.testing.assert_allclose`, but for xarray objects.

    Raises an AssertionError if two objects are not equal up to desired
    tolerance.

    Parameters
    ----------
    a : xarray.Dataset, xarray.DataArray or xarray.Variable
        The first object to compare.
    b : xarray.Dataset, xarray.DataArray or xarray.Variable
        The second object to compare.
    rtol : float, optional
        Relative tolerance.
    atol : float, optional
        Absolute tolerance.
    decode_bytes : bool, optional
        Whether byte dtypes should be decoded to strings as UTF-8 or not.
        This is useful for testing serialization methods on Python 3 that
        return saved strings as bytes.
    check_dim_order : bool, optional, default is True
        Whether dimensions must be in the same order.

    See Also
    --------
    assert_identical, assert_equal, numpy.testing.assert_allclose
    T)r;   r<   r@   allclosec                    s>   t | d| } t |d|}| j|jko<| j|ju p< | j|jS )Nvariable)getattrrP   _datar-   rR   equivr   r   compat_variable  s    z(assert_allclose.<locals>.compat_variable)compatrU   N)rI   rQ   r#   partialrA   __name__rH   r   r   rV   r	   r   Z
dict_equivZcoordsr]   r
   _coord_names	variablesrW   rK   )	rC   rD   r;   r<   r@   rN   r   rb   r\   r   r`   r   assert_allclose   s0    




rh   c                 C   s   | | }t t|}d}t|}|j}| d| }	t|| d }
d|d|	 d|
ddd	| d
| g}|r|d| d|g7 }d|S )Nznot implementedz / d   zArrays are not equalzMismatched elements: z (z.0fz%)zMax absolute difference: zMax relative difference: z x: z y: 
)maxabsr*   Zcount_nonzerosizefloatjoin)xyerr_msgverbosediffZabs_diffZrel_diffZn_diffZn_totalfraction
percentagepartsr   r   r   _format_message%  s&    
rx   Hz> c                 C   s2   d}t j| |||d}|s.J t| |||ddS )z6Like `np.testing.assert_allclose`, but for duckarrays.Tr:   rr   rs   N)r   r?   rx   )actualZdesiredr;   r<   rr   rs   r   r\   r   r   r   assert_duckarray_allclose@  s    r}   c                 C   s   d}t | s"t | s"t| } t |s@t |s@t|}t | rTt |sht | rvt |rv| |k }nt| |}|sJ t| |||ddS )z8Like `np.testing.assert_array_equal`, but for duckarraysTr{   N)	r   Zis_duck_arrayZ	is_scalarr*   Zasarrayallr   r>   rx   )rp   rq   rr   rs   r   ra   r   r   r   assert_duckarray_equalK  s    

r   c                 C   s@   t | tt |tkrtd|  }| }|j|jks<J dS )a  
    Assert that chunksizes along chunked dimensions are equal.

    Parameters
    ----------
    a : xarray.Dataset or xarray.DataArray
        The first object to compare.
    b : xarray.Dataset or xarray.DataArray
        The second object to compare.
    za and b have mismatched typesN)rH   r	   rK   Zunify_chunkschunks)rC   rD   leftrightr   r   r   assert_chunks_equal_  s
    r   c           
         sh  t | tsJ | tdd |  D s>J dd |  D dd | D }|  |kslJ t| |f|  D ]\}}t |tr|j}|| }|j	f|j
ksJ ||f||j	kr|j|jksJ |j|jft |jjtjsJ |jj||jjsJ ||ft |trt|j}|jjD ]}	|	|v sBJ ||f||	 }|j	f|j
ksfJ ||f|j|	 |jksJ |j|	 |jft |jjtjsJ |jj||jjsJ ||f|	| v sJ |	t| f|| |	 u s(J || |	 jfq(qt|rdt|| |    ks<J t| t ft fdd|  D sdJ |  fd S )Nc                 s   s   | ]}t |tV  qd S r8   )rH   r   r   vr   r   r   r3   w  r   z4_assert_indexes_invariants_checks.<locals>.<genexpr>c                 S   s   i | ]\}}|t |qS r   rI   r   kr   r   r   r   
<dictcomp>w  s   z5_assert_indexes_invariants_checks.<locals>.<dictcomp>c                 S   s   h | ]\}}t |tr|qS r   )rH   r   r   r   r   r   	<setcomp>{  s   z4_assert_indexes_invariants_checks.<locals>.<setcomp>c                 3   s    | ]\}}|  | V  qd S r8   )rT   r   defaultsr   r   r3     r   )rH   dictr~   valuesitemskeysrO   r   indexZdimrP   Zcoord_dtyper(   r_   arraypdr   rT   r   namesZlevel_coords_dtypeZ
MultiIndexr   )
ZindexesZpossible_coord_variablesrP   check_defaultZ
index_varsr   r   Zpd_indexvarr[   r   r   r   !_assert_indexes_invariants_checkss  sL    

&
&r   )r   r[   c                 C   s   |d u rd}n|f}t | jts0J || jf t| jt| jjks\J || j| jjf t | jtd tfsJ || jf t | j	td tfsJ || j	f d S )Nr   )
rH   _dimstuplelenr_   shape	_encodingrI   r   _attrs)r   r[   Zname_or_emptyr   r   r   _assert_variable_invariants  s    r   )dacheck_default_indexesc                    s  t  jtsJ  jt j t  jts6J  jtdd  j D sXJ  jt fdd j D sJ  jdd  j	 D ftdd  j	 D sJ dd  j	 D  j	 D ]\}}t|| q̈ j
d urt j
 j j|d d S )	Nc                 s   s   | ]}t |tV  qd S r8   rH   r   r   r   r   r   r3     r   z/_assert_dataarray_invariants.<locals>.<genexpr>c                 3   s"   | ]}t |jt  jkV  qd S r8   )rO   rP   r   r   r   r   r3     r   c                 S   s   i | ]\}}||j qS r   )rP   r   r   r   r   r     r   z0_assert_dataarray_invariants.<locals>.<dictcomp>c                 s   s(   | ] \}}|j |fkrt|tV  qd S r8   rP   rH   r   r   r   r   r   r3     s   c                 S   s   i | ]\}}|t |qS r   r   r   r   r   r   r     r   r   )rH   Z	_variabler   r   Z_coordsr   r~   r   rP   r   _indexesr   )r   r   r   r   r   r   r   _assert_dataarray_invariants  s&    
"
r   )dsr   c                    s  t  jtsJ t jtdd  j D s<J  j j D ]\}}t|| qFt  jt	spJ  j j j
 ksJ  jt	 jft jtu sJ  jtdd  j D sJ  jt	 } j D ]}||j qވ j
 |ksJ t	 j|ft fdd j D sNJ  jdd  j D f|rtdd  j D sJ dd  j D  jd urt j j j|d	 t  jtd tfsJ t  jtd tfsJ d S )
Nc                 s   s   | ]}t |tV  qd S r8   r   r   r   r   r   r3     r   z-_assert_dataset_invariants.<locals>.<genexpr>c                 s   s   | ]}t |tV  qd S r8   )rH   intr   r   r   r   r3     r   c                 3   s.   | ]&}|j D ]} j| |j | kV  qqd S r8   )sizesr   )r   r   r   r   r   r   r3     s   c                 S   s   i | ]\}}||j qS r   )r   r   r   r   r   r     r   z._assert_dataset_invariants.<locals>.<dictcomp>c                 s   s(   | ] \}}|j |fkrt|tV  qd S r8   r   r   r   r   r   r3     s   c                 S   s&   i | ]\}}|j |fkr|t|qS r   )rP   rI   r   r   r   r   r     r   r   )rH   Z
_variablesr   rI   r~   r   r   r   rf   rO   r   r   updaterP   r   r   r   r   )r   r   r   r   Zvar_dimsr   r   r   _assert_dataset_invariants  s@    """r   Z
xarray_objr   c                 C   sv   t | trt|  n^t | tr,t| |d nFt | trDt| |d n.t | tr`t|  |d nt	t
|  ddS )zValidate that an xarray object satisfies its own internal invariants.

    This exists for the benefit of xarray's own test suite, but may be useful
    in external projects if they (ill-advisedly) create objects using xarray's
    private APIs.
    )r   z4 is not a supported type for xarray invariant checksN)rH   r   r   r	   r   r
   r   r   Z
to_datasetrK   rI   r   r   r   r   _assert_internal_invariants  s"    	




r   )r/   r0   T)F)T)TT)T)T)r/   r0   TT)ry   r   rz   T)rz   T)T)N)4__doc__r#   r   collections.abcr   typingr   r   numpyr*   Zpandasr   Zxarray.corer   r   r   Zxarray.core.coordinatesr   Zxarray.core.dataarrayr	   Zxarray.core.datasetr
   Zxarray.core.datatreer   Zxarray.core.formattingr   Zxarray.core.indexesr   r   r   r   Zxarray.core.variabler   r   r%   r.   rA   boolrM   rQ   rS   rY   rh   rx   r}   r   r   r   r   r   r   r   r   r   r   r   <module>   sf   
+
4
4 < 
 
3'