
    PiE                        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d;dededefdZd Z d Z!	 	 d<dZ"ddddddddZ#	 	 	 d=dZ$dddZ%dddZ&eej'        f ee"fi e&eej(        f ee"fi e%eej'        f ee$fi e&eej(        f ee$fi e%iZ)d Z*ej+        ,                    dddg          d             Z-ej+        ,                    d e          d!             Z.d" Z/d# Z0d$ Z1d% Z2ej+        ,                    d&eeg          ej+        ,                    d'ej'        ej(        g          	 d>d)                        Z3ej+        ,                    d&eeg          ej+        ,                    d'ej'        ej(        g          	 d>d*                        Z4ej+        ,                    d+eej'        feej(        feej(        feej'        fg          ej+        ,                    d e          d,                         Z5ej+        ,                    d&eeg          	 d?d-            Z6ej+        ,                    de          ej+        ,                    d.d/          ej+        ,                    d'ej'        ej(        g          ej+        ,                    d e          	 	 	 d@d1                                                Z7ej+        ,                    de          ej+        ,                    d.d/          ej+        ,                    d'ej'        ej(        g          	 	 dAd2                                    Z8ej+        ,                    d&eeg          ej+        ,                    dd3d4g          ej+        ,                    d'ej'        ej(        g          d5                                     Z9ej+        ,                    d'ej'        ej(        g          ej+        ,                    d e          d6                         Z:d7 Z;ej+        ,                    d8g d9          d:             Z<dS )B    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           
         t           j                            |          }| dk    rmt          d          t          d          t          d          t          t           j                  t          d|                    |                    g}|S | dk    r$t          |                    |                    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        /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/sklearn/metrics/tests/test_pairwise_distances_reduction.py_get_metric_params_listr,   +   s     )


%
%C3KKK1III1III26NNN1,,---
  sxx
++,,,-- 4K    c                    t          t          ||                    }t          t          ||                    }t          |                              t          |                    }	|	D ][}
||
         }||
         }	 t	          ||||           '# t
          $ r(}t          d|  d|
 d| d| d| d| d          |d	}~ww xY wd	S )
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/   r0   indices_to_dist_aindices_to_dist_bcommon_indicesidxdist_adist_bes                 r+   *assert_same_distances_for_common_neighborsrC   D   s4   " S
;;<<S
;;<<''44S5G5GHHN  "3'"3'	FFDAAAAA 		 		 		 !!9 ! !36! !!! !.4! !BF! ! ! ! ! 
 			 s   ?B
C#C  Cc                     ||k     }||k     }t          j        ||         |          }t          j        ||         |          }	t          |	          dk    st          |          dk    r%t          d|  d|	 d| d| d| d| d| d	          d
S )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   r1   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lenr6   )
r7   r8   r9   r:   r;   	thresholdmask_amask_bmissing_from_bmissing_from_as
             r+   assert_no_missing_neighborsrM   j   s    ( )#F)#F\-"7GGN\-"7GGN
>Q#n"5"5"9"9/y / /.</ /.</ / %/ / %	/ /
 +/ / +/ / /
 
 	
 #:"9r-   h㈵>ư>c           
      "   d }| j         |j         cxk    r|j         cxk    r|j         k    sn J d            | j         \  }}t          |          D ]}	| |	         }
||	         }||	         }||	         } ||
          sJ d|	              ||          sJ d|	             t          |	|
|||||           d|z
  t          j        t          j        |
          t          j        |                    z  |z
  }t          |	|
||||           dS )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    t          j        | d d         | dd          k              S Nr   r#   allas    r+   <lambda>z3assert_compatible_argkmin_results.<locals>.<lambda>   $    "&3B31QRR511 r-   z+Arrays of results have incompatible shapes.Distances aren't sorted on row r   N)shaperangerC   r#   maximummaxrM   )neighbors_dists_aneighbors_dists_bneighbors_indices_aneighbors_indices_br/   r0   	is_sorted	n_queries_r7   r8   r9   r:   r;   rH   s                  r+   !assert_compatible_argkmin_resultsrf      s   * 21I 	"	% 	% 	% 	%$	% 	% 	% 	% $	% 	% 	% 	% 	% 5		% 	% 	% %*LIq 9%% &
 &
	&y1
&y1
+I6+I6y$$SS&S	&S&SSS$y$$SS&S	&S&SSS$2	
 	
 	
& XF:z 2 2"
 "
 
	 	$	
 	
 	
 	
