BiocNeighbors 1.2.0
Another application of the KMKNN or VP tree algorithms is to identify all neighboring points within a certain (Euclidean) distance 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] 8940 9226 7305 1 6154 7339 3426
##
## [[2]]
## [1] 1243 4157 2
##
## [[3]]
## [1] 9710 140 542 2746 490 7998 1569 3 2528 3527 1112
##
## [[4]]
## [1] 4 6321
##
## [[5]]
## [1] 3458 7555 3493 5 6849 3580 7594 608
##
## [[6]]
## [1] 6 2653 7262
head(fout$distance)
## [[1]]
## [1] 0.9645513 0.9381391 0.9948908 0.0000000 0.9960446 0.9682274 0.9299110
##
## [[2]]
## [1] 0.9937966 0.9970088 0.0000000
##
## [[3]]
## [1] 0.9675741 0.9962632 0.9702986 0.8966603 0.9910072 0.8635799 0.9619686
## [8] 0.0000000 0.9742349 0.9793722 0.9765054
##
## [[4]]
## [1] 0.000000 0.989131
##
## [[5]]
## [1] 0.9868350 0.9580350 0.9444660 0.0000000 0.9753243 0.8816974 0.9564750
## [8] 0.9190515
##
## [[6]]
## [1] 0.0000000 0.9830217 0.9894419
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] 9710 140 542 2746 490 7998 1569 3 2528 3527 1112
… with the following distances to those neighbors:
fout$distance[[3]]
## [1] 0.9675741 0.9962632 0.9702986 0.8966603 0.9910072 0.8635799 0.9619686
## [8] 0.0000000 0.9742349 0.9793722 0.9765054
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 3.6.0 (2019-04-26)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: OS X El Capitan 10.11.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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.18.0 BiocNeighbors_1.2.0 knitr_1.22
## [4] BiocStyle_2.12.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.1 bookdown_0.9 digest_0.6.18
## [4] stats4_3.6.0 magrittr_1.5 evaluate_0.13
## [7] stringi_1.4.3 S4Vectors_0.22.0 rmarkdown_1.12
## [10] tools_3.6.0 stringr_1.4.0 parallel_3.6.0
## [13] xfun_0.6 yaml_2.2.0 compiler_3.6.0
## [16] BiocGenerics_0.30.0 BiocManager_1.30.4 htmltools_0.3.6