
    sgE                     	   d dl Z d dlZd dlZd dlmZ d dlZd dlZd dlm	Z	 d dl
mZmZ d dlmZmZmZmZmZmZ d dlmZmZmZ d dlmZ d dlmZ g d	Zd7d
ededefdZd Z d Z!	 	 d8dZ"ddddddddZ#	 	 	 d9dZ$dddZ%dddZ&eejN                  f ee"fi e&eejP                  f ee"fi e%eejN                  f ee$fi e&eejP                  f ee$fi e%iZ)d Z*ejV                  jY                  dddg      d        Z-ejV                  jY                  de      d        Z.d  Z/d! Z0d" Z1d# Z2ejV                  jY                  d$eeg      ejV                  jY                  d%ejN                  ejP                  g      	 d:d&              Z3ejV                  jY                  d$eeg      ejV                  jY                  d%ejN                  ejP                  g      	 d:d'              Z4ejV                  jY                  d(eejN                  feejP                  feejP                  feejN                  fg      ejV                  jY                  de      d)               Z5ejV                  jY                  d$eeg      	 d;d*       Z6ejV                  jY                  d
e      ejV                  jY                  d+d,      ejV                  jY                  d%ejN                  ejP                  g      ejV                  jY                  de      	 	 	 d<d-                            Z7ejV                  jY                  d
e      ejV                  jY                  d+d,      ejV                  jY                  d%ejN                  ejP                  g      	 	 d=d.                     Z8ejV                  jY                  d$eeg      ejV                  jY                  d
d/d0g      ejV                  jY                  d%ejN                  ejP                  g      d1                      Z9ejV                  jY                  d%ejN                  ejP                  g      ejV                  jY                  de      d2               Z:d3 Z;ejV                  jY                  d4g d5      d6        Z<y)>    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           
      D   t         j                  j                  |      }| dk(  rYt        d      t        d      t        d      t        t         j                        t        d|j                  |            g}|S | dk(  rt        |j                  |            gS i gS )	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.12/site-packages/sklearn/metrics/tests/test_pairwise_distances_reduction.py_get_metric_params_listr+   +   s     ))


%C3K1I1I266N1,-
  sxx
+,-- 4K    c                 @   t        t        ||            }t        t        ||            }t        |      j                  t        |            }	|	D ]  }
||
   }||
   }	 t	        ||||        y	# t
        $ r$}t        d|  d|
 d| d| d| d| d      |d	}~ww xY w)
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 "3'"3'	FFDA	
  		 !*9+ 6336% 8!(+fX\$ Ha!
 			s   A00	B9BBc                     ||k  }||k  }t        j                  ||   |      }t        j                  ||   |      }	t        |	      dkD  st        |      dkD  r!t        d|  d|	 d| d| d| d| d| d	      y
)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           
         d }| j                   |j                   cxk(  r%|j                   cxk(  r|j                   k(  sJ d        J d       | j                   \  }}t        |      D ]  }	| |	   }
||	   }||	   }||	   } ||
      s
J d|	         ||      s
J d|	        t        |	|
|||||       d|z
  t        j                  t        j
                  |
      t        j
                  |            z  |z
  }t        |	|
||||        y)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                 >    t        j                  | d d | dd  k        S N   r"   allas    r*   <lambda>z3assert_compatible_argkmin_results.<locals>.<lambda>       "&&3B1QR51 r,   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_resultsre      s`   * 2I 	""	%$$	% $$	%5
 55	%5
 55	% %**LIq 9% &
	&y1
&y1
+I6+I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                     |	|J d       || J |J t        | |fd|i|}n|d | j                         }|j                  d       |d d |f   j                         S )Nz4Either metric or precomputed_dists must be provided.r   rR   axis)r   copysortmean)rg   rh   r   ri   rj   rk   metric_kwargssampled_distss           r*   _non_trivial_radiusrt      s      	%);>=>;  }}}}*1aPP-P)*?+?@EEGA0016688r,   Tc           
         d }t        |       t        |      cxk(  rt        |      cxk(  rt        |      k(  sJ  J t        |       }	t        |	      D ]  }
| |
   }||
   }||
   }||
   }|r$ ||      s
J d|
         ||      s
J d|
        t        |      t        |      k(  sJ t        |      t        |      k(  sJ t        |      dkD  r*t        j                  |      }||k  sJ d| d| d|
        t        |      dkD  r*t        j                  |      }||k  sJ d| d| d|
        t	        |
||||||       d|z
  |z  |z
  }t        |
|||||        y)	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                 >    t        j                  | d d | dd  k        S rP   rS   rU   s    r*   rW   z2assert_compatible_radius_results.<locals>.<lambda>  rX   r,   rY   r   zLargest returned distance z not within requested radius z on row rR   N)rF   r[   r"   r]   rB   rL   )r^   r_   r`   ra   radiuscheck_sortedr.   r/   rb   rc   r6   r7   r8   r9   r:   
max_dist_a
max_dist_brG   s                     r*    assert_compatible_radius_resultsr{      s   0 2I 	 !	$"#	$ "#	$	$	$ %&I 9% -
	&y1
&y1
+I6+I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                  *   d} d}t        | |      }| dz  }d|z
  }d|z   }d|z
  }d|z   }t        j                  dd|d|g||d	||gg      }t        j                  g d
