a
    Žù»jÌ=  ã                   @   sÆ  d 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	Z	ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZmZmZ d
d„ Zdd„ Zdd„ ZG dd„ deƒZ G dd„ deƒZ!dddddddddddgddgddiddiddidej"dgj#e$dƒe$dƒdddœddddid œgZ%ed!e%ƒed"e%ƒd#d$„ ƒƒZ&d%d&„ Z'ed'd(�ed)d*„ ƒƒZ(d+d,„ Z)d-d.„ Z*eed/d0d1gƒd2d3„ ƒƒZ+ed4d5„ ƒZ,eed6d7d8gƒd9d:„ ƒƒZ-eeej.d;kd<d=�d>d?„ ƒƒZ/d@dA„ Z0dBdC„ Z1edDdE„ ƒZ2edFdG„ ƒZ3dHdI„ Z4dJdK„ Z5dLdM„ Z6dNdO„ Z7edPdQ„ ƒZ8edRdSdTdUdVdW„ e9dXƒD ƒdYfdZg d[¢d\œd]fgƒd^d_„ ƒZ:ed`da„ ƒZ;edbdc„ ƒZ<eddde„ ƒZ=edfdg„ ƒZ>dhdi„ Z?djdk„ Z@dS )lz
Test the hashing module.
é    N)ÚProcessPoolExecutor)ÚDecimal)Úfilter_args©Úhash)ÚMemory)ÚnpÚ
with_numpy)ÚfixtureÚparametrizeÚraisesÚskipifc                 C   s   | S ©N© )Úsr   r   ú>E:\soccer\ml_env\Lib\site-packages\joblib/test/test_hashing.pyÚunicode   s    r   c                 G   s>   t ƒ }tdƒD ]&}t ¡ }| |Ž  | t ¡ | ¡ qt|ƒS )zTime function func on *args.é   )ÚlistÚrangeÚtimeÚappendÚmin)ÚfuncÚargsÚtimesÚ_Út1r   r   r   Ú	time_func"   s    r   c                 G   s<   t | g|¢R Ž }t |g|¢R Ž }dt|| ƒ||   }|S )zKReturn the relative time between func1 and func2 applied on
    *args.
    ç      à?)r   Úabs)Zfunc1Zfunc2r   Z
time_func1Z
time_func2Úrelative_diffr   r   r   Úrelative_time,   s    r"   c                   @   s   e Zd Zdd„ ZdS )ÚKlassc                 C   s   |S r   r   ©ÚselfÚxr   r   r   Úf7   s    zKlass.fN)Ú__name__Ú
__module__Ú__qualname__r'   r   r   r   r   r#   6   s   r#   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚKlassWithCachedMethodc                 C   s   t |d�}| | j¡| _d S )N)Úlocation)r   Úcacher'   )r%   ZcachedirZmemr   r   r   Ú__init__<   s    
zKlassWithCachedMethod.__init__c                 C   s   |S r   r   r$   r   r   r   r'   @   s    zKlassWithCachedMethod.fN)r(   r)   r*   r.   r'   r   r   r   r   r+   ;   s   r+   é   é   g      ð?ç       @y      ð?      ð?y       @      ð?ÚaÚb©r/   )r/   r/   ©r2   r/   )r2   r/   r5   )r2   r/   ÚdÚobj1Úobj2c                 C   s(   t | ƒt |ƒk}| |u }||ks$J ‚dS )z!Smoke test hash on various types.Nr   )r7   r8   Úare_hashes_equalZare_objs_identicalr   r   r   Útest_trivial_hashm   s    r:   c                  C   s^   t  tdƒ¡} t| jƒt| jƒks&J ‚t tdƒ¡}t tdƒ¡}t|jƒt|jƒksZJ ‚d S )Nr2   é
   é	   )	ÚioÚStringIOr   r   ÚflushÚcollectionsÚdequer   Úextend)r2   Za1Za2r   r   r   Útest_hash_methodsw   s
    rC   Úfunction)Úscopec                  C   s@   t j d¡} |  d¡}| ¡ }| ¡ }|d  d7  < |||fS )Nr   ©r;   r;   r/   )r   ÚrandomÚRandomStateÚrandom_sampleÚcopy)ÚrndÚarr1Úarr2Úarr3r   r   r   Úthree_np_arrays€   s    
