a
    =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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ZddlmZmZ ddlmZ dd	lmZm Z m!Z!m"Z" d
dl#m$Z$ e%dZ&ddl'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/ej01ddZ2e23 dkr:dZ2n>e23 dkrNdZ2n*ze4e5e2Z2W n e6yv   dZ2Y n0 d/ddZ7d0ddZ8dd Z9dd Z:d1dd Z;d2d!d"Z<d#d$ Z=d%d& Z>d'd( Z?d3d)d*Z@eAd+kr e;d, e<d, e<d,d- e<d,d. dS )4z
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   )dataio)_fetch)img_as_uintimg_as_float
img_as_intimg_as_ubyte   )expected_warningsz%(\s*>>>.*?)(\s*)#\s*skip\s+if\s+(.*)$P       Z#SKIMAGE_TEST_STRICT_WARNINGS_GLOBAL0trueTZfalseFc                 C   s4   d| |f }|d ur |d| 7 }| |k s0J |d S )Nz%r is not lower than %r:  abmsgmessager   r   6lib/python3.9/site-packages/skimage/_shared/testing.pyassert_less7   s    r&   c                 C   s4   d| |f }|d ur |d| 7 }| |ks0J |d S )Nz%r is not greater than %rr   r   r    r   r   r%   assert_greater>   s    r'   c              	   C   s   | j d}g }|D ]}t|}|du r6|| q| \}}}zt|| jr^|| d }W n, ty   t|| j	jr|| d }Y n0 || q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_parserE   s"    

r9   c                 C   sn   d|vrd| }t |dd}|j}|  tj|| |d tj||d}zt| W n tyh   Y n0 |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imager=   r;   Z	temp_filefnamenewr   r   r%   	roundtript   s    rE   pngc                 C   s   t t }t|| |}t|| |dk}t|| |}t||t t|}t|| |}t|| t	|}|
 dv r|d8 }t|| |}	t|	| nt|| |}	t|	t | t|}
t|
| |}t|| dS )zCheck roundtrip behavior for color images.

    All major input types should be handled as ubytes and read
    back correctly.
       Ztiftiffd   N)r   r   ZchelsearE   r   r
   astypeboolr   r   lowerr   r=   ZfmtZimgZr1Zimg2Zr2Zimg3Zr3Zimg4Zr4Zimg5Zr5r   r   r%   color_check   s&    rO   c                 C   s$  t  dkr&| dkr&|dkr&td tt }t|| |}t	|| |dk}t|| |}t	||
t t|}t|| |}|jjdkrt	|| nt	|t| t|}| dv r|d8 }t|| |}	t	|	| nt|| |}	t	|	t| t|}
t|
| |}t	||
 d	S )
zsCheck the roundtrip behavior for images that support most types.

    All major input types should be handled.
    Zs390xpilrF   zknown to be broken on s390xrG   frH   rJ   N)platformmachinepytestskipr   r   ZmoonrE   r   r
   rK   rL   r   Zdtypekindr   r   rM   rN   r   r   r%   
mono_check   s.    
rW   c                  C   s  t d trddlm} m}m}m}m} ddl	m
} ddlm} tjd t d t jddtd t jdd	td t jdd
td t jddtd t jdtdd t jddtd t jddtdd t jddtdd t jddtd t jddtd t jd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errorz	TiffFile:)r$   categoryzTiffWriter:z
<tifffile.zunclosed fileignorezskimage.viewer)ra   modulez/Conversion of the second argument of issubdtypez.the matrix subclass is not the recommended waynumpy)r$   ra   rc   zYour installed pillow versionz
skimage.iozviewer requires z.The optional dask dependency is not installed.znumpy.ufunc size changedN)warningssimplefilter_error_on_warningsZscipyrY   rZ   r[   r\   r]   Zscipy.ior^   Zskimager_   npZrandomZseedfilterwarningsDeprecationWarningResourceWarningFutureWarningPendingDeprecationWarningUserWarningRuntimeWarning)rY   rZ   r[   r\   r]   r^   r_   r   r   r%   
setup_test   sj    

rp   c                   C   s   t rt  td dS )zhDefault package level teardown routine for skimage tests.

    Restore warnings to default behavior
    rX   N)rg   re   resetwarningsrf   r   r   r   r%   teardown_test  s    rr   c              	   C   s:   z
t | W S  ttfy4   tjd|  dd Y n0 dS )z:Attempt to fetch data, but if unavailable, skip the tests.zUnable to download T)Zallow_module_levelN)r   ConnectionErrorModuleNotFoundErrorrT   rU   )Zdata_filenamer   r   r%   fetch(  s    
ru   c                    s    dksJ  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.

    warnings_matching: list or None
        This parameter is passed on to `expected_warnings` so as not to have
        race conditions with the warnings filters. A single
        `expected_warnings` context manager is used for all threads.
        If None, then no warnings are checked.

    r   c                    s   t   fdd}|S )Nc                     s   t v g }td D ]}tj | |d}|| q|D ]}|  q> | i |}|D ]}|  q^|W  d    S 1 s0    Y  d S )Nr   )targetargskwargs)r   range	threadingZThreadr-   startr3   )rw   rx   Zthreadsithreadresult)r4   num_threadswarnings_matchingr   r%   innerG  s    


z-test_parallel.<locals>.wrapper.<locals>.inner)	functoolswraps)r4   r   r   r   )r4   r%   wrapperF  s    ztest_parallel.<locals>.wrapperr   )r   r   r   r   r   r%   test_parallel1  s    r   __main__rP   ZbmprI   )N)N)rF   )rF   )r   N)Br)   r@   reZstructrz   r   rR   Ztempfiler   rd   rh   r   Znumpy.testingr   r   r   r   r   r	   r
   r   r   r   r   re    r   r   Zdata._fetchersr   utilr   r   r   r   	_warningsr   compiler+   rT   ZmarkZskipifZxfailZparametrizeZraisesZfixtureZcalcsizeZarch32environgetrg   rM   rL   int
ValueErrorr&   r'   r9   rE   rO   rW   rp   rr   ru   r   __name__r   r   r   r%   <module>   s^   4



/
 
%U
	
-

