U
    ?h                     @   s   d dl Zd dlmZmZ d dlmZmZ d dlm	Z	 d dl
mZmZ d dlmZ edejejg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 )    N)assert_almost_equalassert_equal)dataimg_as_float)diamond)match_templatepeak_local_max)testingdtypec                 C   sH  d}t jdd| d}dt |t |d d d   }|j| dd}d	d
g}|D ]$\}}||||| ||| f< qPt jd |dt jjddj| dd 7 }t||}|j| kst	d}t
||d}	t|	dkr
|t|	j }
t |
d d d }|	| d d }	|	t |	d d df  }	t||	D ]\}}t|| q.d S )Nd   )  r         ?)r
   g?F)copy)2   r   )   r      size   )Zmin_distance   r   )npfullZtriZastyperandomseeduniformr   r
   AssertionErrorr   lentupleTargsortzipr   )r
   r   imagetargetZtarget_positionsxyresultdeltaZ	positionsZintensitiesZ	i_maxsortZ	xy_targetZxy r(   U/var/www/html/venv/lib/python3.8/site-packages/skimage/feature/tests/test_template.pytest_template
   s(    " 
r*   c                  C   sJ  d} d}d\}}d\}}t ||fd}d||||  |||  f< d||||  |||  f< t | d | d f}d|dd|  dd|  f< t||}t |j}	|	d }
|	d	 }t |
|j}t ||j}t t 	|d ||fkst
t t 	|d ||fkst
t |j|
 d	s.t
t |j| dsFt
d
S )a  Test that `match_template` gives the correct normalization.

    Normalization gives 1 for a perfect match and -1 for an inverted-match.
    This test adds positive and negative squares to a zero-array and matches
    the array with a positive template.
    r      )r      )      r   r   r   r   r   N)r   r   zerosr   r    flatunravel_indexshapeallarrayr   Zallclose)nNZiposZjposZinegZjnegr"   templater&   sorted_resultZ	iflat_minZ	iflat_maxZ
min_resultZ
max_resultr(   r(   r)   test_normalization*   s&    
 "r9   c                  C   sb   t jd ddt jjdd  } t d}d|dd	ddf< t| |}t t |r^tdS )
a  Test that `match_template` doesn't return NaN values.

    When image values are only slightly different, floating-point errors can
    cause a subtraction inside of a square root to go negative (without an
    explicit check that was added to `match_template`).
    r   r   g&.>)r+   r+   r   )   r:   r   Nr,   )	r   r   r   normalonesr   anyisnanr   r"   r7   r&   r(   r(   r)   test_no_nansN   s    

r@   c               	   C   s8   t d} t d}tt t||  W 5 Q R X d S )Nr   r   r,   r,   r   r<   r	   Zraises
ValueErrorr   r"   r7   r(   r(   r)   test_switched_arguments]   s    

rF   c                  C   s  dt d } dtd }tdd}||ddf  | ddddf 8  < ||dd	f  | 7  < ||d
df  | 8  < ||ddf  | ddddf 7  < t|| d| d}t|j}t|dd |j	\}}t
|d t|dd |j	\}}t
|d dS )ae  Test `match_template` when `pad_input=True`.

    This test places two full templates (one with values lower than the image
    mean, the other higher) and two half templates, which are on the edges of
    the image. The two full templates should score the top (positive and
    negative) matches and the centers of the half templates should score 2nd.
    r   r   )	         Nr,      rG   iT)	pad_inputZconstant_values)r-   r   )   r:   )r   r   r<   slicer   Zmeanr    r0   r1   r2   r   )r7   r"   midr&   r8   ijr(   r(   r)   test_pad_inputd   s    	
((
rT   c                  C   sr   t jd t jddd} t d}| |ddddddf< t|| }t|jd	 tt |	 |jd
 d S )Nr   r,   r-   r-   r-   r:   r      rK   rI   )
   rW   rW   )r,   r   rK   
r   r   r   Zrandr/   r   r   r2   r1   argmaxr7   r"   r&   r(   r(   r)   test_3d   s    

r[   c                  C   sv   t jd t jddd} t d}| |ddddddf< t|| d	d
}t|jd tt |	 |jd d S )Nr   r,   rU   r:   r   rV   rK   rI   T)rM   )rK   r:   r   rX   rZ   r(   r(   r)   test_3d_pad_input   s    
r\   c                  C   sb   t d} td}| d d dd f |ddd df< t|| ddd}tt| |jd	 d S )
Nr   )rW   rW   rJ   rI   r,   TZreflect)rM   mode)rK   r   )r   r   r/   r   r   r1   rY   r2   rZ   r(   r(   r)   test_padding_reflect   s    
$r^   c               	   C   s   t d} t d}tt t||  W 5 Q R X t d} t d}tt t||  W 5 Q R X t d} t d}tt t||  W 5 Q R X d S )N)r   r   r   rB   rA   )r,   r,   r   )r   r   r,   r,   rC   rE   r(   r(   r)   test_wrong_input   s    





r_   c                  C   sX   t t } td}d|d< t| |}t|  | dk sDt|	 dksTtd S )NrB   r   )r   r   g  ?g  )
r   r   pager   r/   r   printmaxr   minr?   r(   r(   r)   test_bounding_values   s    

rd   )numpyr   Zskimage._shared.testingr   r   Zskimager   r   Zskimage.morphologyr   Zskimage.featurer   r   Zskimage._sharedr	   ZparametrizeZfloat32Zfloat64r*   r9   r@   rF   rT   r[   r\   r^   r_   rd   r(   r(   r(   r)   <module>   s    
$