BiocNeighbors 1.12.0
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] 4510 8025 1592 1
##
## [[2]]
## [1] 2 4074 2533 5466
##
## [[3]]
## [1] 6435 8391 4836 9202 1574 5245 9264 9373 3 4713 266 1645 9290 7222 9224
## [16] 7421
##
## [[4]]
## [1] 4758 4
##
## [[5]]
## [1] 5 9797 4260 6593 5288 4199
##
## [[6]]
## [1] 9821 6
head(fout$distance)
## [[1]]
## [1] 0.7631488 0.8498893 0.9997440 0.0000000
##
## [[2]]
## [1] 0.0000000 0.9940036 0.9909404 0.9636897
##
## [[3]]
## [1] 0.9584132 0.9749533 0.9770898 0.9907993 0.8927744 0.9603591 0.8977261
## [8] 0.9893926 0.0000000 0.9853648 0.9523711 0.9703639 0.9716247 0.8835042
## [15] 0.9445512 0.9404080
##
## [[4]]
## [1] 0.9447714 0.0000000
##
## [[5]]
## [1] 0.0000000 0.9112288 0.9201811 0.9598179 0.9439583 0.9726507
##
## [[6]]
## [1] 0.8399847 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] 6435 8391 4836 9202 1574 5245 9264 9373 3 4713 266 1645 9290 7222 9224
## [16] 7421
… with the following distances to those neighbors:
fout$distance[[3]]
## [1] 0.9584132 0.9749533 0.9770898 0.9907993 0.8927744 0.9603591 0.8977261
## [8] 0.9893926 0.0000000 0.9853648 0.9523711 0.9703639 0.9716247 0.8835042
## [15] 0.9445512 0.9404080
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.
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.
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.
sessionInfo()
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
##
## 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
## [9] LC_ADDRESS=C LC_TELEPHONE=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.28.0 BiocNeighbors_1.12.0 knitr_1.36
## [4] BiocStyle_2.22.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 magrittr_2.0.1 BiocGenerics_0.40.0
## [4] lattice_0.20-45 R6_2.5.1 rlang_0.4.12
## [7] fastmap_1.1.0 stringr_1.4.0 tools_4.1.1
## [10] parallel_4.1.1 grid_4.1.1 xfun_0.27
## [13] jquerylib_0.1.4 htmltools_0.5.2 yaml_2.2.1
## [16] digest_0.6.28 bookdown_0.24 Matrix_1.3-4
## [19] BiocManager_1.30.16 S4Vectors_0.32.0 sass_0.4.0
## [22] evaluate_0.14 rmarkdown_2.11 stringi_1.7.5
## [25] compiler_4.1.1 bslib_0.3.1 stats4_4.1.1
## [28] jsonlite_1.7.2