code: def get_threshold_mask_and_sort(
      mention_dists,
      cand_dists,
      biencoder_dists,
      valid_cands_mask,
      threshold,
      top_mention_sort=True,
  ):
      """
      top_mention_sort:
          True: sort top candidates per mention only
              scores_mask and sorted_idxs has dim (#_valid_examples,)
          False: sort ALL candidates (assumes multiple candidates per mention)
              scores_mask and sorted_idxs has dim (#_valid_examples, #_cands)
      """
      mention_scores = mention_dists[valid_cands_mask]
      if len(mention_scores.shape) > 1:
          mention_scores = mention_scores[:, 0]
      scores = torch.log_softmax(
          torch.tensor(cand_dists[valid_cands_mask]), 1
      ) + torch.sigmoid(torch.tensor(mention_scores)).log().unsqueeze(
          -1
      )
      if top_mention_sort:
          scores_mask = scores[:, 0] > threshold
          # sort...
          _, sorted_idxs = scores[:, 0][scores_mask].sort(
              descending=True
          )
          sorted_filtered_scores = scores[scores_mask][sorted_idxs]
      else:
          scores_mask = scores > threshold  # GRAPHQUESTIONS BEST
          sorted_filtered_scores, sorted_idxs = scores[
              scores_mask
          ].sort(descending=True)
      return (
          scores_mask.numpy(),
          sorted_idxs.numpy(),
          sorted_filtered_scores.numpy(),
      )
  
file_path: BLINK/scripts/tune_hyperparams_new.py
imports: import json
  import os
  import numpy
  import torch
  from elq.vcg_utils.measures import entity_linking_tp_with_overlap
  from tqdm import tqdm
  from transformers import BertTokenizer
label_desc: Use image node_id 5 for calling a global function with example usage: get_threshold_mask_and_sort(mention_dists, cand_dists, biencoder_dists, valid_cands_mask, threshold, top_mention_sort) and returns: scores_mask,  sorted_idxs,  sorted_filtered_scores