
    .Ph=                     >   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
 Zd Zd Z G d de          Z  G 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                          Z&d! Z' ed"#          ed$                         Z(d% Z)d& Z*e ed'd(d)g          d*                         Z+ed+             Z,e ed,d-d.g          d/                         Z-e eej.        d0k    d12          d3                         Z/d4 Z0d5 Z1ed6             Z2ed7             Z3d8 Z4d9 Z5d: Z6d; Z7ed<             Z8 ed=d>d?d@dA  e9dB          D             dCfdDg dEdFdGfg          dH             Z:edI             Z;edJ             Z<edK             Z=edL             Z>dM Z?dN Z@dS )Oz
Test the hashing module.
    N)ProcessPoolExecutor)Decimal)filter_argshash)Memory)np
with_numpy)fixtureparametrizeraisesskipifc                     | S N )ss    X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/joblib/test/test_hashing.pyunicoder      s    H    c                     t                      }t          d          D ]C}t          j                    } | |  |                    t          j                    |z
             Dt	          |          S )zTime function func on *args.   )listrangetimeappendmin)funcargstimes_t1s        r   	time_funcr"   "   sb    FFE1XX ' 'Y[[dTY[[2%&&&&u::r   c                 p    t          | g|R  }t          |g|R  }dt          ||z
            ||z   z  z  }|S )zKReturn the relative time between func1 and func2 applied on
    *args.
          ?)r"   abs)func1func2r   
time_func1
time_func2relative_diffs         r   relative_timer+   ,   sU     5(4(((J5(4(((J3zJ677:
;RSTMr   c                       e Zd Zd ZdS )Klassc                     |S r   r   selfxs     r   fzKlass.f7       r   N)__name__
__module____qualname__r2   r   r   r   r-   r-   6   s#            r   r-   c                       e Zd Zd Zd ZdS )KlassWithCachedMethodc                 d    t          |          }|                    | j                  | _        d S )N)location)r   cacher2   )r0   cachedirmems      r   __init__zKlassWithCachedMethod.__init__<   s+    h'''46""r   c                     |S r   r   r/   s     r   r2   zKlassWithCachedMethod.f@   r3   r   N)r4   r5   r6   r>   r2   r   r   r   r8   r8   ;   s2        # # #    r   r8         g      ?       @y      ?      ?y       @      ?abr@   )r@   r@   rC   r@   )rC   r@   rF   )rC   r@   dobj1obj2c                 ^    t          |           t          |          k    }| |u }||k    sJ dS )z!Smoke test hash on various types.Nr   )rH   rI   are_hashes_equalare_objs_identicals       r   test_trivial_hashrM   m   s=    
 DzzT$ZZ/1111111r   c                  |   t          j        t          d                    } t          | j                  t          | j                  k    sJ t          j        t          d                    }t          j        t          d                    }t          |j                  t          |j                  k    sJ d S )NrC   
   	   )	ioStringIOr   r   flushcollectionsdequer   extend)rC   a1a2s      r   test_hash_methodsrY   w   s    
GCLL!!A==DMM))))		599	%	%B		588	$	$B	??d29oo------r   function)scopec                      t           j                            d          } |                     d          }|                                }|                                }|dxx         dz  cc<   |||fS )Nr   rO   rO   r@   )r	   randomRandomStaterandom_samplecopy)rndarr1arr2arr3s       r   three_np_arraysrf      sh     )


