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 : array_like, shape (n,m)
      The n data points of dimension m to be indexed. This array is
      not copied unless this is necessary to produce a contiguous
      array of doubles, and so modifying this data will result in
      bogus results. The data are also copied if the kd-tree is built
      with copy_data=True.
  leafsize : positive int, optional
      The number of points at which the algorithm switches over to
      brute-force.  Default: 10.
  compact_nodes : bool, optional
      If True, the kd-tree is built to shrink the hyperrectangles to
      the actual data range. This usually gives a more compact tree that
      is robust against degenerated input data and gives faster queries
      at the expense of longer build time. Default: True.
  copy_data : bool, optional
      If True the data is always copied to protect the kd-tree against
      data corruption. Default: False.
  balanced_tree : bool, optional
      If True, the median is used to split the hyperrectangles instead of
      the midpoint. This usually gives a more compact tree and
      faster queries at the expense of longer build time. Default: True.
  boxsize : array_like or scalar, optional
      Apply a m-d toroidal topology to the KDTree.. The topology is generated
      by :math:`x_i + n_i L_i` where :math:`n_i` are integers and :math:`L_i`
      is the boxsize along i-th dimension. The input data shall be wrapped
      into :math:`[0, L_i)`. A ValueError is raised if any of the data is
      outside of this bound.
  
  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.
  
  Attributes
  ----------
  data : ndarray, shape (n,m)
      The n data points of dimension m to be indexed. This array is
      not copied unless this is necessary to produce a contiguous
      array of doubles. The data are also copied if the kd-tree is built
      with `copy_data=True`.
  leafsize : positive int
      The number of points at which the algorithm switches over to
      brute-force.
  m : int
      The dimension of a single data-point.
  n : int
      The number of data points.
  maxes : ndarray, shape (m,)
      The maximum value in each dimension of the n data points.
  mins : ndarray, shape (m,)
      The minimum value in each dimension of the n data points.
  size : int
      The number of nodes in the tree.
docstr: Query the kd-tree for nearest neighbors.
  
  Parameters
  ----------
  x : array_like, last dimension self.m
      An array of points to query.
  k : int or Sequence[int], optional
      Either the number of nearest neighbors to return, or a list of the
      k-th nearest neighbors to return, starting from 1.
  eps : nonnegative float, optional
      Return approximate nearest neighbors; the kth returned value
      is guaranteed to be no further than (1+eps) times the
      distance to the real kth nearest neighbor.
  p : float, 1<=p<=infinity, optional
      Which Minkowski p-norm to use.
      1 is the sum-of-absolute-values distance ("Manhattan" distance).
      2 is the usual Euclidean distance.
      infinity is the maximum-coordinate-difference distance.
      A large, finite p may cause a ValueError if overflow can occur.
  distance_upper_bound : nonnegative float, optional
      Return only neighbors within this distance. This is used to prune
      tree searches, so if you are doing a series of nearest-neighbor
      queries, it may help to supply the distance to the nearest neighbor
      of the most recent point.
  workers : int, optional
      Number of workers to use for parallel processing. If -1 is given
      all CPU threads are used. Default: 1.
  
      .. versionadded:: 1.6.0
  
  Returns
  -------
  d : float or array of floats
      The distances to the nearest neighbors.
      If ``x`` has shape ``tuple+(self.m,)``, then ``d`` has shape
      ``tuple+(k,)``.
      When k == 1, the last dimension of the output is squeezed.
      Missing neighbors are indicated with infinite distances.
      Hits are sorted by distance (nearest first).
  
      .. deprecated:: 1.6.0
         If ``k=None``, then ``d`` is an object array of shape ``tuple``,
         containing lists of distances. This behavior is deprecated and
         will be removed in SciPy 1.8.0, use ``query_ball_point``
         instead.
  
  i : integer or array of integers
      The index of each neighbor in ``self.data``.
      ``i`` is the same shape as d.
      Missing neighbors are indicated with ``self.n``.
  
  Examples
  --------
  
  >>> import numpy as np
  >>> from scipy.spatial import KDTree
  >>> x, y = np.mgrid[0:5, 2:8]
  >>> tree = KDTree(np.c_[x.ravel(), y.ravel()])
  
  To query the nearest neighbours and return squeezed result, use
  
  >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=1)
  >>> print(dd, ii, sep='\n')
  [2.         0.2236068]
  [ 0 13]
  
  To query the nearest neighbours and return unsqueezed result, use
  
  >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[1])
  >>> print(dd, ii, sep='\n')
  [[2.        ]
   [0.2236068]]
  [[ 0]
   [13]]
  
  To query the second nearest neighbours and return unsqueezed result,
  use
  
  >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[2])
  >>> print(dd, ii, sep='\n')
  [[2.23606798]
   [0.80622577]]
  [[ 6]
   [19]]
  
  To query the first and second nearest neighbours, use
  
  >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=2)
  >>> print(dd, ii, sep='\n')
  [[2.         2.23606798]
   [0.2236068  0.80622577]]
  [[ 0  6]
   [13 19]]
  
  or, be more specific
  
  >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[1, 2])
  >>> print(dd, ii, sep='\n')
  [[2.         2.23606798]
   [0.2236068  0.80622577]]
  [[ 0  6]
   [13 19]]