?&
 &
r-   
   )XYr   precomputed_distsexpected_n_neighborsn_subsampled_queriesc                     ||
J d            || J |J t          | |fd|i|}n|d |                                         }|                    d           |d d |f                                         S )Nz4Either metric or precomputed_dists must be provided.r   r   axis)r   copysortmean)rh   ri   r   rj   rk   rl   metric_kwargssampled_distss           r+   _non_trivial_radiusru      s     (F,>,>> -?,>>  }}}}}}*1aPPP-PP)*?+?*?@EEGGA00166888r-   Tc           
      ~   d }t          |           t          |          cxk    r't          |          cxk    rt          |          k    sn J t          |           }	t          |	          D ]M}
| |
         }||
         }||
         }||
         }|r0 ||          sJ d|
              ||          sJ d|
             t          |          t          |          k    sJ t          |          t          |          k    sJ t          |          dk    r-t          j        |          }||k    sJ d| d| d|
             t          |          dk    r-t          j        |          }||k    sJ d| d| d|
             t	          |
||||||           d|z
  |z  |z
  }t          |
|||||           OdS )	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    t          j        | d d         | dd          k              S rR   rT   rV   s    r+   rX   z2assert_compatible_radius_results.<locals>.<lambda>  rY   r-   rZ   r   zLargest returned distance z not within requested radius z on row r   N)rG   r\   r#   r^   rC   rM   )r_   r`   ra   rb   radiuscheck_sortedr/   r0   rc   rd   r7   r8   r9   r:   r;   
max_dist_a
max_dist_brH   s                     r+    assert_compatible_radius_resultsr|      s   0 21I 	 !!	$ 	$ 	$ 	$"##	$ 	$ 	$ 	$ "##	$ 	$ 	$ 	$ 	$ 	$ %&&I 9%% -
 -
	&y1
&y1
+I6+I6 	X9Z((WW*WI*W*WWW(9Z((WW*WI*W*WWW(:#m"4"44444:#m"4"44444 z??Q
++J'''7Z 7 7!7 7+47 7 ('' z??Q
++J'''7Z 7 7!7 7+47 7 (''
 	3	
 	
 	
 X'$.	#	
 	
 	
 	
M-
 -
r-   Hz>r0   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            n# 1 swxY w Y   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            n# 1 swxY w Y   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            n# 1 swxY w Y   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            n# 1 swxY w Y   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 S # 1 swxY w Y   d S ) Nr}           r~   r          ?ffffff@333333?      @r   r   r   r                   	   rg   )r   r   r   r   r    g      @)r   r   r    r   r   )r   r   r   rg   r   r   r   r   r   rg   )"      r         )*   r         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   r   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   r   r   r   r   zDneighbors in b missing from a: [5]
neighbors in a missing from b: [])r   r   r    r   r   )r   r   r   r    r    Distances aren't sorted on row 0r   r   r   r   r    )	r&   r#   arrayrf   reescapepytestraisesr6   )r0   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x#uc5)#q#s#	
 H (OO	
 K &(Kd   &
3UC/011
3UC/011
///"##
///"##	 
    &
3S#u-.//
3S%-.//
///"##
///"##	 
    &
33S)*++
1aAs#$%%
"""#$$
"""#$$	 
    &
33S)*++
31a%&''
%%%&''
$$$%&&	 
    )	- C 
~S	1	1	1 
 
)HsCU3455HsCU3455Hooo&''Hooo&''		
 	

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

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

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

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

 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s_   A+K

KKA+M;;M?M?5A+P,,P03P0&A+SS!$S!A+U==VVry   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            n# 1 swxY w 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| 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            n# 1 swxY w Y   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            n# 1 swxY w Y   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            n# 1 swxY w Y   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            n# 1 swxY w Y   | 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            d S # 1 swxY w Y   d S 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 S )#Nr}   r   r~   r    r   r   r   r   r   r   )r   r   r   r   g      @)rx   ry   )r   r   r   r   r    r   r   r   r   r   )r   r   r    r   r   r   )r   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   )r   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   )r   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   r6   )ry   r0   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9:;;
"(CeS%899:;;
"(???++,--
"(???++,--	
 !     %
"(Cac233455
"(Cac233455
"(+++,,-..
"(+++,,-..	
 !     )	- C 
~S	1	1	1 	
 	
(HbhS%e<==>??HbhS%e<==>??Hbh//011Hbh//011		

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

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

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

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

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

 	
 	
 	
 	
 	
 	
 	
sr   8B6K::K>K>)B0R%%R),R)B0VV"VB6ZZZ=B6]??^^)B6a,,a03a0csr_containerc                 ~   t           j                            d          }|                    dd          }|                    dd          } | |          } | |          }d}t	          j        |||          sJ t	          j        |||          sJ t	          j        |||          sJ t	          j        |||          sJ t	          j        |                    t           j                  |                    t           j                  |          sJ t	          j        |                    t           j                  |                    t           j                  |          sJ t	          j        |                    t           j	                  |                    t           j	                  |          rJ t	          j        ||d          rJ t	          j        |                    t           j                  ||          rJ t	          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                            t           j	                  |_        t	          j        |||          rJ d S )	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     sw   
)


