class_docstr: Tests for identifying inverse relationships and leakage.
class_docstr_tok: ["Tests","for","identifying","inverse","relationships","and","leakage","."]
docstr: Test :func:`get_candidate_pairs`.
docstr_tok: ["Test",":","func",":","`","get_candidate_pairs","`","."]
code: def test_candidate_pairs(self):
      """Test :func:`get_candidate_pairs`."""
      num_entities = 11
      num_pairs = 100
      h, t = torch.randint(num_entities, size=(2, num_pairs))
      r = torch.zeros_like(h)
  
      # base relation
      base = torch.stack([h, r, t], dim=-1)
  
      # exact duplicate
      dup100 = torch.stack([h, r + 1, t], dim=-1)
  
      # 99% duplicate
      dup099 = torch.stack([h, r + 2, t], dim=-1)
      dup099[99:, [0, 2]] += 1
  
      # 50% duplicate
      dup050 = torch.stack([h, r + 3, t], dim=-1)
      dup050[50:, [0, 2]] += 1
  
      # exact inverse
      inv100 = torch.stack([t, r + 4, h], dim=-1)
  
      # 99% inverse
      inv099 = torch.stack([t, r + 5, h], dim=-1)
      inv099[99:, [0, 2]] += 1
  
      triples = torch.cat(
          [base, dup100, dup099, dup050, inv100, inv099], dim=0
      )
  
      rel, inv = mapped_triples_to_sparse_matrices(
          triples,
          num_relations=6,
      )
      assert rel.max() == 1 and inv.max() == 1
      candidate_pairs = get_candidate_pairs(a=rel, threshold=0.97)
      expected_candidate_pairs = {
          (0, 1),
          (0, 2),
          (1, 0),
          (1, 2),
          (2, 0),
          (2, 1),
          (4, 5),
          (5, 4),
      }
      self.assertEqual(expected_candidate_pairs, candidate_pairs)
  
      candidate_pairs = get_candidate_pairs(
          a=rel, b=inv, threshold=0.97
      )
      expected_candidate_pairs = {
          (0, 4),
          (0, 5),
          (1, 4),
          (1, 5),
          (2, 4),
          (2, 5),
          (4, 0),
          (4, 1),
          (4, 2),
          (5, 0),
          (5, 1),
          (5, 2),
      }
      self.assertEqual(expected_candidate_pairs, candidate_pairs)
  
file_path: pykeen/tests/test_leakage.py
imports: import itertools
  import unittest
  import numpy
  import numpy
  import scipy.sparse
  import torch
  from pykeen.datasets import Nations
  from pykeen.triples import TriplesFactory
  from pykeen.triples.leakage import Sealant, _generate_compact_vectorized_lookup, _translate_triples, get_candidate_pairs, jaccard_similarity_scipy, mapped_triples_to_sparse_matrices, triples_factory_to_sparse_matrices
label_desc: Use image node_id 6 for calling the TestLeakage obj's underlying member method code with example usage: obj.test_candidate_pairs() without return types