U
    ?hh                     @   s|   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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mathsqrt)abt r   W/var/www/html/venv/lib/python3.8/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   sn  t jddddgddddgdd	d
dggt jd}t|d}t jddddgddddgddddggt jd}t||}|tk s~tt|d}t jddd
d
gddddgddddggt jd}t||}|tk st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*tt|d
}t jdd"gddggt j	d}t||}|tk sjtd#S )$z*Adding/subtracting a constant and clipping         d               
         r         	   h                     `            r      i  i  i iii  N)
r   arrayuint8r   Z_add_constant_clipr   epsAssertionErrorZ_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ddddddddddg
ddddddddddg
dddddddd	d	dg
ddddd
d
d
d	d	dg
dddd
d
d
d
d
ddg
dddd
d
dd
d
ddg
dddd
d
d
d
d
ddg
ddddd
d
d
dddg
ddddddddddg
ddddddddddg
g
t jd}t jddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
g
t jd}t jt jt jt jfD ]4}||}t|d}t	||}|t
k stqdS )zh-maxima for various data typesr         r(         (      <         P   r   r   r   r    N)r   r1   r2   uint64int8int64astyper   h_maximar   r3   r4   r6   r7   expected_resultr	   outr9   r   r   r   test_h_maxima@   s>    



zTestExtrema.test_h_maximac                 C   s@  t jddddddddddg
ddddddddddg
dddddddd	d	dg
ddddd
d
d
d	d	dg
dddd
d
d
d
d
ddg
dddd
d
dd
d
ddg
dddd
d
d
d
d
ddg
ddddd
d
d
dddg
ddddddddddg
ddddddddddg
g
t jd}d| }t jddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
g
t jd}t jt jt jt jfD ]F}||}t|d}t	||}|t
k s&t|j|jkstqdS )zh-minima for various data typesr   r;   r<   r(   r=   r>   r?   r@   rA   rB   rC   rD   r   r   r   r    N)r   r1   r2   rE   rF   rG   rH   r   h_minimar   r3   r4   r	   rJ   r   r   r   test_h_minimaa   sB    



zTestExtrema.test_h_minimac                 C   s,  t jddddddddddg
ddddddddddg
dddddddd	d	dg
ddddd
d
d
d	d	dg
dddd
d
d
d
d
ddg
dddd
ddd
d
ddg
dddd
d
d
d
d
ddg
ddddd
d
d
dddg
ddddddddddg
ddddddddddg
g
t jd}d| }t|d}t jddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
g
t jd}t||}|tk stt	|d}t||}|tk s(tdS )zspecific tests for float type皙?)\(?p=
ף?Q?333333?{Gz?皙?
ףp=
?333333?RQ?粝K?&1?皙?皙?      ?r   g~jth?r   r    N)
r   r1   float32r   rI   r2   r   r3   r4   rN   )r6   r7   Zinverted_datarL   rK   r9   r   r   r   test_extrema_float   s                        


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 st	qdS )z,specific tests for h-maxima float image typer   r   rC   r\   r
   r?   r   rA   r)   r$   rD   r   r^   gfffff3@r   gư>gMbP?g{Gz?rP   rP   N)
r   mgridrH   r_   
zeros_liker   rI   r   r3   r4   	r6   wxyr7   rK   hrL   r9   r   r   r   test_h_maxima_float_image   s    (

z%TestExtrema.test_h_maxima_float_imagec              
   C   s   t jdddddgdddddgdddddgdddddgdddddggt jd}t ddd}d}|D ]P}|d dkrzd	g}ng }t| t||}W 5 Q R X |d
 dkrb|d7 }qb|dkstdS )z-specific tests for h-maxima float h parameterr   r!   r   r   r^          @r   r    (possible precision loss converting imager
   r
   N)r   r1   r2   linspacer   r   rI   r4   )r6   r7   h_valsfailuresrh   msgsmaximar   r   r   test_h_maxima_float_h   s&    

z!TestExtrema.test_h_maxima_float_hc              
   C   s   t jdddddgdddddgdddddgdddddgdddddggt jd}t|d}t |dksjtt jdddddgdddddgdddddgdddddgdddddggt jd}t|d}t |dkstdS )	z.test that h-maxima works correctly for large hr   r<   r(   r   r   r         @N)r   r1   r2   r   rI   r   r4   r_   r6   r7   rq   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 st	qdS )z,specific tests for h-minima float image typer   r      r\   r
      r      r)   r$   x   r   r^   g33333f@ra   N)
r   rb   rH   r_   rc   r   rN   r   r3   r4   rd   r   r   r   test_h_minima_float_image   s    (

z%TestExtrema.test_h_minima_float_imagec              
   C   s   t jdddddgdddddgdddddgdddddgdddddggt jd}t ddd}d}|D ]P}|d dkrzdg}ng }t| t||}W 5 Q R X |d	 dkrb|d7 }qb|dkstd
S )z-specific tests for h-minima float h parameterr   r    r   r   r^   rj   r   rk   rl   N)r   r1   r2   rm   r   r   rN   r4   )r6   r7   rn   ro   rh   rp   Zminimar   r   r   test_h_minima_float_h  s&    

z!TestExtrema.test_h_minima_float_hc              
   C   s   t jdddddgdddddgdddddgdddddgdddddggt jd}t|d}t |dksjtt jdddddgdddddgdddddgdddddgdddddggt jd}t|d}t |dkstdS )	z.test that h-minima works correctly for large hr(   r;   r   r   r   r   rs   N)r   r1   r2   r   rN   r   r4   r_   rt   r   r   r   test_h_minima_large_h  s(    z!TestExtrema.test_h_minima_large_hN)__name__
__module____qualname__r:   rM   rO   r`   ri   rr   ru   rz   r{   r|   r   r   r   r   r      s   (!"-r   c                   @   s  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dddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddggejdZejdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddggedZejdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddg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   c                 C   s   t jtg gdd}|jdks$t|jtks2t|jdks@tt jtg dd}t	|t
sbtt|dksrt|d jdkst|d jtjkstt jtg gdd}t	|t
stt|dkst|d jdkst|d jtjkst|d jdkst|d jtjkstdS )	zTest result with empty image.F)indicesr   )r    r   Tr    r
   N)r   local_maximar   r1   sizer4   r	   boolshape
