class_docstr: kd-tree for quick nearest-neighbor lookup
  
  This class provides an index into a set of k-dimensional points which
  can be used to rapidly look up the nearest neighbors of any point.
  
  Parameters
  ----------
  data : (N,K) array_like
      The data points to be indexed. This array is not copied, and
      so modifying this data will result in bogus results.
  leafsize : int, optional
      The number of points at which the algorithm switches over to
      brute-force.  Has to be positive.
  
  Raises
  ------
  RuntimeError
      The maximum recursion limit can be exceeded for large data
      sets.  If this happens, either increase the value for the `leafsize`
      parameter or increase the recursion limit by::
  
          >>> import sys
          >>> sys.setrecursionlimit(10000)
  
  See Also
  --------
  cKDTree : Implementation of `KDTree` in Cython
  
  Notes
  -----
  The algorithm used is described in Maneewongvatana and Mount 1999.
  The general idea is that the kd-tree is a binary tree, each of whose
  nodes represents an axis-aligned hyperrectangle. Each node specifies
  an axis and splits the set of points based on whether their coordinate
  along that axis is greater than or less than a particular value.
  
  During construction, the axis and splitting point are chosen by the
  "sliding midpoint" rule, which ensures that the cells do not all
  become long and thin.
  
  The tree can be queried for the r closest neighbors of any given point
  (optionally returning only those within some maximum distance of the
  point). It can also be queried, with a substantial gain in efficiency,
  for the r approximate closest neighbors.
  
  For large dimensions (20 is already large) do not expect this to run
  significantly faster than brute force. High-dimensional nearest-neighbor
  queries are a substantial open problem in computer science.
  
  The tree also supports all-neighbors queries, both with arrays of points
  and with other kd-trees. These do use a reasonably efficient algorithm,
  but the kd-tree is not necessarily the best data structure for this
  sort of calculation.
code: def __query(self, x, k=1, eps=0, p=2, distance_upper_bound=np.inf):
      side_distances = np.maximum(
          0, np.maximum(x - self.maxes, self.mins - x)
      )
      if p != np.inf:
          side_distances **= p
          min_distance = np.sum(side_distances)
      else:
          min_distance = np.amax(side_distances)
  
      # priority queue for chasing nodes
      # entries are:
      #  minimum distance between the cell and the target
      #  distances between the nearest side of the cell and the target
      #  the head node of the cell
      q = [(min_distance, tuple(side_distances), self.tree)]
      # priority queue for the nearest neighbors
      # furthest known neighbor first
      # entries are (-distance**p, i)
      neighbors = []
  
      if eps == 0:
          epsfac = 1
      elif p == np.inf:
          epsfac = 1 / (1 + eps)
      else:
          epsfac = 1 / (1 + eps) ** p
  
      if p != np.inf and distance_upper_bound != np.inf:
          distance_upper_bound = distance_upper_bound**p
  
      while q:
          min_distance, side_distances, node = heappop(q)
          if isinstance(node, KDTree.leafnode):
              # brute-force
              data = self.data[node.idx]
              ds = minkowski_distance_p(data, x[np.newaxis, :], p)
              for i in range(len(ds)):
                  if ds[i] < distance_upper_bound:
                      if len(neighbors) == k:
                          heappop(neighbors)
                      heappush(neighbors, (-ds[i], node.idx[i]))
                      if len(neighbors) == k:
                          distance_upper_bound = -neighbors[0][0]
          else:
              # we don't push cells that are too far onto the queue at all,
              # but since the distance_upper_bound decreases, we might get
              # here even if the cell's too far
              if min_distance > distance_upper_bound * epsfac:
                  # since this is the nearest cell, we're done, bail out
                  break
              # compute minimum distances to the children and push them on
              if x[node.split_dim] < node.split:
                  near, far = node.less, node.greater
              else:
                  near, far = node.greater, node.less
  
              # near child is at the same distance as the current node
              heappush(q, (min_distance, side_distances, near))
  
              # far child is further by an amount depending only
              # on the split value
              sd = list(side_distances)
              if p == np.inf:
                  min_distance = max(
                      min_distance, abs(node.split - x[node.split_dim])
                  )
              elif p == 1:
                  sd[node.split_dim] = np.abs(
                      node.split - x[node.split_dim]
                  )
                  min_distance = (
                      min_distance
                      - side_distances[node.split_dim]
                      + sd[node.split_dim]
                  )
              else:
                  sd[node.split_dim] = (
                      np.abs(node.split - x[node.split_dim]) ** p
                  )
                  min_distance = (
                      min_distance
                      - side_distances[node.split_dim]
                      + sd[node.split_dim]
                  )
  
              # far child might be too far, if so, don't bother pushing it
              if min_distance <= distance_upper_bound * epsfac:
                  heappush(q, (min_distance, tuple(sd), far))
  
      if p == np.inf:
          return sorted([(-d, i) for (d, i) in neighbors])
      else:
          return sorted(
              [((-d) ** (1.0 / p), i) for (d, i) in neighbors]
          )
  
imports: from __future__ import division, print_function, absolute_import
  import sys
  import numpy
  from heapq import heappush, heappop
  import scipy.sparse
label_desc: Use image node_id 3 for calling the KDTree obj's underlying member method code with example usage: obj.__query(x, k, eps, p, distance_upper_bound) and returns: sorted, sorted