
    _hi                         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 Z G d d	      Z G d
 dej                         Zy)    N)assert_equal)raiseswarns)expected_warnings)extrema-q=c                     t        j                  | t         j                        } t        j                  |t         j                        }| |z
  dz  j                         }t	        j
                  |      S )Ndtype   )npasarrayfloat64summathsqrt)abts      _/var/www/html/engine/venv/lib/python3.12/site-packages/skimage/morphology/tests/test_extrema.pydiffr      sN    


1BJJ'A


1BJJ'A
a%AA99Q<    c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)TestExtremac                    t        j                  g dg dg dgt         j                        }t        j                  |d      }t        j                  g dg dg dgt         j                        }t        ||      }|t        k  sJ t        j                  |d      }t        j                  g d	g d
g dgt         j                        }t        ||      }|t        k  sJ t        j                  ddgddggt         j                        }t        j                  |d      }t        j                  ddgddggt         j                        }t        ||      }|t        k  sJ t        j                  |d      }t        j                  ddgddggt         j                        }t        ||      }|t        k  sJ y)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   _add_constant_clipr   eps_subtract_constant_clipint16)selfdataimg_constant_addedexpectederrorimg_constant_subtracteds         r   test_saturated_arithmeticz%TestExtrema.test_saturated_arithmetic   s    xx3]C288
 %77a@883]C288
 '2s{{")"A"A$"J882LA
 ,h7s{{ xx%&&)9:"((K$77a@88eU^ff-=>bhhO'2s{{")"A"A$"J88eU^ff-=>bhhO,h7s{{r   c                    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                  	      }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                  	      }t         j                  t         j                  t         j                  t         j
                  fD ]@  }|j                  |      }t        j                  |d      }t        ||      }|t        k  r@J  y)zh-maxima for various data types
r$         r-   r-      r-   r-   rF   rE   
rE   rF   rG      rI   rI   rI   rI   rG   rF   
rF   rG   (   rK      rL   rL   <   rM   rG   
r-   rI   rK   rK      rO   rO   rM   rM   rI   
r-   rI   rL   rO   rO   rO   rO   rO   rL   rI   
rG   rI   rL   rO   rO      rO   rO   rL   rI   
r-   rI   P   rT   rO   rO   rO   r   r   rI   
rF   rG   rT   rT   rL   rL   rL   r   r   rG   r
   
r   r   r   r   r   r   r   r   r   r   
r   r   r   r   r   r   r   r%   r%   r   
r   r   r%   r%   r   r   r   r%   r%   r   rK   N)r   r6   r7   uint64int8int64astyper   h_maximar   r9   r<   r=   expected_resultr   outr@   s         r   test_h_maximazTestExtrema.test_h_maxima6   s     xx8888888::8 ((
  ((.......... ((
 hh		277BHH= 	E;;u%D""4,C#.E3;;	r   c                 R   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
|z
  }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                  	      }t         j                  t         j                  t         j                  t         j
                  fD ][  }|j                  |      }t        j                  |d      }t        ||      }|t        k  sJ |j                  |j                  k(  r[J  y)zh-minima for various data typesrD   rH   rJ   rN   rP   rQ   rS   rU   r
   r   rV   rW   rX   rK   N)r   r6   r7   rY   rZ   r[   r\   r   h_minimar   r9   r   r^   s         r   test_h_minimazTestExtrema.test_h_minima_   s     xx8888888::8 ((
 Tz((.......... ((
 hh		277BHH= 	6E;;u%D""4,C#.E3;;99 5 5555	6r   c                    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
|z
  }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
                  	      }t        ||      }|t        k  sJ t        j                  |d      }t        ||      }|t        k  sJ y)zspecific tests for float type
皙?)\(?p=
ף?Q?rj   333333?rj   rj   ri   rh   
rh   ri   rk   {Gz?rm   rm   rm   rm   rk   ri   
ri   rk   皙?ro   
ףp=
?rp   rp   333333?rq   rk   
rj   rm   ro   ro   RQ?rs   rs   rq   rq   rm   
rj   rm   rp   rs   rs   rs   rs   rs   rp   rm   
rk   gK?rp   rs   g&1?皙?rs   rs   rp   rm   
rj   rm   皙?rx   rs   rs   rs         ?ry   rm   
ri   rk   rx   rx   rp   rp   rp   ry   ry   rk   r
   ry   g~jth?rV   rX   )
r   r   r   r   r%   r   r   r   r   r   N)	r   r6   float32r   r]   r7   r   r9   rc   )r<   r=   inverted_datar`   r_   r@   s         r   test_extrema_floatzTestExtrema.test_extrema_float   s    xxLLLLLNLJJL **
 d
tU+((.......... ((
  _c*s{{}e4_c*s{{r   c                    d}t         j                  d|d|f   \  }}dd||dz  z
  dz  ||dz  z
  dz  z   z  z
  }d|ddddf<   d|ddd	d
f<   d|d	d
ddf<   d|d	d
d	d
f<   |j                  t         j                        }t        j                  |      }d||dkD  <   dD ]/  }t        j                  ||      }t        ||      }|t        k  r/J  y)z,specific tests for h-maxima float image typer$   r   rR   rv   r   rK   r#   rM   r.   r)   rT   r   ry   gfffff3@r   gư>gMbP?g{Gz?rg   rg   N)	r   mgridr\   r{   
zeros_liker   r]   r   r9   	r<   wxyr=   r_   hr`   r@   s	            r   test_h_maxima_float_imagez%TestExtrema.test_h_maxima_float_image   s   xx!QqS!1CAAI!+q1q5yQ.>>??QqS!A#XQqS!A#XQqS!A#XQqS!A#X{{2::&---),&? 	A""4+C#.E3;;	r   c                 ^   t        j                  g dg dg dg dg dgt         j                        }t        j                  ddd      }d}|D ]H  }|d	z  dk7  rd
g}ng }t	        |      5  t        j                  ||      }ddd       d   dk(  sD|d	z  }J |dk(  sJ y# 1 sw Y   !xY w)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
   ry          @r   r   r%   (possible precision loss converting imageNr   r   )r   r6   r7   linspacer   r   r]   )r<   r=   h_valsfailuresr   msgsmaximas          r   test_h_maxima_float_hz!TestExtrema.test_h_maxima_float_h   s    xx ((	
 S#s+ 
	A1uzBC"4( 3 ))$23 d|q A
	 1}}3 3   ,B##B,	c                    t        j                  g dg dg dg dg dgt         j                        }t        j                  |d      }t        j
                  |      dk(  sJ t        j                  g dg dg dg dg dgt         j                        }t        j                  |d      }t        j
                  |      dk(  sJ y)	z.test that h-maxima works correctly for large h)r$   r$   r$   r$   r$   )r$   rF   rF   rF   r$   )r$   rF   r-   rF   r$   r
   r   r         @N)r   r6   r7   r   r]   r   r{   r<   r=   r   s      r   test_h_maxima_large_hz!TestExtrema.test_h_maxima_large_h       xx$$$$$ ((	
 !!$*vvf~"""xx$$$$$ **	
 !!$,vvf~"""r   c                    d}t         j                  d|d|f   \  }}dd||dz  z
  dz  ||dz  z
  dz  z   z  z   }d|ddddf<   d|ddd	d
