BiocNeighbors 1.0.0
The BiocNeighbors package provides an implementation of the Annoy (Approximate Nearest Neighbors Oh Yeah) method based on C++ code in the CRANpkg("RcppAnnoy")
package.
The aim is to provide an approximate method to complement the exact KMKNN algorithm described previously.
Indeed, it is straightforward to switch from one algorithm to another by simply changing the BNPARAM
argument in findKNN
and queryKNN
.
Briefly, the Annoy method works by building a tree where a random hyperplane partitions points into two child groups at each internal node. This is repeated to construct a forest of trees where the number of trees determines the accuracy of the search. Given a query data point, we identify all points in the same leaf node for each tree. We then take the union of leaf node sets across trees and search them exactly for the nearest neighbors.
We perform the k-nearest neighbors search with the Annoy algorithm by specifying BNPARAM=AnnoyParam()
.
nobs <- 10000
ndim <- 20
data <- matrix(runif(nobs*ndim), ncol=ndim)
fout <- findKNN(data, k=10, BNPARAM=AnnoyParam())
head(fout$index)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 7695 1634 5629 8820 1730 6713 6478 6163 2405 2105
## [2,] 8007 8912 935 9541 6330 7868 8292 4340 2628 4491
## [3,] 84 5856 7690 1882 7045 7455 8649 2202 2055 4505
## [4,] 5657 850 4148 2809 9902 7502 1075 1617 2671 6773
## [5,] 5736 9979 9732 196 5545 3792 4985 2728 1020 7822
## [6,] 7631 4537 4277 1753 8112 1619 8449 938 5385 7880
head(fout$distance)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1.0045509 1.0131420 1.0631733 1.1074907 1.1164587 1.1306081 1.1468254
## [2,] 0.8591070 0.8684837 0.8721814 0.9085152 0.9578577 0.9814332 0.9890503
## [3,] 0.9188849 0.9219431 0.9356326 0.9411376 0.9623136 0.9666600 0.9820961
## [4,] 0.8749467 1.0643432 1.0788826 1.0842352 1.1185161 1.1333669 1.1611062
## [5,] 0.8781183 1.0092605 1.0180496 1.0281823 1.0753893 1.1003991 1.1072947
## [6,] 0.9563148 0.9740315 1.0044955 1.0049956 1.0248549 1.0250293 1.0308086
## [,8] [,9] [,10]
## [1,] 1.161545 1.199764 1.2120571
## [2,] 1.005972 1.008241 1.0127877
## [3,] 0.987730 0.990676 0.9984636
## [4,] 1.169415 1.170677 1.1837236
## [5,] 1.117975 1.119320 1.1214956
## [6,] 1.043258 1.054139 1.0653089
We can also identify the k-nearest neighbors in one dataset based on query points in another dataset.
nquery <- 1000
ndim <- 20
query <- matrix(runif(nquery*ndim), ncol=ndim)
qout <- queryKNN(data, query, k=5, BNPARAM=AnnoyParam())
head(qout$index)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 9859 7221 3244 1027 3500
## [2,] 7966 248 5569 7348 2081
## [3,] 7860 3760 3429 6906 9747
## [4,] 4587 4136 7924 2151 2404
## [5,] 9658 6368 2496 582 9448
## [6,] 2344 8956 27 8032 7567
head(qout$distance)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.7800196 0.8143066 0.9631813 0.9779549 1.0072734
## [2,] 0.8939046 0.9016203 0.9096425 0.9469325 0.9473681
## [3,] 0.9409049 1.0304626 1.0745106 1.0978459 1.1103609
## [4,] 0.8281685 0.9051428 0.9474058 0.9857659 1.0205249
## [5,] 0.8582463 0.9386992 0.9425059 0.9973366 1.0147910
## [6,] 0.9268661 0.9460111 0.9808483 1.0184755 1.0645196
Most of the options described for the KMKNN algorithm 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.BNINDEX
to build the forest once for a given data set and re-use it across calls.The use of a pre-built BNINDEX
is illustrated below:
pre <- buildNNIndex(data, BNPARAM=AnnoyParam())
out1 <- findKNN(BNINDEX=pre, k=5)
out2 <- queryKNN(BNINDEX=pre, query=query, k=2)
Users are referred to the documentation of each function for specific details on the available arguments.
The forest of trees form an indexing structure that is saved to file.
By default, this file is located in tempdir()
1 On HPC file systems, you can change TEMPDIR
to a location that is more amenable to parallelized access. and will be removed when the session finishes.
AnnoyIndex_path(pre)
## [1] "/tmp/Rtmp9oS6a3/file148d082f831c.idx"
If the index is to persist across sessions, the path of the index file can be directly specified in buildNNIndex
.
However, this means that it becomes the responsibility of the user to clean up any temporary indexing files after calculations are complete.
sessionInfo()
## R version 3.5.1 Patched (2018-07-12 r74967)
## 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.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/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] BiocNeighbors_1.0.0 BiocParallel_1.16.0 knitr_1.20
## [4] BiocStyle_2.10.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.19 bookdown_0.7 digest_0.6.18
## [4] rprojroot_1.3-2 backports_1.1.2 stats4_3.5.1
## [7] magrittr_1.5 evaluate_0.12 stringi_1.2.4
## [10] S4Vectors_0.20.0 rmarkdown_1.10 tools_3.5.1
## [13] stringr_1.3.1 parallel_3.5.1 xfun_0.4
## [16] yaml_2.2.0 compiler_3.5.1 BiocGenerics_0.28.0
## [19] BiocManager_1.30.3 htmltools_0.3.6