class_docstr: Tests for identifying inverse relationships and leakage.
class_docstr_tok: ["Tests","for","identifying","inverse","relationships","and","leakage","."]
docstr: Test finding test leakages with an asymmetric metric.
docstr_tok: ["Test","finding","test","leakages","with","an","asymmetric","metric","."]
code: def test_find_leak_assymetric(self):
      """Test finding test leakages with an asymmetric metric."""
      n = 100
      min_frequency = 0.97
      test_relation, test_relation_inverse = "r", "r_inverse"
  
      train_generated = list(
          itt.chain.from_iterable(
              (
                  [
                      [str(i), test_relation, str(j + 1 + n)],
                      [str(j + 1 + n), test_relation_inverse, str(i)],
                  ]
                  for i, j in zip(range(n), range(n))
              )
          )
      )
      train_non_inverses = [
          ["a", "fine", "b"],
          ["b", "fine", "c"],
      ]
      forwards_extras = [
          ["-1", test_relation, "-2"],  # this one leaks!
          ["-3", test_relation, "-4"],
      ]
      inverse_extras = [
          ["-5", test_relation_inverse, "-6"],
      ]
      train = (
          train_generated
          + train_non_inverses
          + forwards_extras
          + inverse_extras
      )
      test = [
          ["-2", test_relation_inverse, "-1"],  # this one was leaked!
      ]
      train_factory = TriplesFactory.from_labeled_triples(
          triples=np.array(train, dtype=np.str),
          filter_out_candidate_inverse_relations=False,
      )
      test_factory = TriplesFactory.from_labeled_triples(
          triples=np.array(test, dtype=np.str),
          entity_to_id=train_factory.entity_to_id,
          relation_to_id=train_factory.relation_to_id,
          filter_out_candidate_inverse_relations=False,
      )
  
      expected_forwards_frequency = n / (n + len(forwards_extras))
      expected_inverse_frequency = n / (n + len(inverse_extras))
      # expected_frequency = n / (n + len(forwards_extras) + len(inverse_extras))
      # self.assertLessEqual(min_frequency, expected_frequency)
  
      self.assertGreater(len(forwards_extras), len(inverse_extras))
      self.assertLess(
          expected_forwards_frequency,
          expected_inverse_frequency,
          msg="Forwards frequency should be higher than inverse frequency",
      )
  
      sealant = Sealant(
          train_factory,
          symmetric=False,
          minimum_frequency=min_frequency,
      )
      test_relation_id, test_relation_inverse_id = [
          train_factory.relation_to_id[r]
          for r in (test_relation, test_relation_inverse)
      ]
      self.assertNotEqual(
          0,
          len(sealant.candidate_inverse_relations),
          msg=f"did not find any candidate inverse relations at frequency>={min_frequency}",
      )
      self.assertEqual(
          {
              (
                  test_relation_id,
                  test_relation_inverse_id,
              ): expected_forwards_frequency,
              (
                  test_relation_inverse_id,
                  test_relation_id,
              ): expected_inverse_frequency,
          },
          dict(sealant.candidate_inverse_relations),
      )
  
      self.assertIn(test_relation_id, sealant.inverses)
      self.assertEqual(
          test_relation_inverse_id, sealant.inverses[test_relation]
      )
      self.assertIn(test_relation_inverse_id, sealant.inverses)
      self.assertEqual(
          test_relation, sealant.inverses[test_relation_inverse_id]
      )
  
      self.assertIn(
          test_relation_inverse_id,
          sealant.inverse_relations_to_delete,
          msg="The wrong relation was picked for deletion",
      )
  
      # Test looking up inverse triples
      test_leaked = test_factory.mapped_triples[
          test_factory.get_mask_for_relations(
              relations=sealant.inverse_relations_to_delete,
              invert=False,
          )
      ]
      self.assertEqual(1, len(test_leaked))
      self.assertEqual(
          (
              train_factory.entity_to_id["-2"],
              test_relation_inverse,
              train_factory.entity_to_id["-1"],
          ),
          tuple(test_leaked[0]),
      )
  
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 1 for calling the TestLeakage obj's underlying member method code with example usage: obj.test_find_leak_assymetric() without return types