'Computes the cosine distance.
Distance is from the query to points in the candidates array.
Returns argsort of distances in the candidates
array and sorted distances.'

def _compute_distances(self, query, candidates):

    if (candidates.shape == (0,)):
        return (np.empty(0, dtype=np.int), np.empty(0, dtype=float))
    if sparse.issparse(self._fit_X):
        candidate_X = self._fit_X[candidates]
    else:
        candidate_X = self._fit_X.take(candidates, axis=0, mode='clip')
    distances = pairwise_distances(query, candidate_X, metric='cosine')[0]
    distance_positions = np.argsort(distances)
    distances = distances.take(distance_positions, mode='clip', axis=0)
    return (distance_positions, distances)
