'Finds the neighbors within a given radius of a point or points.
Return the indices and distances of each point from the dataset
lying in a ball with size ``radius`` around the points of the query
array. Points lying on the boundary are included in the results.
The result points are *not* necessarily sorted by distance to their
query point.
Parameters
X : array-like, (n_samples, n_features), optional
The query point or points.
If not provided, neighbors of each indexed point are returned.
In this case, the query point is not considered its own neighbor.
radius : float
Limiting distance of neighbors to return.
(default is the value passed to the constructor).
return_distance : boolean, optional. Defaults to True.
If False, distances will not be returned
Returns
dist : array, shape (n_samples,) of arrays
Array representing the distances to each point, only present if
return_distance=True. The distance values are computed according
to the ``metric`` constructor parameter.
ind : array, shape (n_samples,) of arrays
An array of arrays of indices of the approximate nearest points
from the population matrix that lie within a ball of size
``radius`` around the query points.
Examples
In the following example, we construct a NeighborsClassifier
class from an array representing our data set and ask who\'s
the closest point to [1, 1, 1]:
>>> import numpy as np
>>> samples = [[0., 0., 0.], [0., .5, 0.], [1., 1., .5]]
>>> from sklearn.neighbors import NearestNeighbors
>>> neigh = NearestNeighbors(radius=1.6)
>>> neigh.fit(samples) # doctest: +ELLIPSIS
NearestNeighbors(algorithm=\'auto\', leaf_size=30, ...)
>>> rng = neigh.radius_neighbors([[1., 1., 1.]])
>>> print(np.asarray(rng[0][0])) # doctest: +ELLIPSIS
[ 1.5  0.5]
>>> print(np.asarray(rng[1][0])) # doctest: +ELLIPSIS
[1 2]
The first array returned contains the distances to all points which
are closer than 1.6, while the second array returned contains their
indices.  In general, multiple points can be queried at the same time.
Notes
Because the number of neighbors of each point is not necessarily
equal, the results for multiple query points cannot be fit in a
standard data array.
For efficiency, `radius_neighbors` returns arrays of objects, where
each object is a 1D array of indices or distances.'

def radius_neighbors(self, X=None, radius=None, return_distance=True):

    if (self._fit_method is None):
        raise NotFittedError('Must    fit    neighbors    before    querying.')
    if (X is not None):
        query_is_train = False
        X = check_array(X, accept_sparse='csr')
    else:
        query_is_train = True
        X = self._fit_X
    if (radius is None):
        radius = self.radius
    n_samples = X.shape[0]
    if (self._fit_method == 'brute'):
        if (self.effective_metric_ == 'euclidean'):
            dist = pairwise_distances(X, self._fit_X, 'euclidean', n_jobs=self.n_jobs, squared=True)
            radius *= radius
        else:
            dist = pairwise_distances(X, self._fit_X, self.effective_metric_, n_jobs=self.n_jobs, **self.effective_metric_params_)
        neigh_ind_list = [np.where((d <= radius))[0] for d in dist]
        neigh_ind = np.empty(n_samples, dtype='object')
        neigh_ind[:] = neigh_ind_list
        if return_distance:
            dist_array = np.empty(n_samples, dtype='object')
            if (self.effective_metric_ == 'euclidean'):
                dist_list = [np.sqrt(d[neigh_ind[i]]) for (i, d) in enumerate(dist)]
            else:
                dist_list = [d[neigh_ind[i]] for (i, d) in enumerate(dist)]
            dist_array[:] = dist_list
            results = (dist_array, neigh_ind)
        else:
            results = neigh_ind
    elif (self._fit_method in ['ball_tree', 'kd_tree']):
        if issparse(X):
            raise ValueError(("%s    does    not    work    with    sparse    matrices.    Densify    the    data,    or    set    algorithm='brute'" % self._fit_method))
        results = self._tree.query_radius(X, radius, return_distance=return_distance)
        if return_distance:
            results = results[::(-1)]
    else:
        raise ValueError('internal:    _fit_method    not    recognized')
    if (not query_is_train):
        return results
    else:
        if return_distance:
            (dist, neigh_ind) = results
        else:
            neigh_ind = results
        for (ind, ind_neighbor) in enumerate(neigh_ind):
            mask = (ind_neighbor != ind)
            neigh_ind[ind] = ind_neighbor[mask]
            if return_distance:
                dist[ind] = dist[ind][mask]
        if return_distance:
            return (dist, neigh_ind)
        return neigh_ind
