o
    e/                     @   s  d Z ddlmZmZmZ ddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ zddlmZ W n eyG   ddlZY nw zddlZW n ey[   ddlZY nw ddlZddlmZ ddlmZ G dd dejZd'dd	Zd
d Zdd Z dd Z!d(ddZ"d)ddZ#d*ddZ$dd Z%G dd de&Z'dd Z(G dd  d Z)G d!d" d"e)Z*G d#d$ d$e*Z+d%d& Z,dS )+z
Some utility functions that are only used for unittests.
Placing them in test/ directory seems to be against convention, so they are part of the library.

    )print_functiondivisionabsolute_importN)KeypointsOnImagec                       s    e Zd ZdZ fddZ  ZS )ArgCopyingMagicMockzA MagicMock that copies its call args/kwargs before storing the call.

    This is useful for imgaug as many augmentation methods change data
    in-place.

    Taken from https://stackoverflow.com/a/23264042/3760780

    c                    s*   t |}t |}tt| j|i |S N)copydeepcopysuperr   
_mock_call)selfargskwargsZ	args_copyZkwargs_copy	__class__ @D:\Projects\ConvertPro\env\Lib\site-packages\imgaug/testutils.pyr   +   s   


zArgCopyingMagicMock._mock_call)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r   !   s    	r   -C6?c           	      C   s  t | ts
t |tr5t | tsJ t |tsJ t| t|ks"J t| |D ]\}}t|||d q'd S t| t|ks?J t| jt|jksKJ | j|jksSJ t| j|jD ]\}}|j||dsgJ qZt |t	j
rt| j|jD ]\}}|jr|jsJ qud S d S )N)max_distance)
isinstancelistlenzipassert_cbaois_equaltypeitemsshapeZcoords_almost_equalsiaZPolygonsOnImageZis_valid)	Zobservedexpectedr   Z
observed_iZ
expected_iZitem_aZitem_bZitem_obsZitem_expr   r   r   r   3   s.   
r   c                 C   s   t jdd| t jS )Nr      )nprandomuniformastypeuint8)sizer   r   r   create_random_imagesI   s   r+   c           	      C   s   g }t | d D ]?}g }| d | d }}t |D ]}tjd|d }tjd|d }|tj||d q|tj|| dd  d q	|S )Nr         )xy)r!   )	smxranger%   r&   randintappendr"   ZKeypointr   )	Zsize_imagesZnb_keypoints_per_imgresult_Zkpsheightwidthr.   r/   r   r   r   create_random_keypointsM   s   r8   c                 C   sv   t | tsJ dt| f t |tsJ dt|f t| t|kr&dS t| |D ]\}}t||s8 dS q+dS )Nz)Expected list1 to be a list, got type %s.z)Expected list2 to be a list, got type %s.FT)r   r   r   r   r   r%   array_equal)Zlist1Zlist2Zarr1Zarr2r   r   r   array_equal_listsZ   s   r:   MbP?c                 C   s   t | trt |tsJ | g} |g}t| t|krdS t| |D ]Y\}}|j}|j}t|t|kr6 dS t||D ]>\}}t|j| t|j  koUt|j| kn  }	t|j| t|j  kont|j| kn  }
|	ru|
sy  dS q;q!dS )NFT)r   r   r   r   Z	keypointsfloatr.   r/   )Zkpsois1Zkpsois2ZepsZkpsoi1Zkpsoi2Zkps1Zkps2Zkp1Zkp2Zx_equalZy_equalr   r   r   keypoints_equalj   s6   



r=   c                 C   s$   t |  tj|  t|  d S r   )iarandomseedr%   r&   )r?   r   r   r   reseed   s   
r@      rB      rC   c                 C   sz   t t tt |dt j}||}t	tj
| dd}t |D ]}| |d}||d}t ||s:J q&d S )N   )protocol)image)r%   modZarangeintprodr(   r)   Zreshapepickleloadsdumpsr9   )Z	augmenterr!   Z
iterationsrG   Zaugmenter_pklr5   Z	image_augZimage_aug_pklr   r   r   runtest_pickleable_uint8_img   s   $


rN   c                    s~   g d}t  fdd|D s|  S tjdd}td |  }dt|d jv s,J |W  d	   S 1 s8w   Y  d	S )
zEHelper for tests of CBA shift() functions.

    Added in 0.4.0.

    )toprightbottomleftc                    s   g | ]}| v qS r   r   ).0Zkwnamer   r   r   