"
"CbAbAM!EM!EF ,9!QGGGGG+9%OOOOO+9%FKKKKK+9!UFKKKKK+9	ahhrz22F     ,9	ahhrz22F     0=	AHHRX..     0=a8TTTTTT/=	a     0=	188BHv    
 0=
!a     ,9%;WWWWWW+9	5      0=u]      0=u[       -A&&K/=k1fUUUUU  -""K%-44RX>>K/=k1fUUUUUUUr-   c                     t           j                            d          } |                     dd          }|                     dd          }d}d}d}t	          j        t          |          5  t          j        |	                    t           j
                  |||           d d d            n# 1 swxY w Y   d	}t	          j        t          |          5  t          j        ||	                    t           j                  ||           d d d            n# 1 swxY w Y   t	          j        t          d
          5  t          j        ||d|           d d d            n# 1 swxY w Y   t	          j        t          d          5  t          j        ||d|           d d d            n# 1 swxY w Y   t	          j        t          d          5  t          j        |||d           d d d            n# 1 swxY w Y   t	          j        t          d          5  t          j        t          j        ddg          |||           d d d            n# 1 swxY w Y   t	          j        t          d          5  t          j        t          j        |          |||           d d d            n# 1 swxY w Y   ddi}d}t	          j        t          |          5  t          j        |||||           d d d            n# 1 swxY w Y   dt!          |d          d}d}t	          j        t          |          5  t          j        |||||           d d d            n# 1 swxY w Y   dt!          |d          i}t#          j                    5  t#          j        dt                     t          j        |||||           d d d            n# 1 swxY w Y   t!          |d          t!          |d          d}t#          j                    5  t#          j        dt                     t          j        |||||           d d d            d S # 1 swxY w Y   d S ) Nr   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.rS   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	-	-	- I I!((2:..!qHHHHI I I I I I I I I I I I I I I	1  
z	-	-	- G G!qxx11QvFFFFG G G G G G G G G G G G G G G 
z)A	B	B	B 7 7!qBv66667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 
z)@	A	A	A 6 6!qAf55556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 
z)>	?	?	? > >!qAn====> > > > > > > > > > > > > > > 
X
 
 
 I I 	"(C:..!qHHHHI I I I I I I I I I I I I I I
 
z)F	G	G	G I I"+A..!qHHHHI I I I I I I I I I I I I I I  8EG	k	1	1	1 
 
1&8L	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 /qAAA M
 QG	k	1	1	1 S S!qAfMRRRRS S S S S S S S S S S S S S S
 	/qAAAM 
	 	"	" S Sg<<<<!qAfMRRRRS S S S S S S S S S S S S S S 0qAAA/qAAA M 
	 	"	" S Sg<<<<!qAfMRRRRS S S S S S S S S S S S S S S S S Ss   -6B//B36B36DD D?E$$E(+E(
F//F36F3G::G>G> -II I?+J66J:=J:"LLLM**M.1M.5OO #O 5QQ#&Q#c            
         t           j                            d          } |                     dd          }|                     dd          }d}d}d}|                     ddd          }t          j        |          }d	}t          j        t          |
          5  t          j
        |                    t           j                  ||||||           d d d            n# 1 swxY w Y   d}t          j        t          |
          5  t          j
        ||                    t           j                  |||||           d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        ||d||||           d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        ||d||||           d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        |||d|||           d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        t          j        ddg          ||||||           d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        t          j        |          ||||||           d d d            n# 1 swxY w Y   d}	d|	 d}
t          j        t          |

          5  t          j
        |||||	||           d d d            d S # 1 swxY w Y   d S )Nr   r   rg   r   r   uniformr   lowhighsizer   r   )rh   ri   r   r   weightsY_labelsunique_Y_labelsr   r   rS   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{44Hi))O	3  
