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} )
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())
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})
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)
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)
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})
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())
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)
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.))
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)
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})
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)
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)
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.))
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)
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)