a
    =bdj                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
 d dlmZ dZdd ZG d	d
 d
ZG dd dejZejdg ddd ZdS )    N)assert_equal)raiseswarns)expected_warnings)extrema-q=c                 C   s:   t j| t jd} t j|t jd}| | d  }t|S )Ndtype   )npZasarrayfloat64summathZsqrt)abt r   Dlib/python3.9/site-packages/skimage/morphology/tests/test_extrema.pydiff   s    r   c                   @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )TestExtremac                 C   sJ  t jg dg dg dgt jd}t|d}t jg dg dg dgt jd}t||}|tk sfJ t|d}t jg d	g d
g dgt jd}t||}|tk sJ t jddgddggt jd}t|d}t jddgddggt jd}t||}|tk sJ t|d}t jddgddggt jd}t||}|tk sFJ dS )z*Adding/subtracting a constant and clipping)         r   )d            )   
         r   r   )      	   r#   )h      r"   r"   )      r      )      r   r   )`            )r      r   r   i  i  i ir   ii  N)	r   arrayuint8r   Z_add_constant_clipr   epsZ_subtract_constant_clipint16)selfdataZimg_constant_addedexpectederrorZimg_constant_subtractedr   r   r   test_saturated_arithmetic   sZ    









z%TestExtrema.test_saturated_arithmeticc                 C   s   t jg dg dg dg dg dg dg dg dg dg dg
t jd	}t jg d
g d
g dg dg d
g d
g d
g dg dg d
g
t jd	}t jt jt jt jfD ]0}||}t|d}t	||}|t
k sJ qdS )zh-maxima for various data types
r         r'   r'      r'   r'   r;   r:   
r:   r;   r<      r>   r>   r>   r>   r<   r;   
r;   r<   (   r@      rA   rA   <   rB   r<   
r'   r>   r@   r@      rD   rD   rB   rB   r>   
r'   r>   rA   rD   rD   rD   rD   rD   rA   r>   
r<   r>   rA   rD   rD      rD   rD   rA   r>   
r'   r>   P   rI   rD   rD   rD   r   r   r>   
r;   r<   rI   rI   rA   rA   rA   r   r   r<   r   
r   r   r   r   r   r   r   r   r   r   
r   r   r   r   r   r   r   r   r   r   
r   r   r   r   r   r   r   r   r   r   r@   N)r   r0   r1   uint64int8int64astyper   h_maximar   r2   r4   r5   expected_resultr	   outr7   r   r   r   test_h_maximaA   s>    





zTestExtrema.test_h_maximac                 C   s   t jg dg dg dg dg dg dg dg dg dg dg
t jd	}d
| }t jg dg dg dg dg dg dg dg dg dg dg
t jd	}t jt jt jt jfD ]@}||}t|d}t	||}|t
k sJ |j|jksJ qdS )zh-minima for various data typesr9   r=   r?   rC   rE   rF   rH   rJ   r   r   rK   rL   rM   r@   N)r   r0   r1   rN   rO   rP   rQ   r   h_minimar   r2   r	   rS   r   r   r   test_h_minimab   sB    