z	-	-	- 	
 	
 hhrz""+	
 	
 	
 	
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
	1  
z	-	-	- 	
 	
 hhrx  +	
 	
 	
 	
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 
z)A	B	B	B 	
 	
 +	
 	
 	
 	
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 
z)@	A	A	A 	
 	
 +	
 	
 	
 	
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 
z)>	?	?	? 	
 	
 !+	
 	
 	
 	
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 
X
 
 
 
 
 	 hSz""+	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
z)F	G	G	G 	
 	
 ""+	
 	
 	
 	
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 %D!	;6	; 	; 	;  
z	1	1	1 	
 	
 1+	
 	
 	
 	
	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
s   9C  C$'C$9EEE3FF"FG))G-0G-H77H;>H;0JJ J?.K99K= K='MMMc                  B   t           j                            d          } |                     dd          }|                     dd          }d}d}d}t	          j        t          |          5  t          j        |	                    t           j
                  |||           d d d            n# 1 swxY w Y   d	}t	          j        t          |          5  t          j        ||	                    t           j                  ||           d d d            n# 1 swxY w Y   t	          j        t          d
          5  t          j        ||d|           d d d            n# 1 swxY w Y   t	          j        t          d          5  t          j        |||d           d d d            n# 1 swxY w Y   t	          j        t          d          5  t          j        t          j        ddg          |||           d d d            n# 1 swxY w Y   t	          j        t          d          5  t          j        t          j        |          |||           d d d            n# 1 swxY w Y   ddi}d}t	          j        t          |          5  t          j        |||||           d d d            n# 1 swxY w Y   dt!          |d          d}d}t	          j        t          |          5  t          j        |||||           d d d            n# 1 swxY w Y   t!          |d          t!          |d          d}t#          j                    5  t#          j        dt                     t          j        |||||           d d d            n# 1 swxY w Y   dt!          |d          i}t#          j                    5  t#          j        dt                     t          j        |||||           d d d            d S # 1 swxY w Y   d S )Nr   r   rg   r   r   r   r   )rh   ri   rx   r   r   radius == -1.0, must be >= 0.rS   r   r   r   r   r   r   r   r    r   )rh   ri   rx   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   rx   r   r   r   r   rs   s	            r+   1test_radius_neighbors_factory_method_wrong_usagesr     s   
)


"
"CbAbAFF	3  

 
 
 
 
 	hhrz""av	
 	
 	
 	
	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
	1  

 
 
 Y Y 	!qxx'9'9&QWXXXX	Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 
z)H	I	I	I D D!qFCCCCD D D D D D D D D D D D D D D 
z)>	?	?	? P P!qOOOOP P P P P P P P P P P P P P P 
X
 
 
 
 
 	hSz""av	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
z)F	G	G	G 
 
""av	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 

  8 FG	k	1	1	1 
 
1VFBV	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 /qAAA M
 QG	k	1	1	1 
 
1VF-	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 0qAAA/qAAA M 
	 	"	" 
 
g<<<<1VF-	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 	/qAAAM 
	 	"	" 
 
g<<<<1VF-	
 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   -6B//B36B36DD D?E$$E(+E(
F//F36F3-HHH4+I++I/2I/J==KK9LL#&L# 5N!!N%(N%5PPPc                  ,   t           j                            d          } |                     dd          }|                     dd          }d}d}d}|                     ddd          }t          j        |          }d	}t          j        t          |
          5  t          j
        |                    t           j                  ||||||d            d d d            n# 1 swxY w Y   d}t          j        t          |
          5  t          j
        ||                    t           j                  |||||d            d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        ||d||||d            d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        ||dd|||d            d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        t          j        ddg          ||||||d            d d d            n# 1 swxY w Y   t          j        t          d
          5  t          j
        t          j        |          ||||||d            d d d            n# 1 swxY w Y   d}	d|	 d}t          j        t          |
          5  t          j
        |||d|	||d            d d d            d S # 1 swxY w Y   d S )Nr   r   rg   r   r   r   r   r   r   r   )rh   ri   rx   r   r   r   r   outlier_labelr   r   rS   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   rx   r   r   r   r   r   r   s
             r+   ;test_radius_neighbors_classmode_factory_method_wrong_usagesr     sN   
)


