class_docstr: Class for testing triples factories.
class_docstr_tok: ["Class","for","testing","triples","factories","."]
docstr: Run the actual test for new_with_restriction.
docstr_tok: ["Run","the","actual","test","for","new_with_restriction","."]
code: def _test_restriction(
      self,
      original_triples_factory: TriplesFactory,
      entity_restriction: Optional[Collection[str]],
      invert_entity_selection: bool,
      relation_restriction: Optional[Collection[str]],
      invert_relation_selection: bool,
  ):
      """Run the actual test for new_with_restriction."""
      # apply restriction
      restricted_triples_factory = (
          original_triples_factory.new_with_restriction(
              entities=entity_restriction,
              relations=relation_restriction,
              invert_entity_selection=invert_entity_selection,
              invert_relation_selection=invert_relation_selection,
          )
      )
  
      # check that the triples factory is returned as is, if and only if no restriction is to apply
      no_restriction_to_apply = (
          entity_restriction is None and relation_restriction is None
      )
      equal_factory_object = id(restricted_triples_factory) == id(
          original_triples_factory
      )
      assert no_restriction_to_apply == equal_factory_object
  
      # check that inverse_triples is correctly carried over
      assert (
          original_triples_factory.create_inverse_triples
          == restricted_triples_factory.create_inverse_triples
      )
  
      # verify that the label-to-ID mapping has not been changed
      assert (
          original_triples_factory.entity_to_id
          == restricted_triples_factory.entity_to_id
      )
      assert (
          original_triples_factory.relation_to_id
          == restricted_triples_factory.relation_to_id
      )
  
      # verify that triples have been filtered
      if entity_restriction is not None:
          present_entities = set(
              restricted_triples_factory.triples[:, 0]
          ).union(restricted_triples_factory.triples[:, 2])
          expected_entities = (
              set(
                  original_triples_factory.entity_id_to_label.values()
              ).difference(entity_restriction)
              if invert_entity_selection
              else entity_restriction
          )
          assert expected_entities.issuperset(present_entities)
  
      if relation_restriction is not None:
          present_relations = set(
              restricted_triples_factory.triples[:, 1]
          )
          expected_relations = (
              set(
                  original_triples_factory.relation_id_to_label.values()
              )
              if invert_relation_selection
              else set(relation_restriction)
          )
          assert expected_relations.issuperset(present_relations)
  
imports: import itertools
  import os
  import tempfile
  import unittest
  from contextlib import nullcontext
  from pathlib import Path
  from typing import Any, Collection, Iterable, Mapping, Optional, Tuple
  from unittest.mock import patch
  import numpy
  import pytest
  import torch
  from pykeen.datasets import Hetionet, Nations, SingleTabbedDataset
  from pykeen.datasets.nations import NATIONS_TRAIN_PATH
  from pykeen.triples import CoreTriplesFactory, LCWAInstances, TriplesFactory, TriplesNumericLiteralsFactory
  from pykeen.triples.splitting import splitter_resolver
  from pykeen.triples.triples_factory import INVERSE_SUFFIX, _map_triples_elements_to_ids, get_mapped_triples
  from pykeen.triples.utils import TRIPLES_DF_COLUMNS, load_triples
  from tests.constants import RESOURCES
  from tests.utils import needs_packages
label_desc: Use image node_id 6 for calling the TestTriplesFactory obj's underlying member method code with example usage: obj._test_restriction(original_triples_factory, entity_restriction, invert_entity_selection, relation_restriction, invert_relation_selection) without return types