Exemplo n.º 1
0
def create_tree(data, params):
    '''
	Create a faiss/cKDTree/KDTree/annoy/pynndescent index for nearest neighbour lookup. 
	All undescribed input as in ``bbknn.bbknn()``. Returns the resulting index.

	Input
	-----
	data : ``numpy.array``
		PCA coordinates of a batch's cells to index.
	params : ``dict``
		A dictionary of arguments used to call ``bbknn.matrix.bbknn()``, plus ['computation']
		storing the knn algorithm to use.
	'''
    if params['computation'] == 'annoy':
        ckd = AnnoyIndex(data.shape[1], metric=params['metric'])
        for i in np.arange(data.shape[0]):
            ckd.add_item(i, data[i, :])
        ckd.build(params['annoy_n_trees'])
    elif params['computation'] == 'pynndescent':
        ckd = pynndescent.NNDescent(
            data,
            metric=params['metric'],
            n_jobs=-1,
            n_neighbors=params['pynndescent_n_neighbors'],
            random_state=params['pynndescent_random_state'])
        ckd.prepare()
    elif params['computation'] == 'faiss':
        ckd = faiss.IndexFlatL2(data.shape[1])
        ckd.add(data)
    elif params['computation'] == 'cKDTree':
        ckd = cKDTree(data)
    elif params['computation'] == 'KDTree':
        ckd = KDTree(data, metric=params['metric'])
    return ckd