Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
query_time_param = ['efSearch=50']
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)
query_time_param = ['efSearch=50']
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)
print("Results for the freshly created index:")
with TimeIt('knn query'):
if batch:
num_threads = 10
res = nmslib.knnQueryBatch(index, num_threads, k, query_matrix)
for idx, v in enumerate(res):
if idx < 5:
print(idx, v)
if idx == 0:
for i in v:
print('q0', i, distance.cosine(q0, dataset[i,:]))
else:
for idx, data in enumerate(q):
res = nmslib.knnQuery(index, k, data)
if idx < 5:
print(idx, res)
nmslib.saveIndex(index, index_name)
print("The index %s is saved" % index_name)
nmslib.freeIndex(index)
index_param = ['NN=17', 'efConstruction=50', 'indexThreadQty=4']
query_time_param = ['efSearch=50']
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)
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 query(self, v, n):
return nmslib.knnQuery(self._index, n, v.tolist())