rO   c                 C   sf   | \}}}t j| dd�D ]2\}}t|ƒt|ƒk}t ||k¡}||ksJ ‚qt|ƒt|jƒksbJ ‚d S )Nr0   )Úrepeat)Ú	itertoolsÚproductr   r   ÚallÚT)rO   rL   rM   rN   r7   r8   r9   Zare_arrays_equalr   r   r   Útest_hash_numpy_arrays‹   s    
rU   c                 C   sT   | \}}}||dœ}||dœ}||dœ}t |ƒt |ƒks<J ‚t |ƒt |ƒksPJ ‚d S )N)r/   r0   r   )rO   rL   rM   rN   Zd1Zd2Zd3r   r   r   Útest_hash_numpy_dict_of_arrays–   s    



rV   Údtypezdatetime64[s]ztimedelta64[D]c                 C   s2   t t d¡ƒ}tjdd| d�}t |ƒ|ks.J ‚d S )Nr;   r   ©rW   )r   r   Úarange)rW   Za_hashÚarrayr   r   r   Útest_numpy_datetime_array¡   s    r[   c                  C   sp   t jt  d¡ d¡dd�d d …d d…d d …f } t  | ¡}t| ƒt|ƒksNJ ‚t  | ¡}t| ƒt|ƒkslJ ‚d S )Nip  )éè  r0   r   ÚF)Úorderr/   )r   ÚasarrayrY   ÚreshapeZascontiguousarrayr   Úasfortranarray)r2   r3   Úcr   r   r   Útest_hash_numpy_noncontiguous«   s
    0

rc   Úcoerce_mmapTFc                 C   s~   |   d¡j}zVtj|ddd�}t |¡}t||d�t||d�k}||ksLJ ‚W dtƒ v rz~t ¡  ndtƒ v rx~t ¡  0 dS )zECheck that memmap and arrays hash identically if coerce_mmap is True.Zmemmap_temprF   zw+)ÚshapeÚmode)rd   ÚmN)	ÚjoinÚstrpathr   Zmemmapr_   r   ÚlocalsÚgcÚcollect)Útmpdirrd   Úfilenamerg   r2   r9   r   r   r   Útest_hash_memmapµ   s    
ÿ

û
ro   Úwin32z5This test is not stable under windows for some reason)Úreasonc                  C   s|   t j d¡} |  d¡}dd„ }t|t|ƒ}|dk s6J ‚dt||ƒ }tt|||fƒ}dt|| ƒ||   }|dk sxJ ‚dS )	aî  Check the performance of hashing numpy arrays:

    In [22]: a = np.random.random(1000000)

    In [23]: %timeit hashlib.md5(a).hexdigest()
    100 loops, best of 3: 20.7 ms per loop

    In [24]: %timeit hashlib.md5(pickle.dumps(a, protocol=2)).hexdigest()
    1 loops, best of 3: 73.1 ms per loop

    In [25]: %timeit hashlib.md5(cPickle.dumps(a, protocol=2)).hexdigest()
    10 loops, best of 3: 53.9 ms per loop

    In [26]: %timeit hash(a)
    100 loops, best of 3: 20.8 ms per loop
    r   i@B c                 S   s   t  t| ƒ¡ ¡ S r   )ÚhashlibÚmd5Ú
memoryviewÚ	hexdigest)r&   r   r   r   Úmd5_hashã   s    z-test_hash_numpy_performance.<locals>.md5_hashg333333Ó?r   r   N)r   rG   rH   rI   r"   r   r   r    )rK   r2   rv   r!   Ztime_hashlibZ	time_hashr   r   r   Útest_hash_numpy_performanceÊ   s    
rw   c                  C   s8   t ƒ } t ƒ }tt| jg dƒƒtt|jg dƒƒks4J ‚dS )z}Make sure that calling the same method on two different instances
    of the same class does resolve to the same hashes.
    r4   N)r#   r   r   r'   ©r2   r3   r   r   r   Útest_bound_methods_hashñ   s    ry   c                 C   sD   t | jƒ}t | jƒ}tt|jjg dƒƒtt|jjg dƒƒks@J ‚dS )z†Make sure that calling the same _cached_ method on two different
    instances of the same class does resolve to the same hashes.
    r4   N)r+   ri   r   r   r'   r   )rm   r2   r3   r   r   r   Útest_bound_cached_methods_hashú   s
    

