
    0-Ph$                        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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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%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j.        d          Z/ ej0        d          dz  dk    Z1ddZ2ddZ3d Z4d Z5ddZ6ddZ7d Z8ddZ9d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_warnings)is_wasmz%(\s*>>>.*?)(\s*)#\s*skip\s+if\s+(.*)$P       c                 H    | d|}||d|z   z  }| |k     s
J |            d S )Nz is not lower than :  abmsgmessages       W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/_shared/testing.pyassert_lessr'   5   s?    ....G
4#:q555'55555    c                 H    | d|}||d|z   z  }| |k    s
J |            d S )Nz is not greater than r   r    r!   s       r&   assert_greaterr*   <   s?    00100G
4#:q555'55555r(   c                    | j                             d          }g }|D ]}t                              |          }||                    |           4|                                \  }}}	 t          || j                  r||z   dz   }n2# t          $ r% t          || j	        j                  r||z   dz   }Y nw xY w|                    |           d
                    |          | _         | S )aC  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liner0   codespaceexprs           r&   doctest_skip_parserr?   C   s   0 Lt$$EI  d##=T"""!LLNNeT	9D$*++ 9e|&88 	9 	9 	9D$-344 9e|&88	9
 	99Y''DLKs   ,B

,B98B9c                    d|vrd|z   }t          |d          5 }|j        }ddd           n# 1 swxY w Y   t          j        || |           t          j        ||          }	 t          j        |           n# t          $ r Y nw xY w|S )z/Save and read an image using a specified plugin.F)suffixdeleteN)plugin)r   namer   imsaveimreadosremove	Exception)imagerD   rB   	temp_filefnamenews         r&   	roundtriprO   r   s    
&v	6%	8	8	8 I              IeU6****
)E&
)
)
)C
	%   Js   /33(A= =
B
	B
pngc                    t          t          j                              }t          || |          }t	          j        ||           |dk    }t          || |          }t	          j        ||                    t                               t          |          }t          || |          }t	          j        ||           t          |          }|
                                dv r,|dz  }t          || |          }	t	          j        |	|           n3t          || |          }	t	          j        |	t          |                     t          |          }
t          |
| |          }t	          j        ||           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   chelsearO   r   r   astypeboolr   r   lowerr   rD   fmtimgr1img2r2img3r3img4r4img5r5s               r&   color_checkrg      sT    t|~~
&
&C	3	$	$BC$$$9D	4	%	%BD"))D//222D	4	%	%BB$$$c??D
yy{{''tVS))D))))tVS))L$6$6777sD	4	%	%BB$$$$$r(   c                 \   t          t          j                              }t          || |          }t	          j        ||           |dk    }t          || |          }t	          j        ||                    t                               t          |          }t          || |          }|j	        j
        dk    rt	          j        ||           n"t	          j        |t          |                     t          |          }|                                dv r,|dz  }t          || |          }	t	          j        |	|           n3t          || |          }	t	          j        |	t          |                     t          |          }
t          |
| |          }t	          j        ||
           dS )zsCheck the roundtrip behavior for images that support most types.

    All major input types should be handled.
    rR   frS   rV   N)r   r   moonrO   r   r   rX   rY   r   dtypekindr   r   rZ   r[   s               r&   
mono_checkrm      s    ty{{
#
#C	3	$	$BC$$$9D	4	%	%BD"))D//222D	4	%	%B	x}b))))K$4$4555c??D
yy{{''tVS))D))))tVS))K$5$5666sD	4	%	%BB%%%%%r(   c                     	 t          |           S # t          t          f$ r t          j        d|  d           Y dS w xY 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_filenames    r&   fetchru      sb    Tm$$$01 T T T9-99dSSSSSSSTs    *??c                 &      dk    sJ  f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.

    It is currently skipped when running on WASM-based platforms, as
    the threading module is not supported.

    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                 j     t           r S dd lt          j                    fd            }|S )Nr   c                  V   t                    5  g }t          dz
            D ]/}                    | |          }|                    |           0|D ]}|                                  | i | |D ]}|                                 	 d d d            d S # 1 swxY w Y   d S )Nr   )targetargskwargs)r   rangeThreadr1   startr7   )	rz   r{   threadsithreadr8   num_threads	threadingwarnings_matchings	        r&   innerz/run_in_parallel.<locals>.wrapper.<locals>.inner   s   "#455 " "{Q// + +A&--TV-TTFNN6****% # #FLLNNNNd%f%%%% " "FKKMMMM"" " " " " " " " " " " " " " " " " "s   A?BB"%B")r   r   	functoolswraps)r8   r   r   r   r   s   ` @r&   wrapperz run_in_parallel.<locals>.wrapper   sb     	K				" 	" 	" 	" 	" 	" 	" 
		" r(   r    )r   r   r   s   `` r&   run_in_parallelr      s8    , ????     0 Nr(   )offsetc          	         d}t          j                    d         j        }|j        |z   }|j        j        }| d| }| D ]@}|j         d|j         }d| d| d|j        j	         d|j
         }	||k    s
J |	            AdS )	a6  Assert correct stacklevel of captured warnings.

    When scikit-image raises warnings, the stacklevel should ideally be set
    so that the origin of the warnings will point to the public function
    that was called by the user and not necessarily the very place where the
    warnings were emitted (which may be inside some internal function).
    This utility function helps with checking that
    the stacklevel was set correctly on warnings captured by `pytest.warns`.

    Parameters
    ----------
    warnings : collections.abc.Iterable[warning.WarningMessage]
        Warnings that were captured by `pytest.warns`.
    offset : int, optional
        Offset from the line this function is called to the line were the
        warning is supposed to originate from. For multiline calls, the
        first line is relevant. Defaults to -1 which corresponds to the line
        right above the one where this function is called.

    Raises
    ------
    AssertionError
        If a warning in `warnings` does not match the expected line number or
        file name.

    Examples
    --------
    >>> def test_something():
    ...     with pytest.warns(UserWarning, match="some message") as record:
    ...         something_raising_a_warning()
    ...     assert_stacklevel(record)
    ...
    >>> def test_another_thing():
    ...     with pytest.warns(UserWarning, match="some message") as record:
    ...         iam_raising_many_warnings(
    ...             "A long argument that forces the call to wrap."
    ...         )
    ...     assert_stacklevel(record, offset=-3)
    Tr   :z+Warning with wrong stacklevel:
  Expected: z
  Actual: z
  r   N)inspectstackframef_linenof_codeco_filenamefilenamelinenocategory__name__r%   )
warningsr   __tracebackhide__r   line_numberr   expectedwarningactualr$   s
             r&   assert_stacklevelr     s    P MOOA$E.6)K|'H**[**H ' '$77w~77@#@ @@ @ !*@ @ /6o@ @ 	 !!!3!!!!' 'r(   )N)rP   )r   N);r-   rH   platformrestructsysr   r   tempfiler   numpynpr   numpy.testingr   r   r   r   r	   r
   r   r   r   r   r    r   r   data._fetchersr   utilr   r   r   r   	_warningsr   _dependency_checksr   rr   markskipifxfailparametrizeraisesfixturecompiler/   calcsizearch32r'   r*   r?   rO   rg   rm   ru   r   r   r    r(   r&   <module>r      s    
			  				  



      ' ' ' ' ' '                                            # # # # # # F F F F F F F F F F F F ( ( ( ( ( ( ' ' ' ' ' '  
	k%	
.
"*=
>
>
 
			!R	'      , , ,^  % % % %@ &  &  &  &FT T T0 0 0 0f +- 6' 6' 6' 6' 6' 6' 6'r(   