class_docstr: A test case for quickly defining common tests for samplers.
class_docstr_tok: ["A","test","case","for","quickly","defining","common","tests","for","samplers","."]
docstr: Test generating a negative sample.
docstr_tok: ["Test","generating","a","negative","sample","."]
code: def check_sample(self, instance: NegativeSampler) -> None:
      """Test generating a negative sample."""
      # Generate negative sample
      negative_batch, batch_filter = instance.sample(
          positive_batch=self.positive_batch
      )
  
      # check filter shape if necessary
      if instance.filterer is not None:
          assert batch_filter is not None
          assert batch_filter.shape == (
              self.batch_size,
              instance.num_negs_per_pos,
          )
          assert batch_filter.dtype == torch.bool
      else:
          assert batch_filter is None
  
      # check shape
      assert negative_batch.shape == (
          self.positive_batch.shape[0],
          instance.num_negs_per_pos,
          3,
      )
  
      # check bounds: heads
      assert _array_check_bounds(
          negative_batch[..., 0],
          low=0,
          high=self.triples_factory.num_entities,
      )
  
      # check bounds: relations
      assert _array_check_bounds(
          negative_batch[..., 1],
          low=0,
          high=self.triples_factory.num_relations,
      )
  
      # check bounds: tails
      assert _array_check_bounds(
          negative_batch[..., 2],
          low=0,
          high=self.triples_factory.num_entities,
      )
  
      if instance.filterer is not None:
          positive_batch = self.positive_batch.unsqueeze(dim=1).repeat(
              1, instance.num_negs_per_pos, 1
          )
          positive_batch = positive_batch[batch_filter]
          negative_batch = negative_batch[batch_filter]
  
          # test that the negative triple is not the original positive triple
          assert (negative_batch != positive_batch).any(dim=-1).all()
  
file_path: pykeen/tests/test_sampling/cases.py
imports: from typing import Any, MutableMapping
  import numpy
  import torch
  import unittest_templates
  from pykeen.datasets import Nations
  from pykeen.sampling import NegativeSampler
  from pykeen.sampling.filtering import BloomFilterer, PythonSetFilterer
  from pykeen.triples import Instances, TriplesFactory
label_desc: Use image node_id 3 for calling the NegativeSamplerGenericTestCase obj's underlying member method code with example usage: obj.check_sample(instance) without return types