isinstancetuplelenintp)r6   resultr   r   r   
test_emptyV  s     zTestLocalMaxima.test_emptyc                 C   s<   | j D ]0}t| j|}|jtks*tt|| j	 qdS )zATest results with default configuration for all supported dtypes.N)
supported_dtypesr   r   imagerH   r	   r   r4   r   expected_default)r6   r	   r   r   r   r   test_dtypesk  s    
zTestLocalMaxima.test_dtypesc                 C   s  t jddddddddddg
ddddddddddg
dddddddd	d	dg
ddddd
d
d
d	d	dg
dddd
d
d
d
d
ddg
dddd
d
dd
d
ddg
dddd
d
d
d
d
ddg
ddddd
d
d
dddg
ddddddddddg
ddddddddddg
g
t jd}t jddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
g
td}| jD ]4}||}t|}|jtkst	t
|| qdS )z
        Test results with default configuration and data copied from old unit
        tests for all supported dtypes.
        r   r;   r<   r(   r=   r>   r?   r@   rA   rB   rC   rD   r   r   r   r    N)r   r1   r2   r   r   rH   r   r   r	   r4   r   )r6   r7   r8   r	   r   r   r   r   r   test_dtypes_oldr  sB    




zTestLocalMaxima.test_dtypes_oldc                 C   s   t j| jdd}|jtkstt|| j t j| jdd}|jtksHtt|| j t j| jdd}|jtksrtt|| j dS )z&Test results if footprint is a scalar.r    )connectivityr
   r!   N)	r   r   r   r	   r   r4   r   expected_crossr   )r6   result_conn1result_conn2Zresult_conn3r   r   r   test_connectivity  s    z!TestLocalMaxima.test_connectivityc                 C   s  t jdddgdddgdddggtd}tj| j|d}|jtksDt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stt|| j q~t jdddgdddgdddggtd}t jdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddggtd}tj| j|d}|jtkstt|| dS )z#Test results if footprint is given.r   r    r   	footprint)TTTr   r   )r!   r!   N)r   r1   r   r   r   r   r	   r4   r   r   onesr   r2   r   )r6   Zfootprint_crossZresult_footprint_crossr   Zresult_footprint_squareZfootprint_xZexpected_footprint_xZresult_footprint_xr   r   r   test_footprint  sJ       &      	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   )r6   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 tt|| j tj	dddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddgdddddddddddddddggtd}t j| jddd}|jtkstt|| dS )z*Test maxima detection at the image border.r    T)r   allow_bordersr   r   FN)