ÿrz   c                  C   sP   t jdd„ tdƒD ƒtd�} t jdd„ tdƒD ƒtd�}t| ƒt|ƒksLJ ‚dS )z;Make sure that ndarrays with dtype `object' hash correctly.c                 S   s   g | ]}t  |¡‘qS r   ©r   rY   ©Ú.0Úir   r   r   Ú
<listcomp>	  ó    z*test_hash_object_dtype.<locals>.<listcomp>é   rX   c                 S   s   g | ]}t  |¡‘qS r   r{   r|   r   r   r   r   
  r€   N)r   rZ   r   Úobjectr   rx   r   r   r   Útest_hash_object_dtype  s    rƒ   c                  C   s,   t  d¡} t  d¡}t| ƒt|ƒks(J ‚d S )Nr1   g      @)r   Úfloat64r   rx   r   r   r   Útest_numpy_scalar  s    

r…   c                 C   sp   t | jƒ}dgdgdgdgdgdgdgdgdgdgdgdgdgdœ}| |¡}| |¡}t|ƒt|ƒkslJ ‚d S )Né!   ©z#s12069__c_maps.nii.gzz#s12158__c_maps.nii.gzz#s12258__c_maps.nii.gzz#s12277__c_maps.nii.gzz#s12300__c_maps.nii.gzz#s12401__c_maps.nii.gzz#s12430__c_maps.nii.gzz#s13817__c_maps.nii.gzz#s13903__c_maps.nii.gzz#s13916__c_maps.nii.gzz#s13981__c_maps.nii.gzz#s13982__c_maps.nii.gzz#s13983__c_maps.nii.gz)r+   ri   r'   r   )rm   Úkr6   r2   r3   r   r   r   Útest_dict_hash  s$    
ó

r‰   c                 C   sB   t | jƒ}tg d¢ƒ}| |¡}| |¡}t|ƒt|ƒks>J ‚d S )Nr‡   )r+   ri   Úsetr'   r   )rm   rˆ   r   r2   r3   r   r   r   Útest_set_hash3  s    
ÿ

r‹   c                   C   s8   t ttdƒtdƒgƒƒt ttdƒtdƒgƒƒks4J ‚d S )Nr   ÚNaN)r   rŠ   r   r   r   r   r   Útest_set_decimal_hashP  s    ÿr�   c                  C   sD   d} | di}| di}t  t  |¡¡}t||gƒt||gƒks@J ‚d S )NÚfooÚbar)ÚpickleÚloadsÚdumpsr   )Ústringr2   r3   rb   r   r   r   Útest_stringX  s
    r”   c                  C   s`  t  d¡} t  d¡}| |u s J ‚t| ƒt|ƒks4J ‚t t | ¡¡}| |usPJ ‚t| ƒt|ƒksdJ ‚t| | gƒt||gƒks€J ‚t| | gƒt| |gƒksœJ ‚t  dt jdfdt jdfg¡}t  dt jdfdt jdfg¡}t|ƒt|ƒksìJ ‚t t |¡¡}||u�s
J ‚t|ƒt|ƒk�s J ‚t||gƒt||gƒk�s>J ‚t||gƒt||gƒk�s\J ‚d S )NZf4Únameé   Zgrades)r0   )r   rW   r   r�   r‘   r’   Zstr_r„   )Zdt1Zdt2Zdt1_roundtrippedZcomplex_dt1Zcomplex_dt2Zcomplex_dt1_roundtrippedr   r   r   Útest_numpy_dtype_picklingb  s*    	

ÿÿr—   zto_hash,expected)zThis is a string to hashZ 71b3f47df22cb19431d85d92d0b230b2)u   C'est lÃ©tÃ©Z 2d8d189e9b2b0b2e384d93c868c0e576))i@â i1Ô  i3~þÿZ e205227dd82250871fa25aa0ec690aa3c                 C   s   g | ]}t  d ¡  ¡ ‘qS )é*   )rG   ÚRandom©r}   r   r   r   r   r   ’  r€   r   é   Z a11ffad81f9682a7d901e6edc3d16c84é{   )iñØÿÿr0   r   )ZabcdeZsadfasZ aeda150553d4bb5c69f0e69d51b0e2efc                 C   s   t | ƒ|ksJ ‚d S r   r   )Zto_hashÚexpectedr   r   r   Útest_hashes_stay_the_same‹  s    rž   c                  C   s8   t j d¡} |  d¡}t  |¡}t|ƒt|ƒks4J ‚d S )Nr   rF   )r   rG   rH   rI   ra   r   )ÚrngZarr_cZarr_fr   r   r   ÚAtest_hashes_are_different_between_c_and_fortran_contiguous_arrays¡  s    

