B
    \                 @   s  d 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Z	ddlm
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlZdd	lmZmZmZmZmZm Z  ddl!Z!e"d
Z#e!j$j%Z%e!j$j&Z&e!j$j'Z'e!j(Z(e!j)Z)e*dd dkZ+d%ddZ,d&ddZ-dd Z.dd Z/d'ddZ0d(ddZ1dd Z2dd Z3d)dd Z4e5d!kr|e0d" e1d" e1d"d# e1d"d$ dS )*z
Testing utilities.
    N)NamedTemporaryFile)testing)assert_array_equalassert_array_almost_equalassert_array_lessassert_array_almost_equal_nulpassert_equalTestCaseassert_allcloseassert_almost_equalassert_assert_warnsassert_no_warnings   )expected_warnings   )dataioimg_as_uintimg_as_float
img_as_intimg_as_ubytez%(\s*>>>.*?)(\s*)#\s*skip\s+if\s+(.*)$P       c             C   s4   d| |f }|d k	r |d| 7 }| |k s0t |d S )Nz%r is not lower than %rz: )AssertionError)abmsgmessage r    6lib/python3.7/site-packages/skimage/_shared/testing.pyassert_less)   s    r"   c             C   s4   d| |f }|d k	r |d| 7 }| |ks0t |d S )Nz%r is not greater than %rz: )r   )r   r   r   r   r    r    r!   assert_greater0   s    r#   c          	   C   s   | j d}g }x|D ]}t|}|dkr8|| q| \}}}yt|| jr`|| d }W n. tk
r   t|| j	jr|| d }Y nX || qW d
|| _ | S )aD   Decorator replaces custom skip test markup in doctests

    Say a function has a docstring::

        >>> something, HAVE_AMODULE, HAVE_BMODULE = 0, False, False
        >>> something # skip if not HAVE_AMODULE
        0
        >>> something # skip if HAVE_BMODULE
        0

    This decorator will evaluate the expression after ``skip if``.  If this
    evaluates to True, then the comment is replaced by ``# doctest: +SKIP``. If
    False, then the comment is just removed. The expression is evaluated in the
    ``globals`` scope of `func`.

    For example, if the module global ``HAVE_AMODULE`` is False, and module
    global ``HAVE_BMODULE`` is False, the returned function will have docstring::

        >>> something # doctest: +SKIP
        >>> something + else # doctest: +SKIP
        >>> something # doctest: +SKIP

    
Nz# doctest: +SKIP)__doc__splitSKIP_REmatchappendgroupseval__globals__AttributeError__init__join)funclinesZ	new_linesliner(   codeZspaceexprr    r    r!   doctest_skip_parser7   s"    


r5   c             C   sp   d|krd| }t |dd}|j}|  tj|| |d tj||d}yt| W n tk
rj   Y nX |S )z/Save and read an image using a specified plugin.F)suffixdelete)plugin)	r   namecloser   ZimsaveZimreadosremove	Exception)Zimager9   r7   Z	temp_filefnamenewr    r    r!   	roundtripf   s    rA   pngc          	   C   sP  t t }t|| |}t|| |dk}t|| |}t|tj| t	|}t
dg t|| |}W dQ R X t|| t
dg t|}W dQ R X | dkr|d8 }t
dg t|| |}	W dQ R X t|	| n2t
dg" t|| |}	t|	t | W dQ R X t|}
t
dg t|
| |}W dQ R X t|| dS )zCheck roundtrip behavior for color images.

    All major input types should be handled as ubytes and read
    back correctly.
       zprecision lossN)tiftiffd   z	sign losszsign loss|precision loss)r   r   ZchelsearA   r   r
   astypenpuint8r   r   r   lowerr   )r9   fmtimgr1img2r2img3r3img4r4img5r5r    r    r!   color_checkv   s0    rV   c          	   C   sZ  t t }t|| |}t|| |dk}t|| |}t|tj| t	|}t
dg t|| |}W dQ R X |jjdkrt|| nt|t| t
dg t|}W dQ R X | dkr|d8 }t
dg t|| |}	W dQ R X t|	| n2t
d	g" t|| |}	t|	t| W dQ R X t|}
t|
| |}t||
 dS )
zsCheck the roundtrip behavior for images that support most types.

    All major input types should be handled.
    rC   zprecision|\A\ZNfzprecision loss)rD   rE   rF   zsign loss|\A\Zzprecision loss|sign loss)r   r   ZmoonrA   r   r
   rG   rH   rI   r   r   ZdtypeZkindr   r   rJ   )r9   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   r    r    r!   
mono_check   s2    rX   c              C   sX   t d ddlm} m}m}m}m} ddlm	} ddl
m} tjd t d dS )zDefault package level setup routine for skimage tests.

    Import packages known to raise warnings, and then
    force warnings to raise errors.

    Also set the random seed to zero.
    defaultr   )signalndimagespecialoptimizelinalg)loadmat)viewererrorN)warningssimplefilterZscipyrZ   r[   r\   r]   r^   Zscipy.ior_   Zskimager`   rH   ZrandomZseed)rZ   r[   r\   r]   r^   r_   r`   r    r    r!   
setup_test   s    
rd   c               C   s   t d dS )zhDefault package level teardown routine for skimage tests.

    Restore warnings to default behavior
    rY   N)rb   rc   r    r    r    r!   teardown_test   s    re   c                s    dkst  fdd}|S )a>  Decorator to run the same function multiple times in parallel.

    This decorator is useful to ensure that separate threads execute
    concurrently and correctly while releasing the GIL.

    Parameters
    ----------
    num_threads : int, optional
        The number of times the function is run in parallel.

    r   c                s   t   fdd}|S )Nc                 sn   g }x.t d D ]}tj | |d}|| qW x|D ]}|  q:W  | |}x|D ]}|  qZW |S )Nr   )targetargskwargs)range	threadingZThreadr)   startr/   )rg   rh   ZthreadsiZthreadresult)r0   num_threadsr    r!   inner   s    


z-test_parallel.<locals>.wrapper.<locals>.inner)	functoolswraps)r0   ro   )rn   )r0   r!   wrapper   s    ztest_parallel.<locals>.wrapper)r   )rn   rr   r    )rn   r!   test_parallel   s    rs   __main__ZpilZbmprE   )N)N)rB   )rB   )r   )6r%   r<   reZstructrj   rp   Ztempfiler   ZnumpyrH   r   Znumpy.testingr   r   r   r   r   r	   r
   r   r   r   r   	_warningsr   rb    r   r   r   r   r   r   Zpytestcompiler'   ZmarkZskipifZxfailZparametrizeZraisesZfixtureZcalcsizeZarch32r"   r#   r5   rA   rV   rX   rd   re   rs   __name__r    r    r    r!   <module>   sD   4 


/
%
'
%

