code: def adjacency(dist, idx):
      """Return adjacency matrix of a kNN graph"""
      M, k = dist.shape
      assert M, k == idx.shape
      assert dist.min() >= 0
      assert dist.max() <= 1
  
      # Pairwise distances
      sigma2 = np.mean(dist[:, -1]) ** 2
      dist = np.exp(-(dist**2) / sigma2)
  
      # Weight matrix
      I = np.arange(0, M).repeat(k)
      J = idx.reshape(M * k)
      V = dist.reshape(M * k)
      W = scipy.sparse.coo_matrix((V, (I, J)), shape=(M, M))
  
      # No self-connections
      W.setdiag(0)
  
      # Undirected graph
      bigger = W.T > W
      W = W - W.multiply(bigger) + W.T.multiply(bigger)
  
      assert W.nnz % 2 == 0
      assert np.abs(W - W.T).mean() < 1e-10
      assert type(W) is scipy.sparse.csr.csr_matrix
      return W
  
file_path: dgl/examples/pytorch/model_zoo/geometric/grid_graph.py
imports: import numpy
  import scipy.sparse
  import scipy.sparse.linalg
  import sklearn
  import sklearn.metrics
label_desc: Use image node_id 4 for calling a global function with example usage: adjacency(dist, idx) and returns: W