g dg      }	t        |||	|	|       t        t        j                  dd|d|gg      t        j                  dd|d|gg      t        j                  g d
g      t        j                  g dg      fi | t        t        j                  dddd|gg      t        j                  ddd|dgg      t        j                  g d
g      t        j                  g dg      fi | t        t        j                  |d	|||gg      t        j                  d	d	d	d	|gg      t        j                  g dg      t        j                  g dg      fi | t        t        j                  |d	|||gg      t        j                  |d	d	d	|gg      t        j                  g dg      t        j                  g dg      fi | t	        j
                  d      }
t        j                  t        |
      5  t        t        j                  dd|d|gg      t        j                  dd|d|gg      t        j                  g d
g      t        j                  g dg      fi | d d d        t	        j
                  d      }
t        j                  t        |
      5  t        t        j                  dd|d|gg      t        j                  dd|d|gg      t        j                  g d
g      t        j                  g dg      fi | d d d        t	        j
                  d      }
t        j                  t        |
      5  t        t        j                  |d|d|gg      t        j                  dd|ddgg      t        j                  g d
g      t        j                  g dg      fi | d d d        t	        j
                  d      }
t        j                  t        |
      5  t        t        j                  |d|ddgg      t        j                  dd|d|gg      t        j                  g dg      t        j                  g dg      fi | d d d        d}
t        j                  t        |
      5  t        t        j                  dd|d|gg      t        j                  dd|d|gg      t        j                  g d
g      t        j                  g dg      fi | d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   ^xY w# 1 sw Y   xY w# 1 sw Y   y xY w) Nr|           r}   r         ?ffffff@333333?      @rR   rR   r   r                  	   rf   )rR   r   r   r   r   g      @)rR   r   r   r   r   )r   r   r   rf   r   r   r   r   r   rf   )"      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"   arrayre   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   _  sI   DDT%D
(C
)C
)C#IE#IExx#uc5)#q#s#	
H ((	
K &(Kd &
3UC/01
3UC/01
/"#
/"#	
  &
3S#u-./
3S%-./
/"#
/"#	
  &
33S)*+
1aAs#$%
"#$
"#$	
  &
33S)*+
31a%&'
%&'
$%&	
  ))	-C 
~S	1 
)HHsCU345HHsCU345HHo&'HHo&'		

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

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

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

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

 	

 
i
 

 

 

 

 
sA   A.U A.U#&A.U0A.U=A.V	U #U-0U:=V	Vrx   Fc                    d}d}t        ||      }|dz  }d|z
  }d|z   }d|z
  }d|z   }t        j                  dd|d|g      t        j                  |d	||g      g}	t        j                  g d
      t        j                  g d      g}
