--- title: Detecting approximate nearest neighbors with Annoy author: - name: Aaron Lun affiliation: Cancer Research UK Cambridge Institute, Cambridge, United Kingdom date: "Revised: 27 September 2018" output: BiocStyle::html_document: toc_float: true package: BiocNeighbors vignette: > %\VignetteIndexEntry{2. Detecting approximate nearest neighbors} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} bibliography: ref.bib --- ```{r, echo=FALSE, results="hide", message=FALSE} require(knitr) opts_chunk$set(error=FALSE, message=FALSE, warning=FALSE) library(BiocNeighbors) ``` # Introduction The `r Biocpkg("BiocNeighbors")` package provides an implementation of the [Annoy](https://github.com/spotify/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 `r Biocpkg("BiocNeighbors", vignette="kmknn.html", label="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. # Identifying nearest neighbors We perform the k-nearest neighbors search with the Annoy algorithm by specifying `BNPARAM=AnnoyParam()`. ```{r} nobs <- 10000 ndim <- 20 data <- matrix(runif(nobs*ndim), ncol=ndim) fout <- findKNN(data, k=10, BNPARAM=AnnoyParam()) head(fout$index) head(fout$distance) ``` We can also identify the k-nearest neighbors in one dataset based on query points in another dataset. ```{r} nquery <- 1000 ndim <- 20 query <- matrix(runif(nquery*ndim), ncol=ndim) qout <- queryKNN(data, query, k=5, BNPARAM=AnnoyParam()) head(qout$index) head(qout$distance) ``` # Further options 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: ```{r} 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. # Saving the index files The forest of trees form an indexing structure that is saved to file. By default, this file is located in `tempdir()`^[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. ```{r} AnnoyIndex_path(pre) ``` 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. # Session information ```{r} sessionInfo() ```