f<   d|d	d
ddf<   d|d	d
d	d
f<   |j                  t         j                        }t        j                  |      }d||dk  <   dD ]/  }t        j                  ||      }t        ||      }|t        k  r/J  y)z,specific tests for h-minima float image typer$   r      rv   r      r#      r.   r)   x   r   ry   g33333f@r   N)	r   r   r\   r{   r   r   rc   r   r9   r   s	            r   test_h_minima_float_imagez%TestExtrema.test_h_minima_float_image  s   xx!QqS!1SQQY1,AE	a/??@@QqS!A#XQqS!A#XQqS!A#XQqS!A#X{{2::&---*-'? 	A""4+C#.E3;;	r   c                 ^   t        j                  g dg dg dg dg dgt         j                        }t        j                  ddd      }d}|D ]H  }|d	z  dk7  rd
g}ng }t	        |      5  t        j                  ||      }ddd       d   dk(  sD|d	z  }J |dk(  sJ y# 1 sw Y   !xY w)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
   ry   r   r   r   r%   r   Nr   )r   r6   r7   r   r   r   rc   )r<   r=   r   r   r   r   minimas          r   test_h_minima_float_hz!TestExtrema.test_h_minima_float_h  s    xx ((	
 S#s+ 
	A1uzBC"4( 3 ))$23 d|q A
	 1}}3 3r   c                    t        j                  g dg dg dg dg dgt         j                        }t        j                  |d      }t        j
                  |      dk(  sJ t        j                  g dg dg dg dg dgt         j                        }t        j                  |d      }t        j
                  |      dk(  sJ y)	z.test that h-minima works correctly for large h)r-   r-   r-   r-   r-   )r-   rE   rE   rE   r-   )r-   rE   r$   rE   r-   r
   r   r   r   N)r   r6   r7   r   rc   r   r{   r   s      r   test_h_minima_large_hz!TestExtrema.test_h_minima_large_h3  r   r   N)__name__
__module____qualname__rB   ra   rd   r}   r   r   r   r   r   r    r   r   r   r      s7    >'R(6T)V&<#<&:#r   r   c            
       6   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
                        Z ej                   g d	g d	g d
