BiocNeighbors 1.6.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] 9010 5592 1 6301 281
##
## [[2]]
## [1] 9730 2
##
## [[3]]
## [1] 3 4159 2401 399
##
## [[4]]
## [1] 1387 5187 4 8087 2829 3506 6744 7266 9690 9397 7898
##
## [[5]]
## [1] 9025 5114 3775 4043 2539 5
##
## [[6]]
## [1] 5583 9715 24 1035 3243 6476 5809 1041 2527 6 8846 2890
head(fout$distance)
## [[1]]
## [1] 0.9744489 0.9785377 0.0000000 0.9282055 0.9894275
##
## [[2]]
## [1] 0.9725043 0.0000000
##
## [[3]]
## [1] 0.0000000 0.9436101 0.9830841 0.9701612
##
## [[4]]
## [1] 0.9801942 0.7791897 0.0000000 0.8644223 0.9861242 0.9817542 0.9266104
## [8] 0.8778080 0.9793124 0.9588250 0.9551360
##
## [[5]]
## [1] 0.9704067 0.9612897 0.8589029 0.9660642 0.9188897 0.0000000
##
## [[6]]
## [1] 0.9439781 0.9466752 0.9966159 0.9631872 0.8561364 0.9920858 0.9949519
## [8] 0.9521762 0.9562355 0.0000000 0.9364563 0.8752932
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] 3 4159 2401 399
… with the following distances to those neighbors:
fout$distance[[3]]
## [1] 0.0000000 0.9436101 0.9830841 0.9701612
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.0.0 (2020-04-24)
## 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.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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.22.0 BiocNeighbors_1.6.0 knitr_1.28
## [4] BiocStyle_2.16.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.4.6 bookdown_0.18 lattice_0.20-41
## [4] digest_0.6.25 grid_4.0.0 stats4_4.0.0
## [7] magrittr_1.5 evaluate_0.14 rlang_0.4.5
## [10] stringi_1.4.6 S4Vectors_0.26.0 Matrix_1.2-18
## [13] rmarkdown_2.1 tools_4.0.0 stringr_1.4.0
## [16] parallel_4.0.0 xfun_0.13 yaml_2.2.1
## [19] compiler_4.0.0 BiocGenerics_0.34.0 BiocManager_1.30.10
## [22] htmltools_0.4.0