Skip to content

Commit 467c98f

Browse files
committed
Remove deprecated setup.py test
1 parent e94c5dc commit 467c98f

File tree

8 files changed

+176
-189
lines changed

8 files changed

+176
-189
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ install:
3434
3535
script:
3636
- |
37-
python setup.py test
37+
python -m unittest discover --start-directory python_bindings/tests --pattern "*_test*.py"

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ dist:
66
python3 setup.py sdist
77

88
test:
9-
python3 setup.py test
9+
python3 -m unittest discover --start-directory python_bindings/tests --pattern "*_test*.py"
1010

1111
clean:
1212
rm -rf *.egg-info build dist tmp var tests/__pycache__ hnswlib.cpython*.so
1313

14-
.PHONY: dist
14+
.PHONY: dist

python_bindings/tests/bindings_test.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22
import unittest
33

4+
import numpy as np
5+
6+
import hnswlib
7+
48

59
class RandomSelfTestCase(unittest.TestCase):
610
def testRandomSelf(self):
7-
import hnswlib
8-
import numpy as np
911

1012
dim = 16
1113
num_elements = 10000
@@ -41,7 +43,7 @@ def testRandomSelf(self):
4143

4244
# Query the elements for themselves and measure recall:
4345
labels, distances = p.knn_query(data1, k=1)
44-
self.assertAlmostEqual(np.mean(labels.reshape(-1) == np.arange(len(data1))),1.0,3)
46+
self.assertAlmostEqual(np.mean(labels.reshape(-1) == np.arange(len(data1))), 1.0, 3)
4547

4648
# Serializing and deleting the index:
4749
index_path = 'first_half.bin'
@@ -61,10 +63,6 @@ def testRandomSelf(self):
6163
# Query the elements for themselves and measure recall:
6264
labels, distances = p.knn_query(data, k=1)
6365

64-
self.assertAlmostEqual(np.mean(labels.reshape(-1) == np.arange(len(data))),1.0,3)
66+
self.assertAlmostEqual(np.mean(labels.reshape(-1) == np.arange(len(data))), 1.0, 3)
6567

6668
os.remove(index_path)
67-
68-
69-
if __name__ == "__main__":
70-
unittest.main()

python_bindings/tests/bindings_test_getdata.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import unittest
22

3+
import numpy as np
4+
5+
import hnswlib
6+
37

48
class RandomSelfTestCase(unittest.TestCase):
59
def testGettingItems(self):
610
print("\n**** Getting the data by label test ****\n")
7-
import hnswlib
8-
import numpy as np
911

1012
dim = 16
1113
num_elements = 10000
@@ -42,6 +44,3 @@ def testGettingItems(self):
4244
# After adding them, all labels should be retrievable
4345
returned_items = p.get_items(labels)
4446
self.assertSequenceEqual(data.tolist(), returned_items)
45-
46-
if __name__ == "__main__":
47-
unittest.main()
Lines changed: 118 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,127 @@
11
import os
22
import unittest
33

4+
import numpy as np
45

