code: def density_to_peaks(dists, nbrs, density, max_conn=1, sort="dist"):
      # Note that dists has been sorted in ascending order
      assert dists.shape[0] == density.shape[0]
      assert dists.shape == nbrs.shape
  
      num, _ = dists.shape
      dist2peak = {i: [] for i in range(num)}
      peaks = {i: [] for i in range(num)}
  
      for i, nbr in tqdm(enumerate(nbrs)):
          nbr_conf = density[nbr]
          for j, c in enumerate(nbr_conf):
              nbr_idx = nbr[j]
              if i == nbr_idx or c <= density[i]:
                  continue
              dist2peak[i].append(dists[i, j])
              peaks[i].append(nbr_idx)
              if len(dist2peak[i]) >= max_conn:
                  break
  
      return dist2peak, peaks
  
imports: from itertools import groupby
  import numpy
  import torch
  from tqdm import tqdm
label_desc: Use image node_id 3 for calling a global function with example usage: density_to_peaks(dists, nbrs, density, max_conn, sort) and returns: dist2peak,  peaks