Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_dense_annoy_index(annoy_index_file):
data = np.random.choice([0, 1], size=(10, 5))
index = build_annoy_index(data, annoy_index_file)
assert os.path.exists(annoy_index_file)
loaded_index = AnnoyIndex(5, metric='angular')
loaded_index.load(annoy_index_file)
assert index.f == loaded_index.f == 5
assert index.get_n_items() == loaded_index.get_n_items() == 10
assert index.get_nns_by_item(0, 5) == loaded_index.get_nns_by_item(0, 5)
index.unload()
loaded_index.unload()
def test_build_sparse_annoy_index(annoy_index_file):
data = np.random.choice([0, 1], size=(10, 5))
sparse_data = csr_matrix(data)
index = build_annoy_index(sparse_data, annoy_index_file)
assert os.path.exists(annoy_index_file)
loaded_index = AnnoyIndex(5, metric='angular')
loaded_index.load(annoy_index_file)
assert index.f == loaded_index.f == 5
assert index.get_n_items() == loaded_index.get_n_items() == 10
assert index.get_nns_by_item(0, 5) == loaded_index.get_nns_by_item(0, 5)
index.unload()
loaded_index.unload()
def _fit(self, X, Y=None, shuffle_mode=True):
if self.annoy_index_path is None:
self.annoy_index_path = 'annoy.index'
if self.verbose > 0:
print('Building KNN index')
build_annoy_index(X, self.annoy_index_path,
ntrees=self.ntrees,
build_index_on_disk=self.build_index_on_disk,
verbose=self.verbose)
datagen = generator_from_index(X, Y,
index_path=self.annoy_index_path,
k=self.k,
batch_size=self.batch_size,
search_k=self.search_k,
precompute=self.precompute,
verbose=self.verbose)
loss_monitor = 'loss'
try:
triplet_loss_func = triplet_loss(distance=self.distance,
margin=self.margin)