o
    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   )npasarrayfloat64summathsqrt)abt r   `/var/www/html/scripts/venv/lib/python3.10/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   sF  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 s3J t|d}t jg d	g d
g dgt jd}t||}|tk sUJ 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 sJ t|d}t jddgddggt jd}t||}|tk sJ 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   _add_constant_clipr   eps_subtract_constant_clipint16)selfdataimg_constant_addedexpectederror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 ]}||}t|d}t	||}|t
k snJ qVdS )zh-maxima for various data types
r          r)   r)      r)   r)   rA   r@   
r@   rA   rB      rD   rD   rD   rD   rB   rA   
rA   rB   (   rF      rG   rG   <   rH   rB   
r)   rD   rF   rF      rJ   rJ   rH   rH   rD   
r)   rD   rG   rJ   rJ   rJ   rJ   rJ   rG   rD   
rB   rD   rG   rJ   rJ      rJ   rJ   rG   rD   
r)   rD   P   rO   rJ   rJ   rJ   r   r   rD   
rA   rB   rO   rO   rG   rG   rG   r   r   rB   r   
r   r   r   r   r   r   r   r   r   r   
r   r   r   r   r   r   r   r!   r!   r   
r   r   r!   r!   r   r   r   r!   r!   r   rF   N)r   r2   r3   uint64int8int64astyper   h_maximar   r5   r8   r9   expected_resultr	   outr<   r   r   r   test_h_maxima@   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 srJ |j|jkszJ qZdS )zh-minima for various data typesr?   rC   rE   rI   rK   rL   rN   rP   r   r   rQ   rR   rS   rF   N)r   r2   r3   rT   rU   rV   rW   r   h_minimar   r5   r	   rY   r   r   r   test_h_minimaa   sD   





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 saJ t|d}t||}|tk srJ dS )zspecific tests for float type
皙?)\(?p=
ף?Q?rc   333333?rc   rc   rb   ra   
ra   rb   rd   {Gz?rf   rf   rf   rf   rd   rb   
rb   rd   皙?rh   
ףp=
?ri   ri   333333?rj   rd   
rc   rf   rh   rh   RQ?rl   rl   rj   rj   rf   
rc   rf   ri   rl   rl   rl   rl   rl   ri   rf   
rd   gK?ri   rl   g&1?皙?rl   rl   ri   rf   
rc   rf   皙?rq   rl   rl   rl         ?rr   rf   
rb   rd   rq   rq   ri   ri   ri   rr   rr   rd   r   rr   g~jth?rQ   rS   )
r   r   r   r   r!   r   r   r   r   r   N)	r   r2   float32r   rX   r3   r   r5   r]   )r8   r9   inverted_datar[   rZ   r<   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 sqJ q^dS )z,specific tests for h-maxima float image typer    r   rM   ro   r
   rF   r   rH   r*   r%   rO   r   rr   gfffff3@r   gư>gMbP?g{Gz?r`   r`   N)	r   mgridrW   rt   
zeros_liker   rX   r   r5   	r8   wxyr9   rZ   hr[   r<   r   r   r   test_h_maxima_float_image      (

z%TestExtrema.test_h_maxima_float_imagec              	   C      t jg dg dg dg dg dgt jd}t ddd}d}|D ]2}|d	 dkr.d
g}ng }t| t||}W d   n1 sEw   Y  |d dkrT|d	7 }q"|dks[J 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   rr          @r   r   r!   (possible precision loss converting imageNr
   r
   )r   r2   r3   linspacer   r   rX   )r8   r9   h_valsfailuresr~   msgsmaximar   r   r   test_h_maxima_float_h   s*   

z!TestExtrema.test_h_maxima_float_hc                 C      t jg dg dg dg dg dgt jd}t|d}t |dks&J t jg dg dg dg dg dgt jd}t|d}t |dksLJ dS )	z.test that h-maxima works correctly for large h)r    r    r    r    r    )r    rA   rA   rA   r    )r    rA   r)   rA   r    r   r   r         @N)r   r2   r3   r   rX   r   rt   r8   r9   r   r   r   r   test_h_maxima_large_h   (   

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 sqJ q^dS )z,specific tests for h-minima float image typer    r      ro   r
      r      r*   r%   x   r   rr   g33333f@rw   N)	r   rx   rW   rt   ry   r   r]   r   r5   rz   r   r   r   test_h_minima_float_image   r   z%TestExtrema.test_h_minima_float_imagec              	   C   r   )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   rr   r   r   r   r!   r   Nr   )r   r2   r3   r   r   r   r]   )r8   r9   r   r   r~   r   minimar   r   r   test_h_minima_float_h  s*   

z!TestExtrema.test_h_minima_float_hc                 C   r   )	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   r2   r3   r   r]   r   rt   r   r   r   r   test_h_minima_large_h  r   z!TestExtrema.test_h_minima_large_hN)__name__
__module____qualname__r>   r\   r^   rv   r   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sJ |jdks J t jtg dd}t|t	s1J t
|dks9J |d jdksBJ |d jtjksLJ t jtg gdd}t|t	s^J t
|dksfJ |d jdksoJ |d jtjksyJ |d jdksJ |d jtjksJ dS )	zTest result with empty image.F)indicesr   )r!   r   Tr!   r
   N)r   local_maximar   r2   sizer	   boolshape
isinstancetuplelenintp)r8   resultr   r   r   
test_emptyV  s    zTestLocalMaxima.test_emptyc                 C   s<   | j D ]}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   imagerW   r	   r   r   expected_default)r8   r	   r   r   r   r   test_dtypesk  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 ]}||}t|}|jtksaJ t	|| qNdS )z
        Test results with default configuration and data copied from old unit
        tests for all supported dtypes.
        r?   rC   rE   rI   rK   rL   rN   rP   r   rQ   rS   )
r   r   r   r   r   r!   r   r   r   r   N)
r   r2   r3   r   r   rW   r   r   r	   r   )r8   r9   r;   r	   r   r   r   r   r   test_dtypes_oldr  sD   




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s$J t|| j t j| jdd}|jtks9J t|| j dS )z&Test results if footprint is a scalar.r!   )connectivityr
   r"   N)r   r   r   r	   r   r   expected_crossr   )r8   result_conn1result_conn2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sMJ t|| j q<t 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sJ 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   r2   r   r   r   r   r	   r   r   onesr   r3   r   )r8   footprint_crossresult_footprint_crossr   result_footprint_squarefootprint_xexpected_footprint_x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   nonzeror   r   r   r   r   r   )r8   expected_conn1r   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   r2   )r8   result_with_boderexpected_without_border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sJ 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!   r1   r1   r
   r*   2   r1   r$   )r*   r*   r*   N)	r   r2   r   r   r   r	   r   zerosr3   )r8   x_1dexpected_1d	result_1dx_3dexpected_3d	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 ])}||}t|}|jt	ks'J t
|| t|}|jt	ks8J t
|| qdS )z!Test behaviour for 'flat' images.)r*   r1   *   r   N)r   fullr3   r   r   rW   r   r   r	   r   r   local_minima)r8   const_imager;   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s[J t|| t|}|jtkslJ t|| dS )zSpecific tests for float type.r_   re   rg   rk   rm   rn   rp   rs   r   rr   rQ   rS   )
r   r!   r   r   r!   r   r   r   r   r   N)	r   r2   rt   r   r   r   r	   r   r   )r8   r   inverted_imagerZ   r   r   r   r   rv   &  sF   




z"TestLocalMaxima.test_extrema_floatc                 C   s`  t tdd tj| jtjdtdd W d   n1 sw   Y  t tdd tj| jtjdtdd W d   n1 sAw   Y  t tdd tj| jtjd	tdd W d   n1 sdw   Y  t tdd tj| jtjd
tdd W d   n1 sw   Y  t tdd ttj	dtj
d W d   dS 1 sw   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emptyfloat16)r8   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 d   n1 s(w   Y  t|ddg |jtks;J 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 sgw   Y  t|t j	dt j
d |d jt j
ksJ |d jt j
ksJ 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   r2   r   r   r   UserWarningr   r	   r   r   r   )r8   warning_msgr|   r   r   r   r   test_small_arrayd  s    z TestLocalMaxima.test_small_arrayN)!r   r   r   __doc__r   r3   uint16uint32rT   rU   r7   int32rV   rt   r   r   r2   r   r   r   r   r   r   r   r   r   r   r   r   r   rv   r   r   r   r   r   r   r   0  s`    


%&$)r   )r   unittestnumpyr   numpy.testingr   pytestr   r   skimage._shared.testingr   skimage.morphologyr   r5   r   r   TestCaser   r   r   r   r   <module>   s      