|
1 | 1 | import os |
2 | 2 | import unittest |
3 | 3 |
|
| 4 | +import numpy as np |
4 | 5 |
|
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 |
111 | 7 |
|
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) |
118 | 8 |
|
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") |
127 | 13 |
|
| 14 | + np.random.seed(idx) |
| 15 | + dim = 16 |
| 16 | + num_elements = 10000 |
128 | 17 |
|
| 18 | + # Generating sample data |
| 19 | + data = np.float32(np.random.random((num_elements, dim))) |
129 | 20 |
|
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