forked from nmslib/nmslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nmslib.py
executable file
·472 lines (345 loc) · 14.4 KB
/
test_nmslib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/python
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import sys
import os
import time
import numpy as np
import nmslib
from common import *
MAX_PRINT_QTY=5
def read_data(fn):
with open(fn) as f:
for line in f:
yield [float(v) for v in line.strip().split()]
def read_data_fast(fn, sep='\t'):
import pandas
df = pandas.read_csv(fn, sep=sep, header=None)
return np.ascontiguousarray(df.as_matrix(), dtype=np.float32)
def read_data_fast_batch(fn, batch_size, sep='\t'):
import pandas
for df in pandas.read_csv(fn, sep=sep, header=None, chunksize=batch_size):
yield np.ascontiguousarray(df.as_matrix(), dtype=np.float32)
def read_sparse_data(fn):
with open(fn) as f:
for line in f:
yield [[i, float(v)] for i, v in enumerate(line.split()) if float(v) > 0]
def read_data_as_string(fn):
with open(fn) as f:
for line in f:
yield line.strip()
def test_vector_load(fast=True, fast_batch=True, seq=True):
space_type = 'cosinesimil'
space_param = []
method_name = 'small_world_rand'
index_name = method_name + '.index'
if os.path.isfile(index_name):
os.remove(index_name)
f = '/tmp/foo.txt'
if not os.path.isfile(f):
print 'creating %s' % f
np.savetxt(f, np.random.rand(100000,1000), delimiter="\t")
print 'done'
if fast:
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.DENSE_VECTOR,
nmslib.DistType.FLOAT)
with TimeIt('fast add data point'):
data = read_data_fast(f)
nmslib.addDataPointBatch(index, np.arange(len(data), dtype=np.int32), data)
nmslib.freeIndex(index)
if fast_batch:
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.DENSE_VECTOR,
nmslib.DistType.FLOAT)
with TimeIt('fast_batch add data point'):
offset = 0
for data in read_data_fast_batch(f, 10000):
nmslib.addDataPointBatch(index, np.arange(len(data), dtype=np.int32) + offset, data)
offset += data.shape[0]
print 'offset', offset
nmslib.freeIndex(index)
if seq:
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.DENSE_VECTOR,
nmslib.DistType.FLOAT)
with TimeIt('seq add data point'):
for id, data in enumerate(read_data(f)):
nmslib.addDataPoint(index, id, data)
nmslib.freeIndex(index)
def test_vector_fresh(fast=True):
space_type = 'cosinesimil'
space_param = []
method_name = 'small_world_rand'
index_name = method_name + '.index'
if os.path.isfile(index_name):
os.remove(index_name)
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.DENSE_VECTOR,
nmslib.DistType.FLOAT)
start = time.time()
if fast:
data = read_data_fast('sample_dataset.txt')
print 'data.shape', data.shape
positions = nmslib.addDataPointBatch(index, np.arange(len(data), dtype=np.int32), data)
else:
for id, data in enumerate(read_data('sample_dataset.txt')):
pos = nmslib.addDataPoint(index, id, data)
if id != pos:
print 'id %s != pos %s' % (id, pos)
sys.exit(1)
end = time.time()
print 'added data in %s secs' % (end - start)
print 'Let\'s print a few data entries'
print 'We have added %d data points' % nmslib.getDataPointQty(index)
print "Distance between points (0,0) " + str(nmslib.getDistance(index, 0, 0));
print "Distance between points (1,1) " + str(nmslib.getDistance(index, 1, 1));
print "Distance between points (0,1) " + str(nmslib.getDistance(index, 0, 1));
print "Distance between points (1,0) " + str(nmslib.getDistance(index, 1, 0));
for i in range(0,min(MAX_PRINT_QTY,nmslib.getDataPointQty(index))):
print nmslib.getDataPoint(index, i)
print 'Let\'s invoke the index-build process'
index_param = ['NN=17', 'initIndexAttempts=3', 'indexThreadQty=4']
query_time_param = ['initSearchAttempts=3']
nmslib.createIndex(index, index_param)
print 'The index is created'
nmslib.setQueryTimeParams(index,query_time_param)
print 'Query time parameters are set'
print "Results for the freshly created index:"
k = 3
start = time.time()
if fast:
num_threads = 10
query = read_data_fast('sample_queryset.txt')
res = nmslib.knnQueryBatch(index, num_threads, k, query)
for idx, v in enumerate(res):
print idx, v
else:
for idx, data in enumerate(read_data('sample_queryset.txt')):
print idx, nmslib.knnQuery(index, k, data)
end = time.time()
print 'querying done in %s secs' % (end - start)
nmslib.saveIndex(index, index_name)
print "The index %s is saved" % index_name
nmslib.freeIndex(index)
def test_vector_loaded():
space_type = 'cosinesimil'
space_param = []
method_name = 'small_world_rand'
index_name = method_name + '.index'
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.DENSE_VECTOR,
nmslib.DistType.FLOAT)
for id, data in enumerate(read_data('sample_dataset.txt')):
pos = nmslib.addDataPoint(index, id, data)
if id != pos:
print 'id %s != pos %s' % (id, pos)
sys.exit(1)
print 'Let\'s print a few data entries'
print 'We have added %d data points' % nmslib.getDataPointQty(index)
for i in range(0,min(MAX_PRINT_QTY,nmslib.getDataPointQty(index))):
print nmslib.getDataPoint(index,i)
print 'Let\'s invoke the index-build process'
query_time_param = ['initSearchAttempts=3']
nmslib.loadIndex(index, index_name)
print "The index %s is loaded" % index_name
nmslib.setQueryTimeParams(index,query_time_param)
print 'Query time parameters are set'
print "Results for the loaded index"
k = 2
for idx, data in enumerate(read_data('sample_queryset.txt')):
print idx, nmslib.knnQuery(index, k, data)
nmslib.freeIndex(index)
def gen_sparse_data():
n = 1000
q = 100
dim = 5000
data = np.random.binomial(1, 0.01, size=(n, dim))
print data.shape
np.savetxt('sample_sparse_dataset.txt', data, delimiter='\t')
query = np.random.binomial(1, 0.01, size=(q, dim))
print query.shape
np.savetxt('sample_sparse_queryset.txt', query, delimiter='\t')
def test_sparse_vector_fresh():
space_type = 'cosinesimil_sparse'
space_param = []
method_name = 'small_world_rand'
index_name = method_name + '_sparse.index'
if os.path.isfile(index_name):
os.remove(index_name)
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.SPARSE_VECTOR,
nmslib.DistType.FLOAT)
for id, data in enumerate(read_sparse_data('sample_sparse_dataset.txt')):
nmslib.addDataPoint(index, id, data)
print 'We have added %d data points' % nmslib.getDataPointQty(index)
for i in range(0,min(MAX_PRINT_QTY,nmslib.getDataPointQty(index))):
print nmslib.getDataPoint(index,i)
print 'Let\'s invoke the index-build process'
index_param = ['NN=17', 'initIndexAttempts=3', 'indexThreadQty=4']
query_time_param = ['initSearchAttempts=3']
nmslib.createIndex(index, index_param)
print 'The index is created'
nmslib.setQueryTimeParams(index,query_time_param)
print 'Query time parameters are set'
print "Results for the freshly created index:"
k = 3
for idx, data in enumerate(read_sparse_data('sample_sparse_queryset.txt')):
print idx, nmslib.knnQuery(index, k, data)
nmslib.saveIndex(index, index_name)
print "The index %s is saved" % index_name
nmslib.freeIndex(index)
def test_string_fresh(batch=True):
DATA_STRS = ["xyz", "beagcfa", "cea", "cb",
"d", "c", "bdaf", "ddcd",
"egbfa", "a", "fba", "bcccfe",
"ab", "bfgbfdc", "bcbbgf", "bfbb"
]
QUERY_STRS = ["abc", "def", "ghik"]
space_type = 'leven'
space_param = []
method_name = 'small_world_rand'
index_name = method_name + '.index'
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.OBJECT_AS_STRING,
nmslib.DistType.INT)
if batch:
print 'DATA_STRS', DATA_STRS
positions = nmslib.addDataPointBatch(index, np.arange(len(DATA_STRS), dtype=np.int32), DATA_STRS)
else:
for id, data in enumerate(DATA_STRS):
nmslib.addDataPoint(index, id, data)
print 'Let\'s print a few data entries'
print 'We have added %d data points' % nmslib.getDataPointQty(index)
print "Distance between points (0,0) " + str(nmslib.getDistance(index, 0, 0));
print "Distance between points (1,1) " + str(nmslib.getDistance(index, 1, 1));
print "Distance between points (0,1) " + str(nmslib.getDistance(index, 0, 1));
print "Distance between points (1,0) " + str(nmslib.getDistance(index, 1, 0));
for i in range(0,min(MAX_PRINT_QTY,nmslib.getDataPointQty(index))):
print nmslib.getDataPoint(index,i)
print 'Let\'s invoke the index-build process'
index_param = ['NN=17', 'initIndexAttempts=3', 'indexThreadQty=4']
query_time_param = ['initSearchAttempts=3']
nmslib.createIndex(index, index_param)
nmslib.setQueryTimeParams(index, query_time_param)
print 'Query time parameters are set'
print "Results for the freshly created index:"
k = 2
if batch:
num_threads = 10
res = nmslib.knnQueryBatch(index, num_threads, k, QUERY_STRS)
for idx, data in enumerate(QUERY_STRS):
res = nmslib.knnQuery(index, k, data)
print idx, data, res, [DATA_STRS[i] for i in res]
nmslib.saveIndex(index, index_name)
print "The index %s is saved" % index_name
nmslib.freeIndex(index)
def test_string_loaded():
DATA_STRS = ["xyz", "beagcfa", "cea", "cb",
"d", "c", "bdaf", "ddcd",
"egbfa", "a", "fba", "bcccfe",
"ab", "bfgbfdc", "bcbbgf", "bfbb"
]
QUERY_STRS = ["abc", "def", "ghik"]
space_type = 'leven'
space_param = []
method_name = 'small_world_rand'
index_name = method_name + '.index'
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.OBJECT_AS_STRING,
nmslib.DistType.INT)
for id, data in enumerate(DATA_STRS):
nmslib.addDataPoint(index, id, data)
print 'Let\'s print a few data entries'
print 'We have added %d data points' % nmslib.getDataPointQty(index)
for i in range(0,min(MAX_PRINT_QTY,nmslib.getDataPointQty(index))):
print nmslib.getDataPoint(index,i)
print 'Let\'s invoke the index-build process'
index_param = ['NN=17', 'initIndexAttempts=3', 'indexThreadQty=4']
query_time_param = ['initSearchAttempts=3']
nmslib.loadIndex(index, index_name)
print "The index %s is loaded" % index_name
nmslib.setQueryTimeParams(index, query_time_param)
print 'Query time parameters are set'
print "Results for the loaded index:"
k = 2
for idx, data in enumerate(QUERY_STRS):
print idx, nmslib.knnQuery(index, k, data)
nmslib.freeIndex(index)
def test_object_as_string_fresh(batch=True):
space_type = 'cosinesimil'
space_param = []
method_name = 'small_world_rand'
index_name = method_name + '.index'
if os.path.isfile(index_name):
os.remove(index_name)
index = nmslib.init(
space_type,
space_param,
method_name,
nmslib.DataType.OBJECT_AS_STRING,
nmslib.DistType.FLOAT)
if batch:
data = [s for s in read_data_as_string('sample_dataset.txt')]
positions = nmslib.addDataPointBatch(index, np.arange(len(data), dtype=np.int32), data)
else:
for id, data in enumerate(read_data_as_string('sample_dataset.txt')):
nmslib.addDataPoint(index, id, data)
print 'Let\'s print a few data entries'
print 'We have added %d data points' % nmslib.getDataPointQty(index)
for i in range(0,min(MAX_PRINT_QTY,nmslib.getDataPointQty(index))):
print nmslib.getDataPoint(index, i)
print 'Let\'s invoke the index-build process'
index_param = ['NN=17', 'initIndexAttempts=3', 'indexThreadQty=4']
query_time_param = ['initSearchAttempts=3']
nmslib.createIndex(index, index_param)
print 'The index is created'
nmslib.setQueryTimeParams(index,query_time_param)
print 'Query time parameters are set'
print "Results for the freshly created index:"
k = 3
for idx, data in enumerate(read_data_as_string('sample_queryset.txt')):
print idx, nmslib.knnQuery(index, k, data)
nmslib.saveIndex(index, index_name)
print "The index %s is saved" % index_name
nmslib.freeIndex(index)
if __name__ == '__main__':
print 'DENSE_VECTOR', nmslib.DataType.DENSE_VECTOR
print 'SPARSE_VECTOR', nmslib.DataType.SPARSE_VECTOR
print 'OBJECT_AS_STRING', nmslib.DataType.OBJECT_AS_STRING
print 'DistType.INT', nmslib.DistType.INT
print 'DistType.FLOAT', nmslib.DistType.FLOAT
test_vector_load()
test_vector_fresh()
test_vector_fresh(False)
test_vector_loaded()
gen_sparse_data()
test_sparse_vector_fresh()
test_string_fresh()
test_string_fresh(False)
test_string_loaded()
test_object_as_string_fresh()
test_object_as_string_fresh(False)