code: def fast_knns2spmat(knns, k, th_sim=0, use_sim=True, fill_value=None):
      # convert knns to symmetric sparse matrix
      from scipy.sparse import csr_matrix
  
      eps = 1e-5
      n = len(knns)
      if isinstance(knns, list):
          knns = np.array(knns)
      if len(knns.shape) == 2:
          # knns saved by hnsw has different shape
          n = len(knns)
          ndarr = np.ones([n, 2, k])
          ndarr[:, 0, :] = -1  # assign unknown dist to 1 and nbr to -1
          for i, (nbr, dist) in enumerate(knns):
              size = len(nbr)
              assert size == len(dist)
              ndarr[i, 0, :size] = nbr[:size]
              ndarr[i, 1, :size] = dist[:size]
          knns = ndarr
      nbrs = knns[:, 0, :]
      dists = knns[:, 1, :]
      assert (
          -eps <= dists.min() <= dists.max() <= 1 + eps
      ), "min: {}, max: {}".format(dists.min(), dists.max())
      if use_sim:
          sims = 1.0 - dists
      else:
          sims = dists
      if fill_value is not None:
          print("[fast_knns2spmat] edge fill value:", fill_value)
          sims.fill(fill_value)
      row, col = np.where(sims >= th_sim)
      # remove the self-loop
      idxs = np.where(row != nbrs[row, col])
      row = row[idxs]
      col = col[idxs]
      data = sims[row, col]
      col = nbrs[row, col]  # convert to absolute column
      assert len(row) == len(col) == len(data)
      spmat = csr_matrix((data, (row, col)), shape=(n, n))
      return spmat
  
file_path: dgl/examples/pytorch/hilander/utils/knn.py
imports: import math
  import multiprocessing
  import os
  import numpy
  from tqdm import tqdm
  from utils import Timer
  from .faiss_search import faiss_search_knn
label_desc: Use image node_id 2 for calling a global function with example usage: fast_knns2spmat(knns, k, th_sim, use_sim, fill_value) and returns: spmat