t        |	|	|
|
fd| d| t        t        j                  t        j                  dd|d|g      g      t        j                  t        j                  dd|d|g      g      t        j                  t        j                  g d
      g      t        j                  t        j                  g d      g      fd| d| t        t        j                  t        j                  ||d	||g      g      t        j                  t        j                  ||d	||g      g      t        j                  t        j                  g d      g      t        j                  t        j                  g d      g      fd| d| t	        j
                  d      }t        j                  t        |      5  t        t        j                  t        j                  dd|d|g      g      t        j                  t        j                  dd|d|g      g      t        j                  t        j                  g d
      g      t        j                  t        j                  g d      g      fd| d| d d d        t        t        j                  t        j                  dd|d||g      g      t        j                  t        j                  dd|dg      g      t        j                  t        j                  g d      g      t        j                  t        j                  g d      g      f|| d| t	        j
                  d      }t        j                  t        |      5  t        t        j                  t        j                  g d      g      t        j                  t        j                  ddg      g      t        j                  t        j                  g d      g      t        j                  t        j                  d	dg      g      fd| d| d d d        t	        j
                  d      }t        j                  t        |      5  t        t        j                  t        j                  g d      g      t        j                  t        j                  g d      g      t        j                  t        j                  g d      g      t        j                  t        j                  g d      g      fd| d| d d d        t	        j
                  d      }t        j                  t        |      5  t        t        j                  t        j                  dd|d|g      g      t        j                  t        j                  dd|ddg      g      t        j                  t        j                  g d
      g      t        j                  t        j                  g d      g      fd| d| d d d        t        j                  t        |      5  t        t        j                  t        j                  dd|ddg      g      t        j                  t        j                  dd|d|g      g      t        j                  t        j                  g d
      g      t        j                  t        j                  g d      g      fd| d| d d d        | rd }t        j                  t        |      5  t        t        j                  t        j                  dd|d|g      g      t        j                  t        j                  dd|d|g      g      t        j                  t        j                  g d
      g      t        j                  t        j                  g d      g      f|d!d| d d d        y t        t        j                  t        j                  dd|d|g      g      t        j                  t        j                  dd|d|g      g      t        j                  t        j                  g d
      g      t        j                  t        j                  g d      g      f|d"d| y # 1 sw Y   xY w# 1 sw Y   }xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)#Nr|   r   r}   r   r   r   r   r   rR   r   )r   r   r   r   g      @)rw   rx   )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   )rx   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#s#$H 	!
K %	
 !  %
"((CeS%89:;
"((CeS%89:;
"((?+,-
"((?+,-	
 !  %
"((Cac2345
"((Cac2345
"((+,-.
"((+,-.	
 !  ))	-C 
~S	1 	
(HHbhhS%e<=>?HHbhhS%e<=>?HHbhh/01HHbhh/01		

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

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

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

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

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

 	
 	
}	
 	
:	
 	
	
 	
 	
 	
	
 	
		 		sN   B=d7B7d)&B7d6B=e5B=eB=ed&)d36e eee&csr_containerc                    t         j                  j                  d      }|j                  dd      }|j                  dd      } | |      } | |      }d}t	        j
                  |||      sJ t	        j
                  |||      sJ t	        j
                  |||      sJ t	        j
                  |||      sJ t	        j
                  |j                  t         j                        |j                  t         j                        |      sJ t	        j
                  |j                  t         j                        |j                  t         j                        |      sJ t	        j
                  |j                  t         j                        |j                  t         j                        |      rJ t	        j
                  ||d      rJ t	        j
                  |j                  t         j                        ||      rJ t	        j
                  ||j                  t         j                        |      rJ t	        j
                  t        j                  |      ||      rJ t	        j
                  ||d      sJ t	        j
                  ||d      sJ t	        j
                  ||d      rJ t	        j
                  ||d      rJ  | |dz        }t	        j
                  |||      rJ  | |      }|j                  j                  t         j                        |_        t	        j
                  |||      rJ y )	Nr   d   rf   	manhattanpyfunc)r   r   sqeuclidean)r"   r#   r$   r'   r	   is_usable_forastypefloat64float32int64int32asfortranarrayindices)	r   r(   rg   rh   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!QGGG+99%OOO+99%FKKK+99!UFKKK+99	ahhrzz2F   ,99	ahhrzz2F   0==	AHHRXX.   0==a8TTT/==	a   0==	188BHHv  
 0==
