o
    ji                     @   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   f/var/www/html/Deteccion_Ine/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   Z_add_constant_clipr   epsZ_subtract_constant_clipint16)selfdataZimg_constant_addedexpectederrorZimg_constant_subtractedr   r   r   test_saturated_arithmetic   s0   



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(   r<   r;   
r;   r<   r=      r?   r?   r?   r?   r=   r<   
r<   r=   (   rA      rB   rB   <   rC   r=   
r(   r?   rA   rA      rE   rE   rC   rC   r?   
r(   r?   rB   rE   rE   rE   rE   rE   rB   r?   
r=   r?   rB   rE   rE      rE   rE   rB   r?   
r(   r?   P   rJ   rE   rE   rE   r   r   r?   
r<   r=   rJ   rJ   rB   rB   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   r    r    r   rA   N)r   r1   r2   uint64int8int64astyper   h_maximar   r3   r5   r6   expected_resultr	   outr8   r   r   r   test_h_maxima6   sD   

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:   r>   r@   rD   rF   rG   rI   rK   r   r   rL   rM   rN   rA   N)r   r1   r2   rO   rP   rQ   rR   r   h_minimar   r3   r	   rT   r   r   r   test_h_minima_   sH   

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?r^   333333?r^   r^   r]   r\   
r\   r]   r_   {Gz?ra   ra   ra   ra   r_   r]   
r]   r_   皙?rc   
ףp=
?rd   rd   333333?re   r_   
r^   ra   rc   rc   RQ?rg   rg   re   re   ra   
r^   ra   rd   rg   rg   rg   rg   rg   rd   ra   
r_   gK?rd   rg   g&1?皙?rg   rg   rd   ra   
r^   ra   皙?rl   rg   rg   rg         ?rm   ra   
r]   r_   rl   rl   rd   rd   rd   rm   rm   r_   r   rm   g~jth?rL   rN   )
r   r   r   r   r    r   r   r   r   r   N)	r   r1   float32r   rS   r2   r   r3   rX   )r5   r6   Zinverted_datarV   rU   r8   r   r   r   test_extrema_float   sF   

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   rH   rj   r
   rA   r   rC   r)   r$   rJ   r   rm   gfffff3@r   gư>gMbP?g{Gz?r[   r[   N)	r   mgridrR   ro   
zeros_liker   rS   r   r3   	r5   wxyr6   rU   hrV   r8   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   rm          @r   r   r    (possible precision loss converting imageNr
   r
   )r   r1   r2   linspacer   r   rS   )r5   r6   h_valsfailuresrx   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   r<   r<   r<   r   )r   r<   r(   r<   r   r   r   r         @N)r   r1   r2   r   rS   r   ro   r5   r6   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      rj   r
      r      r)   r$   x   r   rm   g33333f@rq   N)	r   rr   rR   ro   rs   r   rX   r   r3   rt   r   r   r   test_h_minima_float_image  rz   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   rm   r|   r   r   r    r}   Nr~   )r   r1   r2   r   r   r   rX   )r5   r6   r   r   rx   r   Z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   r1   r2   r   rX   r   ro   r   r   r   r   test_h_minima_large_h3  r   z!TestExtrema.test_h_minima_large_hN)__name__
__module____qualname__r9   rW   rY   rp   ry   r   r   r   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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+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   r1   sizer	   boolshape
isinstancetuplelenintp)r5   resultr   r   r   
test_empty  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   imagerR   r	   r   r   expected_default)r5   r	   r   r   r   r   test_dtypes  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:   r>   r@   rD   rF   rG   rI   rK   r   rL   rN   )
r   r   r   r   r   r    r   r   r   r   N)
r   r1   r2   r   r   rR   r   r   r	   r   )r5   r6   r7   r	   r   r   r   r   r   test_dtypes_old  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   )r5   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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   r1   r   r   r   r   r	   r   r   onesr   r2   r   )r5   Zfootprint_crossZresult_footprint_crossr   Zresult_footprint_squareZfootprint_xZexpected_footprint_xZresult_footprint_xr   r   r   test_footprint  s<     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   )r5   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   r1   )r5   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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    r0   r0   r
   r)   2   r0   r#   )r)   r)   r)   N)	r   r1   r   r   r   r	   r   zerosr2   )r5   Zx_1dZexpected_1dZ	result_1dZx_3dZexpected_3dZ	result_3dr   r   r   test_nd)  s2   

"
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)   r0   *   r   N)r   fullr2   r   r   rR   r   r   r	   r   r   local_minima)r5   Zconst_imager7   r	   r   r   r   r   test_constantL  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.rZ   r`   rb   rf   rh   ri   rk   rn   r   rm   rL   rN   )
r   r    r   r   r    r   r   r   r   r   N)	r   r1   ro   r   r   r   r	   r   r   )r5   r   Zinverted_imagerU   r   r   r   r   rp   [  sF   


z"TestLocalMaxima.test_extrema_floatc                 C   s\   t g dg dg dg dg}t|}t g dg dg dg dg}t|| d S )N)Ԕdn2;׭;r   r   )r   R;r   r   )FFFF)FTTF)r   r1   r   r   r   )r5   r   r   rU   r   r   r   test_extrema_small_float  s    
		z(TestLocalMaxima.test_extrema_small_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emptyZfloat16)r5   r   r   r   test_exceptions  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   r1   r   r   r   UserWarningr   r	   r   r   r   )r5   Zwarning_msgrv   r   r   r   r   test_small_array  s    z TestLocalMaxima.test_small_arrayN)"r   r   r   __doc__r   r2   Zuint16Zuint32rO   rP   r4   Zint32rQ   ro   r   r   r1   r   r   r   r   r   r   r   r   r   r   r   r   r   rp   r   r   r   r   r   r   r   r   R  sp    )'#-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      >