U
    ?hC%                     @   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r2dZ1n@e12 dkrFdZ1n,ze3e4e1Z1W n e5k
rp   dZ1Y nX 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 )0z
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 k	r"|d| 7 }| |k s2t |d S )Nz is not lower than : AssertionErrorabmsgmessage r'   I/var/www/html/venv/lib/python3.8/site-packages/skimage/_shared/testing.pyassert_less:   s    r)   c                 C   s6   | d|}|d k	r"|d| 7 }| |ks2t |d S )Nz is not greater than r   r    r"   r'   r'   r(   assert_greaterA   s    r*   c              	   C   s   | j d}g }|D ]}t|}|dkr6|| q| \}}}zt|| jr^|| d }W n. tk
r   t|| j	jr|| d }Y nX || 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   st   d|krd| }t |dd}|j}W 5 Q R X tj|| |d tj||d}zt| W n tk
rn   Y nX |S )z/Save and read an image using a specified plugin.F)suffixdelete)plugin)r   namer   ZimsaveZimreadosremove	Exception)imagerB   r@   	temp_filefnamenewr'   r'   r(   	roundtripw   s    rK   pngc                 C   s   t t }t|| |}t|| |dk}t|| |}t||t t|}t|| |}t|| t	|}|
 dk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tifZtiffd   N)r   r   ZchelsearK   r   r   astypeboolr   r   lowerr   rB   fmtZimgr1Zimg2r2Zimg3Zr3Zimg4Zr4Zimg5Zr5r'   r'   r(   color_check   s&    rW   c                 C   s   t t }t|| |}t|| |dk}t|| |}t||t t|}t|| |}|j	j
dkrxt|| nt|t| t|}| dk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.
    rM   frN   rO   N)r   r   moonrK   r   r   rP   rQ   r   Zdtypekindr   r   rR   rS   r'   r'   r(   
mono_check   s*    r[   c                   C   s   t d trt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 )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   s\    

   	rn   c                   C   s   t rt  td dS )zhDefault package level teardown routine for skimage tests.

    Restore warnings to default behavior
    r\   N)rc   ra   resetwarningsrb   r'   r'   r'   r(   teardown_test  s    rp   c              	   C   s<   z
t | W S  ttfk
r6   tjd|  dd Y nX dS )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    
ru   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.

    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               	      sv   t d g }td D ]}tj | |d}|| q|D ]}|  q> | | |D ]}|  qZW 5 Q R X d S )Nr   )targetargskwargs)r   range	threadingThreadr0   startr6   )rw   rx   threadsithread)r7   num_threadswarnings_matchingr'   r(   inner8  s    


z/run_in_parallel.<locals>.wrapper.<locals>.inner)	functoolswraps)r7   r   r   r   )r7   r(   wrapper7  s    z run_in_parallel.<locals>.wrapperr    )r   r   r   r'   r   r(   run_in_parallel"  s    r   )N)N)rL   )rL   )r   N)@r,   rD   restructrz   r   tempfiler   r_   rd   r   Znumpy.testingr   r   r   r   r   r	   r
   r   r   r   r   ra    r   r   Zdata._fetchersr   utilr   r   r   r   	_warningsr   rs   markZskipifZxfailZparametrizeZraisesZfixturecompiler.   calcsizeZarch32environgetrc   rR   rQ   int
ValueErrorr)   r*   r>   rK   rW   r[   rn   rp   ru   r   r'   r'   r'   r(   <module>   sR   4



/
 
#F
	