r    c                   C   s   t t d¡ƒ d S ©Nr   ©r   r   rZ   r   r   r   r   Útest_0d_array«  s    r£   c                   C   s&   t t d¡ƒt t dg¡ƒks"J ‚d S r¡   r¢   r   r   r   r   Ú)test_0d_and_1d_array_hashing_is_different°  s    r¤   c            
      C   s°   dd„ } | ƒ }| ƒ }t dd�}t dd�}zpt||ƒD ]P\}}| t|¡ ¡ }| t|¡ ¡ }||kshJ ‚| t|¡ ¡ }	||	ks4J ‚q4W | ¡  | ¡  n| ¡  | ¡  0 d S )Nc                     sÖ   t j d¡‰ ˆ jdddd� d¡t‡ fdd„td	ƒD ƒƒ‡ fd
d„td	ƒD ƒˆ  dd	¡ d¡ˆ jddd� d¡ˆ  d¡ d¡gdœt jddd� 	d¡t  
t jddd� 	d¡¡t jddd� 	d¡d d …d d…f g} | S )Nr˜   iüÿÿr\   é2   )ÚhighÚsizez<i8c                 3   s   | ]}ˆ   d ¡ d¡V  qdS )r   ú<f4N©ÚrandnÚastyperš   ©rŸ   r   r   Ú	<genexpr>Ï  r€   z_test_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hash.<locals>.<genexpr>r›   c                    s   g | ]}ˆ   d ¡ d¡‘qS )r   r¨   r©   rš   r¬   r   r   r   Ð  r€   z`test_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hash.<locals>.<listcomp>r   r¨   r;   é   )r§   )iûòÿÿr   éd   rX   rF   r0   )r   rG   rH   Úrandintr«   Útupler   rª   rY   r`   ra   )Zto_hash_listr   r¬   r   Úcreate_objects_to_hashÆ  s    þþ	"ïzLtest_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hashr/   )Úmax_workers)r   ÚzipZsubmitr   ÚresultÚshutdown)
r²   Zto_hash_list_oneZto_hash_list_twoÚe1Úe2Zobj_1Zobj_2Zhash_1Zhash_2Zhash_3r   r   r   Ú,test_hashes_stay_the_same_with_numpy_objectsµ  s     


ÿr¹   c                  C   sH   dd„ } t tjƒ�}t| ƒ W d   ƒ n1 s00    Y  | d¡ d S )Nc                   S   s   dS )Nr˜   r   r   r   r   r   Únon_picklableü  s    z2test_hashing_pickling_error.<locals>.non_picklablezPicklingError while hashing)r   r�   ÚPicklingErrorr   Úmatch)rº   Úexcinfor   r   r   Útest_hashing_pickling_errorû  s    &r¾   c                  C   sH   d} t t| d��$ ddi}t|dd� W d   ƒ n1 s:0    Y  d S )Nz!Valid options for 'hash_name' are)r¼   rŽ   r�   Úinvalid)Ú	hash_name)r   Ú
ValueErrorr   )ÚmsgÚdatar   r   r   Útest_wrong_hash_name  s    rÄ   )AÚ__doc__r@   rk   rr   r=   rQ   r�   rG   Úsysr   Úconcurrent.futuresr   Údecimalr   Zjoblib.func_inspectr   Zjoblib.hashingr   Zjoblib.memoryr   Zjoblib.test.commonr   r	   Zjoblib.testingr
   r   r   r   r   r   r"   r‚   r#   r+   rl   r   rŠ   Z
input_listr:   rC   rO   rU   rV   r[   rc   ro   Úplatformrw   ry   rz   rƒ   r…   r‰   r‹   r�   r”   r—   r   rž   r    r£   r¤   r¹   r¾   rÄ   r   r   r   r   Ú<module>   sÒ   

ÿþÿÞ&		
	þ"	
	


(þøþ
	
	


E	