r   r   r   r	   r   r4   r   r   r   r1   )r6   Zresult_with_boderZexpected_without_borderZresult_without_borderr   r   r   test_allow_borders  s0            	  z"TestLocalMaxima.test_allow_bordersc                 C   s  t dddddddddddg}t jdddddddddddgtd}t|}|jtks\t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st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    r0   r0   r)   2   r0   r#   )r)   r)   r)   N)
r   r1   r   r   r   r	   r4   r   zerosr2   )r6   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sNt
t|| t|}|jt	kspt
t|| q(dS )z!Test behaviour for 'flat' images.)r)   r0   *   r   N)r   fullr2   r   r   rH   r   r   r	   r   r4   r   local_minima)r6   Zconst_imager8   r	   r   r   r   r   test_constant  s    




zTestLocalMaxima.test_constantc                 C   s*  t jddddddddddg
ddddddddddg
dddddddd	d	dg
ddddd
d
d
d	d	dg
dddd
d
d
d
d
ddg
dddd
ddd
d
ddg
dddd
d
d
d
d
ddg
ddddd
d
d
dddg
ddddddddddg
ddddddddddg
g
t jd}d| }t jddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
ddddddddddg
g
td}t|}|jtkstt|| t	|}|jtkstt|| dS )zSpecific tests for float type.rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r   r   r    N)
r   r1   r_   r   r   r   r	   r4   r   r   )r6   r   Zinverted_imagerK   r   r   r   r   r`   &  sF    




z"TestLocalMaxima.test_extrema_floatc              	   C   s   t tdd  tj| jtjdtdd W 5 Q R X t tdd  tj| jtjdtdd W 5 Q R X t tdd  tj| jtjdtdd W 5 Q R X t tdd  tj| jtjd	tdd W 5 Q R X t td
d ttj	dtj
d W 5 Q R X dS )z5Test if input validation triggers correct exceptions.znumber of dimensionsmatch)r!   r!   r!   r   r   )r!   zdimension size)r
   r!   )r   r   zfloat16 which is not supportedr    N)r   
ValueErrorr   r   r   r   r   r   	TypeErroremptyZfloat16)r6   r   r   r   test_exceptionsO  s,        zTestLocalMaxima.test_exceptionsc              	   C   s   d}t ddg}tj|dd tt|d tj|dd}W 5 Q R X t|ddg |jtksbt	t ddgddgg}tj|ddd	 tt|d tj|ddd	}W 5 Q R X t|t j
d
t jd |d jt jkst	|d jt jkst	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   Fr
   )r   r   )r
   r   r   N)r   r1   r   r   r   UserWarningr   r	   r   r4   r   r   )r6   Zwarning_msgrf   r   r   r   r   test_small_arrayd  s    z TestLocalMaxima.test_small_arrayN)!r}   r~   r   __doc__r   r2   Zuint16Zuint32rE   rF   r5   Zint32rG   r_   r   r   r1   r   r   r   r   r   r   r   r   r   r   r   r   r   r`   r   r   r   r   r   r   r   0  sl                
      
      
%&$)r   )r   Zunittestnumpyr   Znumpy.testingr   Zpytestr   r   Zskimage._shared.testingr   Zskimage.morphologyr   r3   r   r   ZTestCaser   r   r   r   r   <module>   s     