BiocNeighbors 1.14.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] 6578 1 8913 9258
##
## [[2]]
## [1] 2 9910
##
## [[3]]
## [1] 8596 7572 7799 3 5692 7704 9439 5123 6090 4715 4625 7435 2013 7649 3920
## [16] 8280 878 1581 3550 4116 7670 9136 1462 9387 3021 2429
##
## [[4]]
## [1] 2739 4
##
## [[5]]
## [1] 7087 18 2285 9210 7327 2024 301 1020 5624 5 2228 5184 3729 8676 6420
## [16] 2122
##
## [[6]]
## [1] 6 8454
head(fout$distance)
## [[1]]
## [1] 0.9098713 0.0000000 0.9780713 0.7144481
##
## [[2]]
## [1] 0.0000000 0.9792177
##
## [[3]]
## [1] 0.9772393 0.9486304 0.9821127 0.0000000 0.9806360 0.9755294 0.9719584
## [8] 0.9216687 0.9537114 0.9589865 0.9705510 0.8716648 0.9741709 0.9176796
## [15] 0.9192616 0.9724008 0.9766120 0.9705945 0.9790979 0.9568474 0.9833196
## [22] 0.9394096 0.9567834 0.8642942 0.9969049 0.8890249
##
## [[4]]
## [1] 0.9946219 0.0000000
##
## [[5]]
## [1] 0.9738206 0.8898706 0.9886069 0.9820319 0.9677893 0.9998204 0.8849032
## [8] 0.9991642 0.8963996 0.0000000 0.9701778 0.8929114 0.9869145 0.9913331
## [15] 0.9478709 0.8606500
##
## [[6]]
## [1] 0.0000000 0.8789111
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] 8596 7572 7799 3 5692 7704 9439 5123 6090 4715 4625 7435 2013 7649 3920
## [16] 8280 878 1581 3550 4116 7670 9136 1462 9387 3021 2429
… with the following distances to those neighbors:
fout$distance[[3]]
## [1] 0.9772393 0.9486304 0.9821127 0.0000000 0.9806360 0.9755294 0.9719584
## [8] 0.9216687 0.9537114 0.9589865 0.9705510 0.8716648 0.9741709 0.9176796
## [15] 0.9192616 0.9724008 0.9766120 0.9705945 0.9790979 0.9568474 0.9833196
## [22] 0.9394096 0.9567834 0.8642942 0.9969049 0.8890249
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.2.0 RC (2022-04-19 r82224)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] BiocParallel_1.30.0 BiocNeighbors_1.14.0 knitr_1.38
## [4] BiocStyle_2.24.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.8.3 magrittr_2.0.3 BiocGenerics_0.42.0
## [4] lattice_0.20-45 R6_2.5.1 rlang_1.0.2
## [7] fastmap_1.1.0 stringr_1.4.0 tools_4.2.0
## [10] parallel_4.2.0 grid_4.2.0 xfun_0.30
## [13] cli_3.3.0 jquerylib_0.1.4 htmltools_0.5.2
## [16] yaml_2.3.5 digest_0.6.29 bookdown_0.26
## [19] Matrix_1.4-1 BiocManager_1.30.17 S4Vectors_0.34.0
## [22] sass_0.4.1 evaluate_0.15 rmarkdown_2.14
## [25] stringi_1.7.6 compiler_4.2.0 bslib_0.3.1
## [28] stats4_4.2.0 jsonlite_1.8.0