3
h;(                 @   sL  d Z ddlZddlZddlmZmZ ddljjZddl	Zddl	m
Z
mZmZmZmZ ddlmZmZmZmZmZmZ ddd	d
ddddddddddgZddlmZ dddddgZee Zd1ddZd2d dZd3d"d#Zd$d Zd4d%dZd5d&d'ZeZ d6d)d	Z!e!Z"d7d*dZ#d8d+dZ$d9d,dZ%d:d-dZ&d;d.d
Z'd<d/dZ(d=d0dZ)dS )>zMiscellaneous functions for testing masked arrays and subclasses

:author: Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
:version: $Id: testutils.py 3529 2007-11-13 08:01:14Z jarrod.millman $

    N)ndarrayfloat_)assert_assert_allcloseassert_array_almost_equal_nulpassert_raisesbuild_err_msg   )mask_orgetmaskmasked_arraynomaskmaskedfilledalmostapproxassert_almost_equalassert_array_almost_equalassert_array_approx_equalassert_array_compareassert_array_equalassert_array_lessassert_closeassert_equalassert_equal_recordsassert_mask_equalassert_not_equalfail_if_array_equal)TestCaser   r   r   r   r   Th㈵>:0yE>c             C   s   t t| t|}t| }t|}|jjdks:|jjdkrJtj||j S tt|d|d|j	t
}tt|d|ddj	t
}	tjtj||	 ||tj|	  }
|
j S )a  
    Returns true if all components of a and b are equal to given tolerances.

    If fill_value is True, masked values considered equal. Otherwise,
    masked values are considered unequal.  The relative error rtol should
    be positive and << 1.0 The absolute error atol comes into play for
    those elements of b that are very small or zero; it says how small a
    must be also.

    OF)copymaskr	   )r
   r   r   dtypecharnpequalravelr   astyper   Z
less_equalumathZabsolute)ab
fill_valuertolZatolmd1d2xyd r5   3/tmp/pip-build-fibhr3ey/numpy/numpy/ma/testutils.pyr   )   s    $   c       
      C   s   t t| t|}t| }t|}|jjdks:|jjdkrJtj||j S tt|d|d|j	t
}tt|d|ddj	t
}tjtj|| |d|  k}	|	j S )z
    Returns True if a and b are equal up to decimal places.

    If fill_value is True, masked values considered equal. Otherwise,
    masked values are considered unequal.

    r!   F)r"   r#   r	   g      $@)r
   r   r   r$   r%   r&   r'   r(   r   r)   r   Zaroundabs)
r+   r,   decimalr-   r/   r0   r1   r2   r3   r4   r5   r5   r6   r   ?   s      c             C   sJ   t t| t|| x0tt|D ] }t | | || d||f  q"W dS )z;
    Asserts the equality of two non-array sequences.

    z
item=%r
%sN)r   lenrange)actualdesirederr_msgkr5   r5   r6   _assert_equal_on_sequencesR   s     rA   c             C   sh   t | j|j xT| jjD ]H}tj| |tj|| }}|tk	r|tk	rt tj| |tj|| qW dS )zI
    Asserts that two records are equal.

    Pretty crude for now.

    N)r   r$   namesoperatorgetitemr   )r+   r,   fafZbfr5   r5   r6   r   ]   s    c             C   s  t |trt | ts$ttt| tt| t|| xH|j D ]<\}}|| krbtd|| f t| | || d||f  qBW dS t |tt	frt | tt	frt
| |ddS t | tpt |tst| |g|}|| kst|dS | tko|tk	s|tkr(| tk	r(t| |g|ddd}t|tj| d	d
d} tj|d	d
d}| j|j }}|jdkr|jdkrt
| j |j ddS t| ||S )z,
    Asserts that two items are equal.

    z%s not in %sz	key=%r
%sNr:   )r?   r2   r3   )headerrB   FT)r"   subokS)r2   r3   )
isinstancedictAssertionErrorreprtyper   r;   itemslisttuplerA   r   r   r   
ValueErrorr&   arrayr$   r%   tolistr   )r=   r>   r?   r@   imsgZactual_dtypeZdesired_dtyper5   r5   r6   r   l   s:    

 c             C   s4  t |trt | ts$ttt| tt| t|| xD|j D ]8\}}|| kr^tt|t| | || d||f  qBW dS t |tt	frt | tt	frtt| t|| x0t
