code: def get_knn_index(
      data,
      method,
      k,
      metric,
      metric_params=None,
      n_jobs=1,
      random_state=None,
      verbose=False,
  ):
      # If we're dealing with a precomputed distance matrix, our job is very easy,
      # so we can skip all the remaining checks
      if metric == "precomputed":
          return nearest_neighbors.PrecomputedDistanceMatrix(data, k=k)
  
      preferred_approx_method = nearest_neighbors.Annoy
      if is_package_installed("pynndescent") and (
          sp.issparse(data)
          or metric
          not in [
              "cosine",
              "euclidean",
              "manhattan",
              "hamming",
              "dot",
              "l1",
              "l2",
              "taxicab",
          ]
      ):
          preferred_approx_method = nearest_neighbors.NNDescent
  
      if data.shape[0] < 1000:
          preferred_method = nearest_neighbors.Sklearn
      else:
          preferred_method = preferred_approx_method
  
      methods = {
          "exact": nearest_neighbors.Sklearn,
          "auto": preferred_method,
          "approx": preferred_approx_method,
          "annoy": nearest_neighbors.Annoy,
          "pynndescent": nearest_neighbors.NNDescent,
          "hnsw": nearest_neighbors.HNSW,
      }
      if isinstance(method, nearest_neighbors.KNNIndex):
          knn_index = method
  
      elif method not in methods:
          raise ValueError(
              "Unrecognized nearest neighbor algorithm `%s`. Please choose one "
              "of the supported methods or provide a valid `KNNIndex` instance."
              % method
          )
      else:
          knn_index = methods[method](
              data=data,
              k=k,
              metric=metric,
              metric_params=metric_params,
              n_jobs=n_jobs,
              random_state=random_state,
              verbose=verbose,
          )
  
      return knn_index
  
imports: import logging
  import numbers
  import operator
  from functools import reduce
  import numpy
  import scipy.sparse
  from openTSNE import _tsne
  from openTSNE import nearest_neighbors
  from openTSNE import utils
  from openTSNE.utils import is_package_installed
  import warnings
label_desc: Use image node_id 1 for calling a global function with example usage: get_knn_index(data, method, k, metric, metric_params, n_jobs, random_state, verbose) and returns: knn_index, nearest_neighbors