1
+ import unittest
2
+
3
+
4
+ class RandomSelfTestCase (unittest .TestCase ):
5
+ def testGettingItems (self ):
6
+ import hnswlib
7
+ import numpy as np
8
+
9
+ dim = 16
10
+ num_elements = 10000
11
+
12
+ # Generating sample data
13
+ data = np .float32 (np .random .random ((num_elements , dim )))
14
+ labels = np .arange (0 , num_elements )
15
+
16
+ # Declaring index
17
+ p = hnswlib .Index (space = 'l2' , dim = dim ) # possible options are l2, cosine or ip
18
+
19
+ # Initing index
20
+ # max_elements - the maximum number of elements, should be known beforehand
21
+ # (probably will be made optional in the future)
22
+ #
23
+ # ef_construction - controls index search speed/build speed tradeoff
24
+ # M - is tightly connected with internal dimensionality of the data
25
+ # stronlgy affects the memory consumption
26
+
27
+ p .init_index (max_elements = num_elements , ef_construction = 100 , M = 16 )
28
+
29
+ # Controlling the recall by setting ef:
30
+ # higher ef leads to better accuracy, but slower search
31
+ p .set_ef (300 )
32
+
33
+ p .set_num_threads (4 ) # by default using all available cores
34
+
35
+ # Before adding anything, getting any labels should fail
36
+ self .assertRaises (Exception , lambda : p .get_items (labels ))
37
+
38
+ print ("Adding all elements (%d)" % (len (data )))
39
+ p .add_items (data , labels )
40
+
41
+ # After adding them, all labels should be retrievable
42
+ returned_items = p .get_items (labels )
43
+ self .assertSequenceEqual (data .tolist (), returned_items )
44
+
45
+ if __name__ == "__main__" :
46
+ unittest .main ()
0 commit comments