-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a test for distance computation correctness
- Loading branch information
1 parent
7c63a15
commit ac43973
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |