def writeXvecs(points, filename): if type(points) is not np.ndarray: raise Exception('Convert to numpy before serializing!') dim = points.shape[1] count = points.shape[0] extension = filename.strip().split('.')[-1] if extension == 'fvecs': points = yael.numpy_to_fvec(points.astype('float32')) yael.fvecs_write(filename, dim, count, points) elif extension == 'bvecs': file = open(filename, 'wb') #raise Exception('Writing of bvecs is not implemented yet!') dimData = struct.pack('i', dim) for i in xrange(count): point = points[i,:].astype('uint8') file.write(dimData) point.tofile(file) file.close() elif extension == 'i8vecs': file = open(filename, 'wb') dimData = struct.pack('i', dim) for i in xrange(count): point = points[i,:].astype('int8') file.write(dimData) point.tofile(file) file.close() elif extension == 'ivecs': points = yael.numpy_to_ivec(points) yael.ivecs_write(filename, dim, count, points) else: raise Exception('Bad file extension!')
def writeXvecs(points, filename): if type(points) is not np.ndarray: raise Exception('Convert to numpy before serializing!') dim = points.shape[1] count = points.shape[0] extension = filename.strip().split('.')[-1] if extension == 'fvecs': points = yael.numpy_to_fvec(points.astype('float32')) yael.fvecs_write(filename, dim, count, points) elif extension == 'bvecs': file = open(filename, 'wb') dimData = struct.pack('i', dim) for i in xrange(count): point = points[i, :].astype('uint8') file.write(dimData) point.tofile(file) file.close() elif extension == 'i8vecs': file = open(filename, 'wb') dimData = struct.pack('i', dim) for i in xrange(count): point = points[i, :].astype('int8') file.write(dimData) point.tofile(file) file.close() elif extension == 'ivecs': points = yael.numpy_to_ivec(points) yael.ivecs_write(filename, dim, count, points) else: raise Exception('Bad file extension!')
def writeXvecsToOpenFile(points, file, extension): if type(points) is not np.ndarray: raise Exception('Convert to numpy before serializing!') dim = points.shape[1] count = points.shape[0] if extension == 'fvecs': points = yael.numpy_to_fvec(points.astype('float32')) yael.fvecs_fwrite(file, dim, count, points) elif extension == 'bvecs': raise Exception('Writing of bvecs is not implemented yet!') elif extension == 'ivecs': points = yael.numpy_to_ivec(points) yael.ivecs_fwrite(file, dim, count, points) else: raise Exception('Bad file extension!')
numpy_a = np.array(list(range(5)), dtype='float32') print(numpy_a) print('-> Yael A') yael_a = yael.FloatArray.acquirepointer(yael.numpy_to_fvec(numpy_a)) n = numpy_a.size yael.fvec_print(yael_a, n) print('-> Numpy A') print(yael.fvec_to_numpy(yael_a, n)) print('int array') numpy_a = np.array(list(range(5)), dtype='int32') print(numpy_a) print('-> Yael A2') yael_a = yael.IntArray.acquirepointer(yael.numpy_to_ivec(numpy_a)) n = numpy_a.size yael.ivec_print(yael_a, n) print('-> Numpy A2') print(yael.ivec_to_numpy(yael_a, n)) print('float array, pass by reference') numpy_a = np.array(list(range(5)), dtype='float32') print(numpy_a) if '--force-crash' in sys.argv: yael_a = yael.FloatArray.acquirepointer(yael.numpy_to_fvec_ref(numpy_a)) n = numpy_a.size yael.fvec_print(yael_a, n) del numpy_a