
    -iE                     .	   S SK r S SKrS SKrS SKJr  S SKrS SKrS SKJ	r	  S SK
JrJr  S SKJrJrJrJrJrJr  S SKJrJrJr  S SKJr  S SKJr  / S	QrS7S
\S\S\4S jjrS r S r!  S8S jr"SSSSSSS.S jr#   S9S jr$SSS.r%SSS.r&\\RN                  4\" \"40 \&D6\\RP                  4\" \"40 \%D6\\RN                  4\" \$40 \&D6\\RP                  4\" \$40 \%D60r)S r*\RV                  RY                  SSS/5      S 5       r-\RV                  RY                  S\5      S 5       r.S  r/S! r0S" r1S# r2\RV                  RY                  S$\\/5      \RV                  RY                  S%\RN                  \RP                  /5       S:S& j5       5       r3\RV                  RY                  S$\\/5      \RV                  RY                  S%\RN                  \RP                  /5       S:S' j5       5       r4\RV                  RY                  S(\\RN                  4\\RP                  4\\RP                  4\\RN                  4/5      \RV                  RY                  S\5      S) 5       5       r5\RV                  RY                  S$\\/5       S;S* j5       r6\RV                  RY                  S
\5      \RV                  RY                  S+S,5      \RV                  RY                  S%\RN                  \RP                  /5      \RV                  RY                  S\5         S<S- j5       5       5       5       r7\RV                  RY                  S
\5      \RV                  RY                  S+S,5      \RV                  RY                  S%\RN                  \RP                  /5        S=S. j5       5       5       r8\RV                  RY                  S$\\/5      \RV                  RY                  S
S/S0/5      \RV                  RY                  S%\RN                  \RP                  /5      S1 5       5       5       r9\RV                  RY                  S%\RN                  \RP                  /5      \RV                  RY                  S\5      S2 5       5       r:S3 r;\RV                  RY                  S4/ S5Q5      S6 5       r<g)>    N)partial)cdist)euclidean_distancespairwise_distances)ArgKminArgKminClassMode BaseDistancesReductionDispatcherRadiusNeighborsRadiusNeighborsClassModesqeuclidean_row_norms)assert_allcloseassert_array_equalcreate_memmap_backed_data)CSR_CONTAINERS)_get_threadpool_controller)
braycurtiscanberra	chebyshev	cityblock	euclidean	minkowski
seuclideanmetric
n_featuresseedc           
      $   [         R                  R                  U5      nU S:X  aJ  [        SS9[        SS9[        SS9[        [         R                  S9[        SUR                  U5      S9/nU$ U S:X  a  [        UR                  U5      S9/$ 0 /$ )	z5Return list of dummy DistanceMetric kwargs for tests.r   g      ?)p      )r   wr   )V)nprandomRandomStatedictinfrand)r   r   r   rngminkowski_kwargss        j/var/www/html/venv/lib/python3.13/site-packages/sklearn/metrics/tests/test_pairwise_distances_reduction.py_get_metric_params_listr+   +   s     ))


%C3K1I1I266N1,-
  sxx