docstr_tok: ["Query","the","kd-tree","for","nearest","neighbors",".","Parameters","--","--","--","--","--","x",":","array_like",",","last","dimension","self.m","An","array","of","points","to","query",".","k",":","int","or","Sequence","[","int","]",",","optional","Either","the","number","of","nearest","neighbors","to","return",",","or","a","list","of","the","k-th","nearest","neighbors","to","return",",","starting","from","1.","eps",":","nonnegative","float",",","optional","Return","approximate","nearest","neighbors",";","the","kth","returned","value","is","guaranteed","to","be","no","further","than","(","1+eps",")","times","the","distance","to","the","real","kth","nearest","neighbor",".","p",":","float",",","1","<","=p","<","=infinity",",","optional","Which","Minkowski","p-norm","to","use",".","1","is","the","sum-of-absolute-values","distance","(","``","Manhattan","''","distance",")",".","2","is","the","usual","Euclidean","distance",".","infinity","is","the","maximum-coordinate-difference","distance",".","A","large",",","finite","p","may","cause","a","ValueError","if","overflow","can","occur",".","distance_upper_bound",":","nonnegative","float",",","optional","Return","only","neighbors","within","this","distance",".","This","is","used","to","prune","tree","searches",",","so","if","you","are","doing","a","series","of","nearest-neighbor","queries",",","it","may","help","to","supply","the","distance","to","the","nearest","neighbor","of","the","most","recent","point",".","workers",":","int",",","optional","Number","of","workers","to","use","for","parallel","processing",".","If","-1","is","given","all","CPU","threads","are","used",".","Default",":","1",".","..","versionadded",":",":","1.6.0","Returns","--","--","--","-","d",":","float","or","array","of","floats","The","distances","to","the","nearest","neighbors",".","If","``","x","``","has","shape","``","tuple+","(","self.m",",",")","``",",","then","``","d","``","has","shape","``","tuple+","(","k",",",")","``",".","When","k","==","1",",","the","last","dimension","of","the","output","is","squeezed",".","Missing","neighbors","are","indicated","with","infinite","distances",".","Hits","are","sorted","by","distance","(","nearest","first",")",".","..","deprecated",":",":","1.6.0","If","``","k=None","``",",","then","``","d","``","is","an","object","array","of","shape","``","tuple","``",",","containing","lists","of","distances",".","This","behavior","is","deprecated","and","will","be","removed","in","SciPy","1.8.0",",","use","``","query_ball_point","``","instead",".","i",":","integer","or","array","of","integers","The","index","of","each","neighbor","in","``","self.data","``",".","``","i","``","is","the","same","shape","as","d.","Missing","neighbors","are","indicated","with","``","self.n","``",".","Examples","--","--","--","--",">",">",">","import","numpy","as","np",">",">",">","from","scipy.spatial","import","KDTree",">",">",">","x",",","y","=","np.mgrid","[","0:5",",","2:8","]",">",">",">","tree","=","KDTree","(","np.c_","[","x.ravel","(",")",",","y.ravel","(",")","]",")","To","query","the","nearest","neighbours","and","return","squeezed","result",",","use",">",">",">","dd",",","ii","=","tree.query","(","[","[","0",",","0","]",",","[","2.2",",","2.9","]","]",",","k=1",")",">",">",">","print","(","dd",",","ii",",","sep='\\n","'",")","[","2",".","0.2236068","]","[","0","13","]","To","query","the","nearest","neighbours","and","return","unsqueezed","result",",","use",">",">",">","dd",",","ii","=","tree.query","(","[","[","0",",","0","]",",","[","2.2",",","2.9","]","]",",","k=","[","1","]",")",">",">",">","print","(","dd",",","ii",",","sep='\\n","'",")","[","[","2",".","]","[","0.2236068","]","]","[","[","0","]","[","13","]","]","To","query","the","second","nearest","neighbours","and","return","unsqueezed","result",",","use",">",">",">","dd",",","ii","=","tree.query","(","[","[","0",",","0","]",",","[","2.2",",","2.9","]","]",",","k=","[","2","]",")",">",">",">","print","(","dd",",","ii",",","sep='\\n","'",")","[","[","2.23606798","]","[","0.80622577","]","]","[","[","6","]","[","19","]","]","To","query","the","first","and","second","nearest","neighbours",",","use",">",">",">","dd",",","ii","=","tree.query","(","[","[","0",",","0","]",",","[","2.2",",","2.9","]","]",",","k=2",")",">",">",">","print","(","dd",",","ii",",","sep='\\n","'",")","[","[","2",".","2.23606798","]","[","0.2236068","0.80622577","]","]","[","[","0","6","]","[","13","19","]","]","or",",","be","more","specific",">",">",">","dd",",","ii","=","tree.query","(","[","[","0",",","0","]",",","[","2.2",",","2.9","]","]",",","k=","[","1",",","2","]",")",">",">",">","print","(","dd",",","ii",",","sep='\\n","'",")","[","[","2",".","2.23606798","]","[","0.2236068","0.80622577","]","]","[","[","0","6","]","[","13","19","]","]"]
code: def query(
      self, x, k=1, eps=0, p=2, distance_upper_bound=np.inf, workers=1
  ):
      r"""Query the kd-tree for nearest neighbors.
  
      Parameters
      ----------
      x : array_like, last dimension self.m
          An array of points to query.
      k : int or Sequence[int], optional
          Either the number of nearest neighbors to return, or a list of the
          k-th nearest neighbors to return, starting from 1.
      eps : nonnegative float, optional
          Return approximate nearest neighbors; the kth returned value
          is guaranteed to be no further than (1+eps) times the
          distance to the real kth nearest neighbor.
      p : float, 1<=p<=infinity, optional
          Which Minkowski p-norm to use.
          1 is the sum-of-absolute-values distance ("Manhattan" distance).
          2 is the usual Euclidean distance.
          infinity is the maximum-coordinate-difference distance.
          A large, finite p may cause a ValueError if overflow can occur.
      distance_upper_bound : nonnegative float, optional
          Return only neighbors within this distance. This is used to prune
          tree searches, so if you are doing a series of nearest-neighbor
          queries, it may help to supply the distance to the nearest neighbor
          of the most recent point.
      workers : int, optional
          Number of workers to use for parallel processing. If -1 is given
          all CPU threads are used. Default: 1.
  
          .. versionadded:: 1.6.0
  
      Returns
      -------
      d : float or array of floats
          The distances to the nearest neighbors.
          If ``x`` has shape ``tuple+(self.m,)``, then ``d`` has shape
          ``tuple+(k,)``.
          When k == 1, the last dimension of the output is squeezed.
          Missing neighbors are indicated with infinite distances.
          Hits are sorted by distance (nearest first).
  
          .. deprecated:: 1.6.0
             If ``k=None``, then ``d`` is an object array of shape ``tuple``,
             containing lists of distances. This behavior is deprecated and
             will be removed in SciPy 1.8.0, use ``query_ball_point``
             instead.
  
      i : integer or array of integers
          The index of each neighbor in ``self.data``.
          ``i`` is the same shape as d.
          Missing neighbors are indicated with ``self.n``.
  
      Examples
      --------
  
      >>> import numpy as np
      >>> from scipy.spatial import KDTree
      >>> x, y = np.mgrid[0:5, 2:8]
      >>> tree = KDTree(np.c_[x.ravel(), y.ravel()])
  
      To query the nearest neighbours and return squeezed result, use
  
      >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=1)
      >>> print(dd, ii, sep='\n')
      [2.         0.2236068]
      [ 0 13]
  
      To query the nearest neighbours and return unsqueezed result, use
  
      >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[1])
      >>> print(dd, ii, sep='\n')
      [[2.        ]
       [0.2236068]]
      [[ 0]
       [13]]
  
      To query the second nearest neighbours and return unsqueezed result,
      use
  
      >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[2])
      >>> print(dd, ii, sep='\n')
      [[2.23606798]
       [0.80622577]]
      [[ 6]
       [19]]
  
      To query the first and second nearest neighbours, use
  
      >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=2)
      >>> print(dd, ii, sep='\n')
      [[2.         2.23606798]
       [0.2236068  0.80622577]]
      [[ 0  6]
       [13 19]]
  
      or, be more specific
  
      >>> dd, ii = tree.query([[0, 0], [2.2, 2.9]], k=[1, 2])
      >>> print(dd, ii, sep='\n')
      [[2.         2.23606798]
       [0.2236068  0.80622577]]
      [[ 0  6]
       [13 19]]
  
      """
      x = np.asarray(x)
      if x.dtype.kind == "c":
          raise TypeError("KDTree does not work with complex data")
  
      if k is None:
          # k=None, return all neighbors
          warnings.warn(
              "KDTree.query with k=None is deprecated and will be removed "
              "in SciPy 1.8.0. Use KDTree.query_ball_point instead.",
              DeprecationWarning,
          )
  
          # Convert index query to a lists of distance and index,
          # sorted by distance
          def inds_to_hits(point, neighbors):
              dist = minkowski_distance(point, self.data[neighbors], p)
              hits = sorted([(d, i) for d, i in zip(dist, neighbors)])
              return [d for d, i in hits], [i for d, i in hits]
  
          x = np.asarray(x, dtype=np.float64)
          inds = super().query_ball_point(
              x, distance_upper_bound, p, eps, workers
          )
  
          if isinstance(inds, list):
              return inds_to_hits(x, inds)
  
          dists = np.empty_like(inds)
          for idx in np.ndindex(inds.shape):
              dists[idx], inds[idx] = inds_to_hits(x[idx], inds[idx])
  
          return dists, inds
  
      d, i = super().query(x, k, eps, p, distance_upper_bound, workers)
      if isinstance(i, int):
          i = np.intp(i)
      return d, i
  
imports: import numpy
  import warnings
  from .ckdtree import cKDTree, cKDTreeNode
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, workers) and returns: d,  i, dists,  inds, unknown,  unknown, inds_to_hits