Exemple #1
0
 def test_incompatible_different_configs_distribution(self):
   # S1 and S2 have different distribution sketch in config.
   s1 = AnySketch(
       SketchConfig([IndexSpecification(UniformDistribution(10), 'uniform')],
                    num_hashes=1,
                    value_functions=[SumFunction()]), 42)
   s2 = AnySketch(
       SketchConfig([IndexSpecification(UniformDistribution(11), 'uniform')],
                    num_hashes=1,
                    value_functions=[SumFunction()]), 42)
   with self.assertRaises(AssertionError):
     s1.assert_compatible(s2)
Exemple #2
0
 def test_incompatible_different_hashes(self):
   # S1 and S2 will have default and therefore different random seeds.
   s1 = AnySketch(
       SketchConfig([IndexSpecification(UniformDistribution(10), 'uniform')],
                    num_hashes=2,
                    value_functions=[SumFunction()]))
   s2 = AnySketch(
       SketchConfig([IndexSpecification(UniformDistribution(10), 'uniform')],
                    num_hashes=2,
                    value_functions=[SumFunction()]))
   with self.assertRaises(AssertionError):
     s1.assert_compatible(s2)
Exemple #3
0
 def test_run_through_compatible_sketches(self):
   # S1 and S2 are compatible.
   s1 = AnySketch(
       SketchConfig([IndexSpecification(UniformDistribution(10), 'uniform')],
                    num_hashes=1,
                    value_functions=[SumFunction()]), 42)
   s2 = AnySketch(
       SketchConfig([IndexSpecification(UniformDistribution(10), 'uniform')],
                    num_hashes=1,
                    value_functions=[SumFunction()]), 42)
   self.assertTrue(
       s1.assert_compatible(s2),
       'Two sketches are compatible, yet raises error.')
Exemple #4
0
 def test_basic_capabilities(self):
   s = AnySketch(
       SketchConfig([IndexSpecification(UniformDistribution(10), 'uniform')],
                    num_hashes=1,
                    value_functions=[SumFunction()]))
   s.add_ids([1, 1])
   self.assertIn(1, s)
   self.assertEqual(s.max_size(), 10)
   self.assertTrue(s.assert_compatible(s))
 def test_get_active_register_indices(self):
     s = AnySketch(
         SketchConfig(
             [IndexSpecification(UniformDistribution(2), 'uniform')],
             num_hashes=1,
             value_functions=[SumFunction()]), 42)
     s.add(1)
     expected = np.array([1])
     np.testing.assert_equal(s.get_active_register_indices(), expected)
 def test_get_active_register_indices_raises(self):
     s = AnySketch(
         SketchConfig([
             IndexSpecification(UniformDistribution(2), 'uniform'),
             IndexSpecification(UniformDistribution(4), 'uniform')
         ],
                      num_hashes=1,
                      value_functions=[SumFunction()]), 84)
     s.add(1)
     with self.assertRaises(AssertionError):
         s.get_active_register_indices()
Exemple #7
0
  def test_with_custom_hash_function(self):

    class TestHash(object):

      def __init__(self, seed):
        self.seed = seed

      def __call__(self, x):
        return 1

    s1 = AnySketch(
        SketchConfig([IndexSpecification(UniformDistribution(2), 'uniform'),
                      IndexSpecification(UniformDistribution(4), 'uniform')],
                     num_hashes=3,
                     value_functions=[SumFunction()]), 42, TestHash)
    s1.add(1)
    expected = np.array([[0, 0, 0, 0], [0, 3, 0, 0]])
    self.assertLen(s1.sketch, len(expected))
    for i, sketch in enumerate(expected):
      np.testing.assert_equal(s1.sketch[i], sketch)
Exemple #8
0
 def test_sum(self):
   sum_func = SumFunction()
   self.assertEqual(sum_func(1, 1), 2, 'SumFunction is not correct')