g dg dg dge      Z ej                   g dg dg dg dg dg dge      Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!y )!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                 >   t        j                  t        j                  g g      d      }|j                  dk(  sJ |j
                  t        k(  sJ |j                  dk(  sJ t        j                  t        j                  g       d      }t        |t              sJ t        |      dk(  sJ |d   j                  dk(  sJ |d   j
                  t        j                  k(  sJ t        j                  t        j                  g g      d      }t        |t              sJ t        |      dk(  sJ |d   j                  dk(  sJ |d   j
                  t        j                  k(  sJ |d   j                  dk(  sJ |d   j
                  t        j                  k(  sJ y)	zTest result with empty image.F)indicesr   )r%   r   Tr%   r   N)r   local_maximar   r6   sizer   boolshape
isinstancetuplelenintp)r<   results     r   
test_emptyzTestLocalMaxima.test_empty  s^   %%bhhtneD{{a||t###||v%%%%%bhhrlDA&%(((6{aay~~"""ay"'')))%%bhhtndC&%(((6{aay~~"""ay"'')))ay~~"""ay"'')))r   c                     | j                   D ][  }t        j                  | j                  j	                  |            }|j
                  t        k(  sJ t        || j                         ] y)zATest results with default configuration for all supported dtypes.N)	supported_dtypesr   r   imager\   r   r   r   expected_default)r<   r   r   s      r   test_dtypeszTestLocalMaxima.test_dtypes  sX    ** 	8E))$***;*;E*BCF<<4'''!6!67	8r   c                    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                  	      }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 ]I  }|j                  |      }t        j                  |      }|j                  t        k(  sJ t        ||       K y)z
        Test results with default configuration and data copied from old unit
        tests for all supported dtypes.
        rD   rH   rJ   rN   rP   rQ   rS   rU   r
   rV   rX   )
r   r   r   r   r   r%   r   r   r   r   N)
r   r6   r7   r   r   r\   r   r   r   r   )r<   r=   r?   r   r   r   s         r   test_dtypes_oldzTestLocalMaxima.test_dtypes_old  s    
 xx8888888::8 ((
 88.......... 
 ** 	+EKK&E))%0F<<4'''*		+r   c                    t        j                  | j                  d      }|j                  t        k(  sJ t        || j                         t        j                  | j                  d      }|j                  t        k(  sJ t        || j                         t        j                  | j                  d      }|j                  t        k(  sJ t        || j                         y)z&Test results if footprint is a scalar.r%   )connectivityr   r&   N)r   r   r   r   r   r   expected_crossr   )r<   result_conn1result_conn2result_conn3s       r   test_connectivityz!TestLocalMaxima.test_connectivity  s     ++DJJQG!!T)))\4#6#67 ++DJJQG!!T)))\4#8#89 ++DJJQG!!T)))\4#8#89r   c           	      v   t        j                  g dg dg dgt              }t        j                  | j
                  |      }|j                  t        k(  sJ t        || j                         dt        j                  dt         j                        t        j                  dt         j                        t        j                  dt              fD ]N  }t        j                  | j
                  |      }|j                  t        k(  sJ t        || j                         P t        j                  g dg dg dgt              }t        j                  g dg d	g d
g dg dg dgt              }t        j                  | j
                  |      }|j                  t        k(  sJ t        ||       y)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   r6   r   r   r   r   r   r   r   onesr   r7   r   )r<   footprint_crossresult_footprint_crossr   result_footprint_squarefootprint_xexpected_footprint_xresult_footprint_xs           r   test_footprintzTestLocalMaxima.test_footprint  sd   ((Iy)#DDQ!(!5!5JJ/"
 &++t333+T-@-@A GGF"**-GGF"((+GGF$'	
 	II '.&:&:

i'# +00D8880$2G2GH	I hh	9i@M!xx====== 
 
 %11$**T!''4///')=>r   c                 8   t        j                  | j                        }t        j                  | j
                  dd      }t        ||       t        j                  | j                        }t        j                  | j
                  dd      }t        ||       y)z,Test output if indices of peaks are desired.r%   T)r   r   r   N)r   nonzeror   r   r   r   r   r   )r<   expected_conn1r   expected_conn2r   s        r   test_indiceszTestLocalMaxima.test_indices  st     D$7$78++DJJQPTU\>2 D$9$9:++DJJQPTU\>2r   c           	      ~   t        j                  | j                  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        	      }t        j                  | j                  dd
      }|j                  t        k(  sJ t        ||       y)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   r6   )r<   result_with_boderexpected_without_borderresult_without_borders       r   test_allow_bordersz"TestLocalMaxima.test_allow_borders  s     $00JJQd
 !&&$...&(;(;<"$((====== 
#
 !( 4 4JJQe!
 !&&$...*,CDr   c                    t        j                  g d      }t        j                  g dt              }t        j                  |      }|j
                  t        k(  sJ t        ||       t        j                  dt         j                        }t        j                  dt              }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xx   dz  cc<   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                  |      }|j
                  t        k(  sJ t        ||       y)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%   r5   r5   r   r.   2   r5   r(   )r.   r.   r.   N)	r   r6   r   r   r   r   r   zerosr7   )r<   x_1dexpected_1d	result_1dx_3dexpected_3d	result_3ds          r   test_ndzTestLocalMaxima.test_nd)  s    xx9:hh@M((.	$&&&Y, xx	2hhy5Q!QqS[WQ!QqS[ GQqS!QY!"AaCAIQ!QqS[WQ!QqS[QqS!A#qs]r!#$AqsAaCK #$AqsAaCK !QqS!A#qs]W G((.	$&&&Y,r   c                    t        j                  ddt         j                        }t        j                  dt         j                        }| j                  D ]  }|j                  |      }t        j                  |      }|j                  t        k(  sJ t        ||       t        j                  |      }|j                  t        k(  sJ t        ||        y)z!Test behaviour for 'flat' images.)r.   r5   *   r
   N)r   fullr7   r   r   r\   r   r   r   r   r   local_minima)r<   const_imager?   r   r   s        r   test_constantzTestLocalMaxima.test_constantL  s    ggfb988F"((3** 		+E%,,U3K))+6F<<4'''*))+6F<<4'''*		+r   c                    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
|z
  }t        j                  g dg dg dg dg dg dg dg dg dg dg
t        	      }t	        j
                  |      }|j                  t        k(  sJ t        ||       t	        j                  |      }|j                  t        k(  sJ t        ||       y)zSpecific tests for float type.rf   rl   rn   rr   rt   ru   rw   rz   r
   ry   rV   rX   )
