# 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 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] 1 ## ## [[2]] ## [1] 9439 1629 4019 9730 2 ## ## [[3]] ## [1] 2094 5603 3 9525 ## ## [[4]] ## [1] 5916 2100 5078 1118 4 3046 ## ## [[5]] ## [1] 166 5 1477 5643 ## ## [[6]] ## [1] 4332 532 6 6600 head(fout$distance)
## [[1]]
## [1] 0
##
## [[2]]
## [1] 0.8990607 0.9530452 0.9805205 0.9698274 0.0000000
##
## [[3]]
## [1] 0.9386033 0.8241258 0.0000000 0.9191562
##
## [[4]]
## [1] 0.9806731 0.9122618 0.9720863 0.9845251 0.0000000 0.9889329
##
## [[5]]
## [1] 0.9412259 0.0000000 0.7904412 0.9392679
##
## [[6]]
## [1] 0.9823983 0.9278105 0.0000000 0.9995591

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] 2094 5603 3 9525 … with the following distances to those neighbors: fout$distance[[3]]
## [1] 0.9386033 0.8241258 0.0000000 0.9191562

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 Under development (unstable) (2022-10-25 r83175)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.1 LTS
##
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.17-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
##
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
##  [3] LC_TIME=en_GB              LC_COLLATE=C
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base
##
## other attached packages:
## [1] BiocParallel_1.33.4  BiocNeighbors_1.17.1 knitr_1.40
## [4] BiocStyle_2.27.0
##
## loaded via a namespace (and not attached):
##  [1] cli_3.4.1           rlang_1.0.6         xfun_0.34
##  [4] stringi_1.7.8       jsonlite_1.8.3      S4Vectors_0.37.0
##  [7] htmltools_0.5.3     stats4_4.3.0        sass_0.4.2
## [10] rmarkdown_2.18      grid_4.3.0          evaluate_0.18
## [13] jquerylib_0.1.4     fastmap_1.1.0       yaml_2.3.6
## [16] bookdown_0.30       BiocManager_1.30.19 stringr_1.4.1
## [19] compiler_4.3.0      codetools_0.2-18    Rcpp_1.0.9
## [22] lattice_0.20-45     digest_0.6.30       R6_2.5.1
## [25] parallel_4.3.0      magrittr_2.0.3      bslib_0.4.1
## [28] Matrix_1.5-1        tools_4.3.0         BiocGenerics_0.45.0
## [31] cachem_1.0.6