!a   ,99%;WWW+99	5   0==u]   0==u[    A&K/==k1fUUU  "K%--44RXX>K/==k1fUUUUr,   c                     t         j                  j                  d      } | j                  dd      }| j                  dd      }d}d}d}t	        j
                  t        |      5  t        j                  |j                  t         j                        |||       d d d        d	}t	        j
                  t        |      5  t        j                  ||j                  t         j                        ||       d d d        t	        j
                  t        d
      5  t        j                  ||d|       d d d        t	        j
                  t        d      5  t        j                  ||d|       d d d        t	        j
                  t        d      5  t        j                  |||d       d d d        t	        j
                  t        d      5  t        j                  t        j                  ddg      |||       d d d        t	        j
                  t        d      5  t        j                  t        j                  |      |||       d d d        ddi}d}t	        j                  t        |      5  t        j                  |||||       d d d        dt!        |d      d}d}t	        j                  t        |      5  t        j                  |||||       d d d        dt!        |d      i}t#        j$                         5  t#        j&                  dt               t        j                  |||||       d d d        t!        |d      t!        |d      d}t#        j$                         5  t#        j&                  dt               t        j                  |||||       d d d        y # 1 sw Y   "xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   sxY w# 1 sw Y   CxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   AxY w# 1 sw Y   xY w# 1 sw Y   y xY w) NrR   r   rf   r   r   kOnly float64 or float32 datasets pairs are supported at this time, got: X.dtype=float32 and Y.dtype=float64r   )rg   rh   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)rg   rh   r   r   rr   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(   rg   rh   r   r   r   unused_metric_kwargsmessagerr   s	            r*   (test_argkmin_factory_method_wrong_usagesr     s   
))


"CbAbA	AF	3  
z	- I!((2::.!qHI	1  
z	- G!qxx1QvFG 
z)A	B 7!qBv67 
z)@	A 6!qAf56 
z)>	? >!qAn=> 
X
 I 	"((C:.!qHI
 
z)F	G I"++A.!qHI  8EG	k	1 
1&8L	

 /qAM
 QG	k	1 S!qAfMRS
 	/qAM 
	 	 	" Sg<!qAfMRS 0qA/qAM 
	 	 	" Sg<!qAfMRS S{I IG G7 76 6> >I I
I I
 
S SS SS Ss   %7N<7O	OO#O0/O=$-P
:P
P$6P1=6P=<O	OO #O-0O:=P
PP!$P.1P:=Qc            
         t         j                  j                  d      } | j                  dd      }| j                  dd      }d}d}d}| j	                  ddd      }t        j
                  |      }d	}t        j                  t        |
      5  t        j                  |j                  t         j                        ||||||       d d d        d}t        j                  t        |
      5  t        j                  ||j                  t         j                        |||||       d d d        t        j                  t        d
      5  t        j                  ||d||||       d d d        t        j                  t        d
      5  t        j                  ||d||||       d d d        t        j                  t        d
      5  t        j                  |||d|||       d d d        t        j                  t        d
      5  t        j                  t        j                  ddg      ||||||       d d d        t        j                  t        d
      5  t        j                  t        j                  |      ||||||       d d d        d}	d|	 d}
t        j                  t        |

      5  t        j                  |||||	||       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   bxY w# 1 sw Y   /xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)NrR   r   rf   r   r   uniformr   lowhighsizer   r   )rg   rh   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(   rg   rh   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 	
  +	
	
 
z)@	A 	
  +	
	
 
z)>	? 	
  !+	
	
 
X
 
 	  hhSz"+	

 
z)F	G 	
  "+	
	
 %D!	67r	;  
z	1 	
  1+	
	
 	