<listcomp>   s    z*wrap_shift_deprecation.<locals>.<listcomp>Trecordalwaysz.These are deprecated. Use `x` and `y` instead.rE   N)anywarningscatch_warningssimplefilterstrmessage)funcr   r   Zdeprecated_kwargsZcaught_warningsr4   r   rT   r   wrap_shift_deprecation   s   
$r`   c                   @   s*   e Zd ZdZdddZdd Zd	d
 ZdS )TemporaryDirectoryzCreate a context for a temporary directory.

    The directory is automatically removed at the end of the context.
    This context is available in ``tmpfile.TemporaryDirectory``, but only
    from 3.2+.

    Added in 0.4.0.

     tmpNc                 C   s   t |||| _d S r   )tempfilemkdtempname)r   suffixprefixdirr   r   r   __init__   s   zTemporaryDirectory.__init__c                 C   s   | j S r   )rf   )r   r   r   r   	__enter__   s   zTemporaryDirectory.__enter__c                 C   s   t | j d S r   )shutilrmtreerf   )r   exc_typeexc_valexc_tbr   r   r   __exit__   s   zTemporaryDirectory.__exit__)rb   rc   N)r   r   r   r   rj   rk   rq   r   r   r   r   ra      s
    

ra   c                    s4   t | trt fdd| D S t | tot|  S )Nc                 3   s    | ]}t | V  qd S r   )_is_subtype)rS   ebasetyper   r   	<genexpr>   s    z_is_subtype.<locals>.<genexpr>)r   tupleallr   
issubclass)r#   ru   r   rt   r   rr      s   
rr   c                   @   s   e Zd Zdd Zdd ZdS )_BaseTestCaseContextc                 C   s
   || _ d S r   )	test_case)r   r{   r   r   r   rj      s   
z_BaseTestCaseContext.__init__c                 C   s   | j | j|}| j |r   )r{   Z_formatMessagemsgZfailureException)r   ZstandardMsgr|   r   r   r   _raiseFailure   s   z"_BaseTestCaseContext._raiseFailureN)r   r   r   rj   r}   r   r   r   r   rz      s    rz   c                   @   s   e Zd ZdddZdd ZdS )_AssertRaisesBaseContextNc                 C   s@   t | | || _|| _|d urt|}|| _d | _d | _d S r   )	rz   rj   r#   r{   recompileexpected_regexobj_namer|   )r   r#   r{   r   r   r   r   rj      s   

z!_AssertRaisesBaseContext.__init__c                 C   s   zmt | j| jstd|| jf |s,|dd| _|r'tdtt|f | W d} S |d }|dd }z|j	| _
W n tyJ   t|| _
Y nw |  ||i | W d   n1 s_w   Y  W d} dS W d} dS d} w )z
        If args is empty, assertRaises/Warns is being used as a
        context manager, so check for a 'msg' kwarg and return self.
        If args is not empty, call a callable passing positional and keyword
        arguments.
        z%s() arg 1 must be %sr|   Nz3%r is an invalid keyword argument for this functionr   r,   )rr   r#   
_base_type	TypeError_base_type_strpopr|   nextiterr   r   AttributeErrorr]   )r   rf   r   r   Zcallable_objr   r   r   handle   s4   z_AssertRaisesBaseContext.handler   )r   r   r   rj   r   r   r   r   r   r~      s    
r~   c                   @   s(   e Zd ZdZeZdZdd Zdd ZdS )_AssertWarnsContextzBA context manager used to implement TestCase.assertWarns* methods.z(a warning type or tuple of warning typesc                 C   sN   t j D ]}t|dd ri |_qtjdd| _| j | _t	d| j
 | S )N__warningregistry__TrV   rX   )sysmodulesvaluesgetattrr   rZ   r[   warnings_managerrk   r\   r#   )r   vr   r   r   rk   %  s   z_AssertWarnsContext.__enter__c                 C   s   | j ||| |d urd S z| jj}W n ty"   t| j}Y nw d }| jD ]-}|j}t|| js4q(|d u r:|}| j	d urH| j	
t|sHq(|| _|j| _|j| _ d S |d urg| d| j	jt| | jrv| d|| j d S | d| d S )Nz"{}" does not match "{}"z{} not triggered by {}z{} not triggered)r   rq   r#   r   r   r]   rZ   r^   r   r   searchwarningfilenamelinenor}   formatpatternr   )r   rn   	exc_valuetbZexc_nameZfirst_matchingmwr   r   r   rq   2  s@   



z_AssertWarnsContext.__exit__N)	r   r   r   r   Warningr   r   rk   rq   r   r   r   r   r     s    r   c                 O   s   t || }|d||S )zContext with same functionality as ``assertWarns`` in ``unittest``.

    Note that ``assertWarns`` is only available in python 3.2+.

    Added in 0.4.0.

    assertWarns)r   r   )ZtestcaseZexpected_warningr   r   contextr   r   r   r   Z  s   
	r   )r   )r;   )r   )rA   rC   )-r   
__future__r   r   r   r&   r   rZ   rd   rl   r   r   numpyr%   Z	six.movesmovesr0   Zunittest.mockZmockImportErrorcPicklerK   Zimgaugr"   Zimgaug.randomr>   Zimgaug.augmentables.kpsr   Z	MagicMockr   r   r+   r8   r:   r=   r@   rN   r`   objectra   rr   rz   r~   r   r   r   r   r   r   <module>   sN    



5<