a
    
dC%                     @   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 e5yn   dZ1Y n0 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 ur"|d| 7 }| |k s2J |d S )Nz is not lower than :  abmsgmessager    r    `/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/skimage/_shared/testing.pyassert_less:   s    r'   c                 C   s6   | d|}|d ur"|d| 7 }| |ks2J |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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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 s80    Y  tj|| |d tj||d}zt| W n ty   Y n0 |S )z/Save and read an image using a specified plugin.F)suffixdeleteN)plugin)r   namer   ZimsaveZimreadosremove	Exception)imager@   r>   	temp_filefnamenewr    r    r&   	roundtripw   s    $rI   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tifZtiffd   N)r   r   ZchelsearI   r   r   astypeboolr   r   lowerr   r@   fmtimgZr1Zimg2Zr2Zimg3Zr3Zimg4Zr4Zimg5Zr5r    r    r&   color_check   s&    rT   c                 C   s   t t }t|| |}t|| |dk}t|| |}t||t t|}t|| |}|j	j
dkrxt|| 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.
    rK   frL   rM   N)r   r   ZmoonrI   r   r   rN   rO   r   Zdtypekindr   r   rP   rQ   r    r    r&   
mono_check   s*    rW   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%   rZ   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   sV    

	rj   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)r_   r]   resetwarningsr^   r    r    r    r&   teardown_test  s    rl   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ModuleNotFoundErrorpytestskip)Zdata_filenamer    r    r&   fetch  s    
rq   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 r g }td D ]}tj | |d}|| q|D ]}|  q> | i | |D ]}|  q^W d    n1 s0    Y  d S )Nr   )targetargskwargs)r   range	threadingThreadr.   startr4   )rs   rt   threadsithread)r5   num_threadswarnings_matchingr    r&   inner8  s    

z/run_in_parallel.<locals>.wrapper.<locals>.inner)	functoolswraps)r5   r~   r|   r}   )r5   r&   wrapper7  s    z run_in_parallel.<locals>.wrapperr    )r|   r}   r   r    r   r&   run_in_parallel"  s    r   )N)N)rJ   )rJ   )r   N)@r*   rB   restructrv   r   tempfiler   r[   r`   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   ro   markZskipifZxfailZparametrizeZraisesZfixturecompiler,   calcsizeZarch32environgetr_   rP   rO   int
ValueErrorr'   r(   r<   rI   rT   rW   rj   rl   rq   r   r    r    r    r&   <module>   sR   4



/
 
#F
	