zTestExtrema.test_h_minimac                 C   s   t jg dg dg dg dg dg dg dg dg dg dg
t jd	}d
| }t|d}t jg dg dg dg dg dg dg dg dg dg dg
t jd	}t||}|tk sJ t|d}t||}|tk sJ dS )zspecific tests for float type
皙?)\(?p=
ף?Q?r]   333333?r]   r]   r\   r[   
r[   r\   r^   {Gz?r`   r`   r`   r`   r^   r\   
r\   r^   皙?rb   
ףp=
?rc   rc   333333?rd   r^   
r]   r`   rb   rb   RQ?rf   rf   rd   rd   r`   
r]   r`   rc   rf   rf   rf   rf   rf   rc   r`   
r^   gK?rc   rf   g&1?皙?rf   rf   rc   r`   
r]   r`   皙?rk   rf   rf   rf         ?rl   r`   
r\   r^   rk   rk   rc   rc   rc   rl   rl   r^   r   rl   g~jth?rK   rM   )
r   r   r   r   r   r   r   r   r   r   N)	r   r0   float32r   rR   r1   r   r2   rW   )r4   r5   Zinverted_datarU   rT   r7   r   r   r   test_extrema_float   sB    




zTestExtrema.test_extrema_floatc           	      C   s   d}t jd|d|f \}}dd||d  d ||d  d    }d|ddddf< d|ddd	d
f< d|d	d
ddf< d|d	d
d	d
f< |t j}t |}d||dk< dD ]&}t||}t||}|tk sJ qdS )z,specific tests for h-maxima float image typer   r   rG   ri   r
   r@   r   rB   r(   r#   rI   r   rl   gfffff3@r   gư>gMbP?g{Gz?rZ   rZ   N)	r   mgridrQ   rn   
zeros_liker   rR   r   r2   	r4   wxyr5   rT   hrU   r7   r   r   r   test_h_maxima_float_image   s    (

z%TestExtrema.test_h_maxima_float_imagec              	   C   s   t jg dg dg dg dg dgt jd}t ddd}d}|D ]d}|d	 dkr\d
g}ng }t| t||}W d   n1 s0    Y  |d dkrD|d	7 }qD|dksJ dS )z-specific tests for h-maxima float h parameter)r   r   r   r   r   )r   r    r    r    r   )r   r    r   r    r   r   rl          @r   r   r   (possible precision loss converting imageNr
   r
   )r   r0   r1   linspacer   r   rR   )r4   r5   h_valsfailuresrw   msgsmaximar   r   r   test_h_maxima_float_h   s&    

*
z!TestExtrema.test_h_maxima_float_hc                 C   s   t jg dg dg dg dg dgt jd}t|d}t |dksLJ t jg dg dg dg dg dgt jd}t|d}t |dksJ dS )	z.test that h-maxima works correctly for large h)r   r   r   r   r   )r   r;   r;   r;   r   )r   r;   r'   r;   r   r   r   r         @N)r   r0   r1   r   rR   r   rn   r4   r5   r   r   r   r   test_h_maxima_large_h   s(    

z!TestExtrema.test_h_maxima_large_hc           	      C   s   d}t jd|d|f \}}dd||d  d ||d  d    }d|ddddf< d|ddd	d
f< d|d	d
ddf< d|d	d
d	d
f< |t j}t |}d||dk < dD ]&}t||}t||}|tk sJ qdS )z,specific tests for h-minima float image typer   r      ri   r
      r      r(   r#   x   r   rl   g33333f@rp   N)	r   rq   rQ   rn   rr   r   rW   r   r2   rs   r   r   r   test_h_minima_float_image   s    (

z%TestExtrema.test_h_minima_float_imagec              	   C   s   t jg dg dg dg dg dgt jd}t ddd}d}|D ]d}|d	 dkr\d
g}ng }t| t||}W d   n1 s0    Y  |d dkrD|d	7 }qD|dksJ dS )z-specific tests for h-minima float h parameter)r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   r   r   rl   ry   r   r   r   rz   Nr{   )r   r0   r1   r|   r   r   rW   )r4   r5   r}   r~   rw   r   Zminimar   r   r   test_h_minima_float_h  s&    

*
z!TestExtrema.test_h_minima_float_hc                 C   s   t jg dg dg dg dg dgt jd}t|d}t |dksLJ t jg dg dg dg dg dgt jd}t|d}t |dksJ dS )	z.test that h-minima works correctly for large h)r'   r'   r'   r'   r'   )r'   r:   r:   r:   r'   )r'   r:   r   r:   r'   r   r   r   r   N)r   r0   r1   r   rW   r   rn   r   r   r   r   test_h_minima_large_h  s(    

z!TestExtrema.test_h_minima_large_hN)__name__
__module____qualname__r8   rV   rX   ro   rx   r   r   r   r   r   r   r   r   r   r      s   (!"-r   c                
   @   s4  e Zd ZdZejejejejej	ej
ejejejejg
Zejg dg dg dg dg dg dgejdZejg d	g d	g d
g dg dg dgedZejg dg dg dg dg dg dgedZdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Z d+S ),TestLocalMaximaz1Some tests for local_minima are included as well.r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r
   r   r   r    r    r   r   r   r   r
   r   r   )r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   )r   r
   r   r   r   r
   r   r   r   r   r   r    r   r   r   )r   r   r
   r   r
   r   r   r   r
   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   c                 C   s   t jtg gdd}|jdks$J |jtks2J |jdks@J t jtg dd}t|t	sbJ t
|dksrJ |d jdksJ |d jtjksJ t jtg gdd}t|t	sJ t
|dksJ |d jdksJ |d jtjksJ |d jdksJ |d jtjksJ dS )	zTest result with empty image.F)indicesr   )r   r   Tr   r
   N)r   local_maximar   r0   sizer	   boolshape
isinstancetuplelenintp)r4   resultr   r   r   
test_emptyW  s     zTestLocalMaxima.test_emptyc                 C   s<   | j D ]0}t| j|}|jtks*J t|| j qdS )zATest results with default configuration for all supported dtypes.N)	supported_dtypesr   r   imagerQ   r	   r   r   expected_default)r4   r	   r   r   r   r   test_dtypesl  s    
zTestLocalMaxima.test_dtypesc                 C   s   t jg dg dg dg dg dg dg dg dg dg dg
t jd	}t jg d
g d
g dg dg d
g dg d
g dg dg d
g
td	}| jD ]0}||}t|}|jtksJ t	|| qdS )z
        Test results with default configuration and data copied from old unit
        tests for all supported dtypes.
        r9   r=   r?   rC   rE   rF   rH   rJ   r   rK   rM   )
r   r   r   r   r   r   r   r   r   r   N)
r   r0   r1   r   r   rQ   r   r   r	   r   )r4   r5   r6   r	   r   r   r   r   r   test_dtypes_olds  sB    




zTestLocalMaxima.test_dtypes_oldc                 C   s   t j| jdd}|jtksJ t|| j t j| jdd}|jtksHJ t|| j t j| jdd}|jtksrJ t|| j dS )z&Test results if footprint is a scalar.r   )connectivityr
   r    N)r   r   r   r	   r   r   expected_crossr   )r4   result_conn1result_conn2Zresult_conn3r   r   r   test_connectivity  s    z!TestLocalMaxima.test_connectivityc                 C   s(  t jg dg dg dgtd}tj| j|d}|jtks>J t|| j dt j	dt j
dt j	dt jdt j	dtdfD ].}tj| j|d}|jtksJ t|| j qxt jg dg dg dgtd}t jg dg d	g d
g dg dg dgtd}tj| j|d}|jtksJ t|| dS )z#Test results if footprint is given.)r   r   r   )r   r   r   r   	footprint)TTTr   r   r    r    )r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   N)r   r0   r   r   r   r   r	   r   r   onesr   r1   r   )r4   Zfootprint_crossZresult_footprint_crossr   Zresult_footprint_squareZfootprint_xZexpected_footprint_xZresult_footprint_xr   r   r   test_footprint  sD     	zTestLocalMaxima.test_footprintc                 C   sT   t | j}tj| jddd}t|| t | j}tj| jddd}t|| dS )z,Test output if indices of peaks are desired.r   T)r   r   r
   N)r   Znonzeror   r   r   r   r   r   )r4   Zexpected_conn1r   Zexpected_conn2r   r   r   r   test_indices  s    


zTestLocalMaxima.test_indicesc                 C   s   t j| jddd}|jtks J t|| j tjg dg dg dg dg dg dgtd	}t j| jdd
d}|jtks~J t|| dS )z*Test maxima detection at the image border.r   T)r   allow_borders)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   FN)	r   r   r   r	   r   r   r   r   r0   )r4   Zresult_with_boderZexpected_without_borderZresult_without_borderr   r   r   test_allow_borders  s(    	z"TestLocalMaxima.test_allow_bordersc                 C   s  t g d}t jg dtd}t|}|jtks8J t|| t jdt jd}t jdtd}d|dddddf< d|d	< d|dddddf< d|d	< d|d
dddf< d|d
dddf< d|dd
dd
df< d|d< d|dd
dd
df< |ddd
dd
df  d7  < d|dd
dd
df< d|dd
dd
df< d|ddddddf< d|d< d|d< t|}|jtksJ t|| dS )z%Test one- and three-dimensional case.)r   r   r   r   r
   r    r   r
   r   r
   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r&   r&   r&   r   r   r    r   )r
   r
   r
   r   r&   r   )r   r/   r/   r
   r(   2   r/   r"   )r(   r(   r(   N)	r   r0   r   r   r   r	   r   zerosr1   )r4   Zx_1dZexpected_1dZ	result_1dZx_3dZexpected_3dZ	result_3dr   r   r   test_nd  s6    


"
zTestLocalMaxima.test_ndc                 C   s   t jddt jd}t jdt jd}| jD ]R}||}t|}|jt	ksNJ t
|| t|}|jt	kspJ t
|| q(dS )z!Test behaviour for 'flat' images.)r(   r/   *   r   N)r   Zfullr1   r   r   rQ   r   r   r	   r   r   local_minima)r4   Zconst_imager6   r	   r   r   r   r   test_constant  s    




zTestLocalMaxima.test_constantc                 C   s   t jg dg dg dg dg dg dg dg dg dg dg
t jd	}d
| }t jg dg dg dg dg dg dg dg dg dg dg
td	}t|}|jtksJ t|| t|}|jtksJ t|| dS )zSpecific tests for float type.rY   r_   ra   re   rg   rh   rj   rm   r   rl   rK   rM   )
r   r   r   r   r   r   r   r   r   r   N)	r   r0   rn   r   r   r   r	   r   r   )r4   r   Zinverted_imagerT   r   r   r   r   ro   '  sF    




z"TestLocalMaxima.test_extrema_floatc                 C   sb  t tdd* tj| jtjdtdd W d   n1 s<0    Y  t tdd* tj| jtjdtdd W d   n1 s0    Y  t tdd* tj| jtjd	tdd W d   n1 s0    Y  t tdd* tj| jtjd
tdd W d   n1 s0    Y  t tdd& ttj	dtj
d W d   n1 sT0    Y  dS )z5Test if input validation triggers correct exceptions.znumber of dimensionsmatch)r    r    r    r   r   N)r    zdimension size)r
   r    )r   r   zfloat16 which is not supportedr   )r   
ValueErrorr   r   r   r   r   r   	TypeErroremptyZfloat16)r4   r   r   r   test_exceptionsP  s$    $$$&zTestLocalMaxima.test_exceptionsc                 C   s  d}t ddg}tj|dd tt|d tj|dd}W d   n1 sP0    Y  t|ddg |jtksvJ t dd	gd	d	gg}tj|ddd
 tt|d  tj|ddd
}W d   n1 s0    Y  t|t j	dt j
d |d jt j
ksJ |d jt j
ksJ dS )a  Test output for arrays with dimension smaller 3.

        If any dimension of an array is smaller than 3 and `allow_borders` is
        false a footprint, which has at least 3 elements in each
        dimension, can't be applied. This is an implementation detail so
        `local_maxima` should still return valid output (see gh-3261).

        If `allow_borders` is true the array is padded internally and there is
        no problem.
        z0maxima can't exist .* any dimension smaller 3 .*r   r   T)r   r   FNr
   )r   r   )r
   r   r   )r   r0   r   r   r   UserWarningr   r	   r   r   r   )r4   Zwarning_msgru   r   r   r   r   test_small_arraye  s    ,.z TestLocalMaxima.test_small_arrayN)!r   r   r   __doc__r   r1   Zuint16Zuint32rN   rO   r3   Zint32rP   rn   r   r   r0   r   r   r   r   r   r   r   r   r   r   r   r   r   ro   r   r   r   r   r   r   r   1  s^   


%&$)r   function)r   r   rW   rR   c                 C   sl   t d}| drdnd}tdg2 tt| |g|R dt di W d    n1 s^0    Y  d S )N)r>   r>   Zh_)rG   r   z%`selem` is a deprecated argument nameZselemr   )r   r   
startswithr   getattrr   r   )r   Zimgargsr   r   r   test_selem_kwarg_deprecation  s    
r   )r   ZunittestZnumpyr   ZpytestZnumpy.testingr   r   r   Zskimage._shared.testingr   Zskimage.morphologyr   r2   r   r   ZTestCaser   ZmarkZparametrizer   r   r   r   r   <module>   s(       R