#NEWLINE# def count_neighbors( self , other , r , p = 2.0 ) : #NEWLINE# #INDENT# '\n        Count how many nearby pairs can be formed.\n\n        Count the number of pairs (x1,x2) can be formed, with x1 drawn\n        from self and x2 drawn from `other`, and where\n        ``distance(x1, x2, p) <= r``.\n        This is the "two-point correlation" described in Gray and Moore 2000,\n        "N-body problems in statistical learning", and the code here is based\n        on their algorithm.\n\n        Parameters\n        ----------\n        other : KDTree instance\n            The other tree to draw points from.\n        r : float or one-dimensional array of floats\n            The radius to produce a count for. Multiple radii are searched with\n            a single tree traversal.\n        p : float, 1<=p<=infinity, optional\n            Which Minkowski p-norm to use\n\n        Returns\n        -------\n        result : int or 1-D array of ints\n            The number of pairs. Note that this is internally stored in a numpy\n            int, and so may overflow if very large (2e9).\n\n        ' #NEWLINE# def traverse( node1 , rect1 , node2 , rect2 , idx ) : #NEWLINE# #INDENT# min_r = rect1 . min_distance_rectangle ( rect2 , p ) #NEWLINE# max_r = rect1 . max_distance_rectangle ( rect2 , p ) #NEWLINE# c_greater = ( r [ idx ] > max_r ) #NEWLINE# result [ idx [ c_greater ] ] += ( node1 . children * node2 . children ) #NEWLINE# idx = idx [ ( ( min_r <= r [ idx ] ) & ( r [ idx ] <= max_r ) ) ] #NEWLINE# if ( len ( idx ) == 0 ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# if isinstance ( node1 , KDTree . leafnode ) : #NEWLINE# #INDENT# if isinstance ( node2 , KDTree . leafnode ) : #NEWLINE# #INDENT# ds = minkowski_distance ( self . data [ node1 . idx ] [ : , np . newaxis , : ] , other . data [ node2 . idx ] [ np . newaxis , : , : ] , p ) . ravel ( ) #NEWLINE# ds . sort ( ) #NEWLINE# result [ idx ] += np . searchsorted ( ds , r [ idx ] , side = 'right' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( less , greater ) = rect2 . split ( node2 . split_dim , node2 . split ) #NEWLINE# traverse ( node1 , rect1 , node2 . less , less , idx ) #NEWLINE# traverse ( node1 , rect1 , node2 . greater , greater , idx ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( node2 , KDTree . leafnode ) : #NEWLINE# #INDENT# ( less , greater ) = rect1 . split ( node1 . split_dim , node1 . split ) #NEWLINE# traverse ( node1 . less , less , node2 , rect2 , idx ) #NEWLINE# traverse ( node1 . greater , greater , node2 , rect2 , idx ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( less1 , greater1 ) = rect1 . split ( node1 . split_dim , node1 . split ) #NEWLINE# ( less2 , greater2 ) = rect2 . split ( node2 . split_dim , node2 . split ) #NEWLINE# traverse ( node1 . less , less1 , node2 . less , less2 , idx ) #NEWLINE# traverse ( node1 . less , less1 , node2 . greater , greater2 , idx ) #NEWLINE# traverse ( node1 . greater , greater1 , node2 . less , less2 , idx ) #NEWLINE# traverse ( node1 . greater , greater1 , node2 . greater , greater2 , idx ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# R1 = Rectangle ( self . maxes , self . mins ) #NEWLINE# R2 = Rectangle ( other . maxes , other . mins ) #NEWLINE# if ( np . shape ( r ) == ( ) ) : #NEWLINE# #INDENT# r = np . array ( [ r ] ) #NEWLINE# result = np . zeros ( 1 , dtype = int ) #NEWLINE# traverse ( self . tree , R1 , other . tree , R2 , np . arange ( 1 ) ) #NEWLINE# return result [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( np . shape ( r ) ) == 1 ) : #NEWLINE# #INDENT# r = np . asarray ( r ) #NEWLINE# ( n , ) = r . shape #NEWLINE# result = np . zeros ( n , dtype = int ) #NEWLINE# traverse ( self . tree , R1 , other . tree , R2 , np . arange ( n ) ) #NEWLINE# return result #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( 'r must be either a single value or a one-dimensional array of values' )