Skip to content

Commit

Permalink
add a test for distance computation correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
yurymalkov authored Oct 4, 2021
1 parent 7c63a15 commit ac43973
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions python_bindings/tests/bindings_test_spaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest

import numpy as np

import hnswlib

class RandomSelfTestCase(unittest.TestCase):
def testRandomSelf(self):

data1 = np.asarray([[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
])

for space, expected_distances in [
('l2', [[0., 1., 2., 2., 2.]]),
('ip', [[-2., -1., 0., 0., 0.]]),
('cosine', [[0, 1.835e-01, 4.23e-01, 4.23e-01, 4.23e-01]])]:

for rightdim in range(1, 128, 3):
for leftdim in range(1, 32, 5):
data2 = np.concatenate(
[np.zeros([data1.shape[0], leftdim]), data1, np.zeros([data1.shape[0], rightdim])], axis=1)
dim = data2.shape[1]
p = hnswlib.Index(space=space, dim=dim)
p.init_index(max_elements=5, ef_construction=100, M=16)

p.set_ef(10)

p.add_items(data2)

# Query the elements for themselves and measure recall:
labels, distances = p.knn_query(np.asarray(data2[-1:]), k=5)


diff=np.mean(np.abs(distances-expected_distances))
print(dim,space, diff)
self.assertAlmostEqual(diff, 0, delta=1e-3)

0 comments on commit ac43973

Please sign in to comment.