code: def text_similarity_avg_ranks(preds, labels):
      """
      Calculates average predicted rank of positive sequence(context/passage) for each sample/query
  
      :param preds: list of numpy arrays of dimension n1 x n2 containing n2 predicted ranks for n1 sequences/queries
      :type preds: List of numpy array containing similarity scores for each sequence in batch
      :param labels: list of arrays of dimension n1 x n2 where each array contains n2 labels(0/1) indicating whether the sequence/passage is a positive(1) passage or hard_negative(0) passage
      :type labels: List of list containing values(0/1)
  
      :return: average predicted ranks of positive sequence/passage for each sample/query
      """
      positive_idx_per_question = list(
          reduce(
              lambda x, y: x + list((y == 1).nonzero()[0]), labels, []
          )
      )
      rank = 0
      for i, idx in enumerate(positive_idx_per_question):
          # aggregate the rank of the known gold passage in the sorted results for each question
          gold_idx = (preds[i] == idx).nonzero()[0]
          rank += gold_idx.item()
      return float(rank / len(preds))
  
imports: import logging
  import numpy
  import torch
  from functools import reduce
  from scipy.stats import pearsonr, spearmanr
  from seqeval.metrics import classification_report
  from seqeval.metrics import f1_score
  from sklearn.metrics import matthews_corrcoef, f1_score, mean_squared_error, r2_score, classification_report
  from farm.utils import flatten_list
label_desc: Use image node_id 19 for calling a global function with example usage: text_similarity_avg_ranks(preds, labels) and returns: float