code: def perturb_and_get_filtered_rank(
      emb, w, s, r, o, test_size, triplets_to_filter, filter_o=True
  ):
      """Perturb subject or object in the triplets"""
      num_nodes = emb.shape[0]
      ranks = []
      for idx in tqdm.tqdm(range(test_size), desc="Evaluate"):
          target_s = s[idx]
          target_r = r[idx]
          target_o = o[idx]
          candidate_nodes = filter(
              triplets_to_filter,
              target_s,
              target_r,
              target_o,
              num_nodes,
              filter_o=filter_o,
          )
          if filter_o:
              emb_s = emb[target_s]
              emb_o = emb[candidate_nodes]
          else:
              emb_s = emb[candidate_nodes]
              emb_o = emb[target_o]
          target_idx = 0
          emb_r = w[target_r]
          emb_triplet = emb_s * emb_r * emb_o
          scores = torch.sigmoid(torch.sum(emb_triplet, dim=1))
  
          _, indices = torch.sort(scores, descending=True)
          rank = int((indices == target_idx).nonzero())
          ranks.append(rank)
      return torch.LongTensor(ranks)
  
imports: import dgl
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import tqdm
  from dgl.data.knowledge_graph import FB15k237Dataset
  from dgl.dataloading import GraphDataLoader
  from dgl.nn.pytorch import RelGraphConv
label_desc: Use image node_id 3 for calling a global function with example usage: perturb_and_get_filtered_rank(emb, w, s, r, o, test_size, triplets_to_filter, filter_o) and returns: torch