"
"CbAbAFFG{{qr{44Hi))O	3  
z	-	-	- 

 

 (hhrz""+		
 		
 		
 		


 

 

 

 

 

 

 

 

 

 

 

 

 

 

	1  
z	-	-	- 

 

 (hhrx  +		
 		
 		
 		


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
z)H	I	I	I 

 

 (+		
 		
 		
 		


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
z)>	?	?	? 

 

 (!+		
 		
 		
 		


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
X
 
 
 
 
 	!(hSz""+		
 		
 		
 		

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
z)F	G	G	G 

 

 (""+		
 		
 		
 		


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 %D!	;6	; 	; 	;  
z	-	-	- 

 

 (!1+		
 		
 		
 		


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

s~   :C!!C%(C%	:EEE5FF"%F"G--G14G11III6/J11J58J5L		LL
Dispatcherdtyper   c                    t           j                            |           }d}|                    g ddd          \  }}|                    ||                              |          |z  }|                    ||                              |          |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 | dS )z3Check that results do not depend on the chunk size.r   a   r   e     r   Fr   replacerg   r   rh   ri   r   rx   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_parametersrx   r   r   distr   s                     r+   test_chunk_size_agnosticismr  _  s    )

 2
3
3CF"zz*=*=*=AuzUUKj))0077&@Aj))0077&@AW	$qAkBBB	$f-,d3.J.		    Hk 'J&		    MD' :u%&$W 0@    r-   c                    t           j                            |           }|                    g ddd          \  }}d}|                    ||                              |          |z  }|                    ||                              |          |z  }	|t          u rd}
i }i }nt          ||	d          }|}
d	|i}d
di} |j        ||	|
fddd|\  }}t                      
                    dd          5   |j        ||	|
fddd|\  }}ddd           n# 1 swxY w Y   t          ||f         ||||fi | dS )z:Check that results do not depend on the number of threads.r   r   Fr   r   rg   r   r   rx   r   T   r  r  r   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  rx   r   r   r  r   s                     r+   test_n_threads_agnosticismr    s    )

 2
3
3C"zz*=*=*=AuzUUKFj))0077&@Aj))0077&@AW	$qAkBBB	$f-,d3.J.		    Hk 
$	%	%	+	+1x	+	H	H 
 
*
*
  
 
 !
 
g
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 :u%&$W 0@    s   <DD#&D#zDispatcher, dtypec                 ^   t           j                            |           }d}d\  }}|                    ||                              |          |z  }|                    ||                              |          |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 | =dS )zLCheck that results do not depend on the format (dense, sparse) of the input.r   )r   r   rg   r   r   rx   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  rx   
dist_denseindices_dense_X_Yr  r   s                         r+   test_format_agnosticismr!    s   " )

 2
3
3CF$IzJ''..u55>AJ''..u55>AM!EM!EW	 %qAkBBB	$f-,d3 2
 2		! ! ! ! !J #QJE
;; 
 
B77rQww*
*
  
 
 !
 
g 	z5)*		
 	

 	
 	
 	
 	

 
r-   c           	      n   t           j                            |           }|                    t          j        g dt
                              }|                    g ddd          \  }}d}|                    ||                              |          |z  }	|                    ||                              |          |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 | d	S )z:Check that the results do not depend on the strategy used.)r   r   r   	haversiner   r   r   Fr   r   r#  Nrg   r   rx   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  rx   
dist_par_Xindices_par_X
dist_par_Yindices_par_Ys                      r+   test_strategies_consistencyr0    s    )

 2
3
3CZZ
   	
 	
 	

 
F  #zz*=*=*=AuzUUKFj))00>>GAj))00>>GA  111bqb5** 111bqb5**W	$qAf===	$f-,d3 2
 2		! -J%7
 
 

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

 !# ! ! ! !J  :|,-J} @P    r-   r'  )r&  r(  r   c                 @   t           j                            |           }|                    ddg          }	|                    ddg          }
d}|
|                    ||	                              |          |z  z   }|
|                    ||	                              |          |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 ]F\  }}t#          j        |||||d|dz  |          \  }}t'          t"          |f         ||||           Gd S )Nr  r   r       .A  r#  r   r   r   r   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  su    )

 2