5-
class RandomSelfTestCase(unittest.TestCase):
6-
def testRandomSelf(self):
7-
for idx in range(16):
8-
print("\n**** Index save-load test ****\n")
9-
import hnswlib
10-
import numpy as np
11-
12-
np.random.seed(idx)
13-
dim = 16
14-
num_elements = 10000
15-
16-
# Generating sample data
17-
data = np.float32(np.random.random((num_elements, dim)))
18-
19-
# Declaring index
20-
p = hnswlib.Index(space='l2', dim=dim) # possible options are l2, cosine or ip
21-
22-
# Initing index
23-
# max_elements - the maximum number of elements, should be known beforehand
24-
# (probably will be made optional in the future)
25-
#
26-
# ef_construction - controls index search speed/build speed tradeoff
27-
# M - is tightly connected with internal dimensionality of the data
28-
# stronlgy affects the memory consumption
29-
30-
p.init_index(max_elements = num_elements, ef_construction = 100, M = 16)
31-
32-
# Controlling the recall by setting ef:
33-
# higher ef leads to better accuracy, but slower search
34-
p.set_ef(100)
35-
36-
p.set_num_threads(4) # by default using all available cores
37-
38-
# We split the data in two batches:
39-
data1 = data[:num_elements // 2]
40-
data2 = data[num_elements // 2:]
41-
42-
print("Adding first batch of %d elements" % (len(data1)))
43-
p.add_items(data1)
44-
45-
# Query the elements for themselves and measure recall:
46-
labels, distances = p.knn_query(data1, k=1)
47-
48-
items=p.get_items(labels)
49-
50-
# Check the recall:
51-
self.assertAlmostEqual(np.mean(labels.reshape(-1) == np.arange(len(data1))),1.0,3)
52-
53-
# Check that the returned element data is correct:
54-
diff_with_gt_labels=np.mean(np.abs(data1-items))
55-
self.assertAlmostEqual(diff_with_gt_labels, 0, delta = 1e-4)
56-
57-
# Serializing and deleting the index.
58-
# We need the part to check that serialization is working properly.
59-
60-
index_path = 'first_half.bin'
61-
print("Saving index to '%s'" % index_path)
62-
p.save_index(index_path)
63-
print("Saved. Deleting...")
64-
del p
65-
print("Deleted")
66-
67-
print("\n**** Mark delete test ****\n")
68-
# Reiniting, loading the index
69-
print("Reiniting")
70-
p = hnswlib.Index(space='l2', dim=dim)
71-
72-
print("\nLoading index from '%s'\n" % index_path)
73-
p.load_index(index_path)
74-
p.set_ef(100)
75-
76-
print("Adding the second batch of %d elements" % (len(data2)))
77-
p.add_items(data2)
78-
79-
# Query the elements for themselves and measure recall:
80-
labels, distances = p.knn_query(data, k=1)
81-
items=p.get_items(labels)
82-
83-
# Check the recall:
84-
self.assertAlmostEqual(np.mean(labels.reshape(-1) == np.arange(len(data))),1.0,3)
85-
86-
# Check that the returned element data is correct:
87-
diff_with_gt_labels=np.mean(np.abs(data-items))
88-
self.assertAlmostEqual(diff_with_gt_labels, 0, delta = 1e-4) # deleting index.
89-
90-
# Checking that all labels are returned correctly:
91-
sorted_labels=sorted(p.get_ids_list())
92-
self.assertEqual(np.sum(~np.asarray(sorted_labels)==np.asarray(range(num_elements))),0)
93-
94-
# Delete data1
95-
labels1, _ = p.knn_query(data1, k=1)
96-
97-
for l in labels1:
98-
p.mark_deleted(l[0])
99-
labels2, _ = p.knn_query(data2, k=1)
100-
items=p.get_items(labels2)
101-
diff_with_gt_labels=np.mean(np.abs(data2-items))
102-
self.assertAlmostEqual(diff_with_gt_labels, 0, delta = 1e-3) # console
103-
104-
105-
labels1_after, _ = p.knn_query(data1, k=1)
106-
for la in labels1_after:
107-
for lb in labels1:
108-
if la[0] == lb[0]:
109-
self.assertTrue(False)
110-
print("All the data in data1 are removed")
6+
import hnswlib
1117

112-
# checking saving/loading index with elements marked as deleted
113-
del_index_path = "with_deleted.bin"
114-
p.save_index(del_index_path)
115-
p = hnswlib.Index(space='l2', dim=dim)
116-
p.load_index(del_index_path)
117-
p.set_ef(100)
1188

119-
labels1_after, _ = p.knn_query(data1, k=1)
120-
for la in labels1_after:
121-
for lb in labels1:
122-
if la[0] == lb[0]:
123-
self.assertTrue(False)
124-
125-
os.remove(index_path)
126-
os.remove(del_index_path)
9+
class RandomSelfTestCase(unittest.TestCase):
10+
def testRandomSelf(self):
11+
for idx in range(16):
12+
print("\n**** Index save-load test ****\n")
12713

14+
np.random.seed(idx)
15+
dim = 16
16+
num_elements = 10000
12817

18+
# Generating sample data
19+
data = np.float32(np.random.random((num_elements, dim)))
12920

130-
if __name__ == "__main__":
131-
unittest.main()
21+
# Declaring index
22+
p = hnswlib.Index(space='l2', dim=dim) # possible options are l2, cosine or ip
23+
24+
# Initing index
25+
# max_elements - the maximum number of elements, should be known beforehand
26+
# (probably will be made optional in the future)
27+
#
28+
# ef_construction - controls index search speed/build speed tradeoff
29+
# M - is tightly connected with internal dimensionality of the data
30+
# stronlgy affects the memory consumption
31+
32+
p.init_index(max_elements=num_elements, ef_construction=100, M=16)
33+
34+
# Controlling the recall by setting ef:
35+
# higher ef leads to better accuracy, but slower search
36+
p.set_ef(100)
37+
38+
p.set_num_threads(4) # by default using all available cores
39+
40+
# We split the data in two batches:
41+
data1 = data[:num_elements // 2]
42+
data2 = data[num_elements // 2:]
43+
44+
print("Adding first batch of %d elements" % (len(data1)))
45+
p.add_items(data1)
46+
47+
# Query the elements for themselves and measure recall:
48+
labels, distances = p.knn_query(data1, k=1)
49+
50+
items=p.get_items(labels)
51+
52+
# Check the recall:
53+
self.assertAlmostEqual(np.mean(labels.reshape(-1) == np.arange(len(data1))), 1.0, 3)
54+
55+
# Check that the returned element data is correct:
56+
diff_with_gt_labels=np.mean(np.abs(data1-items))
57+
self.assertAlmostEqual(diff_with_gt_labels, 0, delta=1e-4)
58+
59+
# Serializing and deleting the index.
60+
# We need the part to check that serialization is working properly.
61+
62+
index_path = 'first_half.bin'
63+
print("Saving index to '%s'" % index_path)
64+
p.save_index(index_path)
65+
print("Saved. Deleting...")
66+
del p
67+
print("Deleted")
68+
69+
print("\n**** Mark delete test ****\n")
70+
# Reiniting, loading the index
71+
print("Reiniting")
72+
p = hnswlib.Index(space='l2', dim=dim)
73+
74+
print("\nLoading index from '%s'\n" % index_path)
75+
p.load_index(index_path)
76+
p.set_ef(100)
77+
78+
print("Adding the second batch of %d elements" % (len(data2)))
79+
p.add_items(data2)
80+
81+
# Query the elements for themselves and measure recall:
82+
labels, distances = p.knn_query(data, k=1)
83+
items=p.get_items(labels)
84+
85+
# Check the recall:
86+
self.assertAlmostEqual(np.mean(labels.reshape(-1) == np.arange(len(data))), 1.0, 3)
87+
88+
# Check that the returned element data is correct:
89+
diff_with_gt_labels=np.mean(np.abs(data-items))
90+
self.assertAlmostEqual(diff_with_gt_labels, 0, delta=1e-4) # deleting index.
91+
92+
# Checking that all labels are returned correctly:
93+
sorted_labels=sorted(p.get_ids_list())
94+
self.assertEqual(np.sum(~np.asarray(sorted_labels) == np.asarray(range(num_elements))), 0)
95+
96+
# Delete data1
97+
labels1, _ = p.knn_query(data1, k=1)
98+
99+
for l in labels1:
100+
p.mark_deleted(l[0])
101+
labels2, _ = p.knn_query(data2, k=1)
102+
items=p.get_items(labels2)
103+
diff_with_gt_labels = np.mean(np.abs(data2-items))
104+
self.assertAlmostEqual(diff_with_gt_labels, 0, delta=1e-3) # console
105+
106+
labels1_after, _ = p.knn_query(data1, k=1)
107+
for la in labels1_after:
108+
for lb in labels1:
109+
if la[0] == lb[0]:
110+
self.assertTrue(False)
111+
print("All the data in data1 are removed")
112+
113+
# checking saving/loading index with elements marked as deleted
114+
del_index_path = "with_deleted.bin"
115+
p.save_index(del_index_path)
116+
p = hnswlib.Index(space='l2', dim=dim)
117+
p.load_index(del_index_path)
118+
p.set_ef(100)
119+
120+
labels1_after, _ = p.knn_query(data1, k=1)
121+
for la in labels1_after:
122+
for lb in labels1:
123+
if la[0] == lb[0]:
124+
self.assertTrue(False)
125+
126+
os.remove(index_path)
127+
os.remove(del_index_path)

0 commit comments

Comments
 (0)