"
"CX&&D99;;D99;;DGGGqLGGGtr   c                    | \  }}}t          j        | d          D ]E\  }}t          |          t          |          k    }t          j        ||k              }||k    sJ Ft          |          t          |j                  k    sJ d S )NrA   )repeat)	itertoolsproductr   r	   allT)rf   rc   rd   re   rH   rI   rK   are_arrays_equals           r   test_hash_numpy_arraysrn      s    &D$'BBB 4 4
d::d36$$,//#333333::df%%%%%%r   c                     | \  }}}||d}||d}||d}t          |          t          |          k    sJ t          |          t          |          k    sJ d S )N)r@   rA   r   )rf   rc   rd   re   d1d2d3s          r   test_hash_numpy_dict_of_arraysrs      su    &D$d		Bd		Bd		B88tBxx88tBxxr   dtypezdatetime64[s]ztimedelta64[D]c                     t          t          j        d                    }t          j        dd|           }t          |          |k    sJ d S )NrO   r   rt   )r   r	   arange)rt   a_hasharrays      r   test_numpy_datetime_arrayrz      sL    
 ")B--  FIa5)))E;;&      r   c                  r   t          j        t          j        d                              d          d          d d d dd d f         } t          j        |           }t          |           t          |          k    sJ t          j        |           }t          |           t          |          k    sJ d S )Nip  )  rA   r   F)orderr@   )r	   asarrayrw   reshapeascontiguousarrayr   asfortranarray)rC   rD   cs      r   test_hash_numpy_noncontiguousr      s    

29T??**<88DDDQQQAAAXNA
QA77d1gg
!A77d1ggr   coerce_mmapTFc                    |                      d          j        }	 t          j        |dd          }t          j        |          }t          ||          t          ||          k    }||k    sJ 	 dt                      v r~t          j                     dS dS # dt                      v r~t          j                     w w xY w)zECheck that memmap and arrays hash identically if coerce_mmap is True.memmap_tempr]   zw+)shapemode)r   mN)	joinstrpathr	   memmapr   r   localsgccollect)tmpdirr   filenamer   rC   rK   s         r   test_hash_memmapr      s     {{=))1HIhhT:::JqMM{;;;t;@
 @
 @
 
  ;.....&((?? JLLLLL ?3&((?? JLLLL s   AB 'Cwin32z5This test is not stable under windows for some reason)reasonc                  L   t           j                            d          } |                     d          }d }t	          |t
          |          }|dk     sJ dt          ||          z  }t          t
          |||f          }dt          ||z
            ||z   z  z  }|dk     sJ 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                 h    t          j        t          |                                                     S r   )hashlibmd5
memoryview	hexdigest)r1   s    r   md5_hashz-test_hash_numpy_performance.<locals>.md5_hash   s$    {:a==))33555r   g333333?r   r$   N)r	   r^   r_   r`   r+   r   r"   r%   )rb   rC   r   r*   time_hashlib	time_hashs         r   test_hash_numpy_performancer      s    , )


"
"C'""A6 6 6 "(D!44M3 y1---L$Aq	**I3y<788I<TUVM3r   c                      t                      } t                      }t          t          | j        g d                    t          t          |j        g d                    k    sJ dS )z}Make sure that calling the same method on two different instances
    of the same class does resolve to the same hashes.
    rE   N)r-   r   r   r2   rC   rD   s     r   test_bound_methods_hashr      sY     	AAACT**++tKR4N4N/O/OOOOOOOr   c                     t          | j                  }t          | j                  }t          t          |j        j        g d                    t          t          |j        j        g d                    k    sJ dS )zMake sure that calling the same _cached_ method on two different
    instances of the same class does resolve to the same hashes.
    rE   N)r8   r   r   r   r2   r   )r   rC   rD   s      r   test_bound_cached_methods_hashr      sz     	fn--Afn--AACHb$//00DACHb$''5 5      r   c                     t          j        d t          d          D             t                    } t          j        d t          d          D             t                    }t	          |           t	          |          k    sJ dS )z;Make sure that ndarrays with dtype `object' hash correctly.c                 6    g | ]}t          j        |          S r   r	   rw   .0is     r   
<listcomp>z*test_hash_object_dtype.<locals>.<listcomp>	       1111")A,,111r      rv   c                 6    g | ]}t          j        |          S r   r   r   s     r   r   z*test_hash_object_dtype.<locals>.<listcomp>
  r   r   N)r	   ry   r   objectr   r   s     r   test_hash_object_dtyper     sy     	11a111@@@A
11a111@@@A77d1ggr   c                      t          j        d          } t          j        d          }t          |           t          |          k    sJ d S )NrB   g      @)r	   float64r   r   s     r   test_numpy_scalarr     sA     	
3A

3A77d1ggr   c                     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    sJ 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)r8   r   r2   r   )r   krG   rC   rD   s        r   test_dict_hashr     s     	fn--A $&$#%$#%$#%$#%$#%$#%$#%$#%$#%$#%$#%$#%$	 	A  	
AA	AA77d1ggr   c                     t          | j                  }t          g d          }|                    |          }|                    |          }t	          |          t	          |          k    sJ d S )Nr   )r8   r   setr2   r   )r   r   r   rC   rD   s        r   test_set_hashr   3  st     	fn--A	
 	
 	
	 	A$ 	
AA	AA77d1ggr   c            	          t          t          t          d          t          d          g                    t          t          t          d          t          d          g                    k    sJ d S )Nr   NaN)r   r   r   r   r   r   test_set_decimal_hashr   P  sn     WQZZ01122dWU^^WQZZ())7 7      r   c                      d} | di}| di}t          j        t          j        |                    }t          ||g          t          ||g          k    sJ d S )Nfoobar)pickleloadsdumpsr   )stringrC   rD   r   s       r   test_stringr   X  s]     F	A	AV\!__%%AA<<4A<<''''''r   c                     t          j        d          } t          j        d          }| |u sJ t          |           t          |          k    sJ t          j        t          j        |                     }| |usJ t          |           t          |          k    sJ t          | | g          t          ||g          k    sJ t          | | g          t          | |g          k    sJ t          j        dt           j        dfdt           j        dfg          }t          j        dt           j        dfdt           j        dfg          }t          |          t          |          k    sJ t          j        t          j        |                    }||usJ t          |          t          |          k    sJ t          ||g          t          ||g          k    sJ t          ||g          t          ||g          k    sJ d S )Nf4name   grades)rA   )r	   rt   r   r   r   r   str_r   )dt1dt2dt1_roundtrippedcomplex_dt1complex_dt2complex_dt1_roundtrippeds         r   test_numpy_dtype_picklingr   b  s&    (4..C
(4..C #::::99S		!!!!|FL$5$566&&&&&99-......c
t%57G$HIIIIIIc
tS*:$;<<<<<<(VRWb1Hbj$3OPQQK(VRWb1Hbj$3OPQQK [ 1 11111%|FL,E,EFF#;6666%= > >>>>>k*++t	!#;<0 0     k*++t	!;/0 0      r   zto_hash,expected)zThis is a string to hash 71b3f47df22cb19431d85d92d0b230b2)u   C'est lété 2d8d189e9b2b0b2e384d93c868c0e576))i@ i1  i3~ e205227dd82250871fa25aa0ec690aa3c                 Z    g | ](}t          j        d                                            )S )*   )r^   Random)r   r    s     r   r   r     s.    :::AV]2%%'':::r       a11ffad81f9682a7d901e6edc3d16c84{   )irA   r   )abcdesadfas aeda150553d4bb5c69f0e69d51b0e2efc                 0    t          |           |k    sJ d S r   r   )to_hashexpecteds     r   test_hashes_stay_the_samer     s!    & ==H$$$$$$r   c                      t           j                            d          } |                     d          }t          j        |          }t          |          t          |          k    sJ d S )Nr   r]   )r	   r^   r_   r`   r   r   )rngarr_carr_fs      r   Atest_hashes_are_different_between_c_and_fortran_contiguous_arraysr     s_     )


"
"Ch''Ee$$E;;$u++%%%%%%r   c                  H    t          t          j        d                     d S Nr   r   r	   ry   r   r   r   test_0d_arrayr     s    !r   c                      t          t          j        d                    t          t          j        dg                    k    sJ d S r   r   r   r   r   )test_0d_and_1d_array_hashing_is_differentr     s;    RXqc]] 3 3333333r   c                  |   d }  |             } |             }t          d          }t          d          }	 t          ||          D ]\  }}|                    t          |                                          }|                    t          |                                          }||k    sJ |                    t          |                                          }	||	k    sJ 	 |                                 |                                 d S # |                                 |                                 w xY w)Nc                  B   t           j                            d                              ddd                              d          t          fdt          d          D                       fd	t          d          D                                 d
d                              d                              dd                              d                              d                              d          gdt          j        dd          	                    d          t          j
        t          j        dd          	                    d                    t          j        dd          	                    d          d d d df         g} | S )Nr   ir|   2   )highsizez<i8c              3   h   K   | ],}                     d                               d          V  -dS )r   <f4Nrandnastyper   r    r   s     r   	<genexpr>z_test_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hash.<locals>.<genexpr>  s;      ??#))A,,%%e,,??????r   r   c                 `    g | ]*}                     d                               d          +S )r   r   r   r   s     r   r   z`test_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hash.<locals>.<listcomp>  s1    :::ASYYq\\  '':::r   r   r   rO      )r   )ir   d   rv   r]   rA   )r	   r^   r_   randintr   tupler   r   rw   r   r   )to_hash_listr   s    @r   create_objects_to_hashzLtest_hashes_stay_the_same_with_numpy_objects.<locals>.create_objects_to_hash  su   i##B'' KKDrK2299%@@????eAhh?????::::q:::yyA--e44KKK,,33E::IIbMM((//  Ic'''//99bi5999AA(KKLLIc'''//99!!!RaR%@#
