# Detecting all neighbors within range

Aaron Lun1

1Cancer Research UK Cambridge Institute, Cambridge, United Kingdom

#### Revised: 28 September 2018

#### Package

BiocNeighbors 1.22.0

# 1 Identifying all neighbors within range

Another application of the KMKNN or VP tree algorithms is to identify all neighboring points within a certain distance1 1 1 The default here is Euclidean, but again, we can set `distance="Manhattan"` in the `BNPARAM` object if so desired. of the current point. We first mock up some data:

    nobs <- 10000
    ndim <- 20
    data <- matrix(runif(nobs*ndim), ncol=ndim)

We apply the `findNeighbors()` function to `data`:

    fout <- findNeighbors(data, threshold=1)
    head(fout$index)

    ## [[1]]
    ##  [1] 9096 6638 1316 5273 3917 5838 2861 7257 5903 9440 2881 7004    1 3044 1119
    ## [16] 9978 8071 3781 3108 7954 9977 2415  606 5245
    ##
    ## [[2]]
    ##  [1] 2945 8359  801 7223 5611 4258 4456 5214 5889    2
    ##
    ## [[3]]
    ## [1] 5940 6788 1556    3 4140
    ##
    ## [[4]]
    ## [1]  194 7710 5661 6281    4 8183
    ##
    ## [[5]]
    ## [1] 3872  518 3143    5 2432 3245
    ##
    ## [[6]]
    ## [1]  501 9935 9190    6

    head(fout$distance)

    ## [[1]]
    ##  [1] 0.9986447 0.9737673 0.9704686 0.9410031 0.9492621 0.9657446 0.9425821
    ##  [8] 0.9409706 0.9009410 0.9877590 0.8336302 0.8556853 0.0000000 0.7598298
    ## [15] 0.9878030 0.9310380 0.9853556 0.9952218 0.9797951 0.9373093 0.9863493
    ## [22] 0.9503523 0.9652413 0.9906854
    ##
    ## [[2]]
    ##  [1] 0.9353288 0.9768538 0.8929048 0.9655462 0.9879069 0.9977974 0.9662870
    ##  [8] 0.9988136 0.9917239 0.0000000
    ##
    ## [[3]]
    ## [1] 0.9930690 0.9935282 0.9194090 0.0000000 0.9946956
    ##
    ## [[4]]
    ## [1] 0.9985820 0.9880177 0.9951931 0.9244196 0.0000000 0.9821734
    ##
    ## [[5]]
    ## [1] 0.9092710 0.9715955 0.8917316 0.0000000 0.9817525 0.9561494
    ##
    ## [[6]]
    ## [1] 0.9245113 0.8790179 0.8978410 0.0000000

Each entry of the `index` list corresponds to a point in `data` and contains the row indices in `data` that are within `threshold`. For example, the 3rd point in `data` has the following neighbors:

    fout$index[[3]]

    ## [1] 5940 6788 1556    3 4140

… with the following distances to those neighbors:

    fout$distance[[3]]

    ## [1] 0.9930690 0.9935282 0.9194090 0.0000000 0.9946956

Note that, for this function, the reported neighbors are not sorted by distance. The order of the output is completely arbitrary and will vary depending on the random seed. However, the identity of the neighbors is fully deterministic.

# 2 Querying another data set for neighbors

The `queryNeighbors()` function is also provided for identifying all points within a certain distance of a query point. Given a query data set:

    nquery <- 1000
    ndim <- 20
    query <- matrix(runif(nquery*ndim), ncol=ndim)

… we apply the `queryNeighbors()` function:

    qout <- queryNeighbors(data, query, threshold=1)
    length(qout$index)

    ## [1] 1000

… where each entry of `qout$index` corresponds to a row of `query` and contains its neighbors in `data`. Again, the order of the output is arbitrary but the identity of the neighbors is deterministic.

# 3 Further options

Most of the options described for `findKNN()` are also applicable here. For example:

- `subset` to identify neighbors for a subset of points.
- `get.distance` to avoid retrieving distances when unnecessary.
- `BPPARAM` to parallelize the calculations across multiple workers.
- `raw.index` to return the raw indices from a precomputed index.

Note that the argument for a precomputed index is `precomputed`:

    pre <- buildIndex(data, BNPARAM=KmknnParam())
    fout.pre <- findNeighbors(BNINDEX=pre, threshold=1)
    qout.pre <- queryNeighbors(BNINDEX=pre, query=query, threshold=1)

Users are referred to the documentation of each function for specific details.

# 4 Session information

    sessionInfo()

    ## R version 4.4.0 beta (2024-04-15 r86425 ucrt)
    ## Platform: x86_64-w64-mingw32/x64
    ## Running under: Windows Server 2022 x64 (build 20348)
    ##
    ## Matrix products: default
    ##
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.utf8
    ## [3] LC_MONETARY=English_United States.utf8
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.utf8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ## [1] BiocParallel_1.38.0  BiocNeighbors_1.22.0 knitr_1.46
    ## [4] BiocStyle_2.32.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] cli_3.6.2           rlang_1.1.3         xfun_0.43
    ##  [4] jsonlite_1.8.8      S4Vectors_0.42.0    htmltools_0.5.8.1
    ##  [7] stats4_4.4.0        sass_0.4.9          rmarkdown_2.26
    ## [10] grid_4.4.0          evaluate_0.23       jquerylib_0.1.4
    ## [13] fastmap_1.1.1       yaml_2.3.8          lifecycle_1.0.4
    ## [16] bookdown_0.39       BiocManager_1.30.22 compiler_4.4.0
    ## [19] codetools_0.2-20    Rcpp_1.0.12         lattice_0.22-6
    ## [22] digest_0.6.35       R6_2.5.1            parallel_4.4.0
    ## [25] bslib_0.7.0         Matrix_1.7-0        tools_4.4.0
    ## [28] BiocGenerics_0.50.0 cachem_1.0.8