o
    e&                     @   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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# ddl$Z$e$j%j&Z&e$j%j'Z'e$j%j(Z(e$j)Z)e$j*Z*e+dZ,e-dd dkZ.ej/0ddZ1e12 dkrdZ1ne12 dkrdZ1nze3e4e1Z1W n e5y   dZ1Y nw d+ddZ6d+ddZ7dd Z8dd Z9d,dd Z:d,d!d"Z;d#d$ Z<d%d& Z=d'd( Z>d-d)d*Z?dS ).z
Testing utilities.
    N)NamedTemporaryFile)testing)TestCaseassert_assert_warnsassert_no_warningsassert_equalassert_almost_equalassert_array_equalassert_allcloseassert_array_almost_equalassert_array_almost_equal_nulpassert_array_less   )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trueTfalseFc                 C   s6   | d|}|d ur|d| 7 }| |k sJ |d S )Nz is not lower than :  abmsgmessager    r    GD:\Projects\ConvertPro\env\Lib\site-packages\skimage/_shared/testing.pyassert_less:      r'   c                 C   s6   | d|}|d ur|d| 7 }| |ksJ |d S )Nz is not greater than r   r    r!   r    r    r&   assert_greaterA   r(   r)   c              	   C   s   | j d}g }|D ]B}t|}|du r|| q
| \}}}zt|| jr/|| d }W n tyF   t|| j	jrD|| d }Y nw || 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lines	new_linesliner.   codespaceexprr    r    r&   doctest_skip_parserH   s(   

r=   c                 C   s   d|vrd| }t |dd}|j}W d   n1 sw   Y  tj|| |d tj||d}zt| W |S  tyB   Y |S w )z/Save and read an image using a specified plugin.F)suffixdeleteN)plugin)r   namer   ZimsaveZimreadosremove	Exception)imagerA   r?   	temp_filefnamenewr    r    r&   	roundtripw   s   rJ   pngc                 C   s   t t }t|| |}t|| |dk}t|| |}t||t t|}t|| |}t|| t	|}|
 dv rP|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tifZtiffd   N)r   r   ZchelsearJ   r   r   astypeboolr   r   lowerr   rA   fmtZimgr1Zimg2r2Zimg3Zr3Zimg4Zr4Zimg5Zr5r    r    r&   color_check   s&   rV   c                 C   s   t t }t|| |}t|| |dk}t|| |}t||t t|}t|| |}|j	j
dkr<t|| 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.
    rL   frM   rN   N)r   r   moonrJ   r   r   rO   rP   r   Zdtypekindr   r   rQ   rR   r    r    r&   
mono_check   s*   rZ   c                   C   s   t d trVtjd t d t jddt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 t jddtd dS 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   errorzunclosed file)r%   categoryz/Conversion of the second argument of issubdtypez.the matrix subclass is not the recommended waynumpy)r%   r]   modulezYour installed pillow versionz
skimage.ioz.The optional dask dependency is not installed.znumpy.ufunc size changedz#

The scipy.sparse array containersz-`np.int0` is a deprecated alias for `np.intp`N)warningssimplefilter_error_on_warningsnprandomseedfilterwarningsResourceWarningFutureWarningPendingDeprecationWarningUserWarningRuntimeWarningDeprecationWarningr    r    r    r&   
setup_test   sX   

	
rm   c                   C   s   t rt  td dS dS )zhDefault package level teardown routine for skimage tests.

    Restore warnings to default behavior
    r[   N)rb   r`   resetwarningsra   r    r    r    r&   teardown_test  s   ro   c              	   C   s8   zt | W S  ttfy   tjd|  dd Y dS w )z:Attempt to fetch data, but if unavailable, skip the tests.zUnable to download T)Zallow_module_levelN)r   ConnectionErrorModuleNotFoundErrorpytestskip)Zdata_filenamer    r    r&   fetch  s   
rt   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 : g }td D ]}tj | |d}|| q|D ]}|  q | i | |D ]}|  q/W d    d S 1 sAw   Y  d S )Nr   )targetargskwargs)r   range	threadingThreadr/   startr5   )rv   rw   threadsithread)r6   num_threadswarnings_matchingr    r&   inner8  s   


"z/run_in_parallel.<locals>.wrapper.<locals>.inner)	functoolswraps)r6   r   r   r   )r6   r&   wrapper7  s   z run_in_parallel.<locals>.wrapperr    )r   r   r   r    r   r&   run_in_parallel"  s   r   )N)rK   )r   N)@r+   rC   restructry   r   tempfiler   r^   rc   r   Znumpy.testingr   r   r   r   r   r	   r
   r   r   r   r   r`    r   r   Zdata._fetchersr   utilr   r   r   r   	_warningsr   rr   markZskipifZxfailZparametrizeZraisesZfixturecompiler-   calcsizeZarch32environgetrb   rQ   rP   int
ValueErrorr'   r)   r=   rJ   rV   rZ   rm   ro   rt   r   r    r    r    r&   <module>   sV    4


/

 #F
	