Beispiel #1
0
    def test_save_cache_remove_from_index(self):
        # Test that the cache is updated appropriately on a removal.
        cache_element = DataMemoryElement()
        self.assertTrue(cache_element.is_empty())

        i = LinearHashIndex(cache_element)
        # noinspection PyTypeChecker
        i.build_index([[0, 1, 0],   # 2
                       [0, 1, 1],   # 3
                       [1, 0, 0],   # 4
                       [1, 1, 0]])  # 6
        self.assertFalse(cache_element.is_empty())
        self.assertSetEqual(
            set(numpy.load(BytesIO(cache_element.get_bytes()))),
            {2, 3, 4, 6}
        )

        # noinspection PyTypeChecker
        i.remove_from_index([[0, 1, 1],   # 3
                             [1, 0, 0]])  # 4
        self.assertFalse(cache_element.is_empty())
        self.assertSetEqual(
            set(numpy.load(BytesIO(cache_element.get_bytes()))),
            {2, 6}
        )
Beispiel #2
0
 def test_build_index_with_cache(self):
     cache_element = DataMemoryElement()
     i = LinearHashIndex(cache_element)
     # noinspection PyTypeChecker
     i.build_index([[0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 0, 1]])
     nose.tools.assert_equal(i.index, {1, 2, 3, 4})
     nose.tools.assert_false(cache_element.is_empty())
Beispiel #3
0
    def test_save_cache_remove_from_index(self):
        # Test that the cache is updated appropriately on a removal.
        cache_element = DataMemoryElement()
        self.assertTrue(cache_element.is_empty())

        i = LinearHashIndex(cache_element)
        # noinspection PyTypeChecker
        i.build_index([
            [0, 1, 0],  # 2
            [0, 1, 1],  # 3
            [1, 0, 0],  # 4
            [1, 1, 0]
        ])  # 6
        self.assertFalse(cache_element.is_empty())
        self.assertSetEqual(
            set(numpy.load(BytesIO(cache_element.get_bytes()))), {2, 3, 4, 6})

        # noinspection PyTypeChecker
        i.remove_from_index([
            [0, 1, 1],  # 3
            [1, 0, 0]
        ])  # 4
        self.assertFalse(cache_element.is_empty())
        self.assertSetEqual(
            set(numpy.load(BytesIO(cache_element.get_bytes()))), {2, 6})
Beispiel #4
0
    def test_save_cache(self):
        cache_element = DataMemoryElement()
        nose.tools.assert_true(cache_element.is_empty())

        i = LinearHashIndex(cache_element)
        # noinspection PyTypeChecker
        i.build_index([[0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 0, 1]])
        nose.tools.assert_false(cache_element.is_empty())
        nose.tools.assert_true(len(cache_element.get_bytes()) > 0)
Beispiel #5
0
 def test_build_index_no_cache(self):
     i = LinearHashIndex()
     # noinspection PyTypeChecker
     i.build_index([[0, 1, 0],
                    [1, 0, 0],
                    [0, 1, 1],
                    [0, 0, 1]])
     self.assertEqual(i.index, {1, 2, 3, 4})
     self.assertIsNone(i.cache_element)
Beispiel #6
0
 def test_update_index_add_hashes(self):
     i = LinearHashIndex()
     # Build index with some initial hashes
     # noinspection PyTypeChecker
     i.build_index([[0, 0], [0, 1]])
     self.assertSetEqual(i.index, {0, 1})
     # Update index with new stuff
     # noinspection PyTypeChecker
     i.update_index([[1, 0], [1, 1]])
     self.assertSetEqual(i.index, {0, 1, 2, 3})
Beispiel #7
0
 def test_build_index_with_cache(self):
     cache_element = DataMemoryElement()
     i = LinearHashIndex(cache_element)
     # noinspection PyTypeChecker
     i.build_index([[0, 1, 0],
                    [1, 0, 0],
                    [0, 1, 1],
                    [0, 0, 1]])
     self.assertEqual(i.index, {1, 2, 3, 4})
     self.assertFalse(cache_element.is_empty())
Beispiel #8
0
    def test_load_cache(self):
        cache_element = DataMemoryElement()
        i1 = LinearHashIndex(cache_element)
        # noinspection PyTypeChecker
        i1.build_index([[0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 0, 1]])

        # load called on initialization.
        i2 = LinearHashIndex(cache_element)

        nose.tools.assert_equal(i1.cache_element, i2.cache_element)
        nose.tools.assert_equal(i1.index, i2.index)