& r   r@   )max_workers)r   zipsubmitr   resultshutdown)
r  to_hash_list_oneto_hash_list_twoe1e2obj_1obj_2hash_1hash_2hash_3s
             r   ,test_hashes_stay_the_same_with_numpy_objectsr    s@   "  > .-//--//		+	+	+B		+	+	+B 02BCC 	$ 	$LE5YYtU++2244FYYtU++2244FV#### YYtU++2244FV#####	$ 	
 	
s   B-D *D;c                      d } t          t          j                  5 }t          |            d d d            n# 1 swxY w Y   |                    d           d S )Nc                      dS )Nr   r   r   r   r   non_picklablez2test_hashing_pickling_error.<locals>.non_picklable  s    rr   zPicklingError while hashing)r   r   PicklingErrorr   match)r  excinfos     r   test_hashing_pickling_errorr    s       
$	%	% ]              MM/00000s   9= =c                      d} t          t          |           5  ddi}t          |d           d d d            d S # 1 swxY w Y   d S )Nz!Valid options for 'hash_name' are)r  r   r   invalid)	hash_name)r   
ValueErrorr   )msgdatas     r   test_wrong_hash_namer    s    
-C	
#	&	&	& ( (u~TY''''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   <A A )A__doc__rT   r   r   rQ   ri   r   r^   sysr   concurrent.futuresr   decimalr   joblib.func_inspectr   joblib.hashingr   joblib.memoryr   joblib.test.commonr	   r
   joblib.testingr   r   r   r   r   r"   r+   r   r-   r8   r   r   r   
input_listrM   rY   rf   rn   rs   rz   r   r   platformr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r   <module>r*     sG        				  				       



  2 2 2 2 2 2       + + + + + +                   - - - - - - - - ? ? ? ? ? ? ? ? ? ? ? ?          F   
    F    

 	
 	
	 FFFJ	CMMC
NN
a!!E#
L VZ  VZ  2 2 !  ! 2. . . 	z   & & &      W(89::! ! ;: !    ]T5M**  +* & LGB   	  
DP P P          6  :  ( ( ( % % %P H@E::q:::.	
 ---	0	02TU	 % % % & & &    4 4 4 B B BJ1 1 1( ( ( ( (r   