q	
 	
	
 	
	
 	
	
 	
	
 	

 
	
 	
 	
 	
s`   :K"/:K/K<L	L2L#!0L/<L;"K,/K9<L	LL #L,/L8;Mc                     t         j                  j                  d      } | j                  dd      }| j                  dd      }d}d}d}t	        j
                  t        |      5  t        j                  |j                  t         j                        |||       d d d        d	}t	        j
                  t        |      5  t        j                  ||j                  t         j                        ||       d d d        t	        j
                  t        d
      5  t        j                  ||d|       d d d        t	        j
                  t        d      5  t        j                  |||d       d d d        t	        j
                  t        d      5  t        j                  t        j                  ddg      |||       d d d        t	        j
                  t        d      5  t        j                  t        j                  |      |||       d d d        ddi}d}t	        j                  t        |      5  t        j                  |||||       d d d        dt!        |d      d}d}t	        j                  t        |      5  t        j                  |||||       d d d        t!        |d      t!        |d      d}t#        j$                         5  t#        j&                  dt               t        j                  |||||       d d d        dt!        |d      i}t#        j$                         5  t#        j&                  dt               t        j                  |||||       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   fxY w# 1 sw Y   6xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   wxY w# 1 sw Y   4xY w# 1 sw Y   xY w# 1 sw Y   y xY w)NrR   r   rf   r   r   r   r   )rg   rh   rw   r   r   radius == -1.0, must be >= 0.rQ   r   r   r   r   r   r   r   r   r   )rg   rh   rw   r   rr   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(   rg   rh   rw   r   r   r   r   rr   s	            r*   1test_radius_neighbors_factory_method_wrong_usagesr     s   
))


"CbAbAFF	3  

 
 	hhrzz"av	
	
	1  

 Y 	!qxx'9&QWX	Y 
z)H	I D!qFCD 
z)>	? P!qOP 
X
 
 	hhSz"av	

 
z)F	G 
"av	


  8 FG	k	1 
1VFBV	

 /qAM
 QG	k	1 
1VF-	

 0qA/qAM 
	 	 	" 
g<1VF-	

 	/qAM 
	 	 	" 
g<1VF-	

 
U
 
Y YD DP P
 

 

 

 

 

 
sx   %7M?7NNN&/N3'-O =OO6O' 6O3?N	NN#&N03N= O
OO$'O03O<c                     t         j                  j                  d      } | j                  dd      }| j                  dd      }d}d}d}| j	                  ddd      }t        j
                  |      }d	}t        j                  t        |
      5  t        j                  |j                  t         j                        ||||||d        d d d        d}t        j                  t        |
      5  t        j                  ||j                  t         j                        |||||d        d d d        t        j                  t        d
      5  t        j                  ||d||||d        d d d        t        j                  t        d
      5  t        j                  ||dd|||d        d d d        t        j                  t        d
      5  t        j                  t        j                  ddg      ||||||d        d d d        t        j                  t        d
      5  t        j                  t        j                  |      ||||||d        d d d        d}	d|	 d}t        j                  t        |
      5  t        j                  |||d|	||d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   YxY w# 1 sw Y   %xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)NrR   r   rf   r   r   r   r   r   r   r   )rg   rh   rw   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(   rg   rh   rw   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 

 ((+		


 
z)>	? 

 ((!+		


 
X
 
 	!((hhSz"+		

 
z)F	G 

 (("+		


 %D!	67r	;  
z	- 

 ((!1+		


 

g

 

 

 



 



 


 


 

"

 

sT   ;J)0;J6KK3K&1K)K5)J36K KKK&)K25K>
Dispatcherdtypec                    t         j                  j                  |       }d}|j                  g ddd      \  }}|j	                  ||      j                  |      |z  }|j	                  ||      j                  |      |z  }	|t        u rd}
i }i }nt        ||	d      }|}
d	|i}d
di} |j                  ||	|
fdddd|\  }} |j                  ||	|
fdddd|\  }}t        ||f   ||||fi | y)z3Check that results do not depend on the chunk size.r   a   r   e     r   Fr   replacerf   r   rg   rh   r   rw   sort_resultsT   r   )
chunk_sizer   return_distance)   N)
r"   r#   r$   choicer'   r   r   rt   r   ASSERT_RESULT)global_random_seedr   r   r   r(   spreadn_samples_Xn_samples_Yrg   rh   	parametercheck_parameterscompute_parametersrw   r   r   distr   s                     r*   test_chunk_size_agnosticismr  _  sT    ))

 2
3CF"zz*=AuzUKj)007&@Aj)007&@AW	$qAkB	$f-,d3.J..		  Hk 'J&&		  MD' :u%&$W0@r,   c                 J   t         j                  j                  |       }|j                  g ddd      \  }}d}|j	                  ||      j                  |      |z  }|j	                  ||      j                  |      |z  }	|t        u rd}
i }i }nt        ||	d      }|}
d	|i}d
di} |j                  ||	|
fddd|\  }}t               j                  dd      5   |j                  ||	|
fddd|\  }}ddd       t        ||f   ||fi | y# 1 sw Y   xY w)z:Check that results do not depend on the number of threads.r   r   Fr   r   rf   r   r   rw   r   T   r  r  rR   openmp)limitsuser_apiN)r"   r#   r$   r  r'   r   r   rt   r   r   limitr  )r  r   r   r   r(   r  r	  r  rg   rh   r
  r  r  rw   r   r   r  r   s                     r*   test_n_threads_agnosticismr    sy    ))

 2
3C"zz*=AuzUKFj)007&@Aj)007&@AW	$qAkB	$f-,d3.J..		  Hk 
$	%	+	+1x	+	H 
*
**
  
 !
g
 :u%&$W0@
 
s    DD"zDispatcher, dtypec                 8   t         j                  j                  |       }d}d\  }}|j                  ||      j	                  |      |z  }|j                  ||      j	                  |      |z  }	 ||      }
 ||	      }|t
        u rd}i }i }nt        ||	d      }|}d|i}ddi} |j                  ||	|fd	dd
|\  }}t        j                  ||
f|	|f      D ]=  \  }}||u r||	u r |j                  |||fd	dd
|\  }}t        ||f   ||||fi | ? y)zLCheck that results do not depend on the format (dense, sparse) of the input.r   )r   r   rf   r   r   rw   r   T2   r  N)r"   r#   r$   r'   r   r   rt   r   	itertoolsproductr  )r  r   r   r   r(   r  	n_samplesr   rg   rh   r   r   r
  r  r  rw   
dist_denseindices_dense_X_Yr  r   s                         r*   test_format_agnosticismr     s   " ))

 2
3CF$IzJ'..u5>AJ'..u5>A!E!EW	 %qAkB	$f-,d3 2
 2 2		! ! !J ##QJE
; 
B7rQw*
**
  
 !
g 	z5)*		

 	

r,   c           	         t         j                  j                  |       }|j                  t        j                  g dt
                    }|j                  g ddd      \  }}d}|j                  ||      j                  |      |z  }	|j                  ||      j                  |      |z  }
|dk(  r<t        j                  |	d	d	d	df         }	t        j                  |
d	d	d	df         }
|t        u rd
}i }i }nt        |	|
|      }|}d|i}ddi} |j                  |	|
|f|t        |||       d   |dz  ddd|\  }} |j                  |	|
|f|t        |||       d   |dz  ddd|\  }}t        ||f   ||||fi | y	)z:Check that the results do not depend on the strategy used.)r   r   r   	haversiner   r   r   Fr   r   r"  Nrf   r   rw   r   Tr   r   r   parallel_on_X)r   rr   r  strategyr  parallel_on_Y)r"   r#   r$   r  r   objectr'   r   ascontiguousarrayr   rt   r   r+   r  )r  global_dtyper   r   r(   r   r  r	  r  rg   rh   r
  r  r  rw   
dist_par_Xindices_par_X
dist_par_Yindices_par_Ys                      r*   test_strategies_consistencyr/    s    ))

 2
3CZZ
 	

F  #zz*=AuzUKFj)00>GAj)00>GA   1bqb5*  1bqb5*W	$qAf=	$f-,d3 2
 2 2		! -J%7

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

 !# ! !J  :|,-J}@Pr,   r&  )r%  r'  c                    t         j                  j                  |       }|j                  ddg      }	|j                  ddg      }
d}|
|j	                  ||	      j                  |      |z  z   }|
|j	                  ||	      j                  |      |z  z   } ||      } ||      }|dk(  r<t        j                  |d d d df         }t        j                  |d d d df         }t        ||	      d   }|dk(  rt        ||      }nt        ||fd	|i|}t        j                  |d
      d d d |f   }t        j                  |j                  t         j                        }t        |j                  d         D ]  }||||   f   ||<    t        j                   ||f||f      D ]?  \  }}t#        j$                  |||||d|dz  |      \  }}t'        t"        |f   ||||       A y )Nr  r   r       .A  r"  r   r   r   rR   rm   r#  Tr   )r   rr   r  r  r&  )r"   r#   r$   r  r'   r   r)  r+   r   r   argsortzerosrZ   r   r[   r  r  r   r   r  )r  r   r&  r   r   rc   r  r   r(   r   translationr  rg   rh   r   r   rr   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chhy*5<<UCfLLAchhy*5<<UCfLLA!E!E   1bqb5*  1bqb5*+FJ?BM )!Q/AqAA=A**[q9!RaR%@HH%8%>%>bjjQ,22156 
)4(11*
g&

 ##QJE
; 
B-4__'  A~
.
*? 	w&'!		

r,   c                    t         j                  j                  |       }|j                  ddg      }|j                  ddg      }d}	||j	                  ||      j                  |      |	z  z   }
||j	                  ||      j                  |      |	z  z   }t        |||       d   }|dk(  rt        |
|      }nt        |
|fd|i|}t        |	      }g }g }|D ]p  }t        j                  |j                  d         ||k     }||   }t        j                  |      }||   ||   }}|j                  |       |j                  |       r t        j                  |
||||d
|dz  |d
	      \  }}t!        t        |f   |||||       y )Nr  r   r   r1  r2  r$  r   r   )ri   Tr   )r   rr   r  r  r&  r   )r"   r#   r$   r  r'   r   r+   r   r   rt   arangerZ   r3  appendr
   r   r  )r  r   r&  r   rc   r  r(   r   r5  r  rg   rh   rr   r6  rw   neigh_indices_refneigh_distances_refrowindr  rp   neigh_distancesneigh_indicess                          r*   (test_pairwise_distances_radius_neighborsrF    s    ))

 2
3CRI&J**aX&KFchhy*5<<UCfLLAchhy*5<<UCfLLA+
!3	M
 )!Q/AqAA=A ;?F  )ii		!%cVm43xzz$ItDzT  %""4() &5%<%<		#>&"O] ?E*+,m=NPVr,   r   r   c                    t         j                  j                  d      }d}d\  }}|j                  ||      j	                  |      |z  }|j                  ||      j	                  |      |z  }t        ||g      \  }	}
|t        u rd}i }i }n"dt        j                  |      z  }|}d|i}ddi} |j                  |||f| dd|\  }} |j                  |	|
|f| dd|\  }}t        ||f   ||||fi | y	)
zACheck that the results do not depend on the datasets writability.r   r   )   rf   rf   rw   r   T)r   r  N)
r"   r#   r$   r'   r   r   r   logr   r  )r   r   r   r(   r  r  r   rg   rh   X_mmY_mmr
  r  r  rw   r   r   dist_mm
indices_mms                      r*   test_memmap_backed_datarN    sY    ))


"CF#IzJ'..u5>AJ'..u5>A +Aq62JD$W	 rvvj))	$f-,d3.J..		  Hk -*,,  GZ :u%&';
6Fr,   c                 \   t         j                  j                  |       }d}|j                  g d      }|j                  g d      }|j                  g d      }|j	                  ||      j                  |      |z  } ||      }	t         j                  j                  |d      dz  }
t        ||      }t        |	|      }t        |
|       t        |
|       t        j                  t              5  t        j                  |      }t        ||       d d d        y # 1 sw Y   y xY w)	Nr   )r   r   r   r2  )r   rf   r   )rR   r   r   rR   rm   r   r   )r"   r#   r$   r  r'   r   linalgnormr   r   r   r   r   r   )r  r   r   r(   r  r  r   r   rg   r   sq_row_norm_referencesq_row_normsq_row_norm_csrs                r*   test_sqeuclidean_row_normsrU    s     ))

 2
3CF

/0IL)J**Y'KJ'..u5>A!EIINN11N5:'{CK+E{KO);7)?;	z	" :a a[9: : :s   6#D""D+c            
      t   t         j                  j                  d      } | j                  dd      }| j                  dd      }d}d}d}| j	                  ddd      }t        j
                  |      }t        j                  |||||||d	
      }t        j                  |||||||d
      }	t        ||	       y )NrR   r   rf   r   r   r   r   r   r%  )rg   rh   r   r   r   r   r   r&  r'  )	r"   r#   r$   r'   r   r   r   r   r   )
r(   rg   rh   r   r   r   r   r   	results_X	results_Ys
             r*   *test_argkmin_classmode_strategy_consistentrY  )  s    
))


"CbAbA	AFG{{qr{4Hii)O ((


' 	I !((


' 	I y),r,   r   )Nr   r   r   r   c                 x   t         j                  j                  d      }|j                  dd      }|j                  dd      }d}d}d}|j	                  ddd      }t        j
                  |      }t        j                  |||||||| d	
	      }	t        j                  |||||||| d
	      }
t        |	|
       y )NrR   r   rf   r   r   r   r   r   r%  )	rg   rh   rw   r   r   r   r   r   r&  r'  )	r"   r#   r$   r'   r   r   r   r   r   )r   r(   rg   rh   rw   r   r   r   r   rW  rX  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 Iy)r,   )rR   )rM   ư>)TrM   r\  )r   )rf   )r   r   rf   )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   re   rt   r{   FLOAT32_TOLSFLOAT64_TOLSr   r   r  r   markparametrizer   r   r   r   r   r   r  r  r   r/  r<  rF  rN  rU  rY  r[   r,   r*   <module>rm     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,   