o
    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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       #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!   R/var/www/html/scripts/venv/lib/python3.10/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   imsaveimreadosremove	Exception)imagerB   r@   	temp_filefnamenewr!   r!   r'   	roundtripw   s   rM   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.
       tiftiffd   N)r   r   chelsearM   r   r   astypeboolr   r   lowerr   rB   fmtimgr1img2r2img3r3img4r4img5r5r!   r!   r'   color_check   s&   rd   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.
    rO   frP   rS   N)r   r   moonrM   r   r   rU   rV   r   dtypekindr   r   rW   rX   r!   r!   r'   
mono_check   s*   ri   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&   rl   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   

	
r|   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
    rj   N)rq   ro   resetwarningsrp   r!   r!   r!   r'   teardown_test  s   r~   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)allow_module_levelN)r   ConnectionErrorModuleNotFoundErrorpytestskip)data_filenamer!   r!   r'   fetch  s   
r   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Threadr0   startr6   )r   r   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)rN   )r   N)@r,   rF   restructr   r   tempfiler   rm   rr   r   numpy.testingr   r   r   r   r   r	   r
   r   r   r   r   ro    r   r   data._fetchersr   utilr   r   r   r   	_warningsr   r   markskipifxfailparametrizeraisesfixturecompiler.   calcsizearch32environgetrq   rW   rV   int
ValueErrorr(   r*   r>   rM   rd   ri   r|   r~   r   r   r!   r!   r!   r'   <module>   sV    4


/

 #F
	