+,-- 4K    c                 4   [        [        X15      5      n[        [        XB5      5      n[        U5      R                  [        U5      5      n	U	 H  n
Xz   nX   n [	        XXVS9  M     g	! [
         a$  n[        SU  SU
 SU SU SU SU S35      UeS	nAff = f)
zCheck that the distances of common neighbors are equal up to tolerance.

This does not check if there are missing neighbors in either result set.
Missingness is handled by assert_no_missing_neighbors.
)rtolatolQuery vector with index z< lead to different distances for common neighbor with index z	: dist_a=z vs dist_b=z (with atol=z
 and rtol=)N)r%   zipsetintersectionr   AssertionError)	query_idx
dist_row_a
dist_row_bindices_row_aindices_row_br.   r/   indices_to_dist_aindices_to_dist_bcommon_indicesidxdist_adist_bes                 r*   *assert_same_distances_for_common_neighborsrB   D   s    " S;<S;<'44S5GHN"'"'	FA	 
  		 !*9+ 6336% 8!(+fX\$ Ha!
 			s   
A))
B3BBc                     X:  nX%:  n[         R                  " X6   U5      n[         R                  " XG   U5      n	[        U	5      S:  d  [        U5      S:  a!  [        SU  SU	 SU SU SU SU SU S	35      eg
)a  Compare the indices of neighbors in two results sets.

Any neighbor index with a distance below the precision threshold should
match one in the other result set. We ignore the last few neighbors beyond
the threshold as those can typically be missing due to rounding errors.

For radius queries, the threshold is just the radius minus the expected
precision level.

For k-NN queries, it is the maximum distance to the k-th neighbor minus the
expected precision level.
r   r0   zC lead to mismatched result indices:
neighbors in b missing from a: z 
neighbors in a missing from b: z
dist_row_a=z
dist_row_b=z
indices_row_a=z
indices_row_b=
N)r"   	setdiff1dlenr5   )
r6   r7   r8   r9   r:   	thresholdmask_amask_bmissing_from_bmissing_from_as
             r*   assert_no_missing_neighborsrL   j   s    ( #F#F\\-"7GN\\-"7GN
>Q#n"5"9&yk 2..<-= >..<-= >$ &$ &*O ,*O2/
 	
 #:r,   h㈵>c           
      0   S nU R                   UR                   s=:X  a(  UR                   s=:X  a  UR                   :X  d    S5       e   S5       eU R                   u  px[        U5       H  n	X	   n
X   nX)   nX9   nU" U
5      (       d
   SU	 35       eU" U5      (       d
   SU	 35       e[        U	U
UUUUU5        SU-
  [        R                  " [        R
                  " U
5      [        R
                  " U5      5      -  U-
  n[        U	U
UUUU5        M     g)a  Assert that argkmin results are valid up to rounding errors.

This function asserts that the results of argkmin queries are valid up to:
- rounding error tolerance on distance values;
- permutations of indices for distances values that differ up to the
  expected precision level.

Furthermore, the distances must be sorted.

To be used for testing neighbors queries on float32 datasets: we accept
neighbors rank swaps only if they are caused by small rounding errors on
the distance computations.
c                 @    [         R                  " U S S U SS  :*  5      $ N   r"   allas    r*   <lambda>3assert_compatible_argkmin_results.<locals>.<lambda>       "&&3B1QR51r,   z+Arrays of results have incompatible shapes.Distances aren't sorted on row rR   N)shaperangerB   r"   maximummaxrL   )neighbors_dists_aneighbors_dists_bneighbors_indices_aneighbors_indices_br.   r/   	is_sorted	n_queries_r6   r7   r8   r9   r:   rG   s                  r*   !assert_compatible_argkmin_resultsrf      sW   * 2I 	""	%$$	% $$	%5
 55	%5
 55	% %**LI 9%	&1
&1
+6+6$$S(G	{&SS$$$S(G	{&SS$2	
& XFF:z 2"
 
	 	$	
? &r,   
   )XYr   precomputed_distsexpected_n_neighborsn_subsampled_queriesc                     Uc
  Uc   S5       eUc  U c   eUc   e[        X4SU0UD6nOUS U R                  5       nUR                  SS9  US S 2U4   R                  5       $ )Nz4Either metric or precomputed_dists must be provided.r   rR   axis)r   copysortmean)rh   ri   r   rj   rk   rl   metric_kwargssampled_distss           r*   _non_trivial_radiusru      s     (F,> >>  }}}}*1PP-P)*?+?@EEGA0016688r,   Tc           
         S n[        U 5      [        U5      s=:X  a!  [        U5      s=:X  a  [        U5      :X  d    e   e[        U 5      n	[        U	5       GH  n
X
   nX   nX*   nX:   nU(       a.  U" U5      (       d
   SU
 35       eU" U5      (       d
   SU
 35       e[        U5      [        U5      :X  d   e[        U5      [        U5      :X  d   e[        U5      S:  a+  [        R                  " U5      nX::  d   SU SU SU
 35       e[        U5      S:  a,  [        R                  " U5      nUU::  d   SU SU SU
 35       e[	        U
UUUUUU5        SU-
  U-  U-
  n[        U
UUUUU5        GM!     g)	a  Assert that radius neighborhood results are valid up to:

  - relative and absolute tolerance on computed distance values
  - permutations of indices for distances values that differ up to
    a precision level
  - missing or extra last elements if their distance is
    close to the radius

To be used for testing neighbors queries on float32 datasets: we
accept neighbors rank swaps only if they are caused by small
rounding errors on the distance computations.

Input arrays must be sorted w.r.t distances.
c                 @    [         R                  " U S S U SS  :*  5      $ rP   rS   rU   s    r*   rW   2assert_compatible_radius_results.<locals>.<lambda>  rY   r,   rZ   r   zLargest returned distance z not within requested radius z on row rR   N)rF   r\   r"   r^   rB   rL   )r_   r`   ra   rb   radiuscheck_sortedr.   r/   rc   rd   r6   r7   r8   r9   r:   
max_dist_a
max_dist_brG   s                     r*    assert_compatible_radius_resultsr}      s   0 2I 	 !	$"#	$ "#	$	$	$ %&I 9%	&1
&1
+6+6Z((W,KI;*WW(Z((W,KI;*WW(:#m"4444:#m"4444 z?Q
+J' ,ZL 9!((9+7' z?Q
+J' ,ZL 9!((9+7'
 	3	
 X'$.	#	
M &r,   Hz>r/   r.   g&.>c                     Sn Sn[        XS9nU S-  nSU-
  nSU-   nSU-
  nSU-   n[        R                  " SSUSU/XDS	XU//5      n[        R                  " / S
Q/ SQ/5      n	[        XXU5        [        [        R                  " SSUSU//5      [        R                  " SSUSU//5      [        R                  " / S
Q/5      [        R                  " / SQ/5      40 UD6  [        [        R                  " SSSSU//5      [        R                  " SSSUS//5      [        R                  " / S
Q/5      [        R                  " / SQ/5      40 UD6  [        [        R                  " US	XUU//5      [        R                  " S	S	S	S	U//5      [        R                  " / SQ/5      [        R                  " / SQ/5      40 UD6  [        [        R                  " US	XUU//5      [        R                  " US	S	S	U//5      [        R                  " / SQ/5      [        R                  " / SQ/5      40 UD6  [        R
                  " S5      n
[        R                  " [        U
S9   [        [        R                  " SSUSU//5      [        R                  " SSUSU//5      [        R                  " / S
Q/5      [        R                  " / SQ/5      40 UD6  S S S 5        [        R
                  " S5      n
[        R                  " [        U
S9   [        [        R                  " SSUSU//5      [        R                  " SSUSU//5      [        R                  " / S
Q/5      [        R                  " / SQ/5      40 UD6  S S S 5        [        R
                  " S5      n
[        R                  " [        U
S9   [        [        R                  " USUSU//5      [        R                  " SSUSS//5      [        R                  " / S
Q/5      [        R                  " / SQ/5      40 UD6  S S S 5        [        R
                  " S5      n
[        R                  " [        U
S9   [        [        R                  " USUSS//5      [        R                  " SSUSU//5      [        R                  " / SQ/5      [        R                  " / SQ/5      40 UD6  S S S 5        Sn
[        R                  " [        U
S9   [        [        R                  " SSUSU//5      [        R                  " SSUSU//5      [        R                  " / S
Q/5      [        R                  " / SQ/5      40 UD6  S S S 5        g ! , (       d  f       GN= f! , (       d  f       GN	= f! , (       d  f       GNr= f! , (       d  f       N= f! , (       d  f       g = f) Nr~           r   r         ?ffffff@333333?      @rR   rR   r   r                  	   rg   )rR   r   r   r   r   g      @)rR   r   r   r   r   )r   r   r   rg   r   r   r   r   r   rg   )"      r         )*   rR         r   pQuery vector with index 0 lead to different distances for common neighbor with index 1: dist_a=1.2 vs dist_b=2.5matchr   rR   r   r   r   zFneighbors in b missing from a: [12]
neighbors in a missing from b: [1])r   r   r      r   zDneighbors in b missing from a: []
neighbors in a missing from b: [3]r   )r   rR   r   r   r   zDneighbors in b missing from a: [5]
neighbors in a missing from b: [])rR   r   r   r   r   )r   rR   r   r   r    Distances aren't sorted on row 0r   rR   r   r   r   )	r%   r"   arrayrf   reescapepytestraisesr5   )r/   r.   tolseps_1m_1p_6_1m_6_1pref_distref_indicesmsgs              r*   &test_assert_compatible_argkmin_resultsr   _  s   DDT%D
(C
)C
)C#IE#IExx#uc5)q##	
H ((	
K &Kd &
3UC/01
3UC/01
/"#
/"#	
  &
3S#u-./
3S%-./
/"#
/"#	
  &
33S)*+
1aAs#$%
"#$
"#$	
  &
33S)*+
31a%&'
%&'
$%&	
  ))	-C 
~S	1)HHsCU345HHsCU345HHo&'HHo&'		

 	
 
2 ))QC 
~S	1)HHsCU345HHsCU345HHo&'HH'()		

 	
 
2 ))OC 
~S	1)HHsCU345HHsCQ/01HHo&'HH&'(		

 	
 
2 ))OC 
~S	1)HHsCQ/01HHsCU345HH&'(HHo&'		

 	
 
2 -C	~S	1)HHsCU345HHsCU345HHo&'HHo&'		

 	
 
2	1i 
2	1 
2	1 
2	1 
2	1 
2	1sA   !A2U,
A2U>3A2VA2V"1A2V3,
U;>
V
V"
V03
Wrz   Fc                    SnSn[        XS9nUS-  nSU-
  nSU-   nSU-
  nSU-   n[        R                  " SSUSU/5      [        R                  " US	Xf/5      /n	[        R                  " / S
Q5      [        R                  " / SQ5      /n
[        U	U	U
U
4SU S.UD6  [        [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " / S
Q5      /5      [        R                  " [        R                  " / SQ5      /5      4SU S.UD6  [        [        R                  " [        R                  " XUS	Xf/5      /5      [        R                  " [        R                  " XUS	Xf/5      /5      [        R                  " [        R                  " / SQ5      /5      [        R                  " [        R                  " / SQ5      /5      4SU S.UD6  [        R
                  " S5      n[        R                  " [        US9   [        [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " / S
Q5      /5      [        R                  " [        R                  " / SQ5      /5      4SU S.UD6  S S S 5        [        [        R                  " [        R                  " SSUSX/5      /5      [        R                  " [        R                  " SSUS/5      /5      [        R                  " [        R                  " / SQ5      /5      [        R                  " [        R                  " / SQ5      /5      4UU S.UD6  [        R
                  " S5      n[        R                  " [        US9   [        [        R                  " [        R                  " / SQ5      /5      [        R                  " [        R                  " SS/5      /5      [        R                  " [        R                  " / SQ5      /5      [        R                  " [        R                  " S	S/5      /5      4SU S.UD6  S S S 5        [        R
                  " S5      n[        R                  " [        US9   [        [        R                  " [        R                  " / SQ5      /5      [        R                  " [        R                  " / SQ5      /5      [        R                  " [        R                  " / SQ5      /5      [        R                  " [        R                  " / SQ5      /5      4SU S.UD6  S S S 5        [        R
                  " S5      n[        R                  " [        US9   [        [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " SSUSS/5      /5      [        R                  " [        R                  " / S
Q5      /5      [        R                  " [        R                  " / SQ5      /5      4SU S.UD6  S S S 5        [        R                  " [        US9   [        [        R                  " [        R                  " SSUSS/5      /5      [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " / S
Q5      /5      [        R                  " [        R                  " / SQ5      /5      4SU S.UD6  S S S 5        U (       a  S n[        R                  " [        US9   [        [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " / S
Q5      /5      [        R                  " [        R                  " / SQ5      /5      4US!S.UD6  S S S 5        g [        [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " SSUSU/5      /5      [        R                  " [        R                  " / S
Q5      /5      [        R                  " [        R                  " / SQ5      /5      4US"S.UD6  g ! , (       d  f       GNU= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       g = f)#Nr~   r   r   r   r   r   r   r   rR   r   )r   r   r   r   g      @)ry   rz   )rR   r   r   r   r   r   r   r   r   r   )rR   r   r   r   r   r   )rR   r   r   r   zQuery vector with index 0 lead to mismatched result indices:
neighbors in b missing from a: []
neighbors in a missing from b: [3])r   r   r   )rR   r   r   r   zQuery vector with index 0 lead to mismatched result indices:
neighbors in b missing from a: [4]
neighbors in a missing from b: [2])r   g @r   )r   r   r   )rR   r   r   zTLargest returned distance 6.100000033333333 not within requested radius 6.1 on row 0r   r   TF)	r%   r"   r   r}   r   r   r   r   r5   )rz   r/   r.   r   r   r   r   r   r   r   r   r   s               r*   %test_assert_compatible_radius_resultsr     s   DDT%D
(C
)C
)C#IE#IE 	#sE3./
#q##$H 	!
K %	
 !  %
"((CeS%89:;
"((CeS%89:;
"((?+,-
"((?+,-	
 !  %
"((Ca2345
"((Ca2345
"((+,-.
"((+,-.	
 !  ))	-C 
~S	1(HHbhhS%e<=>?HHbhhS%e<=>?HHbhh/01HHbhh/01		

 %	
 	
 
2 %
"((CeS%?@AB
"((CeS1234
"((-./0
"((<()*	
 !  ))	BC 
~S	1(HHbhh}-./HHbhhSz*+,HHbhhy)*+HHbhh1v&'(		

 %	
 	
 
2 ))	CC 
~S	1(HHbhh/01HHbhh}-./HHbhhy)*+HHbhhy)*+		

 %	
 	
 
2 ))	C 
~S	1(HHbhhS%e<=>?HHbhhS%c:;<=HHbhh/01HHbhh/01		

 %	
 	
 
2 
~S	1(HHbhhS%c:;<=HHbhhS%e<=>?HHbhh/01HHbhh/01		

 %	
 	
 
2 0]]>5,"((CeS%#@ABC"((CeS%#@ABC"((?345"((?345	
 !  65 	)HHbhhS%e<=>?HHbhhS%e<=>?HHbhh/01HHbhh/01		

 	
 	
} 
2	1: 
2	1 
2	1  
2	1 
2	1 65sN   Ce#B?e5B?f;Cf!Cf+Cf=#
e25
f
f
f(+
f:=
gcsr_containerc                    [         R                  R                  S5      nUR                  SS5      nUR                  SS5      nU " U5      nU " U5      nSn[        R
                  " X#U5      (       d   e[        R
                  " XEU5      (       d   e[        R
                  " XCU5      (       d   e[        R
                  " X%U5      (       d   e[        R
                  " UR                  [         R                  5      UR                  [         R                  5      U5      (       d   e[        R
                  " UR                  [         R                  5      UR                  [         R                  5      U5      (       d   e[        R
                  " UR                  [         R                  5      UR                  [         R                  5      U5      (       a   e[        R
                  " X#SS9(       a   e[        R
                  " UR                  [         R                  5      X65      (       a   e[        R
                  " X#R                  [         R                  5      U5      (       a   e[        R
                  " [         R                  " U5      X65      (       a   e[        R
                  " XCSS9(       d   e[        R
                  " X%SS9(       d   e[        R
                  " XESS9(       a   e[        R
                  " XESS9(       a   eU " US-  5      n[        R
                  " XsU5      (       a   eU " U5      nUR                  R                  [         R                  5      Ul        [        R
                  " XU5      (       a   eg )	Nr   d   rg   	manhattanpyfunc)r   r   sqeuclidean)r"   r#   r$   r'   r	   is_usable_forastypefloat64float32int64int32asfortranarrayindices)	r   r(   rh   ri   X_csrY_csrr   X_csr_0_nnzX_csr_int64s	            r*   /test_pairwise_distances_reduction_is_usable_forr     s   
))


"CbAbA!E!EF ,99!GGGG+99%OOOO+99%FKKKK+99!FKKKK+99	ahhrzz2F    ,99	ahhrzz2F    0==	AHHRXX.    0==a8TTT/==	a    0==	88BHHv   
 0==
!a    ,99%;WWW+99	   0==]   0==[    A&K/==kfUUUU  "K%--44RXX>K/==kfUUUUUr,   c            	      z   [         R                  R                  S5      n U R                  SS5      nU R                  SS5      nSnSnSn[        R
                  " [        US9   [        R                  " UR                  [         R                  5      X#US9  S S S 5        S	n[        R
                  " [        US9   [        R                  " XR                  [         R                  5      X4S9  S S S 5        [        R
                  " [        S
S9   [        R                  " XSUS9  S S S 5        [        R
                  " [        SS9   [        R                  " XSUS9  S S S 5        [        R
                  " [        SS9   [        R                  " XUSS9  S S S 5        [        R
                  " [        SS9   [        R                  " [         R                  " SS/5      X#US9  S S S 5        [        R
                  " [        SS9   [        R                  " [         R                  " U5      X#US9  S S S 5        SS0nSn[        R                  " [        US9   [        R                  " XX4US9  S S S 5        S[!        USS9S.nSn[        R                  " [        US9   [        R                  " XX4US9  S S S 5        S[!        USS90n["        R$                  " 5          ["        R&                  " S[        S9  [        R                  " XX4US9  S S S 5        [!        USS9[!        USS9S.n["        R$                  " 5          ["        R&                  " S[        S9  [        R                  " XX4US9  S S S 5        g ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN~= f! , (       d  f       GNX= f! , (       d  f       GN2= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN[= f! , (       d  f       GN= f! , (       d  f       g = f) NrR   r   rg   r   r   kOnly float64 or float32 datasets pairs are supported at this time, got: X.dtype=float32 and Y.dtype=float64r   )rh   ri   kr   iOnly float64 or float32 datasets pairs are supported at this time, got: X.dtype=float64 and Y.dtype=int32k == -1, must be >= 1.rQ   k == 0, must be >= 1.r   Unrecognized metricwrong metric;Buffer has wrong number of dimensions \(expected 2, got 1\)r          @ndarray is not C-contiguousr   r   4Some metric_kwargs have been passed \({'p': 3}\) but)rh   ri   r   r   rs   r   num_threadsr   Y_norm_squared?Some metric_kwargs have been passed \({'p': 3, 'Y_norm_squared'X_norm_squarederrorcategoryr   r   )r"   r#   r$   r'   r   r   
ValueErrorr   computer   r   r   r   r   warnsUserWarningr   warningscatch_warningssimplefilter)	r(   rh   ri   r   r   r   unused_metric_kwargsmessagers   s	            r*   (test_argkmin_factory_method_wrong_usagesr     s   
))


"CbAbA	AF	3  
z	-!((2::.!H 
.	1  
z	-!xx1QF 
. 
z)A	B!Bv6 
C 
z)@	A!Af5 
B 
z)>	?!An= 
@ 
X
 	"((C:.!H

 
z)F	G"++A.!H 
H  8EG	k	18L	
 
2 /qAM
 QG	k	1!AMR 
2
 	/qAM 
	 	 	"g<!AMR 
# 0qA/qAM 
	 	 	"g<!AMR 
#	"{ 
.	- 
.	- 
C	B 
B	A 
@	?
 

 
H	G 
2	1 
2	1 
#	" 
#	"s   #4M8:3N
NN.>O 6-O+O$O6P0P?0P,8
N

N
N+.
N= 
O
O!$
O36
P
P
P),
P:c                     [         R                  R                  S5      n U R                  SS5      nU R                  SS5      nSnSnSnU R	                  SSSS9n[         R
                  " U5      nS	n[        R                  " [        US
9   [        R                  " UR                  [         R                  5      UUUUUUS9  S S S 5        Sn[        R                  " [        US
9   [        R                  " UUR                  [         R                  5      UUUUUS9  S S S 5        [        R                  " [        SS
9   [        R                  " UUSUUUUS9  S S S 5        [        R                  " [        SS
9   [        R                  " UUSUUUUS9  S S S 5        [        R                  " [        SS
9   [        R                  " UUUSUUUS9  S S S 5        [        R                  " [        SS
9   [        R                  " [         R                  " SS/5      UUUUUUS9  S S S 5        [        R                  " [        SS
9   [        R                  " [         R                  " U5      UUUUUUS9  S S S 5        Sn	SU	 S3n
[        R                  " [        U
S
9   [        R                  " UUUUU	UUS9  S S S 5        g ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GNh= f! , (       d  f       GN>= f! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f)NrR   r   rg   r   r   uniformr   lowhighsizer   r   )rh   ri   r   r   weightsY_labelsunique_Y_labelsr   r   rQ   r   r   r   r   r   r   r   non_existent_weights_strategy[Only the 'uniform' or 'distance' weights options are supported at this time. Got: weights=''.)r"   r#   r$   r'   randintuniquer   r   r   r   r   r   r   r   r   r   )r(   rh   ri   r   r   r   r   r   r   r   r   s              r*   2test_argkmin_classmode_factory_method_wrong_usagesr     s   
))


"CbAbA	AFG{{qr{4Hii)O	3  
z	-  hhrzz"+	
 
.	1  
z	-  hhrxx +	
 
. 
z)A	B  +	
 
C 
z)@	A  +	
 
B 
z)>	?  !+	
 
@ 
X
 	  hhSz"+	

 
z)F	G  "+	
 
H %D!	67r	;  
z	1  1+	
 
2	1q 
.	- 
.	- 
C	B 
B	A 
@	?
 
 
H	G  
2	1s`   8K'8K K&<K88L
41L/L-L>
K
K#&
K58
L

L
L*-
L;>
Mc            	         [         R                  R                  S5      n U R                  SS5      nU R                  SS5      nSnSnSn[        R
                  " [        US9   [        R                  " UR                  [         R                  5      X#US9  S S S 5        S	n[        R
                  " [        US9   [        R                  " XR                  [         R                  5      X4S9  S S S 5        [        R
                  " [        S
S9   [        R                  " XSUS9  S S S 5        [        R
                  " [        SS9   [        R                  " XUSS9  S S S 5        [        R
                  " [        SS9   [        R                  " [         R                  " SS/5      X#US9  S S S 5        [        R
                  " [        SS9   [        R                  " [         R                  " U5      X#US9  S S S 5        SS0nSn[        R                  " [        US9   [        R                  " XX4US9  S S S 5        S[!        USS9S.nSn[        R                  " [        US9   [        R                  " XX4US9  S S S 5        [!        USS9[!        USS9S.n["        R$                  " 5          ["        R&                  " S[        S9  [        R                  " XX4US9  S S S 5        S[!        USS90n["        R$                  " 5          ["        R&                  " S[        S9  [        R                  " XX4US9  S S S 5        g ! , (       d  f       GN= f! , (       d  f       GNl= f! , (       d  f       GNF= f! , (       d  f       GN = f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN~= f! , (       d  f       GNI= f! , (       d  f       N= f! , (       d  f       g = f)NrR   r   rg   r   r   r   r   )rh   ri   ry   r   r   radius == -1.0, must be >= 0.rQ   r   r   r   r   r   r   r   r   r   )rh   ri   ry   r   rs   r   r   r   r   r   r   r   r   )r"   r#   r$   r'   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   )	r(   rh   ri   ry   r   r   r   r   rs   s	            r*   1test_radius_neighbors_factory_method_wrong_usagesr     s   
))


"CbAbAFF	3  

 	hhrzz"av	
	
	1  

 	!xx'9&X	
 
z)H	I!FC 
J 
z)>	?!O 
@ 
X
 	hhSz"av	

 
z)F	G"av	
 
H
  8 FG	k	1VBV	
 
2 /qAM
 QG	k	1V-	
 
2 0qA/qAM 
	 	 	"g<V-	
 
# 	/qAM 
	 	 	"g<V-	
 
#	"U
 

 
 
J	I 
@	?
 
 
H	G 
2	1 
2	1 
#	" 
#	"sx   #4M :3MM$M6>-N+NN,%N>.0O0O! 
M
M!$
M36
N
N
N),
N;>
O
O!
O/c                     [         R                  R                  S5      n U R                  SS5      nU R                  SS5      nSnSnSnU R	                  SSSS9n[         R
                  " U5      nS	n[        R                  " [        US
9   [        R                  " UR                  [         R                  5      UUUUUUS S9  S S S 5        Sn[        R                  " [        US
9   [        R                  " UUR                  [         R                  5      UUUUUS S9  S S S 5        [        R                  " [        SS
9   [        R                  " UUSUUUUS S9  S S S 5        [        R                  " [        SS
9   [        R                  " UUSSUUUS S9  S S S 5        [        R                  " [        SS
9   [        R                  " [         R                  " SS/5      UUUUUUS S9  S S S 5        [        R                  " [        SS
9   [        R                  " [         R                  " U5      UUUUUUS S9  S S S 5        Sn	SU	 S3n[        R                  " [        US
9   [        R                  " UUUSU	UUS S9  S S S 5        g ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GNZ= f! , (       d  f       GN/= f! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f)NrR   r   rg   r   r   r   r   r   r   r   )rh   ri   ry   r   r   r   r   outlier_labelr   r   rQ   r   wrong_metricr   r   r   r   r   r   r   )r"   r#   r$   r'   r   r   r   r   r   r   r   r   r   r   r   r   )
r(   rh   ri   ry   r   r   r   r   r   r   s
             r*   ;test_radius_neighbors_classmode_factory_method_wrong_usagesr     s   
))


"CbAbAFFG{{qr{4Hii)O	3  
z	- ((hhrzz"+		
 
.	1  
z	- ((hhrxx +		
 
. 
z)H	I ((+		
 
J 
z)>	? ((!+		
 
@ 
X
 	!((hhSz"+		

 
z)F	G (("+		
 
H %D!	67r	;  
z	- ((!1+		
 
.	-g 
.	-  
.	- 
J	I 
@	?
 
 
H	G" 
.	-sT   9J(9JJ1?K<2K0K&(K7
J
J.1
K 
K
K#&
K47
L
Dispatcherdtypec                    [         R                  R                  U 5      nSnUR                  / SQSSS9u  pgUR	                  Xc5      R                  U5      U-  nUR	                  Xs5      R                  U5      U-  n	U[        L a  Sn
0 n0 nO[        XSS9nUn
S	U0nS
S0nUR                  " UU	U
4SSSS.UD6u  pUR                  " UU	U
4SSSS.UD6u  nn[        X4   " UUUU40 UD6  g)z3Check that results do not depend on the chunk size.r   a   r   e     r   Fr   replacerg   r   rh   ri   r   ry   sort_resultsT   r   )
chunk_sizer   return_distance)   N)
r"   r#   r$   choicer'   r   r   ru   r   ASSERT_RESULT)global_random_seedr   r   r   r(   spreadn_samples_Xn_samples_Yrh   ri   	parametercheck_parameterscompute_parametersry   r   r   distr   s                     r*   test_chunk_size_agnosticismr  _  sD    ))

 2
3CF"zz*=AuzUK)007&@A)007&@AW	$qkB	$f-,d3&..		  H &&		  MD' :%&$W0@r,   c                 :   [         R                  R                  U 5      nUR                  / SQSSS9u  pVSnUR	                  XS5      R                  U5      U-  nUR	                  Xc5      R                  U5      U-  n	U[        L a  Sn
0 n0 nO[        XSS9nUn
S	U0nS
S0nUR                  " UU	U
4SSS.UD6u  p[        5       R                  SSS9   UR                  " UU	U
4SSS.UD6u  nnSSS5        [        X4   " UWUW40 UD6  g! , (       d  f       N#= f)z:Check that results do not depend on the number of threads.r   r   Fr   r   rg   r   r   ry   r  T   r  r  rR   openmp)limitsuser_apiN)r"   r#   r$   r  r'   r   r   ru   r   r   limitr  )r  r   r   r   r(   r
  r  r	  rh   ri   r  r  r  ry   r   r   r  r   s                     r*   test_n_threads_agnosticismr    sd    ))

 2
3C"zz*=AuzUKF)007&@A)007&@AW	$qkB	$f-,d3&..		  H 
$	%	+	+1x	+	H"**
  
 !
g 
I :%&$W0@ 
I	Hs   D
DzDispatcher, dtypec                 4   [         R                  R                  U 5      nSnSu  pgUR                  Xg5      R	                  U5      U-  nUR                  Xg5      R	                  U5      U-  n	U" U5      n
U" U	5      nU[
        L a  Sn0 n0 nO[        XSS9nUnSU0nSS0nUR                  " UU	U4S	SS
.UD6u  nn[        R                  " X4X45       HA  u  nnUUL a  UU	L a  M  UR                  " UUU4S	SS
.UD6u  nn[        X4   " UUUU40 UD6  MC     g)zLCheck that results do not depend on the format (dense, sparse) of the input.r   )r   r   rg   r   r   ry   r  T2   r  N)r"   r#   r$   r'   r   r   ru   r   	itertoolsproductr  )r  r   r   r   r(   r	  	n_samplesr   rh   ri   r   r   r  r  r  ry   
dist_denseindices_dense_X_Yr  r   s                         r*   test_format_agnosticismr"    sz   " ))

 2
3CF$I'..u5>A'..u5>A!E!EW	 %qkB	$f-,d3 * 2 2		! ! !J ##QJ
;B7rQw"**
  
 !
g 	z)*		

 	
 <r,   c           
         [         R                  R                  U 5      nUR                  [         R                  " / SQ[
        S95      nUR                  / SQSSS9u  pgSnUR                  Xc5      R                  U5      U-  n	UR                  Xs5      R                  U5      U-  n
US:X  a>  [         R                  " U	S	S	2S	S24   5      n	[         R                  " U
S	S	2S	S24   5      n
U[        L a  S
n0 n0 nO[        XUS9nUnSU0nSS0nUR                  " U	U
U4U[        XSU S9S   US-  SSS.UD6u  nnUR                  " U	U
U4U[        XSU S9S   US-  SSS.UD6u  nn[        X!4   " UUUU40 UD6  g	)z:Check that the results do not depend on the strategy used.)r   r   r   	haversiner   r   r   Fr   r   r$  Nrg   r   ry   r  Tr   r   r   parallel_on_X)r   rs   r  strategyr  parallel_on_Y)r"   r#   r$   r  r   objectr'   r   ascontiguousarrayr   ru   r   r+   r  )r  global_dtyper   r   r(   r   r
  r  r	  rh   ri   r  r  r  ry   
dist_par_Xindices_par_X
dist_par_Yindices_par_Ys                      r*   test_strategies_consistencyr1    s    ))

 2
3CZZ
 	

F  #zz*=AuzUKF)00>GA)00>GA   1bqb5*  1bqb5*W	$qf=	$f-,d3 * 2 2		! -%7

 !# ! !J  !+ 2 2		! -%7

 !# ! !J  :,-J}@Pr,   r(  )r'  r)  c                    [         R                  R                  U 5      nUR                  SS/5      n	UR                  SS/5      n
SnXR	                  XY5      R                  U5      U-  -   nXR	                  Xi5      R                  U5      U-  -   nU" U5      nU" U5      nUS:X  a>  [         R                  " US S 2S S24   5      n[         R                  " US S 2S S24   5      n[        X5      S   nUS:X  a  [        X5      nO[        X4S	U0UD6n[         R                  " US
S9S S 2S U24   n[         R                  " UR                  [         R                  S9n[        UR                  S   5       H  nUUUU   4   UU'   M     [        R                   " X4X45       H?  u  nn["        R$                  " UUUUUSUS-  US9u  nn[&        ["        U4   " UUUU5        MA     g )Nr  r   r       .A  r$  r   r   r   rR   rn   r%  Tr   )r   rs   r  r  r(  )r"   r#   r$   r  r'   r   r+  r+   r   r   argsortzerosr[   r   r\   r  r  r   r   r  )r  r   r(  r   r   rd   r  r   r(   r   translationr	  rh   ri   r   r   rs   dist_matrixargkmin_indices_refargkmin_distances_refrow_idxr   r!  argkmin_distancesargkmin_indicess                            r*   test_pairwise_distances_argkminr>  V  s    ))

 2
3CRI&J**aX&KFhhy5<<UCfLLAhhy5<<UCfLLA!E!E   1bqb5*  1bqb5*+F?BM )!/AAA=A**[q9!RaR%@HH%8%>%>bjjQ,22156)4(11*
g& 7
 ##QJ
;B-4__'  A~
.
*? 	w&'!		
 <r,   c                     [         R                  R                  U 5      nUR                  SS/5      nUR                  SS/5      nSn	XR	                  XG5      R                  U5      U	-  -   n
XR	                  XW5      R                  U5      U	-  -   n[        XU S9S   nUS:X  a  [        X5      nO[        X4SU0UD6n[        US	9n/ n/ nU Hs  n[         R                  " UR                  S   5      UU:*     nUU   n[         R                  " U5      nUU   UU   nnUR                  U5        UR                  U5        Mu     [        R                  " U
UUUUS
US-  US
S9	u  nn[         [        U4   " UUUX5        g )Nr  r   r   r3  r4  r&  r   r   )rj   Tr   )r   rs   r  r  r(  r  )r"   r#   r$   r  r'   r   r+   r   r   ru   aranger[   r5  appendr
   r   r  )r  r   r(  r   rd   r  r(   r   r7  r	  rh   ri   rs   r8  ry   neigh_indices_refneigh_distances_refrowindr  rq   neigh_distancesneigh_indicess                          r*   (test_pairwise_distances_radius_neighborsrH    s    ))

 2
3CRI&J**aX&KFhhy5<<UCfLLAhhy5<<UCfLLA+!3	M
 )!/AAA=A ;?F ii		!%cVm43xzz$ItDzT  %""4(  &5%<%<		#>&"O] ?E*+,m=Nr,   r   r   c                    [         R                  R                  S5      nSnSu  pVUR                  XV5      R	                  U5      U-  nUR                  XV5      R	                  U5      U-  n[        Xx/5      u  pU[        L a  Sn0 n0 nO#S[         R                  " U5      -  nUnSU0nSS0nUR                  " UUU4U SS.UD6u  nnUR                  " U	U
U4U SS.UD6u  nn[        X4   " UUUU40 UD6  g	)
zACheck that the results do not depend on the datasets writability.r   r   )   rg   rg   ry   r  T)r   r  N)
r"   r#   r$   r'   r   r   r   logr   r  )r   r   r   r(   r	  r  r   rh   ri   X_mmY_mmr  r  r  ry   r   r   dist_mm
indices_mms                      r*   test_memmap_backed_datarP    sI    ))


"CF#I'..u5>A'..u5>A +A62JDW	 rvvj))	$f-,d3&..		  Hk %,,  GZ :%&';
6Fr,   c                 F   [         R                  R                  U 5      nSnUR                  / SQ5      nUR                  / SQ5      nUR                  / SQ5      nUR	                  XV5      R                  U5      U-  nU" U5      n	[         R                  R                  USS9S-  n
[        XS9n[        XS9n[        X5        [        X5        [        R                  " [        5         [         R                  " U5      n[        XS9  S S S 5        g ! , (       d  f       g = f)	Nr   )r   r   r   r4  )r   rg   r   )rR   r   r   rR   rn   r   r   )r"   r#   r$   r  r'   r   linalgnormr   r   r   r   r   r   )r  r   r   r(   r	  r  r   r   rh   r   sq_row_norm_referencesq_row_normsq_row_norm_csrs                r*   test_sqeuclidean_row_normsrW    s     ))

 2
3CF

/0IL)J**Y'K'..u5>A!EIINN11N5:'CK+EKO)7);	z	"a a9 
#	"	"s   ) D
D c                  f   [         R                  R                  S5      n U R                  SS5      nU R                  SS5      nSnSnSnU R	                  SSSS9n[         R
                  " U5      n[        R                  " UUUUUUUS	S
9n[        R                  " UUUUUUUSS
9n	[        X5        g )NrR   r   rg   r   r   r   r   r   r'  )rh   ri   r   r   r   r   r   r(  r)  )	r"   r#   r$   r'   r   r   r   r   r   )
r(   rh   ri   r   r   r   r   r   	results_X	results_Ys
             r*   *test_argkmin_classmode_strategy_consistentr[  )  s    
))


"CbAbA	AFG{{qr{4Hii)O ((


' 	I !((


' 	I y,r,   r   )Nr   r   r   r   c                 j   [         R                  R                  S5      nUR                  SS5      nUR                  SS5      nSnSnSnUR	                  SSSS9n[         R
                  " U5      n[        R                  " UUUUUUUU S	S
9	n	[        R                  " UUUUUUUU SS
9	n
[        X5        g )NrR   r   rg   r   r   r   r   r   r'  )	rh   ri   ry   r   r   r   r   r   r(  r)  )	r"   r#   r$   r'   r   r   r   r   r   )r   r(   rh   ri   ry   r   r   r   r   rY  rZ  s              r*   3test_radius_neighbors_classmode_strategy_consistentr]  J  s    
))


"CbAbAFFG{{qr{4Hii)O(00

'# 
I )00

'# 
I I)r,   )rR   )rM   ư>)TrM   r^  )r   )rg   )r   r   rg   )r   r   )=r  r   r   	functoolsr   numpyr"   r   scipy.spatial.distancer   sklearn.metricsr   r   -sklearn.metrics._pairwise_distances_reductionr   r   r	   r
   r   r   sklearn.utils._testingr   r   r   sklearn.utils.fixesr   sklearn.utils.parallelr   1CDIST_PAIRWISE_DISTANCES_REDUCTION_COMMON_METRICSstrintr+   rB   rL   rf   ru   r}   FLOAT32_TOLSFLOAT64_TOLSr   r   r  r   markparametrizer   r   r   r   r   r   r  r  r"  r1  r>  rH  rP  rW  r[  r]   r,   r*   <module>ro     s    	     ( C  
 / =5 1C S  2#L!
R 
	G
X 
9F 		Q
j 
 
 bjj7#DUUbjj7#DUU


 /@<@


 /@<@H
V $7_
 8_
D .9AV :AVHJSZp
lY
xk
\ 'AB2::rzz":;
 	- < C-` 'AB2::rzz":;
 	, < C,^ 	"**	"**%	"**	"**%	 .97
 :7
t 'AB
 	I CI^ #TU%GH2::rzz":;.9 <
 : < I V<
~ #TU%GH2::rzz":; 9 < I V9x 'ABK#=>2::rzz":;. < ? C.b 2::rzz":;.9: : <:6-B *<= * > *r,   