code: def compute_ap(ranks, nres):
      """
      Computes average precision for given ranked indexes.
  
      Args:
          ranks: zero-based ranks of positive images
          nres: number of positive images
  
      Returns:
          ap (float): average precision
      """
  
      # number of images ranked by the system
      nimgranks = len(ranks)
  
      # accumulate trapezoids in PR-plot
      ap = 0
  
      recall_step = 1.0 / nres
  
      for j in np.arange(nimgranks):
          rank = ranks[j]
  
          if rank == 0:
              precision_0 = 1.0
          else:
              precision_0 = float(j) / rank
  
          precision_1 = float(j + 1) / (rank + 1)
  
          ap += (precision_0 + precision_1) * recall_step / 2.0
  
      return ap
  
imports: import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: compute_ap(ranks, nres) and returns: ap