Beispiel #9
0
 def test_nn(self):
     i = LinearHashIndex()
     # noinspection PyTypeChecker
     i.build_index([[0, 1, 0], [1, 1, 0], [0, 1, 1], [0, 0, 1]])
     near_codes, near_dists = i.nn([0, 0, 0], 4)
     nose.tools.assert_equal(set(map(tuple, near_codes[:2])), {(0, 1, 0),
                                                               (0, 0, 1)})
     nose.tools.assert_equal(set(map(tuple, near_codes[2:])), {(1, 1, 0),
                                                               (0, 1, 1)})
     numpy.testing.assert_array_almost_equal(
         near_dists, (1 / 3., 1 / 3., 2 / 3., 2 / 3.))
Beispiel #10
0
    def test_save_cache_build_index(self):
        cache_element = DataMemoryElement()
        self.assertTrue(cache_element.is_empty())

        i = LinearHashIndex(cache_element)
        # noinspection PyTypeChecker
        i.build_index([[0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 0, 1]])
        self.assertFalse(cache_element.is_empty())
        # Check byte content
        expected_cache = {1, 2, 3, 4}
        actual_cache = set(numpy.load(BytesIO(cache_element.get_bytes())))
        self.assertSetEqual(expected_cache, actual_cache)
Beispiel #11
0
 def test_update_index_add_hashes(self):
     i = LinearHashIndex()
     # Build index with some initial hashes
     # noinspection PyTypeChecker
     i.build_index([[0, 0],
                    [0, 1]])
     self.assertSetEqual(i.index, {0, 1})
     # Update index with new stuff
     # noinspection PyTypeChecker
     i.update_index([[1, 0],
                     [1, 1]])
     self.assertSetEqual(i.index, {0, 1, 2, 3})
Beispiel #12
0
    def test_load_cache(self):
        cache_element = DataMemoryElement()
        i1 = LinearHashIndex(cache_element)
        # noinspection PyTypeChecker
        i1.build_index([[0, 1, 0],
                        [1, 0, 0],
                        [0, 1, 1],
                        [0, 0, 1]])

        # load called on initialization.
        i2 = LinearHashIndex(cache_element)

        self.assertEqual(i1.cache_element, i2.cache_element)
        self.assertEqual(i1.index, i2.index)
Beispiel #13
0
    def test_save_cache_build_index(self):
        cache_element = DataMemoryElement()
        self.assertTrue(cache_element.is_empty())

        i = LinearHashIndex(cache_element)
        # noinspection PyTypeChecker
        i.build_index([[0, 1, 0],
                       [1, 0, 0],
                       [0, 1, 1],
                       [0, 0, 1]])
        self.assertFalse(cache_element.is_empty())
        # Check byte content
        expected_cache = {1, 2, 3, 4}
        actual_cache = set(numpy.load(BytesIO(cache_element.get_bytes())))
        self.assertSetEqual(expected_cache, actual_cache)
Beispiel #14
0
 def test_nn(self):
     i = LinearHashIndex()
     # noinspection PyTypeChecker
     i.build_index([[0, 1, 0],
                    [1, 1, 0],
                    [0, 1, 1],
                    [0, 0, 1]])
     # noinspection PyTypeChecker
     near_codes, near_dists = i.nn([0, 0, 0], 4)
     self.assertEqual(set(map(tuple, near_codes[:2])),
                      {(0, 1, 0), (0, 0, 1)})
     self.assertEqual(set(map(tuple, near_codes[2:])),
                      {(1, 1, 0), (0, 1, 1)})
     numpy.testing.assert_array_almost_equal(near_dists,
                                             (1/3., 1/3., 2/3., 2/3.))
Beispiel #15
0
 def test_build_index_no_cache(self):
     i = LinearHashIndex()
     # noinspection PyTypeChecker
     i.build_index([[0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 0, 1]])
     nose.tools.assert_equal(i.index, {1, 2, 3, 4})
     nose.tools.assert_is_none(i.cache_element)
Beispiel #16
0
 def test_build_index_no_cache(self):
     i = LinearHashIndex()
     # noinspection PyTypeChecker
     i.build_index([[0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 0, 1]])
     self.assertEqual(i.index, {1, 2, 3, 4})
     self.assertIsNone(i.cache_element)