3
3CRI&&J**aX&&KFchhy*55<<UCCfLLAchhy*55<<UCCfLLAM!EM!E  111bqb5** 111bqb5**+FJ??BM )!Q//AqAAA=AA*[q999!!!RaR%@H%8%>bjQQQ,21566 
 
)4(11*
g&& #QJE
;; 
 
B-4_'  A~
.
 
.
 
.
*? 	w&'!		
 	
 	
 	

 
r-   c                    t           j                            |           }|                    ddg          }|                    ddg          }d}	||                    ||                              |          |	z  z   }
||                    ||                              |          |	z  z   }t          |||           d         }|dk    rt          |
|          }nt          |
|fd|i|}t          |	          }g }g }|D ]}t          j
        |j        d                   ||k             }||         }t          j        |          }||         ||         }}|                    |           |                    |           t          j        |
||||d
|dz  |d
	  	        \  }}t!          t          |f         |||||           d S )Nr  r   r   r2  r3  r%  r   r   )rj   Tr   )r   rs   r  r  r'  r   )r#   r$   r%   r  r(   r   r,   r   r   ru   aranger[   r4  appendr
   r   r  )r  r   r'  r   rd   r  r)   r   r6  r  rh   ri   rs   r7  rx   neigh_indices_refneigh_distances_refrowindr  rq   neigh_distancesneigh_indicess                          r+   (test_pairwise_distances_radius_neighborsrG    s    )

 2
3
3CRI&&J**aX&&KFchhy*55<<UCCfLLAchhy*55<<UCCfLLA+
!3  	M
 )!Q//AqAAA=AA ;???F  ) )i	!%%cVm43xz$ItDzT  %%%""4((((%4%<		#>& & &"O] ?E*+,m=NPV    r-   r   r   c                    t           j                            d          }d}d\  }}|                    ||                              |          |z  }|                    ||                              |          |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 | d	S )
zACheck that the results do not depend on the datasets writability.r   r   )   rg   rg   rx   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  rx   r   r   dist_mm
indices_mms                      r+   test_memmap_backed_datarO    s    )


"
"CF#IzJ''..u55>AJ''..u55>A +Aq622JD$W	 rvj)))	$f-,d3.J.		    Hk -*,    GZ :u%&';
 6F    r-   c                    t           j                            |           }d}|                    g d          }|                    g d          }|                    g d          }|                    ||                              |          |z  } ||          }	t           j                            |d          dz  }
t          ||          }t          |	|          }t          |
|           t          |
|           t          j        t                    5  t          j        |          }t          ||           d d d            d S # 1 swxY w Y   d S )	Nr   )r   r   r   r3  )r   rg   r   )r   r   r   r   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_normsrV    s    )

 2
3
3CF

///00ILLL))J**YYY''KJ''..u55>AM!EINN11N55:'{CCCK+E{KKKO);777)?;;;	z	"	" : :a  a[9999: : : : : : : : : : : : : : : : : :s   &EEEc            
         t           j                            d          } |                     dd          }|                     dd          }d}d}d}|                     ddd          }t          j        |          }t          j        |||||||d	
          }t          j        |||||||d
          }	t          ||	           d S )Nr   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_consistentrZ  )  s    
)


"
"CbAbA	AFG{{qr{44Hi))O (


' 	 	 	I !(


' 	 	 	I y),,,,,r-   r   )Nr   r    r   r   c                    t           j                            d          }|                    dd          }|                    dd          }d}d}d}|                    ddd          }t          j        |          }t          j        |||||||| d	
	  	        }	t          j        |||||||| d
	  	        }
t          |	|
           d S )Nr   r   rg   r   r   r   r   r   r&  )	rh   ri   rx   r   r   r   r   r   r'  r(  )	r#   r$   r%   r(   r   r   r   r   r   )r   r)   rh   ri   rx   r   r   r   r   rX  rY  s              r+   3test_radius_neighbors_classmode_strategy_consistentr\  J  s    
)


"
"CbAbAFFG{{qr{44Hi))O(0

'# 
 
 
I )0

'# 
 
 
I Iy)))))r-   )r   )rN   rO   )TrN   rO   )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,   rC   rM   rf   ru   r|   FLOAT32_TOLSFLOAT64_TOLSr   r   r  r   markparametrizer   r   r   r   r   r   r  r  r!  r0  r=  rG  rO  rV  rZ  r\   r-   r+   <module>rm     sH       				             ( ( ( ( ( ( C C C C C C C C                        
 / . . . . . = = = = = =5 5 5 1 C S     2# # #L!
 !
 !
R 
	G
 G
 G
 G
X 
9 9 9 9 9F 		Q
 Q
 Q
 Q
j  
  
 bj77#DUUUUbj77#DUUUU

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

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