t|D ] }t| | || d||f  qW dS t | tjst |tjrt| ||S t| |g|}|| ks0t|dS )z<
    Raises an assertion error if two items are equal.

    z	key=%r
%sNz
item=%r
%s)rJ   rK   rL   rM   rN   fail_if_equalr;   rO   rP   rQ   r<   r&   r   r   r   )r=   r>   r?   r@   rU   rV   r5   r5   r6   rW      s&    

  
rW      c             C   s^   t | tjst |tjr*t| ||||dS t| |g||d}tt||  |dksZt|dS )z~
    Asserts that two items are almost equal.

    The test is equivalent to abs(desired-actual) < 0.5 * 10**(-decimal).

    )r9   r?   verbose)r?   rY   r   N)rJ   r&   r   r   r   roundr8   rL   )r=   r>   r9   r?   rY   rV   r5   r5   r6   r      s    

c       	      C   s   t t|t|}t|d|ddd}t|d|ddd}|tkrF|tk	sV|tkrt|tk	rtt||g|||dd}t|tjj| |j	||j	||||dS )zn
    Asserts that comparison between two masked arrays is satisfied.

    The comparison is elementwise.

    F)r"   r#   Z	keep_maskrH   r2   r3   )r?   rY   rG   rB   )r?   rY   rG   )r2   r3   )
r
   r   r   r   r   rR   r&   Ztestingr   r   )	Z
comparisonr2   r3   r?   rY   rG   r-   r/   rV   r5   r5   r6   r      s    	
c             C   s   t tj| |||dd dS )z@
    Checks the elementwise equality of two masked arrays.

    zArrays are not equal)r?   rY   rG   N)r   rC   __eq__)r2   r3   r?   rY   r5   r5   r6   r      s    
c             C   s    dd }t || |||dd dS )zT
    Raises an assertion error if two masked arrays are not equal elementwise.

    c             S   s   t jt| | S )N)r&   Zalltruer   )r2   r3   r5   r5   r6   compare   s    z$fail_if_array_equal.<locals>.comparezArrays are not equal)r?   rY   rG   N)r   )r2   r3   r?   rY   r\   r5   r5   r6   r      s    c                s$    fdd}t || |||dd dS )z|
    Checks the equality of two masked arrays, up to given number odecimals.

    The equality is checked elementwise.

    c                s   t | |d   dS )z<Returns the result of the loose comparison between x and y).g      $@)r.   )r   )r2   r3   )r9   r5   r6   r\      s    z*assert_array_approx_equal.<locals>.comparezArrays are not almost equal)r?   rY   rG   N)r   )r2   r3   r9   r?   rY   r\   r5   )r9   r6   r      s    c                s$    fdd}t || |||dd dS )z|
    Checks the equality of two masked arrays, up to given number odecimals.

    The equality is checked elementwise.

    c                s   t | | S )z<Returns the result of the loose comparison between x and y).)r   )r2   r3   )r9   r5   r6   r\     s    z*assert_array_almost_equal.<locals>.comparezArrays are not almost equal)r?   rY   rG   N)r   )r2   r3   r9   r?   rY   r\   r5   )r9   r6   r      s    c             C   s   t tj| |||dd dS )z7
    Checks that x is smaller than y elementwise.

    zArrays are not less-ordered)r?   rY   rG   N)r   rC   __lt__)r2   r3   r?   rY   r5   r5   r6   r     s    
c             C   s:   | t krt|t k |t kr(t| t k t| ||d dS )z-
    Asserts the equality of two masks.

    )r?   N)r   r   r   )m1m2r?   r5   r5   r6   r     s
    )Tr   r    )r7   T)r:   )r:   )r:   )rX   r:   T)r:   Tr:   T)r:   T)r:   T)r7   r:   T)r7   r:   T)r:   T)r:   )*__doc__rC   numpyr&   r   r   Znumpy.core.umathcorer*   Znumpy.testingr   r   r   r   r   r
   r   r   r   r   r   Z__all__maskedZunittestr   Z__some__from_testing__all__r   r   rA   r   r   rW   r   r   r   r   r   r   r   r   r   r   r5   r5   r5   r6   <module>   sB    



'

 







