Scipy spatial
![scipy spatial scipy spatial](https://pysal.org/pysal/_static/images/nonplanar.png)
argsort ( sqd, kind = 'mergesort' ) idx = idx return zip ( sqd, lidx ) def search_kdtree ( tree, datapoint, K ): """ find the k nearest neighbours of datapoint in a kdtree """ stack = ] knn = * K _datapt = datapoint while stack : leaf_idx, leaf_data, left_hrect, \ sum ( axis = 0 ) # data.reshape((param,1)).repeat(ndata, axis=1) idx = numpy. shape K = K if K < ndata else ndata retval = sqd = (( ldata - data ) ** 2 ). sum () < r2 def quadratic_knn_search ( data, lidx, ldata, K ): """ find K nearest neighbours of data among ldata """ ndata = ldata. copy () idx = p maxval p = maxval return (( p - centroid ) ** 2 ). #!python numbers=disable def intersect ( hrect, r2, centroid ): """ checks if the hyperrectangle hrect intersects with the hypersphere defined by centroid and r2 """ maxval = hrect minval = hrect p = centroid. append (( None, None, left_hrect, right_hrect, None, None )) return tree copy () left_hrect = splitval right_hrect = splitval # append node to tree tree. copy () else : left_hrect = _right_hrect. append (( data, didx, depth + 1, nodeptr, False )) splitval = data if leftbranch : left_hrect = _left_hrect. append (( data, didx, depth + 1, nodeptr, True )) stack. argsort ( data, kind = 'mergesort' ) data = data didx = didx nodeptr = len ( tree ) stack. append ( leaf ) # not a leaf, split the data in two else : splitdim = depth % ndim idx = numpy. copy () leaf = ( _didx, _data, None, None, 0, 0 ) tree.
Scipy spatial update#
shape nodeptr = len ( tree ) # update parent node _didx, _data, _left_hrect, _right_hrect, left, right = tree tree = ( _didx, _data, _left_hrect, _right_hrect, nodeptr, right ) if leftbranch \Įlse ( _didx, _data, _left_hrect, _right_hrect, left, nodeptr ) # insert node in kd-tree # leaf node? if ndata <= leafsize : _didx = didx. copy () left_hrect = splitval right_hrect = splitval tree = stack =, idx, 1, 0, True ), ( data, idx, 1, 0, False )] # recursively split data in halves using hyper-rectangles: while stack : # pop data off stack data, didx, depth, parent, leftbranch = stack. argsort ( data, kind = 'mergesort' ) data = data splitval = data left_hrect = hrect. max ( axis = 1 ) # create root of kd-tree idx = numpy. shape # find bounding hyper-rectangle hrect = numpy. number of data points to leave in a leaf output: kd-tree: list of tuples """ ndim = data. #!python numbers=disable # Copyleft 2008 Sturla Molden # University of Oslo #import psyco #psyco.full() import numpy def kdtree ( data, leafsize = 10 ): """ build a kd-tree for O(n log n) nearest neighbour search input: data: 2D ndarray, shape =(ndim,ndata), preferentially C order leafsize: max.