code: def range_PR_multiple_thresholds(
      lims_ref,
      Iref,
      lims_new,
      Dnew,
      Inew,
      thresholds,
      mode="overall",
      do_sort="ref,new",
  ):
      """compute precision-recall values for range search results
      for several thresholds on the "new" results.
      This is to plot PR curves
      """
      # ref should be sorted by ids
      if "ref" in do_sort:
          Iref = sort_range_res_1(lims_ref, Iref)
  
      # new should be sorted by distances
      if "new" in do_sort:
          Inew, Dnew = sort_range_res_2(lims_new, Dnew, Inew)
  
      def ref_result_for(i):
          return Iref[lims_ref[i] : lims_ref[i + 1]]
  
      def new_result_for(i):
          l0, l1 = lims_new[i], lims_new[i + 1]
          return Inew[l0:l1], Dnew[l0:l1]
  
      nq = lims_ref.size - 1
      assert lims_new.size - 1 == nq
  
      nt = len(thresholds)
      counts = np.zeros((nq, nt, 3), dtype="int64")
  
      def compute_PR_for(q):
          gt_ids = ref_result_for(q)
          res_ids, res_dis = new_result_for(q)
  
          counts[q, :, 0] = len(gt_ids)
  
          if res_dis.size == 0:
              # the rest remains at 0
              return
  
          # which offsets we are interested in
          nres = np.searchsorted(res_dis, thresholds)
          counts[q, :, 1] = nres
  
          if gt_ids.size == 0:
              return
  
          # find number of TPs at each stage in the result list
          ii = np.searchsorted(gt_ids, res_ids)
          ii[ii == len(gt_ids)] = -1
          n_ok = np.cumsum(gt_ids[ii] == res_ids)
  
          # focus on threshold points
          n_ok = np.hstack(([0], n_ok))
          counts[q, :, 2] = n_ok[nres]
  
      pool = ThreadPool(20)
      pool.map(compute_PR_for, range(nq))
      # print(counts.transpose(2, 1, 0))
  
      precisions = np.zeros(nt)
      recalls = np.zeros(nt)
      for t in range(nt):
          p, r = counts_to_PR(
              counts[:, t, 0],
              counts[:, t, 1],
              counts[:, t, 2],
              mode=mode,
          )
          precisions[t] = p
          recalls[t] = r
  
      return precisions, recalls
  
imports: import numpy
  import unittest
  import time
  import faiss
  from multiprocessing.pool import ThreadPool
label_desc: Use image node_id 7 for calling a global function with example usage: range_PR_multiple_thresholds(lims_ref, Iref, lims_new, Dnew, Inew, thresholds, mode, do_sort) and returns: precisions,  recalls, Iref, Inew,  Dnew