r   r%   r   r   r%   r   r   r   r   r   N)	r   r6   r{   r   r   r   r   r   r   )r<   r   inverted_imager_   r   s        r   r}   z"TestLocalMaxima.test_extrema_float[  s     LLLLLNLJJL **
 u((.......... 
" %%e,||t###V_- %%n5||t###V_-r   c                     t        j                  g dg dg dg dg      }t        j                  |      }t        j                  g dg dg dg dg      }t	        ||       y )N)Ԕdn2;׭;r   r   )r   R;r   r   )FFFF)FTTF)r   r6   r   r   r   )r<   r   r   r_   s       r   test_extrema_small_floatz(TestLocalMaxima.test_extrema_small_float  s^    PPPP	
 %%e,((,**,	
 	V_-r   c                    t        t        d      5  t        j                  | j                  t        j                  dt                     ddd       t        t        d      5  t        j                  | j                  t        j                  dt                     ddd       t        t        d      5  t        j                  | j                  t        j                  d	t                     ddd       t        t        d      5  t        j                  | j                  t        j                  d
t                     ddd       t        t        d      5  t        j                  t        j                  dt
        j                               ddd       y# 1 sw Y   YxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)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emptyfloat16)r<   s    r   test_exceptionszTestLocalMaxima.test_exceptions  s\    J&<= 	W  rwwyPT7UV	WJ&<= 	R  rwwt47PQ	R J&67 	T  rwwvT7RS	TJ&67 	T  rwwvT7RS	T I%EF 	@  !2::!>?	@ 	@	W 	W	R 	R	T 	T	T 	T	@ 	@s;   ;F$&;F1:;F>;G
"9G$F.1F;>G
GGc                    d}t        j                  ddg      }t        j                  |d       t	        t
        |      5  t        j                  |d      }ddd       t        ddg       |j                  t        k(  sJ t        j                  dd	gd	d	gg      }t        j                  |dd
       t	        t
        |      5  t        j                  |dd
      }ddd       t        |t        j                  dt         j                               |d   j                  t         j                  k(  sJ |d   j                  t         j                  k(  sJ y# 1 sw Y   xY w# 1 sw Y   xY w)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   r6   r   r   r   UserWarningr   r   r   r   r   )r<   warning_msgr   r   s       r   test_small_arrayz TestLocalMaxima.test_small_array  s2    IHHaVQd3;k2 	B))!5AF	BVaV$||t###HHq!fq!f%&QdDA;k2 	P))!5$OF	PVRXXfBGG<=ay"'')))ay"'')))	B 	B	P 	Ps   E	E+E(+E4N)"r   r   r   __doc__r   r7   uint16uint32rY   rZ   r;   int32r[   r{   r   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r}   r   r   r   r   r   r   r   r   R  s+   ; 	
		
		
		









 BHH999999	
 hh
E  rxx999999	
 
 RXX999999	
 
N**8'+R:"%?N
3E6!-F++.Z..@"*r   r   )r   unittestnumpyr   numpy.testingr   pytestr   r   skimage._shared.testingr   skimage.morphologyr   r9   r   r   TestCaser   r   r   r   <module>r     sG       &   5 & y# y#x	w*h'' w*r   